pub struct ToolRegistry { /* private fields */ }Expand description
A registry of named tools available for dynamic dispatch.
Holds type-erased tool implementations and cached ToolDefinition
schemas for fast lookup and execution.
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). Use
try_register for the non-panicking variant.
Sourcepub fn try_register<T: RustTool + 'static>(
&mut self,
tool: T,
) -> Result<&mut Self, ToolError>
pub fn try_register<T: RustTool + 'static>( &mut self, tool: T, ) -> Result<&mut Self, ToolError>
Register a RustTool, returning an error instead of panicking if the
tool’s JSON schema cannot be built.
This is the fallible counterpart to register; prefer
it when tool types are supplied dynamically and a broken JsonSchema
impl should not abort the process. If a tool with the same name was
already registered, it is replaced.
§Errors
Returns ToolError if the tool’s Params type fails to produce a
JSON schema.
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 ToolError::not_found if no tool named name is registered
(carrying error_kind = "not_registered" metadata), or the tool’s own
error if argument deserialization fails or the handler returns an error.
Sourcepub async fn dispatch_str(
&self,
name: &str,
args_json: &str,
ctx: &ToolContext,
) -> Result<ToolOutput, ToolError>
pub async fn dispatch_str( &self, name: &str, args_json: &str, ctx: &ToolContext, ) -> Result<ToolOutput, ToolError>
Dispatch a tool call by name with a raw JSON string argument.
§Errors
Returns ToolError::not_found if no tool named name is registered,
or the tool’s own error if JSON parsing or the handler fails.
Sourcepub fn remove(&mut self, name: &str) -> bool
pub fn remove(&mut self, name: &str) -> bool
Remove a tool by name, returning true if it was present.
Sourcepub fn definition(&self, name: &str) -> Option<&ToolDefinition>
pub fn definition(&self, name: &str) -> Option<&ToolDefinition>
Borrow the cached ToolDefinition for a registered tool by name.
Returns None if no tool named name is registered. Unlike
definitions, this clones nothing.
Sourcepub fn iter(&self) -> ToolDefinitions<'_> ⓘ
pub fn iter(&self) -> ToolDefinitions<'_> ⓘ
Iterate over (name, definition) pairs for every registered tool.
Yields 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.