From bd174fc22bc472178de847b4364fd3f5209c4786 Mon Sep 17 00:00:00 2001 From: JoeBot Date: Sun, 5 Apr 2026 08:14:37 -0400 Subject: [PATCH] fix: normalize API URL to prevent duplicate /chat/completions path Users entering the full endpoint URL (e.g. https://openrouter.ai/api/v1/chat/completions) caused a 404 because the code appended /chat/completions again. Now strips any trailing /chat/completions before re-appending, and adds a hint in the UI. Co-Authored-By: Claude Opus 4.6 --- backend/src/common/utils/ai-caller.ts | 7 ++++++- frontend/src/pages/admin/AdminShadowAiPage.tsx | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/backend/src/common/utils/ai-caller.ts b/backend/src/common/utils/ai-caller.ts index 13f80e8..d3b14a4 100644 --- a/backend/src/common/utils/ai-caller.ts +++ b/backend/src/common/utils/ai-caller.ts @@ -37,7 +37,12 @@ export async function callOpenAICompatible(params: AICallerParams): Promise((resolve, reject) => { - const url = new URL(`${apiUrl}/chat/completions`); + // Normalize: strip trailing slash and /chat/completions if user included it + let baseUrl = apiUrl.replace(/\/+$/, ''); + if (baseUrl.endsWith('/chat/completions')) { + baseUrl = baseUrl.slice(0, -'/chat/completions'.length); + } + const url = new URL(`${baseUrl}/chat/completions`); const options = { hostname: url.hostname, diff --git a/frontend/src/pages/admin/AdminShadowAiPage.tsx b/frontend/src/pages/admin/AdminShadowAiPage.tsx index 996a7f8..70ca42a 100644 --- a/frontend/src/pages/admin/AdminShadowAiPage.tsx +++ b/frontend/src/pages/admin/AdminShadowAiPage.tsx @@ -194,7 +194,7 @@ function ModelSlotCard({ slot, model, isLoading }: { slot: string; model?: Shado setName(e.target.value)} size="sm" /> - setApiUrl(e.target.value)} size="sm" /> + setApiUrl(e.target.value)} size="sm" /> setApiKey(e.target.value)} size="sm" /> setModelName(e.target.value)} size="sm" /> setIsActive(e.currentTarget.checked)} /> -- 2.49.1