diff --git a/packages/devin/index.mjs b/packages/devin/index.mjs index a098b63..7455547 100644 --- a/packages/devin/index.mjs +++ b/packages/devin/index.mjs @@ -957,6 +957,26 @@ const NO_RESULT = "Tool use was interrupted and did not produce a result." const textOf = (c) => typeof c === "string" ? c : Array.isArray(c) ? c.filter((p) => p?.type === "text").map((p) => p.text ?? "").join("") : "" +// joinSplitCalls joins an assistant message onto the assistant message +// before it when that one made tool calls: Anthropic takes consecutive +// assistant messages as one turn, and an agent can send a turn's parallel +// calls split over two of them, answered together after (yetone/magpie#1275). +// Answered at the second message, the first calls would be marked +// interrupted and their real results dropped. msgs is left as it is. +function joinSplitCalls(msgs) { + const out = [] + for (const m of msgs) { + const prev = out.at(-1) + if (m?.role === "assistant" && prev?.role === "assistant" && prev.tool_calls?.length) { + const content = [textOf(prev.content), textOf(m.content)].filter(Boolean).join("\n\n") + out[out.length - 1] = { ...prev, content, tool_calls: [...prev.tool_calls, ...(m.tool_calls ?? [])] } + continue + } + out.push(m) + } + return out +} + // effortOf is the effort a request names, as magpie reads it. function effortOf(s) { const e = String(s ?? "").trim().toLowerCase() @@ -1087,7 +1107,7 @@ function build(chat, uid, key) { pending = [] } const system = [] - for (const m of chat.messages ?? []) { + for (const m of joinSplitCalls(chat.messages ?? [])) { if (m.role === "system" || m.role === "developer") { system.push(textOf(m.content)) continue diff --git a/packages/devin/package.json b/packages/devin/package.json index ac84914..13dfbf5 100644 --- a/packages/devin/package.json +++ b/packages/devin/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-devin-auth", - "version": "0.1.10", + "version": "0.1.11", "description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/devin/split-calls.test.mjs b/packages/devin/split-calls.test.mjs new file mode 100644 index 0000000..1c59ed0 --- /dev/null +++ b/packages/devin/split-calls.test.mjs @@ -0,0 +1,66 @@ +// One turn's parallel tool calls split over two consecutive assistant +// messages, answered together after (yetone/magpie#1275): both calls keep +// their real results, as magpie's built-in buildDevin does since 7067fce5 +// (internal/gateway/split_calls_test.go). +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const NO_RESULT = "Tool use was interrupted and did not produce a result." +const dec = new TextDecoder() +const call = (id, path) => ({ id, type: "function", function: { name: "read", arguments: JSON.stringify({ path }) } }) +const split = () => ({ + messages: [ + { role: "user", content: "read a and b" }, + { role: "assistant", content: null, tool_calls: [call("toolu_a", "a")] }, + { role: "assistant", content: null, tool_calls: [call("toolu_b", "b")] }, + { role: "tool", tool_call_id: "toolu_a", content: "A!" }, + { role: "tool", tool_call_id: "toolu_b", content: "B!" }, + ], +}) + +// summary is each message sent, as magpie's devinSummary writes it: role, +// text, the calls it made and the call it answers. +const summary = (chat) => + _internal + .fields(_internal.build(chat, "swe-2-high", "k")) + .filter((f) => f.num === 3 && f.wire === 2) + .map((f) => { + let role = 0, text = "", answers = "" + const calls = [] + for (const g of _internal.fields(f.data)) { + if (g.num === 2) role = g.n + else if (g.num === 3) text = dec.decode(g.data) + else if (g.num === 6) calls.push(_internal.fields(g.data).slice(0, 2).map((h) => dec.decode(h.data)).join("/")) + else if (g.num === 7) answers = dec.decode(g.data) + } + return `${role}:${text}${calls.map((c) => " call " + c).join("")}${answers ? " for " + answers : ""}` + }) + +test("a split turn's calls are answered by their real results", () => { + const got = summary(split()) + expect(got.join("\n")).not.toContain(NO_RESULT) + expect(got.slice(1)).toEqual(["2: call toolu_a/read call toolu_b/read", "4:A! for toolu_a", "4:B! for toolu_b"]) +}) + +test("a call of a split turn that went unanswered is still answered for", () => { + const chat = split() + chat.messages.splice(3, 1) + expect(summary(chat).slice(1)).toEqual(["2: call toolu_a/read call toolu_b/read", "4:B! for toolu_b", `4:${NO_RESULT} for toolu_a`]) +}) + +test("text the turn said after its call, in a message of its own, joins it", () => { + const chat = { + messages: [ + { role: "user", content: "read a" }, + { role: "assistant", content: null, tool_calls: [call("toolu_a", "a")] }, + { role: "assistant", content: "reading" }, + { role: "tool", tool_call_id: "toolu_a", content: "1" }, + ], + } + expect(summary(chat).slice(1)).toEqual(["2:reading call toolu_a/read", "4:1 for toolu_a"]) +}) + +test("two assistant messages with no calls stay two", () => { + const chat = { messages: [{ role: "user", content: "hi" }, { role: "assistant", content: "one" }, { role: "assistant", content: "two" }] } + expect(summary(chat).slice(1, 3)).toEqual(["2:one", "2:two"]) +})