Skip to content

feat(openai): implement nested namespace schemas and streaming translation for namespaced tools#75

Open
fmosca wants to merge 3 commits into
ZhiYi-R:mainfrom
fmosca:main
Open

feat(openai): implement nested namespace schemas and streaming translation for namespaced tools#75
fmosca wants to merge 3 commits into
ZhiYi-R:mainfrom
fmosca:main

Conversation

@fmosca

@fmosca fmosca commented Jun 3, 2026

Copy link
Copy Markdown

背景 / Background

目前 Moon Bridge 处理 Codex 的 Namespaced 工具(如 mcp__filesystem)时,采用的是扁平化展开(Flattening)模式。这会导致大量子工具直接暴露在顶层,极大增加了发送给上游大模型(如 DeepSeek V4, Kimi K2.6)的 Input Tokens 消耗。

此外,由于部分现代推理模型在多工具调用时对 Polymorphic Schemas (多态架构) 表现更为优秀,使用顶层 anyOf 嵌套模式(Fully Typed Object Nesting)可以将一个 Namespace 下的工具收拢为一个顶层工具调用,从而提升 Token 效率并规避工具名冲突。

This PR introduces native support for nested namespace schemas and streaming translation to group namespaced tools under a unified schema, optimizing context window usage and improving tool calling compliance for reasoning models.

修改内容 / Changes

1. 扩展层支持 / Extension Layer

  • internal/extension/codextool/tool_context.go 中新增 ToolNestedNamespace 类型。
  • internal/extension/codextool/customtool.goOutputItemFromBlock 转换中,新增 ToolNestedNamespace 解析路径。
  • 编写 decodeNestedNamespaceArguments 将模型的嵌套 {"action": "...", "params": {...}} 还原为 Codex 期望的 Namespace 字段结构。

2. 协议转换与流式缓冲 / Protocol Adapter & Stream Buffering

  • Schema 整合: 在 internal/protocol/openai/adapter.go 中,修改 convertToolWithNamespacenamespace 分支,停止扁平化,改为调用 convertNamespaceToNestedTool 生成带有 action (Enum) 和 params (anyOf 整合子工具) 的标准嵌套 Schema。
  • 流式缓冲 (Stream Buffering): 由于在流式(Streaming)路径中,模型发出 Namespaced 工具调用时,最先发出的 Arguments Delta 仅包含顶层 Namespace 结构,而子工具的 action 字段需随着流逐步吐出。我们在 streamLoop 中引入了 nestedBuffers 缓冲机制:
    • 当匹配到 ToolNestedNamespace 时暂缓向 Codex 发出 output_item.added
    • 累积该 Block 的所有流式 Deltas。
    • CoreToolCallArgsDone 阶段解析出最终的 actionparams,重建出标准 Namespaced Call 并一次性推给 Codex 解释器。

3. 测试覆盖 / Testing

  • internal/extension/codextool/customtool_test.go 中补充 TestOutputItemFromBlockForToolNestedNamespace 单元测试,确保提取和还原逻辑无回归。
  • go test ./... 全量测试通过。

影响范围 / Impact

  • 提升了 Namespaced 工具的 Token 效率,优化了推理模型(DeepSeek V4 / Kimi)在执行复杂文件 CRUD 时的稳定性。
  • 保持了向下兼容,未开启 codex_tool_proxy 扩展时原有协议行为不受影响。

@fmosca fmosca force-pushed the main branch 3 times, most recently from 0ee0652 to c33bf63 Compare June 3, 2026 21:54
fmosca added 3 commits June 4, 2026 21:14
…coding

Introduces the ToolNestedNamespace ToolKind to identify tools grouped under
a shared namespace. Implements decodeNestedNamespaceArguments to reverse-map
clean nested parameters back to Codex.
…ation

Converts namespaced tool structures into a single top-level schema using
unified action and anyOf-based parameters fields. Implements a stream
buffering layer to reconstruct streamed action and parameter deltas into
clean namespaced tool calls for the downstream interpreter.
…tion

Adds TestOutputItemFromBlockForToolNestedNamespace to verify the extraction
of clean action, namespace, and parameters when reconstructing output items.
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