mirror of
https://github.com/openai/codex.git
synced 2026-10-31 19:28:49 +00:00
codex-tools: extract dynamic tool adapters
This commit is contained in:
Generated
+1
@@ -2632,6 +2632,7 @@ dependencies = [
|
||||
name = "codex-tools"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"codex-protocol",
|
||||
"pretty_assertions",
|
||||
"rmcp",
|
||||
"serde",
|
||||
|
||||
@@ -46,8 +46,8 @@ use codex_protocol::openai_models::WebSearchToolType;
|
||||
use codex_protocol::protocol::SandboxPolicy;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::SubAgentSource;
|
||||
use codex_tools::parse_dynamic_tool;
|
||||
use codex_tools::parse_mcp_tool;
|
||||
pub use codex_tools::parse_tool_input_schema;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
@@ -2397,14 +2397,14 @@ pub(crate) fn mcp_tool_to_deferred_openai_tool(
|
||||
fn dynamic_tool_to_openai_tool(
|
||||
tool: &DynamicToolSpec,
|
||||
) -> Result<ResponsesApiTool, serde_json::Error> {
|
||||
let input_schema = parse_tool_input_schema(&tool.input_schema)?;
|
||||
let parsed_tool = parse_dynamic_tool(tool)?;
|
||||
|
||||
Ok(ResponsesApiTool {
|
||||
name: tool.name.clone(),
|
||||
description: tool.description.clone(),
|
||||
description: parsed_tool.description,
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: input_schema,
|
||||
defer_loading: parsed_tool.defer_loading.then_some(true),
|
||||
parameters: parsed_tool.input_schema,
|
||||
output_schema: None,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use crate::tools::ToolRouter;
|
||||
use crate::tools::registry::ConfiguredToolSpec;
|
||||
use crate::tools::router::ToolRouterParams;
|
||||
use codex_app_server_protocol::AppInfo;
|
||||
use codex_protocol::dynamic_tools::DynamicToolSpec;
|
||||
use codex_protocol::openai_models::InputModality;
|
||||
use codex_protocol::openai_models::ModelInfo;
|
||||
use codex_protocol::openai_models::ModelsResponse;
|
||||
@@ -126,6 +127,27 @@ fn deferred_responses_api_tool_serializes_with_defer_loading() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dynamic_tool_preserves_defer_loading() {
|
||||
let tool = DynamicToolSpec {
|
||||
name: "lookup_order".to_string(),
|
||||
description: "Look up an order".to_string(),
|
||||
input_schema: serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"order_id": {"type": "string"}
|
||||
},
|
||||
"required": ["order_id"],
|
||||
"additionalProperties": false,
|
||||
}),
|
||||
defer_loading: true,
|
||||
};
|
||||
|
||||
let openai_tool = dynamic_tool_to_openai_tool(&tool).expect("convert dynamic tool");
|
||||
|
||||
assert_eq!(openai_tool.defer_loading, Some(true));
|
||||
}
|
||||
|
||||
fn tool_name(tool: &ToolSpec) -> &str {
|
||||
match tool {
|
||||
ToolSpec::Function(ResponsesApiTool { name, .. }) => name,
|
||||
|
||||
@@ -8,6 +8,7 @@ version.workspace = true
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
codex-protocol = { workspace = true }
|
||||
rmcp = { workspace = true, default-features = false, features = [
|
||||
"base64",
|
||||
"macros",
|
||||
|
||||
@@ -10,6 +10,8 @@ schema primitives that no longer need to live in `core/src/tools/spec.rs`:
|
||||
- `JsonSchema`
|
||||
- `AdditionalProperties`
|
||||
- `parse_tool_input_schema()`
|
||||
- `ParsedDynamicTool`
|
||||
- `parse_dynamic_tool()`
|
||||
- `ParsedMcpTool`
|
||||
- `parse_mcp_tool()`
|
||||
- `mcp_call_tool_result_output_schema()`
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
use crate::JsonSchema;
|
||||
use crate::parse_tool_input_schema;
|
||||
use codex_protocol::dynamic_tools::DynamicToolSpec;
|
||||
|
||||
/// Parsed dynamic tool metadata and schemas that can be adapted into a
|
||||
/// higher-level tool spec by downstream crates.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct ParsedDynamicTool {
|
||||
pub description: String,
|
||||
pub input_schema: JsonSchema,
|
||||
pub defer_loading: bool,
|
||||
}
|
||||
|
||||
pub fn parse_dynamic_tool(tool: &DynamicToolSpec) -> Result<ParsedDynamicTool, serde_json::Error> {
|
||||
Ok(ParsedDynamicTool {
|
||||
description: tool.description.clone(),
|
||||
input_schema: parse_tool_input_schema(&tool.input_schema)?,
|
||||
defer_loading: tool.defer_loading,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "dynamic_tool_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,66 @@
|
||||
use super::ParsedDynamicTool;
|
||||
use super::parse_dynamic_tool;
|
||||
use crate::JsonSchema;
|
||||
use codex_protocol::dynamic_tools::DynamicToolSpec;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[test]
|
||||
fn parse_dynamic_tool_sanitizes_input_schema() {
|
||||
let tool = DynamicToolSpec {
|
||||
name: "lookup_ticket".to_string(),
|
||||
description: "Fetch a ticket".to_string(),
|
||||
input_schema: serde_json::json!({
|
||||
"properties": {
|
||||
"id": {
|
||||
"description": "Ticket identifier"
|
||||
}
|
||||
}
|
||||
}),
|
||||
defer_loading: false,
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
parse_dynamic_tool(&tool).expect("parse dynamic tool"),
|
||||
ParsedDynamicTool {
|
||||
description: "Fetch a ticket".to_string(),
|
||||
input_schema: JsonSchema::Object {
|
||||
properties: BTreeMap::from([(
|
||||
"id".to_string(),
|
||||
JsonSchema::String {
|
||||
description: Some("Ticket identifier".to_string()),
|
||||
},
|
||||
)]),
|
||||
required: None,
|
||||
additional_properties: None,
|
||||
},
|
||||
defer_loading: false,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_dynamic_tool_preserves_defer_loading() {
|
||||
let tool = DynamicToolSpec {
|
||||
name: "lookup_ticket".to_string(),
|
||||
description: "Fetch a ticket".to_string(),
|
||||
input_schema: serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {}
|
||||
}),
|
||||
defer_loading: true,
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
parse_dynamic_tool(&tool).expect("parse dynamic tool"),
|
||||
ParsedDynamicTool {
|
||||
description: "Fetch a ticket".to_string(),
|
||||
input_schema: JsonSchema::Object {
|
||||
properties: BTreeMap::new(),
|
||||
required: None,
|
||||
additional_properties: None,
|
||||
},
|
||||
defer_loading: true,
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
//! Shared tool-schema parsing primitives that can live outside `codex-core`.
|
||||
|
||||
mod dynamic_tool;
|
||||
mod json_schema;
|
||||
mod mcp_tool;
|
||||
|
||||
pub use dynamic_tool::ParsedDynamicTool;
|
||||
pub use dynamic_tool::parse_dynamic_tool;
|
||||
pub use json_schema::AdditionalProperties;
|
||||
pub use json_schema::JsonSchema;
|
||||
pub use json_schema::parse_tool_input_schema;
|
||||
|
||||
Reference in New Issue
Block a user