mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 13:00:52 +08:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
735b21394c | ||
|
|
9cdef937af |
@@ -7,7 +7,6 @@
|
|||||||
package claude
|
package claude
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -180,29 +179,16 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa
|
|||||||
// Returns:
|
// Returns:
|
||||||
// - string: A Claude Code-compatible JSON response containing all message content and metadata
|
// - string: A Claude Code-compatible JSON response containing all message content and metadata
|
||||||
func ConvertCodexResponseToClaudeNonStream(_ context.Context, _ string, originalRequestRawJSON, _ []byte, rawJSON []byte, _ *any) string {
|
func ConvertCodexResponseToClaudeNonStream(_ context.Context, _ string, originalRequestRawJSON, _ []byte, rawJSON []byte, _ *any) string {
|
||||||
scanner := bufio.NewScanner(bytes.NewReader(rawJSON))
|
|
||||||
buffer := make([]byte, 20_971_520)
|
|
||||||
scanner.Buffer(buffer, 20_971_520)
|
|
||||||
revNames := buildReverseMapFromClaudeOriginalShortToOriginal(originalRequestRawJSON)
|
revNames := buildReverseMapFromClaudeOriginalShortToOriginal(originalRequestRawJSON)
|
||||||
|
|
||||||
for scanner.Scan() {
|
rootResult := gjson.ParseBytes(rawJSON)
|
||||||
line := scanner.Bytes()
|
|
||||||
if !bytes.HasPrefix(line, dataTag) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
payload := bytes.TrimSpace(line[len(dataTag):])
|
|
||||||
if len(payload) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
rootResult := gjson.ParseBytes(payload)
|
|
||||||
if rootResult.Get("type").String() != "response.completed" {
|
if rootResult.Get("type").String() != "response.completed" {
|
||||||
continue
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
responseData := rootResult.Get("response")
|
responseData := rootResult.Get("response")
|
||||||
if !responseData.Exists() {
|
if !responseData.Exists() {
|
||||||
continue
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
response := map[string]interface{}{
|
response := map[string]interface{}{
|
||||||
@@ -343,9 +329,6 @@ func ConvertCodexResponseToClaudeNonStream(_ context.Context, _ string, original
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return string(responseJSON)
|
return string(responseJSON)
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildReverseMapFromClaudeOriginalShortToOriginal builds a map[short]original from original Claude request tools.
|
// buildReverseMapFromClaudeOriginalShortToOriginal builds a map[short]original from original Claude request tools.
|
||||||
|
|||||||
@@ -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,7 +477,8 @@ 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() {
|
||||||
|
if contentResult.IsArray() {
|
||||||
var textBuilder strings.Builder
|
var textBuilder strings.Builder
|
||||||
var thinkingBuilder strings.Builder
|
var thinkingBuilder strings.Builder
|
||||||
|
|
||||||
@@ -503,7 +504,7 @@ func ConvertOpenAIResponseToClaudeNonStream(_ context.Context, _ string, origina
|
|||||||
thinkingBuilder.Reset()
|
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":
|
||||||
@@ -551,6 +552,15 @@ func ConvertOpenAIResponseToClaudeNonStream(_ context.Context, _ string, origina
|
|||||||
|
|
||||||
flushThinking()
|
flushThinking()
|
||||||
flushText()
|
flushText()
|
||||||
|
} else if contentResult.Type == gjson.String {
|
||||||
|
textContent := contentResult.String()
|
||||||
|
if textContent != "" {
|
||||||
|
contentBlocks = append(contentBlocks, map[string]interface{}{
|
||||||
|
"type": "text",
|
||||||
|
"text": textContent,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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