diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 87f1430..4a8fd69 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -29,7 +29,7 @@ const BIGMODEL_API = "https://bigmodel.cn" // BigModel's business API, where ZCo const ZAI_BASE = "https://api.z.ai/api/anthropic" const BIGMODEL_BASE = "https://open.bigmodel.cn/api/anthropic" const START_BASE = ZCODE + "/api/v1/zcode-plan/anthropic" -const APP_VERSION = "3.14.3" // the ZCode whose sign-in this makes +const APP_VERSION = "3.16.3" // the ZCode whose sign-in this makes const UA = "ZCode/" + APP_VERSION // ZCode's models before its config is read; the Start Plan has all but GLM-5.3. @@ -1400,28 +1400,48 @@ function intl() { } } -// startHeaders puts ZCode's headers on a Start Plan request, jwt its key. +// startHeaders is a Start Plan request's headers, jwt its key: ZCode's +// own (3.16.3's Frn and Vdt in glm/zcode.cjs) and the body's content-type, +// and nothing else. The agent's headers never go on: Claude Code's +// anthropic-beta, x-app, x-stainless-*, anthropic-dangerous-direct-browser- +// access and x-claude-code-session-id reach the plugin through magpie's +// gateway, and ZCode sends none of them (no anthropic-beta at all on the +// Start Plan: its tool search beta is for coding plans only). Accept and +// accept-encoding are left to the HTTP client, as ZCode leaves them to +// Node's. function startHeaders(h, jwt) { - h.delete("x-api-key") - h.delete("authorization") - h.delete("X-Device-Mid") - h.set("Authorization", "Bearer " + jwt) - h.set("anthropic-version", "2023-06-01") - h.set("HTTP-Referer", ZCODE) - h.set("User-Agent", UA + " ai-sdk/anthropic/3.0.81") - h.set("X-ZCode-App-Version", APP_VERSION) - h.set("X-Title", "Z Code@cli") - h.set("X-Release-Channel", "production") + const out = new Headers() + out.set("content-type", h.get("content-type") || "application/json") + out.set("Authorization", "Bearer " + jwt) + out.set("anthropic-version", "2023-06-01") + out.set("HTTP-Referer", ZCODE) + out.set("User-Agent", UA + " ai-sdk/anthropic/3.0.81") + out.set("X-ZCode-App-Version", APP_VERSION) + out.set("X-Title", "Z Code@cli") + out.set("X-Release-Channel", "production") const o = intl() - h.set("X-Client-Language", o.locale || "unknown") - if (o.timeZone) h.set("X-Client-Timezone", o.timeZone) - h.set("X-ZCode-Agent", "glm") - h.set("X-Platform", platform()) - h.set("X-Os-Category", osCategory()) - h.set("X-Os-Version", osVersion()) - h.set("x-request-id", uuid()) - h.set("x-zcode-session-type", "main") - h.set("x-zcode-trace-id", uuid()) + out.set("X-Client-Language", o.locale || "unknown") + out.set("X-Client-Timezone", o.timeZone || "unknown") + out.set("X-ZCode-Agent", "glm") + out.set("X-Platform", platform()) + out.set("X-Os-Category", osCategory()) + // os.release() alone, as ZCode's buildZCodeSourceHeadersFromContext + // has it; the prompt's OS line is osVersion() + out.set("X-Os-Version", release()) + out.set("x-request-id", uuid()) + out.set("x-zcode-session-type", "main") + out.set("x-zcode-trace-id", uuid()) + // 3.16's createModelRequestAttributionHeaders: what asked the model + out.set("x-zcode-query-source", "main_turn") + return out +} + +// startURL is a Start Plan request's URL without the query the agent put +// on it: Claude Code asks /v1/messages?beta=true, which magpie passes on, +// and ZCode asks /v1/messages alone. +function startURL(url) { + const i = url.indexOf("?") + return i < 0 ? url : url.slice(0, i) } // WORK_DIR and WORK_GIT are where magpie tells a plugin's fetch the agent @@ -1660,7 +1680,8 @@ export async function ZCodeAuthPlugin({ client }) { if (!s.jwt) throw new Error("ZCode's sign-in has no key; sign in again") swap(START_BASE) // the Start Plan is served only to what looks like ZCode's own request - startHeaders(h, s.jwt) + url = startURL(url) + h = startHeaders(h, s.jwt) if (opts.body != null) { const text = orig ?? (typeof opts.body === "string" ? opts.body : await new Response(opts.body).text()) opts.body = dress(text, s.site, s.device, place) diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 1921295..3c88b4a 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.21", + "version": "0.1.22", "description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan", "type": "module", "main": "./index.mjs", diff --git a/packages/zcode/start.test.mjs b/packages/zcode/start.test.mjs index d2b426a..151b80b 100644 --- a/packages/zcode/start.test.mjs +++ b/packages/zcode/start.test.mjs @@ -23,7 +23,7 @@ beforeAll(async () => { server = Bun.serve({ port: 0, async fetch(req) { - got.push({ method: req.method, path: new URL(req.url).pathname, headers: Object.fromEntries(req.headers), body: await req.text() }) + got.push({ method: req.method, path: new URL(req.url).pathname, query: new URL(req.url).search, headers: Object.fromEntries(req.headers), body: await req.text() }) if (answer) return answer() return Response.json({ type: "message", content: [] }) }, @@ -59,11 +59,11 @@ const start = (site) => { // send makes a request as @ai-sdk/anthropic does, to the URL it was made for // place is what magpie tells the fetch of where the agent works, as its // headers: {dir, git} -async function send(auth, body, { url = "https://api.z.ai/api/anthropic/v1/messages", asRequest = false, place = {} } = {}) { +async function send(auth, body, { url = "https://api.z.ai/api/anthropic/v1/messages", asRequest = false, place = {}, extra = {} } = {}) { routeToFake() const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } }) const opts = await hooks.auth.loader(async () => auth, { id: "zcode" }) - const headers = { "content-type": "application/json", "x-api-key": "zcode", "anthropic-version": "2023-06-01", "user-agent": "ai-sdk/anthropic/3.0.81" } + const headers = { "content-type": "application/json", "x-api-key": "zcode", "anthropic-version": "2023-06-01", "user-agent": "ai-sdk/anthropic/3.0.81", ...extra } if (place.dir !== undefined) headers["x-magpie-work-dir"] = place.dir if (place.git !== undefined) headers["x-magpie-work-git"] = place.git const res = asRequest @@ -198,8 +198,8 @@ test("a Start Plan request carries ZCode's headers, and only those", async () => "anthropic-version": "2023-06-01", "content-type": "application/json", "http-referer": "https://zcode.z.ai", - "user-agent": "ZCode/3.14.3 ai-sdk/anthropic/3.0.81", - "x-zcode-app-version": "3.14.3", + "user-agent": "ZCode/3.16.3 ai-sdk/anthropic/3.0.81", + "x-zcode-app-version": "3.16.3", "x-title": "Z Code@cli", "x-release-channel": "production", "x-client-language": o.locale, @@ -207,15 +207,58 @@ test("a Start Plan request carries ZCode's headers, and only those", async () => "x-zcode-agent": "glm", "x-platform": `${process.platform}-${process.arch}`, "x-os-category": { darwin: "macos", win32: "windows" }[process.platform] ?? "linux", - "x-os-version": osVersion, + "x-os-version": release(), "x-request-id": h["x-request-id"], "x-zcode-session-type": "main", "x-zcode-trace-id": h["x-zcode-trace-id"], + "x-zcode-query-source": "main_turn", }) expect(h["x-api-key"]).toBeUndefined() expect(h["x-device-mid"]).toBeUndefined() }) +// What Claude Code sends magpie, which magpie's gateway passes on to a +// provider without an account of its own, as a plugin's is: its betas, +// x-app, its SDK's x-stainless-* and ?beta=true. ZCode sends none of it. +const CLAUDE_CODE = { + "anthropic-beta": "claude-code-20250219,interleaved-thinking-2025-05-14,context-management-2025-06-27", + "anthropic-dangerous-direct-browser-access": "true", + "x-app": "cli", + "x-claude-code-session-id": "0b6f0c55-5e3a-4c55-9d0e-6c7c1f3a2b11", + "x-stainless-lang": "js", + "x-stainless-package-version": "0.60.0", + "x-stainless-os": "MacOS", + "x-stainless-arch": "arm64", + "x-stainless-runtime": "node", + "x-stainless-runtime-version": "v24.3.0", + "x-stainless-retry-count": "0", + "x-stainless-timeout": "600", + accept: "application/json, text/event-stream", + "accept-language": "*", + "sec-fetch-mode": "cors", +} + +test("a Start Plan request from Claude Code goes with ZCode's headers alone, without ?beta=true", async () => { + for (const asRequest of [false, true]) { + got.length = 0 + const r = await send(start("zai"), JSON.stringify(agentBody), { url: "https://api.z.ai/api/anthropic/v1/messages?beta=true", asRequest, extra: CLAUDE_CODE }) + expect(r.path).toBe("/zcode.z.ai/api/v1/zcode-plan/anthropic/v1/messages") + expect(r.query).toBe("") + const h = own(r.headers) + for (const k of Object.keys(CLAUDE_CODE)) if (!TRANSPORT.has(k)) expect(h[k]).toBeUndefined() + expect(Object.keys(h).sort()).toEqual( + ["authorization", "anthropic-version", "content-type", "http-referer", "user-agent", "x-zcode-app-version", "x-title", "x-release-channel", "x-client-language", "x-client-timezone", "x-zcode-agent", "x-platform", "x-os-category", "x-os-version", "x-request-id", "x-zcode-session-type", "x-zcode-trace-id", "x-zcode-query-source"].sort(), + ) + expect(r.headers.accept).not.toBe(CLAUDE_CODE.accept) + } +}) + +test("a GLM Coding Plan request keeps the agent's query and headers", async () => { + const r = await send({ type: "api", key: "one.secret", metadata: { site: "zai" } }, JSON.stringify(agentBody), { url: "https://api.z.ai/api/anthropic/v1/messages?beta=true", extra: { "anthropic-beta": "x-1" } }) + expect(r.query).toBe("?beta=true") + expect(r.headers["anthropic-beta"]).toBe("x-1") +}) + test("a Start Plan request's body is dressed as ZCode's", async () => { const r = await send(start("zai"), JSON.stringify(agentBody)) const b = JSON.parse(r.body) diff --git a/packages/zcode/usage.test.mjs b/packages/zcode/usage.test.mjs index 79a3a50..4bdec23 100644 --- a/packages/zcode/usage.test.mjs +++ b/packages/zcode/usage.test.mjs @@ -371,7 +371,7 @@ test("a gift plan ZCode holds but has not granted is named on the card, to claim // the preview went out as ZCode's own client asks it const p = calls.find((c) => c.url.pathname.endsWith("/preview")) expect(p.method).toBe("GET") - expect(p.url.searchParams.get("app_version")).toBe("3.14.3") + expect(p.url.searchParams.get("app_version")).toBe("3.16.3") expect(p.url.searchParams.get("platform")).toBe(`${process.platform}-${process.arch}`) expect(p.headers.Authorization).toBe("Bearer " + token) expect(p.headers["X-Device-Mid"]).toBe("11111111-2222-4333-8444-555555555555")