Skip to main content

Executor

Trait Executor 

Source
pub trait Executor:
    Send
    + Sync
    + 'static {
    type Output: Send + 'static;

    // Required method
    fn execute(
        &self,
        invocation: &Invocation,
        call: &ToolCall,
        cancellation: Token,
    ) -> impl Future<Output = Result<Self::Output, ExecutorError>> + Send;
}
Expand description

A concrete executor with one registration-specific output type.

An executor returns whatever type is natural for it — a String, a struct, a byte buffer — and Registration::client pairs that type with the one mapper that turns it into a closed ToolResult. Nothing else in the crate sees Executor::Output, which is why it can be anything.

An ordinary failure is not Err. Err(ExecutorError) says the adapter itself broke; a tool that ran and did not like what it found returns Ok(...) and lets the mapper produce crate::value::tool_call::ToolResult::BusinessError, which the model reads and can act on.

use std::future::{Future, ready};
use std::sync::Arc;

use reef_core::cancel;
use reef_core::opaque;
use reef_core::runtime::tool;
use reef_core::value::tool_call;

#[derive(Debug)]
struct Reverse;

impl tool::Executor for Reverse {
    type Output = String;

    fn execute(
        &self,
        _invocation: &tool::Invocation,
        call: &tool_call::ToolCall,
        _cancellation: cancel::Token,
    ) -> impl Future<Output = Result<Self::Output, tool::ExecutorError>> + Send {
        // Arguments are uninterpreted bytes: reef never parsed them, and the
        // schema this tool published is what promised their shape.
        let arguments = String::from_utf8_lossy(call.arguments().as_bytes()).into_owned();
        ready(Ok(arguments.chars().rev().collect()))
    }
}

let registration = tool::Registration::client(
    tool::Descriptor::new(
        tool::Name::local("text.reverse".to_owned()).unwrap(),
        "Reverses its arguments".to_owned(),
        opaque::Payload::copy_from_slice(br#"{"type":"object"}"#),
    ),
    Arc::new(Reverse),
    // The mapper is the only thing that sees `Output`.
    Arc::new(|output: String| Ok(tool_call::ToolResult::success_text(output))),
);
assert_eq!(registration.lane(), tool::Lane::Client);

§Cancellation

The future returned by execute must be cancellation-safe; that method’s own documentation states exactly what reef requires of it.

Required Associated Types§

Source

type Output: Send + 'static

Open adapter output consumed only by the paired registration mapper.

Required Methods§

Source

fn execute( &self, invocation: &Invocation, call: &ToolCall, cancellation: Token, ) -> impl Future<Output = Result<Self::Output, ExecutorError>> + Send

Executes one effective call under its closed invocation context; ordinary errors become business results only when execution completion wins its race with cancellation.

The returned future must be cancellation-safe: its caller may drop it at any await point after first cancelling the supplied token. Dropping the future must synchronously release resources or initiate owned cleanup, and it must not detach any work. If background work is unavoidable, the future itself must own that work and its Drop path must abort it or otherwise synchronously bring it under cleanup that remains owned.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§