Skip to content

Commit 7297025

Browse files
authored
Merge branch 'feature/agentic-chat' into revert-reading
2 parents 60a7fc6 + 4e0659c commit 7297025

File tree

13 files changed

+250
-8
lines changed

13 files changed

+250
-8
lines changed

packages/core/src/amazonq/webview/ui/apps/cwChatConnector.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,14 @@ export class Connector extends BaseConnector {
355355

356356
if (
357357
!this.onChatAnswerUpdated ||
358-
!['accept-code-diff', 'reject-code-diff', 'run-shell-command', 'reject-shell-command'].includes(action.id)
358+
![
359+
'accept-code-diff',
360+
'reject-code-diff',
361+
'run-shell-command',
362+
'reject-shell-command',
363+
'confirm-tool-use',
364+
'reject-tool-use',
365+
].includes(action.id)
359366
) {
360367
return
361368
}
@@ -413,6 +420,28 @@ export class Connector extends BaseConnector {
413420
},
414421
}
415422
break
423+
case 'confirm-tool-use':
424+
answer.header = {
425+
icon: 'shell' as MynahIconsType,
426+
body: 'shell',
427+
status: {
428+
icon: 'ok' as MynahIconsType,
429+
text: 'Accepted',
430+
status: 'success',
431+
},
432+
}
433+
break
434+
case 'reject-tool-use':
435+
answer.header = {
436+
icon: 'shell' as MynahIconsType,
437+
body: 'shell',
438+
status: {
439+
icon: 'cancel' as MynahIconsType,
440+
text: 'Rejected',
441+
status: 'error',
442+
},
443+
}
444+
break
416445
default:
417446
break
418447
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,12 @@ export class ChatController {
837837

838838
const session = this.sessionStorage.getSession(message.tabID!)
839839
const currentToolUse = session.toolUseWithError?.toolUse
840-
if (currentToolUse && currentToolUse.name === ToolType.ExecuteBash) {
840+
if (
841+
currentToolUse &&
842+
(currentToolUse.name === ToolType.ExecuteBash ||
843+
currentToolUse.name === ToolType.FsRead ||
844+
currentToolUse.name === ToolType.ListDirectory)
845+
) {
841846
session.toolUseWithError.error = new Error('Tool use was rejected by the user.')
842847
} else {
843848
getLogger().error(
@@ -853,6 +858,7 @@ export class ChatController {
853858
break
854859
case 'run-shell-command':
855860
case 'generic-tool-execution':
861+
case 'confirm-tool-use':
856862
await this.processToolUseMessage(message)
857863
if (message.action.id === 'run-shell-command' && message.action.text === 'Run') {
858864
this.telemetryHelper.recordInteractionWithAgenticChat(
@@ -870,6 +876,7 @@ export class ChatController {
870876
this.telemetryHelper.recordInteractionWithAgenticChat(AgenticChatInteractionType.RejectDiff, message)
871877
break
872878
case 'reject-shell-command':
879+
case 'reject-tool-use':
873880
await this.rejectShellCommand(message)
874881
await this.processToolUseMessage(message)
875882
break

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,13 +693,13 @@ export class Messenger {
693693
const buttons: ChatItemButton[] = [
694694
{
695695
id: 'reject-shell-command',
696-
text: localize('AWS.amazonq.executeBash.reject', 'Reject'),
696+
text: localize('AWS.generic.reject', 'Reject'),
697697
status: 'clear',
698698
icon: 'cancel' as MynahIconsType,
699699
},
700700
{
701701
id: 'run-shell-command',
702-
text: localize('AWS.amazonq.executeBash.run', 'Run'),
702+
text: localize('AWS.generic.run', 'Run'),
703703
status: 'clear',
704704
icon: 'play' as MynahIconsType,
705705
},
@@ -744,6 +744,28 @@ export class Messenger {
744744
buttons,
745745
fileList,
746746
}
747+
} else if (toolUse?.name === ToolType.ListDirectory || toolUse?.name === ToolType.FsRead) {
748+
if (validation.requiresAcceptance) {
749+
const buttons: ChatItemButton[] = [
750+
{
751+
id: 'confirm-tool-use',
752+
text: localize('AWS.generic.run', 'Run'),
753+
status: 'main',
754+
icon: 'play' as MynahIconsType,
755+
},
756+
{
757+
id: 'reject-tool-use',
758+
text: localize('AWS.generic.reject', 'Reject'),
759+
status: 'clear',
760+
icon: 'cancel' as MynahIconsType,
761+
},
762+
]
763+
header = {
764+
icon: 'shell' as MynahIconsType,
765+
body: 'shell',
766+
buttons,
767+
}
768+
}
747769
}
748770

749771
this.dispatcher.sendChatMessage(

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { fs } from '../../shared/fs/fs'
99
import { ChildProcess, ChildProcessOptions } from '../../shared/utilities/processUtils'
1010
import { InvokeOutput, OutputKind, sanitizePath } from './toolShared'
1111
import { split } from 'shlex'
12+
import path from 'path'
13+
import * as vscode from 'vscode'
14+
import { isInDirectory } from '../../shared/filesystemUtilities'
1215

1316
export enum CommandCategory {
1417
ReadOnly,
@@ -185,6 +188,27 @@ export class ExecuteBash {
185188
return { requiresAcceptance: true }
186189
}
187190

191+
// For each command, validate arguments for path safety within workspace
192+
for (const arg of cmdArgs) {
193+
if (this.looksLikePath(arg)) {
194+
// If not absolute, resolve using workingDirectory if available.
195+
let fullPath = arg
196+
if (!path.isAbsolute(arg) && this.workingDirectory) {
197+
fullPath = path.join(this.workingDirectory, arg)
198+
}
199+
const workspaceFolders = vscode.workspace.workspaceFolders
200+
if (!workspaceFolders || workspaceFolders.length === 0) {
201+
return { requiresAcceptance: true, warning: destructiveCommandWarningMessage }
202+
}
203+
const isInWorkspace = workspaceFolders.some((folder) =>
204+
isInDirectory(folder.uri.fsPath, fullPath)
205+
)
206+
if (!isInWorkspace) {
207+
return { requiresAcceptance: true, warning: destructiveCommandWarningMessage }
208+
}
209+
}
210+
}
211+
188212
const command = cmdArgs[0]
189213
const category = commandCategories.get(command)
190214

@@ -371,4 +395,8 @@ export class ExecuteBash {
371395
updates.write('```shell\n' + this.command + '\n```')
372396
updates.end()
373397
}
398+
399+
private looksLikePath(arg: string): boolean {
400+
return arg.startsWith('/') || arg.startsWith('./') || arg.startsWith('../')
401+
}
374402
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import * as vscode from 'vscode'
66
import { getLogger } from '../../shared/logger/logger'
77
import fs from '../../shared/fs/fs'
8-
import { InvokeOutput, OutputKind, sanitizePath } from './toolShared'
98
import { Writable } from 'stream'
9+
import path from 'path'
10+
import { InvokeOutput, OutputKind, sanitizePath, CommandValidation } from './toolShared'
11+
import { isInDirectory } from '../../shared/filesystemUtilities'
1012

1113
export interface FsReadParams {
1214
path: string
@@ -51,6 +53,18 @@ export class FsRead {
5153
updates.end()
5254
}
5355

56+
public requiresAcceptance(): CommandValidation {
57+
const workspaceFolders = vscode.workspace.workspaceFolders
58+
if (!workspaceFolders || workspaceFolders.length === 0) {
59+
return { requiresAcceptance: true }
60+
}
61+
const isInWorkspace = workspaceFolders.some((folder) => isInDirectory(folder.uri.fsPath, this.fsPath))
62+
if (!isInWorkspace) {
63+
return { requiresAcceptance: true }
64+
}
65+
return { requiresAcceptance: false }
66+
}
67+
5468
public async invoke(updates?: Writable): Promise<InvokeOutput> {
5569
try {
5670
const fileUri = vscode.Uri.file(this.fsPath)

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import * as vscode from 'vscode'
66
import { getLogger } from '../../shared/logger/logger'
77
import { readDirectoryRecursively } from '../../shared/utilities/workspaceUtils'
88
import fs from '../../shared/fs/fs'
9-
import { InvokeOutput, OutputKind, sanitizePath } from './toolShared'
109
import { Writable } from 'stream'
1110
import path from 'path'
11+
import { InvokeOutput, OutputKind, sanitizePath, CommandValidation } from './toolShared'
12+
import { isInDirectory } from '../../shared/filesystemUtilities'
1213

1314
export interface ListDirectoryParams {
1415
path: string
@@ -61,6 +62,18 @@ export class ListDirectory {
6162
updates.end()
6263
}
6364

65+
public requiresAcceptance(): CommandValidation {
66+
const workspaceFolders = vscode.workspace.workspaceFolders
67+
if (!workspaceFolders || workspaceFolders.length === 0) {
68+
return { requiresAcceptance: true }
69+
}
70+
const isInWorkspace = workspaceFolders.some((folder) => isInDirectory(folder.uri.fsPath, this.fsPath))
71+
if (!isInWorkspace) {
72+
return { requiresAcceptance: true }
73+
}
74+
return { requiresAcceptance: false }
75+
}
76+
6477
public async invoke(updates?: Writable): Promise<InvokeOutput> {
6578
try {
6679
const fileUri = vscode.Uri.file(this.fsPath)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ export function sanitizePath(inputPath: string): string {
3232
}
3333
return sanitized
3434
}
35+
36+
export interface CommandValidation {
37+
requiresAcceptance: boolean
38+
warning?: string
39+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ export class ToolUtils {
4040
static requiresAcceptance(tool: Tool): CommandValidation {
4141
switch (tool.type) {
4242
case ToolType.FsRead:
43-
return { requiresAcceptance: false }
43+
return tool.tool.requiresAcceptance()
4444
case ToolType.FsWrite:
4545
return { requiresAcceptance: false }
4646
case ToolType.ExecuteBash:
4747
return tool.tool.requiresAcceptance()
4848
case ToolType.ListDirectory:
49-
return { requiresAcceptance: false }
49+
return tool.tool.requiresAcceptance()
5050
}
5151
}
5252

packages/core/src/shared/localizedText.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export const invalidArn = localize('AWS.error.invalidArn', 'Invalid ARN')
1818
export const localizedDelete = localize('AWS.generic.delete', 'Delete')
1919
export const cancel = localize('AWS.generic.cancel', 'Cancel')
2020
export const help = localize('AWS.generic.help', 'Help')
21+
export const run = localize('AWS.generic.run', 'Run')
22+
export const reject = localize('AWS.generic.reject', 'Reject')
2123
export const invalidNumberWarning = localize('AWS.validateTime.error.invalidNumber', 'Input must be a positive number')
2224
export const viewDocs = localize('AWS.generic.viewDocs', 'View Documentation')
2325
export const recentlyUsed = localize('AWS.generic.recentlyUsed', 'recently used')

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { strict as assert } from 'assert'
77
import sinon from 'sinon'
88
import { destructiveCommandWarningMessage, ExecuteBash } from '../../../codewhispererChat/tools/executeBash'
99
import { ChildProcess } from '../../../shared/utilities/processUtils'
10+
import * as vscode from 'vscode'
1011

1112
describe('ExecuteBash Tool', () => {
1213
let runStub: sinon.SinonStub
@@ -114,4 +115,55 @@ describe('ExecuteBash Tool', () => {
114115

115116
assert.strictEqual(invokeStub.callCount, 1)
116117
})
118+
119+
it('requires acceptance if the command references an absolute file path outside the workspace', () => {
120+
// Stub workspace folders to simulate a workspace at '/workspace/folder'
121+
const workspaceStub = sinon
122+
.stub(vscode.workspace, 'workspaceFolders')
123+
.value([{ uri: { fsPath: '/workspace/folder' } } as any])
124+
// Command references an absolute path outside the workspace
125+
const execBash = new ExecuteBash({ command: 'cat /not/in/workspace/file.txt', cwd: '/workspace/folder' })
126+
const result = execBash.requiresAcceptance()
127+
128+
assert.equal(
129+
result.requiresAcceptance,
130+
true,
131+
'Should require acceptance for an absolute path outside of workspace'
132+
)
133+
workspaceStub.restore()
134+
})
135+
136+
it('does NOT require acceptance if the command references a relative file path inside the workspace', () => {
137+
// Stub workspace folders to simulate a workspace at '/workspace/folder'
138+
const workspaceStub = sinon
139+
.stub(vscode.workspace, 'workspaceFolders')
140+
.value([{ uri: { fsPath: '/workspace/folder' } } as any])
141+
142+
// Command references a relative path that resolves within the workspace
143+
const execBash = new ExecuteBash({ command: 'cat ./file.txt', cwd: '/workspace/folder' })
144+
const result = execBash.requiresAcceptance()
145+
146+
assert.equal(result.requiresAcceptance, false, 'Relative path inside workspace should not require acceptance')
147+
148+
workspaceStub.restore()
149+
})
150+
151+
it('does NOT require acceptance if there is no path-like token in the command', () => {
152+
// Stub workspace folders (even though they are not used in this case)
153+
const workspaceStub = sinon
154+
.stub(vscode.workspace, 'workspaceFolders')
155+
.value([{ uri: { fsPath: '/workspace/folder' } } as any])
156+
157+
// Command with tokens that do not look like file paths
158+
const execBash = new ExecuteBash({ command: 'echo hello world', cwd: '/workspace/folder' })
159+
const result = execBash.requiresAcceptance()
160+
161+
assert.equal(
162+
result.requiresAcceptance,
163+
false,
164+
'A command without any path-like token should not require acceptance'
165+
)
166+
167+
workspaceStub.restore()
168+
})
117169
})

0 commit comments

Comments
 (0)