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"); }