Skip to content

Commit d5207bd

Browse files
committed
feat(chat): Add explanation for tool invocation for fsWrite and executeBash
1 parent 7d74ab8 commit d5207bd

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

packages/core/src/codewhispererChat/controllers/chat/messenger/messenger.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import { TabType } from '../../../../amazonq/webview/ui/storages/tabsStorage'
6161
import { ToolType, ToolUtils } from '../../../tools/toolUtils'
6262
import { ChatStream } from '../../../tools/chatStream'
6363
import path from 'path'
64-
import { CommandValidation } from '../../../tools/executeBash'
64+
import { CommandValidation, ExecuteBashParams } from '../../../tools/executeBash'
6565
import { extractErrorInfo } from '../../../../shared/utilities/messageUtil'
6666
import { noWriteTools, tools } from '../../../constants'
6767
import { Change } from 'diff'
@@ -306,12 +306,28 @@ export class Messenger {
306306
}
307307
const tool = ToolUtils.tryFromToolUse(toolUse)
308308
if ('type' in tool) {
309+
let explanation: string | undefined = undefined
309310
let changeList: Change[] | undefined = undefined
310311
let messageIdToUpdate: string | undefined = undefined
311312
const isReadOrList: boolean = [ToolType.FsRead, ToolType.ListDirectory].includes(
312313
tool.type
313314
)
314-
if (tool.type === ToolType.FsWrite) {
315+
if (tool.type === ToolType.ExecuteBash) {
316+
const input = toolUse.input as unknown as ExecuteBashParams
317+
if (input.explanation) {
318+
getLogger().debug(
319+
`Tool explanation: ${input.explanation} for executeBash toolUseId: ${toolUse.toolUseId}`
320+
)
321+
explanation = input.explanation
322+
}
323+
} else if (tool.type === ToolType.FsWrite) {
324+
const input = toolUse.input as unknown as FsWriteParams
325+
if (input.explanation) {
326+
getLogger().debug(
327+
`Tool explanation: ${input.explanation} for fsWrite toolUseId: ${toolUse.toolUseId}`
328+
)
329+
explanation = input.explanation
330+
}
315331
session.setShowDiffOnFileWrite(true)
316332
changeList = await tool.tool.getDiffChanges()
317333
}
@@ -353,7 +369,8 @@ export class Messenger {
353369
true,
354370
validation,
355371
isReadOrList,
356-
changeList
372+
changeList,
373+
explanation
357374
)
358375
await ToolUtils.queueDescription(tool, chatStream)
359376
if (session.messageIdToUpdate === undefined && tool.type === ToolType.FsRead) {

packages/core/src/codewhispererChat/tools/chatStream.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class ChatStream extends Writable {
3232
private readonly validation: CommandValidation,
3333
private readonly isReadorList: boolean,
3434
private readonly changeList?: Change[],
35+
private readonly explanation?: string,
3536
private readonly logger = getLogger('chatStream')
3637
) {
3738
super()
@@ -41,7 +42,10 @@ export class ChatStream extends Writable {
4142
if (!emitEvent) {
4243
return
4344
}
44-
if (validation.requiresAcceptance) {
45+
if (this.explanation) {
46+
this.messenger.sendDirectiveMessage(tabID, triggerID, this.explanation)
47+
}
48+
if (validation.requiresAcceptance && this.toolUse?.name === 'executeBash') {
4549
this.messenger.sendDirectiveMessage(
4650
tabID,
4751
triggerID,

packages/core/src/codewhispererChat/tools/executeBash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export const mutateCommandWarningMessage = 'Mutation command:\n\n'
113113
export interface ExecuteBashParams {
114114
command: string
115115
cwd?: string
116+
explanation?: string
116117
}
117118

118119
export interface CommandValidation {

packages/core/src/codewhispererChat/tools/fsWrite.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Change, diffLines } from 'diff'
1212

1313
interface BaseParams {
1414
path: string
15+
explanation?: string
1516
}
1617

1718
export interface CreateParams extends BaseParams {

packages/core/src/codewhispererChat/tools/tool_index.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
"inputSchema": {
6666
"type": "object",
6767
"properties": {
68+
"explanation": {
69+
"description": "One sentence explanation as to why this tool is being used, and how it contributes to the goal.",
70+
"type": "string"
71+
},
6872
"command": {
6973
"type": "string",
7074
"description": "Bash command to execute"
@@ -83,10 +87,6 @@
8387
"inputSchema": {
8488
"type": "object",
8589
"properties": {
86-
"explanation": {
87-
"description": "One sentence explanation as to why this tool is being used, and how it contributes to the goal.",
88-
"type": "string"
89-
},
9090
"path": {
9191
"type": "string",
9292
"description": "Absolute path to a directory, e.g., `/repo`."

0 commit comments

Comments
 (0)