pub struct ToolRegistry { /* private fields */ }Implementations§
Source§impl ToolRegistry
impl ToolRegistry
Sourcepub fn register<T: RustTool + 'static>(&mut self, tool: T) -> &mut Self
pub fn register<T: RustTool + 'static>(&mut self, tool: T) -> &mut Self
Register a RustTool. Returns &mut Self for chaining.
The tool’s ToolDefinition (including JSON schema) is computed once
here and cached for the lifetime of the registration.
If a tool with the same name was already registered, it is replaced.
§Panics
Panics if the tool’s JSON schema cannot be serialized. This indicates a
bug in the tool’s Params type (e.g. a broken JsonSchema impl).
Sourcepub fn with_tool<T: RustTool + 'static>(self, tool: T) -> Self
pub fn with_tool<T: RustTool + 'static>(self, tool: T) -> Self
Register a RustTool, consuming and returning Self for owned chaining.
This is the owned counterpart of register, enabling
patterns like:
use llm_tool::{RustTool, ToolContext, ToolError, ToolOutput, ToolRegistry};
use schemars::JsonSchema;
use serde::Deserialize;
#[derive(Deserialize, JsonSchema)]
struct NoParams {}
struct ToolA;
impl RustTool for ToolA {
type Params = NoParams;
const NAME: &'static str = "tool_a";
const DESCRIPTION: &'static str = "Tool A";
async fn call(&self, _: NoParams, _: &ToolContext) -> Result<ToolOutput, ToolError> {
Ok("a".into())
}
}
struct ToolB;
impl RustTool for ToolB {
type Params = NoParams;
const NAME: &'static str = "tool_b";
const DESCRIPTION: &'static str = "Tool B";
async fn call(&self, _: NoParams, _: &ToolContext) -> Result<ToolOutput, ToolError> {
Ok("b".into())
}
}
let registry = ToolRegistry::new().with_tool(ToolA).with_tool(ToolB);
assert_eq!(registry.definitions().len(), 2);Sourcepub fn definitions(&self) -> Vec<ToolDefinition>
pub fn definitions(&self) -> Vec<ToolDefinition>
Collect ToolDefinitions for all registered tools.
Returns clones of the cached definitions computed at registration time.
Sourcepub async fn dispatch(
&self,
name: &str,
args: Value,
ctx: &ToolContext,
) -> Result<ToolOutput, ToolError>
pub async fn dispatch( &self, name: &str, args: Value, ctx: &ToolContext, ) -> Result<ToolOutput, ToolError>
Dispatch a tool call by name with raw JSON arguments and a context.
§Errors
Returns Err if the tool name is unknown or the handler returns an error.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&'static str, ToolDefinition)> + '_
pub fn iter(&self) -> impl Iterator<Item = (&'static str, ToolDefinition)> + '_
Iterate over (name, definition) pairs for every registered tool.
Returns clones of the cached definitions computed at registration time.
Trait Implementations§
Source§impl Debug for ToolRegistry
impl Debug for ToolRegistry
Source§impl Default for ToolRegistry
impl Default for ToolRegistry
Source§impl<'a> IntoIterator for &'a ToolRegistry
Iterate over (name, definition) pairs for every registered tool.
impl<'a> IntoIterator for &'a ToolRegistry
Iterate over (name, definition) pairs for every registered tool.
Yields (&'static str, ToolDefinition) for each tool in the registry.