-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathcopilotCompletionContextProvider.ts
More file actions
375 lines (343 loc) · 21.1 KB
/
copilotCompletionContextProvider.ts
File metadata and controls
375 lines (343 loc) · 21.1 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import { ContextResolver, ResolveRequest, SupportedContextItem } from '@github/copilot-language-server';
import { randomUUID } from 'crypto';
import * as vscode from 'vscode';
import { DocumentSelector } from 'vscode-languageserver-protocol';
import { isNumber, isString } from '../common';
import { getOutputChannelLogger, Logger } from '../logger';
import * as telemetry from '../telemetry';
import { CopilotCompletionContextResult } from './client';
import { CopilotCompletionContextTelemetry } from './copilotCompletionContextTelemetry';
import { getCopilotApi } from './copilotProviders';
import { clients } from './extension';
import { ProjectContext } from './lmTool';
import { CppSettings } from './settings';
class DefaultValueFallback extends Error {
static readonly DefaultValue = "DefaultValue";
constructor() { super(DefaultValueFallback.DefaultValue); }
}
class CancellationError extends Error {
static readonly Canceled = "Canceled";
constructor() {
super(CancellationError.Canceled);
this.name = this.message;
}
}
class InternalCancellationError extends CancellationError {
}
class CopilotCancellationError extends CancellationError {
}
class CopilotContextProviderException extends Error {
}
class WellKnownErrors extends Error {
static readonly ClientNotFound = "ClientNotFound";
private constructor(message: string) { super(message); }
public static clientNotFound(): Error {
return new WellKnownErrors(WellKnownErrors.ClientNotFound);
}
}
// A bit mask for enabling features in the completion context.
export enum CopilotCompletionContextFeatures {
None = 0,
Instant = 1,
Deferred = 2,
}
// Mutually exclusive values for the kind of returned completion context. They either are:
// - computed.
// - obtained from the cache.
// - available in cache but stale (e.g. actual context is far away).
// - missing since the computation took too long and no cache is present (cache miss). The value
// is asynchronously computed and stored in cache.
// - the token is signaled as cancelled, in which case all the operations are aborted.
// - an unknown state.
export enum CopilotCompletionKind {
Computed = 'computed',
GotFromCache = 'gotFromCacheHit',
StaleCacheHit = 'staleCacheHit',
MissingCacheMiss = 'missingCacheMiss',
Canceled = 'canceled',
Unknown = 'unknown'
}
type CacheEntry = [string, CopilotCompletionContextResult];
export class CopilotCompletionContextProvider implements ContextResolver<SupportedContextItem> {
private static readonly providerId = 'ms-vscode.cpptools';
private readonly completionContextCache: Map<string, CacheEntry> = new Map();
private static readonly defaultCppDocumentSelector: DocumentSelector = [{ language: 'cpp' }, { language: 'c' }, { language: 'cuda-cpp' }];
// A percentage expressed as an integer number, i.e. 50 means 50%.
private static readonly defaultTimeBudgetFactor: number = 50;
private static readonly defaultMaxCaretDistance = 4096;
private completionContextCancellation = new vscode.CancellationTokenSource();
private contextProviderDisposable: vscode.Disposable | undefined;
private async waitForCompletionWithTimeoutAndCancellation<T>(promise: Promise<T>, defaultValue: T | undefined,
timeout: number, copilotToken: vscode.CancellationToken): Promise<[T | undefined, CopilotCompletionKind]> {
const defaultValuePromise = new Promise<T>((_resolve, reject) => setTimeout(() => {
if (copilotToken.isCancellationRequested) {
reject(new CancellationError());
} else {
reject(new DefaultValueFallback());
}
}, timeout));
const cancellationPromise = new Promise<T>((_, reject) => {
copilotToken.onCancellationRequested(() => {
reject(new CancellationError());
});
});
let snippetsOrNothing: T | undefined;
try {
snippetsOrNothing = await Promise.race([promise, cancellationPromise, defaultValuePromise]);
} catch (e) {
if (e instanceof DefaultValueFallback) {
return [defaultValue, defaultValue !== undefined ? CopilotCompletionKind.GotFromCache : CopilotCompletionKind.MissingCacheMiss];
} else if (e instanceof CancellationError) {
return [undefined, CopilotCompletionKind.Canceled];
} else {
throw e;
}
}
return [snippetsOrNothing, CopilotCompletionKind.Computed];
}
private static normalizeFeatureFlag(featureFlag: CopilotCompletionContextFeatures): CopilotCompletionContextFeatures {
// eslint-disable-next-line no-bitwise
if ((featureFlag & CopilotCompletionContextFeatures.Instant) === CopilotCompletionContextFeatures.Instant) { return CopilotCompletionContextFeatures.Instant; }
// eslint-disable-next-line no-bitwise
if ((featureFlag & CopilotCompletionContextFeatures.Deferred) === CopilotCompletionContextFeatures.Deferred) { return CopilotCompletionContextFeatures.Deferred; }
return CopilotCompletionContextFeatures.None;
}
// Get the completion context with a timeout and a cancellation token.
// The cancellationToken indicates that the value should not be returned nor cached.
private async getCompletionContextWithCancellation(context: ResolveRequest, featureFlag: CopilotCompletionContextFeatures,
startTime: number, out: Logger, telemetry: CopilotCompletionContextTelemetry, internalToken: vscode.CancellationToken):
Promise<CopilotCompletionContextResult | undefined> {
const documentUri = context.documentContext.uri;
const caretOffset = context.documentContext.offset;
let logMessage = `Copilot: getCompletionContext(${documentUri}:${caretOffset}):`;
try {
const snippetsFeatureFlag = CopilotCompletionContextProvider.normalizeFeatureFlag(featureFlag);
telemetry.addRequestMetadata(documentUri, caretOffset, context.completionId,
context.documentContext.languageId, { featureFlag: snippetsFeatureFlag });
const docUri = vscode.Uri.parse(documentUri);
const client = clients.getClientFor(docUri);
if (!client) { throw WellKnownErrors.clientNotFound(); }
const getCompletionContextStartTime = performance.now();
// Start collection of project traits concurrently with the completion context computation.
const projectContextPromise = (async (): Promise<ProjectContext | undefined> => {
const elapsedTimeMs = performance.now();
const projectContext = await client.getChatContext(docUri, internalToken);
telemetry.addCppStandardVersionMetadata(projectContext.standardVersion,
CopilotCompletionContextProvider.getRoundedDuration(elapsedTimeMs));
return projectContext;
})();
const copilotCompletionContext: CopilotCompletionContextResult =
await client.getCompletionContext(docUri, caretOffset, snippetsFeatureFlag, internalToken);
copilotCompletionContext.codeSnippetsCount = copilotCompletionContext.snippets.length;
telemetry.addRequestId(copilotCompletionContext.requestId);
logMessage += ` (id:${copilotCompletionContext.requestId})`;
// Collect project traits and if any add them to the completion context.
const projectContext = await projectContextPromise;
if (projectContext?.standardVersion) {
copilotCompletionContext.snippets.push({ name: "C++ language standard version", value: `This project uses the ${projectContext.standardVersion} language standard version.` });
copilotCompletionContext.traitsCount = 1;
}
let resultMismatch = false;
if (!copilotCompletionContext.areCodeSnippetsMissing) {
resultMismatch = copilotCompletionContext.translationUnitUri !== docUri.toString();
const cacheEntryId = randomUUID().toString();
this.completionContextCache.set(copilotCompletionContext.translationUnitUri, [cacheEntryId, copilotCompletionContext]);
const duration = CopilotCompletionContextProvider.getRoundedDuration(startTime);
telemetry.addCacheComputedData(duration, cacheEntryId);
if (resultMismatch) { logMessage += `, mismatch TU vs result`; }
logMessage += `, cached ${copilotCompletionContext.snippets.length} snippets in ${duration}ms`;
logMessage += `, response.featureFlag:${copilotCompletionContext.featureFlag},\
${copilotCompletionContext.translationUnitUri}:${copilotCompletionContext.caretOffset},\
snippetsCount:${copilotCompletionContext.codeSnippetsCount}, traitsCount:${copilotCompletionContext.traitsCount}`;
} else {
logMessage += ` (snippets are missing) `;
}
telemetry.addResponseMetadata(copilotCompletionContext.areCodeSnippetsMissing, copilotCompletionContext.snippets.length, copilotCompletionContext.codeSnippetsCount,
copilotCompletionContext.traitsCount, copilotCompletionContext.caretOffset, copilotCompletionContext.featureFlag);
telemetry.addComputeContextElapsed(CopilotCompletionContextProvider.getRoundedDuration(getCompletionContextStartTime));
return resultMismatch ? undefined : copilotCompletionContext;
} catch (e: any) {
if (e instanceof vscode.CancellationError || e.message === CancellationError.Canceled) {
telemetry.addInternalCanceled(CopilotCompletionContextProvider.getRoundedDuration(startTime));
logMessage += `, (internal cancellation)`;
throw InternalCancellationError;
}
if (e instanceof WellKnownErrors) {
telemetry.addWellKnownError(e.message);
}
telemetry.addError();
if (e.message && e.stack) {
out.appendLine(`Copilot: getCompletionContextWithCancellation(${documentUri}: ${caretOffset}): Error: '${e.message}', stack '${e.stack}`);
} else {
out.appendLine(`Copilot: getCompletionContextWithCancellation(${documentUri}: ${caretOffset}): Error: '${e}'`);
}
return undefined;
} finally {
out.appendLine(logMessage);
telemetry.send("cache");
}
}
static readonly CppCodeSnippetsEnabledFeatures = 'CppCodeSnippetsEnabledFeatures';
static readonly CppCodeSnippetsTimeBudgetFactor = 'CppCodeSnippetsTimeBudgetFactor';
static readonly CppCodeSnippetsMaxDistanceToCaret = 'CppCodeSnippetsMaxDistanceToCaret';
private async fetchTimeBudgetFactor(context: ResolveRequest): Promise<number> {
try {
const budgetFactor = context.activeExperiments.get(CopilotCompletionContextProvider.CppCodeSnippetsTimeBudgetFactor);
return (isNumber(budgetFactor) ? budgetFactor : CopilotCompletionContextProvider.defaultTimeBudgetFactor) / 100.0;
} catch (e) {
console.warn(`fetchTimeBudgetFactor(): error fetching ${CopilotCompletionContextProvider.CppCodeSnippetsTimeBudgetFactor}, using default: `, e);
return CopilotCompletionContextProvider.defaultTimeBudgetFactor;
}
}
private async fetchMaxDistanceToCaret(context: ResolveRequest): Promise<number> {
try {
const maxDistance = context.activeExperiments.get(CopilotCompletionContextProvider.CppCodeSnippetsMaxDistanceToCaret);
return isNumber(maxDistance) ? maxDistance : CopilotCompletionContextProvider.defaultMaxCaretDistance;
} catch (e) {
console.warn(`fetchMaxDistanceToCaret(): error fetching ${CopilotCompletionContextProvider.CppCodeSnippetsMaxDistanceToCaret}, using default: `, e);
return CopilotCompletionContextProvider.defaultMaxCaretDistance;
}
}
private async getEnabledFeatureNames(context: ResolveRequest): Promise<string[] | undefined> {
try {
const enabledFeatureNames = new CppSettings().cppCodeSnippetsFeatureNames ?? context.activeExperiments.get(CopilotCompletionContextProvider.CppCodeSnippetsEnabledFeatures);
if (isString(enabledFeatureNames)) {
return enabledFeatureNames.split(',').map(s => s.trim());
}
} catch (e) {
console.warn(`getEnabledFeatures(): error fetching ${CopilotCompletionContextProvider.CppCodeSnippetsEnabledFeatures}: `, e);
}
return undefined;
}
private async getEnabledFeatureFlag(context: ResolveRequest): Promise<CopilotCompletionContextFeatures | undefined> {
let result;
for (const featureName of await this.getEnabledFeatureNames(context) ?? []) {
const flag = CopilotCompletionContextFeatures[featureName as keyof typeof CopilotCompletionContextFeatures];
if (flag !== undefined) { result = (result ?? 0) + flag; }
}
return result;
}
private static getRoundedDuration(startTime: number): number {
return Math.round(performance.now() - startTime);
}
public static async Create() {
const copilotCompletionProvider = new CopilotCompletionContextProvider();
await copilotCompletionProvider.registerCopilotContextProvider();
return copilotCompletionProvider;
}
public dispose(): void {
this.completionContextCancellation.cancel();
this.contextProviderDisposable?.dispose();
}
public removeFile(fileUri: string): void {
this.completionContextCache.delete(fileUri);
}
public async resolve(context: ResolveRequest, copilotCancel: vscode.CancellationToken): Promise<SupportedContextItem[]> {
const resolveStartTime = performance.now();
const out: Logger = getOutputChannelLogger();
let logMessage = `Copilot: resolve(${context.documentContext.uri}:${context.documentContext.offset}): `;
const timeBudgetFactor = await this.fetchTimeBudgetFactor(context);
const maxCaretDistance = await this.fetchMaxDistanceToCaret(context);
const telemetry = new CopilotCompletionContextTelemetry();
let copilotCompletionContext: CopilotCompletionContextResult | undefined;
let copilotCompletionContextKind: CopilotCompletionKind = CopilotCompletionKind.Unknown;
let featureFlag: CopilotCompletionContextFeatures | undefined;
try {
featureFlag = await this.getEnabledFeatureFlag(context);
telemetry.addRequestMetadata(context.documentContext.uri, context.documentContext.offset,
context.completionId, context.documentContext.languageId, { featureFlag, timeBudgetFactor, maxCaretDistance });
if (featureFlag === undefined) { return []; }
this.completionContextCancellation.cancel();
this.completionContextCancellation = new vscode.CancellationTokenSource();
const docUri = context.documentContext.uri;
const cacheEntry: CacheEntry | undefined = this.completionContextCache.get(docUri.toString());
const defaultValue = cacheEntry?.[1];
const computeSnippetsPromise = this.getCompletionContextWithCancellation(context, featureFlag,
resolveStartTime, out, telemetry.fork(), this.completionContextCancellation.token);
[copilotCompletionContext, copilotCompletionContextKind] = await this.waitForCompletionWithTimeoutAndCancellation(
computeSnippetsPromise, defaultValue, context.timeBudget * timeBudgetFactor, copilotCancel);
// Fix up copilotCompletionContextKind accounting for stale-cache-hits.
if (copilotCompletionContextKind === CopilotCompletionKind.GotFromCache &&
copilotCompletionContext && cacheEntry) {
telemetry.addCacheHitEntryGuid(cacheEntry[0]);
const cachedData = cacheEntry[1];
if (Math.abs(cachedData.caretOffset - context.documentContext.offset) > maxCaretDistance) {
copilotCompletionContextKind = CopilotCompletionKind.StaleCacheHit;
copilotCompletionContext.snippets = [];
}
}
telemetry.addCompletionContextKind(copilotCompletionContextKind);
// Handle cancellation.
if (copilotCompletionContextKind === CopilotCompletionKind.Canceled) {
const duration: number = CopilotCompletionContextProvider.getRoundedDuration(resolveStartTime);
telemetry.addCopilotCanceled(duration);
throw new CopilotCancellationError();
}
logMessage += ` (id:${copilotCompletionContext?.requestId}) `;
return copilotCompletionContext?.snippets ?? [];
} catch (e: any) {
if (e instanceof CopilotCancellationError) {
telemetry.addCopilotCanceled(CopilotCompletionContextProvider.getRoundedDuration(resolveStartTime));
logMessage += ` (copilot cancellation) `;
throw e;
}
if (e instanceof InternalCancellationError) {
telemetry.addInternalCanceled(CopilotCompletionContextProvider.getRoundedDuration(resolveStartTime));
logMessage += ` (internal cancellation) `;
throw e;
}
if (e instanceof CancellationError) { throw e; }
// For any other exception's type, it is an error.
telemetry.addError();
throw e;
} finally {
const duration: number = CopilotCompletionContextProvider.getRoundedDuration(resolveStartTime);
logMessage += ` featureFlag:${featureFlag?.toString()},`;
if (copilotCompletionContext === undefined) {
logMessage += ` result is undefined and no snippets provided (${copilotCompletionContextKind.toString()}), elapsed time:${duration}ms`;
} else {
const uri = copilotCompletionContext.translationUnitUri ? copilotCompletionContext.translationUnitUri : "<undefined-uri>";
logMessage += ` for ${uri} provided ${copilotCompletionContext.codeSnippetsCount} code-snippets (${copilotCompletionContextKind.toString()},\
${copilotCompletionContext?.areCodeSnippetsMissing ? " missing code-snippets" : ""}) and ${copilotCompletionContext.traitsCount} traits, elapsed time:${duration}ms`;
}
telemetry.addResponseMetadata(copilotCompletionContext?.areCodeSnippetsMissing ?? true,
copilotCompletionContext?.snippets?.length,
copilotCompletionContext?.codeSnippetsCount, copilotCompletionContext?.traitsCount,
copilotCompletionContext?.caretOffset, copilotCompletionContext?.featureFlag);
telemetry.addResolvedElapsed(duration);
telemetry.addCacheSize(this.completionContextCache.size);
telemetry.send();
out.appendLine(logMessage);
}
}
public async registerCopilotContextProvider(): Promise<void> {
const properties: Record<string, string> = {};
const registerCopilotContextProvider = 'registerCopilotContextProvider';
try {
const copilotApi = await getCopilotApi();
if (!copilotApi) { throw new CopilotContextProviderException("getCopilotApi() returned null, Copilot is missing or inactive."); }
const hasGetContextProviderAPI = "getContextProviderAPI" in copilotApi;
if (!hasGetContextProviderAPI) { throw new CopilotContextProviderException("getContextProviderAPI() is not available."); }
const contextAPI = await copilotApi.getContextProviderAPI("v1");
if (!contextAPI) { throw new CopilotContextProviderException("getContextProviderAPI(v1) returned null."); }
this.contextProviderDisposable = contextAPI.registerContextProvider({
id: CopilotCompletionContextProvider.providerId,
selector: CopilotCompletionContextProvider.defaultCppDocumentSelector,
resolver: this
});
properties["cppCodeSnippetsProviderRegistered"] = "true";
} catch (e) {
console.debug("Failed to register the Copilot Context Provider.");
properties["error"] = "Failed to register the Copilot Context Provider";
if (e instanceof CopilotContextProviderException) {
properties["error"] += `: ${e.message}`;
}
} finally {
telemetry.logCopilotEvent(registerCopilotContextProvider, { ...properties });
}
}
}