Skip to content

fix(relayconvert): improve Responses→Chat tool compatibility - #5939

Draft
orangeboyChen wants to merge 4 commits into
QuantumNous:mainfrom
orangeboyChen:codex/fix-responses-mcp-tools
Draft

fix(relayconvert): improve Responses→Chat tool compatibility#5939
orangeboyChen wants to merge 4 commits into
QuantumNous:mainfrom
orangeboyChen:codex/fix-responses-mcp-tools

Conversation

@orangeboyChen

@orangeboyChen orangeboyChen commented Jul 6, 2026

Copy link
Copy Markdown

📝 变更描述 / Description

解决Response->Chat转换后mcp tool会丢失的问题。
在Response->Chat转换时,会转换mcp工具向上传递,并且添加tool_search代理。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • Closes # (如有)

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

The PR updates Responses-to-Chat conversion to flatten MCP and namespace tools into Chat function tools, proxy tool search, normalize inner parameters, and translate related tool choices. Unit tests cover the new conversion behavior.

Changes

Tool conversion flattening

Layer / File(s) Summary
Flatten tools and convert tool choices
service/relayconvert/responses_request_to_chat.go
Flattens mcp_server and namespace tools, emits a tool_search proxy function, defaults missing parameters to an object schema, and applies flattened names to tool choices.
Validate flattened tool behavior
service/relayconvert/responses_request_to_chat_test.go
Tests flattening, proxying, ordering, parameter defaults, invalid-name filtering, and MCP tool-choice naming.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

  • 5936: Covers the same MCP and namespace tool-flattening defect addressed by this change.
  • 5937: Covers the same conversion defect, including related tool-search and tool-choice handling.

Sequence Diagram(s)

sequenceDiagram
  participant ResponsesRequest
  participant responsesRequestToolsToChat
  participant ChatToolList
  ResponsesRequest->>responsesRequestToolsToChat: provide MCP, namespace, function, or tool_search tools
  responsesRequestToolsToChat->>ChatToolList: emit flattened function tools
  responsesRequestToolsToChat->>ChatToolList: emit tool_search proxy function
Loading

Poem

A rabbit found tools in a nested burrow,
Flattened each one without any sorrow.
Names joined with hops, schemas made sound,
Tool search became a proxy found.
Chat now sees the tools in line—
“Nibble hooray!” says Bunny fine.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR flattens mcp_server/namespace tools, but it also proxies tool_search as a function instead of skipping it entirely as requested. Remove the tool_search proxy and omit tool_search from converted output; keep only the mcp_server/namespace flattening and defer_loading stripping.
✅ Passed checks (4 passed)
Check name Status Explanation
Out of Scope Changes check ✅ Passed The extra tool_choice handling and parameter defaults are part of the same Responses-to-Chat conversion flow, with no clearly unrelated code changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately reflects the main change: improving Responses-to-Chat tool conversion compatibility.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@orangeboyChen

Copy link
Copy Markdown
Author

@codex review

…at conversion

Responses→Chat conversion (responsesRequestToolsToChat) only handled
type:"function" tools. Tools with type:"mcp_server" or type:"namespace"
were dumped into a Custom field that the upstream Chat Completions API
does not understand, causing the model to lose all tool definitions
inside those blocks.

Fix:
- Flatten mcp_server/namespace inner tools[] into individual
  type:"function" entries (name, description, parameters preserved)
- Skip tool_search entirely (no Chat Completions equivalent)
- defer_loading is implicitly stripped since it is not copied into the
  FunctionRequest output

Add tests covering mcp_server flattening, namespace flattening,
tool_search skipping, mixed tool types, and nil parameters fallback.

Closes QuantumNous#5938
@orangeboyChen
orangeboyChen force-pushed the codex/fix-responses-mcp-tools branch from 3c92426 to f562b77 Compare July 6, 2026 10:57

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
service/relayconvert/responses_request_to_chat.go (2)

361-374: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Unreachable []map[string]any branch.

raw originates from tool["tools"], itself decoded via common.Unmarshal(raw, &tools) into map[string]any. JSON-unmarshaling a nested array into an any-typed field always yields []interface{}, never []map[string]any, so the second switch case can never be hit in the real request path (only test code constructing native Go values without going through JSON could trigger it, and the tests here all go through mustRawMessage). Not harmful, but dead code.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@service/relayconvert/responses_request_to_chat.go` around lines 361 - 374,
The responsesFlattenInnerTools type switch has a dead branch for
[]map[string]any that is never reached in the real request path because
tool["tools"] comes from JSON unmarshaling into any and will be []any. Remove
the unreachable case from responsesFlattenInnerTools and keep only the []any
handling, using the existing function name to verify the cleanup.

364-369: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Malformed inner tool entries are silently dropped.

If an item in the inner tools array isn't a map[string]any (e.g., malformed client payload), it's silently skipped with no error or log, which can make missing-tool issues hard to diagnose downstream.

♻️ Optional: surface a warning for skipped entries
 	case []any:
 		for _, item := range v {
 			if m, ok := item.(map[string]any); ok {
 				inner = append(inner, m)
+			} else {
+				common.SysLog(fmt.Sprintf("responsesFlattenInnerTools: skipping malformed inner tool entry: %v", item))
 			}
 		}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@service/relayconvert/responses_request_to_chat.go` around lines 364 - 369,
The inner tools parsing in responses_request_to_chat.go currently drops non-map
entries from the []any case without any visibility. Update the tool-collection
logic in the relevant conversion function to surface a warning or error when an
item in the inner tools array is not a map[string]any, so malformed client
payloads are detectable; keep the existing valid-path behavior for maps and log
enough context to identify the skipped entry.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@service/relayconvert/responses_request_to_chat.go`:
- Around line 361-374: The responsesFlattenInnerTools type switch has a dead
branch for []map[string]any that is never reached in the real request path
because tool["tools"] comes from JSON unmarshaling into any and will be []any.
Remove the unreachable case from responsesFlattenInnerTools and keep only the
[]any handling, using the existing function name to verify the cleanup.
- Around line 364-369: The inner tools parsing in responses_request_to_chat.go
currently drops non-map entries from the []any case without any visibility.
Update the tool-collection logic in the relevant conversion function to surface
a warning or error when an item in the inner tools array is not a
map[string]any, so malformed client payloads are detectable; keep the existing
valid-path behavior for maps and log enough context to identify the skipped
entry.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0168a688-59a4-450d-9f72-8e22c3a4f184

📥 Commits

Reviewing files that changed from the base of the PR and between fc26b88 and 3c92426.

📒 Files selected for processing (2)
  • service/relayconvert/responses_request_to_chat.go
  • service/relayconvert/responses_request_to_chat_test.go

@orangeboyChen
orangeboyChen marked this pull request as ready for review July 6, 2026 11:09
@orangeboyChen

Copy link
Copy Markdown
Author

@codex review

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
service/relayconvert/responses_request_to_chat_test.go (2)

348-348: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Remove duplicate assertion.

Line 348 re-asserts got.Tools[0].Type == "function", which is already checked on line 339.

♻️ Proposed fix
 	assert.Equal(t, "string", query["type"])
-	assert.Equal(t, "function", got.Tools[0].Type)
 	assert.Equal(t, "lookup", got.Tools[1].Function.Name)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@service/relayconvert/responses_request_to_chat_test.go` at line 348, Remove
the duplicate got.Tools[0].Type assertion from the relevant test, keeping the
earlier assertion near line 339 as the sole validation of the tool type.

431-449: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider expanding tool-choice test coverage.

The implementation in responsesRequestToolChoiceToChat handles three choice types (mcp, mcp_server, namespace) and two namespace source fields (server_label, namespace). This test only covers type: "mcp" with server_label. Adding cases for mcp_server/namespace types and the namespace field path would guard against regressions in those branches.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@service/relayconvert/responses_request_to_chat_test.go` around lines 431 -
449, Expand
TestResponsesRequestToChatCompletionsRequestMcpToolChoiceUsesFlattenedName with
table-driven cases covering mcp_server and namespace tool-choice types,
including the namespace source field as well as server_label, and assert each
produces the expected flattened function name. Ensure all cases continue
validating successful conversion and the resulting Chat Completions tool choice.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@service/relayconvert/responses_request_to_chat_test.go`:
- Line 348: Remove the duplicate got.Tools[0].Type assertion from the relevant
test, keeping the earlier assertion near line 339 as the sole validation of the
tool type.
- Around line 431-449: Expand
TestResponsesRequestToChatCompletionsRequestMcpToolChoiceUsesFlattenedName with
table-driven cases covering mcp_server and namespace tool-choice types,
including the namespace source field as well as server_label, and assert each
produces the expected flattened function name. Ensure all cases continue
validating successful conversion and the resulting Chat Completions tool choice.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e4ca404c-9006-4f22-850f-ee4337179a39

📥 Commits

Reviewing files that changed from the base of the PR and between a5941a5 and b13e23e.

📒 Files selected for processing (2)
  • service/relayconvert/responses_request_to_chat.go
  • service/relayconvert/responses_request_to_chat_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • service/relayconvert/responses_request_to_chat.go

@orangeboyChen orangeboyChen changed the title fix(relayconvert): flatten mcp_server/namespace tools in Responses→Ch… fix(relayconvert): improve Responses→Chat tool compatibility Jul 10, 2026
@orangeboyChen
orangeboyChen marked this pull request as draft July 17, 2026 14:32
ishalumi added a commit to ishalumi/new-api that referenced this pull request Aug 20, 2026
…tool_search proxy + tool_choice namespace

QuantumNous#6593 剩余方向:
- OpenAI→Claude 请求: thinking 块 replay (非最新 assistant 轮)
- OpenAI→Responses 请求: reasoning_content → reasoning input item
- OpenAI→Gemini 请求: reasoning_content → thought part

QuantumNous#5939 补充:
- tool_search → function 代理 (Codex MCP 兼容)
- tool_choice: server_label/namespace → flattened name
- tool_choice: mcp/mcp_server/namespace → function choice
@Calcium-Ion
Calcium-Ion force-pushed the main branch 2 times, most recently from 51fdfc5 to 2b6f1df Compare August 30, 2026 15:03
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.

2 participants