mirror of
https://github.com/Finesssee/ProxyPilot.git
synced 2026-11-19 02:17:46 +00:00
Fix tool result ordering for Droid CLI
This commit is contained in:
@@ -77,6 +77,11 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
|
||||
// Ensure max_tokens > thinking.budget_tokens when thinking is enabled
|
||||
body = ensureMaxTokensForThinking(req.Model, body)
|
||||
|
||||
// Claude Messages API requires that all tool_result blocks corresponding to an assistant
|
||||
// tool_use turn appear in the immediately following message. Some clients provide multiple
|
||||
// tool_result-only user messages; merge them into the first following message when needed.
|
||||
body = util.NormalizeClaudeToolResults(body)
|
||||
|
||||
// Extract betas from body and convert to header
|
||||
var extraBetas []string
|
||||
extraBetas, body = extractAndRemoveBetas(body)
|
||||
@@ -188,6 +193,9 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
|
||||
// Ensure max_tokens > thinking.budget_tokens when thinking is enabled
|
||||
body = ensureMaxTokensForThinking(req.Model, body)
|
||||
|
||||
// Claude Messages API tool_result adjacency normalization (see Execute).
|
||||
body = util.NormalizeClaudeToolResults(body)
|
||||
|
||||
// Extract betas from body and convert to header
|
||||
var extraBetas []string
|
||||
extraBetas, body = extractAndRemoveBetas(body)
|
||||
@@ -755,3 +763,5 @@ func checkSystemInstructions(payload []byte) []byte {
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
// Tool-result adjacency normalization is implemented in internal/util.
|
||||
|
||||
@@ -135,10 +135,47 @@ func ConvertOpenAIRequestToClaude(modelName string, inputRawJSON []byte, stream
|
||||
var toolCallIDs []string // Track tool call IDs for matching with tool results
|
||||
|
||||
if messages := root.Get("messages"); messages.Exists() && messages.IsArray() {
|
||||
messages.ForEach(func(_, message gjson.Result) bool {
|
||||
msgs := messages.Array()
|
||||
for i := 0; i < len(msgs); i++ {
|
||||
message := msgs[i]
|
||||
role := message.Get("role").String()
|
||||
contentResult := message.Get("content")
|
||||
|
||||
if role == "tool" {
|
||||
// Group consecutive tool messages into a single user message so Claude receives
|
||||
// all tool_result blocks immediately after the assistant's tool_use blocks.
|
||||
toolResultMsg := map[string]interface{}{
|
||||
"role": "user",
|
||||
"content": []interface{}{},
|
||||
}
|
||||
|
||||
for i < len(msgs) && msgs[i].Get("role").String() == "tool" {
|
||||
toolCallID := msgs[i].Get("tool_call_id").String()
|
||||
contentValue := msgs[i].Get("content")
|
||||
|
||||
var content string
|
||||
switch {
|
||||
case contentValue.Exists() && contentValue.Type == gjson.String:
|
||||
content = contentValue.String()
|
||||
case contentValue.Exists() && contentValue.Raw != "" && contentValue.Raw != "null":
|
||||
content = contentValue.Raw
|
||||
default:
|
||||
content = ""
|
||||
}
|
||||
|
||||
toolResultMsg["content"] = append(toolResultMsg["content"].([]interface{}), map[string]interface{}{
|
||||
"type": "tool_result",
|
||||
"tool_use_id": toolCallID,
|
||||
"content": content,
|
||||
})
|
||||
i++
|
||||
}
|
||||
i-- // compensate for the for-loop increment
|
||||
|
||||
anthropicMessages = append(anthropicMessages, toolResultMsg)
|
||||
continue
|
||||
}
|
||||
|
||||
contentResult := message.Get("content")
|
||||
switch role {
|
||||
case "system", "user", "assistant":
|
||||
// Create Claude Code message with appropriate role mapping
|
||||
@@ -254,29 +291,9 @@ func ConvertOpenAIRequestToClaude(modelName string, inputRawJSON []byte, stream
|
||||
msg["content"] = contentParts
|
||||
}
|
||||
|
||||
anthropicMessages = append(anthropicMessages, msg)
|
||||
|
||||
case "tool":
|
||||
// Handle tool result messages conversion
|
||||
toolCallID := message.Get("tool_call_id").String()
|
||||
content := message.Get("content").String()
|
||||
|
||||
// Create tool result message in Claude Code format
|
||||
msg := map[string]interface{}{
|
||||
"role": "user",
|
||||
"content": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "tool_result",
|
||||
"tool_use_id": toolCallID,
|
||||
"content": content,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
anthropicMessages = append(anthropicMessages, msg)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Set messages in the output template
|
||||
|
||||
@@ -133,14 +133,18 @@ func ConvertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte
|
||||
|
||||
// input array processing
|
||||
if input := root.Get("input"); input.Exists() && input.IsArray() {
|
||||
input.ForEach(func(_, item gjson.Result) bool {
|
||||
items := input.Array()
|
||||
for i := 0; i < len(items); i++ {
|
||||
item := items[i]
|
||||
if extractedFromSystem && strings.EqualFold(item.Get("role").String(), "system") {
|
||||
return true
|
||||
continue
|
||||
}
|
||||
|
||||
typ := item.Get("type").String()
|
||||
if typ == "" && item.Get("role").String() != "" {
|
||||
typ = "message"
|
||||
}
|
||||
|
||||
switch typ {
|
||||
case "message":
|
||||
// Determine role and construct Claude-compatible content parts.
|
||||
@@ -238,39 +242,69 @@ func ConvertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte
|
||||
}
|
||||
|
||||
case "function_call":
|
||||
// Map to assistant tool_use
|
||||
callID := item.Get("call_id").String()
|
||||
if callID == "" {
|
||||
callID = genToolCallID()
|
||||
}
|
||||
name := item.Get("name").String()
|
||||
argsStr := item.Get("arguments").String()
|
||||
|
||||
toolUse := `{"type":"tool_use","id":"","name":"","input":{}}`
|
||||
toolUse, _ = sjson.Set(toolUse, "id", callID)
|
||||
toolUse, _ = sjson.Set(toolUse, "name", name)
|
||||
if argsStr != "" && gjson.Valid(argsStr) {
|
||||
toolUse, _ = sjson.SetRaw(toolUse, "input", argsStr)
|
||||
}
|
||||
|
||||
// Group consecutive function_call items into a single assistant message so Claude receives
|
||||
// all tool_use blocks together (and expects tool_result blocks in the next message).
|
||||
asst := `{"role":"assistant","content":[]}`
|
||||
asst, _ = sjson.SetRaw(asst, "content.-1", toolUse)
|
||||
for ; i < len(items); i++ {
|
||||
callItem := items[i]
|
||||
if extractedFromSystem && strings.EqualFold(callItem.Get("role").String(), "system") {
|
||||
break
|
||||
}
|
||||
t := callItem.Get("type").String()
|
||||
if t == "" && callItem.Get("role").String() != "" {
|
||||
t = "message"
|
||||
}
|
||||
if t != "function_call" {
|
||||
break
|
||||
}
|
||||
|
||||
callID := callItem.Get("call_id").String()
|
||||
if callID == "" {
|
||||
callID = genToolCallID()
|
||||
}
|
||||
name := callItem.Get("name").String()
|
||||
argsStr := callItem.Get("arguments").String()
|
||||
|
||||
toolUse := `{"type":"tool_use","id":"","name":"","input":{}}`
|
||||
toolUse, _ = sjson.Set(toolUse, "id", callID)
|
||||
toolUse, _ = sjson.Set(toolUse, "name", name)
|
||||
if argsStr != "" && gjson.Valid(argsStr) {
|
||||
toolUse, _ = sjson.SetRaw(toolUse, "input", argsStr)
|
||||
}
|
||||
|
||||
asst, _ = sjson.SetRaw(asst, "content.-1", toolUse)
|
||||
}
|
||||
i-- // compensate for the for-loop increment
|
||||
out, _ = sjson.SetRaw(out, "messages.-1", asst)
|
||||
|
||||
case "function_call_output":
|
||||
// Map to user tool_result
|
||||
callID := item.Get("call_id").String()
|
||||
outputStr := item.Get("output").String()
|
||||
toolResult := `{"type":"tool_result","tool_use_id":"","content":""}`
|
||||
toolResult, _ = sjson.Set(toolResult, "tool_use_id", callID)
|
||||
toolResult, _ = sjson.Set(toolResult, "content", outputStr)
|
||||
|
||||
// Group consecutive outputs into a single user message containing multiple tool_result blocks.
|
||||
usr := `{"role":"user","content":[]}`
|
||||
usr, _ = sjson.SetRaw(usr, "content.-1", toolResult)
|
||||
for ; i < len(items); i++ {
|
||||
outItem := items[i]
|
||||
if extractedFromSystem && strings.EqualFold(outItem.Get("role").String(), "system") {
|
||||
break
|
||||
}
|
||||
t := outItem.Get("type").String()
|
||||
if t == "" && outItem.Get("role").String() != "" {
|
||||
t = "message"
|
||||
}
|
||||
if t != "function_call_output" {
|
||||
break
|
||||
}
|
||||
|
||||
callID := outItem.Get("call_id").String()
|
||||
outputStr := outItem.Get("output").String()
|
||||
toolResult := `{"type":"tool_result","tool_use_id":"","content":""}`
|
||||
toolResult, _ = sjson.Set(toolResult, "tool_use_id", callID)
|
||||
toolResult, _ = sjson.Set(toolResult, "content", outputStr)
|
||||
|
||||
usr, _ = sjson.SetRaw(usr, "content.-1", toolResult)
|
||||
}
|
||||
i-- // compensate for the for-loop increment
|
||||
out, _ = sjson.SetRaw(out, "messages.-1", usr)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// tools mapping: parameters -> input_schema
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NormalizeClaudeToolResults ensures Claude Messages-style payloads satisfy the constraint that
|
||||
// tool_result blocks for an assistant tool_use turn appear in the immediately following message.
|
||||
//
|
||||
// This is primarily a resilience feature for clients that may insert user text messages between
|
||||
// tool_use and tool_result (or send tool_result blocks later). When possible, it moves matching
|
||||
// tool_result blocks into a tool_result-only user message directly after the corresponding assistant.
|
||||
func NormalizeClaudeToolResults(body []byte) []byte {
|
||||
var root map[string]any
|
||||
if err := json.Unmarshal(body, &root); err != nil {
|
||||
return body
|
||||
}
|
||||
rawMsgs, ok := root["messages"].([]any)
|
||||
if !ok || len(rawMsgs) == 0 {
|
||||
return body
|
||||
}
|
||||
|
||||
changed := false
|
||||
|
||||
for i := 0; i < len(rawMsgs); i++ {
|
||||
msg, ok := rawMsgs[i].(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if getClaudeRole(msg) != "assistant" {
|
||||
continue
|
||||
}
|
||||
|
||||
toolUseIDs := collectClaudeToolUseIDs(msg)
|
||||
if len(toolUseIDs) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
changedBeforeTurn := changed
|
||||
|
||||
insertAt := i + 1
|
||||
if insertAt > len(rawMsgs) {
|
||||
insertAt = len(rawMsgs)
|
||||
}
|
||||
|
||||
var toolResultMsg map[string]any
|
||||
insertedThisTurn := false
|
||||
if insertAt < len(rawMsgs) {
|
||||
if candidate, ok := rawMsgs[insertAt].(map[string]any); ok && isClaudeUserToolResultOnly(candidate) {
|
||||
toolResultMsg = candidate
|
||||
}
|
||||
}
|
||||
if toolResultMsg == nil {
|
||||
toolResultMsg = map[string]any{"role": "user", "content": []any{}}
|
||||
rawMsgs = append(rawMsgs, nil)
|
||||
copy(rawMsgs[insertAt+1:], rawMsgs[insertAt:])
|
||||
rawMsgs[insertAt] = toolResultMsg
|
||||
insertedThisTurn = true
|
||||
changed = true
|
||||
i++
|
||||
}
|
||||
|
||||
j := insertAt + 1
|
||||
for j < len(rawMsgs) {
|
||||
userMsg, ok := rawMsgs[j].(map[string]any)
|
||||
if !ok || getClaudeRole(userMsg) != "user" {
|
||||
j++
|
||||
continue
|
||||
}
|
||||
|
||||
moved, kept, okParts := splitClaudeToolResults(userMsg, toolUseIDs)
|
||||
if !okParts || len(moved) == 0 {
|
||||
j++
|
||||
continue
|
||||
}
|
||||
|
||||
appendClaudeToolResults(toolResultMsg, moved)
|
||||
if len(kept) == 0 {
|
||||
rawMsgs = append(rawMsgs[:j], rawMsgs[j+1:]...)
|
||||
changed = true
|
||||
continue
|
||||
}
|
||||
userMsg["content"] = kept
|
||||
rawMsgs[j] = userMsg
|
||||
changed = true
|
||||
j++
|
||||
}
|
||||
|
||||
if insertedThisTurn {
|
||||
if content, ok := toolResultMsg["content"].([]any); ok && len(content) == 0 {
|
||||
if insertAt < len(rawMsgs) {
|
||||
rawMsgs = append(rawMsgs[:insertAt], rawMsgs[insertAt+1:]...)
|
||||
changed = changedBeforeTurn
|
||||
i--
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !changed {
|
||||
return body
|
||||
}
|
||||
|
||||
root["messages"] = rawMsgs
|
||||
updated, err := json.Marshal(root)
|
||||
if err != nil {
|
||||
return body
|
||||
}
|
||||
return updated
|
||||
}
|
||||
|
||||
func getClaudeRole(msg map[string]any) string {
|
||||
if msg == nil {
|
||||
return ""
|
||||
}
|
||||
if v, ok := msg["role"].(string); ok {
|
||||
return strings.TrimSpace(v)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func collectClaudeToolUseIDs(msg map[string]any) map[string]struct{} {
|
||||
out := make(map[string]struct{})
|
||||
if msg == nil {
|
||||
return out
|
||||
}
|
||||
content, ok := msg["content"].([]any)
|
||||
if !ok {
|
||||
return out
|
||||
}
|
||||
for _, partAny := range content {
|
||||
part, ok := partAny.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if typ, _ := part["type"].(string); typ != "tool_use" {
|
||||
continue
|
||||
}
|
||||
if id, _ := part["id"].(string); strings.TrimSpace(id) != "" {
|
||||
out[strings.TrimSpace(id)] = struct{}{}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func isClaudeUserToolResultOnly(msg map[string]any) bool {
|
||||
if getClaudeRole(msg) != "user" {
|
||||
return false
|
||||
}
|
||||
content, ok := msg["content"].([]any)
|
||||
if !ok || len(content) == 0 {
|
||||
return false
|
||||
}
|
||||
for _, partAny := range content {
|
||||
part, ok := partAny.(map[string]any)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if typ, _ := part["type"].(string); typ != "tool_result" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func splitClaudeToolResults(msg map[string]any, ids map[string]struct{}) (moved []any, kept []any, ok bool) {
|
||||
content, ok := msg["content"].([]any)
|
||||
if !ok {
|
||||
return nil, nil, false
|
||||
}
|
||||
for _, partAny := range content {
|
||||
part, okPart := partAny.(map[string]any)
|
||||
if !okPart {
|
||||
kept = append(kept, partAny)
|
||||
continue
|
||||
}
|
||||
typ, _ := part["type"].(string)
|
||||
if typ != "tool_result" {
|
||||
kept = append(kept, partAny)
|
||||
continue
|
||||
}
|
||||
id, _ := part["tool_use_id"].(string)
|
||||
id = strings.TrimSpace(id)
|
||||
if id != "" {
|
||||
if _, match := ids[id]; match {
|
||||
moved = append(moved, partAny)
|
||||
continue
|
||||
}
|
||||
}
|
||||
kept = append(kept, partAny)
|
||||
}
|
||||
return moved, kept, true
|
||||
}
|
||||
|
||||
func appendClaudeToolResults(msg map[string]any, parts []any) {
|
||||
if msg == nil || len(parts) == 0 {
|
||||
return
|
||||
}
|
||||
content, ok := msg["content"].([]any)
|
||||
if !ok {
|
||||
content = []any{}
|
||||
}
|
||||
content = append(content, parts...)
|
||||
msg["content"] = content
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func TestNormalizeClaudeToolResults_InsertsToolResultMessageImmediatelyAfterToolUse(t *testing.T) {
|
||||
in := []byte(`{
|
||||
"model":"gemini-claude-opus-4-5-thinking",
|
||||
"max_tokens":64,
|
||||
"messages":[
|
||||
{"role":"user","content":"hi"},
|
||||
{"role":"assistant","content":[{"type":"tool_use","id":"call_a","name":"a","input":{}}]},
|
||||
{"role":"user","content":"(oops user text before tool_result)"},
|
||||
{"role":"user","content":[{"type":"tool_result","tool_use_id":"call_a","content":"outA"}]},
|
||||
{"role":"user","content":"continue"}
|
||||
]
|
||||
}`)
|
||||
|
||||
out := NormalizeClaudeToolResults(in)
|
||||
|
||||
if got := gjson.GetBytes(out, "messages.#").Int(); got != 5 {
|
||||
t.Fatalf("expected 5 messages after normalization, got %d body=%s", got, string(out))
|
||||
}
|
||||
if gjson.GetBytes(out, "messages.2.role").String() != "user" {
|
||||
t.Fatalf("expected messages.2.role=user body=%s", string(out))
|
||||
}
|
||||
if gjson.GetBytes(out, "messages.2.content.0.type").String() != "tool_result" {
|
||||
t.Fatalf("expected messages.2.content[0].type=tool_result body=%s", string(out))
|
||||
}
|
||||
if gjson.GetBytes(out, "messages.2.content.0.tool_use_id").String() != "call_a" {
|
||||
t.Fatalf("expected messages.2.content[0].tool_use_id=call_a body=%s", string(out))
|
||||
}
|
||||
if gjson.GetBytes(out, "messages.3.content").String() != "(oops user text before tool_result)" {
|
||||
t.Fatalf("expected messages.3.content to retain user text body=%s", string(out))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NormalizeOpenAIResponsesToolOrder reorders OpenAI Responses API `input` items so that any
|
||||
// `function_call_output` items for a run of `function_call` items are pulled immediately after
|
||||
// that run. This helps downstream providers that require tool outputs to immediately follow tool calls.
|
||||
func NormalizeOpenAIResponsesToolOrder(body []byte) []byte {
|
||||
var root map[string]any
|
||||
if err := json.Unmarshal(body, &root); err != nil {
|
||||
return body
|
||||
}
|
||||
|
||||
rawInput, ok := root["input"].([]any)
|
||||
if !ok || len(rawInput) == 0 {
|
||||
return body
|
||||
}
|
||||
|
||||
skip := make([]bool, len(rawInput))
|
||||
out := make([]any, 0, len(rawInput))
|
||||
changed := false
|
||||
|
||||
itemType := func(item any) string {
|
||||
obj, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
if t, ok := obj["type"].(string); ok && strings.TrimSpace(t) != "" {
|
||||
return strings.TrimSpace(t)
|
||||
}
|
||||
if r, ok := obj["role"].(string); ok && strings.TrimSpace(r) != "" {
|
||||
return "message"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
callIDOf := func(item any) string {
|
||||
obj, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
if v, ok := obj["call_id"].(string); ok {
|
||||
return strings.TrimSpace(v)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
for i := 0; i < len(rawInput); i++ {
|
||||
if skip[i] {
|
||||
continue
|
||||
}
|
||||
|
||||
if itemType(rawInput[i]) != "function_call" {
|
||||
out = append(out, rawInput[i])
|
||||
skip[i] = true
|
||||
continue
|
||||
}
|
||||
|
||||
callItems := make([]any, 0, 4)
|
||||
callIDs := make(map[string]struct{})
|
||||
callIDByIdx := make(map[int]string)
|
||||
j := i
|
||||
for j < len(rawInput) {
|
||||
if skip[j] {
|
||||
j++
|
||||
continue
|
||||
}
|
||||
if itemType(rawInput[j]) != "function_call" {
|
||||
break
|
||||
}
|
||||
callItems = append(callItems, rawInput[j])
|
||||
skip[j] = true
|
||||
if id := callIDOf(rawInput[j]); id != "" {
|
||||
callIDs[id] = struct{}{}
|
||||
callIDByIdx[len(callItems)-1] = id
|
||||
}
|
||||
j++
|
||||
}
|
||||
|
||||
foundOutputs := make(map[string]struct{})
|
||||
pulledOutputs := make([]any, 0, 4)
|
||||
k := j
|
||||
for k < len(rawInput) {
|
||||
if skip[k] {
|
||||
k++
|
||||
continue
|
||||
}
|
||||
if itemType(rawInput[k]) == "function_call_output" {
|
||||
if id := callIDOf(rawInput[k]); id != "" {
|
||||
if _, ok := callIDs[id]; ok {
|
||||
pulledOutputs = append(pulledOutputs, rawInput[k])
|
||||
skip[k] = true
|
||||
changed = true
|
||||
foundOutputs[id] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
k++
|
||||
}
|
||||
|
||||
// Emit only function_call items that have a matching output somewhere later in the request.
|
||||
// This prevents downstream Claude-style validation failures when clients accidentally send
|
||||
// orphan tool calls without including their tool outputs.
|
||||
for idx, item := range callItems {
|
||||
id, hasID := callIDByIdx[idx]
|
||||
if hasID {
|
||||
if _, ok := foundOutputs[id]; !ok {
|
||||
changed = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
out = append(out, pulledOutputs...)
|
||||
|
||||
i = j - 1
|
||||
}
|
||||
|
||||
if !changed {
|
||||
return body
|
||||
}
|
||||
|
||||
root["input"] = out
|
||||
updated, err := json.Marshal(root)
|
||||
if err != nil {
|
||||
return body
|
||||
}
|
||||
return updated
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func TestNormalizeOpenAIResponsesToolOrder_PullsOutputsImmediatelyAfterCalls(t *testing.T) {
|
||||
in := []byte(`{
|
||||
"model":"x",
|
||||
"input":[
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]},
|
||||
{"type":"function_call","call_id":"call_a","name":"a","arguments":"{}"},
|
||||
{"type":"function_call","call_id":"call_b","name":"b","arguments":"{}"},
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"(interleaved)"}]},
|
||||
{"type":"function_call_output","call_id":"call_b","output":"outB"},
|
||||
{"type":"function_call_output","call_id":"call_a","output":"outA"},
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"after"}]}
|
||||
]
|
||||
}`)
|
||||
|
||||
out := NormalizeOpenAIResponsesToolOrder(in)
|
||||
|
||||
if got := gjson.GetBytes(out, "input.#").Int(); got != 7 {
|
||||
t.Fatalf("expected 7 items, got %d body=%s", got, string(out))
|
||||
}
|
||||
// Calls stay in place.
|
||||
if gjson.GetBytes(out, "input.1.type").String() != "function_call" || gjson.GetBytes(out, "input.2.type").String() != "function_call" {
|
||||
t.Fatalf("expected function_call items at 1 and 2 body=%s", string(out))
|
||||
}
|
||||
// Outputs are pulled right after calls, preserving relative order among pulled outputs.
|
||||
if gjson.GetBytes(out, "input.3.type").String() != "function_call_output" || gjson.GetBytes(out, "input.4.type").String() != "function_call_output" {
|
||||
t.Fatalf("expected function_call_output items at 3 and 4 body=%s", string(out))
|
||||
}
|
||||
if gjson.GetBytes(out, "input.3.call_id").String() != "call_b" || gjson.GetBytes(out, "input.4.call_id").String() != "call_a" {
|
||||
t.Fatalf("unexpected output ordering body=%s", string(out))
|
||||
}
|
||||
// Interleaved message is pushed after the outputs.
|
||||
if gjson.GetBytes(out, "input.5.type").String() != "message" {
|
||||
t.Fatalf("expected interleaved message at 5 body=%s", string(out))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeOpenAIResponsesToolOrder_DropsOrphanCallsWithoutOutputs(t *testing.T) {
|
||||
in := []byte(`{
|
||||
"model":"x",
|
||||
"input":[
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]},
|
||||
{"type":"function_call","call_id":"call_a","name":"a","arguments":"{}"},
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"continue"}]}
|
||||
]
|
||||
}`)
|
||||
|
||||
out := NormalizeOpenAIResponsesToolOrder(in)
|
||||
if got := gjson.GetBytes(out, "input.#").Int(); got != 2 {
|
||||
t.Fatalf("expected orphan function_call to be dropped, got %d items body=%s", got, string(out))
|
||||
}
|
||||
if gjson.GetBytes(out, "input.0.type").String() != "message" || gjson.GetBytes(out, "input.1.type").String() != "message" {
|
||||
t.Fatalf("expected only message items to remain body=%s", string(out))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
# Droid CLI + CLIProxyAPI (Windows)
|
||||
|
||||
This repo can run a local OpenAI-compatible proxy (`CLIProxyAPI`) and configure Factory's `droid` CLI to use it via BYOK custom models.
|
||||
|
||||
## One-time setup
|
||||
|
||||
1. From the repo root, run:
|
||||
- `powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\setup-droid-cliproxy.ps1 -ProxyApiKey "<your-proxy-api-key>"`
|
||||
|
||||
This will:
|
||||
- Build `bin\cliproxyapi.exe`
|
||||
- Write `C:\Users\<you>\.factory\config.json` with a `custom_models` entry pointing at `http://127.0.0.1:8317/v1`
|
||||
|
||||
## Start / stop the proxy
|
||||
|
||||
- Start: `powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\start-cliproxy.ps1`
|
||||
- Stop: `powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\stop-cliproxy.ps1`
|
||||
|
||||
Logs:
|
||||
- `logs\cliproxyapi.out.log`
|
||||
- `logs\cliproxyapi.err.log`
|
||||
|
||||
## Use it in Droid
|
||||
|
||||
- In `droid`, run `/model` and pick `CLIProxy (local)`
|
||||
- In non-interactive mode, use: `droid exec --model custom:gpt-5.1 "..."` (replace model as needed)
|
||||
- `scripts\setup-droid-cliproxy.ps1` adds several `custom:<model>` entries (e.g. `custom:gpt-5.2`, `custom:gpt-5.1-codex`, `custom:gemini-3-pro-preview`)
|
||||
- For reasoning variants, pick a model like `CLIProxy (local): gpt-5.2 (reasoning: high)` (this uses `gpt-5.2(high)` under the hood).
|
||||
- `gpt-5.1-codex-max` also has reasoning variants (including `xhigh`) like `custom:gpt-5.1-codex-max(xhigh)`.
|
||||
@@ -0,0 +1,79 @@
|
||||
param(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$Model = "custom:gpt-5.2(high)"
|
||||
)
|
||||
|
||||
Set-StrictMode -Version Latest
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
function Ensure-CustomModelExists([string]$ModelId) {
|
||||
if (-not $ModelId.StartsWith("custom:")) {
|
||||
throw "Model must start with 'custom:'. Got: $ModelId"
|
||||
}
|
||||
$inner = $ModelId.Substring("custom:".Length)
|
||||
|
||||
$configPath = Join-Path $env:USERPROFILE ".factory\\config.json"
|
||||
if (-not (Test-Path -LiteralPath $configPath)) {
|
||||
throw "Droid BYOK config not found: $configPath"
|
||||
}
|
||||
$cfg = Get-Content -Raw -LiteralPath $configPath | ConvertFrom-Json
|
||||
$models = @($cfg.custom_models | ForEach-Object { [string]$_.model })
|
||||
if (-not ($models -contains $inner)) {
|
||||
throw "Custom model '$ModelId' not found in $configPath. Available: $($models -join ', ')"
|
||||
}
|
||||
}
|
||||
|
||||
function Update-JsonFile([string]$Path, [scriptblock]$Mutator) {
|
||||
$obj = Get-Content -Raw -LiteralPath $Path | ConvertFrom-Json
|
||||
& $Mutator $obj | Out-Null
|
||||
$json = $obj | ConvertTo-Json -Depth 50
|
||||
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
|
||||
[System.IO.File]::WriteAllText($Path, $json, $utf8NoBom)
|
||||
}
|
||||
|
||||
function Update-JsoncModelLine([string]$Path, [string]$ModelId) {
|
||||
$text = Get-Content -Raw -LiteralPath $Path
|
||||
if ($text -notmatch '"model"\s*:\s*"[^"]*"') {
|
||||
throw "Couldn't find a JSON 'model' field in: $Path"
|
||||
}
|
||||
$updated = [regex]::Replace($text, '"model"\s*:\s*"[^"]*"', ('"model": "' + $ModelId + '"'), 1)
|
||||
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
|
||||
[System.IO.File]::WriteAllText($Path, $updated, $utf8NoBom)
|
||||
}
|
||||
|
||||
Ensure-CustomModelExists -ModelId $Model
|
||||
|
||||
# Update global settings.json (JSONC with comments)
|
||||
$globalSettingsPath = Join-Path $env:USERPROFILE ".factory\\settings.json"
|
||||
if (Test-Path -LiteralPath $globalSettingsPath) {
|
||||
Update-JsoncModelLine -Path $globalSettingsPath -ModelId $Model
|
||||
Write-Host "Updated global model -> $globalSettingsPath"
|
||||
} else {
|
||||
Write-Host "Skipped (not found): $globalSettingsPath"
|
||||
}
|
||||
|
||||
# Update the latest session settings for this folder (pure JSON)
|
||||
$sessionDir = Join-Path $env:USERPROFILE ".factory\\sessions\\-D-code-CLIProxyAPI"
|
||||
if (Test-Path -LiteralPath $sessionDir) {
|
||||
$latest = Get-ChildItem -LiteralPath $sessionDir -Filter "*.settings.json" -File |
|
||||
Sort-Object LastWriteTime -Descending |
|
||||
Select-Object -First 1
|
||||
if ($latest) {
|
||||
Update-JsonFile -Path $latest.FullName -Mutator {
|
||||
param($o)
|
||||
$o.model = $Model
|
||||
if ($o.PSObject.Properties.Name -contains "providerLock") { $o.PSObject.Properties.Remove("providerLock") }
|
||||
if ($o.PSObject.Properties.Name -contains "providerLockTimestamp") { $o.PSObject.Properties.Remove("providerLockTimestamp") }
|
||||
if ($o.PSObject.Properties.Name -contains "apiProviderLock") { $o.PSObject.Properties.Remove("apiProviderLock") }
|
||||
if ($o.PSObject.Properties.Name -contains "apiProviderLockTimestamp") { $o.PSObject.Properties.Remove("apiProviderLockTimestamp") }
|
||||
}
|
||||
Write-Host "Updated session model -> $($latest.FullName)"
|
||||
} else {
|
||||
Write-Host "No session settings found in: $sessionDir"
|
||||
}
|
||||
} else {
|
||||
Write-Host "Skipped (not found): $sessionDir"
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "Restart droid, then try Spec mode again."
|
||||
@@ -0,0 +1,159 @@
|
||||
Set-StrictMode -Version Latest
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
param(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$ProxyApiKey = $env:CLIPROXY_API_KEY,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$ProxyBaseUrl = "http://127.0.0.1:8317/v1",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$ProxyModel = "gpt-5.1"
|
||||
)
|
||||
|
||||
function Get-RepoRoot {
|
||||
return (Resolve-Path (Join-Path $PSScriptRoot "..")).Path
|
||||
}
|
||||
|
||||
function Ensure-Dir([string]$Path) {
|
||||
if (-not (Test-Path -LiteralPath $Path)) {
|
||||
New-Item -ItemType Directory -Path $Path | Out-Null
|
||||
}
|
||||
}
|
||||
|
||||
function Build-CLIProxy([string]$RepoRoot) {
|
||||
$binDir = Join-Path $RepoRoot "bin"
|
||||
Ensure-Dir $binDir
|
||||
|
||||
$exePath = Join-Path $binDir "cliproxyapi.exe"
|
||||
|
||||
Write-Host "Building CLIProxyAPI -> $exePath"
|
||||
Push-Location $RepoRoot
|
||||
try {
|
||||
& go build -o $exePath .\cmd\server
|
||||
} finally {
|
||||
Pop-Location
|
||||
}
|
||||
|
||||
return $exePath
|
||||
}
|
||||
|
||||
function Ensure-DroidConfig([string]$BaseUrl, [string]$ApiKey, [string]$Model) {
|
||||
$factoryDir = Join-Path $env:USERPROFILE ".factory"
|
||||
Ensure-Dir $factoryDir
|
||||
|
||||
$configPath = Join-Path $factoryDir "config.json"
|
||||
$backupPath = Join-Path $factoryDir ("config.json.bak." + (Get-Date -Format "yyyyMMdd-HHmmss"))
|
||||
|
||||
$existingConfig = $null
|
||||
if (Test-Path -LiteralPath $configPath) {
|
||||
Copy-Item -LiteralPath $configPath -Destination $backupPath -Force
|
||||
Write-Host "Backed up existing Droid config -> $backupPath"
|
||||
try {
|
||||
$existingConfig = Get-Content -Raw -LiteralPath $configPath | ConvertFrom-Json
|
||||
} catch {
|
||||
$existingConfig = $null
|
||||
}
|
||||
}
|
||||
if (-not $existingConfig) {
|
||||
$existingConfig = [PSCustomObject]@{}
|
||||
}
|
||||
|
||||
$modelsToAdd = @(
|
||||
"gpt-5.2",
|
||||
"gpt-5.2(low)",
|
||||
"gpt-5.2(medium)",
|
||||
"gpt-5.2(high)",
|
||||
"gpt-5.2(xhigh)",
|
||||
"gpt-5.1-codex-max",
|
||||
"gpt-5.1-codex-max(low)",
|
||||
"gpt-5.1-codex-max(medium)",
|
||||
"gpt-5.1-codex-max(high)",
|
||||
"gpt-5.1-codex-max(xhigh)",
|
||||
"gemini-3-pro-preview",
|
||||
"gemini-claude-sonnet-4-5-thinking",
|
||||
"gemini-claude-opus-4-5-thinking"
|
||||
)
|
||||
|
||||
$modelsToRemove = @(
|
||||
"gpt-5-codex",
|
||||
"gpt-5-codex-mini",
|
||||
"gpt-5",
|
||||
"gemini-3-pro-image-preview",
|
||||
"gemini-2.5-flash-lite",
|
||||
"gpt-oss-120b-medium",
|
||||
"gemini-claude-sonnet-4-5",
|
||||
"gpt-5.1",
|
||||
"gemini-2.5-flash",
|
||||
"gemini-2.5-computer-use-preview-10-2025",
|
||||
"gpt-5.1-codex-mini",
|
||||
"gpt-5.1-codex"
|
||||
)
|
||||
|
||||
$newEntries = $modelsToAdd | ForEach-Object {
|
||||
$modelId = $_
|
||||
$displayModelId = $modelId
|
||||
if ($modelId -match '\((low|medium|high|xhigh|none|auto)\)$') {
|
||||
$displayModelId = ($modelId -replace '\((low|medium|high|xhigh|none|auto)\)$', ' (reasoning: $1)')
|
||||
}
|
||||
[PSCustomObject]@{
|
||||
model_display_name = "CLIProxy (local): $displayModelId"
|
||||
model = $modelId
|
||||
base_url = $BaseUrl
|
||||
api_key = $ApiKey
|
||||
provider = "openai"
|
||||
}
|
||||
}
|
||||
|
||||
$existingModels = @()
|
||||
if ($existingConfig.PSObject.Properties.Name -contains "custom_models" -and $existingConfig.custom_models) {
|
||||
$existingModels = @($existingConfig.custom_models)
|
||||
}
|
||||
|
||||
$filteredExisting = $existingModels | Where-Object {
|
||||
$m = $_
|
||||
-not (
|
||||
$m -and
|
||||
($m.PSObject.Properties.Name -contains "base_url") -and
|
||||
($m.PSObject.Properties.Name -contains "model") -and
|
||||
($m.base_url -eq $BaseUrl) -and
|
||||
(
|
||||
($modelsToAdd -contains [string]$m.model) -or
|
||||
($modelsToRemove -contains [string]$m.model)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
$existingConfig | Add-Member -NotePropertyName "custom_models" -NotePropertyValue (@($filteredExisting) + @($newEntries)) -Force
|
||||
|
||||
$json = $existingConfig | ConvertTo-Json -Depth 20
|
||||
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
|
||||
[System.IO.File]::WriteAllText($configPath, $json, $utf8NoBom)
|
||||
Write-Host "Wrote Droid BYOK config -> $configPath"
|
||||
}
|
||||
|
||||
function Write-UsageHint([string]$ApiKey, [string]$BaseUrl) {
|
||||
Write-Host ""
|
||||
Write-Host "Next:"
|
||||
Write-Host " 1) Start the proxy: scripts\\start-cliproxy.ps1"
|
||||
Write-Host " 2) In Droid: /model -> select 'CLIProxy (local)'"
|
||||
Write-Host ""
|
||||
Write-Host "Proxy auth (Droid api_key): $ApiKey"
|
||||
Write-Host "Proxy base_url: $BaseUrl"
|
||||
}
|
||||
|
||||
$repoRoot = Get-RepoRoot
|
||||
$configPath = Join-Path $repoRoot "config.yaml"
|
||||
|
||||
if (-not (Test-Path -LiteralPath $configPath)) {
|
||||
throw "Expected config file not found: $configPath"
|
||||
}
|
||||
|
||||
if (-not $ProxyApiKey -or $ProxyApiKey.Trim() -eq "") {
|
||||
throw "Missing proxy API key. Provide -ProxyApiKey <key> or set env var CLIPROXY_API_KEY."
|
||||
}
|
||||
|
||||
Build-CLIProxy -RepoRoot $repoRoot | Out-Null
|
||||
Ensure-DroidConfig -BaseUrl $ProxyBaseUrl -ApiKey $ProxyApiKey -Model $ProxyModel
|
||||
Write-UsageHint -ApiKey $ProxyApiKey -BaseUrl $ProxyBaseUrl
|
||||
@@ -0,0 +1,44 @@
|
||||
Set-StrictMode -Version Latest
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
function Get-RepoRoot {
|
||||
return (Resolve-Path (Join-Path $PSScriptRoot "..")).Path
|
||||
}
|
||||
|
||||
function Ensure-Dir([string]$Path) {
|
||||
if (-not (Test-Path -LiteralPath $Path)) {
|
||||
New-Item -ItemType Directory -Path $Path | Out-Null
|
||||
}
|
||||
}
|
||||
|
||||
$repoRoot = Get-RepoRoot
|
||||
$exePath = Join-Path $repoRoot "bin\\cliproxyapi.exe"
|
||||
$configPath = Join-Path $repoRoot "config.yaml"
|
||||
$logsDir = Join-Path $repoRoot "logs"
|
||||
Ensure-Dir $logsDir
|
||||
|
||||
$stdoutLog = Join-Path $logsDir "cliproxyapi.out.log"
|
||||
$stderrLog = Join-Path $logsDir "cliproxyapi.err.log"
|
||||
|
||||
if (-not (Test-Path -LiteralPath $exePath)) {
|
||||
throw "Binary not found: $exePath. Run scripts\\setup-droid-cliproxy.ps1 first (or build it with: go build -o bin\\cliproxyapi.exe .\\cmd\\server)."
|
||||
}
|
||||
if (-not (Test-Path -LiteralPath $configPath)) {
|
||||
throw "Config not found: $configPath"
|
||||
}
|
||||
|
||||
Write-Host "Starting CLIProxyAPI..."
|
||||
Write-Host " exe: $exePath"
|
||||
Write-Host " config: $configPath"
|
||||
Write-Host " logs: $logsDir"
|
||||
|
||||
Start-Process -FilePath $exePath `
|
||||
-ArgumentList @("-config", $configPath) `
|
||||
-WorkingDirectory $repoRoot `
|
||||
-WindowStyle Hidden `
|
||||
-RedirectStandardOutput $stdoutLog `
|
||||
-RedirectStandardError $stderrLog | Out-Null
|
||||
|
||||
Write-Host "Started. Tail logs:"
|
||||
Write-Host " Get-Content -Wait $stdoutLog"
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
Set-StrictMode -Version Latest
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$exeName = "cliproxyapi"
|
||||
$procs = Get-Process -Name $exeName -ErrorAction SilentlyContinue
|
||||
|
||||
if (-not $procs) {
|
||||
Write-Host "No running $exeName process found."
|
||||
exit 0
|
||||
}
|
||||
|
||||
$procs | ForEach-Object {
|
||||
Write-Host "Stopping $exeName (PID $($_.Id))"
|
||||
Stop-Process -Id $_.Id -Force
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
. "github.com/router-for-me/CLIProxyAPI/v6/internal/constant"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/tidwall/gjson"
|
||||
@@ -77,6 +78,8 @@ func (h *ClaudeCodeAPIHandler) ClaudeMessages(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
rawJSON = util.NormalizeClaudeToolResults(rawJSON)
|
||||
|
||||
// Check if the client requested a streaming response.
|
||||
streamResult := gjson.GetBytes(rawJSON, "stream")
|
||||
if !streamResult.Exists() || streamResult.Type == gjson.False {
|
||||
@@ -106,6 +109,8 @@ func (h *ClaudeCodeAPIHandler) ClaudeCountTokens(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
rawJSON = util.NormalizeClaudeToolResults(rawJSON)
|
||||
|
||||
c.Header("Content-Type", "application/json")
|
||||
|
||||
alt := h.GetAlt(c)
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
. "github.com/router-for-me/CLIProxyAPI/v6/internal/constant"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
@@ -82,6 +83,8 @@ func (h *OpenAIResponsesAPIHandler) Responses(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
rawJSON = util.NormalizeOpenAIResponsesToolOrder(rawJSON)
|
||||
|
||||
// Check if the client requested a streaming response.
|
||||
streamResult := gjson.GetBytes(rawJSON, "stream")
|
||||
if streamResult.Type == gjson.True {
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator"
|
||||
|
||||
sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func TestOpenAIToClaude_GroupsToolResultsInSingleMessage(t *testing.T) {
|
||||
raw := []byte(`{
|
||||
"model":"gpt-5",
|
||||
"messages":[
|
||||
{"role":"user","content":"hi"},
|
||||
{"role":"assistant","tool_calls":[
|
||||
{"id":"call_a","type":"function","function":{"name":"a","arguments":"{}"}},
|
||||
{"id":"call_b","type":"function","function":{"name":"b","arguments":"{}"}}
|
||||
]},
|
||||
{"role":"tool","tool_call_id":"call_a","content":"outA"},
|
||||
{"role":"tool","tool_call_id":"call_b","content":"outB"},
|
||||
{"role":"user","content":"done"}
|
||||
]
|
||||
}`)
|
||||
|
||||
out := sdktranslator.TranslateRequest(
|
||||
sdktranslator.FromString("openai"),
|
||||
sdktranslator.FromString("claude"),
|
||||
"claude-3-7-sonnet-20250219",
|
||||
raw,
|
||||
false,
|
||||
)
|
||||
|
||||
if got := gjson.GetBytes(out, "messages.#").Int(); got != 4 {
|
||||
t.Fatalf("expected 4 messages, got %d body=%s", got, string(out))
|
||||
}
|
||||
if gjson.GetBytes(out, "messages.1.role").String() != "assistant" {
|
||||
t.Fatalf("expected messages.1.role=assistant body=%s", string(out))
|
||||
}
|
||||
if got := gjson.GetBytes(out, "messages.1.content.#").Int(); got != 2 {
|
||||
t.Fatalf("expected 2 tool_use blocks, got %d body=%s", got, string(out))
|
||||
}
|
||||
if gjson.GetBytes(out, "messages.2.role").String() != "user" {
|
||||
t.Fatalf("expected messages.2.role=user body=%s", string(out))
|
||||
}
|
||||
if got := gjson.GetBytes(out, "messages.2.content.#").Int(); got != 2 {
|
||||
t.Fatalf("expected 2 tool_result blocks, got %d body=%s", got, string(out))
|
||||
}
|
||||
if gjson.GetBytes(out, "messages.2.content.0.tool_use_id").String() != "call_a" {
|
||||
t.Fatalf("expected first tool_use_id=call_a body=%s", string(out))
|
||||
}
|
||||
if gjson.GetBytes(out, "messages.2.content.1.tool_use_id").String() != "call_b" {
|
||||
t.Fatalf("expected second tool_use_id=call_b body=%s", string(out))
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenAIResponsesToClaude_GroupsCallOutputsInSingleMessage(t *testing.T) {
|
||||
raw := []byte(`{
|
||||
"model":"gpt-5",
|
||||
"input":[
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]},
|
||||
{"type":"function_call","call_id":"call_a","name":"a","arguments":"{}"},
|
||||
{"type":"function_call","call_id":"call_b","name":"b","arguments":"{}"},
|
||||
{"type":"function_call_output","call_id":"call_a","output":"outA"},
|
||||
{"type":"function_call_output","call_id":"call_b","output":"outB"},
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"done"}]}
|
||||
]
|
||||
}`)
|
||||
|
||||
out := sdktranslator.TranslateRequest(
|
||||
sdktranslator.FromString("openai-response"),
|
||||
sdktranslator.FromString("claude"),
|
||||
"claude-3-7-sonnet-20250219",
|
||||
raw,
|
||||
false,
|
||||
)
|
||||
|
||||
if got := gjson.GetBytes(out, "messages.#").Int(); got != 4 {
|
||||
t.Fatalf("expected 4 messages, got %d body=%s", got, string(out))
|
||||
}
|
||||
if gjson.GetBytes(out, "messages.1.role").String() != "assistant" {
|
||||
t.Fatalf("expected messages.1.role=assistant body=%s", string(out))
|
||||
}
|
||||
if got := gjson.GetBytes(out, "messages.1.content.#").Int(); got != 2 {
|
||||
t.Fatalf("expected 2 tool_use blocks, got %d body=%s", got, string(out))
|
||||
}
|
||||
if gjson.GetBytes(out, "messages.2.role").String() != "user" {
|
||||
t.Fatalf("expected messages.2.role=user body=%s", string(out))
|
||||
}
|
||||
if got := gjson.GetBytes(out, "messages.2.content.#").Int(); got != 2 {
|
||||
t.Fatalf("expected 2 tool_result blocks, got %d body=%s", got, string(out))
|
||||
}
|
||||
if gjson.GetBytes(out, "messages.2.content.0.tool_use_id").String() != "call_a" {
|
||||
t.Fatalf("expected first tool_use_id=call_a body=%s", string(out))
|
||||
}
|
||||
if gjson.GetBytes(out, "messages.2.content.1.tool_use_id").String() != "call_b" {
|
||||
t.Fatalf("expected second tool_use_id=call_b body=%s", string(out))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user