Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/inlineCompletions/common/api.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import type * as vscode from 'vscode';
6
7
export namespace Copilot {
8
9
export type DocumentUri = string;
10
11
export type Position = {
12
line: number;
13
character: number;
14
};
15
16
export type Range = {
17
start: Position;
18
end: Position;
19
};
20
21
/**
22
* The ContextProvider API allows extensions to provide additional context items that
23
* Copilot can use in its prompt. This file contains type definitions for the methods
24
* and the data structures used by the API.
25
*
26
* Note: providing context is not enough to ensure that the context will be used in the prompt.
27
*
28
* The API is exposed as an export of the Copilot extension. To use it, you can cast the
29
* exported object to the ContextProviderApiV1 interface.
30
*
31
* Example:
32
* ```
33
* const copilot = vscode.extensions.getExtension("github.copilot");
34
* const contextProviderAPI = copilot.exports.getContextProviderAPI("v1") as ContextProviderApiV1;
35
* ```
36
*/
37
export interface ContextProviderApiV1 {
38
registerContextProvider<T extends SupportedContextItem>(provider: ContextProvider<T>): vscode.Disposable;
39
}
40
41
/**
42
* Each extension can register a number of context providers, uniquely identified by their ID.
43
* In addition, each provider has to provide:
44
* - a DocumentSelector, to specify the file types for which the provider is active
45
* - a ContextResolver, a function that returns the context items for a given request
46
*
47
* Example:
48
* ```
49
* contextProviderAPI.registerContextProvider<Trait>({
50
* id: "pythonProvider",
51
* selector: [{ language: "python" }],
52
* resolver: {
53
* resolve: async (request, token) => {
54
* return [{name: 'traitName', value: 'traitValue'}];
55
* }
56
* }
57
* });
58
* ```
59
*/
60
export interface ContextProvider<T extends SupportedContextItem> {
61
id: string;
62
selector: vscode.DocumentSelector;
63
resolver: ContextResolver<T>;
64
}
65
66
export interface ContextResolver<T extends SupportedContextItem> {
67
resolve(request: ResolveRequest, token: vscode.CancellationToken): Promise<T> | Promise<T[]> | AsyncIterable<T>;
68
// Optional method to be invoked if the request timed out. This requests additional context items.
69
resolveOnTimeout?(request: ResolveRequest): T | readonly T[] | undefined;
70
}
71
72
/**
73
* The first argument of the resolve method is a ResolveRequest object, which informs
74
* the provider about:
75
* - the completionId, a unique identifier for the completion request
76
* - the documentContext, which contains information about the document for which the context is requested
77
* - the activeExperiments, a map of active experiments and their values
78
* - the timeBudget the provider has to provide context items
79
* - the previousUsageStatistics, which contains information about the last request to the provider
80
*/
81
export type Status = 'full' | 'partial' | 'none';
82
83
export type ContextUsageStatistics = {
84
usage: Status;
85
resolution: Status;
86
};
87
88
interface TextEdit {
89
/**
90
* The range of the text document to be manipulated. To insert
91
* text into a document create a range where start === end.
92
*/
93
range: Range;
94
/**
95
* The string to be inserted. For delete operations use an
96
* empty string.
97
*/
98
newText: string;
99
}
100
101
export type ProposedTextEdit = TextEdit & {
102
positionAfterEdit: Position;
103
// Indicates whether the edit is suggested by the IDE. Otherwise it's assumed to be speculative
104
source?: 'selectedCompletionInfo';
105
};
106
107
export interface DocumentContext {
108
uri: DocumentUri;
109
languageId: string;
110
version: number;
111
// Position and offset are relative to the provided version of the document.
112
// The position after an edit is applied is found in ProposedTextEdit.positionAfterEdit.
113
/**
114
* @deprecated Use `position` instead.
115
*/
116
offset: number;
117
position?: Position;
118
proposedEdits?: ProposedTextEdit[];
119
}
120
export interface ResolveRequest {
121
// A unique ID to correlate the request with the completion request.
122
completionId: string;
123
// Optional the opportunity ID provided by VS Code core.
124
opportunityId?: string;
125
126
documentContext: DocumentContext;
127
128
activeExperiments: Map<string, string | number | boolean | string[]>;
129
130
/**
131
* The number of milliseconds for the context provider to provide context items.
132
* After the time budget runs out, the request will be cancelled via the CancellationToken.
133
* Providers can use this value as a hint when computing context. Providers should expect the
134
* request to be cancelled once the time budget runs out.
135
*
136
* @deprecated Use `timeoutEnd` instead.
137
*/
138
timeBudget: number;
139
140
/**
141
* Unix timestamp representing the exact time the request will be cancelled via the CancellationToken.
142
*/
143
timeoutEnd: number;
144
145
/**
146
* Various statistics about the last completion request. This can be used by the context provider
147
* to make decisions about what context to provide for the current call.
148
*/
149
previousUsageStatistics?: ContextUsageStatistics;
150
151
/**
152
* Data from completionItem
153
*
154
* See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionItem
155
*/
156
data?: unknown;
157
158
/**
159
* Allows specifying the source of the context item, e.g., 'nes'.
160
*
161
* @experimental
162
*/
163
source: string;
164
}
165
166
/**
167
* These are the data types that can be provided by a context provider. Any non-conforming
168
* context items will be filtered out.
169
*/
170
interface ContextItem {
171
/**
172
* Specifies the relative importance with respect to items of the same type.
173
* Cross-type comparisons is currently handled by the wishlist.
174
* Accepted values are integers in the range [0, 100], where 100 is the highest importance.
175
* Items with non-conforming importance values will be filtered out.
176
* Default value is 0.
177
*/
178
importance?: number;
179
180
/**
181
* A unique ID for the context item, used to provide detailed statistics about
182
* the item's usage. If an ID is not provided, it will be generated randomly.
183
*/
184
id?: string;
185
}
186
187
// A key-value pair used for short string snippets.
188
export interface Trait extends ContextItem {
189
name: string;
190
value: string;
191
}
192
193
// Code snippet extracted from a file. The URI is used for content exclusion.
194
export interface CodeSnippet extends ContextItem {
195
uri: string;
196
value: string;
197
// Additional URIs that contribute the same code snippet.
198
additionalUris?: string[];
199
}
200
201
export interface DiagnosticBag extends ContextItem {
202
uri: vscode.Uri;
203
values: vscode.Diagnostic[];
204
}
205
206
export type SupportedContextItem = Trait | CodeSnippet | DiagnosticBag;
207
}
208
209