Path: blob/main/extensions/copilot/src/platform/languageServer/common/languageContextService.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*--------------------------------------------------------------------------------------------*/45import type * as vscode from 'vscode';6import { createServiceIdentifier } from '../../../util/common/services';78export const ILanguageContextService = createServiceIdentifier<ILanguageContextService>('ILanguageContextService');910export enum ContextKind {11Snippet = 'snippet',12Trait = 'trait',13DiagnosticBag = 'diagnosticBag'14}1516/**17* A context item represents a piece of code usually taken18* from a source file.19*/20export interface SnippetContext {21/**22* The kind of the context.23*/24kind: ContextKind.Snippet;2526/**27* A unique ID for the context item, used to provide28* detailed statistics about the item's usage. If an ID29* is not provided, it will be generated randomly.30*/31id?: string;3233/**34* The priority of the snippet. Value range is [0, 1].35*/36priority: number;3738/**39* The main source the snippet is extracted from.40*/41uri: vscode.Uri;4243/**44* Additional sources if available.45*/46additionalUris?: vscode.Uri[];4748/**49* The actual snippet value.50*/51value: string;52}5354export interface TraitContext {55/**56* The kind of the context.57*/58kind: ContextKind.Trait;5960/**61* A unique ID for the context item, used to provide62* detailed statistics about the item's usage. If an ID63* is not provided, it will be generated randomly.64*/65id?: string;6667/**68* The priority of the context.69*/70priority: number;7172/**73* The name of the trait.74*/75name: string;7677/**78* The value of the trait.79*/80value: string;81}8283export interface DiagnosticBagContext {84/**85* The kind of the context.86*/87kind: ContextKind.DiagnosticBag;8889/**90* A unique ID for the context item, used to provide91* detailed statistics about the item's usage. If an ID92* is not provided, it will be generated randomly.93*/94id?: string;9596/**97* The priority of the context.98*/99priority: number;100101/**102* The resource the diagnostics are associated with.103*/104uri: vscode.Uri;105106/**107* The diagnostics.108*/109values: vscode.Diagnostic[];110}111112export type ContextItem = SnippetContext | TraitContext | DiagnosticBagContext;113114export enum KnownSources {115unknown = 'unknown',116sideCar = 'sideCar',117completion = 'completion',118populateCache = 'populateCache',119nes = 'nes',120chat = 'chat',121fix = 'fix'122}123124export enum TriggerKind {125unknown = 'unknown',126selection = 'selection',127completion = 'completion',128}129130export type RequestContext = {131/**132* A unique request id.133*/134requestId: string;135136/**137* The opportunity ID provided by VS Code core.138*/139opportunityId?: string;140141/**142* The time budget in milliseconds to compute the context.143*/144timeBudget?: number;145146/**147* The token budget to compute the context.148*/149tokenBudget?: number;150151/**152* The source of the request.153*/154source?: KnownSources | string;155156/**157* The158*/159trigger?: TriggerKind | undefined;160161/**162* A list of proposed edits that should be applied before computing the context.163*/164proposedEdits?: { edit: vscode.TextEdit; source?: 'selectedCompletionInfo' }[];165166/**167* If provided the telemetry will be sampled. A value of 1 will log every request, a value of168* 5 will log every 5th request, a value of 10 will log every 10th request, etc. If not provided169* all telemetry will be logged.170*/171sampleTelemetry?: number;172};173174export interface ILanguageContextService {175readonly _serviceBrand: undefined;176177/**178* Checks whether is language server context is activated for the179* given text document or language.180*/181isActivated(documentOrLanguageId: vscode.TextDocument | string): Promise<boolean>;182183/**184* Populates the cache with context information for the given document and position.185*186* @param document The document to populate the cache for.187* @param position The position in the document to populate the cache for.188* @param context The context for the request.189*/190populateCache(document: vscode.TextDocument, position: vscode.Position, context: RequestContext): Promise<void>;191192/**193* Retrieves the context for the given document and position.194*195* @param document The document to retrieve the context for.196* @param position The position in the document to retrieve the context for.197* @param context The context for the request.198* @param token A cancellation token.199* @returns A promise that resolves to an array of context items.200*/201getContext(document: vscode.TextDocument, position: vscode.Position, context: RequestContext, token: vscode.CancellationToken): AsyncIterable<ContextItem>;202203/**204* Retrieves the context for the given document and position when a request timeout is reached.205* This method is called when the `getContext` request takes too long to complete. Items206* returned by this method are usually coming from a cache and might be not a 100% accurate.207*208* This method is optional and should complete execution quickly, ideally without any209* block or long-running operations.210*211* @param document The document to retrieve the context for.212* @param position The position in the document to retrieve the context for.213* @param context The context for the request.214* @returns An array of `ContextItem` or `undefined`.215*/216getContextOnTimeout(document: vscode.TextDocument, position: vscode.Position, context: RequestContext): readonly ContextItem[] | undefined;217}218219class EmptyAsyncIterable<T> implements AsyncIterable<T> {220public async *[Symbol.asyncIterator](): AsyncIterator<T> {221}222}223export const NullLanguageContextService: ILanguageContextService = {224_serviceBrand: undefined,225isActivated: async () => false,226populateCache: async () => { },227getContext: () => new EmptyAsyncIterable<ContextItem>(),228getContextOnTimeout: () => [],229};230231