go further

This commit is contained in:
jif-oai
2026-05-11 19:11:15 +01:00
parent a124ddb854
commit 4e244b254b
17 changed files with 535 additions and 121 deletions
@@ -3,7 +3,6 @@ use codex_protocol::models::FunctionCallOutputPayload;
use codex_protocol::models::ResponseInputItem;
use codex_tool_api::ToolBundle as ExtensionToolBundle;
use codex_tool_api::ToolError as ExtensionToolError;
use codex_tools::ResponsesApiTool;
use codex_tools::ToolName;
use codex_tools::ToolSpec;
use serde_json::Value;
@@ -73,7 +72,7 @@ impl ToolHandler for BundledToolHandler {
type Output = BundledToolOutput;
fn tool_name(&self) -> ToolName {
ToolName::plain(self.bundle.tool_name())
self.bundle.tool_name().clone()
}
fn spec(&self) -> Option<ToolSpec> {
@@ -138,19 +137,6 @@ impl ToolHandler for BundledToolHandler {
}
}
pub(crate) fn extension_tool_spec(
spec: &codex_tool_api::FunctionToolSpec,
) -> Result<ToolSpec, serde_json::Error> {
Ok(ToolSpec::Function(ResponsesApiTool {
name: spec.name.clone(),
description: spec.description.clone(),
strict: spec.strict,
defer_loading: None,
parameters: codex_tools::parse_tool_input_schema(&spec.parameters)?,
output_schema: None,
}))
}
fn map_extension_tool_error(error: ExtensionToolError) -> FunctionCallError {
match error {
ExtensionToolError::RespondToModel(message) => FunctionCallError::RespondToModel(message),
@@ -175,7 +161,6 @@ mod tests {
use super::BundledToolHandler;
use super::BundledToolOutput;
use super::extension_tool_spec;
use crate::tools::context::ToolCallSource;
use crate::tools::context::ToolInvocation;
use crate::tools::context::ToolPayload;
@@ -184,6 +169,7 @@ mod tests {
use crate::tools::registry::PreToolUsePayload;
use crate::tools::registry::ToolHandler;
use crate::turn_diff_tracker::TurnDiffTracker;
use codex_tools::ToolSpec;
struct StubExtensionExecutor;
@@ -196,22 +182,27 @@ mod tests {
#[tokio::test]
async fn 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,
}),
},
codex_tool_api::ToolName::plain("extension_echo"),
"Echoes arguments.".to_string(),
codex_tool_api::JsonSchema::object(
std::collections::BTreeMap::from([(
"message".to_string(),
codex_tool_api::JsonSchema::string(/*description*/ None),
)]),
Some(vec!["message".to_string()]),
Some(false.into()),
),
Arc::new(StubExtensionExecutor),
);
let spec = extension_tool_spec(bundle.spec()).expect("extension spec should convert");
)
.strict();
let spec = ToolSpec::Function(codex_tools::ResponsesApiTool {
name: bundle.tool_name().name.clone(),
description: bundle.description().to_string(),
strict: bundle.is_strict(),
defer_loading: bundle.defer_loading().then_some(true),
parameters: bundle.input_schema().clone(),
output_schema: bundle.output_schema().cloned(),
});
let handler = BundledToolHandler::new(bundle, spec);
let (session, turn) = crate::session::tests::make_session_and_context().await;
let invocation = ToolInvocation {
+63 -14
View File
@@ -17,7 +17,6 @@ use crate::tools::context::ToolOutput;
use crate::tools::context::ToolPayload;
use crate::tools::flat_tool_name;
use crate::tools::handlers::extension_tools::BundledToolHandler;
use crate::tools::handlers::extension_tools::extension_tool_spec;
use crate::tools::hook_names::HookToolName;
use crate::tools::tool_dispatch_trace::ToolDispatchTrace;
use crate::util::error_or_panic;
@@ -25,8 +24,12 @@ use codex_protocol::models::ResponseInputItem;
use codex_protocol::protocol::EventMsg;
use codex_tool_api::ToolBundle as ExtensionToolBundle;
use codex_tools::ConfiguredToolSpec;
use codex_tools::ResponsesApiNamespace;
use codex_tools::ResponsesApiNamespaceTool;
use codex_tools::ResponsesApiTool;
use codex_tools::ToolName;
use codex_tools::ToolSpec;
use codex_tools::default_namespace_description;
use codex_utils_readiness::Readiness;
use futures::future::BoxFuture;
use serde_json::Value;
@@ -512,12 +515,16 @@ impl ToolRegistryBuilder {
}
}
pub(crate) fn push_spec(&mut self, spec: ToolSpec, supports_parallel_tool_calls: bool) {
let spec = if self.code_mode_enabled {
fn prepare_spec(&self, spec: ToolSpec) -> ToolSpec {
if self.code_mode_enabled {
codex_tools::augment_tool_spec_for_code_mode(spec)
} else {
spec
};
}
}
pub(crate) fn push_spec(&mut self, spec: ToolSpec, supports_parallel_tool_calls: bool) {
let spec = self.prepare_spec(spec);
self.specs
.push(ConfiguredToolSpec::new(spec, supports_parallel_tool_calls));
}
@@ -542,22 +549,64 @@ impl ToolRegistryBuilder {
}
pub fn register_tool_bundle(&mut self, bundle: ExtensionToolBundle) {
let tool_name = ToolName::plain(bundle.tool_name());
let tool_name = bundle.tool_name().clone();
if self.handlers.contains_key(&tool_name) {
warn!("Skipping extension tool `{tool_name}`: handler already registered");
return;
}
let spec = match extension_tool_spec(bundle.spec()) {
Ok(spec) => spec,
Err(error) => {
error_or_panic(format!(
"failed to convert extension tool `{tool_name}` to a host spec: {error}"
));
return;
}
let function_tool = ResponsesApiTool {
name: bundle.tool_name().name.clone(),
description: bundle.description().to_string(),
strict: bundle.is_strict(),
defer_loading: bundle.defer_loading().then_some(true),
parameters: bundle.input_schema().clone(),
output_schema: bundle.output_schema().cloned(),
};
self.push_spec(spec.clone(), /*supports_parallel_tool_calls*/ false);
let spec = match bundle.namespace() {
Some(namespace) => ToolSpec::Namespace(ResponsesApiNamespace {
name: namespace.name().to_string(),
description: namespace
.description()
.map(str::to_string)
.unwrap_or_else(|| default_namespace_description(namespace.name())),
tools: vec![ResponsesApiNamespaceTool::Function(function_tool)],
}),
None => ToolSpec::Function(function_tool),
};
match self.prepare_spec(spec.clone()) {
ToolSpec::Namespace(mut namespace) => {
if let Some(existing_namespace) =
self.specs.iter_mut().find_map(|configured_tool| {
match &mut configured_tool.spec {
ToolSpec::Namespace(existing_namespace)
if existing_namespace.name == namespace.name =>
{
Some(existing_namespace)
}
ToolSpec::Function(_)
| ToolSpec::ToolSearch { .. }
| ToolSpec::LocalShell {}
| ToolSpec::ImageGeneration { .. }
| ToolSpec::WebSearch { .. }
| ToolSpec::Freeform(_)
| ToolSpec::Namespace(_) => None,
}
})
{
existing_namespace.tools.append(&mut namespace.tools);
} else {
self.specs.push(ConfiguredToolSpec::new(
ToolSpec::Namespace(namespace),
/*supports_parallel_tool_calls*/ false,
));
}
}
prepared_spec => self.specs.push(ConfiguredToolSpec::new(
prepared_spec,
/*supports_parallel_tool_calls*/ false,
)),
}
let handler: Arc<dyn AnyToolHandler> = Arc::new(BundledToolHandler::new(bundle, spec));
self.handlers.insert(tool_name, handler);
+113 -17
View File
@@ -8,7 +8,6 @@ use crate::turn_diff_tracker::TurnDiffTracker;
use codex_extension_api::ExtensionData;
use codex_extension_api::ExtensionRegistry;
use codex_extension_api::ExtensionRegistryBuilder;
use codex_extension_api::FunctionToolSpec;
use codex_extension_api::ToolBundle;
use codex_extension_api::ToolExecutor;
use codex_extension_api::ToolFuture;
@@ -16,6 +15,7 @@ use codex_protocol::dynamic_tools::DynamicToolSpec;
use codex_protocol::models::FunctionCallOutputBody;
use codex_protocol::models::ResponseInputItem;
use codex_protocol::models::ResponseItem;
use codex_tool_api::JsonSchema;
use codex_tool_api::ToolCall as ExtensionToolCall;
use codex_tools::ResponsesApiNamespaceTool;
use codex_tools::ToolName;
@@ -38,22 +38,40 @@ impl codex_extension_api::ToolContributor for ExtensionEchoContributor {
_session_store: &ExtensionData,
_thread_store: &ExtensionData,
) -> Vec<ToolBundle> {
vec![ToolBundle::new(
FunctionToolSpec {
name: "extension_echo".to_string(),
description: "Echoes arguments through an extension tool.".to_string(),
strict: true,
parameters: json!({
"type": "object",
"properties": {
"message": { "type": "string" },
},
"required": ["message"],
"additionalProperties": false,
}),
},
Arc::new(ExtensionEchoExecutor),
)]
vec![
ToolBundle::new(
ToolName::plain("extension_echo"),
"Echoes arguments through an extension tool.".to_string(),
JsonSchema::object(
std::collections::BTreeMap::from([(
"message".to_string(),
JsonSchema::string(/*description*/ None),
)]),
Some(vec!["message".to_string()]),
Some(false.into()),
),
Arc::new(ExtensionEchoExecutor),
)
.strict(),
ToolBundle::new(
ToolName::plain("echo"),
"Echoes arguments through a namespaced extension tool.".to_string(),
JsonSchema::object(
std::collections::BTreeMap::from([(
"message".to_string(),
JsonSchema::string(/*description*/ None),
)]),
Some(vec!["message".to_string()]),
Some(false.into()),
),
Arc::new(ExtensionEchoExecutor),
)
.in_namespace(
codex_extension_api::ToolNamespace::new("extension_tools")
.with_description("Extension-owned tools."),
)
.strict(),
]
}
}
@@ -345,6 +363,84 @@ async fn extension_tool_bundles_are_model_visible_and_dispatchable() -> anyhow::
Ok(())
}
#[tokio::test]
async fn namespaced_extension_tool_bundles_are_model_visible_and_dispatchable() -> anyhow::Result<()>
{
let (mut session, turn) = make_session_and_context().await;
session.services.extensions = extension_tool_test_registry();
let router = ToolRouter::from_config(
&turn.tools_config,
ToolRouterParams {
deferred_mcp_tools: None,
mcp_tools: None,
unavailable_called_tools: Vec::new(),
parallel_mcp_server_names: HashSet::new(),
discoverable_tools: None,
extension_tool_bundles: extension_tool_bundles(&session),
dynamic_tools: turn.dynamic_tools.as_slice(),
},
);
assert!(
router
.find_spec(&ToolName::namespaced("extension_tools", "echo"))
.is_some(),
"expected namespaced extension-provided tool spec to be registered"
);
assert_eq!(
namespace_function_names(&router.model_visible_specs(), "extension_tools"),
vec!["echo".to_string()]
);
let call = ToolRouter::build_tool_call(
&session,
ResponseItem::FunctionCall {
id: None,
name: "echo".to_string(),
namespace: Some("extension_tools".to_string()),
arguments: json!({ "message": "hello" }).to_string(),
call_id: "call-extension-namespace".to_string(),
},
)
.await?
.expect("function_call should produce a tool call");
let result = router
.dispatch_tool_call_with_code_mode_result(
Arc::new(session),
Arc::new(turn),
CancellationToken::new(),
Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())),
call,
ToolCallSource::Direct,
)
.await?;
let response = result.into_response();
match response {
ResponseInputItem::FunctionCallOutput { call_id, output } => {
assert_eq!(call_id, "call-extension-namespace");
let FunctionCallOutputBody::Text(text) = output.body else {
panic!("expected text function call output")
};
let value: serde_json::Value =
serde_json::from_str(&text).expect("extension tool output should be json");
assert_eq!(
value,
json!({
"arguments": { "message": "hello" },
"callId": "call-extension-namespace",
"ok": true,
})
);
}
other => panic!("expected function call output, got {other:?}"),
}
Ok(())
}
fn namespace_function_names(specs: &[ToolSpec], namespace_name: &str) -> Vec<String> {
specs
.iter()
+12 -1
View File
@@ -74,7 +74,7 @@ pub fn build_tool_registry_builder(
let exec_permission_approvals_enabled = config.exec_permission_approvals_enabled;
if config.code_mode_enabled {
let namespace_descriptions = params
let mut namespace_descriptions = params
.tool_namespaces
.into_iter()
.flatten()
@@ -88,6 +88,17 @@ pub fn build_tool_registry_builder(
)
})
.collect::<BTreeMap<_, _>>();
for bundle in params.extension_tool_bundles {
let Some(namespace) = bundle.namespace() else {
continue;
};
namespace_descriptions
.entry(namespace.name().to_string())
.or_insert_with(|| codex_code_mode::ToolNamespaceDescription {
name: namespace.name().to_string(),
description: namespace.description().unwrap_or_default().to_string(),
});
}
let nested_config = config.for_code_mode_nested_tools();
let nested_builder = build_tool_registry_builder(
&nested_config,
+109 -15
View File
@@ -43,7 +43,6 @@ use codex_protocol::openai_models::ModelInfo;
use codex_protocol::openai_models::WebSearchToolType;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::SubAgentSource;
use codex_tool_api::FunctionToolSpec;
use codex_tool_api::ToolBundle as ExtensionToolBundle;
use codex_tool_api::ToolExecutor;
use codex_tool_api::ToolFuture;
@@ -87,22 +86,30 @@ impl ToolExecutor for UnusedExtensionExecutor {
fn extension_tool_bundle(name: &str, description: &str) -> ExtensionToolBundle {
ExtensionToolBundle::new(
FunctionToolSpec {
name: name.to_string(),
description: description.to_string(),
strict: true,
parameters: serde_json::to_value(JsonSchema::object(
BTreeMap::from([(
"message".to_string(),
JsonSchema::string(/*description*/ None),
)]),
Some(vec!["message".to_string()]),
Some(false.into()),
))
.expect("extension schema should serialize"),
},
ToolName::plain(name),
description.to_string(),
JsonSchema::object(
BTreeMap::from([(
"message".to_string(),
JsonSchema::string(/*description*/ None),
)]),
Some(vec!["message".to_string()]),
Some(false.into()),
),
std::sync::Arc::new(UnusedExtensionExecutor),
)
.strict()
}
fn namespaced_extension_tool_bundle(
name: &str,
description: &str,
namespace: &str,
namespace_description: &str,
) -> ExtensionToolBundle {
extension_tool_bundle(name, description).in_namespace(
codex_tool_api::ToolNamespace::new(namespace).with_description(namespace_description),
)
}
#[test]
@@ -145,6 +152,53 @@ fn extension_tools_do_not_replace_builtin_tools() {
);
}
#[test]
fn namespaced_extension_tools_coalesce_into_namespace_specs() {
let model_info = model_info();
let available_models = Vec::new();
let tools_config = ToolsConfig::new(&ToolsConfigParams {
model_info: &model_info,
available_models: &available_models,
features: &Features::with_defaults(),
image_generation_tool_auth_allowed: true,
web_search_mode: Some(WebSearchMode::Cached),
session_source: SessionSource::Cli,
permission_profile: &PermissionProfile::Disabled,
windows_sandbox_level: WindowsSandboxLevel::Disabled,
});
let extension_tool_bundles = vec![
namespaced_extension_tool_bundle(
"echo",
"Echoes extension arguments.",
"extension_tools",
"Extension-owned tools.",
),
namespaced_extension_tool_bundle(
"status",
"Returns extension status.",
"extension_tools",
"Extension-owned tools.",
),
];
let (tools, _) = build_specs_with_discoverable_tools(
&tools_config,
/*mcp_tools*/ None,
/*deferred_mcp_tools*/ None,
/*discoverable_tools*/ None,
&extension_tool_bundles,
&[],
);
assert_eq!(
namespace_function_names(&tools, "extension_tools"),
vec!["echo".to_string(), "status".to_string()]
);
let ToolSpec::Namespace(namespace) = &find_tool(&tools, "extension_tools").spec else {
panic!("expected extension namespace tool");
};
assert_eq!(namespace.description, "Extension-owned tools.");
}
#[test]
fn test_full_toolset_specs_for_gpt5_codex_unified_exec_web_search() {
let model_info = model_info();
@@ -2302,6 +2356,46 @@ fn code_mode_only_exec_description_includes_extension_tool_details() {
assert!(description.contains("Echoes arguments through an extension tool."));
}
#[test]
fn code_mode_only_exec_description_includes_extension_namespace_guidance() {
let model_info = model_info();
let mut features = Features::with_defaults();
features.enable(Feature::CodeMode);
features.enable(Feature::CodeModeOnly);
let available_models = Vec::new();
let tools_config = ToolsConfig::new(&ToolsConfigParams {
model_info: &model_info,
available_models: &available_models,
features: &features,
image_generation_tool_auth_allowed: true,
web_search_mode: Some(WebSearchMode::Cached),
session_source: SessionSource::Cli,
permission_profile: &PermissionProfile::Disabled,
windows_sandbox_level: WindowsSandboxLevel::Disabled,
});
let extension_tool_bundles = vec![namespaced_extension_tool_bundle(
"echo",
"Echoes arguments through a namespaced extension tool.",
"extension_tools",
"Extension-owned tools.",
)];
let (tools, _) = build_specs_with_discoverable_tools(
&tools_config,
/*mcp_tools*/ None,
/*deferred_mcp_tools*/ None,
/*discoverable_tools*/ None,
&extension_tool_bundles,
&[],
);
let ToolSpec::Freeform(FreeformTool { description, .. }) = &find_tool(&tools, "exec").spec
else {
panic!("expected freeform tool");
};
assert!(description.contains("## extension_tools\nExtension-owned tools."));
}
#[test]
fn code_mode_exec_description_omits_nested_tool_details_when_not_code_mode_only() {
let model_info = model_info();