zen-free 0.1.20: send each protocol where OpenCode 2 does

0.1.19 matched OpenCode 2.2.5's headers and body, but the capture behind
it set OpenCode's baseURL to the recorder, so the path wasn't compared.
OpenCode 2.2.5 reads models.dev's v2 catalog
(models.opencode.ai/experimental/v2.0/api.json; models-dev.ts takes each
model's api.base_url), which gives every opencode model, the 38 free
chat ones included, https://opencode.ai/inference/openai/v1 for
chat-completions and responses and /inference/anthropic/v1 for messages.
The plugin sent all of them to /inference/v1, where no OpenCode 2
request goes. It now sends each to OpenCode's base; a baseURL given in
options (the tests' fixtures) is still asked as given. The model list
stays at /inference/v1/models: OpenCode reads its catalog instead, and
/inference/openai/v1/models is a 404 (checked on mote).

Run:
- bun test in packages/zen-free on mote (temp HOME as CI) and in WSL
  magpie-desktop: 204 pass.
- Broken by hand: upstream back to baseURL: the inference-path test red.
- OpenCode's captured request replayed from mote at
  /inference/openai/v1/chat/completions and at /inference/v1: both 429
  FreeUsageLimitError, as everything from our egress IP is.

Not tried: a 200 (the IP's free limit is spent, see 0.1.19's body), and
a capture of OpenCode at its default URL (TLS to opencode.ai; the path
comes from its source and the catalog instead).
This commit is contained in:
yetone
2026-11-01 13:47:44 +08:00
parent d62193c43c
commit 85019888d7
4 changed files with 49 additions and 8 deletions
+3 -1
View File
@@ -22,7 +22,9 @@ User-Agent (`opencode/latest/2.2.5/cli`, the version kept current from
OpenCode's own updater), its `b3` and `traceparent` trace headers, and
its project id, `sha1("directory:" + folder)` or the repository's remote,
where 0.1.14 to 0.1.18 sent `global`. The model list goes as OpenCode
too, not as Bun.
too, not as Bun. From 0.1.20 each protocol goes where OpenCode 2 sends
it, the base URL models.dev's v2 catalog gives (`/inference/openai/v1`,
`/inference/anthropic/v1`), not `/inference/v1`.
## Install
+32 -5
View File
@@ -740,8 +740,10 @@ test("a Chat stream Zen closes before its [DONE] ends cut short for a streaming
// Zen's free tier answers only at the inference API since mid-October 2026
// (403 FreeTierError at /zen/v1 to OpenCode's own request); a model listed
// before then, whose URL is /zen/v1, is asked there too.
test("asks the inference API, also for a model listed at /zen/v1", async (t) => {
// before then, whose URL is /zen/v1, is asked there too. Each protocol
// goes where OpenCode 2.2.5 sends it, models.dev's v2 base_url
// (/inference/openai/v1, /inference/anthropic/v1), not /inference/v1.
test("asks the inference API where OpenCode 2 does, also for a model listed at /zen/v1", async (t) => {
const { default: plugin } = await subject;
const realFetch = globalThis.fetch;
const asked = [];
@@ -749,8 +751,19 @@ test("asks the inference API, also for a model listed at /zen/v1", async (t) =>
const url = String(input instanceof Request ? input.url : input);
asked.push(url);
if (url.endsWith("/models"))
return Response.json({ data: [{ id: "big-pickle" }] });
if (url.endsWith("/chat/completions"))
return Response.json({ data: [{ id: "big-pickle" }, { id: "m-free" }, { id: "r-free" }] });
if (url === "https://models.opencode.ai/api.json")
return Response.json({
opencode: {
npm: "@ai-sdk/openai-compatible",
models: {
"big-pickle": {},
"m-free": { provider: { npm: "@ai-sdk/anthropic" } },
"r-free": { provider: { npm: "@ai-sdk/openai" } },
},
},
});
if (url.endsWith("/chat/completions") || url.endsWith("/messages") || url.endsWith("/responses"))
return new Response('data: {"choices":[{"index":0,"delta":{"content":"pong"}}]}\n\ndata: [DONE]\n\n', {
headers: { "content-type": "text/event-stream" },
});
@@ -772,9 +785,23 @@ test("asks the inference API, also for a model listed at /zen/v1", async (t) =>
});
assert.equal(response.status, 200);
await response.text();
assert.equal(asked.at(-1), "https://opencode.ai/inference/v1/chat/completions");
assert.equal(asked.at(-1), "https://opencode.ai/inference/openai/v1/chat/completions");
}
for (const [path, model, sent] of [
["messages", "m-free", "https://opencode.ai/inference/anthropic/v1/messages"],
["responses", "r-free", "https://opencode.ai/inference/openai/v1/responses"],
]) {
const response = await loader.fetch(`https://opencode.ai/inference/v1/${path}`, {
method: "POST",
body: JSON.stringify({ model, stream: true, messages: [], input: [] }),
});
await response.text();
assert.equal(asked.at(-1), sent);
}
assert.ok(!asked.some((url) => url.startsWith("https://opencode.ai/zen/")), asked.join("\n"));
assert.ok(!asked.some((url) => /\/inference\/v1\/(chat|messages|responses)/.test(url)), asked.join("\n"));
// the version is asked of OpenCode's own updater, and its 500 leaves 2.2.5
assert.ok(asked.includes("https://opencode.ai/update/api/latest/cli/npm?current=2.2.5"), asked.join("\n"));
});
// Zen's free tier serves a request as OpenCode's only with OpenCode's
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-zen-free-auth",
"version": "0.1.19",
"version": "0.1.20",
"description": "OpenCode Zen free models through the public credential, with native streaming protocols",
"type": "module",
"main": "./index.mjs",
+13 -1
View File
@@ -20,6 +20,17 @@ import { guardToolResponse } from "./tool-response.mjs";
// listed before then still names /zen/v1 (LEGACY_BASE), which is asked here.
const DEFAULT_BASE = "https://opencode.ai/inference/v1";
const LEGACY_BASE = "https://opencode.ai/zen/v1";
// Where OpenCode 2 itself sends each protocol: the base_url models.dev's
// v2 catalog (models.opencode.ai/experimental/v2.0/api.json, which 2.2.5
// reads) gives every opencode model, free ones included, on 2026-11-01.
// 0.1.18 and before sent all of them to /inference/v1, where no OpenCode
// 2 request goes. The model list stays at /inference/v1/models: OpenCode
// reads its catalog instead, and /inference/openai/v1/models is a 404.
const OPENCODE_BASE = {
chat: "https://opencode.ai/inference/openai/v1",
responses: "https://opencode.ai/inference/openai/v1",
anthropic: "https://opencode.ai/inference/anthropic/v1",
};
const CATALOG = "https://models.opencode.ai/api.json";
// OpenCode's table of what each model is asked at; packages/web's zen.mdx,
// read before, was removed on 2026-10-12 (anomalyco/opencode#54761)
@@ -639,7 +650,8 @@ async function server(_input, options = {}) {
headers.set("anthropic-version", given.get("anthropic-version") || "2023-06-01");
if (given.get("anthropic-beta")) headers.set("anthropic-beta", given.get("anthropic-beta"));
}
const response = await fetch(`${baseURL}${PATHS[protocol]}`, {
const upstream = baseURL === DEFAULT_BASE ? OPENCODE_BASE[protocol] : baseURL;
const response = await fetch(`${upstream}${PATHS[protocol]}`, {
method: "POST",
headers,
body: JSON.stringify(body),