定义同步函数 smartChoose(request, context),返回模型选择结果。网关会将原始请求发送给所选模型,函数不会改写请求体。
{ model: string, fallbackModels?: string[] }
- model 必须是 context.candidates 中实际模型的代码,网关在执行前会再次校验。
- fallbackModels 为可选字段:省略时使用已配置且可用的回退顺序,返回 [] 则不执行回退。显式列表最多包含两个不同的、已配置且可用的回退模型,并且不能包含已选择的模型。
- 回退需要在第 2 步明确启用,且仅在提供方安全拒绝后执行。已经产生输出或执行情况不明确时,不会重试请求。
- 输入对象已被深度冻结。排序前先复制数组,例如 [...context.candidates].sort(...)。
保持函数简短、同步
不使用 async/await、网络调用、文件系统、宿主访问或持久状态。Date 和 Math.random 不可用,请使用 context.requestTimeMs 和 context.routingSeed。执行有时间限制,应避免无限循环或昂贵计算。
完整类型参考 · 与编辑器自动补全共用
interface SmartRequest {
readonly apiVersion: 1;
readonly apiDialect: string;
readonly stream: boolean;
readonly body: Readonly<Record<string, unknown>>;
readonly features: {
inputTokenEstimate: number;
outputTokenLimit: number;
hasTools: boolean;
hasImageInput: boolean;
structuredOutput: boolean;
taskTag: string | null;
};
}
interface SmartCandidate {
readonly model: string;
readonly displayName: string;
readonly tags: readonly string[];
readonly priority: number;
readonly weight: number;
readonly capabilities: {
contextWindowTokens: number;
maxOutputTokens: number;
streaming: boolean;
tools: boolean;
structuredOutput: boolean;
};
readonly cost: {
currency: string;
microUnits: string;
snapshotVersion: string;
} | null;
readonly latency: {
p95Ms: number;
samples: number;
observedAtMs: number;
} | null;
}
interface SmartContext {
readonly candidates: readonly SmartCandidate[];
readonly fallbackCandidates: readonly SmartCandidate[];
readonly defaultModel: string | null;
readonly params: Readonly<Record<string, any>>;
readonly billingCurrency: string;
readonly routingSeed: string;
readonly requestTimeMs: number;
readonly snapshotVersion: number;
}
interface RoutingDecision {
model: string;
fallbackModels?: string[];
}
declare function smartChoose(
request: SmartRequest,
context: SmartContext
): RoutingDecision;