Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "Bug Fix",
"description": "Prompt re-authenticate if auto trigger failed with expired token"
}
1 change: 1 addition & 0 deletions packages/amazonq/src/app/inline/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
position,
context,
token,
isAutoTrigger,
getAllRecommendationsOptions
)
// get active item from session for displaying
Expand Down
18 changes: 15 additions & 3 deletions packages/amazonq/src/app/inline/recommendationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { CancellationToken, InlineCompletionContext, Position, TextDocument } fr
import { LanguageClient } from 'vscode-languageclient'
import { SessionManager } from './sessionManager'
import { InlineGeneratingMessage } from './inlineGeneratingMessage'
import { CodeWhispererStatusBarManager } from 'aws-core-vscode/codewhisperer'
import { AuthUtil, CodeWhispererStatusBarManager } from 'aws-core-vscode/codewhisperer'
import { TelemetryHelper } from './telemetryHelper'
import { ICursorUpdateRecorder } from './cursorUpdateManager'
import { globals, getLogger } from 'aws-core-vscode/shared'
Expand All @@ -28,7 +28,6 @@ export class RecommendationService {
private readonly inlineGeneratingMessage: InlineGeneratingMessage,
private cursorUpdateRecorder?: ICursorUpdateRecorder
) {}

/**
* Set the recommendation service
*/
Expand All @@ -42,6 +41,7 @@ export class RecommendationService {
position: Position,
context: InlineCompletionContext,
token: CancellationToken,
isAutoTrigger: boolean,
options: GetAllRecommendationsOptions = { emitTelemetry: true, showUi: true }
) {
// Record that a regular request is being made
Expand Down Expand Up @@ -131,8 +131,20 @@ export class RecommendationService {
this.sessionManager.closeSession()
TelemetryHelper.instance.setAllPaginationEndTime()
options.emitTelemetry && TelemetryHelper.instance.tryRecordClientComponentLatency()
} catch (error) {
} catch (error: any) {
getLogger().error('Error getting recommendations: %O', error)
// bearer token expired
if (error.data && error.data.awsErrorCode === 'E_AMAZON_Q_CONNECTION_EXPIRED') {
// ref: https://github.com/aws/aws-toolkit-vscode/blob/amazonq/v1.74.0/packages/core/src/codewhisperer/service/inlineCompletionService.ts#L104
// show re-auth once if connection expired
if (AuthUtil.instance.isConnectionExpired()) {
await AuthUtil.instance.notifyReauthenticate(isAutoTrigger)
} else {
// get a new bearer token, if this failed, the connection will be marked as expired.
// new tokens will be synced per 10 seconds in auth.startTokenRefreshInterval
await AuthUtil.instance.getBearerToken()
}
}
return []
} finally {
// Remove all UI indicators if UI is enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,14 @@ describe('RecommendationService', () => {

sendRequestStub.resolves(mockFirstResult)

await service.getAllRecommendations(languageClient, mockDocument, mockPosition, mockContext, mockToken)
await service.getAllRecommendations(
languageClient,
mockDocument,
mockPosition,
mockContext,
mockToken,
true
)

// Verify sendRequest was called with correct parameters
assert(sendRequestStub.calledOnce)
Expand Down Expand Up @@ -172,7 +179,14 @@ describe('RecommendationService', () => {
sendRequestStub.onFirstCall().resolves(mockFirstResult)
sendRequestStub.onSecondCall().resolves(mockSecondResult)

await service.getAllRecommendations(languageClient, mockDocument, mockPosition, mockContext, mockToken)
await service.getAllRecommendations(
languageClient,
mockDocument,
mockPosition,
mockContext,
mockToken,
true
)

// Verify sendRequest was called with correct parameters
assert(sendRequestStub.calledTwice)
Expand Down Expand Up @@ -204,7 +218,14 @@ describe('RecommendationService', () => {

sendRequestStub.resolves(mockFirstResult)

await service.getAllRecommendations(languageClient, mockDocument, mockPosition, mockContext, mockToken)
await service.getAllRecommendations(
languageClient,
mockDocument,
mockPosition,
mockContext,
mockToken,
true
)

// Verify recordCompletionRequest was called
// eslint-disable-next-line @typescript-eslint/unbound-method
Expand Down Expand Up @@ -232,10 +253,18 @@ describe('RecommendationService', () => {
const { showGeneratingStub, hideGeneratingStub } = setupUITest()

// Call with showUi: false option
await service.getAllRecommendations(languageClient, mockDocument, mockPosition, mockContext, mockToken, {
showUi: false,
emitTelemetry: true,
})
await service.getAllRecommendations(
languageClient,
mockDocument,
mockPosition,
mockContext,
mockToken,
true,
{
showUi: false,
emitTelemetry: true,
}
)

// Verify UI methods were not called
sinon.assert.notCalled(showGeneratingStub)
Expand All @@ -248,7 +277,14 @@ describe('RecommendationService', () => {
const { showGeneratingStub, hideGeneratingStub } = setupUITest()

// Call with default options (showUi: true)
await service.getAllRecommendations(languageClient, mockDocument, mockPosition, mockContext, mockToken)
await service.getAllRecommendations(
languageClient,
mockDocument,
mockPosition,
mockContext,
mockToken,
true
)

// Verify UI methods were called
sinon.assert.calledOnce(showGeneratingStub)
Expand Down Expand Up @@ -284,6 +320,7 @@ describe('RecommendationService', () => {
mockPosition,
mockContext,
mockToken,
true,
options
)

Expand Down
Loading