diff --git a/packages/agy-cli/README.en.md b/packages/agy-cli/README.en.md index 3353f3b..ef322ef 100644 --- a/packages/agy-cli/README.en.md +++ b/packages/agy-cli/README.en.md @@ -341,7 +341,9 @@ off unless you turn it on. - **HTTP 409 saying tool results do not match the history**: usually the client rewrote the history or tool IDs mid-conversation. When the plugin can work out the correct match it rebuilds the session; when it cannot, it refuses rather - than guessing. + than guessing. Hermes Agent writes one batching `tool_call({"calls": [...]})` + into its history as one call per entry; the plugin folds those back into the + one call agy made, with one result (#1150). - **Turning thought summaries off**: set `AGY_BRIDGE_THINKING=0`, or send `thinking: { "type": "disabled" }` with the request. diff --git a/packages/agy-cli/README.md b/packages/agy-cli/README.md index a8a52e2..37b2b78 100644 --- a/packages/agy-cli/README.md +++ b/packages/agy-cli/README.md @@ -245,6 +245,8 @@ agy 自己的系统提示词,只剩插件的一行工具说明和客户端的 重试中的提示行会被去掉。 - **HTTP 409,提示工具结果与历史对不上**:通常是客户端在对话中途改写了历史或工具 ID。 能确定对应关系时插件会重建会话;无法确定时直接拒绝,不做猜测。 + Hermes Agent 会把一次批量的 `tool_call({"calls": [...]})` 在自己的历史里拆成每项一次调用, + 插件会把它合回 agy 发出的那一次调用、那一份结果(#1150)。 - **想关掉思考摘要**:设置 `AGY_BRIDGE_THINKING=0`,或者在请求里带上 `thinking: { "type": "disabled" }`。 diff --git a/packages/agy-cli/index.mjs b/packages/agy-cli/index.mjs index ff1f9f8..7665e9c 100644 --- a/packages/agy-cli/index.mjs +++ b/packages/agy-cli/index.mjs @@ -4131,6 +4131,81 @@ function lastAssistantCalls(messages) { ? message.content.filter(block => block.type === "tool_use") : []) } +// A bridge call's id, as #handleMcpCall makes it. +const BRIDGE_CALL_ID = /^toolu_[0-9a-f]{24}$/ + +// Hermes Agent unrolls a batching call it executes, tool_call({calls: [a, b, +// c]}), into one tool_call per entry in its own history: the first keeps the +// bridge's id, the others get ids of Hermes' own (call_), and each gets +// a result (NieiR, plugins#1150). No call but the first is agy's, so their +// results matched nothing and the turn ended in 409. Folded back here, the +// round is the one agy made: one call, its input's array the entries joined +// in order, and one result, the entries' results joined in order. Folded +// only when a run of same-named calls opens with a bridge id, every other +// one has an id the bridge never makes, and every input is an object with +// the same single array field. +function foldSplitCalls(body) { + const messages = body?.messages + if (!Array.isArray(messages)) return body + let changed = false + const out = messages.map((message) => message) + for (let index = 0; index < out.length; index++) { + const message = out[index] + if (message?.role !== "assistant" || !Array.isArray(message.content)) continue + const uses = message.content.filter((block) => block?.type === "tool_use") + if (uses.length < 2) continue + const head = uses[0] + if (!BRIDGE_CALL_ID.test(head.id ?? "") || uses.slice(1).some((use) => BRIDGE_CALL_ID.test(use.id ?? "") || !use.id)) continue + if (uses.some((use) => use.name !== head.name)) continue + const keyOf = (use) => { + const input = use.input + if (!input || typeof input !== "object" || Array.isArray(input)) return null + const keys = Object.keys(input) + return keys.length === 1 && Array.isArray(input[keys[0]]) ? keys[0] : null + } + const key = keyOf(head) + if (!key || uses.some((use) => keyOf(use) !== key)) continue + const ids = new Set(uses.map((use) => use.id)) + if (ids.size !== uses.length) continue + // the results: in the messages after this one, up to the next assistant + const results = [] + let end = index + 1 + while (end < out.length && out[end]?.role !== "assistant") end++ + for (let at = index + 1; at < end; at++) { + const content = out[at]?.content + if (!Array.isArray(content)) continue + for (const block of content) if (block?.type === "tool_result" && ids.has(block.tool_use_id)) results.push(block) + } + // a round still waiting has no results; one with some missing is left as it is + if (results.length && results.length !== uses.length) continue + const folded = { ...head, input: { [key]: uses.flatMap((use) => use.input[key]) } } + out[index] = { ...message, content: message.content.filter((block) => block?.type !== "tool_use" || block === head).map((block) => (block === head ? folded : block)) } + if (results.length) { + const byId = new Map(results.map((block) => [block.tool_use_id, block])) + const parts = (block) => typeof block.content === "string" ? [{ type: "text", text: block.content }] + : Array.isArray(block.content) ? block.content : block.content == null ? [] : [block.content] + const ordered = uses.map((use) => byId.get(use.id)) + const merged = { ...byId.get(head.id), content: ordered.flatMap(parts) } + if (ordered.every((block) => block.is_error)) merged.is_error = true + else delete merged.is_error + for (let at = index + 1; at < end; at++) { + const content = out[at]?.content + if (!Array.isArray(content) || !content.some((block) => block?.type === "tool_result" && ids.has(block.tool_use_id))) continue + out[at] = { ...out[at], content: content.flatMap((block) => { + if (block?.type !== "tool_result" || !ids.has(block.tool_use_id)) return [block] + return block.tool_use_id === head.id ? [merged] : [] + }) } + } + // a message left with nothing (its results all folded away) goes + for (let at = end - 1; at > index; at--) { + if (Array.isArray(out[at]?.content) && out[at].content.length === 0) { out.splice(at, 1); end-- } + } + } + changed = true + } + return changed ? { ...body, messages: out } : body +} + function matchToolResults(body, blocks) { const direct = blocks.map(block => ({ block, match: toolCalls.get(block.tool_use_id) })) if (direct.every(item => item.match)) return direct @@ -6360,7 +6435,7 @@ export const AntigravityBridgePlugin = async () => { // One canonical form for both opening-system layouts, before anything // derives a key, instructions or history from the body. const layout = requestLayout(body) - body = { ...canonicalBody(body), model: requestedModel } + body = { ...foldSplitCalls(canonicalBody(body)), model: requestedModel } if (isOrchestrator(requestedModel)) return jsonError(400, orchestratorRefusal(requestedModel), undefined, { noRetry: true }) const model = resolveAgyModel(await loadModelCatalog(agy), requestedModel, body) const selection = { model: requestedModel, effort: requestedEffort(body) } diff --git a/packages/agy-cli/package.json b/packages/agy-cli/package.json index 13bbdd0..1d864bf 100644 --- a/packages/agy-cli/package.json +++ b/packages/agy-cli/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-agy-cli-auth", - "version": "0.1.72", + "version": "0.1.73", "description": "Magpie/OpenCode model provider that connects your local agy CLI through the Agent Client Protocol (agy acp), so Antigravity subscription models run through the real CLI", "type": "module", "main": "./opencode.mjs", diff --git a/packages/agy-cli/test/repeat-batch.mjs b/packages/agy-cli/test/repeat-batch.mjs index c13d5e3..ccc3089 100644 --- a/packages/agy-cli/test/repeat-batch.mjs +++ b/packages/agy-cli/test/repeat-batch.mjs @@ -227,6 +227,67 @@ test("A9: tool calls agy issues across adjacent event-loop turns leave in one re }) }) +// ------------------------------------- A9b a batching call split by the client + +const BATCH_TOOL = { + name: "tool_call", + description: "Run several tools in one call.", + input_schema: { type: "object", properties: { calls: { type: "array", items: { type: "object" } } }, required: ["calls"] }, +} + +// Hermes Agent runs tool_call({calls: [a, b, c]}) and writes it into its +// history as three tool_calls of one entry each, the first under the bridge's +// id and the others under ids of its own, each with a result (plugins#1150). +function splitByClient(body, first, results) { + const use = first.content.find((block) => block.type === "tool_use") + const ids = [use.id, "call_0a1b2c3d4e5f", "call_6a7b8c9d0e1f"] + return { + ...body, + messages: [ + ...body.messages, + { role: "assistant", content: use.input.calls.map((entry, index) => ({ type: "tool_use", id: ids[index], name: use.name, input: { calls: [entry] } })) }, + { role: "user", content: ids.map((id, index) => ({ type: "tool_result", tool_use_id: id, content: results[index] })) }, + ], + } +} + +test("A9b: a batching call the client split into one call per entry is one result for agy (#1150)", async () => { + await withKit({}, async (kit) => { + let reply + const calls = [{ name: "web_search", arguments: { q: "a" } }, { name: "web_search", arguments: { q: "b" } }, { name: "terminal", arguments: { cmd: "c" } }] + kit.prompts.push(async (agent) => { + reply = await agent.tool("tool_call", { calls }) + await agent.text("done") + return { stopReason: "end_turn" } + }) + const body = baseBody({ metadata: { user_id: "split" }, tools: [BATCH_TOOL] }) + const first = await readSse(await kit.request(body)) + assert.equal(first.stopReason, "tool_use") + const response = await kit.request(splitByClient(body, first, ["result a", "result b", "result c"])) + const status = response.status + const final = status === 200 ? await readSse(response) : await response.text() + assert.equal(status, 200, typeof final === "string" ? final : "") + assert.equal(final.stopReason, "end_turn") + const text = (reply?.result?.content ?? reply?.content ?? []).map((part) => part.text).join("\n") + assert.match(text, /result a[\s\S]*result b[\s\S]*result c/) + }) +}) + +test("A9b: separate calls the bridge made are never folded into one (#1150)", async () => { + await withKit({}, async (kit) => { + kit.prompts.push(async (agent) => { + await agent.tools([["tool_call", { calls: [{ name: "x" }] }], ["tool_call", { calls: [{ name: "y" }] }]]) + await agent.text("done") + return { stopReason: "end_turn" } + }) + const body = baseBody({ metadata: { user_id: "apart" }, tools: [BATCH_TOOL] }) + const first = await readSse(await kit.request(body)) + assert.equal(first.content.filter((block) => block.type === "tool_use").length, 2) + const final = await readSse(await kit.request(continueWithToolResults(body, first))) + assert.equal(final.stopReason, "end_turn") + }) +}) + // ------------------------------------------------- A10 canonical system rule test("A10: only the opening system prefix is lifted, so a trailing system context keeps the key across turns", async () => {