fix(gateway): 修复工具名转换破坏 Anthropic 特殊工具的问题
未知工具名不再进行 PascalCase/snake_case 转换,保持原样透传。 修复 text_editor_20250728 等 Anthropic 特殊工具被错误转换的问题。
This commit is contained in:
@@ -20,7 +20,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
|
||||||
|
|
||||||
"github.com/Wei-Shaw/sub2api/internal/config"
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
||||||
"github.com/Wei-Shaw/sub2api/internal/pkg/claude"
|
"github.com/Wei-Shaw/sub2api/internal/pkg/claude"
|
||||||
@@ -620,35 +619,6 @@ func stripToolPrefix(value string) string {
|
|||||||
return toolPrefixRe.ReplaceAllString(value, "")
|
return toolPrefixRe.ReplaceAllString(value, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func toPascalCase(value string) string {
|
|
||||||
if value == "" {
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
normalized := toolNameBoundaryRe.ReplaceAllString(value, " ")
|
|
||||||
tokens := make([]string, 0)
|
|
||||||
for _, token := range strings.Fields(normalized) {
|
|
||||||
expanded := toolNameCamelRe.ReplaceAllString(token, "$1 $2")
|
|
||||||
parts := strings.Fields(expanded)
|
|
||||||
if len(parts) > 0 {
|
|
||||||
tokens = append(tokens, parts...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(tokens) == 0 {
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
var builder strings.Builder
|
|
||||||
for _, token := range tokens {
|
|
||||||
lower := strings.ToLower(token)
|
|
||||||
if lower == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
runes := []rune(lower)
|
|
||||||
runes[0] = unicode.ToUpper(runes[0])
|
|
||||||
_, _ = builder.WriteString(string(runes))
|
|
||||||
}
|
|
||||||
return builder.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func toSnakeCase(value string) string {
|
func toSnakeCase(value string) string {
|
||||||
if value == "" {
|
if value == "" {
|
||||||
return value
|
return value
|
||||||
@@ -664,16 +634,15 @@ func normalizeToolNameForClaude(name string, cache map[string]string) string {
|
|||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
stripped := stripToolPrefix(name)
|
stripped := stripToolPrefix(name)
|
||||||
|
// 只对已知的工具名进行映射,未知工具名保持原样
|
||||||
|
// 避免破坏 Anthropic 特殊工具(如 text_editor_20250728)
|
||||||
mapped, ok := claudeToolNameOverrides[strings.ToLower(stripped)]
|
mapped, ok := claudeToolNameOverrides[strings.ToLower(stripped)]
|
||||||
if !ok {
|
if !ok {
|
||||||
mapped = toPascalCase(stripped)
|
|
||||||
}
|
|
||||||
if mapped != "" && cache != nil && mapped != stripped {
|
|
||||||
cache[mapped] = stripped
|
|
||||||
}
|
|
||||||
if mapped == "" {
|
|
||||||
return stripped
|
return stripped
|
||||||
}
|
}
|
||||||
|
if cache != nil && mapped != stripped {
|
||||||
|
cache[mapped] = stripped
|
||||||
|
}
|
||||||
return mapped
|
return mapped
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -682,15 +651,18 @@ func normalizeToolNameForOpenCode(name string, cache map[string]string) string {
|
|||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
stripped := stripToolPrefix(name)
|
stripped := stripToolPrefix(name)
|
||||||
|
// 优先从请求时建立的映射中查找
|
||||||
if cache != nil {
|
if cache != nil {
|
||||||
if mapped, ok := cache[stripped]; ok {
|
if mapped, ok := cache[stripped]; ok {
|
||||||
return mapped
|
return mapped
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 已知工具名的硬编码映射
|
||||||
if mapped, ok := openCodeToolOverrides[stripped]; ok {
|
if mapped, ok := openCodeToolOverrides[stripped]; ok {
|
||||||
return mapped
|
return mapped
|
||||||
}
|
}
|
||||||
return toSnakeCase(stripped)
|
// 未知工具名保持原样,避免破坏 Anthropic 特殊工具
|
||||||
|
return stripped
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeParamNameForOpenCode(name string, cache map[string]string) string {
|
func normalizeParamNameForOpenCode(name string, cache map[string]string) string {
|
||||||
|
|||||||
Reference in New Issue
Block a user