cursor 0.1.11: a turn's parallel tool calls split over two consecutive assistant messages keep their results (yetone/magpie#1275)
An agent can send one turn's tool calls in two assistant messages in a row, answered together after. conversation() answered the first message's calls at the second one: toolu_a went to Cursor as "Tool use was interrupted and did not produce a result." and its real result A! was dropped. joinSplitCalls now joins an assistant message onto an assistant message before it that made tool calls, as Anthropic reads consecutive assistant messages and as magpie's built-in does since 7067fce5, so the calls are pending together. A call of the split turn that really went unanswered is still answered for; two text-only assistant messages stay two; the caller's messages are not changed. split-calls.test.mjs is #1275's request in Chat form: 3 of its 5 tests fail on 0.1.10 (trace user assistant(toolu_a) tool(toolu_a=interrupted) assistant(toolu_b) tool(toolu_b=B!)) and pass now. HOME=$(mktemp -d) bun test (43 pass, 0 fail) and bun scripts/check.mjs pass. Not tried against a real Cursor account.
This commit is contained in:
@@ -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 ?? ""
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
Reference in New Issue
Block a user