refactor(bedrock): merge header filtering into ApplyBedrockCCCompat

Move anthropic-beta header filtering from separate FilterBedrockBetaHeader
into ApplyBedrockCCCompat, so one function handles all CC compat processing
(body cleanup + header filtering). Change signature from ctx to *gin.Context
to access request headers. Remove the redundant separate call in handler.
This commit is contained in:
erio
2026-06-10 00:18:09 +08:00
parent bf28a0098a
commit 12962bab24
2 changed files with 14 additions and 5 deletions
+2 -2
View File
@@ -769,8 +769,8 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
return
}
}
// Bedrock CC 兼容:渠道模型映射后,清理 Anthropic API 专有字段、注入 Bedrock 必需字段
if err := attemptParsedReq.ReplaceBody(h.gatewayService.ApplyBedrockCCCompat(c.Request.Context(), attemptParsedReq.Body.Bytes(), attemptParsedReq.Model, account, apiKey.GroupID)); err != nil {
// Bedrock CC 兼容:清理 body 专有字段 + 过滤 anthropic-beta header,适用于所有转发路径
if err := attemptParsedReq.ReplaceBody(h.gatewayService.ApplyBedrockCCCompat(c, attemptParsedReq.Body.Bytes(), attemptParsedReq.Model, account, apiKey.GroupID)); err != nil {
h.errorResponse(c, http.StatusBadRequest, "invalid_request_error", "Failed to parse request body")
return
}
+12 -3
View File
@@ -5860,15 +5860,24 @@ func writeAnthropicPassthroughResponseHeaders(dst http.Header, src http.Header,
}
// ApplyBedrockCCCompat 应用 Bedrock CC 兼容转换(渠道级模型映射后调用)
// 清理 Anthropic API 专有字段、注入 Bedrock 必需字段、修复 thinking/tool_use ID
func (s *GatewayService) ApplyBedrockCCCompat(ctx context.Context, body []byte, model string, account *Account, groupID *int64) []byte {
if !s.isBedrockCCCompatEnabled(ctx, account, groupID) {
// 清理 body 中 Anthropic API 专有字段、修复 thinking/tool_use ID、过滤 beta token,
// 同时过滤 HTTP header 中的 anthropic-beta(防止 Passthrough 路径透传不支持的 token)。
func (s *GatewayService) ApplyBedrockCCCompat(c *gin.Context, body []byte, model string, account *Account, groupID *int64) []byte {
if !s.isBedrockCCCompatEnabled(c.Request.Context(), account, groupID) {
return body
}
body = sanitizeBedrockCCFields(body)
body = sanitizeBedrockThinking(body, model)
body = sanitizeBedrockToolUseIDs(body)
body = sanitizeBedrockCCBetaTokens(body, model)
// 过滤 HTTP header 中的 anthropic-beta,只保留 Bedrock 支持的 token
if betaHeader := c.GetHeader("anthropic-beta"); betaHeader != "" {
if filtered := ResolveBedrockBetaTokens(betaHeader, body, model); len(filtered) > 0 {
c.Request.Header.Set("anthropic-beta", strings.Join(filtered, ", "))
} else {
c.Request.Header.Del("anthropic-beta")
}
}
return body
}