Skip to main content

llm_stack/tool/
registry.rs

1//! Tool registry for managing and executing tools.
2
3use std::collections::HashMap;
4use std::future::Future;
5use std::pin::Pin;
6use std::sync::Arc;
7use std::time::Duration;
8
9use rand::Rng;
10
11use super::ToolHandler;
12use crate::chat::{ToolCall, ToolResult};
13use crate::intercept::domain::{ToolExec, ToolRequest, ToolResponse};
14use crate::intercept::{InterceptorStack, Operation};
15use crate::provider::{ToolDefinition, ToolRetryConfig};
16
17/// A registry of tool handlers, indexed by name.
18///
19/// Generic over context type `Ctx` which is passed to tool handlers on
20/// execution. Default is `()` for backwards compatibility.
21///
22/// Provides validation of tool call arguments against their schemas
23/// and parallel execution of multiple tool calls.
24///
25/// # Interceptors
26///
27/// Tool execution can be wrapped with interceptors for cross-cutting concerns
28/// like logging, approval gates, or rate limiting:
29///
30/// ```rust,ignore
31/// use llm_stack::{ToolRegistry, tool_fn};
32/// use llm_stack::intercept::{InterceptorStack, ToolExec, Approval, ApprovalDecision};
33///
34/// let mut registry: ToolRegistry<()> = ToolRegistry::new()
35///     .with_interceptors(
36///         InterceptorStack::<ToolExec<()>>::new()
37///             .with(Approval::new(|req| {
38///                 if req.name.starts_with("dangerous_") {
39///                     ApprovalDecision::Deny("Not allowed".into())
40///                 } else {
41///                     ApprovalDecision::Allow
42///                 }
43///             }))
44///     );
45/// ```
46pub struct ToolRegistry<Ctx = ()>
47where
48    Ctx: Send + Sync + 'static,
49{
50    pub(crate) handlers: HashMap<String, Arc<dyn ToolHandler<Ctx>>>,
51    interceptors: InterceptorStack<ToolExec<Ctx>>,
52}
53
54impl<Ctx> Default for ToolRegistry<Ctx>
55where
56    Ctx: Send + Sync + 'static,
57{
58    fn default() -> Self {
59        Self {
60            handlers: HashMap::new(),
61            interceptors: InterceptorStack::new(),
62        }
63    }
64}
65
66impl<Ctx> Clone for ToolRegistry<Ctx>
67where
68    Ctx: Send + Sync + 'static,
69{
70    /// Clone the registry.
71    ///
72    /// This is cheap — it clones `Arc` pointers to handlers, not the
73    /// handlers themselves.
74    fn clone(&self) -> Self {
75        Self {
76            handlers: self.handlers.clone(),
77            interceptors: self.interceptors.clone(),
78        }
79    }
80}
81
82impl<Ctx> std::fmt::Debug for ToolRegistry<Ctx>
83where
84    Ctx: Send + Sync + 'static,
85{
86    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87        f.debug_struct("ToolRegistry")
88            .field("tools", &self.handlers.keys().collect::<Vec<_>>())
89            .field("interceptors", &self.interceptors.len())
90            .finish()
91    }
92}
93
94impl<Ctx: Send + Sync + 'static> ToolRegistry<Ctx> {
95    /// Creates an empty registry.
96    pub fn new() -> Self {
97        Self::default()
98    }
99
100    /// Registers a tool handler.
101    ///
102    /// If a handler with the same name already exists, it is replaced.
103    pub fn register(&mut self, handler: impl ToolHandler<Ctx> + 'static) -> &mut Self {
104        let name = handler.definition().name.clone();
105        self.handlers.insert(name, Arc::new(handler));
106        self
107    }
108
109    /// Registers a shared tool handler.
110    pub fn register_shared(&mut self, handler: Arc<dyn ToolHandler<Ctx>>) -> &mut Self {
111        let name = handler.definition().name.clone();
112        self.handlers.insert(name, handler);
113        self
114    }
115
116    /// Returns the handler for the given tool name.
117    pub fn get(&self, name: &str) -> Option<&Arc<dyn ToolHandler<Ctx>>> {
118        self.handlers.get(name)
119    }
120
121    /// Returns whether a tool with the given name is registered.
122    pub fn contains(&self, name: &str) -> bool {
123        self.handlers.contains_key(name)
124    }
125
126    /// Returns the definitions of all registered tools.
127    ///
128    /// Pass this to [`ChatParams::tools`](crate::provider::ChatParams::tools) to tell the model which
129    /// tools are available.
130    pub fn definitions(&self) -> Vec<ToolDefinition> {
131        self.handlers.values().map(|h| h.definition()).collect()
132    }
133
134    /// Returns the number of registered tools.
135    pub fn len(&self) -> usize {
136        self.handlers.len()
137    }
138
139    /// Returns true if no tools are registered.
140    pub fn is_empty(&self) -> bool {
141        self.handlers.is_empty()
142    }
143
144    /// Returns a new registry excluding the named tools.
145    ///
146    /// Useful for creating scoped registries in Master/Worker patterns
147    /// where workers should not have access to certain tools (e.g., `spawn_task`).
148    ///
149    /// # Example
150    ///
151    /// ```rust
152    /// use llm_stack::ToolRegistry;
153    ///
154    /// let master_registry: ToolRegistry<()> = ToolRegistry::new();
155    /// // ... register tools ...
156    ///
157    /// // Workers can't spawn or use admin tools
158    /// let worker_registry = master_registry.without(["spawn_task", "admin_tool"]);
159    /// ```
160    #[must_use]
161    pub fn without<'a>(&self, names: impl IntoIterator<Item = &'a str>) -> Self {
162        use std::collections::HashSet;
163        let exclude: HashSet<&str> = names.into_iter().collect();
164        let mut new = Self {
165            handlers: HashMap::new(),
166            interceptors: self.interceptors.clone(),
167        };
168        for (name, handler) in &self.handlers {
169            if !exclude.contains(name.as_str()) {
170                new.handlers.insert(name.clone(), Arc::clone(handler));
171            }
172        }
173        new
174    }
175
176    /// Returns a new registry with only the named tools.
177    ///
178    /// Useful for creating minimal registries with specific capabilities.
179    ///
180    /// # Example
181    ///
182    /// ```rust
183    /// use llm_stack::ToolRegistry;
184    ///
185    /// let full_registry: ToolRegistry<()> = ToolRegistry::new();
186    /// // ... register tools ...
187    ///
188    /// // Read-only registry with just search tools
189    /// let search_registry = full_registry.only(["search_docs", "search_web"]);
190    /// ```
191    #[must_use]
192    pub fn only<'a>(&self, names: impl IntoIterator<Item = &'a str>) -> Self {
193        use std::collections::HashSet;
194        let include: HashSet<&str> = names.into_iter().collect();
195        let mut new = Self {
196            handlers: HashMap::new(),
197            interceptors: self.interceptors.clone(),
198        };
199        for (name, handler) in &self.handlers {
200            if include.contains(name.as_str()) {
201                new.handlers.insert(name.clone(), Arc::clone(handler));
202            }
203        }
204        new
205    }
206
207    /// Sets the interceptor stack for all tool executions.
208    ///
209    /// Interceptors run in the order added (first = outermost). They can
210    /// inspect, modify, or block tool calls before they reach the handler.
211    ///
212    /// # Example
213    ///
214    /// ```rust,ignore
215    /// use llm_stack::{ToolRegistry, tool_fn};
216    /// use llm_stack::intercept::{InterceptorStack, ToolExec, Approval, ApprovalDecision, Retry};
217    ///
218    /// let registry: ToolRegistry<()> = ToolRegistry::new()
219    ///     .with_interceptors(
220    ///         InterceptorStack::<ToolExec<()>>::new()
221    ///             .with(Approval::new(|req| {
222    ///                 if req.name == "dangerous" {
223    ///                     ApprovalDecision::Deny("Not allowed".into())
224    ///                 } else {
225    ///                     ApprovalDecision::Allow
226    ///                 }
227    ///             }))
228    ///             .with(Retry::default())
229    ///     );
230    /// ```
231    #[must_use]
232    pub fn with_interceptors(mut self, interceptors: InterceptorStack<ToolExec<Ctx>>) -> Self {
233        self.interceptors = interceptors;
234        self
235    }
236
237    /// Executes a single tool call with schema validation and optional retry.
238    ///
239    /// 1. Looks up the handler by [`ToolCall::name`]
240    /// 2. Validates arguments against the tool's parameter schema
241    /// 3. Runs the call through interceptors (if any)
242    /// 4. Invokes the handler with the provided context
243    /// 5. If the tool has retry configuration and execution fails,
244    ///    retries with exponential backoff
245    ///
246    /// Returns a [`ToolResult`] (always succeeds at the outer level).
247    /// Execution errors are captured in `ToolResult::is_error`.
248    pub async fn execute(&self, call: &ToolCall, ctx: &Ctx) -> ToolResult {
249        self.execute_inner(&call.name, &call.id, call.arguments.clone(), ctx)
250            .await
251    }
252
253    /// Executes a tool by name with the given arguments.
254    ///
255    /// This is a lower-level method used internally when the tool call
256    /// components are already separated (e.g., from `execute_with_events`).
257    /// Accepts owned arguments to avoid an extra deep clone of `serde_json::Value`.
258    pub(crate) async fn execute_by_name(
259        &self,
260        name: &str,
261        call_id: &str,
262        arguments: serde_json::Value,
263        ctx: &Ctx,
264    ) -> ToolResult {
265        self.execute_inner(name, call_id, arguments, ctx).await
266    }
267
268    /// Shared implementation for `execute` and `execute_by_name`.
269    async fn execute_inner(
270        &self,
271        name: &str,
272        call_id: &str,
273        arguments: serde_json::Value,
274        ctx: &Ctx,
275    ) -> ToolResult {
276        let Some(handler) = self.handlers.get(name) else {
277            return ToolResult {
278                tool_call_id: call_id.to_string(),
279                content: format!("Unknown tool: {name}"),
280                is_error: true,
281            };
282        };
283
284        // Validate arguments against schema
285        #[cfg(feature = "schema")]
286        {
287            let definition = handler.definition();
288            if let Err(e) = definition.parameters.validate(&arguments) {
289                return ToolResult {
290                    tool_call_id: call_id.to_string(),
291                    content: format!("Invalid arguments for tool '{name}': {e}"),
292                    is_error: true,
293                };
294            }
295        }
296
297        let request = ToolRequest {
298            name: name.to_string(),
299            call_id: call_id.to_string(),
300            arguments,
301        };
302
303        let operation = ToolHandlerOperation {
304            handler: handler.clone(),
305            ctx,
306            retry_config: handler.definition().retry,
307        };
308
309        let response = self.interceptors.execute(&request, &operation).await;
310
311        ToolResult {
312            tool_call_id: request.call_id,
313            content: response.content,
314            is_error: response.is_error,
315        }
316    }
317
318    /// Executes multiple tool calls, preserving order.
319    ///
320    /// When `parallel` is true, all calls run concurrently via
321    /// `futures::future::join_all`. When false, they run sequentially.
322    pub async fn execute_all(
323        &self,
324        calls: &[ToolCall],
325        ctx: &Ctx,
326        parallel: bool,
327    ) -> Vec<ToolResult> {
328        if !parallel || calls.len() <= 1 {
329            let mut results = Vec::with_capacity(calls.len());
330            for call in calls {
331                results.push(self.execute(call, ctx).await);
332            }
333            return results;
334        }
335
336        // Parallel execution using join_all (no spawn needed)
337        let futures: Vec<_> = calls.iter().map(|call| self.execute(call, ctx)).collect();
338        futures::future::join_all(futures).await
339    }
340}
341
342/// Computes backoff duration with exponential growth and jitter.
343///
344/// Formula: `min(initial * multiplier^attempt, max) * random(1-jitter, 1)`
345fn compute_backoff(config: &ToolRetryConfig, attempt: u32) -> Duration {
346    // Safe to cast: attempt is bounded by max_retries which is u32,
347    // and reasonable values are << i32::MAX
348    #[allow(clippy::cast_possible_wrap)]
349    let base =
350        config.initial_backoff.as_secs_f64() * config.backoff_multiplier.powi(attempt as i32);
351    let capped = base.min(config.max_backoff.as_secs_f64());
352
353    // Apply jitter: random value in range [1-jitter, 1]
354    let jitter_factor = if config.jitter > 0.0 {
355        let min_factor = 1.0 - config.jitter;
356        let mut rng = rand::rng();
357        rng.random_range(min_factor..=1.0)
358    } else {
359        1.0
360    };
361
362    Duration::from_secs_f64(capped * jitter_factor)
363}
364
365/// Wraps a tool handler as an [`Operation`] for the interceptor stack.
366///
367/// This struct captures the handler, context, and retry config so that
368/// the interceptor stack can execute the tool.
369struct ToolHandlerOperation<'a, Ctx: Send + Sync + 'static> {
370    handler: Arc<dyn ToolHandler<Ctx>>,
371    ctx: &'a Ctx,
372    retry_config: Option<ToolRetryConfig>,
373}
374
375impl<Ctx: Send + Sync + 'static> Operation<ToolExec<Ctx>> for ToolHandlerOperation<'_, Ctx> {
376    fn execute<'b>(
377        &'b self,
378        input: &'b ToolRequest,
379    ) -> Pin<Box<dyn Future<Output = ToolResponse> + Send + 'b>>
380    where
381        ToolRequest: Sync,
382    {
383        Box::pin(async move {
384            match &self.retry_config {
385                Some(config) => execute_with_retry(&self.handler, input, self.ctx, config).await,
386                None => execute_once(&self.handler, input, self.ctx).await,
387            }
388        })
389    }
390}
391
392/// Executes a tool once without retry.
393async fn execute_once<Ctx: Send + Sync + 'static>(
394    handler: &Arc<dyn ToolHandler<Ctx>>,
395    request: &ToolRequest,
396    ctx: &Ctx,
397) -> ToolResponse {
398    match handler.execute(request.arguments.clone(), ctx).await {
399        Ok(output) => ToolResponse {
400            content: output.content,
401            is_error: false,
402        },
403        Err(e) => ToolResponse {
404            content: e.message,
405            is_error: true,
406        },
407    }
408}
409
410/// Executes a tool with retry logic.
411async fn execute_with_retry<Ctx: Send + Sync + 'static>(
412    handler: &Arc<dyn ToolHandler<Ctx>>,
413    request: &ToolRequest,
414    ctx: &Ctx,
415    config: &ToolRetryConfig,
416) -> ToolResponse {
417    let mut attempt = 0u32;
418
419    loop {
420        match handler.execute(request.arguments.clone(), ctx).await {
421            Ok(output) => {
422                return ToolResponse {
423                    content: output.content,
424                    is_error: false,
425                };
426            }
427            Err(e) => {
428                let error_msg = e.message;
429
430                // Check if we should retry this error
431                let should_retry = config
432                    .retry_if
433                    .as_ref()
434                    .is_none_or(|predicate| predicate(&error_msg));
435
436                if !should_retry || attempt >= config.max_retries {
437                    return ToolResponse {
438                        content: error_msg,
439                        is_error: true,
440                    };
441                }
442
443                // Calculate backoff with jitter
444                let backoff = compute_backoff(config, attempt);
445                tokio::time::sleep(backoff).await;
446
447                attempt += 1;
448            }
449        }
450    }
451}