zen-free 0.1.16: requests go with OpenCode 2.0.25's User-Agent, since Zen refuses the 1.19.0 one 0.1.15 sent
杨梅 (yangmei_61422) on magpie's Discord, 10-26: Zen Free from Kimi
Code/Work fails again ("反代失败") a day after 0.1.15.
Cause, found against opencode.ai from this machine on 2026-10-26: Zen's
free tier now answers 429 FreeUsageLimitError ("Free usage limit
reached. 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. One request (short system prompt + the <env>
block + the five core tools, our other headers), only the UA changed:
that exact string 429; with provider-utils 4.0.24, bun 1.3.15, opencode
1.19.1, the ai-sdk or runtime part dropped, or an "x " prefix: 200;
"opencode/latest/2.0.25/cli" (what OpenCode 2.0.25 sends,
`opencode/${channel}/${version}/${name}` in its binary): 200. So it is
that one string, the plugin's fingerprint, not a version floor. The env
block is still needed: 2.0.25's UA without it is 429. Path
(/inference/v1), project "global", x-opencode-request and the
x-session-* pair made no difference either way.
Fix: userAgent() is OpenCode 2.0.25's on every protocol, and the header
set is the one captured from 2.0.25 on 10-25: its session id also under
x-session-affinity and x-session-id, no x-opencode-request. A caller's
own session headers still never go on.
Run:
- Real, through the plugin's loader.fetch: a Kimi Code request (its
system.md rendered, Shell/ReadFile/Glob/Grep/WriteFile/StrReplaceFile/
SetTodoList with Kimi's own descriptions, UA KimiCLI/1.0) and a plain
one. At origin/main (0.1.15): deepseek-v4.1-flash-free 429
FreeUsageLimitError for both. With this change: deepseek-v4.1-flash-
free, glm-5.3-flash-free and big-pickle 200 for both.
- tools-smoke.mjs, real: big-pickle and mimo-v2.6-flash-free, stream and
not, 200 with a Bash call.
- Header tests (project identity x4, per-protocol header set x3, the
streaming one): red on origin/main's provider.mjs (8 of 199 fail),
green with it.
- HOME=$(mktemp -d) bun test at the repo root: 2675 pass, 0 fail;
bun scripts/check.mjs: ok.
Not tried: Kimi Code or Kimi Work themselves through magpie (the request
was built from Kimi CLI's installed prompt and tool files); a Responses
or Anthropic free model (Zen lists none free now).
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user