pub struct Tools { /* private fields */ }Expand description
Tool registry for managing and calling tools by name.
§Example
use aither::llm::tool::Tools;
let mut tools = Tools::new();
// tools.register(Calculator);
let definitions = tools.definitions();
// let result = tools
// .call("calculator", r#"{"operation": "add", "a": 5, "b": 3}"#, ToolContext::new())
// .await;Implementations§
Source§impl Tools
impl Tools
Sourcepub fn get<T>(&self) -> Option<&T>where
T: Tool + 'static,
pub fn get<T>(&self) -> Option<&T>where
T: Tool + 'static,
Retrieves a tool by type.
Returns None if the tool is not found.
Sourcepub fn get_mut<T>(&mut self) -> Option<&mut T>where
T: Tool + 'static,
pub fn get_mut<T>(&mut self) -> Option<&mut T>where
T: Tool + 'static,
Retrieves a mutable reference to a tool by type.
Returns None if the tool is not found.
Sourcepub fn definitions(&self) -> Vec<ToolDefinition>
pub fn definitions(&self) -> Vec<ToolDefinition>
Returns definitions of all registered tools.
Sourcepub fn register<T: Tool + 'static>(
&mut self,
tool: T,
) -> Result<(), RegisterError>
pub fn register<T: Tool + 'static>( &mut self, tool: T, ) -> Result<(), RegisterError>
Registers a new tool.
The tool must implement Tool and be 'static.
§Errors
Returns RegisterError::DuplicateName if a tool of that name is
already registered, or RegisterError::EmptyDescription if the tool
has no description — a model cannot use a tool it cannot read about, so
this is rejected rather than silently passed on.
Sourcepub fn register_dyn<F>(
&mut self,
definition: ToolDefinition,
handler: F,
) -> Result<(), RegisterError>
pub fn register_dyn<F>( &mut self, definition: ToolDefinition, handler: F, ) -> Result<(), RegisterError>
Registers a dynamic tool with a pre-made definition and handler.
This is useful for type-erased tools (e.g., child terminal tools for subagents)
where the concrete type isn’t known at compile time. The handler
receives the JSON arguments and the call’s ToolContext.
§Errors
Same conditions as Self::register.
Sourcepub fn unregister(&mut self, name: &str)
pub fn unregister(&mut self, name: &str)
Removes a tool from the registry.
Sourcepub async fn call(
&self,
name: &str,
args: &str,
cx: ToolContext,
) -> Result<ToolResult>
pub async fn call( &self, name: &str, args: &str, cx: ToolContext, ) -> Result<ToolResult>
Calls a tool by name with JSON arguments, running it with cx as its
ToolContext.
§Errors
Returns an error if the tool is not found, arguments cannot be parsed, or tool execution fails.