/** * Model resolution: exact match ("provider/modelId") with fuzzy fallback for tool inputs. */ import { exactTierModel } from "./model-tiers.js"; export interface ModelEntry { id: string; name: string; provider: string; } export interface ModelRegistry { find(provider: string, modelId: string): any; getAll(): any[]; getAvailable?(): any[]; } /** Logical model aliases supplied only by trusted global machine configuration. */ let modelAliases = new Map(); /** * Replace the configured logical aliases. Alias keys are case-insensitive; * candidates are canonical provider/model IDs and are resolved against the * available registry at dispatch time. */ export function setModelAliases(aliases: Record | undefined): void { modelAliases = new Map( Object.entries(aliases ?? {}).map(([alias, candidates]) => [alias.toLowerCase(), [...candidates]]), ); } /** * Per-agent model overrides from trusted global configuration: agent `type` * (or `"*"`) -> model input. Resolved through `resolveModel`, so a value may be * an alias, an exact `provider/id`, or a fuzzy name. */ let agentModels = new Map(); /** Replace the configured per-agent model map. Keys are matched case-insensitively. */ export function setAgentModels(models: Record | undefined): void { agentModels = new Map(Object.entries(models ?? {}).map(([type, model]) => [type.toLowerCase(), model])); } /** A snapshot for the shared selection policy; callers cannot mutate configuration. */ export function getAgentModels(): ReadonlyMap { return new Map(agentModels); } /** * Narrows fuzzy candidates to the models the user enabled (pi's `enabledModels`), * as lowercase `provider/id` keys. Injected rather than imported so this module * stays free of settings I/O; `undefined` means no scope is configured. An * unresolved dispatch passes its config root only for its diagnostic. */ export type ModelScopeProvider = ( registry: ModelRegistry, context?: { cwd: string; homeDir?: string }, ) => ReadonlySet | undefined; let modelScopeProvider: ModelScopeProvider | undefined; /** Install (or clear) the enabled-model scope used to rank fuzzy matches. */ export function setModelScopeProvider(provider: ModelScopeProvider | undefined): void { modelScopeProvider = provider; } /** A copy for diagnostics and tests; callers cannot mutate resolver state. */ export function getModelAliases(): ReadonlyMap { return new Map([...modelAliases].map(([alias, candidates]) => [alias, [...candidates]])); } /** * Both display forms of a model. The short one goes on tight rows (the widget, * the Agent tool result), the canonical one where there is room to disambiguate * two providers serving a similarly-named model (the conversation viewer). * * One function, because `index.ts` labels the model it resolved before the run * and `agent-manager.ts` relabels it from the live session afterwards — the two * must agree or the label would visibly change the moment the session starts. */ export function describeModel( model: { provider: string; id: string; name?: string }, ): { modelName: string; modelId: string } { return { modelName: (model.name ?? model.id).replace(/^Claude\s+/i, "").toLowerCase(), modelId: `${model.provider}/${model.id}`, }; } /** * A CONFIG model (`agentModels`, an agent's `model:`, a stored job's model) that * did not resolve, so the spawn ran on the parent model instead. Recorded once on * the `AgentRecord` by the shared spawn path; every surface reads it from there. */ export interface ModelFallback { /** The configured input that failed to resolve. */ input: string; /** Further configured inputs that also failed (e.g. a stored job model AND an `agentModels` override). */ also?: string[]; /** `provider/id` of the model actually used; absent when the session default (a parent with no model) ran. */ used?: string; /** Whose model `used` is: the parent's (default) or the agent's own configured model (`agentModels` / definition `model:`), which took over from an unresolved stored model. */ via?: "parent" | "agent"; } /** The fallback outcome for `input` when the spawn ran on `parent`. */ export function modelFallbackTo(input: string, parent: { provider: string; id: string } | undefined): ModelFallback { return parent ? { input, used: `${parent.provider}/${parent.id}` } : { input }; } /** * The disclosure line for a fallback: same `Note:` prefix as the unknown-agent-type * note. Empty string when there was none, so callers can concatenate it blindly. */ export function modelFallbackNote(fallback: ModelFallback | undefined): string { if (!fallback) return ""; const whose = fallback.via === "agent" ? "the agent's own model" : "parent model"; const used = fallback.used ? `${whose} ${fallback.used}` : "the session default model"; const inputs = [fallback.input, ...(fallback.also ?? [])]; const named = inputs.length === 1 ? `model "${inputs[0]}" did` : `models ${inputs.map(i => `"${i}"`).join(", ")} did`; return `Note: configured ${named} not resolve; running on ${used}.`; } /** API id pi gives virtual catalog entries such as `router/default`; they never reach a provider. */ const VIRTUAL_MODEL_API = "pi-virtual"; /** Whether `model` is a virtual (router) selection rather than a physical model. */ export function isVirtualModel(model: { api?: string } | undefined): boolean { return model?.api === VIRTUAL_MODEL_API; } /** The pre-response label of a virtual selection: no physical model has answered yet. */ export function describeVirtualModel(model: { provider: string; id: string; name?: string }): { modelName: string; modelId: string; } { const described = describeModel(model); return { ...described, modelName: `${described.modelName} (routing)` }; } /** * The physical model behind an assistant message, or `undefined` when the * message names none (an error from failed routing carries the virtual model). * * With a virtual selection (`agentModels["*"] = "router/default"`) the session * model is the router; only responses record where a request actually went, so * rows must be relabelled from them. `registry` supplies the display name, and * is optional: without it the id stands in. */ export function describePhysicalModel( message: { api?: string; provider?: string; model?: string } | undefined, registry?: { find?(provider: string, modelId: string): { name?: string } | undefined }, ): { modelName: string; modelId: string } | undefined { if (!message?.provider || !message.model || isVirtualModel(message)) return undefined; const found = registry?.find?.(message.provider, message.model); return describeModel({ provider: message.provider, id: message.model, name: found?.name }); } /** Parsed `[major, minor]` of a model id, or undefined when the family has no known scheme. */ export type ModelVersion = readonly [major: number, minor: number]; /** * Pull a version out of a model id (provider prefix ignored): * - `claude--[-]`, e.g. `claude-opus-4-6`, `claude-opus-5` * - `claude-[-]-`, e.g. `claude-3-5-sonnet-20241022` * - `gpt-[.]-`, e.g. `gpt-6.1-sol` * A minor is one or two digits, so a trailing `-20250514` date stamp is never * read as one. Dots and dashes are interchangeable between the parts. */ export function parseModelVersion(id: string): ModelVersion | undefined { const bare = id.slice(id.lastIndexOf("/") + 1).toLowerCase(); const claude = /^claude-[a-z]+-(\d{1,2})(?:[-.](\d{1,2})(?!\d))?(?:-|$)/.exec(bare) ?? /^claude-(\d{1,2})(?:[-.](\d{1,2})(?!\d))?-[a-z]/.exec(bare); const match = claude ?? /^gpt-(\d+)(?:[.-](\d{1,2})(?!\d))?(?:-|$)/.exec(bare); if (!match) return undefined; return [Number(match[1]), Number(match[2] ?? 0)]; } /** Descending version order; an unversioned model sorts after every versioned one. */ function compareVersionsDesc(a: ModelVersion | undefined, b: ModelVersion | undefined): number { if (a && b) return b[0] - a[0] || b[1] - a[1]; return a ? -1 : b ? 1 : 0; } /** * Resolve a model string to a Model instance. * Tries exact match first ("provider/modelId"), then fuzzy match against all available models. * Returns the Model on success, or an error message string on failure. */ export function resolveModel( input: string, registry: ModelRegistry, context?: { cwd: string; homeDir?: string }, ): any | string { const aliasCandidates = modelAliases.get(input.trim().toLowerCase()); if (aliasCandidates) { for (const candidate of aliasCandidates) { const resolved = exactTierModel(candidate, registry); if (resolved) return resolved; } return `Model alias "${input}" has no available candidate.\n\nCandidates:\n${aliasCandidates.map(candidate => ` ${candidate}`).join("\n")}`; } return resolveCanonicalModel(input, registry, context); } /** * Pick among fuzzy candidates. Restricted to the enabled model scope when one is * configured and any candidate is in it, then newest parsed version first, then * match score. A bare family name like "opus" therefore means the newest opus * the user has enabled, not whichever id happens to be shortest. */ function scopedCandidates( candidates: T[], registry: ModelRegistry, context?: { cwd: string; homeDir?: string }, ): T[] { const scope = modelScopeProvider?.(registry, context); const scoped = scope ? candidates.filter(candidate => scope.has(`${candidate.entry.provider}/${candidate.entry.id}`.toLowerCase())) : []; return scoped.length > 0 ? scoped : candidates; } /** Stable tie-breaker independent of registry insertion order and locale. */ function compareModelIdentity(a: ModelEntry, b: ModelEntry): number { const left = `${a.provider}/${a.id}`; const right = `${b.provider}/${b.id}`; return Number(left > right) - Number(left < right); } function rankFuzzy( matches: { entry: ModelEntry; score: number }[], registry: ModelRegistry, context?: { cwd: string; homeDir?: string }, ): ModelEntry | undefined { const ranked = scopedCandidates(matches, registry, context) .map(c => ({ ...c, version: parseModelVersion(c.entry.id) })) .sort((a, b) => compareVersionsDesc(a.version, b.version) || b.score - a.score || compareModelIdentity(a.entry, b.entry)); return ranked[0]?.entry; } /** Numeric runs stay ordered and contiguous: `5-5` cannot reuse the `5` in `4-5`. */ function versionRuns(value: string): string[] { return [...value.replace(/-\d{8}$/, "").matchAll(/(?:^|[\s-])(\d+(?:[\s-]+\d+)*)(?=$|[\s-])/g)].map(match => match[1]!.replace(/\s+/g, "-")); } /** Resolve an exact or fuzzy provider/model request without consulting aliases. */ function resolveCanonicalModel( input: string, registry: ModelRegistry, context?: { cwd: string; homeDir?: string }, ): any | string { // Available models (those with auth configured) const all = (registry.getAvailable?.() ?? registry.getAll()) as ModelEntry[]; const availableSet = new Set(all.map(m => `${m.provider}/${m.id}`.toLowerCase())); // 1. Exact match: "provider/modelId" — only if available (has auth) const slashIdx = input.indexOf("/"); if (slashIdx !== -1) { const provider = input.slice(0, slashIdx); const modelId = input.slice(slashIdx + 1); if (availableSet.has(input.toLowerCase())) { const found = registry.find(provider, modelId); if (found) return found; } } // 2. Fuzzy match against available models. Normalize separators so cosmetic // punctuation differences still match — e.g. "claude-haiku-4.5" and // "claude-haiku-4-5" (dot vs dash in the version) resolve to the same model. const normalize = (s: string) => s.toLowerCase().replace(/\./g, "-"); const query = normalize(input); const requestedVersions = versionRuns(query.slice(slashIdx + 1)); // Score each model: prefer exact id match > id contains > name contains > provider+id contains const matches: { entry: ModelEntry; score: number }[] = []; for (const m of all) { const id = normalize(m.id); const name = normalize(m.name); const full = normalize(`${m.provider}/${m.id}`); const idVersions = versionRuns(id); const versions = idVersions.length > 0 ? idVersions : versionRuns(name); // Filter before scope/ranking, including substring and name matches. A // major-only input can select a newer minor, but never another major or // a minor token masquerading as the requested major. if (!requestedVersions.every(requested => versions.some(version => version === requested || version.startsWith(`${requested}-`)))) continue; let score = 0; if (id === query || full === query) { score = 100; // exact } else if (id.includes(query) || full.includes(query)) { score = 60 + (query.length / id.length) * 30; // substring, prefer tighter matches } else if (name.includes(query)) { score = 40 + (query.length / name.length) * 20; } else if ( // A trailing date-stamp token (e.g. "20251001") is optional, so a // date-pinned config like "claude-haiku-4-5-20251001" still matches an // undated registry id like "claude-haiku-4-5". query .split(/[\s\-/]+/) .every(part => /^\d{8}$/.test(part) || id.includes(part) || name.includes(part) || m.provider.toLowerCase().includes(part)) ) { score = 20; // all parts present somewhere } if (score >= 20) matches.push({ entry: m, score }); } // An exact id (or provider/id) is the caller naming a model, not describing // one: it wins outright, whatever the scope or any newer sibling. const exact = matches.filter(c => c.score === 100); const exactBest = exact.sort((a, b) => compareModelIdentity(a.entry, b.entry))[0]; const bestMatch = exactBest ? exactBest.entry : rankFuzzy(matches, registry, context); if (bestMatch) { const found = registry.find(bestMatch.provider, bestMatch.id); if (found) return found; } // 3. Provider fallback: a "provider/modelId" query that didn't match under the // named provider (exact or fuzzy above) retries against all providers. The // named provider is preferred when present; this only kicks in when it isn't, // so the same model from another provider beats falling back to "inherit". if (slashIdx !== -1) { const bare = resolveCanonicalModel(input.slice(slashIdx + 1), registry, context); if (typeof bare !== "string") return bare; } // 4. No match — list available models const modelList = scopedCandidates(all.map(entry => ({ entry })), registry, context) .map(({ entry }) => ` ${entry.provider}/${entry.id}`) .sort() .join("\n"); return `Model not found: "${input}".\n\nAvailable models:\n${modelList}`; }