-
Notifications
You must be signed in to change notification settings - Fork 89
feat: support context provider api #924
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1040187
feat: support context provider api
wenytang-ms ada72e1
style: fix format style
wenytang-ms 34515f1
ci: fix
wenytang-ms b0632c1
perf: update code to comments
wenytang-ms d2b1957
Merge branch 'main' into wenyt/contextprovier
wenytang-ms 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
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,153 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
| import { | ||
| ResolveRequest, | ||
| SupportedContextItem, | ||
| type ContextProvider, | ||
| } from '@github/copilot-language-server'; | ||
| import * as vscode from 'vscode'; | ||
| import { CopilotHelper } from './copilotHelper'; | ||
| import { sendError, sendInfo } from "vscode-extension-telemetry-wrapper"; | ||
| import { | ||
| JavaContextProviderUtils, | ||
| CancellationError, | ||
| InternalCancellationError, | ||
| CopilotCancellationError, | ||
| ContextResolverFunction, | ||
| CopilotApi, | ||
| ContextProviderRegistrationError, | ||
| ContextProviderResolverError | ||
| } from './utils'; | ||
|
|
||
| export async function registerCopilotContextProviders( | ||
| context: vscode.ExtensionContext | ||
| ) { | ||
| try { | ||
| const apis = await JavaContextProviderUtils.getCopilotApis(); | ||
| if (!apis.clientApi || !apis.chatApi) { | ||
| return; | ||
| } | ||
| // Register the Java completion context provider | ||
| const provider: ContextProvider<SupportedContextItem> = { | ||
| id: 'vscjava.vscode-java-dependency', // use extension id as provider id for now | ||
| selector: [{ language: "java" }], | ||
| resolver: { resolve: createJavaContextResolver() } | ||
| }; | ||
| const installCount = await JavaContextProviderUtils.installContextProviderOnApis(apis, provider, context, installContextProvider); | ||
| if (installCount === 0) { | ||
| return; | ||
| } | ||
| sendInfo("", { | ||
| "action": "registerCopilotContextProvider", | ||
| "status": "succeeded", | ||
| "installCount": installCount | ||
| }); | ||
| } | ||
| catch (error) { | ||
| const errorMessage = (error as Error).message || "unknown_error"; | ||
| sendError(new ContextProviderRegistrationError( | ||
| 'Failed to register Copilot context provider: ' + errorMessage | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create the Java context resolver function | ||
| */ | ||
| function createJavaContextResolver(): ContextResolverFunction { | ||
| return async (request: ResolveRequest, copilotCancel: vscode.CancellationToken): Promise<SupportedContextItem[]> => { | ||
| try { | ||
| // Check for immediate cancellation | ||
| JavaContextProviderUtils.checkCancellation(copilotCancel); | ||
| return await resolveJavaContext(request, copilotCancel); | ||
| } catch (error: any) { | ||
| sendError(new ContextProviderResolverError('Java Context Resolution Failed: ' + ((error as Error).message || "unknown_error"))); | ||
| // This should never be reached due to handleError throwing, but TypeScript requires it | ||
| return []; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Send telemetry data for Java context resolution | ||
| */ | ||
| function sendContextTelemetry(request: ResolveRequest, start: number, items: SupportedContextItem[], status: string, error?: string) { | ||
| const duration = Math.round(performance.now() - start); | ||
| const tokenCount = JavaContextProviderUtils.calculateTokenCount(items); | ||
| const telemetryData: any = { | ||
| "action": "resolveJavaContext", | ||
| "completionId": request.completionId, | ||
| "duration": duration, | ||
| "itemCount": items.length, | ||
| "tokenCount": tokenCount, | ||
| "status": status | ||
| }; | ||
| if (error) { | ||
| telemetryData.error = error; | ||
| } | ||
| sendInfo("", telemetryData); | ||
| } | ||
|
|
||
| async function resolveJavaContext(request: ResolveRequest, copilotCancel: vscode.CancellationToken): Promise<SupportedContextItem[]> { | ||
| const items: SupportedContextItem[] = []; | ||
| const start = performance.now(); | ||
| try { | ||
| // Check for cancellation before starting | ||
| JavaContextProviderUtils.checkCancellation(copilotCancel); | ||
| // Resolve project dependencies and convert to context items | ||
| const projectDependencyItems = await CopilotHelper.resolveAndConvertProjectDependencies( | ||
| vscode.window.activeTextEditor, | ||
| copilotCancel, | ||
| JavaContextProviderUtils.checkCancellation | ||
| ); | ||
| JavaContextProviderUtils.checkCancellation(copilotCancel); | ||
| items.push(...projectDependencyItems); | ||
|
|
||
| JavaContextProviderUtils.checkCancellation(copilotCancel); | ||
|
|
||
| // Resolve local imports and convert to context items | ||
| const localImportItems = await CopilotHelper.resolveAndConvertLocalImports( | ||
| vscode.window.activeTextEditor, | ||
| copilotCancel, | ||
| JavaContextProviderUtils.checkCancellation | ||
| ); | ||
| JavaContextProviderUtils.checkCancellation(copilotCancel); | ||
| items.push(...localImportItems); | ||
| } catch (error: any) { | ||
| if (error instanceof CopilotCancellationError) { | ||
| sendContextTelemetry(request, start, items, "cancelled_by_copilot"); | ||
| throw error; | ||
| } | ||
| if (error instanceof vscode.CancellationError || error.message === CancellationError.CANCELED) { | ||
| sendContextTelemetry(request, start, items, "cancelled_internally"); | ||
| throw new InternalCancellationError(); | ||
| } | ||
|
|
||
| // Send telemetry for general errors (but continue with partial results) | ||
| sendContextTelemetry(request, start, items, "error_partial_results", error.message || "unknown_error"); | ||
|
|
||
| // Return partial results and log completion for error case | ||
| return items; | ||
| } | ||
|
|
||
| // Send telemetry data once at the end for success case | ||
| sendContextTelemetry(request, start, items, "succeeded"); | ||
|
|
||
| return items; | ||
| } | ||
|
|
||
| export async function installContextProvider( | ||
| copilotAPI: CopilotApi, | ||
| contextProvider: ContextProvider<SupportedContextItem> | ||
| ): Promise<vscode.Disposable | undefined> { | ||
| const hasGetContextProviderAPI = typeof copilotAPI.getContextProviderAPI === 'function'; | ||
| if (hasGetContextProviderAPI) { | ||
| const contextAPI = await copilotAPI.getContextProviderAPI('v1'); | ||
| if (contextAPI) { | ||
| return contextAPI.registerContextProvider(contextProvider); | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
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.