Skip to main content

Tool

Trait Tool 

Source
pub trait Tool: Send + Sync {
    // Required methods
    fn definition(&self) -> ToolDefinition;
    fn execute<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        ctx: &'life1 ToolCallContext,
        control: &'life2 CallControl,
        store: Option<&'life3 dyn ArtifactStore>,
    ) -> Pin<Box<dyn Future<Output = ToolResultPayload> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait;

    // Provided method
    fn hints(&self) -> ToolHints { ... }
}
Expand description

The tool port: one callable tool the model can invoke. Implementations live outside the kernel; the driver’s executor dispatches them. A tool returns only its recorded result. Handling unknown outcomes and retaining output belong to the execution consumer, not tool declarations.

§Requirements on execute

An executor may poll every call of one batch on a single task, with nothing spawned. An implementation therefore:

  • must not block the thread. Blocking or CPU-heavy work runs on a blocking thread (for example tokio::task::spawn_blocking) and the tool awaits it;
  • must watch its CallControl itself. A turn stop — cancellation or the turn deadline — drops the call future: the executor stops polling, a started call settles as ToolResultStatus::UnknownOutcome. Once the executor observes the stop it polls the call no further, so a value returned after that is not recorded. Returning promptly on cancellation releases the resources the future still owns; it does not make the result observed;
  • must return, cleanup included, by its effective call deadline to report ToolResultStatus::TimedOut or ToolResultStatus::Cancelled. The executor enforces a call deadline with timeout_at, which polls the call before its own timer and drops the future once the deadline has expired. A tool that races the same deadline and returns without awaiting anything further is therefore recorded. A tool that awaits cleanup after it stops has to stop early enough for that cleanup to finish before the deadline; otherwise the future is dropped mid-cleanup and the call settles as UnknownOutcome.

Dropping the future runs on the executor’s task, so Drop must not block and must be idempotent. A future that starts external work — a spawned task, a child process, a remote operation — cleans it up in Drop.

For a tool that meets these, a stop takes effect at its next yield point. A tool that holds the thread also holds its sibling calls and the executor’s own stop and timeout checks, so nothing is guaranteed for it: not cancellation, not the deadline, not a per-call timeout.

Calls of one batch are concurrent, not parallel, and their number is not bounded. A tool that needs a limit shares a semaphore across its calls.

Required Methods§

Source

fn definition(&self) -> ToolDefinition

The model-facing ToolDefinition for this tool.

Source

fn execute<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, ctx: &'life1 ToolCallContext, control: &'life2 CallControl, store: Option<&'life3 dyn ArtifactStore>, ) -> Pin<Box<dyn Future<Output = ToolResultPayload> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Runs one call under the call’s CallControl, returning the recorded result. store is the host’s ArtifactStore when one is configured: a tool that produces media or oversized output persists the bytes there and returns a reference; a tool that does not need storage ignores it.

Provided Methods§

Source

fn hints(&self) -> ToolHints

What this tool declares about its effects. An executor reads it when it binds the tool, next to Tool::definition, and shows it to tool processors. The default declares nothing.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§