Files
codex/codex-rs/tools/src/tool_executor.rs
T
sayan-oai 728b8243a9 tools: simplify default tool search text (#27526)
## Why

Default tool search text currently derives identity from both `ToolName`
and `ToolSpec`. For function and namespace specs, this indexes the same
names more than once and also adds a flattened `{namespace}{name}` token
that is not model-visible.

## What changed

- Derive default search text entirely from `ToolSpec` while preserving
names, descriptions, namespace metadata, and recursive schema metadata.
- Keep the default search-text builder private and remove the unused
`ToolName` argument.
- Add coverage for the exact search text generated for a namespaced tool
with nested schema metadata.

## Example

For the `codex_app` namespace and `automation_update` tool (schema terms
omitted):

- Before: `codex_appautomation_update automation update codex_app
codex_app Manage Codex automations. automation_update automation update
...`
- After: `codex_app Manage Codex automations. automation_update
automation update ...`

## Testing

- `just test -p codex-tools`
2026-06-11 03:37:25 +00:00

70 lines
2.2 KiB
Rust

use crate::FunctionCallError;
use crate::ToolName;
use crate::ToolOutput;
use crate::ToolSearchInfo;
use crate::ToolSpec;
use std::future::Future;
use std::pin::Pin;
/// The boxed future returned by [`ToolExecutor::handle`].
pub type ToolExecutorFuture<'a> =
Pin<Box<dyn Future<Output = Result<Box<dyn ToolOutput>, FunctionCallError>> + Send + 'a>>;
/// Controls where a tool is exposed to the model.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ToolExposure {
/// Include this tool in the initial model-visible tool list.
///
/// When code mode is enabled, this tool is also available as a nested
/// code-mode tool.
Direct,
/// Register this tool for later discovery, but omit it from the initial
/// model-visible tool list. Deferred tools must provide search metadata via
/// [`ToolExecutor::search_info`]. The default implementation derives
/// metadata from function and namespace specs.
Deferred,
/// Include this tool in the initial model-visible tool list only.
///
/// In code-mode-only sessions, this keeps the tool callable as a normal
/// model tool while excluding it from the nested code-mode tool surface.
DirectModelOnly,
/// Keep this tool registered for dispatch without exposing it to the model.
Hidden,
}
impl ToolExposure {
pub fn is_direct(self) -> bool {
matches!(self, Self::Direct | Self::DirectModelOnly)
}
}
/// Shared runtime contract for model-visible tools.
///
/// Implementations keep the model-visible spec tied to the executable runtime.
/// Host crates can layer routing, hooks, telemetry, or other orchestration on
/// top without reopening the spec/runtime split.
pub trait ToolExecutor<Invocation>: Send + Sync {
/// The concrete tool name handled by this runtime instance.
fn tool_name(&self) -> ToolName;
fn spec(&self) -> ToolSpec;
fn exposure(&self) -> ToolExposure {
ToolExposure::Direct
}
fn search_info(&self) -> Option<ToolSearchInfo> {
let spec = self.spec();
ToolSearchInfo::from_tool_spec(spec, /*source_info*/ None)
}
fn supports_parallel_tool_calls(&self) -> bool {
false
}
fn handle(&self, invocation: Invocation) -> ToolExecutorFuture<'_>;
}