From a810ee7e882012ab106e2deb742176cd43db781a Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 14 Oct 2026 18:57:57 +0800 Subject: [PATCH] zen-free 0.1.6: a free-model request carries exactly the headers OpenCode 1.19.0 sends to Zen, and none of the caller's (xiaoxiaofeixz on magpie's Discord, #100, #108) Since 10-13 Zen answers 403 FreeTierError ("OpenCode's free tier can only be used from within OpenCode") to a free-model request that isn't OpenCode's. npm still serves 0.1.1, which gets that 403 on every model; 0.1.5's OpenCode-form session id (9074723) fixed it but never reached npm. The owner allows mimicking OpenCode's client on this route (client mimicry is allowed for every vendor but Claude Code), so this reverses 643ef50's README note. Diffed against the release binary itself: OpenCode 1.19.0 (anomalyco/opencode v1.19.0) run in a sandbox HOME with the opencode provider's baseURL pointed at a local capture server, on chat, responses and messages. It sends accept */*, content-type, user-agent "opencode/1.19.0 ai-sdk/provider-utils/ runtime/bun/1.3.14" (4.0.23 chat, 4.0.40 responses, 4.0.46 messages), x-opencode-project, -session, -request, -client, -session-id, and the key (authorization, or x-api-key with anthropic-version). It sends no x-session-id or x-session-affinity to its own gateway (session/llm/request.ts). 0.1.5 differed in: those two extra headers, accept text/event-stream, the chat UA on every protocol, and every header the caller sent going on (through magpie: the agent's own User-Agent, x-stainless-*, anthropic-beta...). 0.1.6 builds the upstream headers from that list alone: the caller's are read only for the session and project, anthropic-version/-beta kept on messages. The new per-protocol test checks the exact set and fails on 0.1.5 (4 fails); the forced-streaming test now expects no x-session-id/x-session-affinity. Run: - bun test in packages/zen-free: 169 pass, 3 runs; bun scripts/check.mjs zen-free passes. - Real opencode.ai, anonymous key "public": npm's 0.1.1 gets 403 FreeTierError on big-pickle, mimo-v2.6-flash-free and muse-spark-1.3-contributor-free, streamed and not. 0.1.6's tools-smoke.mjs: 200 with a Bash call on all three streamed and on big-pickle not streamed; muse non-streamed 200 without a call; mimo non-streamed timed out at 90s, the same on 0.1.5. - magpie 0.1.1208 in a sandbox HOME (scripts/try.sh zen-free): sign-in with public, then `provider test opencode-zen-free`: chat (big-pickle) and responses (muse-spark) both pass. Not tried: a messages-protocol free model (Zen has none free now), Windows. --- packages/zen-free/README.md | 11 +++--- packages/zen-free/index.test.mjs | 52 +++++++++++++++++++++++++-- packages/zen-free/package.json | 2 +- packages/zen-free/provider.mjs | 61 ++++++++++++++------------------ 4 files changed, 83 insertions(+), 43 deletions(-) diff --git a/packages/zen-free/README.md b/packages/zen-free/README.md index 89b993b..adbe68a 100644 --- a/packages/zen-free/README.md +++ b/packages/zen-free/README.md @@ -3,12 +3,11 @@ OpenCode Zen's free models in magpie and OpenCode. Provider id: `opencode-zen-free`. -> **Note (2026-10-13):** OpenCode's free tier now works only inside -> OpenCode itself. Requests from anywhere else, magpie included, get -> 403 FreeTierError ("OpenCode's free tier can only be used from within -> OpenCode"). OpenCode limits it on purpose, and this plugin doesn't work -> around that. To use Zen's models in magpie, add an OpenCode Zen paid -> key or OpenCode Go as a provider instead. +Since 2026-10-13 Zen's free tier answers 403 FreeTierError ("can only be +used from within OpenCode") to a request that isn't OpenCode's. From +0.1.6 the plugin sends a free-model request with exactly the headers +OpenCode 1.19.0 sends to Zen, and a session id of OpenCode's own form. +Older versions (0.1.1 on npm before it) get the 403: update the plugin. ## Install diff --git a/packages/zen-free/index.test.mjs b/packages/zen-free/index.test.mjs index 4d2e42b..5c094d2 100644 --- a/packages/zen-free/index.test.mjs +++ b/packages/zen-free/index.test.mjs @@ -178,6 +178,53 @@ for (const [input, supplied, expected] of [ }); } +// The headers OpenCode 1.19.0's release binary sends to opencode.ai/zen/v1, +// captured on each protocol: these and no others, whatever the caller sent. +for (const [path, model, utils, key] of [ + ["chat/completions", "chat-free", "4.0.23", ["authorization"]], + ["responses", "responses-free", "4.0.40", ["authorization"]], + ["messages", "messages-free", "4.0.46", ["anthropic-version", "x-api-key"]], +]) { + test(`${path}: sends OpenCode's own headers and none of the caller's`, async (t) => { + const { plugin, state, origin } = await fixture(t); + await plugin.provider.models({ models: {} }, { auth: await publicAuth() }); + const loader = await plugin.auth.loader(publicAuth); + const response = await loader.fetch(`${origin}/v1/${path}`, { + method: "POST", + headers: { + "user-agent": "claude-cli/2.1.0", + "x-stainless-lang": "js", + "x-session-affinity": "header-parity-conversation", + "x-magpie-work-dir": "/tmp/x", + accept: "text/event-stream", + }, + body: JSON.stringify({ model, stream: true }), + }); + await response.text(); + const sent = state.calls.at(-1).headers; + assert.equal(sent["user-agent"], `opencode/1.19.0 ai-sdk/provider-utils/${utils} runtime/bun/1.3.14`); + assert.equal(sent.accept, "*/*"); + const own = Object.keys(sent).filter( + (name) => !["host", "connection", "content-length", "accept-encoding"].includes(name), + ); + assert.deepEqual( + own.sort(), + [ + "accept", + "content-type", + "user-agent", + "x-opencode-client", + "x-opencode-project", + "x-opencode-request", + "x-opencode-session", + "x-opencode-session-id", + ...key, + ].sort(), + ); + assert.equal(sent["x-opencode-session-id"], sent["x-opencode-session"]); + }); +} + test("aborts discovery immediately when the inference caller cancels", async (t) => { const { plugin, state, origin } = await fixture(t); const loader = await plugin.auth.loader(publicAuth); @@ -290,8 +337,9 @@ test("forces upstream streaming, preserves existing tools and returns the SSE bo const sent = state.calls.at(-1); assert.equal(sent.headers.authorization, "Bearer public"); assert.match(sent.headers["x-opencode-session"], /^ses_[0-9a-f]{9}ff[0-9a-f][0-9A-Za-z]{14}$/); - assert.equal(sent.headers["x-session-affinity"], sent.headers["x-opencode-session"]); - assert.equal(sent.headers["x-session-id"], sent.headers["x-opencode-session"]); + // OpenCode sends neither to its own gateway; a caller's never goes on. + assert.equal(sent.headers["x-session-affinity"], undefined); + assert.equal(sent.headers["x-session-id"], undefined); assert.equal(sent.headers["x-opencode-session-id"], sent.headers["x-opencode-session"]); assert.match(sent.headers["x-opencode-request"], /^msg_[0-9a-f]{12}[0-9A-Za-z]{14}$/); assert.equal(sent.body.stream, true); diff --git a/packages/zen-free/package.json b/packages/zen-free/package.json index db5e3ea..f11cf8a 100644 --- a/packages/zen-free/package.json +++ b/packages/zen-free/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zen-free-auth", - "version": "0.1.5", + "version": "0.1.6", "description": "OpenCode Zen free models through the public credential, with native streaming protocols", "type": "module", "main": "./index.mjs", diff --git a/packages/zen-free/provider.mjs b/packages/zen-free/provider.mjs index 2753699..58217d3 100644 --- a/packages/zen-free/provider.mjs +++ b/packages/zen-free/provider.mjs @@ -18,7 +18,12 @@ const PATHS = { chat: "/chat/completions", responses: "/responses", anthropic: " const CORE_TOOLS = ["bash", "edit", "glob", "grep", "read"]; const OPENCODE_VERSION = "1.19.0"; // What OpenCode 1.19.0 sends as its User-Agent, ai-sdk and Bun included. -const USER_AGENT = `opencode/${OPENCODE_VERSION} ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.14`; +// Each protocol's ai-sdk package bundles its own provider-utils, so the +// version differs by protocol (captured from the release binary's requests). +const PROVIDER_UTILS = { chat: "4.0.23", responses: "4.0.40", anthropic: "4.0.46" }; +export function userAgent(protocol) { + return `opencode/${OPENCODE_VERSION} ai-sdk/provider-utils/${PROVIDER_UTILS[protocol]} runtime/bun/1.3.14`; +} // How long a request for a model the last list didn't have waits before it // asks Zen's list again: a model Zen made free since is served at once, // and a model typed in by hand doesn't ask the list on every request. @@ -282,41 +287,29 @@ async function server(_input, options = {}) { const toolPolicy = normalizeTools(body, protocol); if (protocol === "chat") body.stream_options = { ...body.stream_options, include_usage: true }; - const headers = new Headers(request.headers); - for (const name of [ - "authorization", - "x-api-key", - "x-goog-api-key", - "content-length", - "content-encoding", - "host", - "cookie", - ]) { - headers.delete(name); - } - headers.set("content-type", "application/json"); - headers.set("accept", "text/event-stream"); - headers.set("user-agent", USER_AGENT); - headers.set("x-opencode-client", "cli"); - headers.set("x-opencode-request", opencodeID("msg", false)); - if (!headers.has("x-opencode-project")) { - const project = _input?.project; - headers.set( - "x-opencode-project", - project?.vcs === "git" && project.id ? project.id : "global", - ); - } - const session = sessionID(headers, body); - for (const name of [ - "x-session-id", - "x-opencode-session", - "x-opencode-session-id", - "x-session-affinity", - ]) - headers.set(name, session); + // Only what OpenCode 1.19.0 itself sends to its gateway, so a + // caller's own headers (an agent's User-Agent, x-stainless-*, + // x-session-affinity, ...) never mark the request as not + // OpenCode's (packages/opencode/src/session/llm/request.ts). + const given = request.headers; + const session = sessionID(given, body); + const project = _input?.project; + const headers = new Headers({ + "content-type": "application/json", + accept: "*/*", + "user-agent": userAgent(protocol), + "x-opencode-project": + given.get("x-opencode-project") || + (project?.vcs === "git" && project.id ? project.id : "global"), + "x-opencode-session": session, + "x-opencode-request": opencodeID("msg", false), + "x-opencode-client": "cli", + "x-opencode-session-id": session, + }); if (protocol === "anthropic") { headers.set("x-api-key", "public"); - if (!headers.has("anthropic-version")) headers.set("anthropic-version", "2023-06-01"); + headers.set("anthropic-version", given.get("anthropic-version") || "2023-06-01"); + if (given.get("anthropic-beta")) headers.set("anthropic-beta", given.get("anthropic-beta")); } else { headers.set("authorization", "Bearer public"); }