Repository navigation
fix(gateway): 记录国产厂商原生 Anthropic 直通路径的 reasoning_effort - #5919
Merged
Wei-Shaw merged 1 commit intoAug 21, 2026
Merged
Conversation
Contributor
|
All contributors have signed the CLA. ✅ |
Contributor
There was a problem hiding this comment.
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.
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
force-pushed
the
fix/cn-native-anthropic-reasoning-effort
branch
from
August 20, 2026 12:16
646dbfc to
2074fe3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
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 保证两条网关路径判定规则一致:output_config.effort(NormalizeClaudeOutputEffort)ApplyThinkingEnabledFallback按 passback-required 白名单兜底为high(与 Anthropic 网关语义对齐;DeepSeek 排除等规则不变)ReasoningEffort透传(流式路径 9 处调用点同步更新)测试计划
新增
openai_gateway_messages_anthropic_native_test.go4 个单元测试:output_config.effort=low→ 记录lowthinking.type=enabled(无 effort,模型 k3)→ 兜底记录highoutput_config.effort=max→ 记录max已验证:
go test -tags=unit ./internal/service/全包通过go test -tags=unit ./internal/handler/全包通过go build ./...通过