diff --git a/packages/zen-free/README.md b/packages/zen-free/README.md index fb1ff7a..e5ac5f6 100644 --- a/packages/zen-free/README.md +++ b/packages/zen-free/README.md @@ -99,7 +99,10 @@ offered beside `ScheduleWakeup`, as Claude Code desktop offers them agent's past calls and `tool_choice` too, and a call of one reaches the agent under its own name. DSH's `subagent_fork`, and its `read_image` offered beside `working_directory`, are refused the same way (#632); they -go as `conversation_delegate` and `workspace_directory`. +go as `conversation_delegate` and `workspace_directory`. So is a name with +`run_code` in it, in any case (DSH's code tool, #704); it goes as +`exec_program`, or `exec_program_2` when the agent has an `exec_program` +of its own. Zen's free tier also tells OpenCode's own requests by the environment block every OpenCode prompt ends with (``, a `Working directory:` diff --git a/packages/zen-free/index.test.mjs b/packages/zen-free/index.test.mjs index d5edb7d..89c508a 100644 --- a/packages/zen-free/index.test.mjs +++ b/packages/zen-free/index.test.mjs @@ -1215,3 +1215,114 @@ test("read_image or working_directory alone goes as named (#632)", async (t) => assert.deepEqual(state.calls.at(-1).body.tools.slice(0, tools.length), tools, name); } }); + +// DSH's ptc tool is named run_code and is the only tool its main request +// offers, and Zen's free tier answers 429 FreeUsageLimitError to any tool +// name with "run_code" in it, in any case (alonewolfsy-web, plugins#704). +// It goes as exec_program, in the agent's past calls and tool_choice too, +// streamed or not, and a call of it comes back as run_code. An agent's own +// exec_program keeps its name, and DSH's goes as exec_program_2. +for (const { protocol, path, model } of protocols) { + for (const stream of [true, false]) { + test(`${protocol}: run_code goes as exec_program and comes back as run_code, client stream=${stream} (#704)`, async (t) => { + const { plugin, state, origin } = await fixture(t); + await plugin.provider.models({ models: {} }, { auth: await publicAuth() }); + const loader = await plugin.auth.loader(publicAuth); + const schema = { type: "object", properties: { code: { type: "string" } } }; + const define = (name) => { + const tool = { name, description: `DSH ${name}` }; + if (protocol === "chat") return { type: "function", function: { ...tool, parameters: schema } }; + if (protocol === "anthropic") return { ...tool, input_schema: schema }; + return { type: "function", ...tool, parameters: schema }; + }; + const nameOf = (tool) => (protocol === "chat" ? tool.function.name : tool.name); + const past = (sent) => + protocol === "chat" + ? sent.messages.find((message) => message.tool_calls).tool_calls[0].function.name + : protocol === "anthropic" + ? sent.messages[1].content[0].name + : sent.input[1].name; + const request = (names) => { + const body = { model, stream, tools: names.map(define) }; + if (protocol === "chat") { + body.messages = [ + { role: "user", content: "hi" }, + { role: "assistant", content: null, tool_calls: [{ id: "c0", type: "function", function: { name: "run_code", arguments: "{}" } }] }, + { role: "tool", tool_call_id: "c0", content: "ok" }, + ]; + body.tool_choice = { type: "function", function: { name: "run_code" } }; + } else if (protocol === "anthropic") { + body.messages = [ + { role: "user", content: "hi" }, + { role: "assistant", content: [{ type: "tool_use", id: "c0", name: "run_code", input: {} }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "c0", content: "ok" }] }, + ]; + body.tool_choice = { type: "tool", name: "run_code" }; + } else { + body.input = [ + { role: "user", content: "hi" }, + { type: "function_call", call_id: "c0", name: "run_code", arguments: "{}" }, + { type: "function_call_output", call_id: "c0", output: "ok" }, + ]; + body.tool_choice = { type: "function", name: "run_code" }; + } + return (upstream) => { + state.reply = toolReply(protocol, upstream, true); + return loader.fetch(`${origin}/v1/${path}`, { method: "POST", body: JSON.stringify(body) }); + }; + }; + const reply = async (response) => { + assert.equal(response.status, 200); + return response.text(); + }; + + // DSH's request: run_code alone + let send = request(["run_code"]); + let text = await reply(await send("exec_program")); + assert.match(text, /"name"\s*:\s*"run_code"/); + assert.doesNotMatch(text, /exec_program/); + let sent = state.calls.at(-1).body; + assert.equal(nameOf(sent.tools[0]), "exec_program"); + assert.equal(sent.tools[0].function?.description ?? sent.tools[0].description, "DSH run_code"); + assert.equal(past(sent), "exec_program"); + assert.equal(sent.tool_choice.function?.name ?? sent.tool_choice.name, "exec_program"); + // only names are read; the description goes as written + assert.ok(!sent.tools.some((tool) => /run_code/i.test(nameOf(tool)))); + + // every case of the word, wherever it stands; runCode and code_run + // were served and keep their names + const names = ["RUN_CODE", "Run_Code", "host_run_code", "run_code_v2", "runCode", "code_run"]; + send = request(names); + await reply(await send("Bash")); + sent = state.calls.at(-1).body; + assert.deepEqual(sent.tools.slice(0, names.length).map(nameOf), [ + "EXEC_PROGRAM", + "Exec_Program", + "host_exec_program", + "exec_program_v2", + "runCode", + "code_run", + ]); + for (const [upstream, expected] of [ + ["EXEC_PROGRAM", "RUN_CODE"], + ["host_exec_program", "host_run_code"], + ["runCode", "runCode"], + ]) { + text = await reply(await send(upstream)); + assert.match(text, new RegExp(`"name"\\s*:\\s*"${expected}"`), upstream); + } + + // an agent's own exec_program keeps its name and its calls + send = request(["exec_program", "run_code"]); + text = await reply(await send("exec_program_2")); + assert.match(text, /"name"\s*:\s*"run_code"/); + sent = state.calls.at(-1).body; + assert.deepEqual(sent.tools.slice(0, 2).map(nameOf), ["exec_program", "exec_program_2"]); + assert.equal(past(sent), "exec_program_2"); + assert.equal(sent.tool_choice.function?.name ?? sent.tool_choice.name, "exec_program_2"); + text = await reply(await send("exec_program")); + assert.match(text, /"name"\s*:\s*"exec_program"/); + assert.doesNotMatch(text, /"name"\s*:\s*"run_code"/); + }); + } +} diff --git a/packages/zen-free/package.json b/packages/zen-free/package.json index c2f13df..f981398 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.25", + "version": "0.1.26", "description": "OpenCode Zen free models through the public credential, with native streaming protocols", "type": "module", "main": "./opencode.mjs", diff --git a/packages/zen-free/provider.mjs b/packages/zen-free/provider.mjs index eaed553..ea3e84f 100644 --- a/packages/zen-free/provider.mjs +++ b/packages/zen-free/provider.mjs @@ -284,6 +284,11 @@ function normalizeTools(body, protocol) { // AmendWorkflow beside FillWorkflowHole (either alone was served): a // real 55-tool ZCode request bisected on 2026-11-04, served whole with // only those four names changed (plugins#693). +// - a name with "run_code" in it, in any case, the underscore needed +// (DSH's ptc tool, the only one its main request offers; run_code_v2, +// host_run_code and RUN_CODE were refused too, while exec_program, +// runCode, run-code and code_run were served): alonewolfsy-web, +// 2026-11-04, plugins#704. // // Such a tool goes to Zen under another name (the ones those bisections // were served under), in the agent's past calls and tool_choice too, and a @@ -306,15 +311,27 @@ const REFUSED_TOGETHER = [ as: { AmendWorkflow: "workflow_amend", FillWorkflowHole: "workflow_answer" }, }, ]; -const REFUSED_WORD = /terminal/i; +// A word refused wherever it stands in a name, in any case, goes as +// another word, in the found word's case: all capitals, each part +// capitalised, or lower case. +const REFUSED_WORDS = [ + { word: /terminal/gi, as: "console" }, + { word: /run_code/gi, as: "exec_program" }, +]; + +function inCaseOf(found, word) { + if (found === found.toUpperCase()) return word.toUpperCase(); + if (found[0] === found[0].toUpperCase()) + return word.split("_").map((part) => part[0].toUpperCase() + part.slice(1)).join("_"); + return word; +} function servedName(name, taken) { for (const rule of REFUSED_TOGETHER) if (Object.hasOwn(rule.as, name) && rule.names.every((other) => taken.has(other))) return rule.as[name]; - if (!REFUSED_WORD.test(name)) return undefined; - return name.replace(new RegExp(REFUSED_WORD.source, "gi"), (word) => - word === word.toUpperCase() ? "CONSOLE" : word[0] === "T" ? "Console" : "console", - ); + let served = name; + for (const { word, as } of REFUSED_WORDS) served = served.replace(word, (found) => inCaseOf(found, as)); + return served === name ? undefined : served; } // renameRefused gives each refused tool its served name and answers the