chore: merge upstream CLIProxyAPI v7.0.2

# Conflicts:
#	internal/translator/openai/openai/responses/openai_openai-responses_request_test.go
This commit is contained in:
RCD
2026-05-11 07:53:17 +00:00
5 changed files with 222 additions and 13 deletions
+1 -1
View File
@@ -1 +1 @@
v7.0.1
v7.0.2
+1 -1
View File
@@ -1 +1 @@
v7.0.1
v7.0.2
@@ -57,11 +57,72 @@ func ConvertOpenAIResponsesRequestToOpenAIChatCompletions(modelName string, inpu
// Convert input array to messages
if input := root.Get("input"); input.Exists() && input.IsArray() {
input.ForEach(func(_, item gjson.Result) bool {
inputItems := input.Array()
outputCallIDs := make(map[string]struct{})
for _, item := range inputItems {
if item.Get("type").String() != "function_call_output" {
continue
}
callID := strings.TrimSpace(item.Get("call_id").String())
if callID == "" {
continue
}
outputCallIDs[callID] = struct{}{}
}
pendingToolCalls := make([]interface{}, 0)
pendingToolCallIDs := make([]string, 0)
awaitingToolOutputs := make(map[string]struct{})
deferredMessages := make([][]byte, 0)
flushPendingToolCalls := func() {
if len(pendingToolCalls) == 0 {
return
}
assistantMessage := []byte(`{"role":"assistant","tool_calls":[]}`)
assistantMessage, _ = sjson.SetBytes(assistantMessage, "tool_calls", pendingToolCalls)
out, _ = sjson.SetRawBytes(out, "messages.-1", assistantMessage)
for _, id := range pendingToolCallIDs {
if strings.TrimSpace(id) == "" {
continue
}
awaitingToolOutputs[id] = struct{}{}
}
pendingToolCalls = pendingToolCalls[:0]
pendingToolCallIDs = pendingToolCallIDs[:0]
}
flushDeferredMessages := func() {
for _, message := range deferredMessages {
out, _ = sjson.SetRawBytes(out, "messages.-1", message)
}
deferredMessages = deferredMessages[:0]
}
hasAwaitingToolOutput := func() bool {
for id := range awaitingToolOutputs {
if _, ok := outputCallIDs[id]; ok {
return true
}
}
return false
}
appendRegularMessage := func(message []byte) {
// Keep tool-call adjacency strict for providers that require
// assistant(tool_calls) -> tool(tool_call_id) with no message in between.
if hasAwaitingToolOutput() {
deferredMessages = append(deferredMessages, message)
return
}
out, _ = sjson.SetRawBytes(out, "messages.-1", message)
}
for _, item := range inputItems {
itemType := item.Get("type").String()
if itemType == "" && item.Get("role").String() != "" {
itemType = "message"
}
if itemType != "function_call" {
flushPendingToolCalls()
}
switch itemType {
case "message", "":
@@ -106,12 +167,10 @@ func ConvertOpenAIResponsesRequestToOpenAIChatCompletions(modelName string, inpu
message, _ = sjson.SetBytes(message, "content", content.String())
}
out, _ = sjson.SetRawBytes(out, "messages.-1", message)
appendRegularMessage(message)
case "function_call":
// Handle function call conversion to assistant message with tool_calls
assistantMessage := []byte(`{"role":"assistant","tool_calls":[]}`)
// Buffer consecutive function calls and emit them as one assistant message.
toolCall := []byte(`{"id":"","type":"function","function":{"name":"","arguments":""}}`)
if callId := item.Get("call_id"); callId.Exists() {
@@ -125,16 +184,19 @@ func ConvertOpenAIResponsesRequestToOpenAIChatCompletions(modelName string, inpu
if arguments := item.Get("arguments"); arguments.Exists() {
toolCall, _ = sjson.SetBytes(toolCall, "function.arguments", arguments.String())
}
assistantMessage, _ = sjson.SetRawBytes(assistantMessage, "tool_calls.0", toolCall)
out, _ = sjson.SetRawBytes(out, "messages.-1", assistantMessage)
pendingToolCalls = append(pendingToolCalls, gjson.ParseBytes(toolCall).Value())
if callID := strings.TrimSpace(item.Get("call_id").String()); callID != "" {
pendingToolCallIDs = append(pendingToolCallIDs, callID)
}
case "function_call_output":
// Handle function call output conversion to tool message
toolMessage := []byte(`{"role":"tool","tool_call_id":"","content":""}`)
callID := ""
if callId := item.Get("call_id"); callId.Exists() {
toolMessage, _ = sjson.SetBytes(toolMessage, "tool_call_id", callId.String())
callID = strings.TrimSpace(callId.String())
toolMessage, _ = sjson.SetBytes(toolMessage, "tool_call_id", callID)
}
if output := item.Get("output"); output.Exists() {
@@ -142,10 +204,17 @@ func ConvertOpenAIResponsesRequestToOpenAIChatCompletions(modelName string, inpu
}
out, _ = sjson.SetRawBytes(out, "messages.-1", toolMessage)
if callID != "" {
delete(awaitingToolOutputs, callID)
}
if len(awaitingToolOutputs) == 0 && len(deferredMessages) > 0 {
flushDeferredMessages()
}
}
return true
})
}
flushPendingToolCalls()
flushDeferredMessages()
} else if input.Type == gjson.String {
msg := []byte(`{}`)
msg, _ = sjson.SetBytes(msg, "role", "user")
@@ -1,11 +1,24 @@
package responses
import (
"bytes"
"encoding/json"
"testing"
"github.com/tidwall/gjson"
)
func prettyJSONForTest(raw []byte) string {
if !gjson.ValidBytes(raw) {
return string(raw)
}
var out bytes.Buffer
if err := json.Indent(&out, raw, "", " "); err != nil {
return string(raw)
}
return out.String()
}
func TestConvertOpenAIResponsesRequestToOpenAIChatCompletions_PreservesDeveloperRole(t *testing.T) {
in := []byte(`{
"model":"gpt-5",
@@ -32,3 +45,107 @@ func TestConvertOpenAIResponsesRequestToOpenAIChatCompletions_PreservesDeveloper
t.Fatalf("expected second message role to remain user, got %q body=%s", got, string(out))
}
}
func TestConvertOpenAIResponsesRequestToOpenAIChatCompletions_MergeConsecutiveFunctionCalls(t *testing.T) {
raw := []byte(`{
"input": [
{"type":"function_call","call_id":"exec_command:0","name":"exec_command","arguments":"{\"cmd\":\"ls\"}"},
{"type":"function_call","call_id":"exec_command:1","name":"exec_command","arguments":"{\"cmd\":\"pwd\"}"},
{"type":"function_call_output","call_id":"exec_command:0","output":"ok0"},
{"type":"function_call_output","call_id":"exec_command:1","output":"ok1"}
]
}`)
t.Logf("input json:\n%s", prettyJSONForTest(raw))
out := ConvertOpenAIResponsesRequestToOpenAIChatCompletions("kimi-k2.6", raw, true)
t.Logf("output json:\n%s", prettyJSONForTest(out))
msgs := gjson.GetBytes(out, "messages")
if !msgs.Exists() || !msgs.IsArray() {
t.Fatalf("messages should be an array")
}
if got := len(msgs.Array()); got != 3 {
t.Fatalf("messages count = %d, want %d", got, 3)
}
if got := gjson.GetBytes(out, "messages.0.role").String(); got != "assistant" {
t.Fatalf("messages.0.role = %q, want %q", got, "assistant")
}
if got := len(gjson.GetBytes(out, "messages.0.tool_calls").Array()); got != 2 {
t.Fatalf("messages.0.tool_calls length = %d, want %d", got, 2)
}
if got := gjson.GetBytes(out, "messages.0.tool_calls.0.id").String(); got != "exec_command:0" {
t.Fatalf("messages.0.tool_calls.0.id = %q, want %q", got, "exec_command:0")
}
if got := gjson.GetBytes(out, "messages.0.tool_calls.1.id").String(); got != "exec_command:1" {
t.Fatalf("messages.0.tool_calls.1.id = %q, want %q", got, "exec_command:1")
}
if got := gjson.GetBytes(out, "messages.1.tool_call_id").String(); got != "exec_command:0" {
t.Fatalf("messages.1.tool_call_id = %q, want %q", got, "exec_command:0")
}
if got := gjson.GetBytes(out, "messages.2.tool_call_id").String(); got != "exec_command:1" {
t.Fatalf("messages.2.tool_call_id = %q, want %q", got, "exec_command:1")
}
}
func TestConvertOpenAIResponsesRequestToOpenAIChatCompletions_SplitFunctionCallsWhenInterrupted(t *testing.T) {
raw := []byte(`{
"input": [
{"type":"function_call","call_id":"call_a","name":"tool_a","arguments":"{}"},
{"type":"message","role":"user","content":"next"},
{"type":"function_call","call_id":"call_b","name":"tool_b","arguments":"{}"}
]
}`)
t.Logf("input json:\n%s", prettyJSONForTest(raw))
out := ConvertOpenAIResponsesRequestToOpenAIChatCompletions("kimi-k2.6", raw, false)
t.Logf("output json:\n%s", prettyJSONForTest(out))
if got := len(gjson.GetBytes(out, "messages").Array()); got != 3 {
t.Fatalf("messages count = %d, want %d", got, 3)
}
if got := gjson.GetBytes(out, "messages.0.tool_calls.0.id").String(); got != "call_a" {
t.Fatalf("messages.0.tool_calls.0.id = %q, want %q", got, "call_a")
}
if got := gjson.GetBytes(out, "messages.2.tool_calls.0.id").String(); got != "call_b" {
t.Fatalf("messages.2.tool_calls.0.id = %q, want %q", got, "call_b")
}
}
func TestConvertOpenAIResponsesRequestToOpenAIChatCompletions_DefersMessageUntilToolOutput(t *testing.T) {
raw := []byte(`{
"input": [
{"type":"function_call","call_id":"call_x","name":"exec_command","arguments":"{\"cmd\":\"echo hi\"}"},
{"type":"message","role":"user","content":"Approved command prefix saved"},
{"type":"function_call_output","call_id":"call_x","output":"ok"},
{"type":"message","role":"user","content":"next"}
]
}`)
t.Logf("input json:\n%s", prettyJSONForTest(raw))
out := ConvertOpenAIResponsesRequestToOpenAIChatCompletions("kimi-k2.6", raw, true)
t.Logf("output json:\n%s", prettyJSONForTest(out))
if got := len(gjson.GetBytes(out, "messages").Array()); got != 4 {
t.Fatalf("messages count = %d, want %d", got, 4)
}
if got := gjson.GetBytes(out, "messages.0.role").String(); got != "assistant" {
t.Fatalf("messages.0.role = %q, want %q", got, "assistant")
}
if got := gjson.GetBytes(out, "messages.1.role").String(); got != "tool" {
t.Fatalf("messages.1.role = %q, want %q", got, "tool")
}
if got := gjson.GetBytes(out, "messages.1.tool_call_id").String(); got != "call_x" {
t.Fatalf("messages.1.tool_call_id = %q, want %q", got, "call_x")
}
if got := gjson.GetBytes(out, "messages.2.role").String(); got != "user" {
t.Fatalf("messages.2.role = %q, want %q", got, "user")
}
if got := gjson.GetBytes(out, "messages.2.content").String(); got != "Approved command prefix saved" {
t.Fatalf("messages.2.content = %q, want %q", got, "Approved command prefix saved")
}
if got := gjson.GetBytes(out, "messages.3.content").String(); got != "next" {
t.Fatalf("messages.3.content = %q, want %q", got, "next")
}
}
+23
View File
@@ -17,7 +17,9 @@ import (
"github.com/router-for-me/CLIProxyAPI/v7/internal/redisqueue"
"github.com/router-for-me/CLIProxyAPI/v7/internal/registry"
"github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor"
"github.com/router-for-me/CLIProxyAPI/v7/internal/util"
"github.com/router-for-me/CLIProxyAPI/v7/internal/watcher"
"github.com/router-for-me/CLIProxyAPI/v7/internal/watcher/diff"
"github.com/router-for-me/CLIProxyAPI/v7/internal/wsrelay"
sdkaccess "github.com/router-for-me/CLIProxyAPI/v7/sdk/access"
sdkAuth "github.com/router-for-me/CLIProxyAPI/v7/sdk/auth"
@@ -608,9 +610,30 @@ func (s *Service) applyHomeOverlay(remoteCfg *config.Config) {
merged.Home = baseCfg.Home
forceHomeRuntimeConfig(&merged)
logHomeConfigChanges(baseCfg, &merged)
s.applyConfigUpdate(&merged)
}
func logHomeConfigChanges(oldCfg, newCfg *config.Config) {
if oldCfg == nil || newCfg == nil || !newCfg.Home.Enabled || (!oldCfg.Debug && !newCfg.Debug) {
return
}
details := diff.BuildConfigChangeDetails(oldCfg, newCfg)
if len(details) == 0 {
return
}
if newCfg.Debug && !log.IsLevelEnabled(log.DebugLevel) {
util.SetLogLevel(newCfg)
}
log.Debugf("home config changes detected:")
for _, detail := range details {
log.Debugf(" %s", detail)
}
}
func (s *Service) startHomeUsageForwarder(ctx context.Context, client *home.Client) {
if s == nil || client == nil {
return