Skip to content

Commit 734bd98

Browse files
authored
fix(nep): truncate editor state (aws#7256)
## Problem We introduced `editorState` in data instrumentation launch. The service has a requirement of 40k character limit for the `text` field. ## Solution Implement a check on text length. If the text length exceeds 40k characters, section 20k max characters from the left and right side of current cursor position, so the final text is always less than 40k. validated prod endpoint inline working for files > 40k characters. Example request id: `57bbbe65-fbe7-47fc-81c4-c65262f47ce8` --- - 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 faeeb98 commit 734bd98

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "Avoid inline completion 'Improperly formed request' errors when file is too large"
4+
}

packages/core/src/codewhisperer/models/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ export const lineBreakWin = '\r\n'
8787
export const supplementalContextTimeoutInMs = 100
8888

8989
export const supplementalContextMaxTotalLength = 20480
90+
91+
export const editorStateMaxLength = 40000
92+
9093
/**
9194
* Ux of recommendations
9295
*/

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import * as codewhispererClient from '../client/codewhisperer'
88
import * as path from 'path'
99
import * as CodeWhispererConstants from '../models/constants'
1010
import { getTabSizeSetting } from '../../shared/utilities/editorUtilities'
11+
import { truncate } from '../../shared/utilities/textUtilities'
1112
import { getLogger } from '../../shared/logger/logger'
1213
import { runtimeLanguageContext } from './runtimeLanguageContext'
1314
import { fetchSupplementalContext } from './supplementalContext/supplementalContextUtil'
14-
import { supplementalContextTimeoutInMs } from '../models/constants'
15+
import { editorStateMaxLength, supplementalContextTimeoutInMs } from '../models/constants'
1516
import { getSelectedCustomization } from './customizationUtil'
1617
import { selectFrom } from '../../shared/utilities/tsUtils'
1718
import { checkLeftContextKeywordsForJson } from './commonUtil'
@@ -216,13 +217,29 @@ export function getTabSize(): number {
216217

217218
export function getEditorState(editor: vscode.TextEditor, fileContext: codewhispererClient.FileContext): any {
218219
try {
220+
const cursorPosition = editor.selection.active
221+
const cursorOffset = editor.document.offsetAt(cursorPosition)
222+
const documentText = editor.document.getText()
223+
224+
// Truncate if document content is too large (defined in constants.ts)
225+
let fileText = documentText
226+
if (documentText.length > editorStateMaxLength) {
227+
const halfLength = Math.floor(editorStateMaxLength / 2)
228+
229+
// Use truncate function to get the text around the cursor position
230+
const leftPart = truncate(documentText.substring(0, cursorOffset), -halfLength, '')
231+
const rightPart = truncate(documentText.substring(cursorOffset), halfLength, '')
232+
233+
fileText = leftPart + rightPart
234+
}
235+
219236
return {
220237
document: {
221238
programmingLanguage: {
222239
languageName: fileContext.programmingLanguage.languageName,
223240
},
224241
relativeFilePath: fileContext.filename,
225-
text: editor.document.getText(),
242+
text: fileText,
226243
},
227244
cursorState: {
228245
position: {

0 commit comments

Comments
 (0)