Skip to content

Commit 7bb66e1

Browse files
feat(amazonq): Port in chat message error handling (#7121)
## Problem During the agentic loop, if there are multiple iterations and then it eventually fails, the partial results would not be posted and the user would not be able to continue chat (it wouldn't allow them to type) ## Solution Port in the VSC client related change from: aws/language-servers#1012 Now if there are errors during the agentic loop, we will display all the partial responses we have gathered and then break out of the generating state, allowing the user to continue. ## How I tested Between the previous and new changes I ran the same test suite - Ask agentic chat to make 101 files (since [a recent change](aws/language-servers#1022) increased the loop limit to 100) in a test folder, adding in some random text - Ask agentic chat to "read every single file, not skipping any of them, and then display the text of each one". - W/ the old changes it would error part way and the UI would show "generating" and the user could not do anything - W/ the new changes it will show partial results (the text of the files it could resolve) and then allow the user to continue with another prompt. --- - 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. Signed-off-by: nkomonen-amazon <[email protected]>
1 parent 1e48e36 commit 7bb66e1

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

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

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,24 @@ export function registerMessageListeners(
182182
break
183183
}
184184
case chatRequestType.method: {
185-
const chatParams = { ...message.params } as ChatParams
185+
const chatParams: ChatParams = { ...message.params }
186186
const partialResultToken = uuidv4()
187-
const chatDisposable = languageClient.onProgress(chatRequestType, partialResultToken, (partialResult) =>
188-
handlePartialResult<ChatResult>(partialResult, encryptionKey, provider, chatParams.tabId)
187+
let lastPartialResult: ChatResult | undefined
188+
const chatDisposable = languageClient.onProgress(
189+
chatRequestType,
190+
partialResultToken,
191+
(partialResult) => {
192+
// Store the latest partial result
193+
if (typeof partialResult === 'string' && encryptionKey) {
194+
void decodeRequest<ChatResult>(partialResult, encryptionKey).then(
195+
(decoded) => (lastPartialResult = decoded)
196+
)
197+
} else {
198+
lastPartialResult = partialResult as ChatResult
199+
}
200+
201+
void handlePartialResult<ChatResult>(partialResult, encryptionKey, provider, chatParams.tabId)
202+
}
189203
)
190204

191205
const editor =
@@ -197,17 +211,36 @@ export function registerMessageListeners(
197211
}
198212

199213
const chatRequest = await encryptRequest<ChatParams>(chatParams, encryptionKey)
200-
const chatResult = (await languageClient.sendRequest(chatRequestType.method, {
201-
...chatRequest,
202-
partialResultToken,
203-
})) as string | ChatResult
204-
void handleCompleteResult<ChatResult>(
205-
chatResult,
206-
encryptionKey,
207-
provider,
208-
chatParams.tabId,
209-
chatDisposable
210-
)
214+
try {
215+
const chatResult = await languageClient.sendRequest<string | ChatResult>(chatRequestType.method, {
216+
...chatRequest,
217+
partialResultToken,
218+
})
219+
await handleCompleteResult<ChatResult>(
220+
chatResult,
221+
encryptionKey,
222+
provider,
223+
chatParams.tabId,
224+
chatDisposable
225+
)
226+
} catch (e) {
227+
languageClient.info(`Error occurred during chat request: ${e}`)
228+
// Use the last partial result if available, append error message
229+
const errorResult: ChatResult = {
230+
...lastPartialResult,
231+
body: lastPartialResult?.body
232+
? `${lastPartialResult.body}\n\n ❌ Error: Request failed to complete`
233+
: '❌ An error occurred while processing your request',
234+
}
235+
236+
await handleCompleteResult<ChatResult>(
237+
errorResult,
238+
encryptionKey,
239+
provider,
240+
chatParams.tabId,
241+
chatDisposable
242+
)
243+
}
211244
break
212245
}
213246
case quickActionRequestType.method: {

0 commit comments

Comments
 (0)