Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,27 @@ describe('AgenticChatController', () => {

assert.strictEqual(await memoryBankFlagForPrompt('Create a memory bank'), false)
})

it('removes invisible characters from the file ranking request without escaping HTML', async () => {
sinon
.stub(MemoryBankController.prototype, 'prepareComprehensiveMemoryBankPrompt')
.callsFake(async (_workspaceFolderUri, rankFiles) => {
await rankFiles('Rank src/a&b\u00AD.ts \u{1F600}\u{E0100}\u{E0158} src/<c>.ts\u{E0041}')
return 'Write the memory bank files'
})

await chatController.onChatPrompt(
{ tabId: mockTabId, prompt: { prompt: 'Create a memory bank' } },
mockCancellationToken
)

sinon.assert.calledOnce(sendMessageStub)
const rankingRequest: SendMessageCommandInput = sendMessageStub.firstCall.firstArg
assert.strictEqual(
rankingRequest.conversationState?.currentMessage?.userInputMessage?.content,
'Rank src/a&b.ts \u{1F600} src/<c>.ts'
)
})
})

it('read all the response streams and return compiled results', async () => {
Expand Down Expand Up @@ -663,6 +684,69 @@ describe('AgenticChatController', () => {
assert.strictEqual(requestInput.conversationState?.conversationId, session.conversationId)
})

it('removes variation selectors from the prompt and keeps emoji', async () => {
chatController.onTabAdd({ tabId: mockTabId })

await chatController.onChatPrompt(
{
tabId: mockTabId,
prompt: { prompt: 'Fix the \u26A0\uFE0F warning \u{1F600}\u{E0100}\u{E0158}\u{E01EF}\uFE01' },
},
mockCancellationToken
)

const requestInput: GenerateAssistantResponseCommandInput = generateAssistantResponseStub.firstCall.firstArg
assert.strictEqual(
requestInput.conversationState?.currentMessage?.userInputMessage?.content,
'Fix the \u26A0\uFE0F warning \u{1F600}'
)
})

it('removes variation selectors from the chat history in the request', async () => {
chatDbInitializedStub.returns(true)
getMessagesStub.returns([
{
type: 'prompt',
body: 'Previous question \u2764\uFE0F\u{E0100}\u{E0158}',
userInputMessageContext: { toolResults: [] },
},
{ type: 'answer', body: 'Previous answer\u{E01EF}\uFE01' },
])

await chatController.onChatPrompt({ tabId: mockTabId, prompt: { prompt: 'Hello' } }, mockCancellationToken)

const requestInput: GenerateAssistantResponseCommandInput = generateAssistantResponseStub.firstCall.firstArg
const history = requestInput.conversationState?.history
assert.strictEqual(history?.[0].userInputMessage?.content, 'Previous question \u2764\uFE0F')
assert.strictEqual(history?.[1].assistantResponseMessage?.content, 'Previous answer')
})

it('removes variation selectors from the chat history in the compaction request', async () => {
chatDbInitializedStub.returns(true)
getMessagesStub.returns([
{
type: 'prompt',
body: 'Previous question \u2764\uFE0F\u{E0100}\u{E0158}',
userInputMessageContext: { toolResults: [] },
},
{ type: 'answer', body: 'Previous answer\u{E01EF}\uFE01' },
])

await chatController.onChatPrompt(
{ tabId: mockTabId, prompt: { prompt: '', command: '/compact' } },
mockCancellationToken
)

const requestInput: GenerateAssistantResponseCommandInput = generateAssistantResponseStub.firstCall.firstArg
const history = requestInput.conversationState?.history
assert.strictEqual(
requestInput.conversationState?.currentMessage?.userInputMessage?.content,
constants.COMPACTION_PROMPT
)
assert.strictEqual(history?.[0].userInputMessage?.content, 'Previous question \u2764\uFE0F')
assert.strictEqual(history?.[1].assistantResponseMessage?.content, 'Previous answer')
})

it('invokes IdleWorkspaceManager recordActivityTimestamp', async () => {
const recordActivityTimestampStub = sinon.stub(IdleWorkspaceManager, 'recordActivityTimestamp')

Expand Down Expand Up @@ -2365,6 +2449,26 @@ describe('AgenticChatController', () => {
assert.deepStrictEqual(chatResult, new ResponseError(LSPErrorCodes.RequestFailed, 'invalid state'))
})

it('removes invisible characters from the prompt and the document text without escaping HTML', async () => {
testFeatures.openDocument(
TextDocument.create('file:///inline.ts', 'typescript', 1, 'const a = 1\u{E0041}\u{E0100}\u{E0158}\n')
)

await chatController.onInlineChatPrompt(
{
prompt: { prompt: 'Make a < b \u26A0\uFE0F\u{E0041}\u{E0100}\uFE01' },
textDocument: { uri: 'file:///inline.ts' },
cursorState: [{ position: { line: 0, character: 0 } }],
},
mockCancellationToken
)

const requestInput: GenerateAssistantResponseCommandInput = generateAssistantResponseStub.firstCall.firstArg
const userInputMessage = requestInput.conversationState?.currentMessage?.userInputMessage
assert.strictEqual(userInputMessage?.content, 'Make a < b \u26A0\uFE0F')
assert.strictEqual(userInputMessage?.userInputMessageContext?.editorState?.document?.text, 'const a = 1\n')
})

describe('#extractDocumentContext', () => {
const typescriptDocument = TextDocument.create('file:///test.ts', 'typescript', 1, 'test')
let extractDocumentContextStub: sinon.SinonStub
Expand Down Expand Up @@ -5513,6 +5617,37 @@ ${' '.repeat(8)}}
})
})

// A result stream and a session for the processToolUses tests below
function makeStream() {
return {
removeResultBlockAndUpdateUI: sinon.stub().resolves(),
writeResultBlock: sinon.stub().resolves(1),
overwriteResultBlock: sinon.stub().resolves(),
removeResultBlock: sinon.stub().resolves(),
getMessageBlockId: sinon.stub().returns(undefined),
hasMessage: sinon.stub().returns(false),
updateOngoingProgressResult: sinon.stub().resolves(),
getResult: sinon.stub().returns({ messageId: 'test', body: '' }),
setMessageIdToUpdateForTool: sinon.stub(),
getMessageIdToUpdateForTool: sinon.stub().returns(undefined),
addMessageOperation: sinon.stub(),
getMessageOperation: sinon.stub().returns(undefined),
}
}

function makeSession() {
const session: any = {
toolUseLookup: new Map(),
pairProgrammingMode: true,
approvedPaths: new Set(),
conversationId: 'conv',
modelId: undefined,
getConversationType: () => 'AgenticChat',
setDeferredToolExecution: sinon.stub(),
}
return session
}

describe('processToolUses with an MCP tool named after a built-in tool', () => {
// An MCP server advertises a tool called `fsRead`. Registration namespaces it
// to `probe___fsRead`, but the controller still receives the server's
Expand All @@ -5536,36 +5671,6 @@ ${' '.repeat(8)}}
}
}

function makeStream() {
return {
removeResultBlockAndUpdateUI: sinon.stub().resolves(),
writeResultBlock: sinon.stub().resolves(1),
overwriteResultBlock: sinon.stub().resolves(),
removeResultBlock: sinon.stub().resolves(),
getMessageBlockId: sinon.stub().returns(undefined),
hasMessage: sinon.stub().returns(false),
updateOngoingProgressResult: sinon.stub().resolves(),
getResult: sinon.stub().returns({ messageId: 'test', body: '' }),
setMessageIdToUpdateForTool: sinon.stub(),
getMessageIdToUpdateForTool: sinon.stub().returns(undefined),
addMessageOperation: sinon.stub(),
getMessageOperation: sinon.stub().returns(undefined),
}
}

function makeSession() {
const session: any = {
toolUseLookup: new Map(),
pairProgrammingMode: true,
approvedPaths: new Set(),
conversationId: 'conv',
modelId: undefined,
getConversationType: () => 'AgenticChat',
setDeferredToolExecution: sinon.stub(),
}
return session
}

// Cards written to the stream, excluding the `explanation` directive that
// precedes the confirmation card.
function toolCards(stub: sinon.SinonStub) {
Expand Down Expand Up @@ -5735,6 +5840,73 @@ ${' '.repeat(8)}}
})
})

describe('processToolUses with an MCP tool whose property names have invisible characters', () => {
const SERVER = 'notes'
const TOOL = 'save_note'
const REGISTERED = `${SERVER}___${TOOL}`
// U+200C is part of the name, as in words of some scripts. The request gives the model "notetitle".
const PROPERTY = 'note\u200Ctitle'
const inputSchema = {
type: 'object',
properties: {
[PROPERTY]: { type: 'string' },
explanation: { type: 'string', description: 'One sentence explanation' },
},
required: [PROPERTY],
}

beforeEach(() => {
;(testFeatures.agent.getTools as sinon.SinonStub).returns([
{
toolSpecification: {
name: REGISTERED,
description: 'Saves a note',
inputSchema: { json: inputSchema },
},
},
])
mcpInstanceStub.get(() => ({
getAllTools: () => [{ serverName: SERVER, toolName: TOOL, description: 'Saves a note', inputSchema }],
getOriginalToolNames: (name: string) =>
name === REGISTERED ? { serverName: SERVER, toolName: TOOL } : undefined,
requiresApproval: () => false,
clearToolNameMapping: () => {},
setToolNameMapping: () => {},
getToolNameMapping: () => new Map([[REGISTERED, { serverName: SERVER, toolName: TOOL }]]),
}))
})

it('runs the tool with the property names of its input schema', async () => {
const runToolStub = testFeatures.agent.runTool as sinon.SinonStub
runToolStub.resolves({ output: { kind: 'text', content: 'saved' } })
const toolUse = {
toolUseId: 'note-id',
name: REGISTERED,
input: { notetitle: 'Plan', explanation: 'Save the plan' },
stop: true,
}

const results = await chatController.processToolUses(
[toolUse],
makeStream() as any,
makeSession(),
'tabId',
mockCancellationToken
)

assert.deepStrictEqual(
results.filter(result => result.status === ToolResultStatus.ERROR),
[],
'no tool result is an error'
)
sinon.assert.calledOnce(runToolStub)
assert.strictEqual(runToolStub.firstCall.args[0], REGISTERED)
assert.deepStrictEqual(runToolStub.firstCall.args[1], { [PROPERTY]: 'Plan' })
// The conversation keeps the input as the model wrote it
assert.deepStrictEqual(toolUse.input, { notetitle: 'Plan' })
})
})

describe('onFileClicked workspace containment', () => {
// Chat file-list cards carry paths that originate in model tool results,
// so a click must not open arbitrary files. A real temp directory stands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Will be deleted or merged.
*/

import * as crypto from 'crypto'

Check warning on line 6 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test (Windows)

Do not import Node.js builtin module "crypto"

Check warning on line 6 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test

Do not import Node.js builtin module "crypto"
import * as path from 'path'

Check warning on line 7 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test (Windows)

Do not import Node.js builtin module "path"

Check warning on line 7 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test

Do not import Node.js builtin module "path"
import * as os from 'os'

Check warning on line 8 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test (Windows)

Do not import Node.js builtin module "os"

Check warning on line 8 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test

Do not import Node.js builtin module "os"
import {
ChatTriggerType,
Origin,
Expand Down Expand Up @@ -129,6 +129,7 @@
isNullish,
getOriginFromClientInfo,
getClientName,
restorePropertyNames,
sanitizeInput,
sanitizeRequestInput,
} from '../../shared/utils'
Expand Down Expand Up @@ -1064,7 +1065,9 @@
},
}

const response = await client.sendMessage(requestInput)
// The prompt lists workspace file paths. Remove invisible characters, as for the
// other chat requests.
const response = await client.sendMessage(sanitizeRequestInput(requestInput))

let responseContent = ''
const maxResponseSize = 50000 // 50KB limit
Expand Down Expand Up @@ -2372,6 +2375,14 @@
}

if (originalNames) {
// The request gives the model the property names of the tool's input schema in their
// sanitized form (sanitizeRequestInput). The tool runs with the names of its schema.
const inputSchema = this.#features.agent
.getTools({ format: 'bedrock' })
.find(tool => tool.toolSpecification.name === toolUse.name)?.toolSpecification
.inputSchema?.json
executionInput = restorePropertyNames(toolUse.input as object, inputSchema)

const { serverName, toolName } = originalNames
let def
try {
Expand Down Expand Up @@ -4221,6 +4232,8 @@
ChatTriggerType.INLINE_CHAT,
this.#customizationArn
)
// Remove invisible characters from the whole request, as for the agent loop and compaction requests
requestInput = sanitizeRequestInput(requestInput)

if (!this.#serviceManager) {
throw new Error('amazonQServiceManager is not initialized')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3345,3 +3345,49 @@ describe('Always Allow persists across a restart', () => {
expect(mgr.requiresApproval('srv', 'tool1')).to.be.false
})
})

describe('tool descriptions from an MCP server', () => {
const fakeHome = '/home/testuser'

afterEach(async () => {
sinon.restore()
try {
await McpManager.instance.close()
} catch {}
})

it('removes variation selectors and keeps emoji', async () => {
stubAgentConfig()
const mgr = await McpManager.init([], {
...features,
workspace: { ...fakeWorkspace, fs: { ...fakeWorkspace.fs, getUserHomeDir: () => fakeHome } },
lsp: {
window: { showMessageRequest: sinon.stub(), showDocument: sinon.stub().resolves({ success: true }) },
},
} as any)
// Loads the stubbed agent config, which has no servers, and sets up tool permissions
await mgr.discoverAllServers()
await mgr.setWorkspaceTrust('trusted')
// The client is not connected to a process, so the stubbed tool list is what the server returns
sinon.stub(Client.prototype, 'connect').resolves()
sinon.stub(Client.prototype, 'listTools').resolves({
tools: [
{
name: 'read_notes',
description: 'Reads notes \u26A0\uFE0F\u{E0100}\u{E0158}\u{E01EF}\uFE01',
inputSchema: { type: 'object' },
},
],
} as any)
const cfg: MCPServerConfig = {
command: 'node',
args: [],
__configPath__: mcpUtils.getGlobalMcpConfigPath(fakeHome),
}

await (mgr as any).initOneServerInternal('svc', cfg)

const tool = mgr.getAllTools().find(t => t.toolName === 'read_notes')
expect(tool?.description).to.equal('Reads notes \u26A0\uFE0F')
})
})
Loading
Loading