Path: blob/main/extensions/copilot/src/platform/endpoint/node/autoChatEndpoint.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 { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';6import { IAuthenticationService } from '../../authentication/common/authentication';7import { IChatMLFetcher } from '../../chat/common/chatMLFetcher';8import { IConfigurationService } from '../../configuration/common/configurationService';9import { IEnvService } from '../../env/common/envService';10import { ILogService } from '../../log/common/logService';11import { IFetcherService } from '../../networking/common/fetcherService';12import { IChatEndpoint } from '../../networking/common/networking';13import { IChatWebSocketManager } from '../../networking/node/chatWebSocketManager';14import { IExperimentationService } from '../../telemetry/common/nullExperimentationService';15import { ITelemetryService } from '../../telemetry/common/telemetry';16import { ITokenizerProvider } from '../../tokenizer/node/tokenizer';17import { ICAPIClientService } from '../common/capiClient';18import { IDomainService } from '../common/domainService';19import { IChatModelInformation } from '../common/endpointProvider';20import { ChatEndpoint } from './chatEndpoint';21import { CopilotChatEndpoint } from './copilotChatEndpoint';2223/**24* This endpoint represents the "Auto" model in the model picker.25* It just effectively wraps a different endpoint and adds the auto stuff on top26*/27export class AutoChatEndpoint extends CopilotChatEndpoint {28public static readonly pseudoModelId = 'auto';2930constructor(31_wrappedEndpoint: IChatEndpoint,32_sessionToken: string,33_discountPercent: number,34public readonly discountRange: { low: number; high: number },35@IDomainService _domainService: IDomainService,36@ICAPIClientService _capiClientService: ICAPIClientService,37@IFetcherService _fetcherService: IFetcherService,38@IEnvService _envService: IEnvService,39@ITelemetryService _telemetryService: ITelemetryService,40@IAuthenticationService _authService: IAuthenticationService,41@IChatMLFetcher _chatMLFetcher: IChatMLFetcher,42@ITokenizerProvider _tokenizerProvider: ITokenizerProvider,43@IInstantiationService _instantiationService: IInstantiationService,44@IConfigurationService _configurationService: IConfigurationService,45@IExperimentationService _expService: IExperimentationService,46@IChatWebSocketManager _chatWebSocketService: IChatWebSocketManager,47@ILogService _logService: ILogService,48) {49super(50calculateAutoModelInfo(_wrappedEndpoint, _sessionToken, _discountPercent),51_domainService,52_capiClientService,53_fetcherService,54_envService,55_telemetryService,56_authService,57_chatMLFetcher,58_tokenizerProvider,59_instantiationService,60_configurationService,61_expService,62_chatWebSocketService,63_logService64);65}66}6768function calculateAutoModelInfo(endpoint: IChatEndpoint, sessionToken: string, discountPercent: number): IChatModelInformation {69let originalModelInfo: IChatModelInformation;70if (endpoint instanceof ChatEndpoint) {71originalModelInfo = endpoint.modelMetadata;72} else {73originalModelInfo = {74id: endpoint.model,75vendor: endpoint.modelProvider,76name: endpoint.name,77version: endpoint.version,78model_picker_enabled: endpoint.showInModelPicker,79is_chat_default: true,80is_chat_fallback: endpoint.isFallback,81capabilities: {82type: 'chat',83family: endpoint.family,84tokenizer: endpoint.tokenizer,85limits: {86max_prompt_tokens: endpoint.modelMaxPromptTokens,87max_output_tokens: endpoint.maxOutputTokens,88},89supports: {90tool_calls: endpoint.supportsToolCalls,91vision: endpoint.supportsVision,92prediction: endpoint.supportsPrediction,93streaming: true, // Assume streaming support for non-ChatEndpoint instances94},95},96billing: endpoint.isPremium !== undefined || endpoint.multiplier !== undefined || endpoint.restrictedToSkus !== undefined97? {98is_premium: endpoint.isPremium ?? false,99multiplier: endpoint.multiplier ?? 0,100restricted_to: endpoint.restrictedToSkus,101}102: undefined,103custom_model: endpoint.customModel,104};105}106// Calculate the multiplier including the discount percent, rounding to two decimal places107const newMultiplier = endpoint.multiplier !== undefined108? Math.round(endpoint.multiplier * (1 - discountPercent) * 100) / 100109: undefined;110const newModelInfo: IChatModelInformation = {111...originalModelInfo,112warning_messages: undefined,113model_picker_enabled: true,114info_messages: undefined,115billing: {116is_premium: originalModelInfo.billing?.is_premium,117multiplier: newMultiplier,118restricted_to: originalModelInfo.billing?.restricted_to119},120requestHeaders: {121...(originalModelInfo.requestHeaders || {}),122'Copilot-Session-Token': sessionToken123}124};125return newModelInfo;126}127128export function isAutoModel(endpoint: IChatEndpoint | undefined): number {129if (!endpoint) {130return -1;131}132return (endpoint.model === AutoChatEndpoint.pseudoModelId || endpoint instanceof AutoChatEndpoint) ? 1 : -1;133}134135136