Files
OmniRoute/src/lib/modelAliasResolver.ts
Benson K B 14afdcb923 fix(routing): fallback to default model alias seeds when unmapped in database (#10124)
* fix(routing): fallback to default model alias seeds when unmapped in database

* fix(routing): rename seed-fallback resolver; hermetic 401 regression test

Maintainer review (PR #10124):
1. Rename resolveModelAlias -> resolveModelAliasWithSeedFallback (and
   resolveModelAliasOnBody -> resolveModelAliasWithSeedFallbackOnBody) to
   avoid the export collision with the sync resolveModelAlias in
   open-sse/services/modelDeprecation.ts and
   src/shared/constants/modelSpecs.ts.
2. Regression test now reproduces the 401: alias unmapped in the (empty,
   DATA_DIR-isolated) modelAliases namespace but present in the static seed
   resolves to the seed target instead of passing through unmapped.
3. Test isolates DATA_DIR (temp dir + resetDbInstance) instead of reading
   the operator's live DB.

* fix(models): add outputTokenLimit to CustomModelEntry

Fixes the open-sse typecheck gate regression: catalog.ts reads
model.outputTokenLimit (for max_output_tokens in custom model metadata)
but CustomModelEntry only declared inputTokenLimit — TS2551. The
field exists in the runtime model data and is already consumed; the
interface just never declared it.
2026-08-17 08:21:53 -03:00

81 lines
2.6 KiB
TypeScript

/**
* Model Alias Resolver — maps client-facing model names to OmniRoute provider IDs.
*
* When a client sends `model: "deepseek-chat"`, this resolver looks up the alias
* in the database and rewrites it to the target model ID (e.g. `"ds/deepseek-v4-flash"`)
* before the request reaches the chat handler.
*
* Aliases are stored in the `modelAliases` key-value namespace and seeded by
* `src/lib/modelAliasSeed.ts`.
*/
import { getModelAliases } from "@/lib/db/models/aliases";
import { DEFAULT_MODEL_ALIAS_SEED } from "@/lib/modelAliasSeed";
let cachedAliases: Record<string, unknown> | null = null;
let lastFetch = 0;
const CACHE_TTL_MS = 60_000; // 1 minute
async function loadAliases(): Promise<Record<string, unknown>> {
const now = Date.now();
if (cachedAliases && now - lastFetch < CACHE_TTL_MS) {
return cachedAliases;
}
cachedAliases = await getModelAliases();
lastFetch = now;
return cachedAliases;
}
/**
* Resolve a model alias to its target provider model ID, falling back to the
* static DEFAULT_MODEL_ALIAS_SEED when the alias is not in the database.
* If the alias maps to an array, returns the first element.
* If no alias is found, returns the original model name unchanged.
*
* Named distinctly from `resolveModelAlias` (modelDeprecation.ts /
* modelSpecs.ts, sync string→string) to avoid export collisions when both
* modules are imported together.
*/
export async function resolveModelAliasWithSeedFallback(
model: string | null | undefined
): Promise<string | null | undefined> {
if (!model) return model;
const aliases = await loadAliases();
const target = aliases[model] ?? (DEFAULT_MODEL_ALIAS_SEED as Record<string, unknown>)[model];
if (target === undefined) return model;
if (typeof target === "string") return target;
if (Array.isArray(target) && target.length > 0) {
const first = target[0];
return typeof first === "string" ? first : model;
}
if (typeof target === "object" && target !== null) {
const t = target as { provider?: string; model?: string };
if (t.provider && t.model) return `${t.provider}/${t.model}`;
}
return model;
}
/**
* Resolve model alias on a parsed request body in-place.
* Mutates `body.model` if an alias is found.
*/
export async function resolveModelAliasWithSeedFallbackOnBody(
body: Record<string, unknown> | null | undefined
): Promise<void> {
if (!body || typeof body !== "object") return;
body.model = await resolveModelAliasWithSeedFallback(body.model as string | null | undefined);
}
/**
* Invalidate the alias cache (e.g. after a new alias is added).
*/
export function invalidateAliasCache(): void {
cachedAliases = null;
lastFetch = 0;
}