IanShaw027
539b41f421
feat(openai): 添加Codex工具调用自动修正功能
实现了完整的Codex工具调用拦截和自动修正系统,解决OpenCode使用Codex模型时的工具调用兼容性问题。
**核心功能:**
1. **工具名称自动映射**
- apply_patch/applyPatch → edit
- update_plan/updatePlan → todowrite
- read_plan/readPlan → todoread
- search_files/searchFiles → grep
- list_files/listFiles → glob
- read_file/readFile → read
- write_file/writeFile → write
- execute_bash/executeBash/exec_bash/execBash → bash
2. **工具参数自动修正**
- bash: 自动移除不支持的 workdir/work_dir 参数
- edit: 自动将 path 参数重命名为 file_path
- 支持 JSON 字符串和对象两种参数格式
3. **流式响应集成**
- 在 SSE 数据流中实时修正工具调用
- 支持多种 JSON 结构(tool_calls, function_call, delta, choices等)
- 不影响响应性能和用户体验
4. **统计和监控**
- 记录每次工具修正的详细信息
- 提供修正统计数据查询
- 便于问题排查和性能优化
**实现文件:**
- `openai_tool_corrector.go`: 工具修正核心逻辑(250行)
- `openai_tool_corrector_test.go`: 完整的单元测试(380+行)
- `openai_gateway_service.go`: 流式响应集成
- `openai_gateway_service_tool_correction_test.go`: 集成测试
**测试覆盖:**
- 工具名称映射测试(18个映射规则)
- 参数修正测试(bash workdir、edit path等)
- SSE数据修正测试(多种JSON结构)
- 统计功能测试
- 所有测试通过 ✅
**解决的问题:**
修复了 OpenCode 使用 sub2api 中转 Codex 时,因工具名称和参数不兼容导致的工具调用失败问题。
Codex 模型有时会忽略指令文件中的工具映射说明,导致调用不存在的工具(如 apply_patch)。
现在通过流式响应拦截,自动将错误的工具调用修正为 OpenCode 兼容的格式。
**参考文档:**
- OpenCode 工具规范: https://opencode.ai/docs/
- Codex Bridge 指令: backend/internal/service/prompts/codex_opencode_bridge.txt
2026-01-15 23:52:50 +08:00
..
2026-01-13 11:14:32 +08:00
2026-01-15 15:14:44 +08:00
2026-01-07 16:59:35 +08:00
2025-12-26 16:45:40 +08:00
2026-01-09 22:00:14 +08:00
2026-01-15 15:14:44 +08:00
2026-01-12 13:29:04 -08:00
2026-01-15 15:14:44 +08:00
2026-01-15 15:14:44 +08:00
2026-01-15 15:14:44 +08:00
2026-01-01 18:59:38 +08:00
2026-01-15 15:14:44 +08:00
2026-01-10 10:00:49 +08:00
2026-01-09 19:43:19 +08:00
2026-01-11 10:55:25 +08:00
2026-01-15 15:15:20 +08:00
2026-01-05 00:56:48 +08:00
2026-01-15 15:14:44 +08:00
2026-01-05 17:14:06 +08:00
2026-01-03 06:29:02 -08:00
2025-12-31 10:33:00 +08:00
2026-01-03 06:29:02 -08:00
2026-01-09 22:00:14 +08:00
2026-01-14 15:55:44 +08:00
2025-12-31 23:37:51 +08:00
2026-01-11 10:55:25 +08:00
2026-01-11 10:55:25 +08:00
2026-01-11 10:55:25 +08:00
2026-01-11 10:55:25 +08:00
2026-01-11 10:55:25 +08:00
2026-01-11 10:55:25 +08:00
2026-01-09 21:59:32 +08:00
2026-01-11 10:55:26 +08:00
2026-01-10 13:23:03 +08:00
2026-01-12 09:14:32 +08:00
2025-12-25 17:15:01 +08:00
2025-12-31 08:50:12 +08:00
2026-01-04 22:10:32 +08:00
2026-01-05 17:07:29 +08:00
2026-01-05 17:07:29 +08:00
2026-01-08 23:07:00 +08:00
2026-01-15 18:27:06 +08:00
2026-01-15 19:08:07 +08:00
2026-01-03 06:32:51 -08:00
2026-01-05 14:41:08 +08:00
2026-01-11 18:20:15 +08:00
2026-01-12 10:59:52 +08:00
2026-01-11 18:20:15 +08:00
2026-01-15 15:14:44 +08:00
2025-12-28 09:49:54 +08:00
2026-01-11 21:54:52 -08:00
2025-12-18 13:50:39 +08:00
2026-01-09 14:49:20 +08:00
2026-01-13 22:49:26 -08:00
2026-01-04 10:45:18 +08:00
2026-01-05 00:56:48 +08:00
2026-01-04 18:26:39 -08:00
2025-12-31 08:50:12 +08:00
2026-01-15 19:42:18 +08:00
2026-01-01 04:21:18 +08:00
2026-01-15 15:14:44 +08:00
2026-01-10 09:39:02 +08:00
2026-01-08 23:47:29 +08:00
2026-01-08 23:47:29 +08:00
2025-12-26 00:11:03 -08:00
2026-01-04 15:36:00 +08:00
2026-01-14 15:55:44 +08:00
2026-01-15 19:08:07 +08:00
2025-12-31 16:25:45 +08:00
2025-12-26 00:11:03 -08:00
2026-01-11 10:55:25 +08:00
2026-01-05 17:07:29 +08:00
2026-01-10 08:40:27 +08:00
2025-12-31 11:43:58 +08:00
2025-12-25 17:15:01 +08:00
2025-12-27 20:42:00 +08:00
2026-01-14 09:17:58 +08:00
2026-01-14 09:46:10 +08:00
2026-01-13 22:49:26 -08:00
2026-01-15 23:52:50 +08:00
2026-01-15 23:52:50 +08:00
2025-12-26 16:45:40 +08:00
2026-01-15 18:27:06 +08:00
2026-01-15 19:08:07 +08:00
2026-01-13 16:47:35 +08:00
2026-01-13 16:47:35 +08:00
2026-01-15 23:52:50 +08:00
2026-01-15 23:52:50 +08:00
2026-01-11 20:33:52 +08:00
2026-01-10 01:38:47 +08:00
2026-01-10 01:38:47 +08:00
2026-01-11 20:55:44 +08:00
2026-01-15 15:14:44 +08:00
2026-01-15 15:14:44 +08:00
2026-01-15 15:14:44 +08:00
2026-01-11 23:57:20 +08:00
2026-01-09 20:53:44 +08:00
2026-01-10 01:38:47 +08:00
2026-01-10 01:38:47 +08:00
2026-01-09 20:53:44 +08:00
2026-01-15 15:14:44 +08:00
2026-01-15 15:14:44 +08:00
2026-01-09 20:53:44 +08:00
2026-01-11 23:00:31 +08:00
2026-01-15 15:14:44 +08:00
2026-01-15 15:14:44 +08:00
2026-01-09 20:53:44 +08:00
2026-01-09 20:52:17 +08:00
2026-01-12 14:17:42 +08:00
2026-01-12 14:17:42 +08:00
2026-01-09 20:53:44 +08:00
2026-01-11 23:57:20 +08:00
2026-01-15 15:14:44 +08:00
2026-01-11 23:26:29 +08:00
2026-01-15 15:14:44 +08:00
2026-01-15 15:14:44 +08:00
2026-01-15 15:14:44 +08:00
2026-01-09 20:52:17 +08:00
2026-01-12 00:02:19 +08:00
2026-01-15 15:14:44 +08:00
2026-01-09 20:53:44 +08:00
2026-01-06 10:49:26 +08:00
2026-01-10 13:14:35 +08:00
2026-01-10 13:14:35 +08:00
2026-01-11 10:55:25 +08:00
2026-01-15 15:15:20 +08:00
2026-01-15 15:14:44 +08:00
2026-01-15 15:15:20 +08:00
2026-01-03 06:29:02 -08:00
2026-01-15 15:06:34 +08:00
2026-01-15 19:08:07 +08:00
2025-12-26 16:45:40 +08:00
2026-01-11 10:55:26 +08:00
2026-01-12 14:19:06 +08:00
2026-01-12 14:19:06 +08:00
2026-01-12 14:19:06 +08:00
2026-01-12 14:19:06 +08:00
2026-01-11 22:09:35 -08:00
2025-12-26 16:45:40 +08:00
2026-01-11 22:09:35 -08:00
2025-12-31 23:42:01 +08:00
2026-01-11 21:54:52 -08:00
2025-12-28 09:49:54 +08:00
2026-01-15 19:08:07 +08:00
2026-01-15 18:27:06 +08:00
2026-01-15 19:08:07 +08:00
2026-01-15 18:27:06 +08:00
2026-01-15 18:27:06 +08:00
2026-01-15 18:27:06 +08:00
2026-01-04 19:28:20 +08:00
2025-12-31 16:25:45 +08:00
2025-12-31 23:42:01 +08:00
2026-01-04 19:28:20 +08:00
2026-01-15 15:14:44 +08:00
2026-01-11 10:55:25 +08:00
2026-01-03 06:37:08 -08:00
2026-01-01 18:58:34 +08:00
2026-01-11 10:55:25 +08:00
2025-12-26 16:45:40 +08:00
2025-12-26 16:45:40 +08:00
2026-01-04 19:28:20 +08:00
2026-01-15 18:27:06 +08:00