-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel-router.ts
More file actions
300 lines (260 loc) · 8.71 KB
/
Copy pathmodel-router.ts
File metadata and controls
300 lines (260 loc) · 8.71 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
/**
* ModelRouter: Intelligent multi-model selection for PRODREADY agents
*
* Strategy:
* - Haiku 4: Deterministic operations (git, formatting, file I/O) - 85% cost savings
* - Sonnet 4.5: Complex reasoning, code generation (most agents)
* - Opus 4.5: Critical validation (Evaluator agent) - quality over cost
*
* Reference: MULTI_MODEL_ROUTING_STRATEGY.md
*/
export enum ModelTier {
HAIKU = 'claude-haiku-4-20250901',
SONNET = 'claude-sonnet-4-5-20250929',
OPUS = 'claude-opus-4-5-20251101'
}
export type AgentType =
| 'orchestrator'
| 'planner'
| 'security'
| 'playwright'
| 'evaluator' // ALWAYS Opus (non-negotiable)
| 'git' // ALWAYS Haiku (deterministic only)
| 'reporting';
export type TaskType =
| 'default'
| 'create_branch'
| 'format_sarif'
| 'generate_playwright_test'
| 'validate_finding'
| 'append_cycle_record'
| 'compute_diff_scope'
| 'execute_remediation_plan'
| 'generate_narrative';
export interface TaskComplexity {
reasoning_depth: 'shallow' | 'medium' | 'deep';
code_generation: boolean;
validation_stakes: 'low' | 'medium' | 'high';
deterministic: boolean;
}
export interface ModelConfig {
model: ModelTier;
temperature: number;
}
export interface ModelUsageLog {
agent: AgentType;
task: TaskType;
model: ModelTier;
input_tokens: number;
output_tokens: number;
cost_usd: number;
duration_ms: number;
timestamp: string;
}
export type ModelStrategyPreset = 'cost-optimized' | 'balanced' | 'quality-focused';
export interface ModelStrategyConfig {
preset: ModelStrategyPreset;
overrides?: Partial<Record<AgentType, ModelTier>>;
max_cost_per_cycle?: number;
fallback_on_unavailable?: ModelTier;
retry_with_upgrade?: boolean;
}
/**
* Model pricing per million tokens (as of 2026-01-15)
*/
const MODEL_PRICING = {
[ModelTier.HAIKU]: { input: 0.40, output: 2.00 },
[ModelTier.SONNET]: { input: 3.00, output: 15.00 },
[ModelTier.OPUS]: { input: 15.00, output: 75.00 }
} as const;
/**
* Default agent-to-model mapping (balanced strategy)
*/
const DEFAULT_AGENT_MODELS: Record<AgentType, ModelTier> = {
orchestrator: ModelTier.SONNET, // Needs coordination logic
planner: ModelTier.SONNET, // Needs reasoning about test coverage
security: ModelTier.SONNET, // Needs code understanding for remediation
playwright: ModelTier.SONNET, // Needs test authoring creativity
evaluator: ModelTier.OPUS, // CRITICAL: needs highest reasoning for validation
git: ModelTier.HAIKU, // Deterministic operations only
reporting: ModelTier.SONNET // Needs narrative generation (can override per task)
};
export class ModelRouter {
private strategy: ModelStrategyConfig;
private usageLog: ModelUsageLog[] = [];
constructor(strategy: ModelStrategyConfig = { preset: 'balanced' }) {
this.strategy = strategy;
}
/**
* Select optimal model based on agent type and task complexity
*/
selectModel(
agent: AgentType,
_task: TaskType = 'default',
complexity?: TaskComplexity
): ModelConfig {
// RULE 1: Check for user overrides first
if (this.strategy.overrides?.[agent]) {
return this.createModelConfig(this.strategy.overrides[agent]);
}
// RULE 2: Evaluator ALWAYS uses Opus (non-negotiable for quality)
if (agent === 'evaluator') {
return this.createModelConfig(ModelTier.OPUS, 0.3);
}
// RULE 3: Git ALWAYS uses Haiku (deterministic operations)
if (agent === 'git') {
return this.createModelConfig(ModelTier.HAIKU, 0);
}
// RULE 4: Task-level complexity routing
if (complexity) {
// Deterministic + no code generation = Haiku
if (complexity.deterministic && !complexity.code_generation) {
return this.createModelConfig(ModelTier.HAIKU, 0);
}
// High validation stakes = Opus
if (complexity.validation_stakes === 'high') {
return this.createModelConfig(ModelTier.OPUS, 0.3);
}
// Deep reasoning = Sonnet minimum
if (complexity.reasoning_depth === 'deep') {
return this.createModelConfig(ModelTier.SONNET, 0.5);
}
}
// RULE 5: Apply preset strategy
const model = this.getModelForPreset(agent);
return this.createModelConfig(model);
}
/**
* Get model based on strategy preset
*/
private getModelForPreset(agent: AgentType): ModelTier {
switch (this.strategy.preset) {
case 'cost-optimized':
// Maximize Haiku usage, no Opus
if (agent === 'evaluator') return ModelTier.SONNET; // Downgrade evaluator
if (agent === 'git' || agent === 'reporting') return ModelTier.HAIKU;
return ModelTier.SONNET;
case 'quality-focused':
// Use Opus for critical agents
if (agent === 'evaluator' || agent === 'security') return ModelTier.OPUS;
if (agent === 'git') return ModelTier.HAIKU; // Still use Haiku for deterministic
return ModelTier.SONNET;
case 'balanced':
default:
return DEFAULT_AGENT_MODELS[agent];
}
}
/**
* Create model config with appropriate temperature
*/
private createModelConfig(model: ModelTier, temperature?: number): ModelConfig {
if (temperature !== undefined) {
return { model, temperature };
}
// Default temperatures per model
switch (model) {
case ModelTier.HAIKU:
return { model, temperature: 0 }; // Deterministic
case ModelTier.OPUS:
return { model, temperature: 0.3 }; // Focused reasoning
case ModelTier.SONNET:
default:
return { model, temperature: 0.5 }; // Balanced creativity
}
}
/**
* Calculate cost for API usage
*/
calculateCost(model: ModelTier, inputTokens: number, outputTokens: number): number {
const pricing = MODEL_PRICING[model];
return (inputTokens * pricing.input + outputTokens * pricing.output) / 1_000_000;
}
/**
* Log model usage for cost tracking and analytics
*/
logUsage(log: Omit<ModelUsageLog, 'timestamp'>): void {
const entry: ModelUsageLog = {
...log,
timestamp: new Date().toISOString()
};
this.usageLog.push(entry);
}
/**
* Get usage statistics for current session
*/
getUsageStats(): {
total_cost: number;
total_tokens: { input: number; output: number };
by_model: Record<ModelTier, { tasks: number; cost: number; avg_latency_ms: number }>;
by_agent: Record<AgentType, { tasks: number; cost: number }>;
} {
const stats = {
total_cost: 0,
total_tokens: { input: 0, output: 0 },
by_model: {} as Record<ModelTier, { tasks: number; cost: number; avg_latency_ms: number }>,
by_agent: {} as Record<AgentType, { tasks: number; cost: number }>
};
// Initialize model stats
Object.values(ModelTier).forEach(model => {
stats.by_model[model] = { tasks: 0, cost: 0, avg_latency_ms: 0 };
});
// Initialize agent stats
(['orchestrator', 'planner', 'security', 'playwright', 'evaluator', 'git', 'reporting'] as AgentType[]).forEach(agent => {
stats.by_agent[agent] = { tasks: 0, cost: 0 };
});
// Aggregate usage
this.usageLog.forEach(log => {
stats.total_cost += log.cost_usd;
stats.total_tokens.input += log.input_tokens;
stats.total_tokens.output += log.output_tokens;
// By model
stats.by_model[log.model].tasks += 1;
stats.by_model[log.model].cost += log.cost_usd;
stats.by_model[log.model].avg_latency_ms += log.duration_ms;
// By agent
stats.by_agent[log.agent].tasks += 1;
stats.by_agent[log.agent].cost += log.cost_usd;
});
// Calculate average latencies
Object.keys(stats.by_model).forEach(model => {
const modelKey = model as ModelTier;
if (stats.by_model[modelKey].tasks > 0) {
stats.by_model[modelKey].avg_latency_ms /= stats.by_model[modelKey].tasks;
}
});
return stats;
}
/**
* Check if cycle cost exceeds budget
*/
checkBudget(cycleId: number): { exceeded: boolean; current: number; limit: number } {
const cycleCost = this.usageLog
.filter(log => log.timestamp >= this.getCycleStartTime(cycleId))
.reduce((sum, log) => sum + log.cost_usd, 0);
const limit = this.strategy.max_cost_per_cycle ?? Infinity;
return {
exceeded: cycleCost > limit,
current: cycleCost,
limit
};
}
/**
* Get cycle start time (placeholder - would be managed by CycleManager)
*/
private getCycleStartTime(_cycleId: number): string {
// TODO: Integrate with CycleManager to get actual cycle start time
return new Date().toISOString();
}
/**
* Export usage log for persistence
*/
exportUsageLog(): ModelUsageLog[] {
return [...this.usageLog];
}
/**
* Clear usage log (e.g., at start of new cycle)
*/
clearUsageLog(): void {
this.usageLog = [];
}
}