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
impl ToolExecutor
Sourcepub fn new(tools: Vec<Arc<dyn Tool>>) -> Self
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 inArcfor thread-safe shared ownership.
Sourcepub fn tool_definitions(&self) -> Vec<FunctionTool>
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.
Sourcepub async fn execute_tools(
&self,
tool_calls: Vec<ToolCallPart>,
) -> Vec<ToolResultPart>
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.
Sourcepub async fn execute_tool_with_stream<F>(
&self,
tool_call: ToolCallPart,
on_preliminary: F,
) -> ToolResultPart
pub async fn execute_tool_with_stream<F>( &self, tool_call: ToolCallPart, on_preliminary: F, ) -> ToolResultPart
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.
Sourcepub fn tools(&self) -> &[Arc<dyn Tool>]
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.