mirror of
https://github.com/openai/codex.git
synced 2026-08-23 13:09:46 +00:00
Support custom tools in namespaces (#36857)
## What changed - Allow namespace tool specs to contain custom freeform tools alongside function tools. - Include namespaced custom tools in deferred tool search and expose them to code mode with names such as `editor__apply_patch`. - Route custom payloads to matching extension tools while preserving function-only payload validation. ## Testing - Add serialization, tool search, code-mode definition, and end-to-end dispatch coverage for namespaced custom tools. GitOrigin-RevId: be64d35f6ae54685c5a9fcf45a732320742ea7e5
This commit is contained in:
@@ -42,6 +42,20 @@ pub fn augment_tool_spec_for_code_mode(spec: ToolSpec) -> ToolSpec {
|
||||
tool.description =
|
||||
codex_code_mode::augment_tool_definition(definition).description;
|
||||
}
|
||||
ResponsesApiNamespaceTool::Custom(tool) => {
|
||||
let tool_name =
|
||||
ToolName::namespaced(namespace.name.clone(), tool.name.clone());
|
||||
let definition = CodeModeToolDefinition {
|
||||
name: code_mode_name_for_tool_name(&tool_name),
|
||||
tool_name,
|
||||
description: tool.description.clone(),
|
||||
kind: CodeModeToolKind::Freeform,
|
||||
input_schema: None,
|
||||
output_schema: None,
|
||||
};
|
||||
tool.description =
|
||||
codex_code_mode::augment_tool_definition(definition).description;
|
||||
}
|
||||
}
|
||||
}
|
||||
ToolSpec::Namespace(namespace)
|
||||
@@ -146,6 +160,17 @@ fn code_mode_tool_definitions_for_spec(spec: &ToolSpec) -> Vec<CodeModeToolDefin
|
||||
output_schema: tool.output_schema.clone(),
|
||||
}
|
||||
}
|
||||
ResponsesApiNamespaceTool::Custom(tool) => {
|
||||
let tool_name = ToolName::namespaced(namespace.name.clone(), tool.name.clone());
|
||||
CodeModeToolDefinition {
|
||||
name: code_mode_name_for_tool_name(&tool_name),
|
||||
tool_name,
|
||||
description: tool.description.clone(),
|
||||
kind: CodeModeToolKind::Freeform,
|
||||
input_schema: None,
|
||||
output_schema: None,
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
ToolSpec::ToolSearch { .. } | ToolSpec::WebSearch { .. } => Vec::new(),
|
||||
|
||||
@@ -4,6 +4,8 @@ use crate::AdditionalProperties;
|
||||
use crate::FreeformTool;
|
||||
use crate::FreeformToolFormat;
|
||||
use crate::JsonSchema;
|
||||
use crate::ResponsesApiNamespace;
|
||||
use crate::ResponsesApiNamespaceTool;
|
||||
use crate::ResponsesApiTool;
|
||||
use crate::ToolName;
|
||||
use crate::ToolSpec;
|
||||
@@ -123,6 +125,42 @@ declare const tools: { apply_patch(input: string): Promise<unknown>; };
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_spec_to_code_mode_tool_definition_supports_namespaced_custom_tools() {
|
||||
let spec = ToolSpec::Namespace(ResponsesApiNamespace {
|
||||
name: "editor".to_string(),
|
||||
description: "Editing tools".to_string(),
|
||||
tools: vec![ResponsesApiNamespaceTool::Custom(FreeformTool {
|
||||
name: "apply_patch".to_string(),
|
||||
description: "Apply a patch".to_string(),
|
||||
defer_loading: None,
|
||||
format: FreeformToolFormat {
|
||||
r#type: "grammar".to_string(),
|
||||
syntax: "lark".to_string(),
|
||||
definition: "start: \"patch\"".to_string(),
|
||||
},
|
||||
})],
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
tool_spec_to_code_mode_tool_definition(&spec),
|
||||
Some(codex_code_mode::ToolDefinition {
|
||||
name: "editor__apply_patch".to_string(),
|
||||
tool_name: ToolName::namespaced("editor", "apply_patch"),
|
||||
description: r#"Apply a patch
|
||||
|
||||
exec tool declaration:
|
||||
```ts
|
||||
declare const tools: { editor__apply_patch(input: string): Promise<unknown>; };
|
||||
```"#
|
||||
.to_string(),
|
||||
kind: codex_code_mode::CodeModeToolKind::Freeform,
|
||||
input_schema: None,
|
||||
output_schema: None,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_spec_to_code_mode_tool_definition_skips_unsupported_variants() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -63,9 +63,12 @@ pub fn default_namespace_description(namespace_name: &str) -> String {
|
||||
|
||||
#[derive(Debug, Clone, Serialize, PartialEq)]
|
||||
#[serde(tag = "type")]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
pub enum ResponsesApiNamespaceTool {
|
||||
#[serde(rename = "function")]
|
||||
Function(ResponsesApiTool),
|
||||
#[serde(rename = "custom")]
|
||||
Custom(FreeformTool),
|
||||
}
|
||||
|
||||
pub fn dynamic_tool_to_responses_api_tool(
|
||||
|
||||
@@ -43,9 +43,15 @@ impl ToolSearchInfo {
|
||||
namespace.description = default_namespace_description(&namespace.name);
|
||||
}
|
||||
for tool in &mut namespace.tools {
|
||||
let ResponsesApiNamespaceTool::Function(tool) = tool;
|
||||
tool.defer_loading = Some(true);
|
||||
tool.output_schema = None;
|
||||
match tool {
|
||||
ResponsesApiNamespaceTool::Function(tool) => {
|
||||
tool.defer_loading = Some(true);
|
||||
tool.output_schema = None;
|
||||
}
|
||||
ResponsesApiNamespaceTool::Custom(tool) => {
|
||||
tool.defer_loading = Some(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
LoadableToolSpec::Namespace(namespace)
|
||||
}
|
||||
@@ -73,8 +79,16 @@ fn default_tool_search_text(spec: &ToolSpec) -> String {
|
||||
push_search_part(&mut parts, namespace.name.clone());
|
||||
push_search_part(&mut parts, namespace.description.clone());
|
||||
for tool in &namespace.tools {
|
||||
let ResponsesApiNamespaceTool::Function(tool) = tool;
|
||||
append_function_search_text(tool, &mut parts);
|
||||
match tool {
|
||||
ResponsesApiNamespaceTool::Function(tool) => {
|
||||
append_function_search_text(tool, &mut parts);
|
||||
}
|
||||
ResponsesApiNamespaceTool::Custom(tool) => {
|
||||
push_search_part(&mut parts, tool.name.clone());
|
||||
push_search_part(&mut parts, tool.description.clone());
|
||||
push_search_part(&mut parts, tool.format.syntax.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ToolSpec::ToolSearch { description, .. } => {
|
||||
|
||||
@@ -46,3 +46,63 @@ fn default_search_text_uses_model_visible_namespace_metadata_once() {
|
||||
"codex_app Manage Codex automations. automation_update automation update Create or update automations. Automation options. mode Update mode. schedule Schedule settings. timezone IANA timezone."
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mixed_namespaced_function_and_custom_tools_are_searchable() {
|
||||
let function_tool = ResponsesApiTool {
|
||||
name: "lookup_order".to_string(),
|
||||
description: "Look up an order".to_string(),
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::object(
|
||||
BTreeMap::new(),
|
||||
/*required*/ None,
|
||||
/*additional_properties*/ None,
|
||||
),
|
||||
output_schema: Some(serde_json::json!({"type": "object"})),
|
||||
};
|
||||
let custom_tool = crate::FreeformTool {
|
||||
name: "apply_patch".to_string(),
|
||||
description: "Apply a patch".to_string(),
|
||||
defer_loading: None,
|
||||
format: crate::FreeformToolFormat {
|
||||
r#type: "grammar".to_string(),
|
||||
syntax: "lark".to_string(),
|
||||
definition: "start: \"patch\"".to_string(),
|
||||
},
|
||||
};
|
||||
let spec = ToolSpec::Namespace(crate::ResponsesApiNamespace {
|
||||
name: "editor".to_string(),
|
||||
description: "Editing tools".to_string(),
|
||||
tools: vec![
|
||||
ResponsesApiNamespaceTool::Function(function_tool.clone()),
|
||||
ResponsesApiNamespaceTool::Custom(custom_tool.clone()),
|
||||
],
|
||||
});
|
||||
|
||||
let search_info = ToolSearchInfo::from_tool_spec(spec, /*source_info*/ None)
|
||||
.expect("mixed namespace should be searchable");
|
||||
|
||||
assert_eq!(
|
||||
search_info.entry.search_text,
|
||||
"editor Editing tools lookup_order lookup order Look up an order apply_patch Apply a patch lark"
|
||||
);
|
||||
assert_eq!(
|
||||
search_info.entry.output,
|
||||
LoadableToolSpec::Namespace(crate::ResponsesApiNamespace {
|
||||
name: "editor".to_string(),
|
||||
description: "Editing tools".to_string(),
|
||||
tools: vec![
|
||||
ResponsesApiNamespaceTool::Function(ResponsesApiTool {
|
||||
defer_loading: Some(true),
|
||||
output_schema: None,
|
||||
..function_tool
|
||||
}),
|
||||
ResponsesApiNamespaceTool::Custom(crate::FreeformTool {
|
||||
defer_loading: Some(true),
|
||||
..custom_tool
|
||||
}),
|
||||
],
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -174,21 +174,33 @@ fn namespace_tool_spec_serializes_expected_wire_shape() {
|
||||
serde_json::to_value(ToolSpec::Namespace(ResponsesApiNamespace {
|
||||
name: "mcp__demo__".to_string(),
|
||||
description: "Demo tools".to_string(),
|
||||
tools: vec![ResponsesApiNamespaceTool::Function(ResponsesApiTool {
|
||||
name: "lookup_order".to_string(),
|
||||
description: "Look up an order".to_string(),
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::object(
|
||||
BTreeMap::from([(
|
||||
"order_id".to_string(),
|
||||
JsonSchema::string(/*description*/ None),
|
||||
)]),
|
||||
/*required*/ None,
|
||||
/*additional_properties*/ None,
|
||||
),
|
||||
output_schema: None,
|
||||
})],
|
||||
tools: vec![
|
||||
ResponsesApiNamespaceTool::Function(ResponsesApiTool {
|
||||
name: "lookup_order".to_string(),
|
||||
description: "Look up an order".to_string(),
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::object(
|
||||
BTreeMap::from([(
|
||||
"order_id".to_string(),
|
||||
JsonSchema::string(/*description*/ None),
|
||||
)]),
|
||||
/*required*/ None,
|
||||
/*additional_properties*/ None,
|
||||
),
|
||||
output_schema: None,
|
||||
}),
|
||||
ResponsesApiNamespaceTool::Custom(FreeformTool {
|
||||
name: "apply_patch".to_string(),
|
||||
description: "Apply a patch".to_string(),
|
||||
defer_loading: None,
|
||||
format: FreeformToolFormat {
|
||||
r#type: "grammar".to_string(),
|
||||
syntax: "lark".to_string(),
|
||||
definition: "start: \"patch\"".to_string(),
|
||||
},
|
||||
}),
|
||||
],
|
||||
}))
|
||||
.expect("serialize namespace tool"),
|
||||
json!({
|
||||
@@ -208,6 +220,16 @@ fn namespace_tool_spec_serializes_expected_wire_shape() {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "custom",
|
||||
"name": "apply_patch",
|
||||
"description": "Apply a patch",
|
||||
"format": {
|
||||
"type": "grammar",
|
||||
"syntax": "lark",
|
||||
"definition": "start: \"patch\"",
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user