From afe2ab37e45f3b7303367cabfe6f9600b7e990c2 Mon Sep 17 00:00:00 2001 From: diegosouzapw Date: Wed, 11 Mar 2026 12:42:18 -0300 Subject: [PATCH] fix(aliases): resolve custom model aliases before routing + restore on startup (#315, #316) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #315: Import and call resolveModelAlias() in chatCore.ts before the getModelTargetFormat() lookup so that custom aliases configured in Settings → Model Aliases → Pattern→Target are actually applied during routing instead of being silently ignored. #316: Load persisted custom model aliases from settings DB at server startup (instrumentation.ts). Previously _customAliases started as an empty object after every restart since setCustomAliases() was only called by the PUT /api/settings/model-aliases handler — never at init. Now aliases are restored from settings.modelAliases JSON field on boot. --- open-sse/handlers/chatCore.ts | 8 +++++++- src/instrumentation.ts | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) 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");