File size: 2,577 Bytes
7a1ad33 | 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 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { Content, PartListUnion } from '@google/genai';
import type { BaseLlmClient } from '../core/baseLlmClient.js';
import type { Config } from '../config/config.js';
import type { LocalLiteRtLmClient } from '../core/localLiteRtLmClient.js';
/**
* The output of a routing decision. It specifies which model to use and why.
*/
export interface RoutingDecision {
/** The model identifier string to use for the next API call (e.g., 'gemini-2.5-pro'). */
model: string;
/**
* Metadata about the routing decision for logging purposes.
*/
metadata: {
source: string;
latencyMs: number;
reasoning: string;
error?: string;
};
}
/**
* The context provided to the router for making a decision.
*/
export interface RoutingContext {
/** The full history of the conversation. */
history: readonly Content[];
/** The immediate request parts to be processed. */
request: PartListUnion;
/** An abort signal to cancel an LLM call during routing. */
signal: AbortSignal;
/** The model string requested for this turn, if any. */
requestedModel?: string;
}
/**
* The core interface that all routing strategies must implement.
* Strategies implementing this interface may decline a request by returning null.
*/
export interface RoutingStrategy {
/** The name of the strategy (e.g., 'fallback', 'override', 'composite'). */
readonly name: string;
/**
* Determines which model to use for a given request context.
* @param context The full context of the request.
* @param config The current configuration.
* @param client A reference to the GeminiClient, allowing the strategy to make its own API calls if needed.
* @returns A promise that resolves to a RoutingDecision, or null if the strategy is not applicable.
*/
route(
context: RoutingContext,
config: Config,
baseLlmClient: BaseLlmClient,
localLiteRtLmClient: LocalLiteRtLmClient,
): Promise<RoutingDecision | null>;
}
/**
* A strategy that is guaranteed to return a decision. It must not return null.
* This is used to ensure that a composite chain always terminates.
*/
export interface TerminalStrategy extends RoutingStrategy {
/**
* Determines which model to use for a given request context.
* @returns A promise that resolves to a RoutingDecision.
*/
route(
context: RoutingContext,
config: Config,
baseLlmClient: BaseLlmClient,
localLiteRtLmClient: LocalLiteRtLmClient,
): Promise<RoutingDecision>;
}
|