-
Notifications
You must be signed in to change notification settings - Fork 745
feat(amazonq): Use codewhisperer language server for completions #6158
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
Merged
jpinkney-aws
merged 6 commits into
aws:feature/amazonqLSP
from
jpinkney-aws:language-server-loading
Dec 5, 2024
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aa1f16c
feat(amazonq): Use codewhisperer language server for completions
jpinkney-aws 6afae55
fixup
jpinkney-aws 762e43d
fix(amazonq): Add missing bearer token auth
jpinkney-aws 4dcd50a
save
jpinkney-aws 78dbb83
fixup
jpinkney-aws 80fc57b
fixup
jpinkney-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { | ||
| CancellationToken, | ||
| InlineCompletionContext, | ||
| InlineCompletionItem, | ||
| InlineCompletionItemProvider, | ||
| InlineCompletionList, | ||
| Position, | ||
| TextDocument, | ||
| commands, | ||
| languages, | ||
| } from 'vscode' | ||
| import { LanguageClient } from 'vscode-languageclient' | ||
| import { | ||
| InlineCompletionItemWithReferences, | ||
| InlineCompletionListWithReferences, | ||
| InlineCompletionWithReferencesParams, | ||
| inlineCompletionWithReferencesRequestType, | ||
| logInlineCompletionSessionResultsNotificationType, | ||
| LogInlineCompletionSessionResultsParams, | ||
| } from '@aws/language-server-runtimes/protocol' | ||
|
|
||
| export const CodewhispererInlineCompletionLanguages = [ | ||
| { scheme: 'file', language: 'typescript' }, | ||
| { scheme: 'file', language: 'javascript' }, | ||
| { scheme: 'file', language: 'json' }, | ||
| { scheme: 'file', language: 'yaml' }, | ||
| { scheme: 'file', language: 'java' }, | ||
| { scheme: 'file', language: 'go' }, | ||
| { scheme: 'file', language: 'php' }, | ||
| { scheme: 'file', language: 'rust' }, | ||
| { scheme: 'file', language: 'kotlin' }, | ||
| { scheme: 'file', language: 'terraform' }, | ||
| { scheme: 'file', language: 'ruby' }, | ||
| { scheme: 'file', language: 'shellscript' }, | ||
| { scheme: 'file', language: 'dart' }, | ||
| { scheme: 'file', language: 'lua' }, | ||
| { scheme: 'file', language: 'powershell' }, | ||
| { scheme: 'file', language: 'r' }, | ||
| { scheme: 'file', language: 'swift' }, | ||
| { scheme: 'file', language: 'systemverilog' }, | ||
| { scheme: 'file', language: 'scala' }, | ||
| { scheme: 'file', language: 'vue' }, | ||
| { scheme: 'file', language: 'csharp' }, | ||
| ] | ||
|
|
||
| export function registerInlineCompletion(languageClient: LanguageClient) { | ||
| const inlineCompletionProvider = new AmazonQInlineCompletionItemProvider(languageClient) | ||
| languages.registerInlineCompletionItemProvider(CodewhispererInlineCompletionLanguages, inlineCompletionProvider) | ||
|
|
||
| const onInlineAcceptance = async ( | ||
| sessionId: string, | ||
| itemId: string, | ||
| requestStartTime: number, | ||
| firstCompletionDisplayLatency?: number | ||
| ) => { | ||
| const params: LogInlineCompletionSessionResultsParams = { | ||
| sessionId: sessionId, | ||
| completionSessionResult: { | ||
| [itemId]: { | ||
| seen: true, | ||
| accepted: true, | ||
| discarded: false, | ||
| }, | ||
| }, | ||
| totalSessionDisplayTime: Date.now() - requestStartTime, | ||
| firstCompletionDisplayLatency: firstCompletionDisplayLatency, | ||
| } | ||
| languageClient.sendNotification(logInlineCompletionSessionResultsNotificationType as any, params) | ||
| } | ||
| commands.registerCommand('aws.sample-vscode-ext-amazonq.accept', onInlineAcceptance) | ||
| } | ||
|
|
||
| export class AmazonQInlineCompletionItemProvider implements InlineCompletionItemProvider { | ||
| constructor(private readonly languageClient: LanguageClient) {} | ||
|
|
||
| async provideInlineCompletionItems( | ||
| document: TextDocument, | ||
| position: Position, | ||
| context: InlineCompletionContext, | ||
| token: CancellationToken | ||
| ): Promise<InlineCompletionItem[] | InlineCompletionList> { | ||
| const requestStartTime = Date.now() | ||
| const request: InlineCompletionWithReferencesParams = { | ||
| textDocument: { | ||
| uri: document.uri.toString(), | ||
| }, | ||
| position, | ||
| context, | ||
| } | ||
|
|
||
| const response = await this.languageClient.sendRequest( | ||
| inlineCompletionWithReferencesRequestType as any, | ||
| request, | ||
| token | ||
| ) | ||
|
|
||
| const list: InlineCompletionListWithReferences = response as InlineCompletionListWithReferences | ||
| this.languageClient.info(`Client: Received ${list.items.length} suggestions`) | ||
| const firstCompletionDisplayLatency = Date.now() - requestStartTime | ||
|
|
||
| // Add completion session tracking and attach onAcceptance command to each item to record used decision | ||
| list.items.forEach((item: InlineCompletionItemWithReferences) => { | ||
| item.command = { | ||
| command: 'aws.sample-vscode-ext-amazonq.accept', | ||
| title: 'On acceptance', | ||
| arguments: [list.sessionId, item.itemId, requestStartTime, firstCompletionDisplayLatency], | ||
| } | ||
| }) | ||
|
|
||
| return list as InlineCompletionList | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { | ||
| ConnectionMetadata, | ||
| NotificationType, | ||
| RequestType, | ||
| ResponseMessage, | ||
| } from '@aws/language-server-runtimes/protocol' | ||
| import * as jose from 'jose' | ||
| import * as crypto from 'crypto' | ||
| import { LanguageClient } from 'vscode-languageclient' | ||
| import { AuthUtil } from 'aws-core-vscode/codewhisperer' | ||
| import { Writable } from 'stream' | ||
|
|
||
| const encryptionKey = crypto.randomBytes(32) | ||
|
|
||
| /** | ||
| * Sends a json payload to the language server, who is waiting to know what the encryption key is. | ||
| * Code reference: https://github.com/aws/language-servers/blob/7da212185a5da75a72ce49a1a7982983f438651a/client/vscode/src/credentialsActivation.ts#L77 | ||
| */ | ||
| export function writeEncryptionInit(stream: Writable): void { | ||
| const request = { | ||
| version: '1.0', | ||
| mode: 'JWT', | ||
| key: encryptionKey.toString('base64'), | ||
| } | ||
| stream.write(JSON.stringify(request)) | ||
| stream.write('\n') | ||
| } | ||
|
|
||
| /** | ||
| * Request for custom notifications that Update Credentials and tokens. | ||
| * See core\aws-lsp-core\src\credentials\updateCredentialsRequest.ts for details | ||
| */ | ||
| export interface UpdateCredentialsRequest { | ||
| /** | ||
| * Encrypted token (JWT or PASETO) | ||
| * The token's contents differ whether IAM or Bearer token is sent | ||
| */ | ||
| data: string | ||
| /** | ||
| * Used by the runtime based language servers. | ||
| * Signals that this client will encrypt its credentials payloads. | ||
| */ | ||
| encrypted: boolean | ||
| } | ||
|
|
||
| export const notificationTypes = { | ||
| updateBearerToken: new RequestType<UpdateCredentialsRequest, ResponseMessage, Error>( | ||
| 'aws/credentials/token/update' | ||
| ), | ||
| deleteBearerToken: new NotificationType('aws/credentials/token/delete'), | ||
| getConnectionMetadata: new RequestType<undefined, ConnectionMetadata, Error>( | ||
| 'aws/credentials/getConnectionMetadata' | ||
| ), | ||
| } | ||
|
|
||
| /** | ||
| * Facade over our VSCode Auth that does crud operations on the language server auth | ||
| */ | ||
| export class AmazonQLSPAuth { | ||
jpinkney-aws marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| constructor(private readonly client: LanguageClient) {} | ||
|
|
||
| async init() { | ||
| const activeConnection = AuthUtil.instance.auth.activeConnection | ||
| if (activeConnection?.type === 'sso') { | ||
| // send the token to the language server | ||
| const token = await AuthUtil.instance.getBearerToken() | ||
| await this.updateBearerToken(token) | ||
| } | ||
| } | ||
|
|
||
| private async updateBearerToken(token: string) { | ||
| const request = await this.createUpdateCredentialsRequest({ | ||
| token, | ||
| }) | ||
|
|
||
| await this.client.sendRequest(notificationTypes.updateBearerToken.method, request) | ||
|
|
||
| this.client.info(`UpdateBearerToken: ${JSON.stringify(request)}`) | ||
| } | ||
|
|
||
| private async createUpdateCredentialsRequest(data: any) { | ||
| const payload = new TextEncoder().encode(JSON.stringify({ data })) | ||
|
|
||
| const jwt = await new jose.CompactEncrypt(payload) | ||
| .setProtectedHeader({ alg: 'dir', enc: 'A256GCM' }) | ||
| .encrypt(encryptionKey) | ||
|
|
||
| return { | ||
| data: jwt, | ||
| encrypted: true, | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.