Marks Zen Free's api method keyless and its auth single, so magpie's host (yetone/magpie#2695, 5871f02dd) signs it in with no key field and Sign in again instead of another account. authorize is unchanged; older hosts and OpenCode keep the public-key flow. Run at the PR head 29f0e9b38: bun test in packages/zen-free under a sandbox HOME, 175 pass, 0 fail. Checked against magpie main: host.js reads m.keyless (api method with authorize) and auth.single, the two fields this sets. Co-authored-by: LoKi <317300729+lokic7123-star@users.noreply.github.com>
387 lines
15 KiB
JavaScript
387 lines
15 KiB
JavaScript
import { createHash, randomBytes } from "node:crypto";
|
||
import {
|
||
PROVIDER,
|
||
SDK,
|
||
bootstrapProtocols,
|
||
bootstrapProvider,
|
||
buildModels,
|
||
configModels,
|
||
parseProtocolDocs,
|
||
} from "./models.mjs";
|
||
import { collapseResponse } from "./stream.mjs";
|
||
import { guardToolResponse } from "./tool-response.mjs";
|
||
|
||
// 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";
|
||
// 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)
|
||
const DOCS =
|
||
"https://raw.githubusercontent.com/anomalyco/opencode/dev/services/www/src/docs/content/console/models.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
|
||
// 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`;
|
||
}
|
||
// 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,
|
||
// and a model typed in by hand doesn't ask the list on every request.
|
||
const RELIST_AFTER = 60_000;
|
||
|
||
function httpURL(value) {
|
||
const url = new URL(value);
|
||
if (
|
||
!["http:", "https:"].includes(url.protocol) ||
|
||
url.username ||
|
||
url.password ||
|
||
url.search ||
|
||
url.hash
|
||
) {
|
||
throw new Error("Expected an HTTP(S) URL without credentials, query or fragment");
|
||
}
|
||
return url.href.replace(/\/+$/, "");
|
||
}
|
||
|
||
function requirePublic(auth) {
|
||
if (auth?.type !== "api" || auth.key !== "public") {
|
||
throw new Error(
|
||
"OpenCode Zen Free uses the public credential. Enable its free access login method.",
|
||
);
|
||
}
|
||
}
|
||
|
||
async function readRemote(url, json, headers, callerSignal) {
|
||
const timeout = AbortSignal.timeout(15000);
|
||
const signal = callerSignal ? AbortSignal.any([callerSignal, timeout]) : timeout;
|
||
const response = await fetch(url, { headers, signal });
|
||
if (!response.ok) {
|
||
await response.body?.cancel();
|
||
throw new Error(`Model discovery returned HTTP ${response.status}`);
|
||
}
|
||
return json ? response.json() : response.text();
|
||
}
|
||
|
||
function normalizeTools(body, protocol) {
|
||
if (body.tools !== undefined && !Array.isArray(body.tools)) {
|
||
throw new Error("tools must be an array");
|
||
}
|
||
body.tools ??= [];
|
||
const present = new Set(
|
||
body.tools.map((tool) => (protocol === "chat" ? tool?.function?.name : tool?.name)),
|
||
);
|
||
const declared = new Set([...present].filter((name) => typeof name === "string"));
|
||
if (protocol === "chat" && Array.isArray(body.functions)) {
|
||
for (const tool of body.functions) if (typeof tool?.name === "string") declared.add(tool.name);
|
||
}
|
||
for (const name of CORE_TOOLS) {
|
||
if (present.has(name)) continue;
|
||
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"
|
||
? { type: "function", function: { ...definition, parameters } }
|
||
: protocol === "anthropic"
|
||
? { ...definition, input_schema: parameters }
|
||
: { type: "function", ...definition, parameters },
|
||
);
|
||
}
|
||
return { declared };
|
||
}
|
||
|
||
const ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||
let lastStamp = 0;
|
||
let counter = 0;
|
||
|
||
// An id the way OpenCode makes one (packages/schema/src/identifier.ts):
|
||
// 12 hex of Date.now() * 0x1000 + a per-millisecond counter (bit-inverted
|
||
// for a session, so newer sorts first), then 14 random base62 characters.
|
||
// Zen's free tier checks a session id has this shape, a small counter
|
||
// included, and answers 403 FreeTierError to one that doesn't (#100).
|
||
export function opencodeID(prefix, descending, now = Date.now()) {
|
||
if (now !== lastStamp) {
|
||
lastStamp = now;
|
||
counter = 0;
|
||
}
|
||
counter++;
|
||
const current = BigInt(now) * 0x1000n + BigInt(counter);
|
||
const value = (descending ? ~current : current) & 0xffffffffffffn;
|
||
const random = randomBytes(14);
|
||
let tail = "";
|
||
for (const byte of random) tail += ALPHABET[byte % 62];
|
||
return `${prefix}_${value.toString(16).padStart(12, "0")}${tail}`;
|
||
}
|
||
|
||
// A session id OpenCode itself could have made: its counter (the low 12
|
||
// bits, inverted) is small. A hash cut to the same shape isn't one.
|
||
export function realSessionID(value) {
|
||
const match = /^ses_[0-9a-f]{9}([0-9a-f]{3})[0-9A-Za-z]{14}$/.exec(value ?? "");
|
||
return !!match && 0xfff - Number.parseInt(match[1], 16) < 16;
|
||
}
|
||
|
||
// One made-up session per conversation, so its turns keep one session.
|
||
const SESSIONS = new Map();
|
||
const KEEP_SESSIONS = 1000;
|
||
|
||
export function sessionID(headers, body) {
|
||
let signal;
|
||
for (const name of [
|
||
"x-opencode-session",
|
||
"x-opencode-session-id",
|
||
"x-session-affinity",
|
||
"x-session-id",
|
||
"conversation-id",
|
||
]) {
|
||
if (headers.get(name)) {
|
||
signal = headers.get(name);
|
||
break;
|
||
}
|
||
}
|
||
if (!signal && typeof body.metadata?.session_id === "string") signal = body.metadata.session_id;
|
||
if (realSessionID(signal)) return signal;
|
||
if (!signal) {
|
||
const messages = body.messages ?? body.input;
|
||
const first = Array.isArray(messages)
|
||
? messages.find((message) => message.role === "user")?.content
|
||
: messages;
|
||
if (!first) return opencodeID("ses", true);
|
||
signal = JSON.stringify(first);
|
||
}
|
||
const key = createHash("sha256").update(`ses\0${signal}`).digest("hex");
|
||
let session = SESSIONS.get(key);
|
||
if (session) {
|
||
SESSIONS.delete(key);
|
||
} else {
|
||
session = opencodeID("ses", true);
|
||
if (SESSIONS.size >= KEEP_SESSIONS) SESSIONS.delete(SESSIONS.keys().next().value);
|
||
}
|
||
SESSIONS.set(key, session);
|
||
return session;
|
||
}
|
||
|
||
async function server(_input, options = {}) {
|
||
const baseURL = httpURL(options.baseURL ?? DEFAULT_BASE);
|
||
const catalogURL = httpURL(options.catalogURL ?? CATALOG);
|
||
const docsURL = httpURL(options.docsURL ?? DOCS);
|
||
let known = buildModels(
|
||
Object.keys(bootstrapProvider.models),
|
||
bootstrapProvider,
|
||
bootstrapProtocols,
|
||
baseURL,
|
||
);
|
||
let metadata = bootstrapProvider;
|
||
let protocols = { ...bootstrapProtocols };
|
||
let listed = false;
|
||
let listedAt = 0;
|
||
|
||
async function discover(signal) {
|
||
const headers = { authorization: "Bearer public", "x-opencode-client": "cli" };
|
||
const [listing, catalog, docs] = await Promise.allSettled([
|
||
readRemote(`${baseURL}/models`, true, headers, signal),
|
||
readRemote(catalogURL, true, undefined, signal),
|
||
readRemote(docsURL, false, undefined, signal),
|
||
]);
|
||
signal?.throwIfAborted();
|
||
let partial = false;
|
||
if (catalog.status === "fulfilled" && catalog.value?.opencode?.models) {
|
||
metadata = catalog.value.opencode;
|
||
} else {
|
||
partial = true;
|
||
}
|
||
if (docs.status === "fulfilled" && Object.keys(parseProtocolDocs(docs.value)).length) {
|
||
protocols = { ...bootstrapProtocols, ...parseProtocolDocs(docs.value) };
|
||
} else {
|
||
partial = true;
|
||
}
|
||
if (listing.status !== "fulfilled" || !Array.isArray(listing.value?.data)) {
|
||
const fallback = { ...known };
|
||
fallback[Symbol.for("magpie.fellBack")] = true;
|
||
return fallback;
|
||
}
|
||
const ids = listing.value.data.map((item) => item.id);
|
||
const discovered = buildModels(ids, metadata, protocols, baseURL);
|
||
if (partial) {
|
||
// Preserve previously verified models only while they remain in the live catalog.
|
||
for (const id of ids) {
|
||
if (!discovered[id] && !metadata.models?.[id] && !protocols[id] && known[id]) {
|
||
discovered[id] = known[id];
|
||
}
|
||
}
|
||
// Not said as fallen back: Zen's own list was read, and what it
|
||
// lists is what is offered. Said so, magpie kept the last list read
|
||
// in full instead, and a user who can't reach the catalog or the
|
||
// docs (GitHub's raw host, from mainland China) never saw a model
|
||
// Zen made free since (yetone/magpie#2385).
|
||
}
|
||
known = discovered;
|
||
listed = true;
|
||
listedAt = Date.now();
|
||
return discovered;
|
||
}
|
||
|
||
return {
|
||
async config(cfg) {
|
||
cfg.provider ??= {};
|
||
cfg.provider[PROVIDER] ??= {
|
||
name: "OpenCode Zen Free",
|
||
npm: SDK.chat,
|
||
api: baseURL,
|
||
models: configModels(known),
|
||
};
|
||
},
|
||
auth: {
|
||
provider: PROVIDER,
|
||
single: true,
|
||
icon: "https://opencode.ai/favicon-v3.ico",
|
||
maxConcurrency: 2,
|
||
methods: [
|
||
{
|
||
type: "api",
|
||
keyless: true,
|
||
label: "Zen 免费访问(密钥填 public;自己的 OpenCode Key 请加到内置的 OpenCode Zen)",
|
||
placeholder: "public",
|
||
async authorize() {
|
||
return { type: "success", key: "public" };
|
||
},
|
||
},
|
||
],
|
||
async loader(getAuth) {
|
||
const auth = await getAuth();
|
||
if (!auth) return {};
|
||
requirePublic(auth);
|
||
return {
|
||
baseURL,
|
||
apiKey: "public",
|
||
async fetch(input, init) {
|
||
requirePublic(await getAuth());
|
||
const request = new Request(input, init);
|
||
const protocol = Object.keys(PATHS).find(
|
||
(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");
|
||
request.signal.throwIfAborted();
|
||
const body = await request.json();
|
||
if (!body || typeof body !== "object" || Array.isArray(body))
|
||
throw new Error("Expected a JSON request object");
|
||
if (
|
||
!Object.hasOwn(known, body.model) &&
|
||
(!listed || Date.now() - listedAt >= RELIST_AFTER)
|
||
) {
|
||
await discover(request.signal);
|
||
}
|
||
const model = Object.hasOwn(known, body.model) ? known[body.model] : undefined;
|
||
if (!model) {
|
||
// Zen no longer gives it free, or never did: said as a model
|
||
// not served, so the caller sets this model aside and not the
|
||
// account, whose other free models are still served.
|
||
const free = Object.keys(known).sort().join(", ");
|
||
const name = typeof body.model === "string" ? body.model : "";
|
||
return new Response(
|
||
JSON.stringify({
|
||
type: "error",
|
||
error: {
|
||
type: "not_found_error",
|
||
message: `Model ${name} is not available as an OpenCode Zen free model now. Free models: ${free || "none"}.`,
|
||
},
|
||
}),
|
||
{ status: 404, headers: { "content-type": "application/json" } },
|
||
);
|
||
}
|
||
if (model.api.npm !== SDK[protocol]) {
|
||
throw new Error(
|
||
"Model is not an available free model for this protocol. Refresh the model list.",
|
||
);
|
||
}
|
||
const streaming = body.stream === true;
|
||
body.stream = true;
|
||
const toolPolicy = normalizeTools(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).
|
||
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),
|
||
"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,
|
||
});
|
||
if (protocol === "anthropic") {
|
||
headers.set("x-api-key", "public");
|
||
headers.set("anthropic-version", given.get("anthropic-version") || "2023-06-01");
|
||
if (given.get("anthropic-beta")) headers.set("anthropic-beta", given.get("anthropic-beta"));
|
||
} else {
|
||
headers.set("authorization", "Bearer public");
|
||
}
|
||
const response = await fetch(`${baseURL}${PATHS[protocol]}`, {
|
||
method: "POST",
|
||
headers,
|
||
body: JSON.stringify(body),
|
||
signal: request.signal,
|
||
redirect: "error",
|
||
});
|
||
const guarded = await guardToolResponse(response, protocol, toolPolicy, {
|
||
endCutShort: streaming,
|
||
});
|
||
return streaming ? guarded : collapseResponse(guarded, protocol);
|
||
},
|
||
};
|
||
},
|
||
async usage() {
|
||
return { plan: "OpenCode Zen Free", windows: [] };
|
||
},
|
||
},
|
||
provider: {
|
||
id: PROVIDER,
|
||
async models(provider, { auth } = {}) {
|
||
if (!auth) return provider.models;
|
||
requirePublic(auth);
|
||
return discover();
|
||
},
|
||
},
|
||
async "chat.headers"(input, output) {
|
||
if (![PROVIDER, `${PROVIDER}-plugin`].includes(input.model?.providerID) || !input.sessionID)
|
||
return;
|
||
output.headers["x-session-id"] = input.sessionID;
|
||
output.headers["x-opencode-session"] = input.sessionID;
|
||
output.headers["x-session-affinity"] = input.sessionID;
|
||
},
|
||
};
|
||
}
|
||
|
||
export default { id: PROVIDER, server };
|