diff --git a/packages/zen-free/README.md b/packages/zen-free/README.md index fca3fa5..7a383c8 100644 --- a/packages/zen-free/README.md +++ b/packages/zen-free/README.md @@ -31,6 +31,9 @@ Requests go to `https://opencode.ai/zen/v1` using each model's native Chat Completions, Responses or Anthropic Messages API. Streaming and ordinary JSON replies are supported. -The agent's tools are preserved. If Zen calls an added tool, its name is -mapped to the agent's unique case-insensitive match (`bash` to `Bash`). -A call with no matching tool returns an error. Tools run in the agent. +The agent's tools are preserved. Zen's models often call OpenCode's own +tool names; such a call is given the agent's tool whose name differs only +in case (`bash` to `Bash`), or else only in case and `_`/`-` (`todowrite` +to `todo_write`, `webfetch` to `web_fetch`), when exactly one does. A +call with no such tool returns an error that names it. Tools run in the +agent. diff --git a/packages/zen-free/package.json b/packages/zen-free/package.json index 487d180..6c5079e 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.0", + "version": "0.1.1", "description": "OpenCode Zen free models through the public credential, with native streaming protocols", "type": "module", "main": "./index.mjs", diff --git a/packages/zen-free/tool-response.mjs b/packages/zen-free/tool-response.mjs index 1a8c37d..77e03a1 100644 --- a/packages/zen-free/tool-response.mjs +++ b/packages/zen-free/tool-response.mjs @@ -30,20 +30,35 @@ function list(value) { return value; } -function nameGuard(policy) { - const declared = new Set(policy.declared); - const injected = new Set(policy.injected); +// OpenCode's own models call OpenCode's tool names (bash, todowrite, +// webfetch) whatever the agent declared. A call is given the agent's tool +// whose name differs only in case, or else only in case and separators +// (todowrite → todo_write, webfetch → web_fetch), when exactly one does; a +// name two of the agent's tools fold to is never guessed between. +const separators = /[\s_.-]/g; +function foldsTo(declared, key) { const folded = new Map(); for (const name of declared) { - const key = name.toLowerCase(); - folded.set(key, folded.has(key) ? null : name); + const k = key(name); + folded.set(k, folded.has(k) ? null : name); } + return folded; +} + +function nameGuard(policy) { + const declared = new Set(policy.declared); + const lower = (name) => name.toLowerCase(); + const bare = (name) => name.toLowerCase().replace(separators, ""); + const byCase = foldsTo(declared, lower); + const byShape = foldsTo(declared, bare); return (name) => { if (typeof name !== "string" || !name) throw new GuardError("Missing upstream tool name"); if (declared.has(name)) return name; - const match = injected.has(name) && folded.get(name.toLowerCase()); + const match = byCase.get(lower(name)) ?? (byCase.has(lower(name)) ? null : byShape.get(bare(name))); if (match) return match; - throw new GuardError("Upstream called a tool not declared by the client"); + throw new GuardError( + `Upstream called a tool not declared by the client: ${JSON.stringify(name.slice(0, 128))}`, + ); }; } diff --git a/packages/zen-free/tool-response.test.mjs b/packages/zen-free/tool-response.test.mjs index 98e9457..49b8712 100644 --- a/packages/zen-free/tool-response.test.mjs +++ b/packages/zen-free/tool-response.test.mjs @@ -195,8 +195,8 @@ for (const protocol of protocols) { for (const [name, rules] of [ ["unknown", policy], ["shell", policy], - ["BASH", policy], ["bash", { declared: new Set(["Bash", "BASH"]), injected: new Set(["bash"]) }], + ["todowrite", { declared: new Set(["todo_write", "todo-write"]), injected: new Set() }], ["bash", { declared: new Set(), injected: new Set(["bash"]) }], ]) { const response = await guardToolResponse( @@ -211,6 +211,55 @@ for (const protocol of protocols) { } }); + // #76: DSH declares all five core tools (so nothing is injected) and + // todo_write; step-5-preview-free opens with OpenCode's todowrite. + test(`${protocol}: an OpenCode tool name is given the agent's own spelling of it (#76)`, async () => { + const rules = { + declared: new Set(["bash", "read", "edit", "glob", "grep", "write", "todo_write", "web_fetch", "TodoRead"]), + injected: new Set(), + }; + for (const [asked, given] of [ + ["todowrite", "todo_write"], + ["webfetch", "web_fetch"], + ["todoread", "TodoRead"], + ["BASH", "bash"], + ]) { + const json = await guardToolResponse( + Response.json(snapshot(protocol, [asked])), + protocol, + rules, + ); + assert.equal(json.status, 200); + assert.deepEqual(namesIn(protocol, await json.json()), [given]); + const streamed = await guardToolResponse( + sse(event(streamSnapshot(protocol, [asked])) + terminal(protocol)), + protocol, + rules, + ); + const frames = parse(await streamed.text()).filter( + (frame) => frame.data && frame.data !== "[DONE]", + ); + assert.deepEqual(namesIn(protocol, frames[0].data), [given]); + } + }); + + test(`${protocol}: a refused call names the tool (#76)`, async () => { + const json = await guardToolResponse( + Response.json(snapshot(protocol, ["subagent"])), + protocol, + policy, + ); + const body = await json.json(); + assert.match(body.message ?? body.error?.message, /: "subagent"$/); + const streamed = await guardToolResponse( + sse(event(streamSnapshot(protocol, ["subagent"])) + terminal(protocol)), + protocol, + policy, + ); + const frame = parse(await streamed.text())[0].data; + assert.match(frame.message ?? frame.error?.message, /: "subagent"$/); + }); + test(`${protocol}: exact matches win over ambiguous casing`, async () => { const rules = { declared: new Set(["bash", "Bash"]), injected: new Set(["bash"]) }; const result = await guardToolResponse( @@ -245,7 +294,7 @@ for (const protocol of protocols) { }); test(`${protocol}: bad mixed SSE snapshot never leaks a tool and cancels immediately`, async () => { - for (const name of ["unknown", "shell", "BASH"]) { + for (const name of ["unknown", "shell", "todowrite"]) { let cancelled = false; const source = openSource( event(streamSnapshot(protocol, ["Read", name])) + event({ sentinel: "must not escape" }),