Skip to content

Commit 4742872

Browse files
authored
feat(amazonq): Get repomap from Flare directly (#7985)
## Problem ## Solution --- - 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 2887458 commit 4742872

File tree

8 files changed

+73
-44
lines changed

8 files changed

+73
-44
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"webpack-merge": "^5.10.0"
7575
},
7676
"dependencies": {
77-
"@aws/language-server-runtimes": "^0.2.125",
77+
"@aws/language-server-runtimes": "^0.2.128",
7878
"@types/node": "^22.7.5",
7979
"jaro-winkler": "^0.2.8",
8080
"vscode-nls": "^5.2.0",

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ import {
2222
vsCodeState,
2323
} from 'aws-core-vscode/codewhisperer'
2424
import { Commands, getLogger, globals, sleep } from 'aws-core-vscode/shared'
25+
import { LanguageClient } from 'vscode-languageclient'
2526

26-
export async function activate() {
27+
export async function activate(languageClient: LanguageClient) {
2728
const codewhispererSettings = CodeWhispererSettings.instance
2829
const client = new DefaultCodeWhispererClient()
2930

3031
if (isInlineCompletionEnabled()) {
3132
await setSubscriptionsforInlineCompletion()
3233
await AuthUtil.instance.setVscodeContextProps()
34+
RecommendationHandler.instance.setLanguageClient(languageClient)
3335
}
3436

3537
function getAutoTriggerStatus(): boolean {

packages/amazonq/src/lsp/client.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,35 @@ async function onLanguageServerReady(
342342
const enableInlineRollback = true
343343
if (enableInlineRollback) {
344344
// use VSC inline
345-
await activateInline()
345+
await activateInline(client)
346346
} else {
347347
// use language server for inline completion
348348
const inlineManager = new InlineCompletionManager(client, sessionManager, lineTracker, inlineTutorialAnnotation)
349349
inlineManager.registerInlineCompletion()
350-
toDispose.push(inlineManager)
350+
toDispose.push(
351+
inlineManager,
352+
Commands.register('aws.amazonq.showPrev', async () => {
353+
await sessionManager.maybeRefreshSessionUx()
354+
await vscode.commands.executeCommand('editor.action.inlineSuggest.showPrevious')
355+
sessionManager.onPrevSuggestion()
356+
}),
357+
Commands.register('aws.amazonq.showNext', async () => {
358+
await sessionManager.maybeRefreshSessionUx()
359+
await vscode.commands.executeCommand('editor.action.inlineSuggest.showNext')
360+
sessionManager.onNextSuggestion()
361+
}),
362+
// this is a workaround since handleDidShowCompletionItem is not public API
363+
Commands.register('aws.amazonq.checkInlineSuggestionVisibility', async () => {
364+
sessionManager.checkInlineSuggestionVisibility()
365+
}),
366+
Commands.register({ id: 'aws.amazonq.invokeInlineCompletion', autoconnect: true }, async () => {
367+
vsCodeState.lastManualTriggerTime = performance.now()
368+
await vscode.commands.executeCommand('editor.action.inlineSuggest.trigger')
369+
}),
370+
vscode.workspace.onDidCloseTextDocument(async () => {
371+
await vscode.commands.executeCommand('aws.amazonq.rejectCodeSuggestion')
372+
})
373+
)
351374
}
352375

353376
activateInlineChat(extensionContext, client, encryptionKey, inlineChatTutorialAnnotation)
@@ -364,24 +387,6 @@ async function onLanguageServerReady(
364387
await initializeLanguageServerConfiguration(client, 'startup')
365388

366389
toDispose.push(
367-
Commands.register('aws.amazonq.showPrev', async () => {
368-
await sessionManager.maybeRefreshSessionUx()
369-
await vscode.commands.executeCommand('editor.action.inlineSuggest.showPrevious')
370-
sessionManager.onPrevSuggestion()
371-
}),
372-
Commands.register('aws.amazonq.showNext', async () => {
373-
await sessionManager.maybeRefreshSessionUx()
374-
await vscode.commands.executeCommand('editor.action.inlineSuggest.showNext')
375-
sessionManager.onNextSuggestion()
376-
}),
377-
// this is a workaround since handleDidShowCompletionItem is not public API
378-
Commands.register('aws.amazonq.checkInlineSuggestionVisibility', async () => {
379-
sessionManager.checkInlineSuggestionVisibility()
380-
}),
381-
Commands.register({ id: 'aws.amazonq.invokeInlineCompletion', autoconnect: true }, async () => {
382-
vsCodeState.lastManualTriggerTime = performance.now()
383-
await vscode.commands.executeCommand('editor.action.inlineSuggest.trigger')
384-
}),
385390
Commands.register('aws.amazonq.refreshAnnotation', async (forceProceed: boolean) => {
386391
telemetry.record({
387392
traceId: TelemetryHelper.instance.traceId,
@@ -407,9 +412,6 @@ async function onLanguageServerReady(
407412
getLogger().debug(`codewhisperer: user dismiss tutorial.`)
408413
}
409414
}),
410-
vscode.workspace.onDidCloseTextDocument(async () => {
411-
await vscode.commands.executeCommand('aws.amazonq.rejectCodeSuggestion')
412-
}),
413415
AuthUtil.instance.auth.onDidChangeActiveConnection(async () => {
414416
await auth.refreshConnection()
415417
}),

packages/core/src/codewhisperer/service/recommendationHandler.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { indent } from '../../shared/utilities/textUtilities'
4444
import path from 'path'
4545
import { isIamConnection } from '../../auth/connection'
4646
import { UserWrittenCodeTracker } from '../tracker/userWrittenCodeTracker'
47+
import { LanguageClient } from 'vscode-languageclient'
4748

4849
/**
4950
* This class is for getRecommendation/listRecommendation API calls and its states
@@ -99,6 +100,7 @@ export class RecommendationHandler {
99100
private next: vscode.Disposable
100101
private prev: vscode.Disposable
101102
private _timer?: NodeJS.Timer
103+
private languageClient?: LanguageClient
102104
documentUri: vscode.Uri | undefined = undefined
103105

104106
constructor() {
@@ -121,6 +123,10 @@ export class RecommendationHandler {
121123
return session.recommendations.some((r) => r.content.trim() !== '')
122124
}
123125

126+
setLanguageClient(languageClient: LanguageClient) {
127+
this.languageClient = languageClient
128+
}
129+
124130
async getServerResponse(
125131
triggerType: CodewhispererTriggerType,
126132
isManualTriggerOn: boolean,
@@ -204,7 +210,8 @@ export class RecommendationHandler {
204210
session.requestContext = await EditorContext.buildListRecommendationRequest(
205211
editor as vscode.TextEditor,
206212
this.nextToken,
207-
config.isSuggestionsWithCodeReferencesEnabled
213+
config.isSuggestionsWithCodeReferencesEnabled,
214+
this.languageClient
208215
)
209216
} else {
210217
session.requestContext = {

packages/core/src/codewhisperer/util/editorContext.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { indent } from '../../shared/utilities/textUtilities'
2222
import { isInDirectory } from '../../shared/filesystemUtilities'
2323
import { AuthUtil } from './authUtil'
2424
import { predictionTracker } from '../nextEditPrediction/activation'
25+
import { LanguageClient } from 'vscode-languageclient'
2526

2627
let tabSize: number = getTabSizeSetting()
2728

@@ -224,7 +225,8 @@ async function getWorkspaceId(editor: vscode.TextEditor): Promise<string | undef
224225
export async function buildListRecommendationRequest(
225226
editor: vscode.TextEditor,
226227
nextToken: string,
227-
allowCodeWithReference: boolean
228+
allowCodeWithReference: boolean,
229+
languageClient?: LanguageClient
228230
): Promise<{
229231
request: codewhispererClient.ListRecommendationsRequest
230232
supplementalMetadata: CodeWhispererSupplementalContext | undefined
@@ -236,7 +238,7 @@ export async function buildListRecommendationRequest(
236238
tokenSource.cancel()
237239
}, supplementalContextTimeoutInMs)
238240

239-
const supplementalContexts = await fetchSupplementalContext(editor, tokenSource.token)
241+
const supplementalContexts = await fetchSupplementalContext(editor, tokenSource.token, languageClient)
240242

241243
logSupplementalContext(supplementalContexts)
242244

packages/core/src/codewhisperer/util/supplementalContext/crossFileContextUtil.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ import {
2323
import { waitUntil } from '../../../shared/utilities/timeoutUtils'
2424
import { FeatureConfigProvider } from '../../../shared/featureConfig'
2525
import fs from '../../../shared/fs/fs'
26+
import { LanguageClient } from 'vscode-languageclient'
2627

28+
import {
29+
GetSupplementalContextParams,
30+
getSupplementalContextRequestType,
31+
SupplementalContextItem,
32+
} from '@aws/language-server-runtimes/protocol'
2733
type CrossFileSupportedLanguage =
2834
| 'java'
2935
| 'python'
@@ -66,7 +72,8 @@ type SupplementalContextConfig = 'none' | 'opentabs' | 'codemap' | 'bm25' | 'def
6672

6773
export async function fetchSupplementalContextForSrc(
6874
editor: vscode.TextEditor,
69-
cancellationToken: vscode.CancellationToken
75+
cancellationToken: vscode.CancellationToken,
76+
languageClient?: LanguageClient
7077
): Promise<Pick<CodeWhispererSupplementalContext, 'supplementalContextItems' | 'strategy'> | undefined> {
7178
const supplementalContextConfig = getSupplementalContextConfig(editor.document.languageId)
7279

@@ -101,7 +108,7 @@ export async function fetchSupplementalContextForSrc(
101108
async function () {
102109
const result: CodeWhispererSupplementalContextItem[] = []
103110
const opentabsContext = await fetchOpentabsContext(editor, cancellationToken)
104-
const codemap = await fetchProjectContext(editor, 'codemap')
111+
const codemap = await fetchProjectContext(editor, 'codemap', languageClient)
105112

106113
function addToResult(items: CodeWhispererSupplementalContextItem[]) {
107114
for (const item of items) {
@@ -145,7 +152,7 @@ export async function fetchSupplementalContextForSrc(
145152
if (supplementalContextConfig === 'bm25') {
146153
const projectBM25Promise = waitUntil(
147154
async function () {
148-
return await fetchProjectContext(editor, 'bm25')
155+
return await fetchProjectContext(editor, 'bm25', languageClient)
149156
},
150157
{ timeout: supplementalContextTimeoutInMs, interval: 5, truthy: false }
151158
)
@@ -168,7 +175,7 @@ export async function fetchSupplementalContextForSrc(
168175
// global bm25 with repomap
169176
const projectContextAndCodemapPromise = waitUntil(
170177
async function () {
171-
return await fetchProjectContext(editor, 'default')
178+
return await fetchProjectContext(editor, 'default', languageClient)
172179
},
173180
{ timeout: supplementalContextTimeoutInMs, interval: 5, truthy: false }
174181
)
@@ -192,13 +199,20 @@ export async function fetchSupplementalContextForSrc(
192199

193200
export async function fetchProjectContext(
194201
editor: vscode.TextEditor,
195-
target: 'default' | 'codemap' | 'bm25'
202+
target: 'default' | 'codemap' | 'bm25',
203+
languageclient?: LanguageClient
196204
): Promise<CodeWhispererSupplementalContextItem[]> {
197-
// const inputChunkContent = getInputChunk(editor)
198-
// TODO:
199-
const inlineProjectContext: { content: string; score: number; filePath: string }[] = []
200-
201-
return inlineProjectContext
205+
if (languageclient) {
206+
const request: GetSupplementalContextParams = {
207+
filePath: editor.document.uri.fsPath,
208+
}
209+
const response = await languageclient.sendRequest<SupplementalContextItem[]>(
210+
getSupplementalContextRequestType.method,
211+
request
212+
)
213+
return response as CodeWhispererSupplementalContextItem[]
214+
}
215+
return []
202216
}
203217

204218
export async function fetchOpentabsContext(

packages/core/src/codewhisperer/util/supplementalContext/supplementalContextUtil.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import { getLogger } from '../../../shared/logger/logger'
1313
import { CodeWhispererSupplementalContext } from '../../models/model'
1414
import * as os from 'os'
1515
import { crossFileContextConfig } from '../../models/constants'
16+
import { LanguageClient } from 'vscode-languageclient'
1617

1718
export async function fetchSupplementalContext(
1819
editor: vscode.TextEditor,
19-
cancellationToken: vscode.CancellationToken
20+
cancellationToken: vscode.CancellationToken,
21+
languageClient?: LanguageClient
2022
): Promise<CodeWhispererSupplementalContext | undefined> {
2123
const timesBeforeFetching = performance.now()
2224

@@ -32,7 +34,7 @@ export async function fetchSupplementalContext(
3234
if (isUtg) {
3335
supplementalContextPromise = fetchSupplementalContextForTest(editor, cancellationToken)
3436
} else {
35-
supplementalContextPromise = fetchSupplementalContextForSrc(editor, cancellationToken)
37+
supplementalContextPromise = fetchSupplementalContextForSrc(editor, cancellationToken, languageClient)
3638
}
3739

3840
return supplementalContextPromise

0 commit comments

Comments
 (0)