feat(openai): implement nested namespace schemas and streaming translation for namespaced tools#75
Open
fmosca wants to merge 3 commits into
Open
feat(openai): implement nested namespace schemas and streaming translation for namespaced tools#75fmosca wants to merge 3 commits into
fmosca wants to merge 3 commits into
Conversation
0ee0652 to
c33bf63
Compare
…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.
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.
背景 / 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.go的OutputItemFromBlock转换中,新增ToolNestedNamespace解析路径。decodeNestedNamespaceArguments将模型的嵌套{"action": "...", "params": {...}}还原为 Codex 期望的 Namespace 字段结构。2. 协议转换与流式缓冲 / Protocol Adapter & Stream Buffering
internal/protocol/openai/adapter.go中,修改convertToolWithNamespace的namespace分支,停止扁平化,改为调用convertNamespaceToNestedTool生成带有action(Enum) 和params(anyOf整合子工具) 的标准嵌套 Schema。action字段需随着流逐步吐出。我们在streamLoop中引入了nestedBuffers缓冲机制:ToolNestedNamespace时暂缓向 Codex 发出output_item.added。CoreToolCallArgsDone阶段解析出最终的action与params,重建出标准 Namespaced Call 并一次性推给 Codex 解释器。3. 测试覆盖 / Testing
internal/extension/codextool/customtool_test.go中补充TestOutputItemFromBlockForToolNestedNamespace单元测试,确保提取和还原逻辑无回归。go test ./...全量测试通过。影响范围 / Impact
codex_tool_proxy扩展时原有协议行为不受影响。