diff --git a/packages/zen-free/README.md b/packages/zen-free/README.md index 7c57311..000a890 100644 --- a/packages/zen-free/README.md +++ b/packages/zen-free/README.md @@ -6,8 +6,15 @@ OpenCode Zen's free models in magpie and OpenCode. Provider id: 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. +OpenCode 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. + +Since 2026-10-26 Zen's free tier answers `429 FreeUsageLimitError` +("Free models work in OpenCode") to the User-Agent OpenCode 1.19.0 sent, +which 0.1.6 to 0.1.15 sent on every request. From 0.1.16 the plugin +sends OpenCode 2.0.25's headers (`opencode/latest/2.0.25/cli`, its +session id under `x-session-affinity` and `x-session-id` too): update +the plugin. ## Install diff --git a/packages/zen-free/index.test.mjs b/packages/zen-free/index.test.mjs index 5ff7a9f..1318da4 100644 --- a/packages/zen-free/index.test.mjs +++ b/packages/zen-free/index.test.mjs @@ -177,19 +177,18 @@ for (const [input, supplied, expected] of [ }); await response.text(); assert.equal(state.calls.at(-1).headers["x-opencode-project"], expected); - assert.equal( - state.calls.at(-1).headers["user-agent"], - "opencode/1.19.0 ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.14", - ); + assert.equal(state.calls.at(-1).headers["user-agent"], "opencode/latest/2.0.25/cli"); }); } -// 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"]], +// The headers OpenCode 2.0.25's release binary sends to Zen, captured on +// 2026-10-25: these and no others, whatever the caller sent. Zen's free +// tier refuses (429 FreeUsageLimitError) the User-Agent OpenCode 1.19.0 +// sent, which 0.1.6 to 0.1.15 sent on every request, since 2026-10-26. +for (const [path, model, key] of [ + ["chat/completions", "chat-free", ["authorization"]], + ["responses", "responses-free", ["authorization"]], + ["messages", "messages-free", ["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); @@ -208,7 +207,8 @@ for (const [path, model, utils, key] of [ }); 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["user-agent"], "opencode/latest/2.0.25/cli"); + assert.notEqual(sent["user-agent"], "claude-cli/2.1.0"); assert.equal(sent.accept, "*/*"); const own = Object.keys(sent).filter( (name) => !["host", "connection", "content-length", "accept-encoding"].includes(name), @@ -221,13 +221,18 @@ for (const [path, model, utils, key] of [ "user-agent", "x-opencode-client", "x-opencode-project", - "x-opencode-request", "x-opencode-session", "x-opencode-session-id", + "x-session-affinity", + "x-session-id", ...key, ].sort(), ); assert.equal(sent["x-opencode-session-id"], sent["x-opencode-session"]); + // OpenCode sends its own session id under both names; the caller's + // never goes on. + assert.equal(sent["x-session-affinity"], sent["x-opencode-session"]); + assert.equal(sent["x-session-id"], sent["x-opencode-session"]); }); } @@ -417,11 +422,11 @@ 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}$/); - // 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); + // OpenCode 2.0.25 sends its own session id under both; a caller's + // (session_123) never goes on. + assert.equal(sent.headers["x-session-affinity"], sent.headers["x-opencode-session"]); + assert.equal(sent.headers["x-session-id"], sent.headers["x-opencode-session"]); 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); assert.equal(sent.body.stream_options.include_usage, true); assert.deepEqual(sent.body.tools[0], bash); diff --git a/packages/zen-free/package.json b/packages/zen-free/package.json index 290cf63..b34e9f3 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.15", + "version": "0.1.16", "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 d287350..78ec85a 100644 --- a/packages/zen-free/provider.mjs +++ b/packages/zen-free/provider.mjs @@ -33,13 +33,17 @@ const CORE_PARAMETER = { grep: "pattern", read: "filePath", }; -const OPENCODE_VERSION = "1.19.0"; -// What OpenCode 1.19.0 sends as its User-Agent, ai-sdk and Bun included. -// 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`; +// What OpenCode 2.0.25 sends as its User-Agent on every protocol: +// `opencode/${channel}/${version}/${name}` (its App service, read from the +// release binary and captured from its requests on 2026-10-25). Since +// 2026-10-26 Zen's free tier answers 429 FreeUsageLimitError ("Free models +// work in OpenCode") to the exact User-Agent 0.1.6 to 0.1.15 sent, OpenCode +// 1.19.0's `opencode/1.19.0 ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.14`, +// whatever the body says; the same request under any other string, OpenCode +// 2.0.25's included, is served. +const OPENCODE_VERSION = "2.0.25"; +export function userAgent() { + return `opencode/latest/${OPENCODE_VERSION}/cli`; } // 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, @@ -375,24 +379,24 @@ async function server(_input, options = {}) { addEnvironment(body, protocol); if (protocol === "chat") body.stream_options = { ...body.stream_options, include_usage: true }; - // 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). + // Only what OpenCode 2.0.25 itself sends to its gateway, so a + // caller's own headers (an agent's User-Agent, x-stainless-*, ...) + // never mark the request as not OpenCode's. 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), + "user-agent": userAgent(), "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, + "x-session-affinity": session, + "x-session-id": session, }); if (protocol === "anthropic") { headers.set("x-api-key", "public");