From d3aaca7a4f0f12732e32e91d5fc6e7c25b27e42f Mon Sep 17 00:00:00 2001 From: Gui-Yue Date: Sun, 1 Mar 2026 10:50:32 +0800 Subject: [PATCH] fix(openai): preserve non-v1 version suffix in base URL --- src/infra/providers/utils.ts | 4 ++-- tests/unit/providers/openai.test.ts | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/infra/providers/utils.ts b/src/infra/providers/utils.ts index 7af874b..18ef69e 100644 --- a/src/infra/providers/utils.ts +++ b/src/infra/providers/utils.ts @@ -58,8 +58,8 @@ export function normalizeBaseUrl(url: string): string { export function normalizeOpenAIBaseUrl(url: string): string { let normalized = url.replace(/\/+$/, ''); - // Auto-append /v1 if not present (for OpenAI-compatible APIs) - if (!normalized.endsWith('/v1')) { + // Auto-append /v1 if no version path detected (e.g., /v1, /v2, /v4) + if (!/\/v\d+$/.test(normalized)) { normalized += '/v1'; } return normalized; diff --git a/tests/unit/providers/openai.test.ts b/tests/unit/providers/openai.test.ts index 0efd3d2..14e94bf 100644 --- a/tests/unit/providers/openai.test.ts +++ b/tests/unit/providers/openai.test.ts @@ -10,6 +10,11 @@ runner const config = provider.toConfig(); expect.toEqual(config.baseUrl, 'https://api.openai.com/v1'); }) + .test('baseUrl 保留已有版本路径 /v4 (GLM coding endpoint)', async () => { + const provider = new OpenAIProvider('test-key', 'any-model', 'https://open.bigmodel.cn/api/coding/paas/v4'); + const config = provider.toConfig(); + expect.toEqual(config.baseUrl, 'https://open.bigmodel.cn/api/coding/paas/v4'); + }) .test('请求体包含 system 与工具调用结构', async () => { const provider = new OpenAIProvider('test-key', 'gpt-4o', 'https://api.openai.com'); const messages: Message[] = [