forked from google-gemini/gemini-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelRouterService.ts
More file actions
132 lines (119 loc) · 4.27 KB
/
modelRouterService.ts
File metadata and controls
132 lines (119 loc) · 4.27 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
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { Config } from '../config/config.js';
import {
PREVIEW_GEMINI_MODEL,
DEFAULT_GEMINI_MODEL,
} from '../config/models.js';
import type {
RoutingContext,
RoutingDecision,
TerminalStrategy,
} from './routingStrategy.js';
import { DefaultStrategy } from './strategies/defaultStrategy.js';
import { ClassifierStrategy } from './strategies/classifierStrategy.js';
import { CompositeStrategy } from './strategies/compositeStrategy.js';
import { FallbackStrategy } from './strategies/fallbackStrategy.js';
import { OverrideStrategy } from './strategies/overrideStrategy.js';
import { ImageStrategy } from './strategies/ImageStrategy.js';
import { ScriptOutputSummarizationStrategy } from './strategies/scriptOutputSummarizationStrategy.js';
import { logModelRouting } from '../telemetry/loggers.js';
import { ModelRoutingEvent } from '../telemetry/types.js';
/**
* A centralized service for making model routing decisions.
*/
export class ModelRouterService {
private config: Config;
private strategy: TerminalStrategy;
constructor(config: Config) {
this.config = config;
this.strategy = this.initializeDefaultStrategy();
}
private initializeDefaultStrategy(): TerminalStrategy {
// Initialize the composite strategy with the desired priority order.
// The strategies are ordered in order of highest priority.
return new CompositeStrategy(
[
new FallbackStrategy(),
new OverrideStrategy(),
new ScriptOutputSummarizationStrategy(),
new ScriptOutputSummarizationStrategy(),
new ImageStrategy(),
new ClassifierStrategy(),
new DefaultStrategy(),
],
'agent-router',
);
}
/**
* Determines which model to use for a given request context.
*
* @param context The full context of the request.
* @returns A promise that resolves to a RoutingDecision.
*/
async route(context: RoutingContext): Promise<RoutingDecision> {
const startTime = Date.now();
let decision: RoutingDecision;
try {
decision = await this.strategy.route(
context,
this.config,
this.config.getBaseLlmClient(),
);
// Unified Preview Model Logic:
// If the decision is to use 'gemini-2.5-pro' and preview features are enabled,
// we attempt to upgrade to 'gemini-3.0-pro' (Preview Model).
if (
decision.model === DEFAULT_GEMINI_MODEL &&
this.config.getPreviewFeatures() &&
!decision.metadata.source.includes('override')
) {
// We ALWAYS attempt to upgrade to Preview Model here.
// If we are in fallback mode, the 'previewModelBypassMode' flag (handled in handler.ts/geminiChat.ts)
// will ensure we downgrade to 2.5 Pro for the actual API call if needed.
// This allows us to "probe" Preview Model periodically (i.e., every new request tries Preview Model first).
decision.model = PREVIEW_GEMINI_MODEL;
decision.metadata.source += ' (Preview Model)';
decision.metadata.reasoning += ' (Upgraded to Preview Model)';
}
const event = new ModelRoutingEvent(
decision.model,
decision.metadata.source,
decision.metadata.latencyMs,
decision.metadata.reasoning,
false, // failed
undefined, // error_message
);
logModelRouting(this.config, event);
return decision;
} catch (e) {
const failed = true;
const error_message = e instanceof Error ? e.message : String(e);
// Create a fallback decision for logging purposes
// We do not actually route here. This should never happen so we should
// fail loudly to catch any issues where this happens.
decision = {
model: this.config.getModel(),
metadata: {
source: 'router-exception',
latencyMs: Date.now() - startTime,
reasoning: 'An exception occurred during routing.',
error: error_message,
},
};
const event = new ModelRoutingEvent(
decision.model,
decision.metadata.source,
decision.metadata.latencyMs,
decision.metadata.reasoning,
failed,
error_message,
);
logModelRouting(this.config, event);
throw e;
}
}
}