ToolExecutor

Struct ToolExecutor 

Source
pub struct ToolExecutor { /* private fields */ }
Expand description

Manages the execution of tools invoked by language models.

The ToolExecutor is responsible for:

  • Maintaining a registry of available tools
  • Executing tool calls invoked by the language model
  • Handling approval checks for sensitive operations
  • Managing both single and streaming tool execution
  • Converting tool results to structured output formats

It supports both parallel execution of multiple tool calls and sequential streaming execution with preliminary result callbacks.

Implementations§

Source§

impl ToolExecutor

Source

pub fn new(tools: Vec<Arc<dyn Tool>>) -> Self

Creates a new ToolExecutor with the provided set of tools.

§Arguments
  • tools - A vector of tool implementations to be managed by this executor. Tools are wrapped in Arc for thread-safe shared ownership.
Source

pub fn tool_definitions(&self) -> Vec<FunctionTool>

Retrieves the tool definitions in a format suitable for language model APIs.

This converts the internal tool representations into FunctionTool definitions that can be provided to language models. These definitions include the tool’s name, description, and input schema.

§Returns

A vector of FunctionTool definitions, one for each registered tool.

Source

pub async fn execute_tools( &self, tool_calls: Vec<ToolCallPart>, ) -> Vec<ToolResultPart>

Executes multiple tool calls in parallel and returns their results.

This method processes all provided tool calls concurrently, allowing for efficient execution of multiple independent tools. Each tool call is handled independently, with its own error handling and result conversion.

The execution includes:

  • Tool lookup by name
  • Input validation and parsing
  • Approval checks (tools may require user approval)
  • Actual tool execution
  • Result conversion to structured format
§Arguments
  • tool_calls - A vector of tool calls to execute, each specifying the tool name, call ID, and JSON-encoded input parameters.
§Returns

A vector of ToolResultPart structures, one for each input tool call. Results include either successful output or error information for each tool.

Source

pub async fn execute_tool_with_stream<F>( &self, tool_call: ToolCallPart, on_preliminary: F, ) -> ToolResultPart
where F: Fn(ToolResultPart) + Send,

Executes a single tool call with streaming support and preliminary result callbacks.

This method is specialized for tools that produce streaming output. It handles the execution of a single tool call and invokes a callback for each preliminary result as they arrive. This is useful for long-running operations that can produce incremental output.

The method:

  • Finds and validates the tool
  • Parses and validates input
  • Checks approval requirements
  • Executes the tool
  • For streaming results, invokes the callback for each intermediate value
  • Returns the final result after all streaming is complete
§Arguments
  • tool_call - The tool call to execute, specifying the tool name, call ID, and JSON-encoded input parameters.
  • on_preliminary - A callback function that is invoked for each preliminary result as they stream in. Useful for real-time processing or UI updates. The callback is not invoked for the final result.
§Returns

A ToolResultPart containing the final tool result, or an error if the tool execution or streaming fails. The final result does not have the preliminary flag set.

Source

pub fn tools(&self) -> &[Arc<dyn Tool>]

Returns a reference to the list of all available tools in this executor.

This provides access to the complete registry of tools that can be invoked by the language model. Useful for introspection and tool enumeration.

§Returns

A slice of all registered tools, allowing iteration over the available tools.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more