|
| 1 | +import { |
| 2 | + CancellationToken, |
| 3 | + Disposable, |
| 4 | + DocumentSelector, |
| 5 | + DocumentUri, |
| 6 | + Position, |
| 7 | + TextEdit, |
| 8 | +} from 'vscode-languageserver-protocol'; |
| 9 | + |
| 10 | +/** |
| 11 | + * The ContextProvider API allows extensions to provide additional context items that |
| 12 | + * Copilot can use in its prompt. This file contains type definitions for the methods |
| 13 | + * and the data structures used by the API. |
| 14 | + * |
| 15 | + * Note: providing context is not enough to ensure that the context will be used in the prompt. |
| 16 | + * |
| 17 | + * The API is exposed as an export of the Copilot extension. To use it, you can cast the |
| 18 | + * exported object to the ContextProviderApiV1 interface. |
| 19 | + * |
| 20 | + * Example: |
| 21 | + * ``` |
| 22 | + * const copilot = vscode.extensions.getExtension("github.copilot"); |
| 23 | + * const contextProviderAPI = copilot.exports.getContextProviderAPI("v1") as ContextProviderApiV1; |
| 24 | + * ``` |
| 25 | + */ |
| 26 | +export interface ContextProviderApiV1 { |
| 27 | + registerContextProvider<T extends SupportedContextItem>(provider: ContextProvider<T>): Disposable; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Each extension can register a number of context providers, uniquely identified by their ID. |
| 32 | + * In addition, each provider has to provide: |
| 33 | + * - a DocumentSelector, to specify the file types for which the provider is active |
| 34 | + * - a ContextResolver, a function that returns the context items for a given request |
| 35 | + * |
| 36 | + * Example: |
| 37 | + * ``` |
| 38 | + * contextProviderAPI.registerContextProvider<Trait>({ |
| 39 | + * id: "pythonProvider", |
| 40 | + * selector: [{ language: "python" }], |
| 41 | + * resolver: { |
| 42 | + * resolve: async (request, token) => { |
| 43 | + * return [{name: 'traitName', value: 'traitValue'}]; |
| 44 | + * } |
| 45 | + * } |
| 46 | + * }); |
| 47 | + * ``` |
| 48 | + */ |
| 49 | +export interface ContextProvider<T extends SupportedContextItem> { |
| 50 | + id: string; |
| 51 | + selector: DocumentSelector; |
| 52 | + resolver: ContextResolver<T>; |
| 53 | +} |
| 54 | +export interface ContextResolver<T extends SupportedContextItem> { |
| 55 | + resolve(request: ResolveRequest, token: CancellationToken): Promise<T> | Promise<T[]> | AsyncIterable<T>; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * The first argument of the resolve method is a ResolveRequest object, which informs |
| 60 | + * the provider about: |
| 61 | + * - the completionId, a unique identifier for the completion request |
| 62 | + * - the documentContext, which contains information about the document for which the context is requested |
| 63 | + * - the activeExperiments, a map of active experiments and their values |
| 64 | + * - the timeBudget the provider has to provide context items |
| 65 | + * - the previousUsageStatistics, which contains information about the last request to the provider |
| 66 | + */ |
| 67 | +export type ResolutionStatus = 'full' | 'partial' | 'none' | 'error'; |
| 68 | +export type UsageStatus = ResolutionStatus | 'partial_content_excluded' | 'none_content_excluded'; |
| 69 | + |
| 70 | +export type ContextItemUsageDetails = { |
| 71 | + id: string; |
| 72 | + type: SupportedContextItemType; |
| 73 | + origin?: ContextItemOrigin; |
| 74 | +} & ( |
| 75 | + | { |
| 76 | + usage: Extract<UsageStatus, 'full' | 'partial' | 'partial_content_excluded'>; |
| 77 | + expectedTokens: number; |
| 78 | + actualTokens: number; |
| 79 | + } |
| 80 | + | {usage: Extract<UsageStatus, 'none' | 'none_content_excluded' | 'error'>} |
| 81 | +); |
| 82 | + |
| 83 | +export type ContextUsageStatistics = { |
| 84 | + usage: UsageStatus; |
| 85 | + resolution: ResolutionStatus; |
| 86 | + usageDetails?: ContextItemUsageDetails[]; |
| 87 | +}; |
| 88 | + |
| 89 | +export interface DocumentContext { |
| 90 | + uri: DocumentUri; |
| 91 | + languageId: string; |
| 92 | + version: number; |
| 93 | + /** |
| 94 | + * @deprecated Use `position` instead. |
| 95 | + */ |
| 96 | + offset: number; |
| 97 | + position: Position; |
| 98 | + proposedEdits?: TextEdit[]; |
| 99 | +} |
| 100 | +export interface ResolveRequest { |
| 101 | + // A unique ID to correlate the request with the completion request. |
| 102 | + completionId: string; |
| 103 | + documentContext: DocumentContext; |
| 104 | + |
| 105 | + activeExperiments: Map<string, string | number | boolean | string[]>; |
| 106 | + |
| 107 | + /** |
| 108 | + * The number of milliseconds for the context provider to provide context items. |
| 109 | + * After the time budget runs out, the request will be cancelled via the CancellationToken. |
| 110 | + * Providers can use this value as a hint when computing context. Providers should expect the |
| 111 | + * request to be cancelled once the time budget runs out. |
| 112 | + */ |
| 113 | + timeBudget: number; |
| 114 | + |
| 115 | + /** |
| 116 | + * Various statistics about the last completion request. This can be used by the context provider |
| 117 | + * to make decisions about what context to provide for the current call. |
| 118 | + */ |
| 119 | + previousUsageStatistics?: ContextUsageStatistics; |
| 120 | + |
| 121 | + /** |
| 122 | + * Data from completionItem |
| 123 | + * |
| 124 | + * See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionItem |
| 125 | + */ |
| 126 | + data?: unknown; |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * These are the data types that can be provided by a context provider. Any non-conforming |
| 131 | + * context items will be filtered out. |
| 132 | + */ |
| 133 | +interface ContextItem { |
| 134 | + /** |
| 135 | + * Specifies the relative importance with respect to items of the same type. |
| 136 | + * Cross-type comparisons is currently handled by the wishlist. |
| 137 | + * Accepted values are integers in the range [0, 100], where 100 is the highest importance. |
| 138 | + * Items with non-conforming importance values will be filtered out. |
| 139 | + * Default value is 0. |
| 140 | + */ |
| 141 | + importance?: number; |
| 142 | + |
| 143 | + /** |
| 144 | + * A unique ID for the context item, used to provide detailed statistics about |
| 145 | + * the item's usage. If an ID is not provided, it will be generated randomly. |
| 146 | + */ |
| 147 | + id?: string; |
| 148 | + |
| 149 | + /** |
| 150 | + * Specifies where the context item comes from, mostly relevant for LSP providers. |
| 151 | + * - request: context is provided in the completion request |
| 152 | + * - update: context is provided via context/update |
| 153 | + */ |
| 154 | + origin?: ContextItemOrigin; |
| 155 | +} |
| 156 | + |
| 157 | +// A key-value pair used for short string snippets. |
| 158 | +export interface Trait extends ContextItem { |
| 159 | + name: string; |
| 160 | + value: string; |
| 161 | +} |
| 162 | + |
| 163 | +// Code snippet extracted from a file. The URI is used for content exclusion. |
| 164 | +export interface CodeSnippet extends ContextItem { |
| 165 | + uri: string; |
| 166 | + value: string; |
| 167 | + // Additional URIs that contribute the same code snippet. |
| 168 | + additionalUris?: string[]; |
| 169 | +} |
| 170 | + |
| 171 | +export type SupportedContextItem = Trait | CodeSnippet; |
| 172 | +export type SupportedContextItemType = 'Trait' | 'CodeSnippet'; |
| 173 | +export type ContextItemOrigin = 'request' | 'update'; |
0 commit comments