Skip to content

fix(gateway): 记录国产厂商原生 Anthropic 直通路径的 reasoning_effort - #5919

Merged
Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
clearmann:fix/cn-native-anthropic-reasoning-effort
Aug 21, 2026
Merged

Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
clearmann:fix/cn-native-anthropic-reasoning-effort

Conversation

@clearmann

Copy link
Copy Markdown
Contributor

问题

kimi / zhipu / deepseek 平台分组的 /v1/messages 请求,在账号 api_protocol=anthropic 时走零转换直通路径 forwardAnthropicViaNativeAnthropicEndpoint(openai_gateway_messages_anthropic_native.go)。该路径的两个结果构造函数(handleNativeAnthropicBufferedResponse 非流式 / nativeAnthropicStreamResult 流式)从不设置 ReasoningEffort,导致:

  • 客户端显式携带 output_config.effort(如 Claude Code 的 effort 机制)→ 丢失
  • 客户端启用 thinking(应触发国产模型兜底记为 high)→ 丢失
  • usage_log.reasoning_effort 恒为 NULL,管理端用量页「推理强度」列长期显示 -

对比:Anthropic 平台分组走 Gateway.Messages 有完整的提取逻辑(gateway_handler.go 中 NormalizeClaudeOutputEffort + thinking 兜底),Anthropic→CC 转换路径(openai_gateway_messages_chat_fallback.go)也已记录,唯独这条零转换直通路径遗漏。

修复

在 forwardAnthropicViaNativeAnthropicEndpoint 转发前从入站 body 提取推理强度,复用现有 helper 保证两条网关路径判定规则一致:

reasoningEffort := ApplyThinkingEnabledFallback(
    NormalizeClaudeOutputEffort(gjson.GetBytes(body, "output_config.effort").String()),
    body,
    billingModel,
)
  • 优先 Claude 协议 output_config.effort(NormalizeClaudeOutputEffort)
  • 缺失且 thinking 已启用时,经 ApplyThinkingEnabledFallback 按 passback-required 白名单兜底为 high(与 Anthropic 网关语义对齐;DeepSeek 排除等规则不变)
  • 两个结果构造函数补上 ReasoningEffort 透传(流式路径 9 处调用点同步更新)

测试计划

新增 openai_gateway_messages_anthropic_native_test.go 4 个单元测试:

  • 非流式 + output_config.effort=low → 记录 low
  • 非流式 + thinking.type=enabled(无 effort,模型 k3)→ 兜底记录 high
  • 流式 + output_config.effort=max → 记录 max
  • 无 effort 且无 thinking → 保持 nil(不做语义注入的守卫场景)

已验证:

  • 新增测试修复前 3 个失败(复现)、修复后全绿
  • go test -tags=unit ./internal/service/ 全包通过
  • go test -tags=unit ./internal/handler/ 全包通过
  • go build ./... 通过

Copilot AI lite review requested due to automatic review settings August 20, 2026 12:03
@github-actions

github-actions Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA. ✅
Posted by the CLA Assistant Lite bot.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 修复了国产厂商(kimi/zhipu/deepseek)在 api_protocol=anthropic 且走原生 Anthropic /v1/messages 直通路径时,usage_log.reasoning_effort 长期为 NULL 的问题;通过在转发前复用现有 helper 统一推理强度判定逻辑,并将结果透传到直通路径的非流式/流式结果构造中,确保用量页“推理强度”可正确展示且与其它网关路径语义对齐。

Changes:

  • 在 forwardAnthropicViaNativeAnthropicEndpoint 中从入站 body 提取 output_config.effort 并应用 thinking-enabled fallback,得到 reasoningEffort。
  • 为非流式与流式直通结果构造补齐 OpenAIForwardResult.ReasoningEffort 透传(流式路径所有返回点同步传参)。
  • 新增 4 个单元测试覆盖:显式 effort、thinking 兜底、流式 effort、以及“无 effort 且无 thinking 保持 nil”。

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
backend/internal/service/openai_gateway_messages_anthropic_native.go 在 CN Anthropic 原生直通路径提取并透传 ReasoningEffort 到 buffered/streaming 结果,补齐 usage 记录语义。
backend/internal/service/openai_gateway_messages_anthropic_native_test.go 新增单测覆盖直通路径的 effort 提取与 thinking fallback 行为(含流式与非流式)。

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@clearmann

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

kimi/zhipu/deepseek 平台分组的 /v1/messages 请求在账号 api_protocol=anthropic
时走零转换直通路径(forwardAnthropicViaNativeAnthropicEndpoint)。该路径的
两个结果构造函数(非流式/流式)从不设置 ReasoningEffort,导致客户端显式
携带 output_config.effort 或启用 thinking 时,usage_log.reasoning_effort
仍恒为 NULL,用量页推理强度列长期显示 '-'。

修复:转发前从入站 body 提取推理强度——优先 Claude 协议 output_config.effort
(NormalizeClaudeOutputEffort),缺失且 thinking 已启用时经
ApplyThinkingEnabledFallback 按 passback-required 白名单兜底为 high,与
Anthropic 网关(gateway_handler)的记录语义对齐;两个结果构造函数补上
ReasoningEffort 字段透传。

测试:新增 4 个单元测试覆盖显式 effort(非流式/流式)、thinking 兜底、
无字段保持 nil 的守卫场景。
@clearmann
clearmann force-pushed the fix/cn-native-anthropic-reasoning-effort branch from 646dbfc to 2074fe3 Compare August 20, 2026 12:16
github-actions Bot added a commit that referenced this pull request Aug 20, 2026
@Wei-Shaw
Wei-Shaw merged commit 9f74eb5 into Wei-Shaw:main Aug 21, 2026
8 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants