zcode 0.1.22: a Start Plan request goes with ZCode 3.16.3's headers alone, never the agent's, and without ?beta=true
01huadalang (Discord) gets 405 / "ZCode's Start Plan still turned this request away" again on 0.1.21. #343's check was a curl request to magpie, which carries no agent headers. What reached zcode.z.ai: startHeaders set ZCode's headers on top of the ones it was given and removed only x-api-key, authorization and X-Device-Mid. A plugin provider has no account of its own in magpie's gateway, so a request from Claude Code goes to the plugin with Claude Code's own headers (anthropic-beta claude-code-20250219,..., x-app: cli, x-stainless-*, anthropic-dangerous-direct-browser-access, x-claude-code-session-id) and ?beta=true on the URL (gateway.go claudeCodeHeader, #179). All of that went on to the Start Plan. The retired built-in deleted anthropic-beta, and its accounts never got the Claude Code pass-through. ZCode sends none of these on the Start Plan: its only beta, advanced-tool-use, is for coding plans. Now startHeaders builds a new header set: the body's content-type and ZCode's headers, as ZCode.app 3.16.3 (the current stable, 10-23) sends them in glm/zcode.cjs: - buildZCodeSourceHeadersFromContext: X-Os-Version is os.release() alone (it was "darwin 25.2.0 arm64"), and X-Client-Timezone is "unknown" when there is none. - createModelRequestAttributionHeaders: the new x-zcode-query-source: main_turn. - APP_VERSION goes to 3.16.3 (User-Agent, X-ZCode-App-Version and the app_version query). The Start Plan URL loses its query. Prompt texts, the Start Plan provider entry and ai-sdk/anthropic 3.0.81 are the same in 3.14.3 and 3.16.3 (checked against both bundles). The GLM Coding Plan and team paths are unchanged, and a new test holds that they keep the agent's query and headers. Not established: which header (if any) zcode.z.ai keys on. No Start Plan account was used. A network block of the user's IP is still possible, and the error still says so. Run: bun test in packages/zcode under a temp HOME, 90 pass. The two start.test.mjs header tests fail on 0.1.21's index.mjs (Claude Code's x-app/x-stainless-*/anthropic-beta and the query reach the fake).
This commit is contained in:
+43
-22
@@ -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)
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user