fix(gateway): 修复模型前缀映射逻辑错误

问题:normalizeClaudeModelForAnthropic 函数错误地将长模型ID截断为短ID,
导致 APIKey 账号的模型名被错误修改。

修复:
- 删除错误的 normalizeClaudeModelForAnthropic 函数和 anthropicPrefixMappings 变量
- 直接使用 claude.NormalizeModelID(正确的短ID->长ID扩展)
- APIKey 账号无显式映射时透传原始模型名
This commit is contained in:
shaw
2026-02-04 17:50:05 +08:00
parent ac4371fa98
commit 8f39754812

View File

@@ -70,15 +70,6 @@ func shortSessionHash(sessionHash string) string {
return sessionHash[:8] return sessionHash[:8]
} }
func normalizeClaudeModelForAnthropic(requestedModel string) string {
for _, prefix := range anthropicPrefixMappings {
if strings.HasPrefix(requestedModel, prefix) {
return prefix
}
}
return requestedModel
}
func redactAuthHeaderValue(v string) string { func redactAuthHeaderValue(v string) string {
v = strings.TrimSpace(v) v = strings.TrimSpace(v)
if v == "" { if v == "" {
@@ -261,12 +252,6 @@ var (
"You are a file search specialist for Claude Code", // Explore Agent 版 "You are a file search specialist for Claude Code", // Explore Agent 版
"You are a helpful AI assistant tasked with summarizing conversations", // Compact 版 "You are a helpful AI assistant tasked with summarizing conversations", // Compact 版
} }
anthropicPrefixMappings = []string{
"claude-opus-4-5",
"claude-haiku-4-5",
"claude-sonnet-4-5",
}
) )
// ErrClaudeCodeOnly 表示分组仅允许 Claude Code 客户端访问 // ErrClaudeCodeOnly 表示分组仅允许 Claude Code 客户端访问
@@ -2576,8 +2561,9 @@ func (s *GatewayService) isModelSupportedByAccount(account *Account, requestedMo
// Antigravity 平台使用专门的模型支持检查 // Antigravity 平台使用专门的模型支持检查
return IsAntigravityModelSupported(requestedModel) return IsAntigravityModelSupported(requestedModel)
} }
if account.Platform == PlatformAnthropic { // OAuth/SetupToken 账号使用 Anthropic 标准映射短ID → 长ID
requestedModel = normalizeClaudeModelForAnthropic(requestedModel) if account.Platform == PlatformAnthropic && account.Type != AccountTypeAPIKey {
requestedModel = claude.NormalizeModelID(requestedModel)
} }
// Gemini API Key 账户直接透传,由上游判断模型是否支持 // Gemini API Key 账户直接透传,由上游判断模型是否支持
if account.Platform == PlatformGemini && account.Type == AccountTypeAPIKey { if account.Platform == PlatformGemini && account.Type == AccountTypeAPIKey {
@@ -3028,7 +3014,9 @@ func (s *GatewayService) Forward(ctx context.Context, c *gin.Context, account *A
// 强制执行 cache_control 块数量限制(最多 4 个) // 强制执行 cache_control 块数量限制(最多 4 个)
body = enforceCacheControlLimit(body) body = enforceCacheControlLimit(body)
// 应用模型映射APIKey 明确映射优先,其次使用 Anthropic 前缀映射) // 应用模型映射
// - APIKey 账号:使用账号级别的显式映射(如果配置),否则透传原始模型名
// - OAuth/SetupToken 账号:使用 Anthropic 标准映射短ID → 长ID
mappedModel := reqModel mappedModel := reqModel
mappingSource := "" mappingSource := ""
if account.Type == AccountTypeAPIKey { if account.Type == AccountTypeAPIKey {
@@ -3037,8 +3025,8 @@ func (s *GatewayService) Forward(ctx context.Context, c *gin.Context, account *A
mappingSource = "account" mappingSource = "account"
} }
} }
if mappingSource == "" && account.Platform == PlatformAnthropic { if mappingSource == "" && account.Platform == PlatformAnthropic && account.Type != AccountTypeAPIKey {
normalized := normalizeClaudeModelForAnthropic(reqModel) normalized := claude.NormalizeModelID(reqModel)
if normalized != reqModel { if normalized != reqModel {
mappedModel = normalized mappedModel = normalized
mappingSource = "prefix" mappingSource = "prefix"
@@ -4979,7 +4967,9 @@ func (s *GatewayService) ForwardCountTokens(ctx context.Context, c *gin.Context,
return nil return nil
} }
// 应用模型映射APIKey 明确映射优先,其次使用 Anthropic 前缀映射) // 应用模型映射
// - APIKey 账号:使用账号级别的显式映射(如果配置),否则透传原始模型名
// - OAuth/SetupToken 账号:使用 Anthropic 标准映射短ID → 长ID
if reqModel != "" { if reqModel != "" {
mappedModel := reqModel mappedModel := reqModel
mappingSource := "" mappingSource := ""
@@ -4989,8 +4979,8 @@ func (s *GatewayService) ForwardCountTokens(ctx context.Context, c *gin.Context,
mappingSource = "account" mappingSource = "account"
} }
} }
if mappingSource == "" && account.Platform == PlatformAnthropic { if mappingSource == "" && account.Platform == PlatformAnthropic && account.Type != AccountTypeAPIKey {
normalized := normalizeClaudeModelForAnthropic(reqModel) normalized := claude.NormalizeModelID(reqModel)
if normalized != reqModel { if normalized != reqModel {
mappedModel = normalized mappedModel = normalized
mappingSource = "prefix" mappingSource = "prefix"