-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathcontextProvider.ts
More file actions
153 lines (142 loc) · 6.01 KB
/
contextProvider.ts
File metadata and controls
153 lines (142 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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;
}