diff --git a/packages/cursor/index.mjs b/packages/cursor/index.mjs index bbed100..42a9425 100644 --- a/packages/cursor/index.mjs +++ b/packages/cursor/index.mjs @@ -954,6 +954,26 @@ function statusOf(status, code, msg) { 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 +} + function parseArgs(s) { if (s && typeof s === "object") return s try { @@ -1035,7 +1055,7 @@ function conversation(chat, tools) { if (results.length) add({ role: "tool", content: results }) results = [] } - for (const m of chat.messages ?? []) { + for (const m of joinSplitCalls(chat.messages ?? [])) { if (m.role === "system" || m.role === "developer") continue if (m.role === "tool") { const id = m.tool_call_id ?? "" diff --git a/packages/cursor/package.json b/packages/cursor/package.json index b2a617b..f2fc993 100644 --- a/packages/cursor/package.json +++ b/packages/cursor/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-cursor-auth", - "version": "0.1.10", + "version": "0.1.11", "description": "Cursor subscriptions (Pro, Pro+, Ultra, Teams) in OpenCode and magpie, through the API cursor-agent talks to", "type": "module", "main": "./index.mjs", diff --git a/packages/cursor/split-calls.test.mjs b/packages/cursor/split-calls.test.mjs new file mode 100644 index 0000000..a9ed370 --- /dev/null +++ b/packages/cursor/split-calls.test.mjs @@ -0,0 +1,78 @@ +// 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 does since 7067fce5 +// (internal/gateway/split_calls_test.go). This is the request magpie +// relays from a Chat client, and what OpenCode's AI SDK sends. +import "./nonet.mjs" +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 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!" }, + ], +}) + +// trace is each message sent: its role, and the calls it makes or the +// results it carries. +const trace = (chat) => + _internal + .conversation(chat, []) + .msgs.map((b) => JSON.parse(b.toString())) + .filter((m) => m.role !== "system") + .map((m) => { + if (typeof m.content === "string") return m.role + const bits = m.content.map((p) => + p.type === "tool-call" ? "call:" + p.toolCallId : p.type === "tool-result" ? `result:${p.toolCallId}=${p.experimental_content[0].text}` : p.type + ":" + (p.text ?? ""), + ) + return `${m.role}(${bits.join(",")})` + }) + .join(" ") + +test("a split turn's calls are answered by their real results", () => { + const got = trace(split()) + expect(got).not.toContain(NO_RESULT) + expect(got).toBe("user(text:read a and b) assistant(call:toolu_a,call:toolu_b) tool(result:toolu_a=A!,result:toolu_b=B!)") +}) + +test("a call of a split turn that went unanswered is still answered for", () => { + const chat = split() + chat.messages.splice(3, 1) + expect(trace(chat)).toBe(`user(text:read a and b) assistant(call:toolu_a,call:toolu_b) tool(result:toolu_b=B!,result:toolu_a=${NO_RESULT})`) +}) + +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(trace(chat)).toBe("user(text:read a) assistant(text:reading,call:toolu_a) tool(result:toolu_a=1)") +}) + +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(trace(chat)).toBe("user(text:hi) assistant(text:one) assistant(text:two)") +}) + +test("the caller's messages are left as they were", () => { + const chat = split() + _internal.conversation(chat, []) + expect(chat.messages.length).toBe(5) + expect(chat.messages[1].tool_calls.length).toBe(1) +})