Skip to content

Commit 907c784

Browse files
authored
fix(amazonq): explain issue fails (aws#7194)
## Problem - Explain issue from security scan hasn't been hooked up when moving to flare ## Solution - use sendToPrompt from flare to automatically submit an "explain message". This message contains a prompt that gets shown to the user and an escaped prompt that gets sent to the backend - text/formatting for uiMessage and contextMessage were taken from codewhisperer chat --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent f1ede42 commit 907c784

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

packages/amazonq/src/lsp/chat/commands.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import { Commands, globals } from 'aws-core-vscode/shared'
77
import { window } from 'vscode'
88
import { AmazonQChatViewProvider } from './webviewProvider'
9+
import { CodeScanIssue } from 'aws-core-vscode/codewhisperer'
10+
import { EditorContextExtractor } from 'aws-core-vscode/codewhispererChat'
911

1012
/**
1113
* TODO: Re-enable these once we can figure out which path they're going to live in
@@ -17,6 +19,45 @@ export function registerCommands(provider: AmazonQChatViewProvider) {
1719
registerGenericCommand('aws.amazonq.refactorCode', 'Refactor', provider),
1820
registerGenericCommand('aws.amazonq.fixCode', 'Fix', provider),
1921
registerGenericCommand('aws.amazonq.optimizeCode', 'Optimize', provider),
22+
Commands.register('aws.amazonq.explainIssue', async (issue: CodeScanIssue) => {
23+
void focusAmazonQPanel().then(async () => {
24+
const editorContextExtractor = new EditorContextExtractor()
25+
const extractedContext = await editorContextExtractor.extractContextForTrigger('ContextMenu')
26+
const selectedCode =
27+
extractedContext?.activeFileContext?.fileText
28+
?.split('\n')
29+
.slice(issue.startLine, issue.endLine)
30+
.join('\n') ?? ''
31+
32+
// The message that gets sent to the UI
33+
const uiMessage = [
34+
'Explain the ',
35+
issue.title,
36+
' issue in the following code:',
37+
'\n```\n',
38+
selectedCode,
39+
'\n```',
40+
].join('')
41+
42+
// The message that gets sent to the backend
43+
const contextMessage = `Explain the issue "${issue.title}" (${JSON.stringify(
44+
issue
45+
)}) and generate code demonstrating the fix`
46+
47+
void provider.webview?.postMessage({
48+
command: 'sendToPrompt',
49+
params: {
50+
selection: '',
51+
triggerType: 'contextMenu',
52+
prompt: {
53+
prompt: uiMessage, // what gets sent to the user
54+
escapedPrompt: contextMessage, // what gets sent to the backend
55+
},
56+
autoSubmit: true,
57+
},
58+
})
59+
})
60+
}),
2061
Commands.register('aws.amazonq.sendToPrompt', (data) => {
2162
const triggerType = getCommandTriggerType(data)
2263
const selection = getSelectedText()

packages/core/src/codewhispererChat/commands/registerCommands.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export function registerCommands() {
4040
/**
4141
* make these no-ops, since theres still callers that need to be deprecated
4242
*/
43-
Commands.register('aws.amazonq.explainIssue', async (issue) => {})
4443
Commands.register('aws.amazonq.generateUnitTests', async (data) => {})
4544
Commands.register('aws.amazonq.updateContextCommandItems', () => {})
4645
}

0 commit comments

Comments
 (0)