-
Notifications
You must be signed in to change notification settings - Fork 39.1k
Expand file tree
/
Copy pathautomodeService.ts
More file actions
407 lines (363 loc) · 16.9 KB
/
automodeService.ts
File metadata and controls
407 lines (363 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { RequestType } from '@vscode/copilot-api';
import type { ChatRequest } from 'vscode';
import { FetchedValue } from '../../../shared-fetch-utils/common/fetchedValue';
import { createServiceIdentifier } from '../../../util/common/services';
import { Disposable, DisposableMap } from '../../../util/vs/base/common/lifecycle';
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
import { ChatLocation } from '../../../vscodeTypes';
import { IAuthenticationService } from '../../authentication/common/authentication';
import { ConfigKey, IConfigurationService } from '../../configuration/common/configurationService';
import { IEnvService } from '../../env/common/envService';
import { ILogService } from '../../log/common/logService';
import { createCapiClientFetchedValue } from '../../networking/common/capiClientFetchedValue';
import { isAbortError } from '../../networking/common/fetcherService';
import { IChatEndpoint } from '../../networking/common/networking';
import { IRequestLogger } from '../../requestLogger/node/requestLogger';
import { IExperimentationService } from '../../telemetry/common/nullExperimentationService';
import { ITelemetryService } from '../../telemetry/common/telemetry';
import { ICAPIClientService } from '../common/capiClient';
import { AutoChatEndpoint } from './autoChatEndpoint';
import { RouterDecisionFetcher, RoutingContextSignals } from './routerDecisionFetcher';
interface AutoModeAPIResponse {
available_models: string[];
expires_at: number;
discounted_costs?: { [key: string]: number };
session_token: string;
}
interface AutoModelCacheEntry {
endpoint: AutoChatEndpoint;
tokenBank: AutoModeTokenBank;
lastSessionToken?: string;
lastRoutedPrompt?: string;
routerFallbackReason?: string;
turnCount: number;
needsReEval: boolean;
}
class AutoModeTokenBank extends Disposable {
private readonly _fetchedValue: FetchedValue<AutoModeAPIResponse>;
private _usedSinceLastFetch = false;
constructor(
public debugName: string,
location: ChatLocation,
capiClientService: ICAPIClientService,
authService: IAuthenticationService,
_logService: ILogService,
expService: IExperimentationService,
envService: IEnvService,
) {
super();
const expName = location === ChatLocation.Editor
? 'copilotchat.autoModelHint.editor'
: 'copilotchat.autoModelHint';
this._fetchedValue = this._register(createCapiClientFetchedValue<AutoModeAPIResponse>(capiClientService, envService, {
request: async () => {
const authToken = (await authService.getCopilotToken()).token;
const autoModeHint = expService.getTreatmentVariable<string>(expName) || 'auto';
return {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`,
},
method: 'POST' as const,
json: { auto_mode: { model_hints: [autoModeHint] } },
};
},
requestMetadata: { type: RequestType.AutoModels },
parseResponse: async (res) => {
if (res.status < 200 || res.status >= 300) {
const text = await res.text().catch(() => '');
throw new Error(`AutoMode token response status: ${res.status}${text ? `, body: ${text}` : ''}`);
}
const data = await res.json() as AutoModeAPIResponse;
this._usedSinceLastFetch = false;
return data;
},
isStale: (token) => {
if (!this._usedSinceLastFetch) {
return false;
}
return token.expires_at * 1000 - Date.now() < 5 * 60 * 1000;
},
keepCacheHot: true,
}));
}
async getToken(): Promise<AutoModeAPIResponse> {
this._usedSinceLastFetch = true;
return this._fetchedValue.resolve();
}
}
export const IAutomodeService = createServiceIdentifier<IAutomodeService>('IAutomodeService');
export interface IAutomodeService {
readonly _serviceBrand: undefined;
resolveAutoModeEndpoint(chatRequest: ChatRequest | undefined, knownEndpoints: IChatEndpoint[]): Promise<IChatEndpoint>;
/**
* Marks the router cache for this conversation as needing re-evaluation.
* The next call to {@link resolveAutoModeEndpoint} will re-run the router
* instead of returning the cached endpoint.
*/
invalidateRouterCache(chatRequest: ChatRequest): void;
}
export class AutomodeService extends Disposable implements IAutomodeService {
readonly _serviceBrand: undefined;
private readonly _autoModelCache: Map<string, AutoModelCacheEntry> = new Map();
private _reserveTokens: DisposableMap<ChatLocation, AutoModeTokenBank> = new DisposableMap();
private readonly _routerDecisionFetcher: RouterDecisionFetcher;
constructor(
@ICAPIClientService private readonly _capiClientService: ICAPIClientService,
@IAuthenticationService private readonly _authService: IAuthenticationService,
@ILogService private readonly _logService: ILogService,
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@IExperimentationService private readonly _expService: IExperimentationService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@IEnvService private readonly _envService: IEnvService,
@ITelemetryService private readonly _telemetryService: ITelemetryService,
@IRequestLogger private readonly _requestLogger: IRequestLogger,
) {
super();
this._register(this._authService.onDidAuthenticationChange(() => {
for (const entry of this._autoModelCache.values()) {
entry.tokenBank.dispose();
}
this._autoModelCache.clear();
const keys = Array.from(this._reserveTokens.keys());
this._reserveTokens.clearAndDisposeAll();
for (const location of keys) {
this._reserveTokens.set(location, new AutoModeTokenBank('reserve', location, this._capiClientService, this._authService, this._logService, this._expService, this._envService));
}
}));
this._serviceBrand = undefined;
this._routerDecisionFetcher = new RouterDecisionFetcher(this._capiClientService, this._authService, this._logService, this._telemetryService, this._requestLogger);
}
override dispose(): void {
for (const entry of this._autoModelCache.values()) {
entry.tokenBank.dispose();
}
this._autoModelCache.clear();
this._reserveTokens.dispose();
super.dispose();
}
/**
* Resolve an auto mode endpoint
* Optionally uses a router model to select the best endpoint based on the prompt.
*/
invalidateRouterCache(chatRequest: ChatRequest): void {
const conversationId = chatRequest.sessionResource?.toString() ?? chatRequest.sessionId ?? 'unknown';
const entry = this._autoModelCache.get(conversationId);
if (entry) {
entry.needsReEval = true;
this._logService.trace(`[AutomodeService] Router cache invalidated for conversation ${conversationId}`);
}
}
async resolveAutoModeEndpoint(chatRequest: ChatRequest | undefined, knownEndpoints: IChatEndpoint[]): Promise<IChatEndpoint> {
if (!knownEndpoints.length) {
throw new Error('No auto mode endpoints provided.');
}
const conversationId = chatRequest?.sessionResource?.toString() ?? chatRequest?.sessionId ?? 'unknown';
const entry = this._autoModelCache.get(conversationId);
const tokenBank = this._acquireTokenBank(entry, chatRequest?.location, conversationId);
const token = await tokenBank.getToken();
// After the first turn, skip the router unless explicitly invalidated
// (e.g. after conversation compaction/summarization). Token refresh and
// default model selection still run so available-model changes are respected.
const skipRouter = entry !== undefined && entry.turnCount > 0 && !entry.needsReEval;
if (entry?.needsReEval) {
entry.needsReEval = false;
}
const routerResult = skipRouter
? { lastRoutedPrompt: chatRequest?.prompt?.trim() ?? entry?.lastRoutedPrompt }
: await this._tryRouterSelection(chatRequest, conversationId, entry, token, knownEndpoints);
let selectedModel = routerResult.selectedModel;
const lastRoutedPrompt = routerResult.lastRoutedPrompt;
const routerFallbackReason = routerResult.fallbackReason;
// Default model selection when router was skipped or failed
if (!selectedModel) {
if (routerFallbackReason) {
/* __GDPR__
"automode.routerFallback" : {
"owner": "lramos15",
"comment": "Reports when the auto mode router is skipped or fails and falls back to default model selection",
"reason": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "comment": "The reason the router was skipped or failed (hasImage, noMatchingEndpoint, routerError, routerTimeout)" }
}
*/
this._telemetryService.sendMSFTTelemetryEvent('automode.routerFallback', {
reason: routerFallbackReason,
});
}
selectedModel = this._selectDefaultModel(entry?.endpoint?.modelProvider, token.available_models, knownEndpoints);
}
selectedModel = this._applyVisionFallback(chatRequest, selectedModel, token.available_models, knownEndpoints);
// Reuse the cached endpoint if the session token and model haven't changed
const autoEndpoint = (entry?.endpoint && entry.lastSessionToken === token.session_token && entry.endpoint.model === selectedModel.model)
? entry.endpoint
: this._instantiationService.createInstance(AutoChatEndpoint, selectedModel, token.session_token, token.discounted_costs?.[selectedModel.model] || 0, this._calculateDiscountRange(token.discounted_costs));
const isNewTurn = !entry || lastRoutedPrompt !== entry.lastRoutedPrompt;
this._autoModelCache.set(conversationId, {
endpoint: autoEndpoint,
tokenBank,
lastSessionToken: token.session_token,
lastRoutedPrompt,
routerFallbackReason,
turnCount: (entry?.turnCount ?? 0) + (isNewTurn ? 1 : 0),
needsReEval: false,
});
return autoEndpoint;
}
private _acquireTokenBank(entry: AutoModelCacheEntry | undefined, location: ChatLocation | undefined, conversationId: string): AutoModeTokenBank {
if (entry) {
return entry.tokenBank;
}
const loc = location ?? ChatLocation.Panel;
const tokenBank = this._reserveTokens.deleteAndLeak(loc) || new AutoModeTokenBank('reserve', loc, this._capiClientService, this._authService, this._logService, this._expService, this._envService);
this._reserveTokens.set(loc, new AutoModeTokenBank('reserve', loc, this._capiClientService, this._authService, this._logService, this._expService, this._envService));
tokenBank.debugName = conversationId;
return tokenBank;
}
private async _tryRouterSelection(
chatRequest: ChatRequest | undefined,
conversationId: string,
entry: AutoModelCacheEntry | undefined,
token: AutoModeAPIResponse,
knownEndpoints: IChatEndpoint[],
): Promise<{ selectedModel?: IChatEndpoint; lastRoutedPrompt?: string; fallbackReason?: string }> {
const prompt = chatRequest?.prompt?.trim();
const lastRoutedPrompt = entry?.lastRoutedPrompt ?? prompt;
if (hasImage(chatRequest)) {
return { lastRoutedPrompt, fallbackReason: 'hasImage' };
}
if (!this._isRouterEnabled(chatRequest) || conversationId === 'unknown') {
return { lastRoutedPrompt };
}
if (!prompt?.length) {
return { lastRoutedPrompt, fallbackReason: 'emptyPrompt' };
}
// Prompt hasn't changed since last decision — skip router but allow endpoint refresh
if (entry && entry.lastRoutedPrompt === prompt) {
return { lastRoutedPrompt };
}
try {
const contextSignals: RoutingContextSignals = {
session_id: conversationId !== 'unknown' ? conversationId : undefined,
reference_count: chatRequest?.references?.length,
prompt_char_count: prompt.length,
previous_model: entry?.endpoint?.model,
turn_number: (entry?.turnCount ?? 0) + 1,
};
const routingMethod = this._configurationService.getExperimentBasedConfig(ConfigKey.TeamInternal.AutoModeRoutingMethod, this._expService) || undefined;
const result = await this._routerDecisionFetcher.getRouterDecision(prompt, token.session_token, token.available_models, undefined, contextSignals, chatRequest?.sessionId, chatRequest?.id, routingMethod);
if (result.fallback) {
this._logService.info(`[AutomodeService] Router signaled fallback: ${result.fallback_reason ?? 'unknown'}, routing_method=${result.routing_method ?? 'n/a'}`);
return { lastRoutedPrompt: prompt, fallbackReason: 'routerFallback' };
}
if (!result.candidate_models.length) {
return { lastRoutedPrompt: prompt, fallbackReason: 'emptyCandidateList' };
}
// Prefer same-provider model, then fall back to the router's top candidate
const selectedModel = (entry?.endpoint && this._findSameProviderModel(entry.endpoint.modelProvider, result.candidate_models, knownEndpoints))
?? knownEndpoints.find(e => e.model === result.candidate_models[0]);
if (!selectedModel) {
return { lastRoutedPrompt: prompt, fallbackReason: 'noMatchingEndpoint' };
}
if (result.sticky_override) {
this._logService.trace(`[AutomodeService] Sticky routing override: confidence=${(result.confidence * 100).toFixed(1)}%, label=${result.predicted_label}, router_model=${result.candidate_models[0]}, actual_model=${selectedModel.model}`);
}
return { selectedModel, lastRoutedPrompt: prompt };
} catch (e) {
const isTimeout = isAbortError(e);
const fallbackReason = isTimeout ? 'routerTimeout' : 'routerError';
this._logService.error(`Failed to get routed model for conversation ${conversationId} (${fallbackReason}):`, (e as Error).message);
return { lastRoutedPrompt: prompt, fallbackReason };
}
}
private _selectDefaultModel(currentModelProvider: string | undefined, availableModels: string[], knownEndpoints: IChatEndpoint[]): IChatEndpoint {
const selectedModel = (currentModelProvider && this._findSameProviderModel(currentModelProvider, availableModels, knownEndpoints))
?? this._findFirstAvailableModel(availableModels, knownEndpoints);
if (!selectedModel) {
const errorMsg = 'Auto mode failed: no available model found in known endpoints.';
this._logService.error(errorMsg);
throw new Error(errorMsg);
}
return selectedModel;
}
private _isRouterEnabled(chatRequest: ChatRequest | undefined): boolean {
const isPanelChat = !chatRequest?.location || chatRequest?.location === ChatLocation.Panel;
return isPanelChat && this._configurationService.getExperimentBasedConfig(ConfigKey.TeamInternal.UseAutoModeRouting, this._expService);
}
/**
* Find the first model in available_models that has a known endpoint.
*/
private _findFirstAvailableModel(availableModels: string[], knownEndpoints: IChatEndpoint[]): IChatEndpoint | undefined {
for (const model of availableModels) {
const endpoint = knownEndpoints.find(e => e.model === model);
if (endpoint) {
return endpoint;
}
}
return undefined;
}
/**
* Find the first model in available_models whose knownEndpoint has the same modelProvider
* as the current model. Skips any model that doesn't have a known endpoint.
*/
private _findSameProviderModel(currentModelProvider: string, availableModels: string[], knownEndpoints: IChatEndpoint[]): IChatEndpoint | undefined {
for (const model of availableModels) {
const endpoint = knownEndpoints.find(e => e.model === model);
if (endpoint && endpoint.modelProvider === currentModelProvider) {
return endpoint;
}
}
return undefined;
}
/**
* If the request contains an image and the selected model doesn't support vision,
* fall back to the first vision-capable model from the available models.
*/
private _applyVisionFallback(chatRequest: ChatRequest | undefined, selectedModel: IChatEndpoint, availableModels: string[], knownEndpoints: IChatEndpoint[]): IChatEndpoint {
if (!hasImage(chatRequest) || selectedModel.supportsVision) {
return selectedModel;
}
const visionModel = availableModels
.map(model => knownEndpoints.find(e => e.model === model))
.find(endpoint => endpoint?.supportsVision);
if (visionModel) {
this._logService.trace(`Selected model '${selectedModel.model}' does not support vision, falling back to '${visionModel.model}'.`);
return visionModel;
}
this._logService.warn(`Request contains an image but no vision-capable model is available.`);
return selectedModel;
}
private _calculateDiscountRange(discounts: Record<string, number> | undefined): { low: number; high: number } {
if (!discounts) {
return { low: 0, high: 0 };
}
let low = Infinity;
let high = -Infinity;
let hasValues = false;
for (const value of Object.values(discounts)) {
hasValues = true;
if (value < low) {
low = value;
}
if (value > high) {
high = value;
}
}
return hasValues ? { low, high } : { low: 0, high: 0 };
}
}
function hasImage(chatRequest: ChatRequest | undefined): boolean {
if (!chatRequest || !chatRequest.references) {
return false;
}
return chatRequest.references.some(ref => {
const value = ref.value;
return typeof value === 'object' &&
value !== null &&
'mimeType' in value &&
typeof value.mimeType === 'string'
&& value.mimeType.startsWith('image/');
});
}