Skip to content

fix: parse tool_use/tool_result messages and add OpenCode SQLite backend#1401

Open
BlueOcean223 wants to merge 2 commits intofarion1231:mainfrom
BlueOcean223:fix/session-tool-message-parsing
Open

fix: parse tool_use/tool_result messages and add OpenCode SQLite backend#1401
BlueOcean223 wants to merge 2 commits intofarion1231:mainfrom
BlueOcean223:fix/session-tool-message-parsing

Conversation

@BlueOcean223
Copy link

问题

之前 session 消息加载存在以下问题:

  1. 工具调用消息错误归类/丢失:Claude 的 tool_use/tool_result会被解析为用户消息。Codex 的 function_call/function_call_output会被直接忽略。Gemini 的 toolCalls 在加载会话时被忽略或显示为空,导致对话历史不完整,工具交互过程无法回溯 。 前端提供了工具的卡片,但由于后端错误导致实际上没有使用。
  2. OpenCode 仅支持 JSON 存储:OpenCode 较新版本已迁移到 SQLite(opencode.db)存储 session,当前只读取旧版 JSON 文件,新版用户看不到任何会话记录
  3. 时间戳解析不全parse_timestamp_to_ms 仅支持 RFC3339 字符串,OpenCode SQLite
    中存储的整数时间戳(秒/毫秒)无法解析
  4. Gemini 内容格式不兼容:Gemini 的 content 字段有时为 [{text: "..."}]
    数组格式,之前仅处理了字符串格式;info/error 类型的系统消息也被错误地展示出来

更新

  • tool_use/tool_result 解析:Claude、Codex、Gemini 三个 provider 现在能正确解析工具调用消息,以 [Tool: name]
    格式展示,tool_result 归类为 tool 角色
  • OpenCode SQLite 后端:新增对 OpenCode SQLite 数据库(opencode.db)的 session 扫描、消息加载和删除支持,与原 JSON 存储自动合并去重
  • 时间戳解析增强parse_timestamp_to_ms 支持整数(秒/毫秒)和 RFC3339 字符串
  • Gemini 内容格式兼容:支持 content 为字符串或 [{text: ...}] 数组两种格式,过滤 info/error 类型消息

效果比对

claude code

修改前

会将工具信息解析为用户信息
image


修改后

image

codex

修复前

image

修复后

image

gemini

修复前

消息丢失
image


修复后

image

opencode

修复前

无法解析用户的最新消息,且时间解析错误
image


修复后

image

@BlueOcean223 BlueOcean223 force-pushed the fix/session-tool-message-parsing branch 2 times, most recently from cf8820d to 1047a34 Compare March 11, 2026 07:41
  - Claude: reclassify user messages containing tool_result as "tool" role
  - Codex: handle function_call and function_call_output payload types
  - Gemini: support array content and toolCalls extraction, filter info/error types
  - OpenCode: add SQLite session scan, load and delete alongside legacy JSON
  - utils: extend parse_timestamp_to_ms for integer timestamps, extract tool_use/tool_result in shared extract_text
  - Claude: fix role misclassification for mixed user+tool_result messages (any → all)
  - OpenCode: extract duplicate part text logic into extract_part_text()
  - OpenCode: add path validation for SQLite delete to prevent foreign DB access
  - OpenCode: wrap SQLite deletion in transaction for atomicity
  - openclaw_config: remove redundant as_deref() on Option<&str>
@BlueOcean223 BlueOcean223 force-pushed the fix/session-tool-message-parsing branch from f314dfd to eb01c6d Compare March 11, 2026 08:33
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.

1 participant