mirror of
https://github.com/openai/codex.git
synced 2026-11-13 14:46:14 +00:00
[tool search] support namespaced deferred dynamic tools (#18413)
Deferred dynamic tools need to round-trip a namespace so a tool returned by `tool_search` can be called through the same registry key that core uses for dispatch. This change adds namespace support for dynamic tool specs/calls, persists it through app-server thread state, and routes dynamic tool calls by full `ToolName` while still sending the app the leaf tool name. Deferred dynamic tools must provide a namespace; non-deferred dynamic tools may remain top-level. It also introduces `LoadableToolSpec` as the shared function-or-namespace Responses shape used by both `tool_search` output and dynamic tool registration, so dynamic tools use the same wrapping logic in both paths. Validation: - `cargo test -p codex-tools` - `cargo test -p codex-core tool_search` --------- Co-authored-by: Sayan Sisodiya <sayan@openai.com>
This commit is contained in:
1 parent
1dcea729d3
commit
dc1a8f2190
60 files changed
+676
-147
No files matched your search
@@ -40,7 +40,7 @@ pub struct ResponsesApiTool {
|
||||
#[derive(Debug, Clone, Serialize, PartialEq)]
|
||||
#[serde(tag = "type")]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
pub enum ToolSearchOutputTool {
|
||||
pub enum LoadableToolSpec {
|
||||
#[allow(dead_code)]
|
||||
#[serde(rename = "function")]
|
||||
Function(ResponsesApiTool),
|
||||
@@ -74,6 +74,51 @@ pub fn dynamic_tool_to_responses_api_tool(
|
||||
)?))
|
||||
}
|
||||
|
||||
pub fn dynamic_tool_to_loadable_tool_spec(
|
||||
tool: &DynamicToolSpec,
|
||||
) -> Result<LoadableToolSpec, serde_json::Error> {
|
||||
let output_tool = dynamic_tool_to_responses_api_tool(tool)?;
|
||||
Ok(match tool.namespace.as_ref() {
|
||||
Some(namespace) => LoadableToolSpec::Namespace(ResponsesApiNamespace {
|
||||
name: namespace.clone(),
|
||||
// the user doesn't provide a description for dynamic tools, so we use the default
|
||||
description: default_namespace_description(namespace),
|
||||
tools: vec![ResponsesApiNamespaceTool::Function(output_tool)],
|
||||
}),
|
||||
None => LoadableToolSpec::Function(output_tool),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn coalesce_loadable_tool_specs(
|
||||
specs: impl IntoIterator<Item = LoadableToolSpec>,
|
||||
) -> Vec<LoadableToolSpec> {
|
||||
let mut coalesced_specs = Vec::new();
|
||||
for spec in specs {
|
||||
match spec {
|
||||
LoadableToolSpec::Function(tool) => {
|
||||
coalesced_specs.push(LoadableToolSpec::Function(tool));
|
||||
}
|
||||
LoadableToolSpec::Namespace(mut namespace) => {
|
||||
if let Some(existing_namespace) =
|
||||
coalesced_specs.iter_mut().find_map(|spec| match spec {
|
||||
LoadableToolSpec::Namespace(existing_namespace)
|
||||
if existing_namespace.name == namespace.name =>
|
||||
{
|
||||
Some(existing_namespace)
|
||||
}
|
||||
LoadableToolSpec::Function(_) | LoadableToolSpec::Namespace(_) => None,
|
||||
})
|
||||
{
|
||||
existing_namespace.tools.append(&mut namespace.tools);
|
||||
} else {
|
||||
coalesced_specs.push(LoadableToolSpec::Namespace(namespace));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
coalesced_specs
|
||||
}
|
||||
|
||||
pub fn mcp_tool_to_responses_api_tool(
|
||||
tool_name: &ToolName,
|
||||
tool: &rmcp::model::Tool,
|
||||
|
||||
Reference in new issue
Block a user