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[] = [