/** * Per-action model routing — strategy registry. * * Resolves an action's model class independently from the planner model, so an * action can run on a small or local model while planning uses a larger model. * * Design constraints (AGENTS.md): * - No runtime type-branching mess: each `ActionModelClass` is a strategy * entry in {@link ACTION_MODEL_STRATEGIES}. New classes are additions to * that table, not new `if/else` branches. * - Strong typing: no `any`, no unsafe casts at the call sites. * - When `action.modelClass` is absent, the resolver * returns `null` and the runtime falls through to its existing behavior. * * Fallback chains (ascending cost / capability): * - `TEXT_SMALL → TEXT_LARGE` * - `LOCAL` is terminal: it resolves only local-provider registrations and * never escalates to a cloud handler. With no local handler registered the * call fails instead of taking the default path. * * Failure semantics: * - If the preferred model handler has a retryable provider failure, escalate * and retry. The last step in the chain is terminal — its error is * re-raised. * - If the call returns a `confidence` field below the strategy's threshold, * escalate one step up and retry. This is opt-in: returns without a * `confidence` field are never re-evaluated. */ import type { ActionModelClass } from "../types/components.js"; import { type ModelHandler, type ModelRegistrationMetadata } from "../types/model.js"; /** * Minimal capability view of a model registration, used by routing predicates. * * Routing decisions are driven by provider-declared capability metadata * ({@link ModelRegistrationMetadata}) first, with the provider *name* available * only as an explicitly-tested fallback for registrations that have not yet * adopted the capability flags. Accepting this view (rather than a bare name * string) is what lets {@link isLocalHandler} consult the declared contract * instead of duck-typing the provider name. */ export interface ModelCapabilityView { readonly provider: string; readonly metadata?: ModelRegistrationMetadata; } /** * One step in a fallback chain. * * `modelType` is the model registration key to look up (one of `ModelType.*`). * * `providerFilter` is an optional predicate over the registered provider's * capability view. For the `LOCAL` strategy this restricts resolution to * local-provider registrations (Ollama, LM Studio, MLX, llama.cpp, etc.) so * that a `LOCAL` request never silently calls a cloud provider that also * happens to be registered for `TEXT_SMALL`. */ export interface ActionModelRoutingStep { readonly modelType: string; /** * Optional predicate over a candidate registration. Receives the full * capability view ({@link ModelCapabilityView}) so the filter can consult * provider-declared metadata (e.g. `metadata.local`) rather than substring- * matching the provider name. For the `LOCAL` strategy this is * {@link isLocalHandler}, which prefers the declared `local` capability and * falls back to the {@link isLocalProvider} name heuristic only for providers * that have not adopted the flag. */ readonly providerFilter?: (candidate: ModelCapabilityView) => boolean; } /** * Strategy entry for one {@link ActionModelClass}. * * `chain` is ordered cheapest-first / most-preferred-first. The runtime tries * each step in order, escalating to the next on error or low confidence. * * `confidenceThreshold` is the minimum confidence below which the call is * considered low-confidence and the runtime escalates. `undefined` disables * confidence-based escalation for this class. */ export interface ActionModelRoutingStrategy { readonly chain: readonly ActionModelRoutingStep[]; readonly confidenceThreshold?: number; /** When true, a chain that resolves no handler fails the call instead of * falling back to the default (possibly cloud) resolution. */ readonly requireChainMatch?: boolean; } /** Name-based local-provider fallback for registrations without declared locality metadata. Prefer isLocalHandler for capability decisions. */ export declare function isLocalProvider(provider: string): boolean; /** * Predicate: is this registration a local-inference target? * * Capability-first classification. Precedence: * 1. If the provider declared `metadata.local` explicitly (`true`/`false`), * that verdict wins — the owner metadata is authoritative. * 2. Otherwise fall back to the {@link isLocalProvider} name heuristic for * providers that have not adopted the capability flag yet. * * This is the predicate the `LOCAL` routing strategy uses so a `LOCAL` request * never silently resolves to a cloud provider, and so providers can opt out of * the name heuristic (or opt in without matching a name family) by declaring * the flag. */ export declare function isLocalHandler(candidate: ModelCapabilityView): boolean; /** * Should the runtime re-route this `useModel` call based on the action's * `modelClass`? Reroute only when: * - The action has a `modelClass`. * - The requested model type is in {@link ROUTABLE_TEXT_MODEL_TYPES}. * * Returns the strategy if rerouting applies; otherwise `undefined`. */ export declare function maybeReroute(modelClass: ActionModelClass | undefined, requestedModelType: string): ActionModelRoutingStrategy | undefined; /** * Result of resolving a single step against a runtime's model registry. * * `handler` is the registered {@link ModelHandler.handler}. `modelType` is the * key the handler was found under (after the providerFilter narrowing). * `provider` is the registered provider name — useful for telemetry and for * surfacing in the trajectory recorder. */ export interface ResolvedActionModel { readonly handler: ModelHandler["handler"]; readonly modelType: string; readonly provider: string; } /** * Resolve the full chain into an ordered list of usable handlers. Steps with * no matching registration are skipped silently — the chain is best-effort and * the caller treats an empty result as "no local handler available, fall back * to default resolution". */ export declare function resolveChain(strategy: ActionModelRoutingStrategy, lookup: (modelType: string) => readonly ModelHandler[] | undefined): ResolvedActionModel[]; /** * Execute the resolved chain with ascending-fallback semantics. * * Steps are attempted in order. On error or low-confidence result, the next * step is tried. The first acceptable result is returned. If every step fails, * the last error is re-raised (preserves the stack from the terminal handler * rather than the first one). * * `invoke` is the runtime's per-call invoker. It receives the resolved handler * along with the model-type key it was found under (so the runtime can log / * record the actual model that ran) and returns the model result. This * indirection lets the runtime keep its existing model-settings merge, logging, * and trajectory hooks identical between routed and non-routed calls. */ export declare function executeChainWithFallback(chain: readonly ResolvedActionModel[], threshold: number | undefined, invoke: (resolved: ResolvedActionModel) => Promise): Promise;