fix(routing): preserve combo precedence and skip hidden models in alias resolver (#11107)

Validated on the combined batch board over tip 8a42aeeb: static gates clean (changelog, file-size 159 frozen, complexity 2621<=2774, cognitive 1181<=1223, dead-code 408<=416), typecheck:core clean, 107 focused tests green.

Combo precedence preserved when a requested name matches an existing combo or combo/* prefix, and hidden/disabled models are skipped during alias resolution (wildcard + mapped). model-alias-seed-fallback green. Related to #10124. Thank you @SCys!
This commit is contained in:
Alex Chan
2026-08-23 18:01:07 +08:00
committed by GitHub
parent 98289a8c99
commit 0fe1bb2390
2 changed files with 104 additions and 3 deletions

View File

@@ -10,11 +10,21 @@
*/
import { getModelAliases } from "@/lib/db/models/aliases";
import { DEFAULT_MODEL_ALIAS_SEED } from "@/lib/modelAliasSeed";
import { getComboByName } from "@/lib/db/combos";
import { getModelIsHidden } from "@/lib/db/models";
import { resolveProviderId } from "@/shared/constants/providers";
let cachedAliases: Record<string, unknown> | null = null;
let lastFetch = 0;
const CACHE_TTL_MS = 60_000; // 1 minute
function isTargetModelHidden(provider: string, modelId: string): boolean {
if (getModelIsHidden(provider, modelId)) return true;
const canonicalProvider = resolveProviderId(provider);
if (canonicalProvider !== provider && getModelIsHidden(canonicalProvider, modelId)) return true;
return false;
}
async function loadAliases(): Promise<Record<string, unknown>> {
const now = Date.now();
if (cachedAliases && now - lastFetch < CACHE_TTL_MS) {
@@ -40,21 +50,52 @@ export async function resolveModelAliasWithSeedFallback(
): Promise<string | null | undefined> {
if (!model) return model;
// Combo routing takes precedence over individual model aliases (#10124 / #9020)
if (model.startsWith("combo/")) return model;
const existingCombo = await getComboByName(model).catch(() => null);
if (existingCombo) 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 (typeof target === "string") {
const slashIndex = target.indexOf("/");
if (slashIndex > 0) {
const targetProvider = target.slice(0, slashIndex);
const targetModel = target.slice(slashIndex + 1);
if (isTargetModelHidden(targetProvider, targetModel)) {
return model;
}
}
return target;
}
if (Array.isArray(target) && target.length > 0) {
const first = target[0];
return typeof first === "string" ? first : model;
if (typeof first === "string") {
const slashIndex = first.indexOf("/");
if (slashIndex > 0) {
const targetProvider = first.slice(0, slashIndex);
const targetModel = first.slice(slashIndex + 1);
if (isTargetModelHidden(targetProvider, targetModel)) {
return model;
}
}
return first;
}
return 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}`;
if (t.provider && t.model) {
if (isTargetModelHidden(t.provider, t.model)) {
return model;
}
return `${t.provider}/${t.model}`;
}
}
return model;