Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion internal/protocol/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 {
Expand Down
114 changes: 114 additions & 0 deletions internal/protocol/request_test.go
Original file line number Diff line number Diff line change
@@ -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
}