mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-02 20:40:52 +08:00
Add reverse mappings for original tool names and improve error logging
- Introduced reverse mapping logic for tool names in translators to restore original names when shortened. - Enhanced error handling by logging API response errors consistently across handlers. - Refactored request and response loggers to include API error details, improving debugging capabilities. - Integrated robust tool name shortening and uniqueness mechanisms for OpenAI, Gemini, and Claude requests. - Improved handler retry logic to properly capture and respond to errors.
This commit is contained in:
@@ -8,6 +8,8 @@ package claude
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/luispater/CLIProxyAPI/internal/misc"
|
||||
"github.com/tidwall/gjson"
|
||||
@@ -94,7 +96,17 @@ func ConvertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
|
||||
// Handle tool use content by creating function call message.
|
||||
functionCallMessage := `{"type":"function_call"}`
|
||||
functionCallMessage, _ = sjson.Set(functionCallMessage, "call_id", messageContentResult.Get("id").String())
|
||||
functionCallMessage, _ = sjson.Set(functionCallMessage, "name", messageContentResult.Get("name").String())
|
||||
{
|
||||
// Shorten tool name if needed based on declared tools
|
||||
name := messageContentResult.Get("name").String()
|
||||
toolMap := buildReverseMapFromClaudeOriginalToShort(rawJSON)
|
||||
if short, ok := toolMap[name]; ok {
|
||||
name = short
|
||||
} else {
|
||||
name = shortenNameIfNeeded(name)
|
||||
}
|
||||
functionCallMessage, _ = sjson.Set(functionCallMessage, "name", name)
|
||||
}
|
||||
functionCallMessage, _ = sjson.Set(functionCallMessage, "arguments", messageContentResult.Get("input").Raw)
|
||||
template, _ = sjson.SetRaw(template, "input.-1", functionCallMessage)
|
||||
} else if contentType == "tool_result" {
|
||||
@@ -130,10 +142,29 @@ func ConvertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
|
||||
template, _ = sjson.SetRaw(template, "tools", `[]`)
|
||||
template, _ = sjson.Set(template, "tool_choice", `auto`)
|
||||
toolResults := toolsResult.Array()
|
||||
// Build short name map from declared tools
|
||||
var names []string
|
||||
for i := 0; i < len(toolResults); i++ {
|
||||
n := toolResults[i].Get("name").String()
|
||||
if n != "" {
|
||||
names = append(names, n)
|
||||
}
|
||||
}
|
||||
shortMap := buildShortNameMap(names)
|
||||
for i := 0; i < len(toolResults); i++ {
|
||||
toolResult := toolResults[i]
|
||||
tool := toolResult.Raw
|
||||
tool, _ = sjson.Set(tool, "type", "function")
|
||||
// Apply shortened name if needed
|
||||
if v := toolResult.Get("name"); v.Exists() {
|
||||
name := v.String()
|
||||
if short, ok := shortMap[name]; ok {
|
||||
name = short
|
||||
} else {
|
||||
name = shortenNameIfNeeded(name)
|
||||
}
|
||||
tool, _ = sjson.Set(tool, "name", name)
|
||||
}
|
||||
tool, _ = sjson.SetRaw(tool, "parameters", toolResult.Get("input_schema").Raw)
|
||||
tool, _ = sjson.Delete(tool, "input_schema")
|
||||
tool, _ = sjson.Delete(tool, "parameters.$schema")
|
||||
@@ -170,3 +201,97 @@ func ConvertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
|
||||
|
||||
return []byte(template)
|
||||
}
|
||||
|
||||
// shortenNameIfNeeded applies a simple shortening rule for a single name.
|
||||
func shortenNameIfNeeded(name string) string {
|
||||
const limit = 64
|
||||
if len(name) <= limit {
|
||||
return name
|
||||
}
|
||||
if strings.HasPrefix(name, "mcp__") {
|
||||
idx := strings.LastIndex(name, "__")
|
||||
if idx > 0 {
|
||||
cand := "mcp__" + name[idx+2:]
|
||||
if len(cand) > limit {
|
||||
return cand[:limit]
|
||||
}
|
||||
return cand
|
||||
}
|
||||
}
|
||||
return name[:limit]
|
||||
}
|
||||
|
||||
// buildShortNameMap ensures uniqueness of shortened names within a request.
|
||||
func buildShortNameMap(names []string) map[string]string {
|
||||
const limit = 64
|
||||
used := map[string]struct{}{}
|
||||
m := map[string]string{}
|
||||
|
||||
baseCandidate := func(n string) string {
|
||||
if len(n) <= limit {
|
||||
return n
|
||||
}
|
||||
if strings.HasPrefix(n, "mcp__") {
|
||||
idx := strings.LastIndex(n, "__")
|
||||
if idx > 0 {
|
||||
cand := "mcp__" + n[idx+2:]
|
||||
if len(cand) > limit {
|
||||
cand = cand[:limit]
|
||||
}
|
||||
return cand
|
||||
}
|
||||
}
|
||||
return n[:limit]
|
||||
}
|
||||
|
||||
makeUnique := func(cand string) string {
|
||||
if _, ok := used[cand]; !ok {
|
||||
return cand
|
||||
}
|
||||
base := cand
|
||||
for i := 1; ; i++ {
|
||||
suffix := "~" + strconv.Itoa(i)
|
||||
allowed := limit - len(suffix)
|
||||
if allowed < 0 {
|
||||
allowed = 0
|
||||
}
|
||||
tmp := base
|
||||
if len(tmp) > allowed {
|
||||
tmp = tmp[:allowed]
|
||||
}
|
||||
tmp = tmp + suffix
|
||||
if _, ok := used[tmp]; !ok {
|
||||
return tmp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, n := range names {
|
||||
cand := baseCandidate(n)
|
||||
uniq := makeUnique(cand)
|
||||
used[uniq] = struct{}{}
|
||||
m[n] = uniq
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// buildReverseMapFromClaudeOriginalToShort builds original->short map, used to map tool_use names to short.
|
||||
func buildReverseMapFromClaudeOriginalToShort(original []byte) map[string]string {
|
||||
tools := gjson.GetBytes(original, "tools")
|
||||
m := map[string]string{}
|
||||
if !tools.IsArray() {
|
||||
return m
|
||||
}
|
||||
var names []string
|
||||
arr := tools.Array()
|
||||
for i := 0; i < len(arr); i++ {
|
||||
n := arr[i].Get("name").String()
|
||||
if n != "" {
|
||||
names = append(names, n)
|
||||
}
|
||||
}
|
||||
if len(names) > 0 {
|
||||
m = buildShortNameMap(names)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -122,7 +122,15 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa
|
||||
template = `{"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"","name":"","input":{}}}`
|
||||
template, _ = sjson.Set(template, "index", rootResult.Get("output_index").Int())
|
||||
template, _ = sjson.Set(template, "content_block.id", itemResult.Get("call_id").String())
|
||||
template, _ = sjson.Set(template, "content_block.name", itemResult.Get("name").String())
|
||||
{
|
||||
// Restore original tool name if shortened
|
||||
name := itemResult.Get("name").String()
|
||||
rev := buildReverseMapFromClaudeOriginalShortToOriginal(originalRequestRawJSON)
|
||||
if orig, ok := rev[name]; ok {
|
||||
name = orig
|
||||
}
|
||||
template, _ = sjson.Set(template, "content_block.name", name)
|
||||
}
|
||||
|
||||
output = "event: content_block_start\n"
|
||||
output += fmt.Sprintf("data: %s\n\n", template)
|
||||
@@ -171,3 +179,27 @@ func ConvertCodexResponseToClaude(_ context.Context, _ string, originalRequestRa
|
||||
func ConvertCodexResponseToClaudeNonStream(_ context.Context, _ string, originalRequestRawJSON, requestRawJSON, _ []byte, _ *any) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// buildReverseMapFromClaudeOriginalShortToOriginal builds a map[short]original from original Claude request tools.
|
||||
func buildReverseMapFromClaudeOriginalShortToOriginal(original []byte) map[string]string {
|
||||
tools := gjson.GetBytes(original, "tools")
|
||||
rev := map[string]string{}
|
||||
if !tools.IsArray() {
|
||||
return rev
|
||||
}
|
||||
var names []string
|
||||
arr := tools.Array()
|
||||
for i := 0; i < len(arr); i++ {
|
||||
n := arr[i].Get("name").String()
|
||||
if n != "" {
|
||||
names = append(names, n)
|
||||
}
|
||||
}
|
||||
if len(names) > 0 {
|
||||
m := buildShortNameMap(names)
|
||||
for orig, short := range m {
|
||||
rev[short] = orig
|
||||
}
|
||||
}
|
||||
return rev
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/luispater/CLIProxyAPI/internal/misc"
|
||||
@@ -46,6 +47,27 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
|
||||
|
||||
root := gjson.ParseBytes(rawJSON)
|
||||
|
||||
// Pre-compute tool name shortening map from declared functionDeclarations
|
||||
shortMap := map[string]string{}
|
||||
if tools := root.Get("tools"); tools.IsArray() {
|
||||
var names []string
|
||||
tarr := tools.Array()
|
||||
for i := 0; i < len(tarr); i++ {
|
||||
fns := tarr[i].Get("functionDeclarations")
|
||||
if !fns.IsArray() {
|
||||
continue
|
||||
}
|
||||
for _, fn := range fns.Array() {
|
||||
if v := fn.Get("name"); v.Exists() {
|
||||
names = append(names, v.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(names) > 0 {
|
||||
shortMap = buildShortNameMap(names)
|
||||
}
|
||||
}
|
||||
|
||||
// helper for generating paired call IDs in the form: call_<alphanum>
|
||||
// Gemini uses sequential pairing across possibly multiple in-flight
|
||||
// functionCalls, so we keep a FIFO queue of generated call IDs and
|
||||
@@ -124,7 +146,13 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
|
||||
if fc := p.Get("functionCall"); fc.Exists() {
|
||||
fn := `{"type":"function_call"}`
|
||||
if name := fc.Get("name"); name.Exists() {
|
||||
fn, _ = sjson.Set(fn, "name", name.String())
|
||||
n := name.String()
|
||||
if short, ok := shortMap[n]; ok {
|
||||
n = short
|
||||
} else {
|
||||
n = shortenNameIfNeeded(n)
|
||||
}
|
||||
fn, _ = sjson.Set(fn, "name", n)
|
||||
}
|
||||
if args := fc.Get("args"); args.Exists() {
|
||||
fn, _ = sjson.Set(fn, "arguments", args.Raw)
|
||||
@@ -185,7 +213,13 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
|
||||
tool := `{}`
|
||||
tool, _ = sjson.Set(tool, "type", "function")
|
||||
if v := fn.Get("name"); v.Exists() {
|
||||
tool, _ = sjson.Set(tool, "name", v.String())
|
||||
name := v.String()
|
||||
if short, ok := shortMap[name]; ok {
|
||||
name = short
|
||||
} else {
|
||||
name = shortenNameIfNeeded(name)
|
||||
}
|
||||
tool, _ = sjson.Set(tool, "name", name)
|
||||
}
|
||||
if v := fn.Get("description"); v.Exists() {
|
||||
tool, _ = sjson.Set(tool, "description", v.String())
|
||||
@@ -227,3 +261,76 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
|
||||
|
||||
return []byte(out)
|
||||
}
|
||||
|
||||
// shortenNameIfNeeded applies the simple shortening rule for a single name.
|
||||
func shortenNameIfNeeded(name string) string {
|
||||
const limit = 64
|
||||
if len(name) <= limit {
|
||||
return name
|
||||
}
|
||||
if strings.HasPrefix(name, "mcp__") {
|
||||
idx := strings.LastIndex(name, "__")
|
||||
if idx > 0 {
|
||||
cand := "mcp__" + name[idx+2:]
|
||||
if len(cand) > limit {
|
||||
return cand[:limit]
|
||||
}
|
||||
return cand
|
||||
}
|
||||
}
|
||||
return name[:limit]
|
||||
}
|
||||
|
||||
// buildShortNameMap ensures uniqueness of shortened names within a request.
|
||||
func buildShortNameMap(names []string) map[string]string {
|
||||
const limit = 64
|
||||
used := map[string]struct{}{}
|
||||
m := map[string]string{}
|
||||
|
||||
baseCandidate := func(n string) string {
|
||||
if len(n) <= limit {
|
||||
return n
|
||||
}
|
||||
if strings.HasPrefix(n, "mcp__") {
|
||||
idx := strings.LastIndex(n, "__")
|
||||
if idx > 0 {
|
||||
cand := "mcp__" + n[idx+2:]
|
||||
if len(cand) > limit {
|
||||
cand = cand[:limit]
|
||||
}
|
||||
return cand
|
||||
}
|
||||
}
|
||||
return n[:limit]
|
||||
}
|
||||
|
||||
makeUnique := func(cand string) string {
|
||||
if _, ok := used[cand]; !ok {
|
||||
return cand
|
||||
}
|
||||
base := cand
|
||||
for i := 1; ; i++ {
|
||||
suffix := "~" + strconv.Itoa(i)
|
||||
allowed := limit - len(suffix)
|
||||
if allowed < 0 {
|
||||
allowed = 0
|
||||
}
|
||||
tmp := base
|
||||
if len(tmp) > allowed {
|
||||
tmp = tmp[:allowed]
|
||||
}
|
||||
tmp = tmp + suffix
|
||||
if _, ok := used[tmp]; !ok {
|
||||
return tmp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, n := range names {
|
||||
cand := baseCandidate(n)
|
||||
uniq := makeUnique(cand)
|
||||
used[uniq] = struct{}{}
|
||||
m[n] = uniq
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -80,7 +80,15 @@ func ConvertCodexResponseToGemini(_ context.Context, modelName string, originalR
|
||||
if itemType == "function_call" {
|
||||
// Create function call part
|
||||
functionCall := `{"functionCall":{"name":"","args":{}}}`
|
||||
functionCall, _ = sjson.Set(functionCall, "functionCall.name", itemResult.Get("name").String())
|
||||
{
|
||||
// Restore original tool name if shortened
|
||||
n := itemResult.Get("name").String()
|
||||
rev := buildReverseMapFromGeminiOriginal(originalRequestRawJSON)
|
||||
if orig, ok := rev[n]; ok {
|
||||
n = orig
|
||||
}
|
||||
functionCall, _ = sjson.Set(functionCall, "functionCall.name", n)
|
||||
}
|
||||
|
||||
// Parse and set arguments
|
||||
argsStr := itemResult.Get("arguments").String()
|
||||
@@ -250,7 +258,14 @@ func ConvertCodexResponseToGeminiNonStream(_ context.Context, modelName string,
|
||||
hasToolCall = true
|
||||
functionCall := map[string]interface{}{
|
||||
"functionCall": map[string]interface{}{
|
||||
"name": value.Get("name").String(),
|
||||
"name": func() string {
|
||||
n := value.Get("name").String()
|
||||
rev := buildReverseMapFromGeminiOriginal(originalRequestRawJSON)
|
||||
if orig, ok := rev[n]; ok {
|
||||
return orig
|
||||
}
|
||||
return n
|
||||
}(),
|
||||
"args": map[string]interface{}{},
|
||||
},
|
||||
}
|
||||
@@ -292,6 +307,35 @@ func ConvertCodexResponseToGeminiNonStream(_ context.Context, modelName string,
|
||||
return ""
|
||||
}
|
||||
|
||||
// buildReverseMapFromGeminiOriginal builds a map[short]original from original Gemini request tools.
|
||||
func buildReverseMapFromGeminiOriginal(original []byte) map[string]string {
|
||||
tools := gjson.GetBytes(original, "tools")
|
||||
rev := map[string]string{}
|
||||
if !tools.IsArray() {
|
||||
return rev
|
||||
}
|
||||
var names []string
|
||||
tarr := tools.Array()
|
||||
for i := 0; i < len(tarr); i++ {
|
||||
fns := tarr[i].Get("functionDeclarations")
|
||||
if !fns.IsArray() {
|
||||
continue
|
||||
}
|
||||
for _, fn := range fns.Array() {
|
||||
if v := fn.Get("name"); v.Exists() {
|
||||
names = append(names, v.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(names) > 0 {
|
||||
m := buildShortNameMap(names)
|
||||
for orig, short := range m {
|
||||
rev[short] = orig
|
||||
}
|
||||
}
|
||||
return rev
|
||||
}
|
||||
|
||||
// mustMarshalJSON marshals a value to JSON, panicking on error.
|
||||
func mustMarshalJSON(v interface{}) string {
|
||||
data, err := json.Marshal(v)
|
||||
|
||||
@@ -9,6 +9,9 @@ package chat_completions
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/luispater/CLIProxyAPI/internal/misc"
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
@@ -67,6 +70,31 @@ func ConvertOpenAIRequestToCodex(modelName string, inputRawJSON []byte, stream b
|
||||
// Model
|
||||
out, _ = sjson.Set(out, "model", modelName)
|
||||
|
||||
// Build tool name shortening map from original tools (if any)
|
||||
originalToolNameMap := map[string]string{}
|
||||
{
|
||||
tools := gjson.GetBytes(rawJSON, "tools")
|
||||
if tools.IsArray() && len(tools.Array()) > 0 {
|
||||
// Collect original tool names
|
||||
var names []string
|
||||
arr := tools.Array()
|
||||
for i := 0; i < len(arr); i++ {
|
||||
t := arr[i]
|
||||
if t.Get("type").String() == "function" {
|
||||
fn := t.Get("function")
|
||||
if fn.Exists() {
|
||||
if v := fn.Get("name"); v.Exists() {
|
||||
names = append(names, v.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(names) > 0 {
|
||||
originalToolNameMap = buildShortNameMap(names)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extract system instructions from first system message (string or text object)
|
||||
messages := gjson.GetBytes(rawJSON, "messages")
|
||||
instructions := misc.CodexInstructions
|
||||
@@ -177,7 +205,15 @@ func ConvertOpenAIRequestToCodex(modelName string, inputRawJSON []byte, stream b
|
||||
funcCall := `{}`
|
||||
funcCall, _ = sjson.Set(funcCall, "type", "function_call")
|
||||
funcCall, _ = sjson.Set(funcCall, "call_id", tc.Get("id").String())
|
||||
funcCall, _ = sjson.Set(funcCall, "name", tc.Get("function.name").String())
|
||||
{
|
||||
name := tc.Get("function.name").String()
|
||||
if short, ok := originalToolNameMap[name]; ok {
|
||||
name = short
|
||||
} else {
|
||||
name = shortenNameIfNeeded(name)
|
||||
}
|
||||
funcCall, _ = sjson.Set(funcCall, "name", name)
|
||||
}
|
||||
funcCall, _ = sjson.Set(funcCall, "arguments", tc.Get("function.arguments").String())
|
||||
out, _ = sjson.SetRaw(out, "input.-1", funcCall)
|
||||
}
|
||||
@@ -249,7 +285,13 @@ func ConvertOpenAIRequestToCodex(modelName string, inputRawJSON []byte, stream b
|
||||
fn := t.Get("function")
|
||||
if fn.Exists() {
|
||||
if v := fn.Get("name"); v.Exists() {
|
||||
item, _ = sjson.Set(item, "name", v.Value())
|
||||
name := v.String()
|
||||
if short, ok := originalToolNameMap[name]; ok {
|
||||
name = short
|
||||
} else {
|
||||
name = shortenNameIfNeeded(name)
|
||||
}
|
||||
item, _ = sjson.Set(item, "name", name)
|
||||
}
|
||||
if v := fn.Get("description"); v.Exists() {
|
||||
item, _ = sjson.Set(item, "description", v.Value())
|
||||
@@ -273,3 +315,81 @@ func ConvertOpenAIRequestToCodex(modelName string, inputRawJSON []byte, stream b
|
||||
out, _ = sjson.Set(out, "store", store)
|
||||
return []byte(out)
|
||||
}
|
||||
|
||||
// shortenNameIfNeeded applies the simple shortening rule for a single name.
|
||||
// If the name length exceeds 64, it will try to preserve the "mcp__" prefix and last segment.
|
||||
// Otherwise it truncates to 64 characters.
|
||||
func shortenNameIfNeeded(name string) string {
|
||||
const limit = 64
|
||||
if len(name) <= limit {
|
||||
return name
|
||||
}
|
||||
if strings.HasPrefix(name, "mcp__") {
|
||||
// Keep prefix and last segment after '__'
|
||||
idx := strings.LastIndex(name, "__")
|
||||
if idx > 0 {
|
||||
candidate := "mcp__" + name[idx+2:]
|
||||
if len(candidate) > limit {
|
||||
return candidate[:limit]
|
||||
}
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
return name[:limit]
|
||||
}
|
||||
|
||||
// buildShortNameMap generates unique short names (<=64) for the given list of names.
|
||||
// It preserves the "mcp__" prefix with the last segment when possible and ensures uniqueness
|
||||
// by appending suffixes like "~1", "~2" if needed.
|
||||
func buildShortNameMap(names []string) map[string]string {
|
||||
const limit = 64
|
||||
used := map[string]struct{}{}
|
||||
m := map[string]string{}
|
||||
|
||||
baseCandidate := func(n string) string {
|
||||
if len(n) <= limit {
|
||||
return n
|
||||
}
|
||||
if strings.HasPrefix(n, "mcp__") {
|
||||
idx := strings.LastIndex(n, "__")
|
||||
if idx > 0 {
|
||||
cand := "mcp__" + n[idx+2:]
|
||||
if len(cand) > limit {
|
||||
cand = cand[:limit]
|
||||
}
|
||||
return cand
|
||||
}
|
||||
}
|
||||
return n[:limit]
|
||||
}
|
||||
|
||||
makeUnique := func(cand string) string {
|
||||
if _, ok := used[cand]; !ok {
|
||||
return cand
|
||||
}
|
||||
base := cand
|
||||
for i := 1; ; i++ {
|
||||
suffix := "~" + strconv.Itoa(i)
|
||||
allowed := limit - len(suffix)
|
||||
if allowed < 0 {
|
||||
allowed = 0
|
||||
}
|
||||
tmp := base
|
||||
if len(tmp) > allowed {
|
||||
tmp = tmp[:allowed]
|
||||
}
|
||||
tmp = tmp + suffix
|
||||
if _, ok := used[tmp]; !ok {
|
||||
return tmp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, n := range names {
|
||||
cand := baseCandidate(n)
|
||||
uniq := makeUnique(cand)
|
||||
used[uniq] = struct{}{}
|
||||
m[n] = uniq
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -119,7 +119,16 @@ func ConvertCodexResponseToOpenAI(_ context.Context, modelName string, originalR
|
||||
}
|
||||
template, _ = sjson.SetRaw(template, "choices.0.delta.tool_calls", `[]`)
|
||||
functionCallItemTemplate, _ = sjson.Set(functionCallItemTemplate, "id", itemResult.Get("call_id").String())
|
||||
functionCallItemTemplate, _ = sjson.Set(functionCallItemTemplate, "function.name", itemResult.Get("name").String())
|
||||
{
|
||||
// Restore original tool name if it was shortened
|
||||
name := itemResult.Get("name").String()
|
||||
// Build reverse map on demand from original request tools
|
||||
rev := buildReverseMapFromOriginalOpenAI(originalRequestRawJSON)
|
||||
if orig, ok := rev[name]; ok {
|
||||
name = orig
|
||||
}
|
||||
functionCallItemTemplate, _ = sjson.Set(functionCallItemTemplate, "function.name", name)
|
||||
}
|
||||
functionCallItemTemplate, _ = sjson.Set(functionCallItemTemplate, "function.arguments", itemResult.Get("arguments").String())
|
||||
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
|
||||
template, _ = sjson.SetRaw(template, "choices.0.delta.tool_calls.-1", functionCallItemTemplate)
|
||||
@@ -244,7 +253,12 @@ func ConvertCodexResponseToOpenAINonStream(_ context.Context, _ string, original
|
||||
}
|
||||
|
||||
if nameResult := outputItem.Get("name"); nameResult.Exists() {
|
||||
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "function.name", nameResult.String())
|
||||
n := nameResult.String()
|
||||
rev := buildReverseMapFromOriginalOpenAI(originalRequestRawJSON)
|
||||
if orig, ok := rev[n]; ok {
|
||||
n = orig
|
||||
}
|
||||
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "function.name", n)
|
||||
}
|
||||
|
||||
if argsResult := outputItem.Get("arguments"); argsResult.Exists() {
|
||||
@@ -289,3 +303,34 @@ func ConvertCodexResponseToOpenAINonStream(_ context.Context, _ string, original
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// buildReverseMapFromOriginalOpenAI builds a map of shortened tool name -> original tool name
|
||||
// from the original OpenAI-style request JSON using the same shortening logic.
|
||||
func buildReverseMapFromOriginalOpenAI(original []byte) map[string]string {
|
||||
tools := gjson.GetBytes(original, "tools")
|
||||
rev := map[string]string{}
|
||||
if tools.IsArray() && len(tools.Array()) > 0 {
|
||||
var names []string
|
||||
arr := tools.Array()
|
||||
for i := 0; i < len(arr); i++ {
|
||||
t := arr[i]
|
||||
if t.Get("type").String() != "function" {
|
||||
continue
|
||||
}
|
||||
fn := t.Get("function")
|
||||
if !fn.Exists() {
|
||||
continue
|
||||
}
|
||||
if v := fn.Get("name"); v.Exists() {
|
||||
names = append(names, v.String())
|
||||
}
|
||||
}
|
||||
if len(names) > 0 {
|
||||
m := buildShortNameMap(names)
|
||||
for orig, short := range m {
|
||||
rev[short] = orig
|
||||
}
|
||||
}
|
||||
}
|
||||
return rev
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user