Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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": "Avoid inline completion 'Improperly formed request' errors when file is too large"
}
3 changes: 3 additions & 0 deletions packages/core/src/codewhisperer/models/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export const lineBreakWin = '\r\n'
export const supplementalContextTimeoutInMs = 100

export const supplementalContextMaxTotalLength = 20480

export const editorStateMaxLength = 40000

/**
* Ux of recommendations
*/
Expand Down
25 changes: 21 additions & 4 deletions packages/core/src/codewhisperer/util/editorContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import * as codewhispererClient from '../client/codewhisperer'
import * as path from 'path'
import * as CodeWhispererConstants from '../models/constants'
import { getTabSizeSetting } from '../../shared/utilities/editorUtilities'
import { truncate } from '../../shared/utilities/textUtilities'
import { getLogger } from '../../shared/logger/logger'
import { runtimeLanguageContext } from './runtimeLanguageContext'
import { fetchSupplementalContext } from './supplementalContext/supplementalContextUtil'
import { supplementalContextTimeoutInMs } from '../models/constants'
import { editorStateMaxLength, supplementalContextTimeoutInMs } from '../models/constants'
import { getSelectedCustomization } from './customizationUtil'
import { selectFrom } from '../../shared/utilities/tsUtils'
import { checkLeftContextKeywordsForJson } from './commonUtil'
Expand Down Expand Up @@ -216,13 +217,29 @@ export function getTabSize(): number {

export function getEditorState(editor: vscode.TextEditor, fileContext: codewhispererClient.FileContext): any {
try {
const cursorPosition = editor.selection.active
const cursorOffset = editor.document.offsetAt(cursorPosition)
const documentText = editor.document.getText()

// Truncate if document content is too large (defined in constants.ts)
let fileText = documentText
if (documentText.length > editorStateMaxLength) {
const halfLength = Math.floor(editorStateMaxLength / 2)

// Use truncate function to get the text around the cursor position
const leftPart = truncate(documentText.substring(0, cursorOffset), -halfLength, '')
const rightPart = truncate(documentText.substring(cursorOffset), halfLength, '')

fileText = leftPart + rightPart
}

return {
document: {
programmingLanguage: {
languageName: fileContext.programmingLanguage.languageName,
},
relativeFilePath: fileContext.filename,
text: editor.document.getText(),
text: fileText,
},
cursorState: {
position: {
Expand Down Expand Up @@ -278,8 +295,8 @@ function logSupplementalContext(supplementalContext: CodeWhispererSupplementalCo
logString += indent(`\nChunk ${index}:\n`, 4, true)
logString += indent(
`Path: ${context.filePath}
Length: ${context.content.length}
Score: ${context.score}`,
Length: ${context.content.length}
Score: ${context.score}`,
8,
true
)
Expand Down
Loading