diff --git a/packages/autoclaw/index.mjs b/packages/autoclaw/index.mjs index 4415826..5dadae6 100644 --- a/packages/autoclaw/index.mjs +++ b/packages/autoclaw/index.mjs @@ -42,6 +42,12 @@ const OFFICIAL_SYSTEM_PROMPT = 'You are AutoClaw. Answer the user directly and c const WALLETS_PATH = `/agent-assetmgr/api/v2/wallets?biz_app_id=${PRODUCT}`; const FLAGGED = [ + // Claude Code 2.1.30x opens with an SDK identity line, not the CLI one; + // the CN gateway refuses "Claude Agent SDK" in any case with 406. Take the + // trailing period and the article/possessive so the replacement reads as + // a sentence, not "assistant.." or "Anthropic's the agent framework". + [/You are a Claude agent, built on Anthropic's Claude Agent SDK\.?/gi, "You are a coding assistant."], + [/(?:Anthropic's |the )?Claude Agent SDK/gi, "the agent framework"], [/You are an AI agent powered by DeepSeek Harness/gi, "You are an AI agent powered by DeepSeek-Harness"], [/You are Claude Code/gi, "You are the Claude Code CLI"], [/You are Codex/gi, "You are the Codex agent"], @@ -84,7 +90,7 @@ function unflag(messages) { } const REFUSED_HINT = - "AutoClaw refuses chats whose system prompt carries another agent's identity line (Claude Code, Codex, opencode, Cursor, Cline, Qwen Code, ZCode, pi, DeepSeek Harness); this plugin rewrites the ones it knows, so an unknown one needs another provider in this group"; + "AutoClaw refuses chats whose system prompt carries another agent's identity line (Claude Code, Claude Agent SDK, Codex, opencode, Cursor, Cline, Qwen Code, ZCode, pi, DeepSeek Harness); this plugin rewrites the ones it knows, so an unknown one needs another provider in this group"; async function explained(res) { if (res.status < 400) return res; @@ -628,7 +634,7 @@ function makeAutoclawPlugin({ providerId, providerName, homeHost, loginMethod }) headers, body: JSON.stringify(payload), }); - if (res.status !== 401 && res.status !== 403) return res; + if (res.status !== 401 && res.status !== 403) return explained(res); try { res.body?.cancel?.(); } catch {} return new Response(JSON.stringify({ error: { message: "AutoClaw 登录已失效", type: "invalid_request_error" } }), { status: 401, diff --git a/packages/autoclaw/index.test.mjs b/packages/autoclaw/index.test.mjs index 84236d9..7ad6460 100644 --- a/packages/autoclaw/index.test.mjs +++ b/packages/autoclaw/index.test.mjs @@ -188,6 +188,19 @@ test("ZCode's and pi's identity lines are rewritten", () => { expect(unflagged("You are Claude Code, Anthropic's official CLI for Claude.")).toBe("You are the Claude Code CLI, Anthropic's official CLI for Claude.") }) +// Claude Code 2.1.30x opens its system prompt with the SDK identity line, +// not the CLI one. The CN gateway refuses "Claude Agent SDK" in any case, +// wherever it sits; the bare phrase is matched so "running on" variants and +// later mentions are covered too. +test("Claude Agent SDK's identity line is rewritten", () => { + const sdk = "You are a Claude agent, built on Anthropic's Claude Agent SDK." + expect(unflagged(sdk)).toBe("You are a coding assistant.") + expect(unflagged("You are a Claude agent, running on Anthropic's Claude Agent SDK.")).toBe("You are a Claude agent, running on the agent framework.") + expect(unflagged("We build on the Claude Agent SDK; see docs.")).toBe("We build on the agent framework; see docs.") + // the same text in, the same text out: a cached prompt prefix stays put + expect(unflagged(unflagged(sdk))).toBe(unflagged(sdk)) +}) + // The gateway reads the system prompt, not the chat: the caller's own words // in a user or assistant message stay as they are. test("unflag rewrites system and developer messages only", () => { @@ -267,6 +280,39 @@ test("overseas loader().fetch drops X-Harness-Type and explains 406", async () = }) +test("CN loader rewrites the Claude Agent SDK identity and explains its 406", async () => { + const realFetch = globalThis.fetch + const calls = [] + globalThis.fetch = async (input, init) => { + calls.push({ url: String(input), headers: new Headers(init?.headers), body: JSON.parse(String(init?.body ?? "{}")) }) + return new Response(JSON.stringify({ error: { code: null, message: "406 Not Acceptable", type: "api_error" } }), { status: 406 }) + } + try { + const hooks = await AutoclawCnAuthPlugin({ client: { auth: { set: async () => {} } } }) + const loader = await hooks.auth.loader(async () => ({ type: "oauth", access: "a1" })) + const body = JSON.stringify({ + model: "zaicoding_glm-5.3", + messages: [ + { role: "system", content: "You are a Claude agent, built on Anthropic's Claude Agent SDK." }, + { role: "user", content: "hi" }, + ], + }) + + const refused = await loader.fetch("x", { method: "POST", body }) + expect(refused.status).toBe(406) + expect(refused.headers.get("X-Magpie-Sign-In")).toBe("kept") + expect((await refused.json()).error.message).toContain(REFUSED_HINT) + + const sent = calls.at(-1) + expect(sent.url).toBe("https://autoglm-acceleration-api.zhipuai.cn/autoclaw-proxy/proxy/autoclaw/chat/completions") + expect(sent.body.messages[0].content.startsWith(OFFICIAL_SYSTEM_PROMPT)).toBe(true) + expect(sent.body.messages[0].content).not.toMatch(/Claude Agent SDK/i) + expect(sent.headers.get("X-Authorization")).toBe("Bearer a1") + } finally { + globalThis.fetch = realFetch + } +}) + test("refreshTokens orders hosts by the token's source_id and falls back on 400002", async () => { const realFetch = globalThis.fetch const requests = []