mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-18 12:20:52 +08:00
fix(translator): resolve invalid function name errors by sanitizing Claude tool names
This commit centralizes tool name sanitization in SanitizeFunctionName, applying character compliance, starting character rules, and length limits. It also fixes a regression in gemini_schema tests and preserves MCP-specific shortening logic while ensuring compliance. Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
@@ -264,6 +264,18 @@ func ConvertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
|
||||
|
||||
// shortenNameIfNeeded applies a simple shortening rule for a single name.
|
||||
func shortenNameIfNeeded(name string) string {
|
||||
const limit = 64
|
||||
if len(name) <= limit {
|
||||
// Even if within limit, we still apply SanitizeFunctionName to ensure character compliance
|
||||
return util.SanitizeFunctionName(name)
|
||||
}
|
||||
if strings.HasPrefix(name, "mcp__") {
|
||||
idx := strings.LastIndex(name, "__")
|
||||
if idx > 0 {
|
||||
cand := "mcp__" + name[idx+2:]
|
||||
return util.SanitizeFunctionName(cand)
|
||||
}
|
||||
}
|
||||
return util.SanitizeFunctionName(name)
|
||||
}
|
||||
|
||||
@@ -274,6 +286,17 @@ func buildShortNameMap(names []string) map[string]string {
|
||||
m := map[string]string{}
|
||||
|
||||
baseCandidate := func(n string) string {
|
||||
const limit = 64
|
||||
if len(n) <= limit {
|
||||
return util.SanitizeFunctionName(n)
|
||||
}
|
||||
if strings.HasPrefix(n, "mcp__") {
|
||||
idx := strings.LastIndex(n, "__")
|
||||
if idx > 0 {
|
||||
cand := "mcp__" + n[idx+2:]
|
||||
return util.SanitizeFunctionName(cand)
|
||||
}
|
||||
}
|
||||
return util.SanitizeFunctionName(n)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user