diff --git a/packages/zen-free/README.md b/packages/zen-free/README.md index d6fc296..a557d89 100644 --- a/packages/zen-free/README.md +++ b/packages/zen-free/README.md @@ -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 diff --git a/packages/zen-free/index.test.mjs b/packages/zen-free/index.test.mjs index d878802..692bd17 100644 --- a/packages/zen-free/index.test.mjs +++ b/packages/zen-free/index.test.mjs @@ -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 diff --git a/packages/zen-free/package.json b/packages/zen-free/package.json index 5836ce1..a7646ad 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.19", + "version": "0.1.20", "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 f8c8844..1a420b1 100644 --- a/packages/zen-free/provider.mjs +++ b/packages/zen-free/provider.mjs @@ -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),