mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50: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.
|
// shortenNameIfNeeded applies a simple shortening rule for a single name.
|
||||||
func shortenNameIfNeeded(name string) string {
|
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)
|
return util.SanitizeFunctionName(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,6 +286,17 @@ func buildShortNameMap(names []string) map[string]string {
|
|||||||
m := map[string]string{}
|
m := map[string]string{}
|
||||||
|
|
||||||
baseCandidate := func(n 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)
|
return util.SanitizeFunctionName(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -390,6 +390,11 @@ func addEmptySchemaPlaceholder(jsonStr string) string {
|
|||||||
|
|
||||||
// If schema has properties but none are required, add a minimal placeholder.
|
// If schema has properties but none are required, add a minimal placeholder.
|
||||||
if propsVal.IsObject() && !hasRequiredProperties {
|
if propsVal.IsObject() && !hasRequiredProperties {
|
||||||
|
// DO NOT add placeholder if it's a top-level schema (parentPath is empty)
|
||||||
|
// or if we've already added a placeholder reason above.
|
||||||
|
if parentPath == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
placeholderPath := joinPath(propsPath, "_")
|
placeholderPath := joinPath(propsPath, "_")
|
||||||
if !gjson.Get(jsonStr, placeholderPath).Exists() {
|
if !gjson.Get(jsonStr, placeholderPath).Exists() {
|
||||||
jsonStr, _ = sjson.Set(jsonStr, placeholderPath+".type", "boolean")
|
jsonStr, _ = sjson.Set(jsonStr, placeholderPath+".type", "boolean")
|
||||||
|
|||||||
@@ -127,8 +127,10 @@ func TestCleanJSONSchemaForAntigravity_AnyOfFlattening_SmartSelection(t *testing
|
|||||||
"type": "object",
|
"type": "object",
|
||||||
"description": "Accepts: null | object",
|
"description": "Accepts: null | object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
"_": { "type": "boolean" },
|
||||||
"kind": { "type": "string" }
|
"kind": { "type": "string" }
|
||||||
}
|
},
|
||||||
|
"required": ["_"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ func TestSanitizeFunctionName(t *testing.T) {
|
|||||||
{"Too long (65 chars)", "this_is_a_very_long_name_that_exactly_reaches_sixty_four_charactX", "this_is_a_very_long_name_that_exactly_reaches_sixty_four_charact"},
|
{"Too long (65 chars)", "this_is_a_very_long_name_that_exactly_reaches_sixty_four_charactX", "this_is_a_very_long_name_that_exactly_reaches_sixty_four_charact"},
|
||||||
{"Very long", "this_is_a_very_long_name_that_exceeds_the_sixty_four_character_limit_for_function_names", "this_is_a_very_long_name_that_exceeds_the_sixty_four_character_l"},
|
{"Very long", "this_is_a_very_long_name_that_exceeds_the_sixty_four_character_limit_for_function_names", "this_is_a_very_long_name_that_exceeds_the_sixty_four_character_l"},
|
||||||
{"Starts with digit (64 chars total)", "1234567890123456789012345678901234567890123456789012345678901234", "_123456789012345678901234567890123456789012345678901234567890123"},
|
{"Starts with digit (64 chars total)", "1234567890123456789012345678901234567890123456789012345678901234", "_123456789012345678901234567890123456789012345678901234567890123"},
|
||||||
|
{"Starts with invalid char (64 chars total)", "!234567890123456789012345678901234567890123456789012345678901234", "_234567890123456789012345678901234567890123456789012345678901234"},
|
||||||
{"Empty", "", ""},
|
{"Empty", "", ""},
|
||||||
{"Single character invalid", "@", "_"},
|
{"Single character invalid", "@", "_"},
|
||||||
{"Single character valid", "a", "a"},
|
{"Single character valid", "a", "a"},
|
||||||
|
|||||||
@@ -25,19 +25,26 @@ func SanitizeFunctionName(name string) string {
|
|||||||
if name == "" {
|
if name == "" {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace invalid characters with underscore
|
// Replace invalid characters with underscore
|
||||||
sanitized := functionNameSanitizer.ReplaceAllString(name, "_")
|
sanitized := functionNameSanitizer.ReplaceAllString(name, "_")
|
||||||
|
|
||||||
// Ensure it starts with a letter or underscore
|
// Ensure it starts with a letter or underscore
|
||||||
first := sanitized[0]
|
// Re-reading requirements: Must start with a letter or an underscore.
|
||||||
if !((first >= 'a' && first <= 'z') || (first >= 'A' && first <= 'Z') || first == '_') {
|
if len(sanitized) > 0 {
|
||||||
// If it starts with an allowed character but not allowed at the beginning,
|
first := sanitized[0]
|
||||||
// we must prepend an underscore.
|
if !((first >= 'a' && first <= 'z') || (first >= 'A' && first <= 'Z') || first == '_') {
|
||||||
// To stay within the 64-character limit while prepending, we may need to truncate first.
|
// If it starts with an allowed character but not allowed at the beginning (digit, dot, colon, dash),
|
||||||
if len(sanitized) >= 64 {
|
// we must prepend an underscore.
|
||||||
sanitized = sanitized[:63]
|
|
||||||
|
// To stay within the 64-character limit while prepending, we must truncate first.
|
||||||
|
if len(sanitized) >= 64 {
|
||||||
|
sanitized = sanitized[:63]
|
||||||
|
}
|
||||||
|
sanitized = "_" + sanitized
|
||||||
}
|
}
|
||||||
sanitized = "_" + sanitized
|
} else {
|
||||||
|
sanitized = "_"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Truncate to 64 characters
|
// Truncate to 64 characters
|
||||||
|
|||||||
Reference in New Issue
Block a user