diff --git a/packages/zen-free/README.md b/packages/zen-free/README.md index adbe68a..8146693 100644 --- a/packages/zen-free/README.md +++ b/packages/zen-free/README.md @@ -39,7 +39,7 @@ remaining-quota data. ## Requests -Requests go to `https://opencode.ai/zen/v1` using each model's native +Requests go to `https://opencode.ai/inference/v1` using each model's native Chat Completions, Responses or Anthropic Messages API. Streaming and ordinary JSON replies are supported. diff --git a/packages/zen-free/index.test.mjs b/packages/zen-free/index.test.mjs index 5c094d2..634d23c 100644 --- a/packages/zen-free/index.test.mjs +++ b/packages/zen-free/index.test.mjs @@ -119,7 +119,8 @@ for (const { protocol, path, model } of protocols) { const tool = protocol === "chat" ? raw.function : raw; assert.match(tool.description, /Not available\. Never call this tool\./); assert.equal((tool.parameters ?? tool.input_schema).additionalProperties, false); - assert.deepEqual((tool.parameters ?? tool.input_schema).properties, {}); + // Zen's free tier refuses a bash or read that takes nothing + assert.equal(Object.keys((tool.parameters ?? tool.input_schema).properties).length, 1); } // A call of a tool the agent doesn't have is left out, said in // the reply's text, and the turn ends as a stop rather than @@ -518,3 +519,42 @@ test("a Chat stream Zen closes before its [DONE] ends cut short for a streaming assert.notEqual(whole.status, 200); assert.match(await whole.text(), /without a completion marker \(reply x2, 3 events/); }); + +// 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) => { + const { default: plugin } = await subject; + const realFetch = globalThis.fetch; + const asked = []; + globalThis.fetch = async (input, init) => { + 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 new Response('data: {"choices":[{"index":0,"delta":{"content":"pong"}}]}\n\ndata: [DONE]\n\n', { + headers: { "content-type": "text/event-stream" }, + }); + return new Response("", { status: 500 }); + }; + t.after(() => { + globalThis.fetch = realFetch; + }); + const hooks = await plugin.server({ project: { id: "x" } }); + const loader = await hooks.auth.loader(async () => ({ type: "api", key: "public" })); + assert.equal(loader.baseURL, "https://opencode.ai/inference/v1"); + for (const url of [ + "https://opencode.ai/zen/v1/chat/completions", + "https://opencode.ai/inference/v1/chat/completions", + ]) { + const response = await loader.fetch(url, { + method: "POST", + body: JSON.stringify({ model: "big-pickle", stream: true, messages: [] }), + }); + assert.equal(response.status, 200); + await response.text(); + assert.equal(asked.at(-1), "https://opencode.ai/inference/v1/chat/completions"); + } + assert.ok(!asked.some((url) => url.startsWith("https://opencode.ai/zen/")), asked.join("\n")); +}); diff --git a/packages/zen-free/package.json b/packages/zen-free/package.json index f11cf8a..1f57a21 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.6", + "version": "0.1.7", "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 58217d3..e3bf5c6 100644 --- a/packages/zen-free/provider.mjs +++ b/packages/zen-free/provider.mjs @@ -10,12 +10,26 @@ import { import { collapseResponse } from "./stream.mjs"; import { guardToolResponse } from "./tool-response.mjs"; -const DEFAULT_BASE = "https://opencode.ai/zen/v1"; +// Zen's free models answer only at the inference API since mid-October +// 2026: the same request OpenCode 1.19.0 sends answers 403 FreeTierError +// at /zen/v1 and 200 here, and models.dev gives OpenCode this URL. A model +// 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"; const CATALOG = "https://models.opencode.ai/api.json"; const DOCS = "https://raw.githubusercontent.com/anomalyco/opencode/dev/packages/web/src/content/docs/zen.mdx"; const PATHS = { chat: "/chat/completions", responses: "/responses", anthropic: "/messages" }; const CORE_TOOLS = ["bash", "edit", "glob", "grep", "read"]; +// The parameter each stand-in takes, OpenCode's own first one: Zen's free +// tier answers 403 FreeTierError to bash or read offered with none. +const CORE_PARAMETER = { + bash: "command", + edit: "filePath", + glob: "pattern", + 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 @@ -76,7 +90,11 @@ function normalizeTools(body, protocol) { } for (const name of CORE_TOOLS) { if (present.has(name)) continue; - const parameters = { type: "object", properties: {}, additionalProperties: false }; + const parameters = { + type: "object", + properties: { [CORE_PARAMETER[name]]: { type: "string" } }, + additionalProperties: false, + }; const definition = { name, description: "Not available. Never call this tool." }; body.tools.push( protocol === "chat" @@ -245,7 +263,9 @@ async function server(_input, options = {}) { requirePublic(await getAuth()); const request = new Request(input, init); const protocol = Object.keys(PATHS).find( - (key) => request.url === `${baseURL}${PATHS[key]}`, + (key) => + request.url === `${baseURL}${PATHS[key]}` || + (baseURL === DEFAULT_BASE && request.url === `${LEGACY_BASE}${PATHS[key]}`), ); if (!protocol || request.method !== "POST") throw new Error("Unsupported Zen endpoint or method"); @@ -313,7 +333,7 @@ async function server(_input, options = {}) { } else { headers.set("authorization", "Bearer public"); } - const response = await fetch(request.url, { + const response = await fetch(`${baseURL}${PATHS[protocol]}`, { method: "POST", headers, body: JSON.stringify(body),