Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/endpoint/node/autoChatEndpoint.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
7
import { IAuthenticationService } from '../../authentication/common/authentication';
8
import { IChatMLFetcher } from '../../chat/common/chatMLFetcher';
9
import { IConfigurationService } from '../../configuration/common/configurationService';
10
import { IEnvService } from '../../env/common/envService';
11
import { ILogService } from '../../log/common/logService';
12
import { IFetcherService } from '../../networking/common/fetcherService';
13
import { IChatEndpoint } from '../../networking/common/networking';
14
import { IChatWebSocketManager } from '../../networking/node/chatWebSocketManager';
15
import { IExperimentationService } from '../../telemetry/common/nullExperimentationService';
16
import { ITelemetryService } from '../../telemetry/common/telemetry';
17
import { ITokenizerProvider } from '../../tokenizer/node/tokenizer';
18
import { ICAPIClientService } from '../common/capiClient';
19
import { IDomainService } from '../common/domainService';
20
import { IChatModelInformation } from '../common/endpointProvider';
21
import { ChatEndpoint } from './chatEndpoint';
22
import { CopilotChatEndpoint } from './copilotChatEndpoint';
23
24
/**
25
* This endpoint represents the "Auto" model in the model picker.
26
* It just effectively wraps a different endpoint and adds the auto stuff on top
27
*/
28
export class AutoChatEndpoint extends CopilotChatEndpoint {
29
public static readonly pseudoModelId = 'auto';
30
31
constructor(
32
_wrappedEndpoint: IChatEndpoint,
33
_sessionToken: string,
34
_discountPercent: number,
35
public readonly discountRange: { low: number; high: number },
36
@IDomainService _domainService: IDomainService,
37
@ICAPIClientService _capiClientService: ICAPIClientService,
38
@IFetcherService _fetcherService: IFetcherService,
39
@IEnvService _envService: IEnvService,
40
@ITelemetryService _telemetryService: ITelemetryService,
41
@IAuthenticationService _authService: IAuthenticationService,
42
@IChatMLFetcher _chatMLFetcher: IChatMLFetcher,
43
@ITokenizerProvider _tokenizerProvider: ITokenizerProvider,
44
@IInstantiationService _instantiationService: IInstantiationService,
45
@IConfigurationService _configurationService: IConfigurationService,
46
@IExperimentationService _expService: IExperimentationService,
47
@IChatWebSocketManager _chatWebSocketService: IChatWebSocketManager,
48
@ILogService _logService: ILogService,
49
) {
50
super(
51
calculateAutoModelInfo(_wrappedEndpoint, _sessionToken, _discountPercent),
52
_domainService,
53
_capiClientService,
54
_fetcherService,
55
_envService,
56
_telemetryService,
57
_authService,
58
_chatMLFetcher,
59
_tokenizerProvider,
60
_instantiationService,
61
_configurationService,
62
_expService,
63
_chatWebSocketService,
64
_logService
65
);
66
}
67
}
68
69
function calculateAutoModelInfo(endpoint: IChatEndpoint, sessionToken: string, discountPercent: number): IChatModelInformation {
70
let originalModelInfo: IChatModelInformation;
71
if (endpoint instanceof ChatEndpoint) {
72
originalModelInfo = endpoint.modelMetadata;
73
} else {
74
originalModelInfo = {
75
id: endpoint.model,
76
vendor: endpoint.modelProvider,
77
name: endpoint.name,
78
version: endpoint.version,
79
model_picker_enabled: endpoint.showInModelPicker,
80
is_chat_default: true,
81
is_chat_fallback: endpoint.isFallback,
82
capabilities: {
83
type: 'chat',
84
family: endpoint.family,
85
tokenizer: endpoint.tokenizer,
86
limits: {
87
max_prompt_tokens: endpoint.modelMaxPromptTokens,
88
max_output_tokens: endpoint.maxOutputTokens,
89
},
90
supports: {
91
tool_calls: endpoint.supportsToolCalls,
92
vision: endpoint.supportsVision,
93
prediction: endpoint.supportsPrediction,
94
streaming: true, // Assume streaming support for non-ChatEndpoint instances
95
},
96
},
97
billing: endpoint.isPremium !== undefined || endpoint.multiplier !== undefined || endpoint.restrictedToSkus !== undefined
98
? {
99
is_premium: endpoint.isPremium ?? false,
100
multiplier: endpoint.multiplier ?? 0,
101
restricted_to: endpoint.restrictedToSkus,
102
}
103
: undefined,
104
custom_model: endpoint.customModel,
105
};
106
}
107
// Calculate the multiplier including the discount percent, rounding to two decimal places
108
const newMultiplier = endpoint.multiplier !== undefined
109
? Math.round(endpoint.multiplier * (1 - discountPercent) * 100) / 100
110
: undefined;
111
const newModelInfo: IChatModelInformation = {
112
...originalModelInfo,
113
warning_messages: undefined,
114
model_picker_enabled: true,
115
info_messages: undefined,
116
billing: {
117
is_premium: originalModelInfo.billing?.is_premium,
118
multiplier: newMultiplier,
119
restricted_to: originalModelInfo.billing?.restricted_to
120
},
121
requestHeaders: {
122
...(originalModelInfo.requestHeaders || {}),
123
'Copilot-Session-Token': sessionToken
124
}
125
};
126
return newModelInfo;
127
}
128
129
export function isAutoModel(endpoint: IChatEndpoint | undefined): number {
130
if (!endpoint) {
131
return -1;
132
}
133
return (endpoint.model === AutoChatEndpoint.pseudoModelId || endpoint instanceof AutoChatEndpoint) ? 1 : -1;
134
}
135
136