From 273368fc0eaf2680fcceb1d4030de671ea2c2945 Mon Sep 17 00:00:00 2001 From: liyanan Date: Sun, 26 Jul 2026 15:26:13 +0800 Subject: [PATCH] fix: prevent provider_error 400 on dmodel/ultimate by using content placeholder When assistant messages contain only tool_calls (no text/thinking), transform.ts was setting content to null. Qoder's gateway drops these null-content assistant messages, orphaning the following tool_result and causing upstream rejections: - dmodel (OpenAI): 'tool must follow a message with tool_calls' - ultimate (Anthropic): 'unexpected tool_use_id in tool_result blocks' Analysis of 28 historical sessions showed 100% failure rate on dmodel and 75% on ultimate for this pattern, while auto/GLM5.2 (GLM upstream) tolerated it. Fix: inject a single-space placeholder when tool_calls are present but content is empty, so the gateway preserves the assistant message and tool pairing stays valid. Also fixes pre-existing lint formatting in stream.ts and package.json. Adds regression test for the full assistant+toolResult pairing scenario. --- .gitignore | 1 + package.json | 4 +-- src/__tests__/transform.test.ts | 43 +++++++++++++++++++++++++++++++-- src/stream.ts | 4 +-- src/transform.ts | 7 +++++- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index dd6e803..772d914 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules/ dist/ *.log .DS_Store +.pi/ diff --git a/package.json b/package.json index 72e2f2c..594a088 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "pi-provider-qoder", - "version": "0.2.9", - "description": "Pi extension for Qoder AI — with OAuth authentication, COSY signatures and WAF bypass", + "version": "0.2.9-fix.1", + "description": "Pi extension for Qoder AI \u2014 with OAuth authentication, COSY signatures and WAF bypass", "type": "module", "license": "MIT", "publishConfig": { diff --git a/src/__tests__/transform.test.ts b/src/__tests__/transform.test.ts index b3adb74..36bf99b 100644 --- a/src/__tests__/transform.test.ts +++ b/src/__tests__/transform.test.ts @@ -222,7 +222,11 @@ describe("transformMessagesForQoder", () => { }); }); - it("sets content to null for assistant messages with empty content", () => { + it("uses a placeholder content for assistant messages with only tool calls (gateway workaround)", () => { + // Regression: Qoder's gateway drops assistant messages with content:null, + // which orphans the following tool_result and causes dmodel/ultimate to + // reject the request with provider_error 400. Content must be non-null + // whenever tool_calls are present. const msgs = [ { role: "assistant", @@ -238,7 +242,42 @@ describe("transformMessagesForQoder", () => { ] as unknown as Message[]; const result = transformMessagesForQoder(msgs); const msg0 = result[0] as { role: string; content: unknown; tool_calls?: unknown[] }; - expect(msg0.content).toBeNull(); + expect(msg0.content).not.toBeNull(); + expect(typeof msg0.content).toBe("string"); expect(msg0.tool_calls).toHaveLength(1); }); + + it("preserves tool_call_id pairing across assistant+toolResult when assistant has only tool calls", () => { + // End-to-end regression: a toolCall-only assistant followed by a toolResult + // must produce a valid OpenAI message sequence that upstreams accept. + const msgs = [ + { role: "user", content: "do it" }, + { + role: "assistant", + content: [{ type: "toolCall", id: "call_abc123", name: "bash", arguments: { command: "ls" } }], + }, + { + role: "toolResult", + toolCallId: "call_abc123", + content: "file1\nfile2", + }, + ] as unknown as Message[]; + const result = transformMessagesForQoder(msgs); + + const asst = result[1] as { + role: string; + content: unknown; + tool_calls?: Array<{ id: string; type: string; function: { name: string; arguments: string } }>; + }; + const tool = result[2] as { role: string; tool_call_id: string; content: string }; + + // Assistant must keep the message (non-null content) so the tool pairs up. + expect(asst.content).not.toBeNull(); + expect(asst.tool_calls).toHaveLength(1); + expect(asst.tool_calls?.[0].id).toBe("call_abc123"); + + // Tool result must reference the assistant's tool_call id. + expect(tool.role).toBe("tool"); + expect(tool.tool_call_id).toBe("call_abc123"); + }); }); diff --git a/src/stream.ts b/src/stream.ts index 920afc0..171d7da 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -193,9 +193,7 @@ export function streamQoder( // model never sees it). Inject the system prompt as a leading // role:system message instead, which the server does honor. system: "", - messages: systemText - ? [{ role: "system", content: systemText }, ...normalizedMessages] - : normalizedMessages, + messages: systemText ? [{ role: "system", content: systemText }, ...normalizedMessages] : normalizedMessages, tools: toolsRaw || [], parameters: { max_tokens: maxTokens }, chat_context: { diff --git a/src/transform.ts b/src/transform.ts index 810c233..3ccf014 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -135,9 +135,14 @@ export function transformMessagesForQoder(messages: Message[]): QoderMessage[] { content = am.content || ""; } + // Qoder's gateway drops assistant messages whose content is null, which + // orphans the following tool_result and makes dmodel/ultimate upstreams + // reject the request ("tool must follow a message with tool_calls"). + // When an assistant turn has tool calls but no text/thinking, inject a + // single-space placeholder so the gateway keeps the message. const mapped: QoderMessage = { role: "assistant", - content: content || null, + content: content || (toolCalls.length > 0 ? " " : null), }; if (toolCalls.length > 0) { mapped.tool_calls = toolCalls;