Skip to content

[codex] Fix legacy Claude thinking round-trip#13

Open
utopiafar wants to merge 2 commits into
memex-lab:mainfrom
utopiafar:codex/legacy-thinking-roundtrip
Open

[codex] Fix legacy Claude thinking round-trip#13
utopiafar wants to merge 2 commits into
memex-lab:mainfrom
utopiafar:codex/legacy-thinking-roundtrip

Conversation

@utopiafar

@utopiafar utopiafar commented May 15, 2026

Copy link
Copy Markdown
Contributor

摘要

  • 修复旧版 ModelMessage.thought 在 Claude / Bedrock Claude fallback 请求构造中没有被回放为 thinking block 的问题。
  • 对 OpenAI-compatible thinking 模型补齐 reasoning_content 回放:保留显式空字符串,并为 DeepSeek V4 旧 tool-call turn 加兜底。
  • 对齐 Gemini 官方 SDK / 文档的 tool replay 结构:functionResponse.name 使用函数名,单独保留可选 call id。
  • 增加 Claude、Bedrock Claude、OpenAI-compatible reasoning、Gemini tool replay 的回归测试。

根因

PR #10 修到了 Anthropic provider-native contentBlocks 主路径,但旧状态 / 手动构造的 fallback 路径仍然会在 thoughtSignature == null 时丢掉 thinking 内容。thinking/tool-use provider 在下一轮继续请求时需要看到前一轮 reasoning/thinking 内容,因此会报 400。

系统排查后还发现两个同类协议坑:

  • DeepSeek V4 thinking mode 中,带 tool call 的 assistant turn 必须把 reasoning_content 继续传回 API。
  • Gemini function response 的 name 是函数名,call id 是独立字段;thought signature 也需要跟随上一轮模型 part 回放。

兼容性

  • 有 provider-native contentBlocks 时继续优先原样回放。
  • Anthropic signature 存在时继续保留。
  • OpenAI-compatible 的新响应仍然保留真实 reasoning_content;空值兜底只覆盖旧状态 / 丢失 reasoning 的 tool-call turn。
  • Gemini 兼容未返回 call id 的 provider:没有 id 时仍用函数名作为 fallback。

验证

dart test test/openai_client_test.dart test/gemini_client_test.dart test/claude_client_test.dart test/bedrock_claude_client_test.dart
dart analyze

本轮已重新回归,全部通过。

@utopiafar

Copy link
Copy Markdown
Contributor Author

Audit update pushed in 71b3fd9:

  • Added OpenAI-compatible reasoning coverage: preserve explicit empty reasoning_content and add a DeepSeek V4 legacy tool-call fallback.
  • Added Gemini replay fixes: use functionResponse.name for the function name and preserve optional function call IDs separately.
  • Added OpenAIClient and GeminiClient regression tests.

Validation after this update:

dart analyze
dart test

@utopiafar

Copy link
Copy Markdown
Contributor Author

@sparkleMing

@sparkleMing

Copy link
Copy Markdown
Collaborator

@sparkleMing

这个改动用真实的key测过了不

@utopiafar

Copy link
Copy Markdown
Contributor Author

@sparkleMing

这个改动用真实的key测过了不

测过的,不过刚发现这个fix不全,并且暴露出memex另一个架构问题,先不着急merge,再梳理下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants