From ef513622dd4ac586a1dd9d40c80b2d40ce7b7de3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=A4=A7=E7=8C=AB?= <16399091+binaricat@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:48:35 +0800 Subject: [PATCH] fix(ai): use the versioned Ark Agent Plan messages endpoint --- infrastructure/ai/anthropicCompatBaseUrl.test.ts | 16 ++++++++++++++++ infrastructure/ai/anthropicCompatBaseUrl.ts | 4 ++++ infrastructure/ai/sdkProviders.test.ts | 3 +++ 3 files changed, 23 insertions(+) diff --git a/infrastructure/ai/anthropicCompatBaseUrl.test.ts b/infrastructure/ai/anthropicCompatBaseUrl.test.ts index cfd181400..79b5b6bd9 100644 --- a/infrastructure/ai/anthropicCompatBaseUrl.test.ts +++ b/infrastructure/ai/anthropicCompatBaseUrl.test.ts @@ -71,3 +71,19 @@ test("normalizeAnthropicSdkBaseURL preserves custom non-/v1 path prefixes", () = "https://gateway.example/api/v1", ); }); + +test("normalizes the official Ark Agent Plan Anthropic prefix only", () => { + const base = "https://ark.cn-beijing.volces.com/api/plan"; + for (const input of [base, base + "/", " " + base + " ", base + "/v1", base + "/v1/"]) { + assert.equal(normalizeAnthropicSdkBaseURL(input), base + "/v1"); + } + for (const input of [ + base.replace("https:", "http:"), + base.replace(".com", ".com.example"), + base.replace(".com", ".com:8443"), + base + "/custom", + "https://gateway.example/api/plan", + ]) { + assert.equal(normalizeAnthropicSdkBaseURL(input), input); + } +}); diff --git a/infrastructure/ai/anthropicCompatBaseUrl.ts b/infrastructure/ai/anthropicCompatBaseUrl.ts index 26c780a8d..235920581 100644 --- a/infrastructure/ai/anthropicCompatBaseUrl.ts +++ b/infrastructure/ai/anthropicCompatBaseUrl.ts @@ -39,6 +39,7 @@ export function isBareOriginBaseURL(baseURL: string): boolean { * Normalize a stored Anthropic-compat Base URL for @ai-sdk/anthropic. * - Bare hosts gain a /v1 suffix (Claude Code style → SDK style). * - Bases that already end in /v1 are left unchanged. + * - The official Ark Agent Plan /api/plan prefix gains a /v1 suffix. * - Other path prefixes (custom proxies) are left unchanged so chat keeps * requesting `{prefix}/messages` rather than `{prefix}/v1/messages`. */ @@ -46,6 +47,9 @@ export function normalizeAnthropicSdkBaseURL(baseURL: string): string { const trimmed = stripTrailingSlashes(baseURL); if (!trimmed) return trimmed; if (anthropicBaseIncludesV1(trimmed)) return trimmed; + // Claude Code appends /v1/messages to the documented Ark Agent Plan base. + // The Anthropic SDK appends only /messages, so supply the missing /v1. + if (trimmed === "https://ark.cn-beijing.volces.com/api/plan") return `${trimmed}/v1`; if (!isBareOriginBaseURL(trimmed)) return trimmed; return `${trimmed}/v1`; } diff --git a/infrastructure/ai/sdkProviders.test.ts b/infrastructure/ai/sdkProviders.test.ts index cc0cc772f..5e21de5fa 100644 --- a/infrastructure/ai/sdkProviders.test.ts +++ b/infrastructure/ai/sdkProviders.test.ts @@ -365,6 +365,9 @@ test("Anthropic SDK sends x-api-key only on official host and adds Bearer on cus })); await assert.rejects(async () => await model.doGenerate({ prompt })); const request = requests.at(-1)!; + assert.equal(request.url, baseURL.endsWith("/api/plan") + ? baseURL + "/v1/messages" + : baseURL + "/messages"); assert.equal(request.headers.get("x-api-key"), "__IPC_SECURED__"); assert.equal(request.headers.get("authorization"), "Bearer __IPC_SECURED__"); assert.equal(request.headers.get("anthropic-version"), "2023-06-01");