mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-17 04:32:31 +03:00
* fix(ci): clear base-reds on release/v3.8.50 (round 3) - CHANGELOG.md: restore the top [Unreleased] section dropped by the #10189 reconcile (docs-sync gate: first section must be Unreleased) - env-doc-sync: document CONDUCTOR_ORCHESTRATOR_TOKEN + CONDUCTOR_SPOKESPERSON_URL in .env.example/ENVIRONMENT.md; allowlist the CI-only GITHUB_STEP_SUMMARY and TS7_BASE_REF (ts7 ratchet signals); drop a stray merge artifact line - providers: restore the audited chatanywhere metadata entry that base-reds round 2 dropped together with its duplicate — the provider was half-wired (registry+endpoint without APIKEY metadata), which is what the wave3 test catches; re-pin providers-constants-split at the measured 228 - docs counts: 338 -> 339 (today's +2 void-ai/helixmind, -1 Puter) via gen:provider-reference + README/AGENTS/llm.txt/package.json/diagrams/i18n mirrors - file-size ratchet: annotated rebaseline for the two pre-existing drifts (ModelSelectModal 1138, gateways 1250) following the 2026-08-11 precedent Refs #9985 * fix(ci): base-reds round 3b — stale sibling tests + mode-pack weight contract - check-docs-counts-sync.test.ts: drop the imports/subtests of the four helpers #10196 removed from the gate script (readMcpFactsFromSource, listLocalizedDocs, makeRequiredCountsValidator, checkFreeTierInventory) — the new-API tests that #10196 added stay; the file now loads again under the node runner - quota-connection-recovery.test.ts: convert from vitest APIs to node:test — the file lives in tests/unit/*.test.ts (node-runner glob) and the vitest runtime crashes when imported outside vitest, killing the whole shard entry - modePacks.ts: re-normalize all six mode packs to sum 1.0 — #8940 added sessionAvailability: 0.05 to every pack without rebalancing (1.05 total); ratios preserved exactly (÷1.05), so post-normalizeScoringWeights behavior is unchanged; restores the declared sum-to-1.0 contract the 4235 test pins Refs #9985 * fix(ci): base-reds round 3c — vitest siblings, weights default, secrets FP, mutation tap - DistributeProxiesButton.test.tsx: wrap renders in NextIntlClientProvider — #9245 localized the component (useTranslations) and left the test without the intl context, failing all 14 cases - scoring.ts: re-normalize DEFAULT_WEIGHTS to sum 1.0 (same #8940 class as the mode packs — sessionAvailability added without rebalancing; ratios preserved) - .gitleaks.toml: generalize the kimi sponsor-banner localStorage-key allowlist to -v\d+ — #10200 bumped v1→v2 and the stale regex regressed the secrets ratchet with a false positive - stryker.conf.json: register 6 covering unit tests in tap.testFiles (4 modules) so their mutant kills count — unblocks check:mutation-test-coverage --strict Refs #9985 * fix(ci): base-reds round 3d — inspector factor gap, stale registry/gap tests, i18n key sync - comboScoringInspector: add cacheAffinity/sessionAvailability/connectionDensity to FACTOR_KEYS + the factor-key type — calculateScore() weighs them but the breakdown omitted them, so the explained contributions never summed to the reported score (inspector bug, red on the pure tip) - combo-scoring-inspector.test: make the explicit-weights override sum-neutral (±0.05 shift) so it stays valid for any DEFAULT_WEIGHTS values — the hardcoded override only summed to 1.0 against the pre-#8940 defaults, which is also why explicit weights silently fell back to 'default' on the tip - unorouter-registry.test: align to the canonical .com host (api.unorouter.ai 301-redirects there, verified live) and to wave4's live model discovery (passthrough, no static seed) — the .ai/auto-model expectations were stale - check-migration-numbering.test: 147 left KNOWN_GAPS when 147_api_keys_model_access_mode.sql landed — assert absent (same as 143) - i18n: sync-ui pass — 35,914 missing UI keys stamped as __MISSING__ placeholders across 42 locales (mechanical; greens the pt-BR key-presence integrity test; coverage pct unchanged by design — translation is a separate workstream) Refs #9985 * fix(ci): base-reds round 3e — 2 real defects + 14 stale sibling tests (waves A-E) Real defects fixed: - src/lib/db/apiKeys.ts: #9313's empty-allowlist early return bypassed the group permission check, silently disabling group deny rules (#8817) for every key without a per-key allowlist; fall-through restored, restricted+[] deny-all kept - open-sse/utils/proxyFetch.ts: #10032 re-appended the raw transport error to the propagated message, reintroducing the proxy user:password leak #9837 closed; new redactProxyDetailsInMessage() keeps the reason, redacts URL/credentials - .github/workflows/quality.yml: #10134 added the TS7 ratchet as a separate blocking step AFTER the aggregated gates — the exact #8542 masking mechanism; folded into the non-fail-fast loop (still blocking, still PR-only) ⚠️ CI edit, gate-strengthening — explicit owner sign-off requested on the PR - src/i18n/messages/ko.json: 3 machine-mistranslation regressions caught by the #8244 glossary checker (장애인→비활성화됨, 양말5://→socks5://, 비클로드→Claude가 아닌) Stale sibling tests aligned to deliberately-moved contracts (each cites its mover): request-log-detail-layout + -stream (#9245 intl provider), repro-8542 pin update, quality-rail-gate-membership (#10134 shape), agentSkills-routes 45→46 (#9058), cloudflare-ai-catalog-8717 (#8804 supersedes #8808), executor-xai (#9994), vision-bridge-claude-wire (#9463 minimax→openai), sse-auth forced-pin (#8893), tls-proxy-context (strengthened leak guards), rate-limit-local-error-classification (#9164/#9342), minimax-thinking-signature (#9463), codebuddy-cn (#9723 +1 test), github-copilot-custom-model (#9050), providers-g4f-batch3 (#9584), synced-capability-warmup (#9199, stricter), sidebar-tools-group (#8221), oauth-modal-grok-cli-paste (#9245); agentSkills/catalog.ts comment 45→46; file-size rebaseline for proxyFetch (+19, annotated) Refs #9985 * fix(ci): base-reds round 3f — waves F-J: 9 more real defects + stale sibling sweep Real production defects fixed (all red on the pure tip, each with its origin): - routeGuard.ts: #8949 accidentally DELETED the /api/providers/[id]/login local-only pattern — the route spawns a browser, so the loopback gate for a process-spawning route was gone (Hard Rules #15/#17); restored (314 guard tests green) - agentSkills generator: #9058's category dispatch gave the config category an empty body, wiping skills/config-codex-cli/SKILL.md at the #10131 sync; fixed + SKILL.md regenerated via the official generator - imageRegistry: #9982 broke same-provider bare aliasing (antigravity preview id sent upstream unresolved); new resolveSameProviderBareAlias() keeps the fal cross-provider fix intact - imageRegistry: #9982's prefix strip handed the bare nano-banana ids to fal-ai, violating the pinned 2026-07-31 operator decision (adobe-firefly owns them); fal entries made prefix-only (dispatch already re-prefixes) - mediaGeneration/fal.ts: the missing-credential 401 guard was lost when #10198 deleted the superseded falHandler — tests were hitting the live network - bottleneckPatch/rateLimitManager: #9041's merge clobbered #9604, resurrecting the Bottleneck v2.19.5 heartbeat bug (reservoir never refills); patched the library defect at the root and re-aligned chat-rate-limit-body-lock to the working reservoir contract - processSupervisor.mjs: #9761 regressed the Node spawn to bare "node" (the #9156 launchd bug) and dropped #9209's ipv4first args; both restored - openai-responses/pureHelpers: #9423's Agent null-sentinel was unreachable on the schemaless JSON-string path; gate extended - i18n en.json: #8222's regen reverted the #9976 unclosed-tag fix and #8559's combo-cooldown copy; #9038 shipped 40 t() calls with no messages (runtime MISSING_MESSAGE); all restored/added + official sync-ui stamps, and vi's zero-marker policy re-established via the sanctioned translation backend Stale sibling tests aligned (movers cited inline): chat-helpers (#9447), executor-antigravity (#9351), video-fal-grok (#9982), visionBridge (#9759), web-session-credentials (#8974), production-build-module-integrity (positive anchor added), agentSkills-generator/skillManifestsLint/skills-injection/ agentSkillTools-mcp/listCapabilities-a2a (#9058), memory-settings (#10010), model-catalog-policy-invalidation (#8906), model-alias-seed (#9485), reactive-context-compaction (#8949), combo-provider-wildcard (broken upsert helper), oauth-google-loopback (43-locale resurrected-key removal) Validation: 501/501 across the 47 touched test files; typecheck:core, lint, file-size, docs-sync all green. Refs #9985 * fix(ci): base-reds round 3g — wave K/L: 4 more real defects + stale alignments Real defects: - base/reasoningEffort.ts: the stale duplicate cherry-pick #9612 re-added the codex minimal→low rewrite that #9883 had deliberately removed (OMP minimal passthrough); block removed again - cursorImages.ts: #9840 wired prepareCursorImageForWire (sharp re-encode, fail-closed) into the SHARED resolveCursorImages, breaking zai-web and conol-web image uploads (HTTP 400 'undecodable'); new prepareForWire opt-out, Cursor default path unchanged (8 cursor suites green) - modelCapabilities/snapshot: catalog prepare still issued 323 per-model reads of model_context_overrides + max_input_tokens overrides, violating #9199's bulk-load contract; both now resolve from the snapshot single pass - v1-models-discovery-conformance: re-pinned to the bounded 30s SWR window (#9199/#10198) — the old 'stale-first regardless of age' contract is gone Stale tests aligned (movers cited inline): codex-tools-strict-default (#9828 redundant-oneOf strip), devin-providers (#9245 i18n), db-migrationrunner- constants-split (147→151 renumber #8228), gitlab-duo-oauth-setup (#9245), chatcore-extracted-modules (#9161 outbound-protocol keying) compression-api CI failures were cascade artifacts of codex-tools-strict-default failing in the same force-exit shard process — no own defect (171/171 local). Refs #9985 * fix(test): compression-api — register both describes before the runner starts The DATA_DIR setup + route/db top-level awaits sat BETWEEN the two describes; under --test-force-exit (the CI unit-runner flag) the process exits once the already-registered tests finish, so on slow CI machines the whole second describe died as 'Promise resolution is still pending' — the recurring CI-only shard-2 failure that never reproduced locally without the flag. Moved to the top of the file; 10/10 under --test-force-exit locally. Refs #9985 * fix(quality): freeze modelCapabilities.ts at 1006 (annotated) — snapshot routing growth Refs #9985 * fix(quality): move the modelCapabilities freeze into the frozen map (nested schema) Refs #9985 * fix(i18n): translate all 39,718 pending UI keys across 42 locales (owner-approved) Mass-translated every __MISSING__ placeholder via the official i18n:sync-ui --translate-markers pipeline (operator backend), restoring i18nUiCoverage to the 100 baseline (was 89.9 after the merge-storm UI landings + the 42 keys #9038 never shipped). Post-pass repairs, all caught by the existing gates: - glossary: retired renderings the machine reintroduced normalized again (提供商→提供者 zh-CN/zh-TW, 鏈接→連結, 文檔→文件, 調用→呼叫, 供應商→提供者, 響應→回應, 不活躍→未啟用 zh-TW; 클로드→Claude, 옴니루트→OmniRoute ko); DATA_DIR forbidden rendering avoided via 数据文件夹 rephrase - ICU integrity: 120 values with renamed/dropped {params} repaired (39 positional renames, 81 reset to the en source — functional over fluent) Validation: glossary/pt-BR/vi/deno-relay/settings-keys/value-drift/google- loopback suites 76/76; placeholder diff en×42 locales = 0; worst-locale coverage = 100.0%. Refs #9985 --------- Co-authored-by: backryun <bakryun0718@proton.me>
1006 lines
38 KiB
TypeScript
1006 lines
38 KiB
TypeScript
import {
|
|
PROVIDER_ID_TO_ALIAS,
|
|
PROVIDER_MODELS,
|
|
} from "@omniroute/open-sse/config/providerModels.ts";
|
|
import { parseModel, resolveCanonicalProviderModel } from "@omniroute/open-sse/services/model.ts";
|
|
import {
|
|
findModelSpecIdByExactOrAlias,
|
|
getAuthoritativeContextWindow,
|
|
getAuthoritativeProviderContextWindow,
|
|
getModelSpec,
|
|
type ModelSpec,
|
|
} from "@/shared/constants/modelSpecs";
|
|
import { getSyncedCapability } from "@/lib/modelsDevSync";
|
|
import { MODELS_DEV_PROVIDER_MAP } from "@/lib/modelsDevSync/transform";
|
|
import { getModelContextOverride } from "@/lib/db/modelContextOverrides";
|
|
import { getModelCapabilityOverride } from "@/lib/db/modelCapabilityOverrides";
|
|
import { getDbInstance } from "@/lib/db/core";
|
|
import { getKeyValue } from "@/lib/db/models/shared";
|
|
import type { ModelCapabilityResolutionSnapshot } from "@/lib/modelCapabilityResolutionSnapshot";
|
|
|
|
export type { ModelCapabilityResolutionSnapshot } from "@/lib/modelCapabilityResolutionSnapshot";
|
|
export { createModelCapabilityResolutionSnapshot } from "@/lib/modelCapabilityResolutionSnapshot";
|
|
import { isVisionModelId } from "@/shared/constants/visionModels";
|
|
import { getUnsupportedParams } from "@omniroute/open-sse/config/providerRegistry.ts";
|
|
import {
|
|
getLearnedThinkingCap,
|
|
GEMINI_FALLBACK_THINKING_CAP,
|
|
} from "@omniroute/open-sse/services/learnedThinkingCaps.ts";
|
|
|
|
const TOOL_CALLING_UNSUPPORTED_PATTERNS: string[] = [
|
|
// Specialty / non-chat surfaces must never inherit optimistic tool defaults (#8016)
|
|
"whisper",
|
|
"tts-1",
|
|
"gpt-4o-mini-tts",
|
|
"omni-moderation",
|
|
"moderation",
|
|
"eleven_multilingual",
|
|
"eleven_turbo",
|
|
"seedance",
|
|
"/veo",
|
|
"veo-",
|
|
"rerank",
|
|
"embedding",
|
|
"dall-e",
|
|
"flux-",
|
|
"stable-diffusion",
|
|
];
|
|
const REASONING_UNSUPPORTED_PATTERNS = [
|
|
"antigravity/claude-sonnet-4-6",
|
|
"antigravity/claude-sonnet-4-5",
|
|
"antigravity/claude-sonnet-4",
|
|
// Non-Claude antigravity models don't support thinking params (#1361)
|
|
"antigravity/gemini-",
|
|
"antigravity/gpt-oss-",
|
|
"antigravity/gemini-3",
|
|
"antigravity/tab_",
|
|
// Specialty / non-chat surfaces (#8016)
|
|
"whisper",
|
|
"tts-1",
|
|
"gpt-4o-mini-tts",
|
|
"omni-moderation",
|
|
"moderation",
|
|
"eleven_multilingual",
|
|
"eleven_turbo",
|
|
"seedance",
|
|
"/veo",
|
|
"veo-",
|
|
"rerank",
|
|
"embedding",
|
|
"dall-e",
|
|
"flux-",
|
|
"stable-diffusion",
|
|
];
|
|
|
|
/** Catalog/API surface types that are not chat completions. */
|
|
const NON_CHAT_SURFACE_TYPES = new Set([
|
|
"audio",
|
|
"video",
|
|
"image",
|
|
"moderation",
|
|
"rerank",
|
|
"embedding",
|
|
"music",
|
|
]);
|
|
|
|
export function isNonChatCatalogSurface(type: unknown): boolean {
|
|
return typeof type === "string" && NON_CHAT_SURFACE_TYPES.has(type);
|
|
}
|
|
|
|
const MAX_TOKENS_UNSUPPORTED_PATTERNS = [
|
|
"o1-preview",
|
|
"o1-mini",
|
|
"o1",
|
|
"o3-mini",
|
|
"o3",
|
|
"gpt-5.4",
|
|
"gpt-5.5",
|
|
];
|
|
|
|
type CapabilityInput =
|
|
| string
|
|
| {
|
|
provider?: string | null;
|
|
model?: string | null;
|
|
};
|
|
|
|
type SyncedCapabilities = ReturnType<typeof getSyncedCapability>;
|
|
|
|
/**
|
|
* Controls whether persisted operator/discovery overrides participate in resolution.
|
|
* Omit it (the public default) to resolve effective runtime capabilities. Catalog
|
|
* reconciliation alone uses `persistedOverrides: false` to compare discovery with
|
|
* static/synced catalog data without feeding an existing override back into itself.
|
|
*/
|
|
export interface ResolveModelCapabilitiesOptions {
|
|
persistedOverrides?: boolean;
|
|
}
|
|
|
|
export interface ResolvedModelCapabilities {
|
|
provider: string | null;
|
|
model: string | null;
|
|
rawModel: string | null;
|
|
toolCalling: boolean;
|
|
reasoning: boolean;
|
|
supportsThinking: boolean | null;
|
|
supportsTools: boolean | null;
|
|
supportsVision: boolean | null;
|
|
supportsAudio: boolean | null;
|
|
supportsMaxTokens: boolean;
|
|
attachment: boolean | null;
|
|
structuredOutput: boolean | null;
|
|
temperature: boolean | null;
|
|
contextWindow: number | null;
|
|
maxInputTokens: number | null;
|
|
maxOutputTokens: number | null;
|
|
defaultThinkingBudget: number;
|
|
thinkingBudgetCap: number | null;
|
|
thinkingOverhead: number | null;
|
|
adaptiveMaxTokens: number | null;
|
|
family: string | null;
|
|
status: string | null;
|
|
openWeights: boolean | null;
|
|
knowledgeCutoff: string | null;
|
|
releaseDate: string | null;
|
|
lastUpdated: string | null;
|
|
modalitiesInput: string[];
|
|
modalitiesOutput: string[];
|
|
interleavedField: string | null;
|
|
}
|
|
|
|
function toNonEmptyString(value: unknown): string | null {
|
|
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
|
|
}
|
|
|
|
function parseModalities(value: string | null | undefined): string[] {
|
|
if (typeof value !== "string" || value.trim().length === 0) return [];
|
|
try {
|
|
const parsed = JSON.parse(value);
|
|
return Array.isArray(parsed)
|
|
? parsed.filter((entry): entry is string => typeof entry === "string" && entry.length > 0)
|
|
: [];
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function getRegistryModel(providerIdOrAlias: string | null, modelId: string | null) {
|
|
if (!providerIdOrAlias || !modelId) return null;
|
|
const providerAlias = PROVIDER_ID_TO_ALIAS[providerIdOrAlias] || providerIdOrAlias;
|
|
const models = PROVIDER_MODELS[providerAlias];
|
|
if (!Array.isArray(models)) return null;
|
|
const normalizedModelId =
|
|
providerAlias === "cnl" ? modelId.replace(/-(?:xhigh|high|medium|low)$/i, "") : modelId;
|
|
return models.find((model) => model?.id === normalizedModelId) || null;
|
|
}
|
|
|
|
function resolveCapabilityInput(input: CapabilityInput) {
|
|
if (typeof input === "string") {
|
|
const parsed = parseModel(input);
|
|
const rawModel = toNonEmptyString(parsed.model);
|
|
if (parsed.provider) {
|
|
const canonical = resolveCanonicalProviderModel(parsed.provider, rawModel);
|
|
return {
|
|
provider: canonical.provider,
|
|
model: toNonEmptyString(canonical.model),
|
|
rawModel,
|
|
lookupKey: input,
|
|
};
|
|
}
|
|
|
|
return {
|
|
provider: null,
|
|
model: rawModel,
|
|
rawModel,
|
|
lookupKey: input,
|
|
};
|
|
}
|
|
|
|
const rawProvider = toNonEmptyString(input.provider);
|
|
const rawModel = toNonEmptyString(input.model);
|
|
if (rawProvider) {
|
|
const canonical = resolveCanonicalProviderModel(rawProvider, rawModel);
|
|
return {
|
|
provider: canonical.provider,
|
|
model: toNonEmptyString(canonical.model),
|
|
rawModel,
|
|
lookupKey: rawModel ? `${canonical.provider}/${rawModel}` : canonical.provider,
|
|
};
|
|
}
|
|
|
|
return {
|
|
provider: null,
|
|
model: rawModel,
|
|
rawModel,
|
|
lookupKey: rawModel || "",
|
|
};
|
|
}
|
|
|
|
function heuristicToolCalling(modelStr: string): boolean {
|
|
const normalized = String(modelStr || "").toLowerCase();
|
|
if (!normalized) return false;
|
|
const blocked = TOOL_CALLING_UNSUPPORTED_PATTERNS.some((pattern) => {
|
|
if (normalized === pattern) return true;
|
|
if (normalized.endsWith(`/${pattern}`)) return true;
|
|
return normalized.includes(pattern);
|
|
});
|
|
return !blocked;
|
|
}
|
|
|
|
function heuristicReasoning(modelStr: string): boolean {
|
|
const normalized = String(modelStr || "").toLowerCase();
|
|
if (!normalized) return true;
|
|
const blocked = REASONING_UNSUPPORTED_PATTERNS.some(
|
|
(pattern) =>
|
|
normalized === pattern || normalized.endsWith(`/${pattern}`) || normalized.includes(pattern)
|
|
);
|
|
return !blocked;
|
|
}
|
|
|
|
function heuristicMaxTokens(modelStr: string): boolean {
|
|
const normalized = String(modelStr || "").toLowerCase();
|
|
if (!normalized) return true;
|
|
const blocked = MAX_TOKENS_UNSUPPORTED_PATTERNS.some(
|
|
(pattern) =>
|
|
normalized === pattern || normalized.endsWith(`/${pattern}`) || normalized.includes(pattern)
|
|
);
|
|
return !blocked;
|
|
}
|
|
|
|
/** Last path segment of a path-shaped model id (`cline-pass/kimi-k3` → `kimi-k3`). */
|
|
function leafModelId(modelId: string | null | undefined): string | null {
|
|
if (!modelId || !modelId.includes("/")) return null;
|
|
const leaf = modelId.split("/").filter(Boolean).pop() ?? null;
|
|
return leaf && leaf !== modelId ? leaf : null;
|
|
}
|
|
|
|
function getStaticSpec(modelId: string | null, rawModel: string | null): ModelSpec | undefined {
|
|
if (modelId) {
|
|
const byCanonical = getModelSpec(modelId);
|
|
if (byCanonical) return byCanonical;
|
|
}
|
|
if (rawModel && rawModel !== modelId) {
|
|
return getModelSpec(rawModel);
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
/**
|
|
* #8032: vision-only leaf fallback for path-shaped routed ids.
|
|
*
|
|
* Must NOT live in getStaticSpec() — that helper also feeds supportsTools /
|
|
* supportsThinking / contextWindow / maxOutputTokens. A shared leaf lookup
|
|
* incorrectly promotes e.g. aihorde/deepseek/deepseek-v4-flash to the real
|
|
* DeepSeek V4 Flash tool-calling spec (#8212 regression).
|
|
*/
|
|
function getVisionStaticSpec(
|
|
modelId: string | null,
|
|
rawModel: string | null
|
|
): ModelSpec | undefined {
|
|
const direct = getStaticSpec(modelId, rawModel);
|
|
if (direct) return direct;
|
|
for (const candidate of [modelId, rawModel]) {
|
|
const leaf = leafModelId(candidate);
|
|
if (!leaf) continue;
|
|
const byLeaf = getModelSpec(leaf);
|
|
if (byLeaf) return byLeaf;
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
function getAuthoritativeStaticContextWindow(
|
|
provider: string | null,
|
|
modelId: string | null,
|
|
rawModel: string | null
|
|
): number | null {
|
|
for (const candidate of [modelId, rawModel]) {
|
|
const providerContextWindow = getAuthoritativeProviderContextWindow(provider, candidate);
|
|
if (typeof providerContextWindow === "number") return providerContextWindow;
|
|
}
|
|
for (const candidate of [modelId, rawModel]) {
|
|
const contextWindow = getAuthoritativeContextWindow(candidate);
|
|
if (typeof contextWindow === "number") return contextWindow;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// #8697-adjacent: this used to rescan Object.entries(MODEL_SPECS) per candidate per
|
|
// call — the top hotspot in a full catalog-rebuild profile once the pricing-path and
|
|
// getCanonicalModelSpecId() bottlenecks were fixed. Reuses the lazy index already built
|
|
// for getCanonicalModelSpecId() (@/shared/constants/modelSpecs) instead of duplicating a
|
|
// second cache over the same static table.
|
|
function getStaticSpecCanonicalModelId(modelId: string | null, rawModel: string | null) {
|
|
const candidates = [modelId, rawModel].filter(
|
|
(candidate): candidate is string => typeof candidate === "string" && candidate.length > 0
|
|
);
|
|
for (const candidate of candidates) {
|
|
const hit = findModelSpecIdByExactOrAlias(candidate);
|
|
if (hit) return hit;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Strip a trailing `-latest` alias suffix from a model id (#4073). Returns the
|
|
* short id (`pixtral-12b-latest` → `pixtral-12b`) or `null` when there is no
|
|
* `-latest` suffix to drop. Used only as a last-resort synced-lookup fallback.
|
|
*/
|
|
function stripLatestAlias(modelId: string | null): string | null {
|
|
if (!modelId) return null;
|
|
const stripped = modelId.replace(/-latest$/i, "");
|
|
return stripped && stripped !== modelId ? stripped : null;
|
|
}
|
|
|
|
// #8697-adjacent: MODELS_DEV_PROVIDER_MAP is a static module constant, so the result
|
|
// of reverseModelsDevProviders() never changes for a given provider — memoized by
|
|
// provider key instead of rescanning Object.entries(MODELS_DEV_PROVIDER_MAP) on every
|
|
// call (called once per model in a catalog rebuild). Never evicted — bounded by the
|
|
// number of distinct providers ever queried (~50-100 in practice), negligible memory.
|
|
const reverseModelsDevProvidersCache = new Map<string, readonly string[]>();
|
|
|
|
function reverseModelsDevProviders(provider: string): readonly string[] {
|
|
// models.dev may store capabilities under a different OmniRoute provider id
|
|
// that also maps from the same upstream models.dev provider. Build reverse
|
|
// candidates from MODELS_DEV_PROVIDER_MAP (e.g. openai ↔ cx).
|
|
//
|
|
// MODELS_DEV_PROVIDER_MAP's RHS is inconsistent: most providers list their
|
|
// canonical id directly, but the OAuth CLI providers (codex/claude) only
|
|
// list their alias (cx/cc), never the canonical id. Also probe the
|
|
// provider's alias so a canonical id like "codex"/"claude" still matches
|
|
// the map entries keyed only by "cx"/"cc" (#8429).
|
|
const cached = reverseModelsDevProvidersCache.get(provider);
|
|
if (cached) return cached;
|
|
|
|
const out = new Set<string>();
|
|
const providerAlias = PROVIDER_ID_TO_ALIAS[provider] || provider;
|
|
for (const [modelsDevId, omniIds] of Object.entries(MODELS_DEV_PROVIDER_MAP)) {
|
|
if (
|
|
omniIds.includes(provider) ||
|
|
omniIds.includes(providerAlias) ||
|
|
modelsDevId === provider ||
|
|
modelsDevId === providerAlias
|
|
) {
|
|
out.add(modelsDevId);
|
|
for (const id of omniIds) out.add(id);
|
|
}
|
|
}
|
|
// Frozen: the result is now shared across every future call for this provider (via
|
|
// the cache above) instead of a fresh array per call — freeze prevents an accidental
|
|
// caller mutation (e.g. .push()) from corrupting the cache for everyone else.
|
|
const result = Object.freeze([...out]);
|
|
reverseModelsDevProvidersCache.set(provider, result);
|
|
return result;
|
|
}
|
|
|
|
function getSyncedCapabilityForResolved(
|
|
provider: string | null,
|
|
model: string | null,
|
|
rawModel: string | null,
|
|
snapshot?: ModelCapabilityResolutionSnapshot | null
|
|
): SyncedCapabilities {
|
|
if (!provider || !model) return null;
|
|
|
|
const modelCandidates = Array.from(
|
|
new Set(
|
|
[model, rawModel, getStaticSpecCanonicalModelId(model, rawModel)]
|
|
.filter((value): value is string => typeof value === "string" && value.length > 0)
|
|
.flatMap((candidate) => {
|
|
const values = [candidate];
|
|
const stripped = stripLatestAlias(candidate);
|
|
if (stripped) values.push(stripped);
|
|
const leaf = leafModelId(candidate);
|
|
if (leaf) values.push(leaf);
|
|
// models.dev often stores OpenAI-family specialty models as qualified
|
|
// ids under another mapped provider, e.g. vercel + "openai/whisper-1".
|
|
if (!candidate.includes("/")) {
|
|
values.push(`${provider}/${candidate}`);
|
|
}
|
|
return values;
|
|
})
|
|
)
|
|
);
|
|
|
|
// Include common host providers that re-publish OpenAI specialty models under
|
|
// qualified ids (observed: vercel/openai/whisper-1, vercel/openai/tts-1).
|
|
const providerCandidates = Array.from(
|
|
new Set([provider, ...reverseModelsDevProviders(provider), "vercel"])
|
|
);
|
|
|
|
const bulk = snapshot?.synced ?? null;
|
|
for (const prov of providerCandidates) {
|
|
for (const mid of modelCandidates) {
|
|
const found = getSyncedCapability(prov, mid, bulk);
|
|
if (found) return found;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Last-resort vision fallback in resolveVisionCapability when there is no
|
|
* synced/registry/spec capability data (e.g. Mistral Pixtral, which ships no
|
|
* models.dev `attachment` flag and no registry `supportsVision`). Delegates to
|
|
* the single shared source (`@/shared/constants/visionModels`, #4072) so routing,
|
|
* the `/v1/models` listing and lite compression can never disagree on whether a
|
|
* model is vision-capable. The list is intentionally conservative — a false
|
|
* positive would let an image request route to a text-only model.
|
|
*/
|
|
export function modelIdLikelyVision(modelId: string | null | undefined): boolean {
|
|
return isVisionModelId(modelId);
|
|
}
|
|
|
|
/**
|
|
* Models that upstream catalogs (notably models.dev) mislabel as vision-capable but
|
|
* are TEXT-ONLY per the vendor's own docs. Listed here so a wrong synced
|
|
* `attachment:true` cannot route an image request to a blind model (the #4071 failure
|
|
* mode). Keep this list tiny and doc-backed.
|
|
*
|
|
* Xiaomi MiMo: only `mimo-v2.5` and `mimo-v2-omni` accept images; the `*-pro` chat
|
|
* models are text-only (mimo.mi.com .../image-understanding; hermes-agent#18884).
|
|
* Anchored to the full id (`$`) and tolerant of a `provider/` prefix so `mimo-v2.5-pro`
|
|
* never matches the multimodal `mimo-v2.5`, and `mimo-v2-pro` never matches `mimo-v2-omni`.
|
|
*/
|
|
const KNOWN_TEXT_ONLY_DESPITE_SYNC: readonly RegExp[] = [
|
|
/(?:^|\/)mimo-v2\.5-pro$/i,
|
|
/(?:^|\/)mimo-v2-pro$/i,
|
|
];
|
|
|
|
function isKnownTextOnlyDespiteSync(modelId: string | null | undefined): boolean {
|
|
if (!modelId) return false;
|
|
const id = String(modelId);
|
|
return KNOWN_TEXT_ONLY_DESPITE_SYNC.some((pattern) => pattern.test(id));
|
|
}
|
|
|
|
/** True when a modality list declares image and/or video input/output. */
|
|
function modalitiesDeclareVision(modalities: readonly string[]): boolean {
|
|
return modalities.some((entry) => {
|
|
const lower = String(entry).toLowerCase();
|
|
return lower.includes("image") || lower.includes("video");
|
|
});
|
|
}
|
|
|
|
/**
|
|
* #9195: Read the customModels supportsVision override for a given provider/model
|
|
* pair from the database. Returns true/false when an explicit override exists, or
|
|
* null if no custom model entry or no explicit flag. Sync read (better-sqlite3).
|
|
*/
|
|
function getCustomModelVisionOverride(provider: string, model: string): boolean | null {
|
|
try {
|
|
const db = getDbInstance();
|
|
const row = db
|
|
.prepare("SELECT value FROM key_value WHERE namespace = 'customModels' AND key = ?")
|
|
.get(provider);
|
|
if (!row) return null;
|
|
const parsed = getKeyValue(row);
|
|
if (!parsed.value) return null;
|
|
const models: Array<{ id: string; supportsVision?: boolean }> = JSON.parse(parsed.value);
|
|
const entry = models.find((m) => m.id === model);
|
|
if (entry && typeof entry.supportsVision === "boolean") {
|
|
return entry.supportsVision;
|
|
}
|
|
return null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function resolveVisionCapability(
|
|
spec: ModelSpec | undefined,
|
|
registryModel: { supportsVision?: boolean } | null,
|
|
synced: SyncedCapabilities,
|
|
modalitiesInput: string[],
|
|
modalitiesOutput: string[],
|
|
modelId?: string,
|
|
customVisionOverride?: boolean | null
|
|
): boolean | null {
|
|
const allModalities = [...modalitiesInput, ...modalitiesOutput].map((entry) =>
|
|
String(entry).toLowerCase()
|
|
);
|
|
|
|
// #9195: explicit custom model supportsVision override (from the dashboard
|
|
// "Vision capable" toggle) is the operator's authoritative choice for a
|
|
// self-hosted model. Check before the synced/registry/heuristic cascade so
|
|
// an operator-flagged vision model is never rejected by the Combo vision filter.
|
|
if (typeof customVisionOverride === "boolean") {
|
|
return customVisionOverride;
|
|
}
|
|
|
|
// Hard override FIRST: a wrong synced `attachment:true` (or image modality) must not
|
|
// win for models the vendor documents as text-only. Beats every branch below so an
|
|
// image request can never be routed to a blind model (#4071).
|
|
if (isKnownTextOnlyDespiteSync(modelId)) return false;
|
|
|
|
if (typeof synced?.attachment === "boolean") {
|
|
// #8250: models.dev sometimes ships attachment=false alongside image/video
|
|
// modalities (observed for Kimi K3). Prefer the richer modality signal over
|
|
// the contradictory false flag so supportsVision / attachment / modalities
|
|
// can be reconciled to a single vision-capable verdict.
|
|
if (synced.attachment === false && modalitiesDeclareVision(allModalities)) {
|
|
return true;
|
|
}
|
|
// #8032: attachment=false without modalities must not beat authoritative
|
|
// registry/spec vision for path-shaped custom/routed ids (e.g. Cline Pass
|
|
// `cp/cline-pass/kimi-k3` → MODEL_SPECS["kimi-k3"].supportsVision).
|
|
if (synced.attachment === false) {
|
|
if (registryModel?.supportsVision === true) return true;
|
|
if (spec?.supportsVision === true) return true;
|
|
return false;
|
|
}
|
|
return synced.attachment;
|
|
}
|
|
|
|
if (allModalities.some((entry) => entry.includes("image"))) {
|
|
return true;
|
|
}
|
|
|
|
if (allModalities.length > 0) {
|
|
return false;
|
|
}
|
|
|
|
if (typeof registryModel?.supportsVision === "boolean") return registryModel.supportsVision;
|
|
if (typeof spec?.supportsVision === "boolean") return spec.supportsVision;
|
|
|
|
// Last resort: no capability data at all. Positively confirm known multimodal
|
|
// families by model id so image requests can be routed to them; everything
|
|
// else stays `null` (unknown).
|
|
if (modelIdLikelyVision(modelId)) return true;
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Resolve whether a chat model accepts audio input.
|
|
*
|
|
* Explicit catalog metadata wins. Synced input modalities are authoritative
|
|
* only when they contain at least one declared modality; an empty list means
|
|
* that no source knows the answer and remains `null` so Audio Bridge can act
|
|
* conservatively.
|
|
*/
|
|
export function resolveAudioCapability(
|
|
spec: Pick<ModelSpec, "supportsAudio"> | undefined,
|
|
registryModel: { supportsAudio?: boolean } | null,
|
|
modalitiesInput: readonly string[]
|
|
): boolean | null {
|
|
if (typeof registryModel?.supportsAudio === "boolean") return registryModel.supportsAudio;
|
|
if (typeof spec?.supportsAudio === "boolean") return spec.supportsAudio;
|
|
if (modalitiesInput.length === 0) return null;
|
|
return modalitiesInput.some((entry) => String(entry).toLowerCase().includes("audio"));
|
|
}
|
|
|
|
/**
|
|
* Issue #6524: an operator-set `max_output_tokens` capability override (see
|
|
* `src/lib/db/modelCapabilityOverrides.ts`) is the manual escape hatch for a
|
|
* wrong/stale synced `limit_output` value (e.g. a provider's models.dev catalog
|
|
* row reporting `limit_output` equal to `limit_context`). It already won over the
|
|
* synced value in `getResolvedModelCapabilities().maxOutputTokens` — this helper
|
|
* makes `getExplicitModelOutputCap()` (used by the reasoning-token-buffer clamp)
|
|
* consult the same override so both read paths agree.
|
|
*/
|
|
/**
|
|
* Exact-match capability override lookup with intentional raw-alias fallback.
|
|
*
|
|
* An override may be stored under either the canonical model id or the exact
|
|
* provider-scoped raw alias the operator used (e.g. `github/claude-opus-4.5`
|
|
* resolving to canonical `claude-opus-4-5-20251101`). We consult the canonical
|
|
* id first, then the raw alias — both are exact provider/model matches. There is
|
|
* deliberately NO suffix/effort/family inheritance: an override for
|
|
* `codex/gpt-5.6` never applies to `codex/gpt-5.6-high`.
|
|
*/
|
|
function getCapabilityOverride(
|
|
resolved: { provider: string | null; model: string | null; rawModel: string | null },
|
|
key: "max_input_tokens" | "max_output_tokens"
|
|
): number | null {
|
|
const canonical = getModelCapabilityOverride(resolved.provider, resolved.model, key);
|
|
if (canonical !== null) return canonical;
|
|
return resolved.rawModel && resolved.rawModel !== resolved.model
|
|
? getModelCapabilityOverride(resolved.provider, resolved.rawModel, key)
|
|
: null;
|
|
}
|
|
|
|
function getContextOverride(
|
|
resolved: {
|
|
provider: string | null;
|
|
model: string | null;
|
|
rawModel: string | null;
|
|
},
|
|
snapshot?: ModelCapabilityResolutionSnapshot | null
|
|
): number | null {
|
|
const bulk = snapshot?.contextOverrides ?? null;
|
|
const canonical = getModelContextOverride(resolved.provider, resolved.model, bulk);
|
|
if (canonical !== null) return canonical;
|
|
return resolved.rawModel && resolved.rawModel !== resolved.model
|
|
? getModelContextOverride(resolved.provider, resolved.rawModel, bulk)
|
|
: null;
|
|
}
|
|
|
|
/**
|
|
* Resolve a persisted context override by canonical id, then by the exact raw
|
|
* alias supplied by the caller. Neither lookup inherits to related models.
|
|
*/
|
|
export function getResolvedModelContextOverride(input: CapabilityInput): number | null {
|
|
return getContextOverride(resolveCapabilityInput(input));
|
|
}
|
|
|
|
function getInputTokenCapabilityOverride(resolved: {
|
|
provider: string | null;
|
|
model: string | null;
|
|
rawModel: string | null;
|
|
}): number | null {
|
|
return getCapabilityOverride(resolved, "max_input_tokens");
|
|
}
|
|
|
|
function getOutputTokenCapabilityOverride(resolved: {
|
|
provider: string | null;
|
|
model: string | null;
|
|
rawModel: string | null;
|
|
}): number | null {
|
|
return getCapabilityOverride(resolved, "max_output_tokens");
|
|
}
|
|
|
|
/**
|
|
* Bulk-load friendly max_token override lookup (#9199). When a snapshot is
|
|
* supplied its preloaded map is used; otherwise falls back to the on-demand
|
|
* read (same precedence as getOutputTokenCapabilityOverride).
|
|
*/
|
|
function getMaxTokenCapabilityOverride(
|
|
resolved: {
|
|
provider: string | null;
|
|
model: string | null;
|
|
rawModel: string | null;
|
|
},
|
|
snapshot?: ModelCapabilityResolutionSnapshot | null
|
|
): number | null {
|
|
const bulk = snapshot?.maxTokenOverrides ?? null;
|
|
return (
|
|
getModelCapabilityOverride(resolved.provider, resolved.model, "max_output_tokens", bulk) ??
|
|
(resolved.rawModel && resolved.rawModel !== resolved.model
|
|
? getModelCapabilityOverride(resolved.provider, resolved.rawModel, "max_output_tokens", bulk)
|
|
: null)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Bulk-load friendly max_input_tokens override lookup (#9199): resolves from the
|
|
* snapshot's preloaded map instead of a per-model SQLite read.
|
|
*/
|
|
function getMaxInputTokenCapabilityOverride(
|
|
resolved: {
|
|
provider: string | null;
|
|
model: string | null;
|
|
rawModel: string | null;
|
|
},
|
|
snapshot: ModelCapabilityResolutionSnapshot
|
|
): number | null {
|
|
const bulk = snapshot.maxInputTokenOverrides;
|
|
return (
|
|
getModelCapabilityOverride(resolved.provider, resolved.model, "max_input_tokens", bulk) ??
|
|
(resolved.rawModel && resolved.rawModel !== resolved.model
|
|
? getModelCapabilityOverride(resolved.provider, resolved.rawModel, "max_input_tokens", bulk)
|
|
: null)
|
|
);
|
|
}
|
|
|
|
export function getExplicitModelOutputCap(
|
|
input: CapabilityInput,
|
|
snapshot?: ModelCapabilityResolutionSnapshot | null
|
|
): number | null {
|
|
const resolved = resolveCapabilityInput(input);
|
|
const maxTokenOverride = snapshot
|
|
? getMaxTokenCapabilityOverride(resolved, snapshot)
|
|
: getOutputTokenCapabilityOverride(resolved);
|
|
if (maxTokenOverride !== null) return maxTokenOverride;
|
|
|
|
const synced = getSyncedCapabilityForResolved(
|
|
resolved.provider,
|
|
resolved.model,
|
|
resolved.rawModel,
|
|
snapshot
|
|
);
|
|
if (synced && typeof synced.limit_output === "number") return synced.limit_output;
|
|
|
|
const registryModel = getRegistryModel(resolved.provider, resolved.model);
|
|
if (typeof registryModel?.maxOutputTokens === "number") return registryModel.maxOutputTokens;
|
|
|
|
const spec = getStaticSpec(resolved.model, resolved.rawModel);
|
|
return spec?.maxOutputTokens ?? null;
|
|
}
|
|
|
|
export function getResolvedModelCapabilities(
|
|
input: CapabilityInput,
|
|
options?: ResolveModelCapabilitiesOptions,
|
|
snapshot?: ModelCapabilityResolutionSnapshot | null
|
|
): ResolvedModelCapabilities {
|
|
// Reconciliation / auto-discovery needs the override-free catalog view so a
|
|
// persisted override never feeds back into the comparison that (re)writes it.
|
|
const usePersistedOverrides = options?.persistedOverrides !== false;
|
|
const resolved = resolveCapabilityInput(input);
|
|
const spec = getStaticSpec(resolved.model, resolved.rawModel);
|
|
const registryModel = getRegistryModel(resolved.provider, resolved.model);
|
|
const synced = getSyncedCapabilityForResolved(
|
|
resolved.provider,
|
|
resolved.model,
|
|
resolved.rawModel,
|
|
snapshot
|
|
);
|
|
|
|
const modalitiesInput = parseModalities(synced?.modalities_input);
|
|
const modalitiesOutput = parseModalities(synced?.modalities_output);
|
|
const lookupKey =
|
|
toNonEmptyString(
|
|
resolved.provider && resolved.model
|
|
? `${resolved.provider}/${resolved.model}`
|
|
: resolved.model || resolved.rawModel || resolved.lookupKey
|
|
) || "";
|
|
const reasoningDenied = !heuristicReasoning(lookupKey);
|
|
|
|
// Provider-level fallback: a live-discovered model (passthroughModels
|
|
// providers like AI Horde) has no per-model registry entry, synced
|
|
// capability, or static spec — every source above resolves to null, so
|
|
// toolCalling would otherwise fall through to heuristicToolCalling's
|
|
// optimistic default (true). Reuse the same unsupportedParams signal the
|
|
// request-time strip already relies on: if the provider declares "tools"
|
|
// unsupported for every model it serves, that's authoritative here too.
|
|
const providerDeniesTools =
|
|
resolved.provider && resolved.model
|
|
? getUnsupportedParams(resolved.provider, resolved.model).includes("tools")
|
|
: false;
|
|
|
|
const supportsTools =
|
|
synced?.tool_call ??
|
|
(typeof registryModel?.toolCalling === "boolean" ? registryModel.toolCalling : null) ??
|
|
(typeof spec?.supportsTools === "boolean" ? spec.supportsTools : null) ??
|
|
(providerDeniesTools ? false : null);
|
|
|
|
const supportsThinking = reasoningDenied
|
|
? false
|
|
: (synced?.reasoning ??
|
|
(typeof registryModel?.supportsReasoning === "boolean"
|
|
? registryModel.supportsReasoning
|
|
: null) ??
|
|
(typeof spec?.supportsThinking === "boolean" ? spec.supportsThinking : null));
|
|
|
|
const authoritativeContextWindow = getAuthoritativeStaticContextWindow(
|
|
resolved.provider,
|
|
resolved.model,
|
|
resolved.rawModel
|
|
);
|
|
// A persisted context-window override (operator-set or auto-discovered)
|
|
// reflects the real *total* window and wins over every static/synced source.
|
|
// `maxInputTokens` still follows its own precedence chain; only when that
|
|
// chain has no narrower source does it naturally fall back to this window.
|
|
const persistedContextWindow = usePersistedOverrides ? getContextOverride(resolved, snapshot) : null;
|
|
const contextWindow =
|
|
persistedContextWindow ??
|
|
authoritativeContextWindow ??
|
|
synced?.limit_context ??
|
|
(typeof registryModel?.contextLength === "number" ? registryModel.contextLength : null) ??
|
|
spec?.contextWindow ??
|
|
null;
|
|
|
|
const maxInputOverride = !usePersistedOverrides
|
|
? null
|
|
: snapshot
|
|
? getMaxInputTokenCapabilityOverride(resolved, snapshot)
|
|
: getInputTokenCapabilityOverride(resolved);
|
|
const maxTokenOverride = snapshot
|
|
? getMaxTokenCapabilityOverride(resolved, snapshot)
|
|
: usePersistedOverrides
|
|
? getOutputTokenCapabilityOverride(resolved)
|
|
: null;
|
|
|
|
// Vision consults leaf static metadata for path-shaped ids; other capability
|
|
// fields keep using the non-leaf `spec` from getStaticSpec() above.
|
|
const visionSpec = getVisionStaticSpec(resolved.model, resolved.rawModel);
|
|
|
|
// #9195: read the custom model's supportsVision override from the DB so the
|
|
// dashboard "Vision capable" toggle affects Combo routing.
|
|
const customVisionOverride =
|
|
resolved.provider && resolved.model
|
|
? getCustomModelVisionOverride(resolved.provider, resolved.model)
|
|
: null;
|
|
|
|
const supportsVision = resolveVisionCapability(
|
|
visionSpec,
|
|
registryModel,
|
|
synced,
|
|
modalitiesInput,
|
|
modalitiesOutput,
|
|
lookupKey,
|
|
customVisionOverride
|
|
);
|
|
const supportsAudio = resolveAudioCapability(spec, registryModel, modalitiesInput);
|
|
|
|
// #8250: when resolve promoted vision over a contradictory attachment=false,
|
|
// expose attachment=true so catalog / Vision Bridge / clients see one verdict.
|
|
let attachment = synced?.attachment ?? null;
|
|
if (supportsVision === true && attachment === false) {
|
|
attachment = true;
|
|
}
|
|
|
|
return {
|
|
provider: resolved.provider,
|
|
model: resolved.model,
|
|
rawModel: resolved.rawModel,
|
|
toolCalling: supportsTools ?? heuristicToolCalling(lookupKey),
|
|
reasoning: supportsThinking ?? heuristicReasoning(lookupKey),
|
|
supportsThinking,
|
|
supportsTools,
|
|
supportsVision,
|
|
supportsAudio,
|
|
supportsMaxTokens: heuristicMaxTokens(lookupKey),
|
|
attachment,
|
|
structuredOutput: synced?.structured_output ?? null,
|
|
temperature: synced?.temperature ?? null,
|
|
contextWindow,
|
|
maxInputTokens: (() => {
|
|
// Input cap is input-only. An explicit `max_input_tokens` override wins;
|
|
// otherwise fall back to the existing per-source input limits, then to the
|
|
// total window. The effective cap can never exceed the total window
|
|
// (input + output), so clamp it — but never double-count a requested
|
|
// output against this input cap.
|
|
const candidate =
|
|
maxInputOverride ??
|
|
(typeof registryModel?.maxInputTokens === "number" ? registryModel.maxInputTokens : null) ??
|
|
authoritativeContextWindow ??
|
|
synced?.limit_input ??
|
|
contextWindow;
|
|
return candidate !== null && contextWindow !== null
|
|
? Math.min(candidate, contextWindow)
|
|
: candidate;
|
|
})(),
|
|
maxOutputTokens:
|
|
maxTokenOverride ??
|
|
synced?.limit_output ??
|
|
(typeof registryModel?.maxOutputTokens === "number" ? registryModel.maxOutputTokens : null) ??
|
|
spec?.maxOutputTokens ??
|
|
null,
|
|
defaultThinkingBudget: spec?.defaultThinkingBudget ?? 0,
|
|
thinkingBudgetCap: spec?.thinkingBudgetCap ?? null,
|
|
thinkingOverhead: spec?.thinkingOverhead ?? null,
|
|
adaptiveMaxTokens: spec?.adaptiveMaxTokens ?? null,
|
|
family: synced?.family ?? null,
|
|
status: synced?.status ?? null,
|
|
openWeights: synced?.open_weights ?? null,
|
|
knowledgeCutoff: synced?.knowledge_cutoff ?? null,
|
|
releaseDate: synced?.release_date ?? null,
|
|
lastUpdated: synced?.last_updated ?? null,
|
|
modalitiesInput,
|
|
modalitiesOutput,
|
|
interleavedField:
|
|
synced?.interleaved_field ??
|
|
(typeof registryModel?.interleavedField === "string" ? registryModel.interleavedField : null),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Input cap enforced at the request-time hard gate, with explicit combo semantics.
|
|
*
|
|
* Feature 5004 lets a raw, exact `model_context_overrides` entry supersede a
|
|
* deliberately smaller catalog/client input hint for COMBO routing: the combo
|
|
* compatibility filter (`open-sse/services/combo/contextOverrideGate.ts`) already
|
|
* rescues such targets, so the final hard gate in handleChatCore must not turn
|
|
* around and reject the rescued target on the very hint the filter bypassed.
|
|
*
|
|
* Semantics (deliberately narrow — NO suffix/effort/family inheritance):
|
|
* - An explicit `max_input_tokens` capability override is ALWAYS enforced, for
|
|
* direct and combo requests alike. It is the operator's input-only ceiling and
|
|
* must never be bypassed by a context-window override.
|
|
* - Otherwise, for a COMBO request with an exact persisted context override, that
|
|
* context override is the input cap (it reflects the real window; the smaller
|
|
* catalog hint does not apply). Direct requests ignore this branch.
|
|
* - Otherwise the canonical `maxInputTokens` chain applies (registry input hint →
|
|
* authoritative window → synced limit_input → total window), clamped to the
|
|
* total window.
|
|
*
|
|
* `isCombo` selects the combo-rescue branch; pass `false`/omit for direct calls.
|
|
* The returned cap is still only an *input* bound — the total-window/output
|
|
* reserve check is enforced separately by `enforceOutputTokenBudget`.
|
|
*/
|
|
export function resolveInputTokenCapForGate(
|
|
input: CapabilityInput,
|
|
{ isCombo = false }: { isCombo?: boolean } = {}
|
|
): number | null {
|
|
const resolved = resolveCapabilityInput(input);
|
|
|
|
// 1. An explicit `max_input_tokens` override always wins and is never bypassed.
|
|
const explicitInputOverride = getInputTokenCapabilityOverride(resolved);
|
|
if (explicitInputOverride !== null) return explicitInputOverride;
|
|
|
|
// 2. Combo rescue: an exact persisted context override supersedes the smaller
|
|
// catalog/client input hint (mirrors contextOverrideGate.evaluateContextLimit).
|
|
if (isCombo) {
|
|
const contextOverride = getContextOverride(resolved);
|
|
if (contextOverride !== null) return contextOverride;
|
|
}
|
|
|
|
// 3. Canonical chain (already clamped to the total window by the resolver).
|
|
return getResolvedModelCapabilities(input).maxInputTokens;
|
|
}
|
|
|
|
export function supportsToolCalling(input: CapabilityInput): boolean {
|
|
if (typeof input === "string" && !String(input || "").trim()) return false;
|
|
return getResolvedModelCapabilities(input).toolCalling;
|
|
}
|
|
|
|
export function supportsReasoning(input: CapabilityInput): boolean {
|
|
if (typeof input === "string" && !String(input || "").trim()) return true;
|
|
return getResolvedModelCapabilities(input).reasoning;
|
|
}
|
|
|
|
export function supportsMaxTokens(input: CapabilityInput): boolean {
|
|
if (typeof input === "string" && !String(input || "").trim()) return true;
|
|
return getResolvedModelCapabilities(input).supportsMaxTokens;
|
|
}
|
|
|
|
export function capMaxOutputTokens(input: CapabilityInput, requested?: number): number | null {
|
|
const cap = getResolvedModelCapabilities(input).maxOutputTokens;
|
|
const hasRequested = typeof requested === "number" && Number.isFinite(requested);
|
|
if (cap === null) return hasRequested ? requested : null;
|
|
return hasRequested ? Math.min(requested, cap) : cap;
|
|
}
|
|
|
|
export function getDefaultThinkingBudget(input: CapabilityInput): number {
|
|
return getResolvedModelCapabilities(input).defaultThinkingBudget;
|
|
}
|
|
|
|
/**
|
|
* Clamp a requested thinking budget to the model's real ceiling.
|
|
*
|
|
* Resolution order (lowest wins):
|
|
* 1. Registry cap (MODEL_SPECS.thinkingBudgetCap) — authoritative when present.
|
|
* 2. Learned cap — a lower ceiling previously discovered via an upstream 400
|
|
* ("thinking_budget must be in the range ...") recorded by the executor
|
|
* (open-sse/services/learnedThinkingCaps.ts). In-memory, per provider+model.
|
|
* 3. Gemini-family fallback — when the registry has no cap but the model id
|
|
* contains "gemini" (any provider: many providers host Gemini models), clamp
|
|
* to GEMINI_FALLBACK_THINKING_CAP (32768, the known pro-tier cap) instead of
|
|
* letting an xhigh budget (131072) sail through to a 400. Registered flash
|
|
* models already carry their explicit 24576 cap via rule 1, so this only
|
|
* fires for unregistered Gemini ids.
|
|
*/
|
|
export function capThinkingBudget(input: CapabilityInput, budget: number): number {
|
|
const resolved = getResolvedModelCapabilities(input);
|
|
let cap = resolved.thinkingBudgetCap;
|
|
|
|
const modelId = resolved.model ?? resolved.rawModel ?? "";
|
|
const modelLower = modelId.toLowerCase();
|
|
// Learned-cap lookup needs a concrete provider key (the executor records under
|
|
// `this.provider`). When the input is a bare Gemini id, `resolved.provider` is
|
|
// null — but bare Gemini ids always route to the native Gemini provider, so
|
|
// default to "gemini". Without this a cap learned via the executor would be
|
|
// invisible to bare-model callers. Provider-qualified inputs keep their own
|
|
// provider, preserving per-provider independence.
|
|
const providerForLearned = resolved.provider ?? (modelLower.includes("gemini") ? "gemini" : null);
|
|
|
|
const learned = getLearnedThinkingCap(providerForLearned, modelId);
|
|
if (learned !== null) {
|
|
cap = cap === null ? learned : Math.min(cap, learned);
|
|
}
|
|
|
|
if (cap === null && modelLower.includes("gemini")) {
|
|
cap = GEMINI_FALLBACK_THINKING_CAP;
|
|
}
|
|
|
|
return Math.min(budget, cap ?? budget);
|
|
}
|
|
|
|
export function getModelContextLimit(
|
|
providerOrInput: CapabilityInput,
|
|
modelId?: string,
|
|
snapshot?: ModelCapabilityResolutionSnapshot | null
|
|
): number | null {
|
|
const resolved =
|
|
typeof providerOrInput === "string" && modelId !== undefined
|
|
? getResolvedModelCapabilities({ provider: providerOrInput, model: modelId }, undefined, snapshot)
|
|
: getResolvedModelCapabilities(providerOrInput, undefined, snapshot);
|
|
// Feature 5004: a persisted override (operator-set or auto-discovered) wins over the
|
|
// static catalog / models.dev sync. `getResolvedModelCapabilities` stays override-free
|
|
// so the reconciler can compare the catalog value against provider-declared windows.
|
|
const override = getModelContextOverride(
|
|
resolved.provider,
|
|
resolved.model,
|
|
snapshot?.contextOverrides ?? null
|
|
);
|
|
return override ?? resolved.contextWindow;
|
|
}
|