Skip to content

Commit 2c236ce

Browse files
authored
fix(amazonq): adding truncation logic to fileText for inline suggestions (#2589)
* fix(amazonq): adding truncation logic to filetext for inline suggestions * Fixing tests
1 parent a837cc9 commit 2c236ce

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

server/aws-lsp-codewhisperer/src/language-server/inline-completion/codeWhispererServer.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,16 @@ describe('CodeWhisperer Server', () => {
916916
{ content: 'class Foo', filePath: 'Foo.java' },
917917
{ content: 'class Bar', filePath: 'Bar.java' },
918918
],
919+
editorState: {
920+
document: {
921+
relativeFilePath: 'file:///TargetFile.java',
922+
programmingLanguage: { languageName: 'java' },
923+
text: '',
924+
},
925+
cursorState: {
926+
position: { line: 0, character: 0 },
927+
},
928+
},
919929
// workspaceId: undefined,
920930
}
921931
sinon.assert.calledOnceWithExactly(test_service.generateSuggestions, expectedGenerateSuggestionsRequest)

server/aws-lsp-codewhisperer/src/language-server/inline-completion/contants/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const FILE_URI_CHARS_LIMIT = 1024
22
export const FILENAME_CHARS_LIMIT = 1024
33
export const CONTEXT_CHARACTERS_LIMIT = 10240
4+
export const EDITOR_STATE_MAX_LENGTH = 40000
45
export const EMPTY_RESULT = { sessionId: '', items: [] }
56
export const EDIT_DEBOUNCE_INTERVAL_MS = 500
67
// ABAP ADT extensions commonly used with Eclipse

server/aws-lsp-codewhisperer/src/language-server/inline-completion/handler/inlineCompletionHandler.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
emitServiceInvocationTelemetry,
4646
emitUserTriggerDecisionTelemetry,
4747
} from '../telemetry/telemetry'
48-
import { EMPTY_RESULT } from '../contants/constants'
48+
import { EDITOR_STATE_MAX_LENGTH, EMPTY_RESULT } from '../contants/constants'
4949
import { IdleWorkspaceManager } from '../../workspaceContext/IdleWorkspaceManager'
5050
import { mergeSuggestionsWithRightContext } from '../utils/mergeRightUtils'
5151
import { getTextDocument } from '../utils/textDocumentUtils'
@@ -331,9 +331,36 @@ export class InlineCompletionHandler {
331331

332332
if (codeWhispererService instanceof CodeWhispererServiceToken) {
333333
const tokenRequest = requestContext as GenerateTokenSuggestionsRequest
334+
335+
// Build editorState with truncation for token-based requests
336+
const documentText = textDocument.getText()
337+
const cursorOffset = textDocument.offsetAt(params.position)
338+
let fileText = documentText
339+
if (documentText.length > EDITOR_STATE_MAX_LENGTH) {
340+
const halfLength = Math.floor(EDITOR_STATE_MAX_LENGTH / 2)
341+
const leftPart = documentText.substring(Math.max(0, cursorOffset - halfLength), cursorOffset)
342+
const rightPart = documentText.substring(cursorOffset, cursorOffset + halfLength)
343+
fileText = leftPart + rightPart
344+
}
345+
334346
generateCompletionReq = {
335347
...tokenRequest,
336348
...(workspaceId ? { workspaceId } : {}),
349+
editorState: {
350+
document: {
351+
relativeFilePath: textDocument.uri,
352+
programmingLanguage: {
353+
languageName: fileContext.programmingLanguage.languageName,
354+
},
355+
text: fileText,
356+
},
357+
cursorState: {
358+
position: {
359+
line: params.position.line,
360+
character: params.position.character,
361+
},
362+
},
363+
},
337364
}
338365
} else {
339366
const iamRequest = requestContext as GenerateIAMSuggestionsRequest
@@ -344,7 +371,9 @@ export class InlineCompletionHandler {
344371

345372
try {
346373
const authType = codeWhispererService instanceof CodeWhispererServiceToken ? 'token' : 'iam'
347-
this.logging.debug(`[INLINE_COMPLETION] API call - generateSuggestions (new session, ${authType})`)
374+
this.logging.debug(
375+
`[INLINE_COMPLETION] API call - generateSuggestions (new session, ${authType}), generateCompletion request = ${JSON.stringify(generateCompletionReq)}`
376+
)
348377
const suggestionResponse = await codeWhispererService.generateSuggestions(generateCompletionReq)
349378
return await this.processSuggestionResponse(suggestionResponse, newSession, true, selectionRange)
350379
} catch (error) {

0 commit comments

Comments
 (0)