diff --git a/packages/cursor/conversation.test.mjs b/packages/cursor/conversation.test.mjs index 3915de1..bd13aa8 100644 --- a/packages/cursor/conversation.test.mjs +++ b/packages/cursor/conversation.test.mjs @@ -173,3 +173,37 @@ test("usage takes the cache out of input_tokens, and keeps reasoning", async () expect(usage.prompt_tokens).toBeGreaterThan(90) expect(usage.prompt_tokens_details).toEqual({ cached_tokens: 0, cache_write_tokens: 0 }) }) + +// GLM-5.3 called the catalog's entries bare, as tools of +// their own (#11): each entry is the CallDynamicTool call itself +test("the tool catalog writes each tool as its CallDynamicTool call", () => { + const tools = [ + { name: "Read", description: "read a file", schemaText: '{"type":"object"}' }, + { name: "Bash", description: "run a command", schemaText: '{"type":"object"}' }, + ] + const s = _internal.catalog(tools) + expect(s).not.toContain("`) + } + expect(_internal.catalog([])).toBe("") +}) + +// a turn of thinking only is an empty reply, which the client asks again +test("a turn that ends with thinking only is an empty reply", async () => { + const update = (num, body) => ({ data: pb().bytes(1, pb().bytes(num, body)).done() }) + const run = async (...frames) => { + const out = [] + for await (const p of _internal.decode(frames, { send() {}, blobs: new Map(), tools: [], estimate: 1 })) out.push(p) + return out + } + const empty = { error: { status: 502, message: "an empty reply" } } + const thinking = update(4, pb().str(1, "hmm").done()) + const ended = update(14, pb().varint(2, 3).done()) + expect(await run(thinking, ended)).toEqual([{ reasoning: "hmm" }, empty]) + // and so at the stream's end + expect(await run(thinking, { end: true, data: Buffer.from("{}") })).toEqual([{ reasoning: "hmm" }, empty]) + // with text it finishes + const out = await run(thinking, update(1, pb().str(1, "hi").done()), ended) + expect(out.at(-1).stop).toBe("stop") +}) diff --git a/packages/cursor/index.mjs b/packages/cursor/index.mjs index 60eb622..2f94573 100644 --- a/packages/cursor/index.mjs +++ b/packages/cursor/index.mjs @@ -981,11 +981,14 @@ function toolsOf(chat) { // catalog lists the caller's tools for the model. It sees MCP tools only // through Cursor's GetDynamicTools and CallDynamicTool, and Cursor names // them in a prompt of its own, which this conversation goes without: -// without the list the model says it has no such tool. +// without the list the model says it has no such tool. Each entry is +// written as the CallDynamicTool call itself: listed as , +// some models (GLM-5.3) called the names bare, as tools of their own, and +// those calls went nowhere (#11). function catalog(tools) { if (!tools.length) return "" - let s = `\n\n\nThe tools below are available in the MCP namespace "magpie". Call one with \`${CALL}\` (namespace "magpie", toolName, arguments). Their schemas are given here, so there is no need to call \`GetDynamicTools\` first.\n` - for (const t of tools) s += `\n${t.description}\ninput schema: ${t.schemaText}\n\n` + let s = `\n\n\nThe MCP namespace "magpie" has the tools below. They are reached only through \`${CALL}\`, never as tools of their own. Each entry is the exact call to make; its arguments must match the schema given. The schemas are complete, so there is no need to call \`GetDynamicTools\` first.\n` + for (const t of tools) s += `${CALL}({"namespace":"magpie","toolName":"${t.name}","arguments":{...}})\n${t.description}\narguments schema: ${t.schemaText}\n\n` return s + "" } @@ -1345,6 +1348,8 @@ async function* decode(stream, { send, blobs, tools, estimate }) { let calls = 0 let listed = 0 let said = 0 + // wrote: the reply's text alone; a turn of thinking only says nothing + let wrote = 0 let usage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, reasoning: 0 } const finish = () => { if (!usage.output) usage.output = Math.floor((said + 3) / 4) @@ -1369,7 +1374,7 @@ async function* decode(stream, { send, blobs, tools, estimate }) { if (f.end) { const e = failure(200, f.data) if (e.status < 200 || e.status > 299) yield { error: e } - else if (said === 0 && calls === 0) yield { error: { status: 502, message: "an empty reply" } } + else if (wrote === 0 && calls === 0) yield { error: { status: 502, message: "an empty reply" } } else yield finish() return } @@ -1384,6 +1389,7 @@ async function* decode(stream, { send, blobs, tools, estimate }) { const t = pbStr(uf, 1) if (t) { said += t.length + wrote += t.length yield { text: t } } break @@ -1400,7 +1406,9 @@ async function* decode(stream, { send, blobs, tools, estimate }) { case 14: // turn ended, with what it used usage = usageOf(uf) if (calls === 0) { - yield finish() + // nothing written and nothing called: an empty reply, + // which the client asks again, not a finished turn + yield wrote === 0 ? { error: { status: 502, message: "an empty reply" } } : finish() return } break @@ -1725,4 +1733,4 @@ export async function CursorAuthPlugin() { } // for tests -export const _internal = { usable, maxRequired, conversationID, usageOf, sessionOf, SESSION, errorResponse, kept, poolBase, splitID, families, byEffort, offered, modelID, conversation, buildRun, decode, exec, fields, pb, pbValue, pbAny, frame, frames, failure, toolsOf, expiry } +export const _internal = { catalog, usable, maxRequired, conversationID, usageOf, sessionOf, SESSION, errorResponse, kept, poolBase, splitID, families, byEffort, offered, modelID, conversation, buildRun, decode, exec, fields, pb, pbValue, pbAny, frame, frames, failure, toolsOf, expiry } diff --git a/packages/cursor/package.json b/packages/cursor/package.json index a2c1816..2df0591 100644 --- a/packages/cursor/package.json +++ b/packages/cursor/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-cursor-auth", - "version": "0.1.7", + "version": "0.1.8", "description": "Cursor subscriptions (Pro, Pro+, Ultra, Teams) in OpenCode and magpie, through the API cursor-agent talks to", "type": "module", "main": "./index.mjs",