diff --git a/packages/zen-free/README.md b/packages/zen-free/README.md index e5ac5f6..3c442bc 100644 --- a/packages/zen-free/README.md +++ b/packages/zen-free/README.md @@ -104,6 +104,13 @@ go as `conversation_delegate` and `workspace_directory`. So is a name with `exec_program`, or `exec_program_2` when the agent has an `exec_program` of its own. +DSH 0.2.2's `edit` and `read` are refused by name with their own +parameters, and the descriptions of its `edit`, `read` and `skill` are +refused under any name (#632). Those descriptions go reworded with the +same meaning, and `edit` and `read` with DSH's parameters go as +`text_replace` and `file_view`; the `edit` and `read` stand-ins Zen wants +still go beside them. + Zen's free tier also tells OpenCode's own requests by the environment block every OpenCode prompt ends with (``, a `Working directory:` line, ``) in a system message. Another agent's request without one diff --git a/packages/zen-free/index.test.mjs b/packages/zen-free/index.test.mjs index 89c508a..136fc57 100644 --- a/packages/zen-free/index.test.mjs +++ b/packages/zen-free/index.test.mjs @@ -1110,6 +1110,75 @@ for (const { protocol, path, model } of protocols) { }); } +// DSH 0.2.2's edit, read and skill (SuperWheel, plugins#632, 2026-11-16): +// each description is refused under any name, and edit and read are refused +// by name with their own parameters (the same parameters under another name +// are served). They go reworded and renamed; the core stand-ins Zen wants +// still go as edit and read; a call of a renamed tool comes back as DSH's. +const DSH_EDIT = { + name: "edit", + description: "Edit an existing UTF-8 text file by replacing literal text.", + parameters: { type: "object", properties: { file_path: { type: "string" }, old_string: { type: "string" }, new_string: { type: "string" }, replace_all: { type: "boolean" }, sandbox_permissions: { type: "string" }, justification: { type: "string" } } }, +}; +const DSH_READ = { + name: "read", + description: "Read a UTF-8 text file and return line-numbered content.", + parameters: { type: "object", properties: { file_path: { type: "string" }, offset: { type: "integer" }, limit: { type: "integer" } } }, +}; +const DSH_SKILL = { + name: "skill", + description: " Load the full instructions for a skill. Call it before acting on a task that names or clearly matches a skill in the session skill catalog. ", + parameters: { type: "object", properties: { name: { type: "string" } } }, +}; +for (const { protocol, path, model } of protocols) { + test(`${protocol}: DSH's edit, read and skill go reworded, edit and read renamed, and come back as DSH's (#632)`, 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 tools = [DSH_EDIT, DSH_READ, DSH_SKILL].map(({ name, description, parameters }) => { + const tool = { name, description }; + if (protocol === "chat") return { type: "function", function: { ...tool, parameters } }; + if (protocol === "anthropic") return { ...tool, input_schema: parameters }; + return { type: "function", ...tool, parameters }; + }); + const body = { model, stream: false, tools }; + if (protocol === "responses") body.input = [{ role: "user", content: "hi" }]; + else body.messages = [{ role: "user", content: "hi" }]; + const send = () => loader.fetch(`${origin}/v1/${path}`, { method: "POST", body: JSON.stringify(body) }); + for (const [upstream, expected] of [["text_replace", "edit"], ["file_view", "read"], ["skill", "skill"]]) { + state.reply = toolReply(protocol, upstream, true); + const response = await send(); + assert.equal(response.status, 200); + assert.match(await response.text(), new RegExp(`"name"\\s*:\\s*"${expected}"`), upstream); + } + const sent = state.calls.at(-1).body; + const nameOf = (tool) => tool.function?.name ?? tool.name; + const descriptionOf = (tool) => tool.function?.description ?? tool.description; + const sentNames = sent.tools.map(nameOf); + assert.deepEqual(sentNames.slice(0, 3), ["text_replace", "file_view", "skill"]); + // the stand-ins Zen wants for its core tools, beside the renamed ones + assert.ok(sentNames.includes("edit") && sentNames.includes("read"), sentNames.join(",")); + assert.equal(descriptionOf(sent.tools[0]), "Replace an exact piece of text in a UTF-8 file that already exists."); + assert.equal(descriptionOf(sent.tools[1]), "Read a UTF-8 text file. Each line comes back with its number."); + assert.equal(descriptionOf(sent.tools[2]), "Open a skill's whole instructions. Use it first when a task names a skill from the session's skill catalog, or plainly fits one."); + for (const text of sent.tools.map(descriptionOf)) assert.ok(![DSH_EDIT, DSH_READ, DSH_SKILL].some((tool) => text?.trim() === tool.description.trim()), text); + }); +} + +test("a read or edit without DSH's parameters keeps its name (#632)", async (t) => { + const { plugin, state, origin } = await fixture(t); + await plugin.provider.models({ models: {} }, { auth: await publicAuth() }); + const loader = await plugin.auth.loader(publicAuth); + // OpenCode's own read: filePath, offset, limit + const tools = [ + { type: "function", function: { name: "read", description: "Reads a file", parameters: { type: "object", properties: { filePath: { type: "string" }, offset: { type: "number" }, limit: { type: "number" } } } } }, + { type: "function", function: { name: "edit", description: "Edits a file", parameters: { type: "object", properties: { file_path: { type: "string" } } } } }, + ]; + const response = await loader.fetch(`${origin}/v1/chat/completions`, { method: "POST", body: JSON.stringify({ model: "chat-free", stream: false, tools }) }); + await response.text(); + assert.deepEqual(state.calls.at(-1).body.tools.slice(0, 2).map((tool) => tool.function.name), ["read", "edit"]); +}); + // Z.ai's ZCode 3.17.2 declares its dynamic-workflow tools, and Zen refuses // the whole request when two of them travel together: CreateWorkflow beside // GetWorkflowRun, and AmendWorkflow beside FillWorkflowHole. Either name diff --git a/packages/zen-free/package.json b/packages/zen-free/package.json index 4b19760..d7c4fd1 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.27", + "version": "0.1.28", "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 ea3e84f..112edbf 100644 --- a/packages/zen-free/provider.mjs +++ b/packages/zen-free/provider.mjs @@ -236,13 +236,15 @@ function normalizeTools(body, protocol) { 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")); + const nameOf = (tool) => (protocol === "chat" ? tool?.function?.name : tool?.name); + const declared = new Set(body.tools.map(nameOf).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); } + // renamed first, so a core tool the agent offers under a refused form + // (DSH's edit and read) still has its stand-in beside the renamed one + const renamed = renameRefused(body, protocol); + const present = new Set(body.tools.map(nameOf)); for (const name of CORE_TOOLS) { if (present.has(name)) continue; const parameters = { @@ -260,7 +262,7 @@ function normalizeTools(body, protocol) { : { type: "function", ...definition, parameters }, ); } - return { declared, renamed: renameRefused(body, protocol) }; + return { declared, renamed }; } // Tool names Zen's free tier refuses to serve under, whatever else the @@ -311,6 +313,35 @@ const REFUSED_TOGETHER = [ as: { AmendWorkflow: "workflow_amend", FillWorkflowHole: "workflow_answer" }, }, ]; +// A name refused together with its own parameters: the same parameters +// under another name are served, and so is the name with none. DSH's edit +// and read, bisected by SuperWheel against Zen on 2026-11-16 (plugins#632): +// `edit` or `read` with their own parameters and a neutral description +// were refused, `foo` with the same parameters was served. A rule renames +// the tool only when it has every parameter the rule lists. +const REFUSED_WITH_PARAMETERS = [ + { name: "edit", parameters: ["file_path", "old_string", "new_string"], as: "text_replace" }, + { name: "read", parameters: ["file_path", "offset", "limit"], as: "file_view" }, +]; +// Tool descriptions Zen refuses on their own, under any name (the same +// bisection: each of these under a neutral name `foo` was refused, and a +// shorter wording was served). Such a description goes reworded, with the +// same meaning; it is matched whole, after trimming. +const REFUSED_DESCRIPTIONS = new Map([ + [ + "Edit an existing UTF-8 text file by replacing literal text.", + "Replace an exact piece of text in a UTF-8 file that already exists.", + ], + [ + "Read a UTF-8 text file and return line-numbered content.", + "Read a UTF-8 text file. Each line comes back with its number.", + ], + [ + "Load the full instructions for a skill. Call it before acting on a task that names or clearly matches a skill in the session skill catalog.", + "Open a skill's whole instructions. Use it first when a task names a skill from the session's skill catalog, or plainly fits one.", + ], +]); + // 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. @@ -326,9 +357,11 @@ function inCaseOf(found, word) { return word; } -function servedName(name, taken) { +function servedName(name, taken, parameters) { for (const rule of REFUSED_TOGETHER) if (Object.hasOwn(rule.as, name) && rule.names.every((other) => taken.has(other))) return rule.as[name]; + for (const rule of REFUSED_WITH_PARAMETERS) + if (rule.name === name && rule.parameters.every((key) => parameters?.has(key))) return rule.as; let served = name; for (const { word, as } of REFUSED_WORDS) served = served.replace(word, (found) => inCaseOf(found, as)); return served === name ? undefined : served; @@ -342,10 +375,24 @@ function renameRefused(body, protocol) { const taken = new Set( [...body.tools.map(get), ...functions.map((fn) => fn?.name)].filter((name) => typeof name === "string"), ); + // each tool's parameter names, and a refused description reworded + const parametersOf = new Map(); + for (const tool of body.tools) { + const definition = protocol === "chat" ? tool?.function : tool; + if (!definition || typeof definition !== "object") continue; + const schema = protocol === "anthropic" ? definition.input_schema : definition.parameters; + const properties = schema && typeof schema === "object" ? schema.properties : undefined; + if (typeof definition.name === "string" && properties && typeof properties === "object") + parametersOf.set(definition.name, new Set(Object.keys(properties))); + if (typeof definition.description === "string") { + const reworded = REFUSED_DESCRIPTIONS.get(definition.description.trim()); + if (reworded) definition.description = reworded; + } + } const served = new Map(); // agent's name -> served name const renamed = new Map(); // served name -> agent's name for (const name of taken) { - const wanted = servedName(name, taken); + const wanted = servedName(name, taken, parametersOf.get(name)); if (!wanted) continue; let alias = wanted; for (let i = 2; taken.has(alias) || renamed.has(alias); i++) alias = `${wanted}_${i}`;