🐛 修复内置 AI 多轮对话丢弃工具历史导致编造结果 #230 - #261
Merged
Merged
Conversation
前端回放会话历史时,除名字以 deepseek-v4 开头的模型外,一律把每一轮塌缩成
{role, content},丢掉了 assistant 的 tool_calls 与 role=tool 的工具结果
(aiStore.ts _sendForConversation)。模型跨 turn 看不到自己曾调用过工具,
对话越长越会被自身“直接作答”的历史带偏,从而不再发起真正的工具调用、直接
编造结果——这与 issue 里“与模型无关、和上下文强相关、顶尖模型也躲不掉”的现象吻合。
另有第二个缺陷:存盘/重载会话时 toDisplayMessages / convertDisplayMessages 未
携带 toolCallId,导致即使 deepseek-v4,切走再切回也会因 canExpand=false 退化回塌缩。
- expandToAPIMessages 对所有 provider 展开 tool_calls + 工具结果;reasoning_content
仍仅对 deepseek-v4 回放(它强制要求,且 Anthropic 会拒绝未签名 thinking)
- content-only / 无 text block 的 assistant 历史加兜底,避免展开时丢失回复文本
- 持久化与重载都保留 toolCallId
closes #230
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
问题 (closes #230)
内置 AI 在较长的多轮对话里会开始编造工具结果——声称"已调用接口/已修改配置",但实际没有发起任何工具调用。现象与模型无关(换顶尖模型也躲不掉)、和上下文强相关、对话越长越容易出现。
根因(前端历史回放,两个缺陷)
回放会话历史时,
_sendForConversation对除名字以deepseek-v4开头以外的所有模型,把每一轮塌缩成{role, content},丢掉了 assistant 的tool_calls与role:"tool"工具结果(frontend/src/stores/aiStore.ts)。模型跨 turn 看不到自己曾经调用过工具,长对话里被自身"直接作答"的历史带偏,于是不再发起真正的工具调用、直接编造——正好解释了 issue 里"与模型无关、上下文强相关、顶尖模型也躲不掉"。第二个缺陷:存盘/重载会话时
toDisplayMessages/convertDisplayMessages未携带toolCallId,导致即便deepseek-v4,切走再切回也会因canExpand=false退化回塌缩。改动
expandToAPIMessages对所有 provider 展开tool_calls+ 工具结果;reasoning_content仍仅对deepseek-v4回放(它强制要求带 tool_calls 时回传 reasoning_content,其它 provider 不需要,Anthropic 还会拒绝未签名 thinking)——用新参数includeThinking解耦。toolCallId。测试
toolCallId),先对旧代码跑 → 2 个都 FAIL(复现 bug),对修复跑 → 全绿。pnpm lint干净。附:GLM-5.2 等模型的流式畸形(单独的上游问题)
复现时发现 GLM-5.2 经 OpenAI 兼容代理在流式下会吐出错乱/空名的 tool-call 增量,被 cago 累加成空名 tool_use →
agent: tool not found,是另一个独立的加重项。已验证parallel_tool_calls:false无效(模型忽略该参数);有效的修法是在 cagoagent/loop.go finalizeToolUses丢弃空名 tool_use。这属 cago 上游,不在本 PR 范围。