|
| 1 | +/* -------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. |
| 3 | + * See 'LICENSE' in the project root for license information. |
| 4 | + * ------------------------------------------------------------------------------------------ */ |
| 5 | +import * as vscode from 'vscode'; |
| 6 | +import { DocumentSelector } from 'vscode-languageserver-protocol'; |
| 7 | +import { getOutputChannelLogger, Logger } from '../logger'; |
| 8 | +import * as telemetry from '../telemetry'; |
| 9 | +import { getCopilotApi } from "./copilotProviders"; |
| 10 | +import { clients } from './extension'; |
| 11 | +import { CodeSnippet, CompletionContext, ContextProviderApiV1 } from './tmp/contextProviderV1'; |
| 12 | + |
| 13 | +// An ever growing cache of completion context snippets. //?? TODO Evict old entries. |
| 14 | +const completionContextCache: Map<string, CodeSnippet[]> = new Map<string, CodeSnippet[]>(); |
| 15 | +const cppDocumentSelector: DocumentSelector = [{ language: 'cpp' }, { language: 'c' }]; |
| 16 | + |
| 17 | +class DefaultValueFallback extends Error { |
| 18 | + static readonly DefaultValue = "DefaultValue"; |
| 19 | + constructor() { super(DefaultValueFallback.DefaultValue); } |
| 20 | +} |
| 21 | + |
| 22 | +class CancellationError extends Error { |
| 23 | + static readonly Cancelled = "Cancelled"; |
| 24 | + constructor() { super(CancellationError.Cancelled); } |
| 25 | +} |
| 26 | + |
| 27 | +let completionContextCancellation = new vscode.CancellationTokenSource(); |
| 28 | + |
| 29 | +// Mutually exclusive values for the kind of snippets. They either come from the cache, |
| 30 | +// are computed, or the computation is taking too long and no cache is present. In the latter |
| 31 | +// case, the cache is computed anyway while unblocking the execution flow returning undefined. |
| 32 | +enum SnippetsKind { |
| 33 | + Computed = 'computed', |
| 34 | + CacheHit = 'cacheHit', |
| 35 | + CacheMiss = 'cacheMiss' |
| 36 | +} |
| 37 | + |
| 38 | +// Get the default value if the timeout expires, but throws an exception if the token is cancelled. |
| 39 | +async function waitForCompletionWithTimeoutAndCancellation<T>(promise: Promise<T>, defaultValue: T | undefined, |
| 40 | + timeout: number, token: vscode.CancellationToken): Promise<[T | undefined, SnippetsKind]> { |
| 41 | + const defaultValuePromise = new Promise<T>((resolve, reject) => setTimeout(() => { |
| 42 | + if (token.isCancellationRequested) { |
| 43 | + reject('DefaultValuePromise was cancelled'); |
| 44 | + } else { |
| 45 | + reject(new DefaultValueFallback()); |
| 46 | + } |
| 47 | + }, timeout)); |
| 48 | + const cancellationPromise = new Promise<T>((_, reject) => { |
| 49 | + token.onCancellationRequested(() => { |
| 50 | + reject(new CancellationError()); |
| 51 | + }); |
| 52 | + }); |
| 53 | + let snippetsOrNothing: T | undefined; |
| 54 | + try { |
| 55 | + snippetsOrNothing = await Promise.race([promise, cancellationPromise, defaultValuePromise]); |
| 56 | + } catch (e) { |
| 57 | + if (e instanceof DefaultValueFallback) { |
| 58 | + return [defaultValue, defaultValue !== undefined ? SnippetsKind.CacheHit : SnippetsKind.CacheMiss]; |
| 59 | + } |
| 60 | + |
| 61 | + // Rethrow the error for cancellation cases. |
| 62 | + throw e; |
| 63 | + } |
| 64 | + |
| 65 | + return [snippetsOrNothing, SnippetsKind.Computed]; |
| 66 | +} |
| 67 | + |
| 68 | +// Get the completion context with a timeout and a cancellation token. |
| 69 | +// The cancellationToken indicates that the value should not be returned nor cached. |
| 70 | +async function getCompletionContextWithCancellation(documentUri: string, caretOffset: number, |
| 71 | + startTime: number, out: Logger, token: vscode.CancellationToken): Promise<CodeSnippet[]> { |
| 72 | + try { |
| 73 | + const activeEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor; |
| 74 | + if (!activeEditor || |
| 75 | + activeEditor.document.uri.toString() !== vscode.Uri.parse(documentUri).toString()) { |
| 76 | + return []; |
| 77 | + } |
| 78 | + |
| 79 | + const snippets = await clients.ActiveClient.getCompletionContext(activeEditor.document.uri, caretOffset, token); |
| 80 | + |
| 81 | + const codeSnippets = snippets.context.map((item) => { |
| 82 | + if (token.isCancellationRequested) { |
| 83 | + throw new CancellationError(); |
| 84 | + } |
| 85 | + return { |
| 86 | + importance: item.importance, uri: item.uri, value: item.text |
| 87 | + }; |
| 88 | + }); |
| 89 | + |
| 90 | + completionContextCache.set(documentUri, codeSnippets); |
| 91 | + const duration: number = Date.now() - startTime; |
| 92 | + out.appendLine(`Copilot: getCompletionContextWithCancellation(): Cached in [ms]: ${duration}`); |
| 93 | + // //?? TODO Add telemetry for elapsed time. |
| 94 | + |
| 95 | + return codeSnippets; |
| 96 | + } catch (e) { |
| 97 | + const err = e as Error; |
| 98 | + out.appendLine(`Copilot: getCompletionContextWithCancellation(): Error: '${err?.message}', stack '${err?.stack}`); |
| 99 | + |
| 100 | + // //?? TODO Add telemetry for failure. |
| 101 | + return []; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +const timeBudgetFactor: number = 0.5; |
| 106 | +const cppToolsResolver = { |
| 107 | + async resolve(context: CompletionContext, copilotAborts: vscode.CancellationToken): Promise<CodeSnippet[]> { |
| 108 | + const startTime = Date.now(); |
| 109 | + const out: Logger = getOutputChannelLogger(); |
| 110 | + let snippetsKind: SnippetsKind = SnippetsKind.Computed; |
| 111 | + try { |
| 112 | + completionContextCancellation.cancel(); |
| 113 | + completionContextCancellation = new vscode.CancellationTokenSource(); |
| 114 | + const docUri = context.documentContext.uri; |
| 115 | + const cachedValue: CodeSnippet[] | undefined = completionContextCache.get(docUri.toString()); |
| 116 | + const snippetsPromise = getCompletionContextWithCancellation(docUri, |
| 117 | + context.documentContext.offset, startTime, out, completionContextCancellation.token); |
| 118 | + const [codeSnippets, kind] = await waitForCompletionWithTimeoutAndCancellation( |
| 119 | + snippetsPromise, cachedValue, context.timeBudget * timeBudgetFactor, copilotAborts); |
| 120 | + snippetsKind = kind; |
| 121 | + // //?? TODO Add telemetry for Computed vs Cached. |
| 122 | + |
| 123 | + return codeSnippets ?? []; |
| 124 | + } catch (e: any) { |
| 125 | + if (e instanceof CancellationError) { |
| 126 | + out.appendLine(`Copilot: getCompletionContext(): cancelled!`); |
| 127 | + } |
| 128 | + // //?? TODO Add telemetry for failure. |
| 129 | + } finally { |
| 130 | + const duration: number = Date.now() - startTime; |
| 131 | + out.appendLine(`Copilot: getCompletionContext(): snippets retrieval (${snippetsKind.toString()}) elapsed time (ms): ${duration}`); |
| 132 | + // //?? TODO Add telemetry for elapsed time. |
| 133 | + } |
| 134 | + |
| 135 | + return []; |
| 136 | + } |
| 137 | +}; |
| 138 | + |
| 139 | +export async function registerCopilotContextProvider(): Promise<void> { |
| 140 | + try { |
| 141 | + const isCustomSnippetProviderApiEnabled = await telemetry.isExperimentEnabled("CppToolsCustomSnippetsApi"); |
| 142 | + if (isCustomSnippetProviderApiEnabled) { |
| 143 | + const contextAPI = (await getCopilotApi() as any).getContextProviderAPI('v1') as ContextProviderApiV1; |
| 144 | + contextAPI.registerContextProvider({ |
| 145 | + id: 'cppTools', |
| 146 | + selector: cppDocumentSelector, |
| 147 | + resolver: cppToolsResolver |
| 148 | + }); |
| 149 | + } |
| 150 | + } catch { |
| 151 | + console.warn("Failed to register the Copilot Context Provider."); |
| 152 | + // //?? TODO Add telemetry for failure. |
| 153 | + } |
| 154 | +} |
0 commit comments