diff --git a/open-sse/handlers/chatCore.ts b/open-sse/handlers/chatCore.ts index f0175a0b3d..c8fecb49da 100644 --- a/open-sse/handlers/chatCore.ts +++ b/open-sse/handlers/chatCore.ts @@ -12,6 +12,7 @@ import { addBufferToUsage, filterUsageForFormat, estimateUsage } from "../utils/ import { refreshWithRetry } from "../services/tokenRefresh.ts"; import { createRequestLogger } from "../utils/requestLogger.ts"; import { getModelTargetFormat, PROVIDER_ID_TO_ALIAS } from "../config/providerModels.ts"; +import { resolveModelAlias } from "../services/modelDeprecation.ts"; import { createErrorResult, parseUpstreamError, formatProviderError } from "../utils/error.ts"; import { HTTP_STATUS } from "../config/constants.ts"; import { handleBypassRequest } from "../utils/bypassHandler.ts"; @@ -105,8 +106,13 @@ export async function handleChatCore({ // Detect source format and get target format // Model-specific targetFormat takes priority over provider default + // Apply custom model aliases (Settings → Model Aliases → Pattern→Target) before routing (#315) + // Custom aliases take priority over built-in and must be resolved here so the + // downstream getModelTargetFormat() lookup uses the correct, aliased model ID. + const resolvedModel = resolveModelAlias(model); + const alias = PROVIDER_ID_TO_ALIAS[provider] || provider; - const modelTargetFormat = getModelTargetFormat(alias, model); + const modelTargetFormat = getModelTargetFormat(alias, resolvedModel); const targetFormat = modelTargetFormat || getTargetFormat(provider); // Default to false unless client explicitly sets stream: true (OpenAI spec compliant) diff --git a/src/instrumentation.ts b/src/instrumentation.ts index 3852eb517c..aa97723fed 100644 --- a/src/instrumentation.ts +++ b/src/instrumentation.ts @@ -46,6 +46,30 @@ export async function register() { startBackgroundRefresh(); console.log("[STARTUP] Quota cache background refresh started"); + // Model aliases: restore persisted custom aliases into in-memory state (#316) + // Custom aliases are saved to settings.modelAliases on PUT /api/settings/model-aliases + // but the in-memory _customAliases resets to {} on every restart — load them here. + try { + const { getSettings } = await import("@/lib/db/settings"); + const { setCustomAliases } = await import("@omniroute/open-sse/services/modelDeprecation.ts"); + const settings = getSettings(); + if (settings.modelAliases) { + const aliases = + typeof settings.modelAliases === "string" + ? JSON.parse(settings.modelAliases) + : settings.modelAliases; + if (aliases && typeof aliases === "object") { + setCustomAliases(aliases); + console.log( + `[STARTUP] Restored ${Object.keys(aliases).length} custom model alias(es) from settings` + ); + } + } + } catch (err: unknown) { + const msg = err instanceof Error ? err.message : String(err); + console.warn("[STARTUP] Could not restore model aliases:", msg); + } + // Compliance: Initialize audit_log table + cleanup expired logs try { const { initAuditLog, cleanupExpiredLogs } = await import("@/lib/compliance/index");