Path: blob/main/extensions/copilot/src/platform/inlineCompletions/common/api.ts
13401 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/4import type * as vscode from 'vscode';56export namespace Copilot {78export type DocumentUri = string;910export type Position = {11line: number;12character: number;13};1415export type Range = {16start: Position;17end: Position;18};1920/**21* The ContextProvider API allows extensions to provide additional context items that22* Copilot can use in its prompt. This file contains type definitions for the methods23* and the data structures used by the API.24*25* Note: providing context is not enough to ensure that the context will be used in the prompt.26*27* The API is exposed as an export of the Copilot extension. To use it, you can cast the28* exported object to the ContextProviderApiV1 interface.29*30* Example:31* ```32* const copilot = vscode.extensions.getExtension("github.copilot");33* const contextProviderAPI = copilot.exports.getContextProviderAPI("v1") as ContextProviderApiV1;34* ```35*/36export interface ContextProviderApiV1 {37registerContextProvider<T extends SupportedContextItem>(provider: ContextProvider<T>): vscode.Disposable;38}3940/**41* Each extension can register a number of context providers, uniquely identified by their ID.42* In addition, each provider has to provide:43* - a DocumentSelector, to specify the file types for which the provider is active44* - a ContextResolver, a function that returns the context items for a given request45*46* Example:47* ```48* contextProviderAPI.registerContextProvider<Trait>({49* id: "pythonProvider",50* selector: [{ language: "python" }],51* resolver: {52* resolve: async (request, token) => {53* return [{name: 'traitName', value: 'traitValue'}];54* }55* }56* });57* ```58*/59export interface ContextProvider<T extends SupportedContextItem> {60id: string;61selector: vscode.DocumentSelector;62resolver: ContextResolver<T>;63}6465export interface ContextResolver<T extends SupportedContextItem> {66resolve(request: ResolveRequest, token: vscode.CancellationToken): Promise<T> | Promise<T[]> | AsyncIterable<T>;67// Optional method to be invoked if the request timed out. This requests additional context items.68resolveOnTimeout?(request: ResolveRequest): T | readonly T[] | undefined;69}7071/**72* The first argument of the resolve method is a ResolveRequest object, which informs73* the provider about:74* - the completionId, a unique identifier for the completion request75* - the documentContext, which contains information about the document for which the context is requested76* - the activeExperiments, a map of active experiments and their values77* - the timeBudget the provider has to provide context items78* - the previousUsageStatistics, which contains information about the last request to the provider79*/80export type Status = 'full' | 'partial' | 'none';8182export type ContextUsageStatistics = {83usage: Status;84resolution: Status;85};8687interface TextEdit {88/**89* The range of the text document to be manipulated. To insert90* text into a document create a range where start === end.91*/92range: Range;93/**94* The string to be inserted. For delete operations use an95* empty string.96*/97newText: string;98}99100export type ProposedTextEdit = TextEdit & {101positionAfterEdit: Position;102// Indicates whether the edit is suggested by the IDE. Otherwise it's assumed to be speculative103source?: 'selectedCompletionInfo';104};105106export interface DocumentContext {107uri: DocumentUri;108languageId: string;109version: number;110// Position and offset are relative to the provided version of the document.111// The position after an edit is applied is found in ProposedTextEdit.positionAfterEdit.112/**113* @deprecated Use `position` instead.114*/115offset: number;116position?: Position;117proposedEdits?: ProposedTextEdit[];118}119export interface ResolveRequest {120// A unique ID to correlate the request with the completion request.121completionId: string;122// Optional the opportunity ID provided by VS Code core.123opportunityId?: string;124125documentContext: DocumentContext;126127activeExperiments: Map<string, string | number | boolean | string[]>;128129/**130* The number of milliseconds for the context provider to provide context items.131* After the time budget runs out, the request will be cancelled via the CancellationToken.132* Providers can use this value as a hint when computing context. Providers should expect the133* request to be cancelled once the time budget runs out.134*135* @deprecated Use `timeoutEnd` instead.136*/137timeBudget: number;138139/**140* Unix timestamp representing the exact time the request will be cancelled via the CancellationToken.141*/142timeoutEnd: number;143144/**145* Various statistics about the last completion request. This can be used by the context provider146* to make decisions about what context to provide for the current call.147*/148previousUsageStatistics?: ContextUsageStatistics;149150/**151* Data from completionItem152*153* See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionItem154*/155data?: unknown;156157/**158* Allows specifying the source of the context item, e.g., 'nes'.159*160* @experimental161*/162source: string;163}164165/**166* These are the data types that can be provided by a context provider. Any non-conforming167* context items will be filtered out.168*/169interface ContextItem {170/**171* Specifies the relative importance with respect to items of the same type.172* Cross-type comparisons is currently handled by the wishlist.173* Accepted values are integers in the range [0, 100], where 100 is the highest importance.174* Items with non-conforming importance values will be filtered out.175* Default value is 0.176*/177importance?: number;178179/**180* A unique ID for the context item, used to provide detailed statistics about181* the item's usage. If an ID is not provided, it will be generated randomly.182*/183id?: string;184}185186// A key-value pair used for short string snippets.187export interface Trait extends ContextItem {188name: string;189value: string;190}191192// Code snippet extracted from a file. The URI is used for content exclusion.193export interface CodeSnippet extends ContextItem {194uri: string;195value: string;196// Additional URIs that contribute the same code snippet.197additionalUris?: string[];198}199200export interface DiagnosticBag extends ContextItem {201uri: vscode.Uri;202values: vscode.Diagnostic[];203}204205export type SupportedContextItem = Trait | CodeSnippet | DiagnosticBag;206}207208209