From 4e717142692b2b3b8ef2c833d00e0a8015a34259 Mon Sep 17 00:00:00 2001 From: billyyi Date: Sun, 15 Nov 2026 02:29:39 +0800 Subject: [PATCH] fix(protocol): merge a Responses assistant message item into the trailing assistant turn A Responses turn can carry its assistant text in a message item that follows the function_call items of the same turn. decodeBridgeRequest appended that item as a new bridge message, so encodeChatRequest saw an assistant message while the tool calls it belongs to were still waiting for their results and rejected the request with: messages[N]: assistant message appears before tool results for ... The text now joins the trailing assistant message, which is what the Chat and Anthropic encoders can express: one assistant turn holding content and tool_calls, followed by the tool results. --- internal/protocol/request.go | 20 +++++- internal/protocol/request_test.go | 114 ++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 internal/protocol/request_test.go diff --git a/internal/protocol/request.go b/internal/protocol/request.go index 9ba6a18a..e6e560a3 100644 --- a/internal/protocol/request.go +++ b/internal/protocol/request.go @@ -540,7 +540,9 @@ func decodeBridgeRequest(protocol Protocol, input map[string]any) (bridgeRequest request.System = append(request.System, blocks...) } else if role == "developer" { request.Developer = append(request.Developer, blocks...) - } else if role == "user" || role == "assistant" { + } else if role == "assistant" { + appendAssistantMessage(&request.Messages, blocks) + } else if role == "user" { request.Messages = append(request.Messages, bridgeMessage{Role: role, Blocks: blocks}) } default: @@ -674,6 +676,22 @@ func appendBridgeBlock(messages *[]bridgeMessage, role string, block bridgeBlock *messages = append(*messages, bridgeMessage{Role: role, Blocks: []bridgeBlock{block}}) } +// appendAssistantMessage adds the blocks of a Responses message item with the +// assistant role. A turn may carry its text in an item that follows the +// function_call items of the same turn, and the Chat and Anthropic encoders +// cannot express an assistant message that interrupts its own tool calls, so +// the text joins the trailing assistant message instead of starting a new one. +func appendAssistantMessage(messages *[]bridgeMessage, blocks []bridgeBlock) { + if len(blocks) > 0 && len(*messages) > 0 { + last := &(*messages)[len(*messages)-1] + if last.Role == "assistant" { + last.Blocks = append(last.Blocks, blocks...) + return + } + } + *messages = append(*messages, bridgeMessage{Role: "assistant", Blocks: blocks}) +} + // bridgeBlocksOnlyTools reports whether blocks is non-empty and holds only // tool calls and tool results. func bridgeBlocksOnlyTools(blocks []bridgeBlock) bool { diff --git a/internal/protocol/request_test.go b/internal/protocol/request_test.go new file mode 100644 index 00000000..57b742f6 --- /dev/null +++ b/internal/protocol/request_test.go @@ -0,0 +1,114 @@ +package protocol + +import ( + "encoding/json" + "testing" +) + +// A Responses turn can carry its assistant text in a message item that follows +// the function_call items of the same turn. Bridging that to Chat must keep the +// text and the calls in one assistant message: an assistant message between the +// calls and their results is not representable and was rejected as +// "assistant message appears before tool results". +func TestDecodeResponsesAssistantTextAfterToolCalls(t *testing.T) { + input := map[string]any{ + "model": "deepseek-v4.1-flash", + "input": []any{ + map[string]any{"type": "message", "role": "user", "content": []any{ + map[string]any{"type": "input_text", "text": "list the docker dir"}, + }}, + map[string]any{"type": "function_call", "call_id": "call_a", "name": "shell", "arguments": `{"command":"ls"}`}, + map[string]any{"type": "function_call", "call_id": "call_b", "name": "shell", "arguments": `{"command":"pwd"}`}, + map[string]any{"type": "message", "role": "assistant", "content": []any{ + map[string]any{"type": "output_text", "text": "I will inspect the directory first."}, + }}, + map[string]any{"type": "function_call_output", "call_id": "call_a", "output": "a b c"}, + map[string]any{"type": "function_call_output", "call_id": "call_b", "output": "/tmp"}, + map[string]any{"type": "message", "role": "user", "content": []any{ + map[string]any{"type": "input_text", "text": "continue"}, + }}, + }, + } + request, err := decodeBridgeRequest(Responses, input) + if err != nil { + t.Fatalf("decode: %v", err) + } + if len(request.Messages) != 4 { + t.Fatalf("got %d bridge messages, want 4", len(request.Messages)) + } + assistant := request.Messages[1] + if assistant.Role != "assistant" { + t.Fatalf("messages[1] role = %q, want assistant", assistant.Role) + } + var calls, texts int + for _, block := range assistant.Blocks { + switch block.Kind { + case "tool_call": + calls++ + case "text": + texts++ + } + } + if calls != 2 || texts != 1 { + t.Fatalf("assistant blocks = %d calls / %d texts, want 2 / 1", calls, texts) + } + if _, err := encodeBridgeRequest(Chat, request); err != nil { + t.Fatalf("encode chat: %v", err) + } +} + +// The text of a later assistant turn still starts its own message. +func TestDecodeResponsesAssistantTextAfterToolResults(t *testing.T) { + input := map[string]any{ + "model": "deepseek-v4.1-flash", + "input": []any{ + map[string]any{"type": "message", "role": "user", "content": []any{ + map[string]any{"type": "input_text", "text": "list the docker dir"}, + }}, + map[string]any{"type": "function_call", "call_id": "call_a", "name": "shell", "arguments": `{"command":"ls"}`}, + map[string]any{"type": "function_call_output", "call_id": "call_a", "output": "a b c"}, + map[string]any{"type": "message", "role": "assistant", "content": []any{ + map[string]any{"type": "output_text", "text": "The directory holds three files."}, + }}, + }, + } + request, err := decodeBridgeRequest(Responses, input) + if err != nil { + t.Fatalf("decode: %v", err) + } + if len(request.Messages) != 4 { + t.Fatalf("got %d bridge messages, want 4", len(request.Messages)) + } + body, err := encodeBridgeRequest(Chat, request) + if err != nil { + t.Fatalf("encode chat: %v", err) + } + encoded, err := json.Marshal(body) + if err != nil { + t.Fatalf("marshal: %v", err) + } + if got := countOccurrences(string(encoded), `"tool_calls"`); got != 1 { + t.Fatalf("tool_calls appears %d times, want 1: %s", got, encoded) + } +} + +func countOccurrences(haystack, needle string) int { + count := 0 + for { + index := indexOf(haystack, needle) + if index < 0 { + return count + } + count++ + haystack = haystack[index+len(needle):] + } +} + +func indexOf(haystack, needle string) int { + for i := 0; i+len(needle) <= len(haystack); i++ { + if haystack[i:i+len(needle)] == needle { + return i + } + } + return -1 +}