diff --git a/packages/zen-free/README.md b/packages/zen-free/README.md index 0e5809b..ca69350 100644 --- a/packages/zen-free/README.md +++ b/packages/zen-free/README.md @@ -56,15 +56,18 @@ 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 (or with no name) is left out of the reply, and -the reply's text says which name was left out; a turn whose every call -was left out ends as an ordinary answer. Tools run in the agent. +call of a tool the agent has no spelling of (`psh` beside `pwsh`, `bedit`) +reaches the agent as the model called it, as with any other provider: the +agent answers it with its own unknown-tool error, and the model corrects +the name and goes on. Only a call with no name is left out, and the +reply's text says so; a turn whose every call was left out ends as an +ordinary answer. Tools run in the agent. Zen's free tier only answers a request that carries tools, so a request is sent with the five core OpenCode tools the agent doesn't declare itself (`bash`, `edit`, `glob`, `grep`, `read`), described as not -available. A call of one of them the agent doesn't have is left out like -any other. +available. A call of one of them the agent doesn't have reaches the agent +like any other. Now and then Zen closes a streamed Chat Completions reply before its `[DONE]`. When the reply has said something and no tool call of it is diff --git a/packages/zen-free/index.test.mjs b/packages/zen-free/index.test.mjs index a6f54fc..4729325 100644 --- a/packages/zen-free/index.test.mjs +++ b/packages/zen-free/index.test.mjs @@ -126,16 +126,17 @@ for (const { protocol, path, model } of protocols) { // 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 - // failing (#76). + // A call of a tool the agent doesn't have reaches the agent as + // called, as a tool call, so the agent answers it with its own + // unknown-tool error and the model goes on; it neither fails the + // turn (#76) nor ends it as a plain answer (#209). state.reply = toolReply(protocol, "edit", upstreamSSE); - const left = await send(); - assert.equal(left.status, 200); - const text = await left.text(); - assert.doesNotMatch(text, /"name"\s*:\s*"edit"/); - assert.doesNotMatch(text, /"error"|"tool_calls"|"tool_use"|"function_call"/); - assert.match(text, /a tool this agent doesn't have: \\"edit\\"/); + const handed = await send(); + assert.equal(handed.status, 200); + const text = await handed.text(); + assert.match(text, /"name"\s*:\s*"edit"/); + assert.match(text, /"tool_calls"|"tool_use"|"function_call"/); + assert.doesNotMatch(text, /"error"|The call was left out/); }); } } @@ -156,7 +157,7 @@ test("keeps declared tools isolated between requests using the same loader", asy assert.equal(blocked.status, 200); const text = await blocked.text(); assert.doesNotMatch(text, /"name"\s*:\s*"Bash"/); - assert.match(text, /a tool this agent doesn't have: \\"bash\\"/); + assert.match(text, /"name"\s*:\s*"bash"/); }); for (const [input, supplied, expected] of [ diff --git a/packages/zen-free/package.json b/packages/zen-free/package.json index fc9c2e3..7c22a1b 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.10", + "version": "0.1.11", "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 bf13a2c..b0dc678 100644 --- a/packages/zen-free/tool-response.mjs +++ b/packages/zen-free/tool-response.mjs @@ -48,8 +48,13 @@ function foldsTo(declared, key) { return folded; } -// nameGuard answers the agent's name for a call, or null for a call of no -// tool the agent has (a missing name included). +// nameGuard answers the agent's name for a call: the agent's spelling when +// one folds to it, else the name as the model wrote it. A call of a tool +// the agent doesn't have (bedit, psh, mcp__netcatcatty__unused) goes to the +// agent as called, like any other provider's: the agent answers it with +// its own unknown-tool error, and the model corrects the name and goes on +// (#209). Leaving it out ended an agent's turn as a plain answer. Only a +// call with no name answers null, and is left out. function nameGuard(policy) { const declared = new Set(policy.declared); const lower = (name) => name.toLowerCase(); @@ -61,16 +66,15 @@ function nameGuard(policy) { if (declared.has(name)) return name; const match = byCase.get(lower(name)) ?? (byCase.has(lower(name)) ? null : byShape.get(bare(name))); - return match || null; + return match || name; }; } -// The models also call tools no agent has: bedit, bcp, ash, a mangled -// mcp__netcatcatty__unused (#76). Such a call can't be run or named onto -// one the agent has, and failing the reply for it threw away the whole -// turn. It is left out instead, and the reply says so in its text, where -// the agent's user and the model's next turn both read it. A reply left -// with no call ends as a plain stop. +// A call with no name can't be answered by any agent, and failing the +// reply for it threw away the whole turn (#76). It is left out instead, +// and the reply says so in its text, where the agent's user and the +// model's next turn both read it. A reply left with no call ends as a +// plain stop. export function droppedNote(name) { return typeof name === "string" && name ? `(The model called a tool this agent doesn't have: ${JSON.stringify(name.slice(0, 128))}. The call was left out.)` diff --git a/packages/zen-free/tool-response.test.mjs b/packages/zen-free/tool-response.test.mjs index 108eea0..a1aa6c1 100644 --- a/packages/zen-free/tool-response.test.mjs +++ b/packages/zen-free/tool-response.test.mjs @@ -352,7 +352,7 @@ for (const protocol of protocols) { } }); - test(`${protocol}: JSON leaves out unknown, non-injected, ambiguous and semantic aliases, and says so (#76)`, async () => { + test(`${protocol}: JSON hands on unknown, non-injected, ambiguous and semantic aliases as called (#209)`, async () => { for (const [name, rules] of [ ["unknown", policy], ["shell", policy], @@ -370,15 +370,19 @@ for (const protocol of protocols) { ); assert.equal(response.status, 200); const body = await response.json(); - assert.deepEqual(callsIn(protocol, [body]), ["Read"]); - assert.equal(textIn(protocol, [body]), droppedNote(name)); + assert.deepEqual(callsIn(protocol, [body]), ["Read", name]); + assert.equal(textIn(protocol, [body]), ""); } }); - // #76, tkhs101's table of 2026-10-10: the names step-5-preview-free - // called that DSH has no tool for, and no spelling of one either. Each - // failed the whole turn; each is now left out with the turn going on. - test(`${protocol}: the names from #76's table are left out, not failed on`, async () => { + // #76, tkhs101's table of 2026-10-10, and #209, xiotbox's DSH sessions + // of 2026-10-16/17 (pwsh declared): the names Zen's free models called + // that the agent has no tool for, and no spelling of one either. Left + // out, a reply of only such a call ended DSH's turn as a plain answer. + // Each now reaches the agent as called, with its id and a tool-call + // finish, so the agent answers it with its own unknown-tool error and + // the model corrects it. + test(`${protocol}: a call of a tool the agent doesn't have reaches it as called, with its id and a tool-call finish (#76, #209)`, async () => { const rules = { declared: new Set([ "bash", @@ -387,21 +391,76 @@ for (const protocol of protocols) { "glob", "grep", "write", + "pwsh", "todo_write", "mcp__netcatty-external__list_hosts", ]), injected: new Set(), }; - for (const name of ["bedit", "bcp", "ash", "mcp__netcatcatty__unused"]) { - const json = await guardToolResponse( + const ids = (values) => + values.flatMap((data) => { + if (protocol === "chat") + return (data.choices ?? []) + .flatMap((choice) => (choice.message ?? choice.delta)?.tool_calls ?? []) + .map((value) => value.id) + .filter(Boolean); + const items = + protocol === "responses" + ? [...((data.response ?? data).output ?? []), ...(data.item ? [data.item] : [])] + : [ + ...((data.message ?? data).content ?? []), + ...(data.content_block ? [data.content_block] : []), + ]; + return items + .filter((item) => ["function_call", "tool_use"].includes(item.type)) + .map((item) => item.call_id ?? item.id); + }); + const finishes = (values) => + values.flatMap((data) => + protocol === "chat" + ? (data.choices ?? []).map((choice) => choice.finish_reason).filter(Boolean) + : protocol === "anthropic" + ? [data.delta?.stop_reason, data.stop_reason].filter(Boolean) + : [], + ); + for (const name of [ + "bedit", + "bcp", + "ash", + "mcp__netcatcatty__unused", + "psh", + "pash", + "ppwsh", + "pshh", + "shell_command", + ]) { + // Beside a call the agent has (#209's case B), and alone (case A). + const mixed = await guardToolResponse( Response.json(snapshot(protocol, ["bash", name])), protocol, rules, ); - assert.equal(json.status, 200); - const body = await json.json(); - assert.deepEqual(callsIn(protocol, [body]), ["bash"]); - assert.equal(textIn(protocol, [body]), droppedNote(name)); + assert.equal(mixed.status, 200); + const body = await mixed.json(); + assert.deepEqual(callsIn(protocol, [body]), ["bash", name]); + assert.equal(textIn(protocol, [body]), ""); + const alone = { + chat: { + choices: [ + { + index: 0, + message: { role: "assistant", content: null, tool_calls: [call(name)] }, + finish_reason: "tool_calls", + }, + ], + }, + responses: { output: [tool(protocol, name)] }, + anthropic: { type: "message", content: [tool(protocol, name)], stop_reason: "tool_use" }, + }[protocol]; + const json = await ( + await guardToolResponse(Response.json(alone), protocol, rules) + ).json(); + assert.deepEqual(json, alone); for (const upstream of [ event(streamSnapshot(protocol, [name])) + terminal(protocol), toolStream(protocol, name), @@ -410,14 +469,30 @@ for (const protocol of protocols) { assert.equal(streamed.status, 200); const values = frameData(await streamed.text()); assert.ok(!values.some((data) => data.type === "error" || data.error)); - assert.deepEqual(callsIn(protocol, values), []); - assert.match( - textIn(protocol, values), - new RegExp(`^(\\n\\n)?${escape(droppedNote(name))}`), - ); - assert.ok(!JSON.stringify(values).includes(`"${name}"`)); - assert.doesNotMatch(JSON.stringify(values), /"(?:tool_calls|tool_use|function_call)"/); + assert.ok(callsIn(protocol, values).length > 0); + assert.ok(callsIn(protocol, values).every((called) => called === name)); + assert.ok(ids(values).length > 0); + assert.equal(textIn(protocol, values), ""); + assert.ok(!JSON.stringify(values).includes("The call was left out")); + assert.ok(finishes(values).every((f) => f === "tool_calls" || f === "tool_use")); } + // Zen's own stream comes through as it was sent (Chat's call whole, + // as its buffer hands every call on). + const raw = toolStream(protocol, name); + const passed = frameData(await (await guardToolResponse(sse(raw), protocol, rules)).text()); + if (protocol !== "chat") assert.deepEqual(passed, frameData(raw)); + else + assert.deepEqual( + passed.flatMap((data) => data.choices.flatMap((choice) => choice.delta.tool_calls ?? [])), + [ + { + index: 0, + id: "call_773767abbc494d2a82872502", + type: "function", + function: { name, arguments: '{"command": "ls"}' }, + }, + ], + ); } }); @@ -463,19 +538,19 @@ for (const protocol of protocols) { } }); - test(`${protocol}: a call left out is named in the reply's text (#76)`, async () => { + test(`${protocol}: a call with no name is left out, and the reply's text says so (#76)`, async () => { const json = await guardToolResponse( - Response.json(snapshot(protocol, ["subagent"])), + Response.json(snapshot(protocol, [""])), protocol, policy, ); - assert.equal(textIn(protocol, [await json.json()]), droppedNote("subagent")); + assert.equal(textIn(protocol, [await json.json()]), droppedNote("")); const streamed = await guardToolResponse( - sse(event(streamSnapshot(protocol, ["subagent"])) + terminal(protocol)), + sse(event(streamSnapshot(protocol, [""])) + terminal(protocol)), protocol, policy, ); - assert.equal(textIn(protocol, frameData(await streamed.text())), droppedNote("subagent")); + assert.equal(textIn(protocol, frameData(await streamed.text())), droppedNote("")); assert.equal( droppedNote("x".repeat(300)), `(The model called a tool this agent doesn't have: "${"x".repeat(128)}". The call was left out.)`, @@ -536,7 +611,7 @@ for (const protocol of protocols) { assert.equal(source.response.body.locked, false); }); - test(`${protocol}: a mixed SSE snapshot hands on the declared call and leaves out the rest (#76)`, async () => { + test(`${protocol}: a mixed SSE snapshot hands on every named call and leaves out a nameless one (#76, #209)`, async () => { for (const name of ["unknown", "shell", "todowrite"]) { const result = await guardToolResponse( sse(event(streamSnapshot(protocol, ["Read", name])) + terminal(protocol)), @@ -545,9 +620,18 @@ for (const protocol of protocols) { ); const values = frameData(await result.text()); assert.ok(!values.some((data) => data.type === "error" || data.error)); - assert.deepEqual(callsIn(protocol, values), ["Read"]); - assert.equal(textIn(protocol, values), droppedNote(name)); + assert.deepEqual(callsIn(protocol, values), ["Read", name]); + assert.equal(textIn(protocol, values), ""); } + const result = await guardToolResponse( + sse(event(streamSnapshot(protocol, ["Read", ""])) + terminal(protocol)), + protocol, + policy, + ); + const values = frameData(await result.text()); + assert.ok(!values.some((data) => data.type === "error" || data.error)); + assert.deepEqual(callsIn(protocol, values), ["Read"]); + assert.equal(textIn(protocol, values), droppedNote("")); }); test(`${protocol}: upstream SSE errors pass through byte-for-byte and cancel open streams`, async () => { @@ -745,8 +829,17 @@ test("chat: JSON checks every choice, legacy function_call and missing tool name policy, ); assert.equal((await result.json()).choices[0].message.function_call.name, "Read"); + const unknown = await guardToolResponse( + Response.json({ choices: [{ message: { function_call: { name: "unknown", arguments: "{}" } }, finish_reason: "function_call" }] }), + "chat", + policy, + ); + assert.deepEqual((await unknown.json()).choices[0], { + message: { function_call: { name: "unknown", arguments: "{}" } }, + finish_reason: "function_call", + }); for (const [message, name] of [ - [{ function_call: { name: "unknown" } }, "unknown"], + [{ function_call: { name: "" } }, ""], [{ tool_calls: [{ type: "function", function: {} }] }, undefined], ]) { const source = Response.json({ @@ -788,7 +881,7 @@ test("Responses: nested JSON responses and all streaming snapshots are checked", "response.incomplete", "vendor.snapshot", ]) { - for (const name of ["read", "unknown"]) { + for (const name of ["read", "unknown", ""]) { const data = type.includes("output_item") ? { type, item: tool("responses", name) } : { type, response: snapshot("responses", [name]) }; @@ -798,7 +891,7 @@ test("Responses: nested JSON responses and all streaming snapshots are checked", policy, ); const frames = parse(await result.text()); - if (name === "unknown") { + if (name === "") { const raw = frames.map((frame) => frame.data); assert.ok(!raw.some((data) => data.type === "error" || data.error)); assert.deepEqual(callsIn("responses", raw), []); @@ -809,16 +902,19 @@ test("Responses: nested JSON responses and all streaming snapshots are checked", type === "response.output_item.added" ? textIn("responses", raw) : message.content[0].text; - assert.equal(said, droppedNote("unknown")); + assert.equal(said, droppedNote("")); } else - assert.equal(frames[0].data.item?.name ?? frames[0].data.response.output[0].name, "Read"); + assert.equal( + frames[0].data.item?.name ?? frames[0].data.response.output[0].name, + name === "read" ? "Read" : name, + ); } } }); test("Anthropic: content_block_start, message_start and server_tool_use are guarded", async () => { for (const type of ["tool_use", "server_tool_use"]) { - for (const name of ["bash", "unknown"]) { + for (const name of ["bash", "unknown", ""]) { const block = { type, name, id: "t", input: {} }; const json = await guardToolResponse( Response.json({ content: [block] }), @@ -826,8 +922,9 @@ test("Anthropic: content_block_start, message_start and server_tool_use are guar policy, ); assert.equal(json.status, 200); - if (name === "unknown") + if (name === "") assert.deepEqual((await json.json()).content, [{ type: "text", text: droppedNote(name) }]); + else if (name === "unknown") assert.deepEqual((await json.json()).content, [block]); for (const data of [ { type: "content_block_start", index: 0, content_block: block }, { type: "message_start", message: { content: [block] } }, @@ -838,7 +935,7 @@ test("Anthropic: content_block_start, message_start and server_tool_use are guar policy, ); const frames = parse(await response.text()); - if (name === "unknown") { + if (name === "") { const values = frames.map((frame) => frame.data); assert.ok(!values.some((value) => value.type === "error")); assert.deepEqual(callsIn("anthropic", values), []); @@ -846,7 +943,7 @@ test("Anthropic: content_block_start, message_start and server_tool_use are guar } else assert.equal( frames[0].data.content_block?.name ?? frames[0].data.message.content[0].name, - "Bash", + name === "bash" ? "Bash" : name, ); } } @@ -1047,7 +1144,7 @@ test("cumulative pending chat tools are limited across choices and calls", { tim assert.ok(pulls <= 33); }); -test("chat: each pending choice is settled on its own, an unknown or missing final name left out", async () => { +test("chat: each pending choice is settled on its own, an unknown final name handed on and a missing one left out", async () => { for (const ending of [ terminal("chat"), event({ @@ -1068,15 +1165,15 @@ test("chat: each pending choice is settled on its own, an unknown or missing fin const choices = values.flatMap((data) => data.choices ?? []); const of = (index) => choices.filter((choice) => choice.index === index); assert.deepEqual(callsIn("chat", [{ choices: of(0) }]), ["Bash"]); - assert.deepEqual(callsIn("chat", [{ choices: of(1) }]), []); - assert.equal(textIn("chat", [{ choices: of(1) }]), droppedNote(name)); + assert.deepEqual(callsIn("chat", [{ choices: of(1) }]), name ? [name] : []); + assert.equal(textIn("chat", [{ choices: of(1) }]), name ? "" : droppedNote(name)); const finishes = (index) => of(index) .map((choice) => choice.finish_reason) .filter(Boolean); if (ending !== terminal("chat")) { assert.deepEqual(finishes(0), ["tool_calls"]); - assert.deepEqual(finishes(1), ["stop"]); + assert.deepEqual(finishes(1), [name ? "tool_calls" : "stop"]); } } } @@ -1085,32 +1182,32 @@ test("chat: each pending choice is settled on its own, an unknown or missing fin test("chat: a choice whose every call was left out finishes as a stop, again and again (#76)", async () => { // step-5-preview-free sends its tool_calls finish twice, the second with // usage (seen 2026-10-11); neither may tell the agent to run a tool. - const response = await guardToolResponse(sse(toolStream("chat", "bedit")), "chat", policy); + const response = await guardToolResponse(sse(toolStream("chat", "")), "chat", policy); const values = frameData(await response.text()); const finishes = values .flatMap((data) => data.choices ?? []) .map((choice) => choice.finish_reason) .filter(Boolean); assert.deepEqual(finishes, ["stop", "stop"]); - assert.equal(textIn("chat", values), droppedNote("bedit")); + assert.equal(textIn("chat", values), droppedNote("")); assert.ok(values.some((data) => data.usage?.completion_tokens === 5)); }); test("chat: the note follows the text the model wrote before its call", async () => { const text = event(chat({ content: "Fixing the stray heredoc marker." })) + - event(chat({ tool_calls: [call("bedit")] }, "tool_calls")) + + event(chat({ tool_calls: [call("")] }, "tool_calls")) + terminal("chat"); const response = await guardToolResponse(sse(text), "chat", policy); assert.equal( textIn("chat", frameData(await response.text())), - `Fixing the stray heredoc marker.\n\n${droppedNote("bedit")}`, + `Fixing the stray heredoc marker.\n\n${droppedNote("")}`, ); }); test("chat: a call kept beside one left out is numbered from 0", async () => { const text = - event(chat({ tool_calls: [call("bedit", 0), call("read", 1, '{"path":"a"}')] }, "tool_calls")) + + event(chat({ tool_calls: [call("", 0), call("read", 1, '{"path":"a"}')] }, "tool_calls")) + terminal("chat"); const response = await guardToolResponse(sse(text), "chat", policy); const values = frameData(await response.text()); @@ -1129,7 +1226,7 @@ test("chat: a call kept beside one left out is numbered from 0", async () => { assert.ok(finishes.includes("tool_calls")); }); -test("SSE: ambiguous injected names are left out in every protocol", async () => { +test("SSE: an ambiguous injected name is handed on as called, never guessed, in every protocol", async () => { const rules = { declared: new Set(["Bash", "BASH"]), injected: new Set(["bash"]) }; for (const protocol of protocols) { const response = await guardToolResponse( @@ -1139,15 +1236,15 @@ test("SSE: ambiguous injected names are left out in every protocol", async () => ); const values = frameData(await response.text()); assert.ok(!values.some((data) => data.type === "error" || data.error)); - assert.deepEqual(callsIn(protocol, values), []); - assert.equal(textIn(protocol, values), droppedNote("bash")); + assert.deepEqual(callsIn(protocol, values), ["bash"]); + assert.equal(textIn(protocol, values), ""); } }); test("Responses and Messages: a streamed call left out comes as text in its place", async () => { const responses = frameData( await ( - await guardToolResponse(sse(toolStream("responses", "bcp")), "responses", policy) + await guardToolResponse(sse(toolStream("responses", "")), "responses", policy) ).text(), ); assert.deepEqual( @@ -1163,7 +1260,7 @@ test("Responses and Messages: a streamed call left out comes as text in its plac "response.completed", ], ); - const note = droppedNote("bcp"); + const note = droppedNote(""); const message = { type: "message", id: "fc_1", role: "assistant", status: "completed" }; assert.deepEqual(responses[6].item, { ...message, @@ -1176,7 +1273,7 @@ test("Responses and Messages: a streamed call left out comes as text in its plac } const messages = frameData( await ( - await guardToolResponse(sse(toolStream("anthropic", "ash")), "anthropic", policy) + await guardToolResponse(sse(toolStream("anthropic", "")), "anthropic", policy) ).text(), ); assert.deepEqual( @@ -1191,7 +1288,7 @@ test("Responses and Messages: a streamed call left out comes as text in its plac ], ); assert.deepEqual(messages[1].content_block, { type: "text", text: "" }); - assert.deepEqual(messages[2].delta, { type: "text_delta", text: droppedNote("ash") }); + assert.deepEqual(messages[2].delta, { type: "text_delta", text: droppedNote("") }); assert.equal(messages[4].delta.stop_reason, "end_turn"); }); @@ -1204,7 +1301,7 @@ test("Messages: a kept call keeps the tool_use stop beside one left out", async { type: "content_block_start", index: 0, - content_block: { type: "tool_use", id: "a", name: "ash", input: {} }, + content_block: { type: "tool_use", id: "a", name: "", input: {} }, }, { type: "content_block_stop", index: 0 }, { @@ -1411,8 +1508,8 @@ test("a declared name of separators only is never handed another call (#76 follo rules, ); const data = await result.json(); - assert.deepEqual(callsIn("chat", [data]), []); - assert.match(textIn("chat", [data]), /a tool this agent doesn't have/); + assert.deepEqual(callsIn("chat", [data]), [name]); + assert.equal(textIn("chat", [data]), ""); } const exact = await guardToolResponse(Response.json(snapshot("chat", ["_"])), "chat", rules); assert.deepEqual(callsIn("chat", [await exact.json()]), ["_"]);