Skip to content

Commit 181b5bb

Browse files
authored
fix(amazonq): improve typeahead experience (#7512)
## Problem In the inline flare branch, the typeahead experience is bad because even when user typed matching characters after seeing a suggestion, we are still re-fetching the new suggestions, which causes the cached suggestion to disappear and new suggestion re-appear. ## Solution Use the previous suggestion from last invocation if what user typed matches the suggestion prefix. https://github.com/user-attachments/assets/8c039a80-edd9-4687-9f2c-fb646707b79c This also aligns the experience in prod plugins without Flare inline. --- - 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 068ba67 commit 181b5bb

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

packages/amazonq/src/app/inline/completion.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,41 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
208208
return []
209209
}
210210

211-
// report suggestion state for previous suggestions if they exist
212-
const prevSessionId = this.sessionManager.getActiveSession()?.sessionId
211+
// handling previous session
212+
const prevSession = this.sessionManager.getActiveSession()
213+
const prevSessionId = prevSession?.sessionId
213214
const prevItemId = this.sessionManager.getActiveRecommendation()?.[0]?.itemId
214-
if (prevSessionId && prevItemId) {
215+
const prevStartPosition = prevSession?.startPosition
216+
const editor = window.activeTextEditor
217+
if (prevSession && prevSessionId && prevItemId && prevStartPosition) {
218+
const prefix = document.getText(new Range(prevStartPosition, position))
219+
const prevItemMatchingPrefix = []
220+
for (const item of this.sessionManager.getActiveRecommendation()) {
221+
const text = typeof item.insertText === 'string' ? item.insertText : item.insertText.value
222+
if (text.startsWith(prefix) && position.isAfterOrEqual(prevStartPosition)) {
223+
item.command = {
224+
command: 'aws.amazonq.acceptInline',
225+
title: 'On acceptance',
226+
arguments: [
227+
prevSessionId,
228+
item,
229+
editor,
230+
prevSession?.requestStartTime,
231+
position.line,
232+
prevSession?.firstCompletionDisplayLatency,
233+
],
234+
}
235+
item.range = new Range(prevStartPosition, position)
236+
prevItemMatchingPrefix.push(item as InlineCompletionItem)
237+
}
238+
}
239+
// re-use previous suggestions as long as new typed prefix matches
240+
if (prevItemMatchingPrefix.length > 0) {
241+
getLogger().debug(`Re-using suggestions that match user typed characters`)
242+
return prevItemMatchingPrefix
243+
}
244+
getLogger().debug(`Auto rejecting suggestions from previous session`)
245+
// if no such suggestions, report the previous suggestion as Reject
215246
const params: LogInlineCompletionSessionResultsParams = {
216247
sessionId: prevSessionId,
217248
completionSessionResult: {
@@ -242,7 +273,6 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
242273
const items = this.sessionManager.getActiveRecommendation()
243274
const itemId = this.sessionManager.getActiveRecommendation()?.[0]?.itemId
244275
const session = this.sessionManager.getActiveSession()
245-
const editor = window.activeTextEditor
246276

247277
// Show message to user when manual invoke fails to produce results.
248278
if (items.length === 0 && context.triggerKind === InlineCompletionTriggerKind.Invoke) {

packages/amazonq/src/app/inline/recommendationService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export class RecommendationService {
6666
firstResult.sessionId,
6767
firstResult.items,
6868
requestStartTime,
69+
position,
6970
firstCompletionDisplayLatency
7071
)
7172

packages/amazonq/src/app/inline/sessionManager.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5-
5+
import * as vscode from 'vscode'
66
import { InlineCompletionItemWithReferences } from '@aws/language-server-runtimes-types'
77

88
// TODO: add more needed data to the session interface
@@ -13,6 +13,7 @@ interface CodeWhispererSession {
1313
isRequestInProgress: boolean
1414
requestStartTime: number
1515
firstCompletionDisplayLatency?: number
16+
startPosition: vscode.Position
1617
}
1718

1819
export class SessionManager {
@@ -25,13 +26,15 @@ export class SessionManager {
2526
sessionId: string,
2627
suggestions: InlineCompletionItemWithReferences[],
2728
requestStartTime: number,
29+
startPosition: vscode.Position,
2830
firstCompletionDisplayLatency?: number
2931
) {
3032
this.activeSession = {
3133
sessionId,
3234
suggestions,
3335
isRequestInProgress: true,
3436
requestStartTime,
37+
startPosition,
3538
firstCompletionDisplayLatency,
3639
}
3740
}

0 commit comments

Comments
 (0)