Skip to main content

ToolRegistry

Struct ToolRegistry 

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

Registry of tools available to an LLM session.

Why: Centralizes tool lookup and schema emission so the PM loop and multi-turn sub-agent loop stay short and test-friendly. What: HashMap<String, Arc<dyn ToolExecutor>> with register, dispatch, dispatch_gated, dispatch_for_user, schemas, and filter_tools_for_user. Test: See unit tests below.

Implementations§

Source§

impl ToolRegistry

Source

pub fn new() -> Self

Construct an empty registry.

Why: Callers build up the registry incrementally via register. What: Returns a ToolRegistry with no tools registered. Test: new_registry_is_empty.

Source

pub fn register(&mut self, tool: Arc<dyn ToolExecutor>)

Register a tool by its name().

Why: Using the tool’s own name prevents mismatches between registered key and schema-declared function name. What: Inserts into the map. A debug_assert! panics in debug builds on duplicate names so collisions surface during development. Test: register_then_dispatch_succeeds, register_panics_on_duplicate_in_debug.

Source

pub fn contains(&self, name: &str) -> bool

Whether a tool with the given name is registered.

Why: Lets callers skip registration when a tool already exists. What: HashMap membership check. Test: contains_after_register.

Source

pub async fn dispatch(&self, name: &str, args: Value) -> ToolResult

Dispatch a named tool call.

Why: Single place where missing-tool errors are surfaced as a structured ToolResult::Error so the LLM loop can continue. What: Looks up name, calls execute(args), returns the ToolResult. Test: Dispatch to an unregistered name returns ToolResult::Error; dispatch to a registered one returns ToolResult::Success.

Source

pub async fn dispatch_gated( &self, name: &str, args: Value, allowed: Option<&[String]>, ) -> ToolResult

Dispatch with an optional per-agent allowlist.

Why: Per-agent tool gating prevents, e.g., the plan-agent from shelling out or the research-agent from delegating. Allowlist is checked centrally so every call site inherits the same policy. What: If allowed is Some and does not contain name, returns a recoverable ToolResult::Error naming what is permitted. Otherwise delegates to dispatch. Test: dispatch_gated_rejects_disallowed.

Source

pub async fn dispatch_for_user( &self, name: &str, args: Value, allowed: Option<&[String]>, user: &UserIdentity, ) -> ToolResult

Dispatch honoring a UserIdentity’s ServiceTier.

Why: Some tools must be denied to less-trusted transports (Slack guest users, unauthenticated HTTP) even if the LLM tries to call them. Enforcing the tier check at the dispatch boundary means transport authors can’t accidentally bypass RBAC by forgetting to pre-filter the tool list. What: If the tool is registered AND user.can_access_tier is false, returns ToolResult::Error. Otherwise delegates to dispatch_gated. Test: dispatch_for_user_blocks_restricted_tier.

Source

pub fn gated(&self, allowed: &[String]) -> ToolRegistry

Build a new registry containing only the tools named in allowed.

Why: The agent loop calls dispatch_gated(name, args, None) — it does not thread a per-agent allowlist through every dispatch. The robust place to enforce an agent’s tools.allowed (issue #1029 acceptance criterion) is therefore before the loop runs: hand the loop a registry that simply does not contain the denied tools, so a denied call surfaces as the same structured “no tool registered” error and the model can self-correct. Unknown names in allowed are ignored (they register nothing); this keeps a typo in an agent config from being a hard error. What: Returns a fresh ToolRegistry whose map holds cloned Arcs for exactly the registered tools whose name() appears in allowed, preserving the shared executors (no re-construction). Order-independent. Test: gated_keeps_only_allowed, gated_ignores_unknown_names.

Source

pub fn schemas(&self) -> Vec<Value>

Emit all registered tool schemas as raw JSON values.

Why: The sub-agent tool-calling loop attaches raw JSON schemas to LLM requests; the OpenAI-compatible schema is produced by each tool. What: Collects tool.schema() for every registered tool. Test: Registering two tools returns a vector of length 2.

Source

pub fn schema_for(&self, name: &str) -> Option<Value>

Return the raw schema for a single registered tool by name.

Why: llm::ToolCallExtractor (#1023) validates one tool call’s arguments at a time; looking up exactly one schema is cheaper and simpler than enumerating and filtering the full schemas() vector on every dispatch. What: Returns Some(tool.schema()) for a registered name, else None. Test: schema_for_returns_registered_tool_schema, schema_for_none_when_missing.

Source

pub fn filter_tools_for_user( &self, user: &UserIdentity, ) -> Vec<Arc<dyn ToolExecutor>>

Return the subset of registered tools that user may invoke.

Why: Schema emission for the LLM request must reflect what the user can actually call, otherwise the model will hallucinate denied calls. What: Returns cloned Arcs for tools where user.can_access_tier is true. Test: filter_tools_for_user_drops_restricted.

Trait Implementations§

Source§

impl Default for ToolRegistry

Source§

fn default() -> ToolRegistry

Returns the “default value” for a type. Read more

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + 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: Sized + 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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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<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