mirror of
https://github.com/openai/codex.git
synced 2026-11-24 23:22:05 +00:00
## Why This is the next narrow step toward moving concrete tool families out of core. After #22138 introduced `codex-tool-api`, we still needed a real end-to-end seam that lets an extension own an executable tool definition once and have core install it without the temporary `extension-api` wrapper or a dependency on `codex-tools`. `codex-tool-api` is the small extension-facing execution contract, while `codex-tools` still has a different job: host-side shared tool metadata and planning logic that is not “run this contributed tool”, like spec shaping, namespaces, discovery, code-mode augmentation, and MCP/dynamic-to-Responses API conversion ## What changed - Moved the shared leaf tool-spec and JSON Schema types into `codex-tool-api`, so the executable contract now lives with [`ToolBundle`](https://github.com/openai/codex/blob/c538758095337d4fe0a52a172363ccede4066bda/codex-rs/tool-api/src/bundle.rs#L19-L70). - Replaced the temporary extension-side tool wrapper with direct `ToolBundle` use in `codex-extension-api`. - Taught core to collect contributed bundles, include them in spec planning, register them through [`ToolRegistryBuilder::register_tool_bundle`](https://github.com/openai/codex/blob/c538758095337d4fe0a52a172363ccede4066bda/codex-rs/core/src/tools/registry.rs#L653-L667), and dispatch them through the existing router/runtime path. - Added focused coverage for contributed tools becoming model-visible and dispatchable, plus spec-planning coverage for contributed function and freeform tools. ## Verification - Added `extension_tool_bundles_are_model_visible_and_dispatchable` in `core/src/tools/router_tests.rs`. - Added spec-plan coverage in `core/src/tools/spec_plan_tests.rs` for contributed extension bundles. ## Related - Follow-up to #22138
150 lines
5.1 KiB
Rust
150 lines
5.1 KiB
Rust
use super::*;
|
|
use crate::tools::handlers::GetGoalHandler;
|
|
use crate::tools::handlers::goal_spec::GET_GOAL_TOOL_NAME;
|
|
use crate::tools::handlers::goal_spec::create_get_goal_tool;
|
|
use pretty_assertions::assert_eq;
|
|
use serde_json::json;
|
|
|
|
struct TestHandler {
|
|
tool_name: codex_tools::ToolName,
|
|
}
|
|
|
|
impl ToolHandler for TestHandler {
|
|
type Output = crate::tools::context::FunctionToolOutput;
|
|
|
|
fn tool_name(&self) -> codex_tools::ToolName {
|
|
self.tool_name.clone()
|
|
}
|
|
|
|
fn kind(&self) -> ToolKind {
|
|
ToolKind::Function
|
|
}
|
|
|
|
async fn handle(&self, _invocation: ToolInvocation) -> Result<Self::Output, FunctionCallError> {
|
|
Ok(crate::tools::context::FunctionToolOutput::from_text(
|
|
"ok".to_string(),
|
|
Some(true),
|
|
))
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn handler_looks_up_namespaced_aliases_explicitly() {
|
|
let namespace = "mcp__codex_apps__gmail";
|
|
let tool_name = "gmail_get_recent_emails";
|
|
let plain_name = codex_tools::ToolName::plain(tool_name);
|
|
let namespaced_name = codex_tools::ToolName::namespaced(namespace, tool_name);
|
|
let plain_handler = Arc::new(TestHandler {
|
|
tool_name: plain_name.clone(),
|
|
}) as Arc<dyn AnyToolHandler>;
|
|
let namespaced_handler = Arc::new(TestHandler {
|
|
tool_name: namespaced_name.clone(),
|
|
}) as Arc<dyn AnyToolHandler>;
|
|
let registry = ToolRegistry::new(HashMap::from([
|
|
(plain_name.clone(), Arc::clone(&plain_handler)),
|
|
(namespaced_name.clone(), Arc::clone(&namespaced_handler)),
|
|
]));
|
|
|
|
let plain = registry.handler(&plain_name);
|
|
let namespaced = registry.handler(&namespaced_name);
|
|
let missing_namespaced = registry.handler(&codex_tools::ToolName::namespaced(
|
|
"mcp__codex_apps__calendar",
|
|
tool_name,
|
|
));
|
|
|
|
assert_eq!(plain.is_some(), true);
|
|
assert_eq!(namespaced.is_some(), true);
|
|
assert_eq!(missing_namespaced.is_none(), true);
|
|
assert!(
|
|
plain
|
|
.as_ref()
|
|
.is_some_and(|handler| Arc::ptr_eq(handler, &plain_handler))
|
|
);
|
|
assert!(
|
|
namespaced
|
|
.as_ref()
|
|
.is_some_and(|handler| Arc::ptr_eq(handler, &namespaced_handler))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn register_handler_adds_handler_and_augments_specs_for_code_mode() {
|
|
let mut builder = ToolRegistryBuilder::new(/*code_mode_enabled*/ true);
|
|
builder.register_handler(Arc::new(GetGoalHandler));
|
|
|
|
let (specs, registry) = builder.build();
|
|
|
|
assert_eq!(specs.len(), 1);
|
|
assert_eq!(
|
|
specs[0].spec,
|
|
codex_tools::augment_tool_spec_for_code_mode(create_get_goal_tool())
|
|
);
|
|
assert!(registry.has_handler(&codex_tools::ToolName::plain(GET_GOAL_TOOL_NAME)));
|
|
}
|
|
|
|
struct StubExtensionExecutor;
|
|
|
|
impl codex_tool_api::ToolExecutor for StubExtensionExecutor {
|
|
fn execute<'a>(&'a self, _call: codex_tool_api::ToolCall) -> codex_tool_api::ToolFuture<'a> {
|
|
Box::pin(async { Ok(json!({ "ok": true })) })
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn bundled_tool_handler_exposes_generic_hook_payloads_and_is_conservatively_mutating() {
|
|
let bundle = codex_tool_api::ToolBundle::new(
|
|
codex_tool_api::FunctionToolSpec {
|
|
name: "extension_echo".to_string(),
|
|
description: "Echoes arguments.".to_string(),
|
|
strict: true,
|
|
parameters: json!({
|
|
"type": "object",
|
|
"properties": {
|
|
"message": { "type": "string" },
|
|
},
|
|
"required": ["message"],
|
|
"additionalProperties": false,
|
|
}),
|
|
},
|
|
Arc::new(StubExtensionExecutor),
|
|
);
|
|
let spec = extension_tool_spec(bundle.spec()).expect("extension spec should convert");
|
|
let handler = BundledToolHandler::new(bundle, spec);
|
|
let (session, turn) = crate::session::tests::make_session_and_context().await;
|
|
let invocation = ToolInvocation {
|
|
session: session.into(),
|
|
turn: turn.into(),
|
|
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
|
tracker: Arc::new(tokio::sync::Mutex::new(
|
|
crate::turn_diff_tracker::TurnDiffTracker::new(),
|
|
)),
|
|
call_id: "call-extension".to_string(),
|
|
tool_name: codex_tools::ToolName::plain("extension_echo"),
|
|
source: crate::tools::context::ToolCallSource::Direct,
|
|
payload: ToolPayload::Function {
|
|
arguments: json!({ "message": "hello" }).to_string(),
|
|
},
|
|
};
|
|
let output = BundledToolOutput {
|
|
value: json!({ "ok": true }),
|
|
};
|
|
|
|
assert!(ToolHandler::is_mutating(&handler, &invocation).await);
|
|
assert_eq!(
|
|
ToolHandler::pre_tool_use_payload(&handler, &invocation),
|
|
Some(PreToolUsePayload {
|
|
tool_name: HookToolName::new("extension_echo"),
|
|
tool_input: json!({ "message": "hello" }),
|
|
})
|
|
);
|
|
assert_eq!(
|
|
ToolHandler::post_tool_use_payload(&handler, &invocation, &output),
|
|
Some(PostToolUsePayload {
|
|
tool_name: HookToolName::new("extension_echo"),
|
|
tool_use_id: "call-extension".to_string(),
|
|
tool_input: json!({ "message": "hello" }),
|
|
tool_response: json!({ "ok": true }),
|
|
})
|
|
);
|
|
}
|