openai: add tool call appending to previous assistant message (#13434)

* openai: add tool call appending to previous asst message

* add tests for thinking appending
This commit is contained in:
Parth Sareen
2025-12-11 17:30:12 -08:00
committed by GitHub
parent 93d45d7a04
commit 9b2035d194
2 changed files with 324 additions and 14 deletions

View File

@@ -365,22 +365,33 @@ func FromResponsesRequest(r ResponsesRequest) (*api.ChatRequest, error) {
return nil, fmt.Errorf("failed to parse function call arguments: %w", err)
}
}
msg := api.Message{
Role: "assistant",
ToolCalls: []api.ToolCall{{
ID: v.CallID,
Function: api.ToolCallFunction{
Name: v.Name,
Arguments: args,
},
}},
toolCall := api.ToolCall{
ID: v.CallID,
Function: api.ToolCallFunction{
Name: v.Name,
Arguments: args,
},
}
// Attach pending thinking
if pendingThinking != "" {
msg.Thinking = pendingThinking
pendingThinking = ""
// Merge tool call into existing assistant message if it has content or tool calls
if len(messages) > 0 && messages[len(messages)-1].Role == "assistant" {
lastMsg := &messages[len(messages)-1]
lastMsg.ToolCalls = append(lastMsg.ToolCalls, toolCall)
if pendingThinking != "" {
lastMsg.Thinking = pendingThinking
pendingThinking = ""
}
} else {
msg := api.Message{
Role: "assistant",
ToolCalls: []api.ToolCall{toolCall},
}
if pendingThinking != "" {
msg.Thinking = pendingThinking
pendingThinking = ""
}
messages = append(messages, msg)
}
messages = append(messages, msg)
case ResponsesFunctionCallOutput:
messages = append(messages, api.Message{
Role: "tool",