Skip to content

Commit 65791e5

Browse files
committed
Add reject button for shell command
1 parent d7fe9b5 commit 65791e5

File tree

3 files changed

+70
-26
lines changed

3 files changed

+70
-26
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
ChatItemButton,
99
ChatItemFormItem,
1010
ChatItemType,
11-
MynahIcons,
1211
MynahIconsType,
1312
MynahUIDataModel,
1413
QuickActionCommand,
@@ -240,6 +239,8 @@ export class Connector extends BaseConnector {
240239
}
241240

242241
if (messageData.type === 'customFormActionMessage') {
242+
// eslint-disable-next-line aws-toolkits/no-console-log
243+
console.log('messageData', messageData)
243244
this.onCustomFormAction(messageData.tabID, messageData.messageId, messageData.action)
244245
return
245246
}
@@ -292,7 +293,7 @@ export class Connector extends BaseConnector {
292293

293294
if (
294295
!this.onChatAnswerUpdated ||
295-
!['accept-code-diff', 'reject-code-diff', 'confirm-tool-use', 'run-shell-command'].includes(action.id)
296+
!['accept-code-diff', 'reject-code-diff', 'run-shell-command', 'reject-shell-command'].includes(action.id)
296297
) {
297298
return
298299
}
@@ -330,36 +331,39 @@ export class Connector extends BaseConnector {
330331
answer.body = ' '
331332
}
332333
break
333-
case 'confirm-tool-use':
334-
answer.buttons = [
335-
{
336-
keepCardAfterClick: true,
337-
text: 'Confirmed',
338-
id: 'confirmed-tool-use',
339-
status: 'success',
340-
position: 'outside',
341-
disabled: true,
342-
},
343-
]
344-
break
345334
case 'run-shell-command':
346335
answer.header = {
347336
body: 'shell',
348337
icon: 'code-block' as MynahIconsType,
349338
status: {
350-
icon: MynahIcons.OK,
339+
icon: 'ok' as MynahIconsType,
351340
text: 'Accepted',
352341
status: 'success',
353342
},
354343
}
355344
break
345+
case 'reject-shell-command':
346+
answer.header = {
347+
body: 'shell',
348+
icon: 'code-block' as MynahIconsType,
349+
status: {
350+
icon: 'cancel' as MynahIconsType,
351+
text: 'Rejected',
352+
status: 'error',
353+
},
354+
}
355+
break
356356
default:
357357
break
358358
}
359359

360360
if (currentChatItem && answer.messageId) {
361361
const updatedItem = { ...currentChatItem, ...answer }
362362
this.storeChatItem(tabId, answer.messageId, updatedItem)
363+
} else if (action.id === 'run-shell-command') {
364+
// eslint-disable-next-line aws-toolkits/no-console-log
365+
console.log('answer of run-shell-command', answer)
366+
this.storeChatItem(tabId, messageId, answer)
363367
}
364368

365369
this.onChatAnswerUpdated(tabId, answer)

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,13 +736,29 @@ export class ChatController {
736736
}
737737
}
738738

739+
private async rejectShellCommand(message: CustomFormActionMessage) {
740+
// Not execute shell command if customer reject to run it.
741+
const triggerID = randomUUID()
742+
this.triggerEventsStorage.addTriggerEvent({
743+
id: triggerID,
744+
tabID: message.tabID,
745+
message: undefined,
746+
type: 'quick_action',
747+
context: undefined,
748+
})
749+
getLogger().warn(
750+
`Customer reject execute shell command for tabID : ${message.tabID}, action.id: ${message.action.id}`
751+
)
752+
await this.generateStaticTextResponse('reject-shell-command', triggerID)
753+
return
754+
}
755+
739756
private async processCustomFormAction(message: CustomFormActionMessage) {
740757
switch (message.action.id) {
741758
case 'submit-create-prompt':
742759
await this.handleCreatePrompt(message)
743760
break
744761
case 'accept-code-diff':
745-
case 'confirm-tool-use':
746762
case 'run-shell-command':
747763
case 'generic-tool-execution':
748764
await this.closeDiffView()
@@ -751,6 +767,9 @@ export class ChatController {
751767
case 'reject-code-diff':
752768
await this.closeDiffView()
753769
break
770+
case 'reject-shell-command':
771+
await this.rejectShellCommand(message)
772+
break
754773
default:
755774
getLogger().warn(`Unhandled action: ${message.action.id}`)
756775
}

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ import { getWorkspaceForFile } from '../../../../shared/utilities/workspaceUtils
4747
import path from 'path'
4848
import { CommandValidation } from '../../../tools/executeBash'
4949

50-
export type StaticTextResponseType = 'quick-action-help' | 'onboarding-help' | 'transform' | 'help'
50+
export type StaticTextResponseType =
51+
| 'quick-action-help'
52+
| 'onboarding-help'
53+
| 'transform'
54+
| 'help'
55+
| 'reject-shell-command'
5156

5257
export type MessengerResponseType = {
5358
$metadata: { requestId?: string; httpStatusCode?: number }
@@ -227,12 +232,19 @@ export class Messenger {
227232
await ToolUtils.queueDescription(tool, chatStream)
228233

229234
if (!validation.requiresAcceptance) {
230-
// Need separate id for read tool and safe bash command execution as 'confirm-tool-use' id is required to change button status from `Confirm` to `Confirmed` state in cwChatConnector.ts which will impact generic tool execution.
231-
this.dispatcher.sendCustomFormActionMessage(
232-
new CustomFormActionMessage(tabID, {
233-
id: 'generic-tool-execution',
234-
})
235-
)
235+
if (tool.type === ToolType.ExecuteBash) {
236+
this.dispatcher.sendCustomFormActionMessage(
237+
new CustomFormActionMessage(tabID, {
238+
id: 'run-shell-command',
239+
})
240+
)
241+
} else {
242+
this.dispatcher.sendCustomFormActionMessage(
243+
new CustomFormActionMessage(tabID, {
244+
id: 'generic-tool-execution',
245+
})
246+
)
247+
}
236248
}
237249
} else {
238250
// TODO: Handle the error
@@ -446,6 +458,11 @@ export class Messenger {
446458
text: 'Run',
447459
icon: 'play' as MynahIconsType,
448460
})
461+
buttons.push({
462+
id: 'reject-shell-command',
463+
text: 'Reject',
464+
icon: 'cancel' as MynahIconsType,
465+
})
449466
}
450467

451468
shellCommandHeader = {
@@ -511,17 +528,17 @@ export class Messenger {
511528
? undefined
512529
: buttons,
513530
fullWidth: toolUse?.name === ToolType.FsWrite || toolUse?.name === ToolType.ExecuteBash,
514-
padding: !(toolUse?.name === ToolType.FsWrite),
531+
padding: !(toolUse?.name === ToolType.FsWrite || toolUse?.name === ToolType.ExecuteBash),
515532
header:
516533
toolUse?.name === ToolType.FsWrite
517534
? { icon: 'code-block' as MynahIconsType, buttons: buttons, fileList: fileList }
518535
: toolUse?.name === ToolType.ExecuteBash
519536
? shellCommandHeader
520537
: undefined,
521538
codeBlockActions:
522-
// eslint-disable-next-line unicorn/no-null, prettier/prettier
523539
toolUse?.name === ToolType.FsWrite || toolUse?.name === ToolType.ExecuteBash
524-
? { 'insert-to-cursor': null, copy: null }
540+
? // eslint-disable-next-line unicorn/no-null
541+
{ 'insert-to-cursor': null, copy: null }
525542
: undefined,
526543
},
527544
tabID
@@ -574,6 +591,10 @@ export class Messenger {
574591
]
575592
followUpsHeader = 'Try Examples:'
576593
break
594+
case 'reject-shell-command':
595+
// will update the string later
596+
message = 'Command execution rejected, aborted now!'
597+
break
577598
}
578599

579600
this.dispatcher.sendChatMessage(

0 commit comments

Comments
 (0)