mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-18 20:30:51 +08:00
fix: initialize contentBlocks with an empty slice and improve content handling in ConvertOpenAIResponseToClaudeNonStream
This commit is contained in:
@@ -466,7 +466,7 @@ func ConvertOpenAIResponseToClaudeNonStream(_ context.Context, _ string, origina
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var contentBlocks []interface{}
|
contentBlocks := make([]interface{}, 0)
|
||||||
hasToolCall := false
|
hasToolCall := false
|
||||||
|
|
||||||
if choices := root.Get("choices"); choices.Exists() && choices.IsArray() && len(choices.Array()) > 0 {
|
if choices := root.Get("choices"); choices.Exists() && choices.IsArray() && len(choices.Array()) > 0 {
|
||||||
@@ -477,80 +477,90 @@ func ConvertOpenAIResponseToClaudeNonStream(_ context.Context, _ string, origina
|
|||||||
}
|
}
|
||||||
|
|
||||||
if message := choice.Get("message"); message.Exists() {
|
if message := choice.Get("message"); message.Exists() {
|
||||||
if contentArray := message.Get("content"); contentArray.Exists() && contentArray.IsArray() {
|
if contentResult := message.Get("content"); contentResult.Exists() {
|
||||||
var textBuilder strings.Builder
|
if contentResult.IsArray() {
|
||||||
var thinkingBuilder strings.Builder
|
var textBuilder strings.Builder
|
||||||
|
var thinkingBuilder strings.Builder
|
||||||
|
|
||||||
flushText := func() {
|
flushText := func() {
|
||||||
if textBuilder.Len() == 0 {
|
if textBuilder.Len() == 0 {
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
contentBlocks = append(contentBlocks, map[string]interface{}{
|
||||||
|
"type": "text",
|
||||||
|
"text": textBuilder.String(),
|
||||||
|
})
|
||||||
|
textBuilder.Reset()
|
||||||
}
|
}
|
||||||
contentBlocks = append(contentBlocks, map[string]interface{}{
|
|
||||||
"type": "text",
|
|
||||||
"text": textBuilder.String(),
|
|
||||||
})
|
|
||||||
textBuilder.Reset()
|
|
||||||
}
|
|
||||||
|
|
||||||
flushThinking := func() {
|
flushThinking := func() {
|
||||||
if thinkingBuilder.Len() == 0 {
|
if thinkingBuilder.Len() == 0 {
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
contentBlocks = append(contentBlocks, map[string]interface{}{
|
||||||
|
"type": "thinking",
|
||||||
|
"thinking": thinkingBuilder.String(),
|
||||||
|
})
|
||||||
|
thinkingBuilder.Reset()
|
||||||
}
|
}
|
||||||
contentBlocks = append(contentBlocks, map[string]interface{}{
|
|
||||||
"type": "thinking",
|
|
||||||
"thinking": thinkingBuilder.String(),
|
|
||||||
})
|
|
||||||
thinkingBuilder.Reset()
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, item := range contentArray.Array() {
|
for _, item := range contentResult.Array() {
|
||||||
typeStr := item.Get("type").String()
|
typeStr := item.Get("type").String()
|
||||||
switch typeStr {
|
switch typeStr {
|
||||||
case "text":
|
case "text":
|
||||||
flushThinking()
|
flushThinking()
|
||||||
textBuilder.WriteString(item.Get("text").String())
|
textBuilder.WriteString(item.Get("text").String())
|
||||||
case "tool_calls":
|
case "tool_calls":
|
||||||
flushThinking()
|
flushThinking()
|
||||||
flushText()
|
flushText()
|
||||||
toolCalls := item.Get("tool_calls")
|
toolCalls := item.Get("tool_calls")
|
||||||
if toolCalls.IsArray() {
|
if toolCalls.IsArray() {
|
||||||
toolCalls.ForEach(func(_, tc gjson.Result) bool {
|
toolCalls.ForEach(func(_, tc gjson.Result) bool {
|
||||||
hasToolCall = true
|
hasToolCall = true
|
||||||
toolUse := map[string]interface{}{
|
toolUse := map[string]interface{}{
|
||||||
"type": "tool_use",
|
"type": "tool_use",
|
||||||
"id": tc.Get("id").String(),
|
"id": tc.Get("id").String(),
|
||||||
"name": tc.Get("function.name").String(),
|
"name": tc.Get("function.name").String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
argsStr := util.FixJSON(tc.Get("function.arguments").String())
|
argsStr := util.FixJSON(tc.Get("function.arguments").String())
|
||||||
if argsStr != "" {
|
if argsStr != "" {
|
||||||
var parsed interface{}
|
var parsed interface{}
|
||||||
if err := json.Unmarshal([]byte(argsStr), &parsed); err == nil {
|
if err := json.Unmarshal([]byte(argsStr), &parsed); err == nil {
|
||||||
toolUse["input"] = parsed
|
toolUse["input"] = parsed
|
||||||
|
} else {
|
||||||
|
toolUse["input"] = map[string]interface{}{}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
toolUse["input"] = map[string]interface{}{}
|
toolUse["input"] = map[string]interface{}{}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
toolUse["input"] = map[string]interface{}{}
|
|
||||||
}
|
|
||||||
|
|
||||||
contentBlocks = append(contentBlocks, toolUse)
|
contentBlocks = append(contentBlocks, toolUse)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
case "reasoning":
|
||||||
|
flushText()
|
||||||
|
if thinking := item.Get("text"); thinking.Exists() {
|
||||||
|
thinkingBuilder.WriteString(thinking.String())
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
flushThinking()
|
||||||
|
flushText()
|
||||||
}
|
}
|
||||||
case "reasoning":
|
}
|
||||||
flushText()
|
|
||||||
if thinking := item.Get("text"); thinking.Exists() {
|
flushThinking()
|
||||||
thinkingBuilder.WriteString(thinking.String())
|
flushText()
|
||||||
}
|
} else if contentResult.Type == gjson.String {
|
||||||
default:
|
textContent := contentResult.String()
|
||||||
flushThinking()
|
if textContent != "" {
|
||||||
flushText()
|
contentBlocks = append(contentBlocks, map[string]interface{}{
|
||||||
|
"type": "text",
|
||||||
|
"text": textContent,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
flushThinking()
|
|
||||||
flushText()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if toolCalls := message.Get("tool_calls"); toolCalls.Exists() && toolCalls.IsArray() {
|
if toolCalls := message.Get("tool_calls"); toolCalls.Exists() && toolCalls.IsArray() {
|
||||||
|
|||||||
Reference in New Issue
Block a user