fix(protocol_proxy): chat/completions 转发支持 tool_search,保留 mcp_servers (issue #2263) - #2265
Merged
Merged
Conversation
… (issue BigPizzaV3#2263) Responses → Chat Completions 转发路径此前把 Codex 客户端发出的 type: "tool_search" 工具静默丢弃(落入 _ => {} 分支),导致模型侧 完全看不到该工具、无法检索任何 mcp__* 工具,表现为 MCP 全部不可用。 去程: - responses_tools_to_chat_tools 新增 tool_search 分支,按 function 工具透传(名字保持 tool_search,description/parameters 原样保留) - build_codex_tool_context 将 tool_search 登记为 ToolSearch 类代理工具 回程(模型调用还原): - 非流式与流式路径均还原成 tool_search_call item(execution: client, arguments 为对象),item id 使用官方 tsc_ 前缀;官方客户端的 tool_search handler 只接受 tool_search_call,function_call 形态会被 "unsupported payload" 拒绝 - 流式 delta/done 事件走 response.function_call_arguments.*,客户端 按 tool_search_call.arguments 聚合 历史回放: - append_responses_item 新增 tool_search_call / tool_search_output 分支,分别映射为 assistant tool_call 与 role:tool 消息,保持调用 配对完整 附带修复(同 issue 报告): - preserve_live_app_settings 重写 config.toml 时通过 preserve_missing_table_keys 保留 live 配置里的 mcp_servers 条目 (只补缺不覆盖,通用配置/模板已有条目优先),避免每次 apply 后 MCP server 逐个消失 测试:新增 5 个回归测试覆盖去程透传、非流式/流式还原、历史回放与 mcp_servers 保留。
- 孤儿 tool_search_output(压缩/截断后常见)降级为 user 消息不丢内容
- 畸形 arguments 走 {"input": ...} 包装兜底,不产出非法 item
- tool_search_call 缺 call_id 时回退 id 字段、两者皆空则丢弃
- 未声明的 tool_search 越权调用退化为普通 function_call(钉住默认行为)
- live 配置无 mcp_servers 时不注入空段(负例)
全量 cargo test:1221 passed / 0 failed。
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.
Fixes #2263
Summary
chat/completions 转发路径此前把 Codex 客户端发出的
type: "tool_search"工具静默丢弃(落入_ => {}分支),导致模型侧完全看不到该工具、无法检索任何mcp__*工具,表现为 MCP 工具全部不可用。本 PR 双向打通 tool_search 的转发,并附带修复 config.toml 重写时 MCP 条目被逐次删除的问题。协议形态依据 openai/codex 官方源码核对:
{"type":"tool_search","execution":...,"description":...,"parameters":...}tool_search_call(arguments为对象,execution: "client")ToolSearchHandler只接受ToolSearchpayload,function_call形态会被 "unsupported payload" 拒绝,因此回程必须还原成专属 item 类型;item id 官方前缀为tsc_Changes
去程(Responses → Chat)
responses_tools_to_chat_tools新增tool_search分支:按 function 工具透传(名字保持tool_search,description/parameters 原样保留)build_codex_tool_context将 tool_search 登记为ToolSearch类代理工具回程(Chat → Responses,非流式 + 流式)
tool_search_callitem(execution: "client",arguments 为对象),item id 使用官方tsc_前缀response.function_call_arguments.*历史回放(Responses input → Chat messages)
append_responses_item新增tool_search_call/tool_search_output分支,分别映射为 assistant tool_call 与role:tool消息,保持调用配对完整;孤儿 output 降级为 user 消息附带修复:保留 mcp_servers
preserve_live_app_settings重写 config.toml 时通过preserve_missing_table_keys保留 live 配置里的mcp_servers条目(只补缺不覆盖,模板/通用配置已有条目优先),避免每次 apply 后 MCP server 逐个消失Testing
tsc_前缀与execution: client断言)、历史回放配对、孤儿 output、畸形 arguments 兜底、id 回退与空值丢弃、越权调用钉板、mcp_servers 保留正/负例cargo test -p codex-plus-core全量 1217 passed / 0 failedfunction_call转发(钉住默认行为)