Path: blob/main/extensions/copilot/src/platform/endpoint/common/endpointProvider.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*--------------------------------------------------------------------------------------------*/456import { RequestMetadata } from '@vscode/copilot-api';7import type { LanguageModelChat } from 'vscode';8import { createServiceIdentifier } from '../../../util/common/services';9import { TokenizerType } from '../../../util/common/tokenizer';10import { Event } from '../../../util/vs/base/common/event';11import type { ChatRequest } from '../../../vscodeTypes';12import { IChatEndpoint, IEmbeddingsEndpoint } from '../../networking/common/networking';1314export type CustomModel = {15key_name: string;16owner_name: string;17};1819export type EndpointEditToolName = 'find-replace' | 'multi-find-replace' | 'apply-patch' | 'code-rewrite';2021const allEndpointEditToolNames: ReadonlySet<EndpointEditToolName> = new Set([22'find-replace',23'multi-find-replace',24'apply-patch',25'code-rewrite'26]);2728export function isEndpointEditToolName(toolName: string): toolName is EndpointEditToolName {29return allEndpointEditToolNames.has(toolName as EndpointEditToolName);30}3132export type IChatModelCapabilities = {33type: 'chat';34family: string;35tokenizer: TokenizerType;36limits?: {37max_prompt_tokens?: number;38max_output_tokens?: number;39max_context_window_tokens?: number;40vision?: {41max_prompt_images?: number;42};43};44supports: {45parallel_tool_calls?: boolean;46tool_calls?: boolean;47// Whether or not the model supports streaming, if not explicitly true we will try to parse the response as not streamed48streaming: boolean | undefined;49vision?: boolean;50prediction?: boolean;51thinking?: boolean;52adaptive_thinking?: boolean;53max_thinking_budget?: number;54min_thinking_budget?: number;55reasoning_effort?: string[];56tool_search?: boolean;57context_editing?: boolean;58};59};6061export type IEmbeddingModelCapabilities = {62type: 'embeddings';63family: string;64tokenizer: TokenizerType;65limits?: { max_inputs?: number };66};6768type ICompletionModelCapabilities = {69type: 'completion';70family: string;71tokenizer: TokenizerType;72};7374export enum ModelSupportedEndpoint {75ChatCompletions = '/chat/completions',76Responses = '/responses',77WebSocketResponses = 'ws:/responses',78Messages = '/v1/messages'79}8081export interface IModelTokenPrices {82batch_size: number;83cache_price: number;84input_price: number;85output_price: number;86}8788export interface IModelBilling {89is_premium?: boolean;90multiplier?: number;91restricted_to?: string[];92token_prices?: IModelTokenPrices;93}9495export interface IModelAPIResponse {96id: string;97vendor: string;98name: string;99model_picker_enabled: boolean;100preview?: boolean;101is_chat_default: boolean;102is_chat_fallback: boolean;103version: string;104warning_messages?: { code: string; message: string }[];105info_messages?: { code: string; message: string }[];106billing?: IModelBilling;107capabilities: IChatModelCapabilities | ICompletionModelCapabilities | IEmbeddingModelCapabilities;108supported_endpoints?: ModelSupportedEndpoint[];109custom_model?: CustomModel;110}111112export type IChatModelInformation = IModelAPIResponse & {113capabilities: IChatModelCapabilities;114urlOrRequestMetadata?: string | RequestMetadata;115requestHeaders?: Readonly<Record<string, string>>;116zeroDataRetentionEnabled?: boolean;117};118119export function isChatModelInformation(model: IModelAPIResponse): model is IChatModelInformation {120return model.capabilities.type === 'chat';121}122123export function isEmbeddingModelInformation(model: IModelAPIResponse): model is IEmbeddingModelInformation {124return model.capabilities.type === 'embeddings';125}126127export type IEmbeddingModelInformation = IModelAPIResponse & { capabilities: IEmbeddingModelCapabilities };128129export type ICompletionModelInformation = IModelAPIResponse & {130capabilities: ICompletionModelCapabilities;131};132133export function isCompletionModelInformation(model: IModelAPIResponse): model is ICompletionModelInformation {134return model.capabilities.type === 'completion';135}136137export type ChatEndpointFamily = 'copilot-base' | 'copilot-fast';138export type EmbeddingsEndpointFamily = 'text3small' | 'metis';139140export interface IEndpointProvider {141readonly _serviceBrand: undefined;142143/**144* Fires whenever model metadata is refreshed from the server.145* Does not always indicate there is a change, just that the data is fresh.146*/147readonly onDidModelsRefresh: Event<void>;148149/**150* Gets all the completion models known by the endpoint provider.151*/152getAllCompletionModels(forceRefresh?: boolean): Promise<ICompletionModelInformation[]>;153154/**155* Gets all the chat endpoints known by the endpoint provider. Mainly used by language model access156*/157getAllChatEndpoints(): Promise<IChatEndpoint[]>;158159/**160* Given a chat request returns the appropriate chat endpoint to serve that request161* @param requestOrFamily The chat request to get the endpoint for, the family you want the endpoint for, or the LanguageModelChat.162*/163getChatEndpoint(requestOrFamily: LanguageModelChat | ChatRequest | ChatEndpointFamily): Promise<IChatEndpoint>;164165/**166* Get the CAPI embedding endpoint information167*/168getEmbeddingsEndpoint(family?: EmbeddingsEndpointFamily): Promise<IEmbeddingsEndpoint>;169}170171export const IEndpointProvider = createServiceIdentifier<IEndpointProvider>('IEndpointProvider');172173174