2025-09-11 13:38:50 -04:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import type { Config } from '../config/config.js';
|
|
|
|
|
import type {
|
|
|
|
|
RoutingContext,
|
|
|
|
|
RoutingDecision,
|
|
|
|
|
TerminalStrategy,
|
|
|
|
|
} from './routingStrategy.js';
|
|
|
|
|
import { DefaultStrategy } from './strategies/defaultStrategy.js';
|
2025-09-15 19:51:25 -04:00
|
|
|
import { ClassifierStrategy } from './strategies/classifierStrategy.js';
|
2025-09-11 13:38:50 -04:00
|
|
|
import { CompositeStrategy } from './strategies/compositeStrategy.js';
|
|
|
|
|
import { FallbackStrategy } from './strategies/fallbackStrategy.js';
|
|
|
|
|
import { OverrideStrategy } from './strategies/overrideStrategy.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(
|
2025-09-15 19:51:25 -04:00
|
|
|
[
|
|
|
|
|
new FallbackStrategy(),
|
|
|
|
|
new OverrideStrategy(),
|
|
|
|
|
new ClassifierStrategy(),
|
|
|
|
|
new DefaultStrategy(),
|
|
|
|
|
],
|
2025-09-11 13:38:50 -04:00
|
|
|
'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 decision = await this.strategy.route(
|
|
|
|
|
context,
|
|
|
|
|
this.config,
|
|
|
|
|
this.config.getBaseLlmClient(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return decision;
|
|
|
|
|
}
|
|
|
|
|
}
|