mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
Share loadable tool spec conversion
This commit is contained in:
@@ -14,8 +14,8 @@ use codex_protocol::models::ResponseInputItem;
|
||||
use codex_protocol::models::SearchToolCallParams;
|
||||
use codex_protocol::models::ShellToolCallParams;
|
||||
use codex_protocol::models::function_call_output_content_items_to_text;
|
||||
use codex_tools::LoadableToolSpec;
|
||||
use codex_tools::ToolName;
|
||||
use codex_tools::ToolSearchOutputTool;
|
||||
use codex_utils_output_truncation::TruncationPolicy;
|
||||
use codex_utils_output_truncation::formatted_truncate_text;
|
||||
use codex_utils_string::take_bytes_at_char_boundary;
|
||||
@@ -183,7 +183,7 @@ impl McpToolOutput {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ToolSearchOutput {
|
||||
pub tools: Vec<ToolSearchOutputTool>,
|
||||
pub tools: Vec<LoadableToolSpec>,
|
||||
}
|
||||
|
||||
impl ToolOutput for ToolSearchOutput {
|
||||
|
||||
@@ -283,20 +283,18 @@ fn tool_search_payloads_roundtrip_as_tool_search_outputs() {
|
||||
},
|
||||
};
|
||||
let response = ToolSearchOutput {
|
||||
tools: vec![ToolSearchOutputTool::Function(
|
||||
codex_tools::ResponsesApiTool {
|
||||
name: "create_event".to_string(),
|
||||
description: String::new(),
|
||||
strict: false,
|
||||
defer_loading: Some(true),
|
||||
parameters: codex_tools::JsonSchema::object(
|
||||
/*properties*/ Default::default(),
|
||||
/*required*/ None,
|
||||
/*additional_properties*/ None,
|
||||
),
|
||||
output_schema: None,
|
||||
},
|
||||
)],
|
||||
tools: vec![LoadableToolSpec::Function(codex_tools::ResponsesApiTool {
|
||||
name: "create_event".to_string(),
|
||||
description: String::new(),
|
||||
strict: false,
|
||||
defer_loading: Some(true),
|
||||
parameters: codex_tools::JsonSchema::object(
|
||||
/*properties*/ Default::default(),
|
||||
/*required*/ None,
|
||||
/*additional_properties*/ None,
|
||||
),
|
||||
output_schema: None,
|
||||
})],
|
||||
}
|
||||
.to_response_item("search-1", &payload);
|
||||
|
||||
|
||||
@@ -9,9 +9,9 @@ use bm25::Document;
|
||||
use bm25::Language;
|
||||
use bm25::SearchEngine;
|
||||
use bm25::SearchEngineBuilder;
|
||||
use codex_tools::LoadableToolSpec;
|
||||
use codex_tools::TOOL_SEARCH_DEFAULT_LIMIT;
|
||||
use codex_tools::TOOL_SEARCH_TOOL_NAME;
|
||||
use codex_tools::ToolSearchOutputTool;
|
||||
use std::collections::HashMap;
|
||||
|
||||
const COMPUTER_USE_MCP_SERVER_NAME: &str = "computer-use";
|
||||
@@ -93,7 +93,7 @@ impl ToolSearchHandler {
|
||||
query: &str,
|
||||
limit: usize,
|
||||
use_default_limit: bool,
|
||||
) -> Result<Vec<ToolSearchOutputTool>, FunctionCallError> {
|
||||
) -> Result<Vec<LoadableToolSpec>, FunctionCallError> {
|
||||
let results = self.search_result_entries(query, limit, use_default_limit);
|
||||
self.search_output_tools(results)
|
||||
}
|
||||
@@ -135,28 +135,24 @@ impl ToolSearchHandler {
|
||||
fn search_output_tools<'a>(
|
||||
&self,
|
||||
results: impl IntoIterator<Item = &'a ToolSearchEntry>,
|
||||
) -> Result<Vec<ToolSearchOutputTool>, FunctionCallError> {
|
||||
) -> Result<Vec<LoadableToolSpec>, FunctionCallError> {
|
||||
let mut tools = Vec::new();
|
||||
// Preserve search order: group namespace children, emit standalone tools directly.
|
||||
for entry in results {
|
||||
match &entry.output {
|
||||
ToolSearchOutputTool::Function(tool) => {
|
||||
tools.push(ToolSearchOutputTool::Function(tool.clone()));
|
||||
LoadableToolSpec::Function(tool) => {
|
||||
tools.push(LoadableToolSpec::Function(tool.clone()));
|
||||
}
|
||||
ToolSearchOutputTool::Namespace(namespace) => {
|
||||
LoadableToolSpec::Namespace(namespace) => {
|
||||
if let Some(output) = tools.iter_mut().find_map(|tool| match tool {
|
||||
ToolSearchOutputTool::Namespace(output)
|
||||
if output.name == namespace.name =>
|
||||
{
|
||||
LoadableToolSpec::Namespace(output) if output.name == namespace.name => {
|
||||
Some(output)
|
||||
}
|
||||
ToolSearchOutputTool::Namespace(_) | ToolSearchOutputTool::Function(_) => {
|
||||
None
|
||||
}
|
||||
LoadableToolSpec::Namespace(_) | LoadableToolSpec::Function(_) => None,
|
||||
}) {
|
||||
output.tools.extend(namespace.tools.clone());
|
||||
} else {
|
||||
tools.push(ToolSearchOutputTool::Namespace(namespace.clone()));
|
||||
tools.push(LoadableToolSpec::Namespace(namespace.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -248,7 +244,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
tools,
|
||||
vec![
|
||||
ToolSearchOutputTool::Namespace(ResponsesApiNamespace {
|
||||
LoadableToolSpec::Namespace(ResponsesApiNamespace {
|
||||
name: "mcp__calendar__".to_string(),
|
||||
description: "Tools in the mcp__calendar__ namespace.".to_string(),
|
||||
tools: vec![
|
||||
@@ -278,7 +274,7 @@ mod tests {
|
||||
}),
|
||||
],
|
||||
}),
|
||||
ToolSearchOutputTool::Namespace(ResponsesApiNamespace {
|
||||
LoadableToolSpec::Namespace(ResponsesApiNamespace {
|
||||
name: "codex_app".to_string(),
|
||||
description: "Tools provided by the current Codex thread.".to_string(),
|
||||
tools: vec![ResponsesApiNamespaceTool::Function(ResponsesApiTool {
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
use codex_mcp::ToolInfo;
|
||||
use codex_protocol::dynamic_tools::DynamicToolSpec;
|
||||
use codex_tools::ResponsesApiNamespace;
|
||||
use codex_tools::ResponsesApiNamespaceTool;
|
||||
use codex_tools::ToolSearchOutputTool;
|
||||
use codex_tools::LoadableToolSpec;
|
||||
use codex_tools::ToolSearchResultSource;
|
||||
use codex_tools::dynamic_tool_to_responses_api_tool;
|
||||
use codex_tools::tool_search_result_source_to_output_tool;
|
||||
use codex_tools::dynamic_tool_to_loadable_tool_spec;
|
||||
use codex_tools::tool_search_result_source_to_loadable_tool_spec;
|
||||
use std::collections::HashMap;
|
||||
|
||||
const DYNAMIC_TOOL_NAMESPACE_DESCRIPTION: &str = "Tools provided by the current Codex thread.";
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ToolSearchEntry {
|
||||
pub(crate) search_text: String,
|
||||
pub(crate) output: ToolSearchOutputTool,
|
||||
pub(crate) output: LoadableToolSpec,
|
||||
pub(crate) limit_bucket: Option<String>,
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ pub(crate) fn build_tool_search_entries(
|
||||
fn mcp_tool_search_entry(info: &ToolInfo) -> Result<ToolSearchEntry, serde_json::Error> {
|
||||
Ok(ToolSearchEntry {
|
||||
search_text: build_mcp_search_text(info),
|
||||
output: tool_search_result_source_to_output_tool(ToolSearchResultSource {
|
||||
output: tool_search_result_source_to_loadable_tool_spec(ToolSearchResultSource {
|
||||
server_name: info.server_name.as_str(),
|
||||
tool_namespace: info.callable_namespace.as_str(),
|
||||
tool_name: info.callable_name.as_str(),
|
||||
@@ -70,15 +70,9 @@ fn mcp_tool_search_entry(info: &ToolInfo) -> Result<ToolSearchEntry, serde_json:
|
||||
}
|
||||
|
||||
fn dynamic_tool_search_entry(tool: &DynamicToolSpec) -> Result<ToolSearchEntry, serde_json::Error> {
|
||||
let output_tool = dynamic_tool_to_responses_api_tool(tool)?;
|
||||
let output = match tool.namespace.as_ref() {
|
||||
Some(namespace) => ToolSearchOutputTool::Namespace(ResponsesApiNamespace {
|
||||
name: namespace.clone(),
|
||||
description: "Tools provided by the current Codex thread.".to_string(),
|
||||
tools: vec![ResponsesApiNamespaceTool::Function(output_tool)],
|
||||
}),
|
||||
None => ToolSearchOutputTool::Function(output_tool),
|
||||
};
|
||||
let output = dynamic_tool_to_loadable_tool_spec(tool, |_| {
|
||||
DYNAMIC_TOOL_NAMESPACE_DESCRIPTION.to_string()
|
||||
})?;
|
||||
|
||||
Ok(ToolSearchEntry {
|
||||
search_text: build_dynamic_search_text(tool),
|
||||
|
||||
@@ -83,11 +83,12 @@ pub use request_user_input_tool::request_user_input_tool_description;
|
||||
pub use request_user_input_tool::request_user_input_unavailable_message;
|
||||
pub use responses_api::FreeformTool;
|
||||
pub use responses_api::FreeformToolFormat;
|
||||
pub use responses_api::LoadableToolSpec;
|
||||
pub use responses_api::ResponsesApiNamespace;
|
||||
pub use responses_api::ResponsesApiNamespaceTool;
|
||||
pub use responses_api::ResponsesApiTool;
|
||||
pub use responses_api::ToolSearchOutputTool;
|
||||
pub(crate) use responses_api::default_namespace_description;
|
||||
pub use responses_api::dynamic_tool_to_loadable_tool_spec;
|
||||
pub use responses_api::dynamic_tool_to_responses_api_tool;
|
||||
pub use responses_api::mcp_tool_to_deferred_responses_api_tool;
|
||||
pub use responses_api::mcp_tool_to_responses_api_tool;
|
||||
@@ -115,7 +116,7 @@ pub use tool_discovery::collect_tool_suggest_entries;
|
||||
pub use tool_discovery::create_tool_search_tool;
|
||||
pub use tool_discovery::create_tool_suggest_tool;
|
||||
pub use tool_discovery::filter_tool_suggest_discoverable_tools_for_client;
|
||||
pub use tool_discovery::tool_search_result_source_to_output_tool;
|
||||
pub use tool_discovery::tool_search_result_source_to_loadable_tool_spec;
|
||||
pub use tool_registry_plan::build_tool_registry_plan;
|
||||
pub use tool_registry_plan_types::ToolHandlerKind;
|
||||
pub use tool_registry_plan_types::ToolHandlerSpec;
|
||||
|
||||
@@ -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,24 @@ pub fn dynamic_tool_to_responses_api_tool(
|
||||
)?))
|
||||
}
|
||||
|
||||
pub fn dynamic_tool_to_loadable_tool_spec<F>(
|
||||
tool: &DynamicToolSpec,
|
||||
namespace_description: F,
|
||||
) -> Result<LoadableToolSpec, serde_json::Error>
|
||||
where
|
||||
F: FnOnce(&str) -> String,
|
||||
{
|
||||
let output_tool = dynamic_tool_to_responses_api_tool(tool)?;
|
||||
Ok(match tool.namespace.as_ref() {
|
||||
Some(namespace) => LoadableToolSpec::Namespace(ResponsesApiNamespace {
|
||||
name: namespace.clone(),
|
||||
description: namespace_description(namespace),
|
||||
tools: vec![ResponsesApiNamespaceTool::Function(output_tool)],
|
||||
}),
|
||||
None => LoadableToolSpec::Function(output_tool),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn mcp_tool_to_responses_api_tool(
|
||||
tool_name: &ToolName,
|
||||
tool: &rmcp::model::Tool,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::LoadableToolSpec;
|
||||
use super::ResponsesApiNamespace;
|
||||
use super::ResponsesApiNamespaceTool;
|
||||
use super::ResponsesApiTool;
|
||||
use super::ToolSearchOutputTool;
|
||||
use super::dynamic_tool_to_responses_api_tool;
|
||||
use super::mcp_tool_to_deferred_responses_api_tool;
|
||||
use super::tool_definition_to_responses_api_tool;
|
||||
@@ -131,8 +131,8 @@ fn mcp_tool_to_deferred_responses_api_tool_sets_defer_loading() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_search_output_namespace_serializes_with_deferred_child_tools() {
|
||||
let namespace = ToolSearchOutputTool::Namespace(ResponsesApiNamespace {
|
||||
fn loadable_tool_spec_namespace_serializes_with_deferred_child_tools() {
|
||||
let namespace = LoadableToolSpec::Namespace(ResponsesApiNamespace {
|
||||
name: "mcp__codex_apps__calendar".to_string(),
|
||||
description: "Plan events".to_string(),
|
||||
tools: vec![ResponsesApiNamespaceTool::Function(ResponsesApiTool {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use crate::JsonSchema;
|
||||
use crate::LoadableToolSpec;
|
||||
use crate::ResponsesApiNamespace;
|
||||
use crate::ResponsesApiNamespaceTool;
|
||||
use crate::ResponsesApiTool;
|
||||
use crate::ToolName;
|
||||
use crate::ToolSearchOutputTool;
|
||||
use crate::ToolSpec;
|
||||
use crate::default_namespace_description;
|
||||
use crate::mcp_tool_to_deferred_responses_api_tool;
|
||||
@@ -203,10 +203,10 @@ pub fn create_tool_search_tool(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tool_search_result_source_to_output_tool(
|
||||
pub fn tool_search_result_source_to_loadable_tool_spec(
|
||||
source: ToolSearchResultSource<'_>,
|
||||
) -> Result<ToolSearchOutputTool, serde_json::Error> {
|
||||
Ok(ToolSearchOutputTool::Namespace(ResponsesApiNamespace {
|
||||
) -> Result<LoadableToolSpec, serde_json::Error> {
|
||||
Ok(LoadableToolSpec::Namespace(ResponsesApiNamespace {
|
||||
name: source.tool_namespace.to_string(),
|
||||
description: tool_search_result_source_namespace_description(source),
|
||||
tools: vec![tool_search_result_source_to_namespace_tool(source)?],
|
||||
|
||||
@@ -58,7 +58,7 @@ use crate::create_wait_tool;
|
||||
use crate::create_web_search_tool;
|
||||
use crate::create_write_stdin_tool;
|
||||
use crate::default_namespace_description;
|
||||
use crate::dynamic_tool_to_responses_api_tool;
|
||||
use crate::dynamic_tool_to_loadable_tool_spec;
|
||||
use crate::mcp_tool_to_responses_api_tool;
|
||||
use crate::request_permissions_tool_description;
|
||||
use crate::request_user_input_tool_description;
|
||||
@@ -557,20 +557,11 @@ pub fn build_tool_registry_plan(
|
||||
}
|
||||
|
||||
for tool in params.dynamic_tools {
|
||||
match dynamic_tool_to_responses_api_tool(tool) {
|
||||
Ok(converted_tool) => {
|
||||
match dynamic_tool_to_loadable_tool_spec(tool, default_namespace_description) {
|
||||
Ok(loadable_tool) => {
|
||||
let handler_name = ToolName::new(tool.namespace.clone(), tool.name.clone());
|
||||
let spec = if let Some(namespace) = tool.namespace.as_ref() {
|
||||
ToolSpec::Namespace(ResponsesApiNamespace {
|
||||
name: namespace.clone(),
|
||||
description: default_namespace_description(namespace),
|
||||
tools: vec![ResponsesApiNamespaceTool::Function(converted_tool)],
|
||||
})
|
||||
} else {
|
||||
ToolSpec::Function(converted_tool)
|
||||
};
|
||||
plan.push_spec(
|
||||
spec,
|
||||
loadable_tool.into(),
|
||||
/*supports_parallel_tool_calls*/ false,
|
||||
config.code_mode_enabled,
|
||||
);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::FreeformTool;
|
||||
use crate::JsonSchema;
|
||||
use crate::LoadableToolSpec;
|
||||
use crate::ResponsesApiNamespace;
|
||||
use crate::ResponsesApiTool;
|
||||
use codex_protocol::config_types::WebSearchConfig;
|
||||
@@ -70,6 +71,15 @@ impl ToolSpec {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LoadableToolSpec> for ToolSpec {
|
||||
fn from(value: LoadableToolSpec) -> Self {
|
||||
match value {
|
||||
LoadableToolSpec::Function(tool) => ToolSpec::Function(tool),
|
||||
LoadableToolSpec::Namespace(namespace) => ToolSpec::Namespace(namespace),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_local_shell_tool() -> ToolSpec {
|
||||
ToolSpec::LocalShell {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user