diff --git a/packages/amazonq/.changes/next-release/Bug Fix-ab23e716-ee5c-47cd-8b6a-545dd6c7bda7.json b/packages/amazonq/.changes/next-release/Bug Fix-ab23e716-ee5c-47cd-8b6a-545dd6c7bda7.json new file mode 100644 index 00000000000..2723d81c118 --- /dev/null +++ b/packages/amazonq/.changes/next-release/Bug Fix-ab23e716-ee5c-47cd-8b6a-545dd6c7bda7.json @@ -0,0 +1,4 @@ +{ + "type": "Bug Fix", + "description": "fix increased latency due to inline completion API is called in a blocking manner" +} diff --git a/packages/amazonq/src/app/inline/recommendationService.ts b/packages/amazonq/src/app/inline/recommendationService.ts index 10dd25f5cdf..47abe2053ea 100644 --- a/packages/amazonq/src/app/inline/recommendationService.ts +++ b/packages/amazonq/src/app/inline/recommendationService.ts @@ -80,7 +80,7 @@ export class RecommendationService { nextToken: request.partialResultToken, }, }) - let result: InlineCompletionListWithReferences = await languageClient.sendRequest( + const result: InlineCompletionListWithReferences = await languageClient.sendRequest( inlineCompletionWithReferencesRequestType.method, request, token @@ -120,18 +120,10 @@ export class RecommendationService { getLogger().info( 'Suggestion type is COMPLETIONS. Start fetching for more items if partialResultToken exists.' ) - try { - while (result.partialResultToken) { - const paginatedRequest = { ...request, partialResultToken: result.partialResultToken } - result = await languageClient.sendRequest( - inlineCompletionWithReferencesRequestType.method, - paginatedRequest, - token - ) - this.sessionManager.updateSessionSuggestions(result.items) - } - } catch (error) { - languageClient.warn(`Error when getting suggestions: ${error}`) + if (result.partialResultToken) { + this.processRemainingRequests(languageClient, request, result, token).catch((error) => { + languageClient.warn(`Error when getting suggestions: ${error}`) + }) } } else { // Skip fetching for more items if the suggesion is EDITS. If it is EDITS suggestion, only fetching for more @@ -167,4 +159,31 @@ export class RecommendationService { } } } + + private async processRemainingRequests( + languageClient: LanguageClient, + initialRequest: InlineCompletionWithReferencesParams, + firstResult: InlineCompletionListWithReferences, + token: CancellationToken + ): Promise { + let nextToken = firstResult.partialResultToken + while (nextToken) { + const request = { ...initialRequest, partialResultToken: nextToken } + + const result: InlineCompletionListWithReferences = await languageClient.sendRequest( + inlineCompletionWithReferencesRequestType.method, + request, + token + ) + + this.sessionManager.updateSessionSuggestions(result.items) + nextToken = result.partialResultToken + } + + this.sessionManager.closeSession() + + // All pagination requests completed + TelemetryHelper.instance.setAllPaginationEndTime() + TelemetryHelper.instance.tryRecordClientComponentLatency() + } }