Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate left and right file contents for suggestions requests #77

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -6,7 +6,7 @@ import sinon, { StubbedInstance, stubInterface } from 'ts-sinon'
import { CancellationToken, InlineCompletionTriggerKind } from 'vscode-languageserver'
import { TextDocument } from 'vscode-languageserver-textdocument'
import { TestFeatures } from './TestFeatures'
import { CodewhispererServerFactory } from './codeWhispererServer'
import { CONTEXT_CHARACTERS_LIMIT, CodewhispererServerFactory } from './codeWhispererServer'
import { CodeWhispererServiceBase, ResponseContext, Suggestion } from './codeWhispererService'
import { CodeWhispererSession, SessionData, SessionManager } from './session/sessionManager'

Expand Down Expand Up @@ -186,6 +186,35 @@ class HelloWorld
sinon.assert.calledOnceWithExactly(service.generateSuggestions, expectedGenerateSuggestionsRequest)
})

it('should truncate left and right context', async () => {
const BIG_FILE_CONTENT = '123456789\n'.repeat(5000)
const BIG_FILE = TextDocument.create('file:///big_file.cs', 'csharp', 1, BIG_FILE_CONTENT)
const cutOffLine = 2000
features.openDocument(BIG_FILE)

await features.doInlineCompletionWithReferences(
{
textDocument: { uri: BIG_FILE.uri },
position: { line: cutOffLine, character: 1 },
context: { triggerKind: InlineCompletionTriggerKind.Invoked },
},
CancellationToken.None
)
const leftContentChecker = (leftContent: string) =>
leftContent.length == CONTEXT_CHARACTERS_LIMIT && leftContent.endsWith('\n1')
const rightContentChecker = (rightContent: string) =>
rightContent.length == CONTEXT_CHARACTERS_LIMIT && rightContent.startsWith('234')

sinon.assert.calledWith(
service.generateSuggestions,
sinon.match.hasNested('fileContext.leftFileContent', sinon.match(leftContentChecker))
)
sinon.assert.calledWith(
service.generateSuggestions,
sinon.match.hasNested('fileContext.rightFileContent', sinon.match(rightContentChecker))
)
})

it('should return recommendations when using a different languageId casing', async () => {
const result = await features.doInlineCompletionWithReferences(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { CodeWhispererServiceInvocationEvent } from './telemetry/types'
import { getCompletionType, isAwsError } from './utils'

const EMPTY_RESULT = { sessionId: '', items: [] }
export const CONTEXT_CHARACTERS_LIMIT = 10240

// Both clients (token, sigv4) define their own types, this return value needs to match both of them.
const getFileContext = (params: {
Expand Down Expand Up @@ -229,6 +230,14 @@ export const CodewhispererServerFactory =
credentialStartUrl: credentialsProvider.getConnectionMetadata()?.sso?.startUrl ?? undefined,
})

requestContext.fileContext.leftFileContent = requestContext.fileContext.leftFileContent.slice(
-CONTEXT_CHARACTERS_LIMIT
)
requestContext.fileContext.rightFileContent = requestContext.fileContext.rightFileContent.slice(
0,
CONTEXT_CHARACTERS_LIMIT
)

return codeWhispererService
.generateSuggestions(requestContext)
.then(suggestionResponse => {
Expand Down