-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathcontextProvider.ts
More file actions
184 lines (171 loc) · 6.81 KB
/
contextProvider.ts
File metadata and controls
184 lines (171 loc) · 6.81 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*---------------------------------------------------------------------------------------------
* 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,
sendContextResolutionTelemetry
} 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 [];
}
};
}
async function resolveJavaContext(request: ResolveRequest, copilotCancel: vscode.CancellationToken): Promise<SupportedContextItem[]> {
const items: SupportedContextItem[] = [];
const start = performance.now();
let duration: number;
let dependenciesResult: CopilotHelper.IResolveResult | undefined;
let importsResult: CopilotHelper.IResolveResult | undefined;
try {
// Check for cancellation before starting
JavaContextProviderUtils.checkCancellation(copilotCancel);
// Resolve project dependencies and convert to context items
dependenciesResult = await CopilotHelper.resolveAndConvertProjectDependencies(
vscode.window.activeTextEditor,
copilotCancel,
JavaContextProviderUtils.checkCancellation
);
JavaContextProviderUtils.checkCancellation(copilotCancel);
items.push(...dependenciesResult.items);
JavaContextProviderUtils.checkCancellation(copilotCancel);
// Resolve local imports and convert to context items
importsResult = await CopilotHelper.resolveAndConvertLocalImports(
vscode.window.activeTextEditor,
copilotCancel,
JavaContextProviderUtils.checkCancellation
);
JavaContextProviderUtils.checkCancellation(copilotCancel);
items.push(...importsResult.items);
} catch (error: any) {
if (error instanceof CopilotCancellationError) {
duration = Math.round(performance.now() - start);
sendContextResolutionTelemetry(
request,
duration,
items,
"cancelled_by_copilot",
undefined,
dependenciesResult?.emptyReason,
importsResult?.emptyReason,
dependenciesResult?.itemCount,
importsResult?.itemCount
);
throw error;
}
if (error instanceof vscode.CancellationError || error.message === CancellationError.CANCELED) {
duration = Math.round(performance.now() - start);
sendContextResolutionTelemetry(
request,
duration,
items,
"cancelled_internally",
undefined,
dependenciesResult?.emptyReason,
importsResult?.emptyReason,
dependenciesResult?.itemCount,
importsResult?.itemCount
);
throw new InternalCancellationError();
}
// Send telemetry for general errors (but continue with partial results)
duration = Math.round(performance.now() - start);
sendContextResolutionTelemetry(
request,
duration,
items,
"error_partial_results",
error.message || "unknown_error",
dependenciesResult?.emptyReason,
importsResult?.emptyReason,
dependenciesResult?.itemCount,
importsResult?.itemCount
);
// Return partial results and log completion for error case
return items;
}
// Send telemetry data once at the end for success case
duration = Math.round(performance.now() - start);
sendContextResolutionTelemetry(
request,
duration,
items,
"succeeded",
undefined,
dependenciesResult?.emptyReason,
importsResult?.emptyReason,
dependenciesResult?.itemCount,
importsResult?.itemCount
);
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;
}