♻️ refactor(context engine): tool message normalization#13359
♻️ refactor(context engine): tool message normalization#13359arvinxx merged 10 commits intolobehub:canaryfrom
Conversation
|
@cy948 is attempting to deploy a commit to the LobeHub OSS Team on Vercel. A member of the Team first needs to authorize it. |
|
@arvinxx - This is a refactor of tool message normalization in the context engine. Please take a look. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## canary #13359 +/- ##
=========================================
Coverage 66.94% 66.95%
=========================================
Files 1903 1903
Lines 153684 153721 +37
Branches 15534 17693 +2159
=========================================
+ Hits 102889 102925 +36
- Misses 50675 50676 +1
Partials 120 120
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 00bab2b849
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| typeof matchedToolMessage.content === 'string' && | ||
| matchedToolMessage.content.length > 0 | ||
| ? matchedToolMessage.content | ||
| : pluginErrorMessage || DEFAULT_TOOL_FAILURE_CONTENT, |
There was a problem hiding this comment.
Keep empty tool outputs from being rewritten as failures
When a matched tool message has content: '', this branch overwrites it with DEFAULT_TOOL_FAILURE_CONTENT (or pluginError.message). Empty content is a valid successful output in this codebase (e.g., tool invocation stores result.content || result.error?.message || '' in src/store/chat/slices/plugin/actions/pluginTypes.ts:146), so this change can fabricate a failure for successful calls and mislead subsequent model turns into retry/error-recovery paths. The fallback should only be synthesized when the tool message is actually missing, not when a real tool message exists with empty content.
Useful? React with 👍 / 👎.
|
❤️ Great PR @cy948 ❤️ The growth of project is inseparable from user feedback and contribution, thanks for your contribution! If you are interesting with the lobehub developer community, please join our discord and then dm @arvinxx or @canisminor1990. They will invite you to our private developer channel. We are talking about the lobe-chat development or sharing ai newsletter around the world. |
* ♻️ normalize tool call messages in context engine * ♻️ prune tool message normalization implementation * ♻️ prune tool message normalization diff * ♻️ simplify tool message normalization diff * ♻️ restore tool message reorder logging * ♻️ restore reorder tool message shape * ♻️ restore tool message reorder comment * ♻️ prune tool message normalization diff * ♻️ restore tool message reorder shape * 🐛 fix(context-engine): keep empty tool content in reorder
💻 Change Type
🔗 Related Issue
🔀 Description of Change
改造目的是为 context-engine 在整理 tool 调用消息时进行“归一化”的闭环:确保每个保留下来的 tool_call 在最终消息序列里都对应一条可消费的 tool 结果。即使上游含有错误信息,进入 context-engine 后的消息链路仍然是稳定、完整、可继续消费的。
reorderedMessages 与 removedInvalidTools;先去重 assistant 侧重复 tool_calls,再过滤无效或重复的 tool message,并按
tool_call 顺序回填结果。
每个合法 tool_call 都能生成对应的 tool 结果;同时在真实 tool 返回为空时,优先透出 pluginError.message。
统计,不再依赖重排前后消息总数差值,日志语义也随之对齐到“移除了多少无效 tool message”。
tool_call 去重、重复 / 孤儿 tool message 清理、真实错误结果优先于 synthetic fallback 等场景覆盖,并同步更新原有断言。
🧪 How to Test
📸 Screenshots / Videos
📝 Additional Information