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.
This commit is contained in:
liyanan
2026-07-26 15:26:13 +08:00
parent 6964615056
commit 273368fc0e
5 changed files with 51 additions and 8 deletions
+41 -2
View File
@@ -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");
});
});
+1 -3
View File
@@ -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: {
+6 -1
View File
@@ -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;