serdes-ai-tools 0.2.2

Tool system for serdes-ai agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Tool registry for managing multiple tools.
//!
//! This module provides the `ToolRegistry` type which allows registering,
//! looking up, and calling tools by name.

use std::collections::HashMap;
use std::sync::Arc;

use crate::{
    definition::ToolDefinition, errors::ToolError, return_types::ToolReturn, tool::Tool, RunContext,
};

/// Registry of tools that can be called by an agent.
///
/// The `ToolRegistry` manages a collection of tools and provides:
/// - Registration of tools
/// - Lookup by name
/// - Batch retrieval of definitions
/// - Tool execution
///
/// # Type Parameters
///
/// - `Deps`: The type of dependencies available to tools.
///
/// # Example
///
/// ```ignore
/// use serdes_ai_tools::{ToolRegistry, Tool};
///
/// let mut registry = ToolRegistry::<()>::new();
/// registry.register(MyTool::new());
///
/// // Get all definitions for the model
/// let defs = registry.definitions();
///
/// // Call a tool by name
/// let result = registry.call("my_tool", &ctx, args).await?;
/// ```
pub struct ToolRegistry<Deps = ()> {
    tools: HashMap<String, Arc<dyn Tool<Deps>>>,
}

impl<Deps> ToolRegistry<Deps> {
    /// Create a new empty registry.
    #[must_use]
    pub fn new() -> Self {
        Self {
            tools: HashMap::new(),
        }
    }

    /// Register a tool.
    ///
    /// Returns `&mut self` for chaining.
    ///
    /// # Panics
    ///
    /// Panics if a tool with the same name is already registered.
    /// Use `register_replace` to allow replacement.
    pub fn register<T: Tool<Deps> + 'static>(&mut self, tool: T) -> &mut Self {
        let name = tool.definition().name.clone();
        if self.tools.contains_key(&name) {
            panic!("Tool '{}' is already registered", name);
        }
        self.tools.insert(name, Arc::new(tool));
        self
    }

    /// Register a tool, replacing any existing tool with the same name.
    pub fn register_replace<T: Tool<Deps> + 'static>(&mut self, tool: T) -> &mut Self {
        let name = tool.definition().name.clone();
        self.tools.insert(name, Arc::new(tool));
        self
    }

    /// Register a boxed tool.
    pub fn register_boxed(&mut self, tool: Arc<dyn Tool<Deps>>) -> &mut Self {
        let name = tool.definition().name.clone();
        self.tools.insert(name, tool);
        self
    }

    /// Register a tool if not already present.
    ///
    /// Returns `true` if the tool was registered, `false` if it already existed.
    pub fn register_if_absent<T: Tool<Deps> + 'static>(&mut self, tool: T) -> bool {
        let name = tool.definition().name.clone();
        if let std::collections::hash_map::Entry::Vacant(e) = self.tools.entry(name) {
            e.insert(Arc::new(tool));
            true
        } else {
            false
        }
    }

    /// Unregister a tool by name.
    ///
    /// Returns the removed tool if it existed.
    pub fn unregister(&mut self, name: &str) -> Option<Arc<dyn Tool<Deps>>> {
        self.tools.remove(name)
    }

    /// Get all tool definitions.
    #[must_use]
    pub fn definitions(&self) -> Vec<ToolDefinition> {
        self.tools.values().map(|t| t.definition()).collect()
    }

    /// Get tool definitions as a map by name.
    #[must_use]
    pub fn definitions_map(&self) -> HashMap<String, ToolDefinition> {
        self.tools
            .iter()
            .map(|(name, tool)| (name.clone(), tool.definition()))
            .collect()
    }

    /// Get definitions with prepare applied.
    ///
    /// This calls the `prepare` method on each tool with the given context,
    /// allowing tools to modify their definitions or hide themselves.
    pub async fn prepared_definitions(&self, ctx: &RunContext<Deps>) -> Vec<ToolDefinition>
    where
        Deps: Send + Sync,
    {
        let mut defs = Vec::with_capacity(self.tools.len());
        for tool in self.tools.values() {
            let base_def = tool.definition();
            if let Some(prepared) = tool.prepare(ctx, base_def).await {
                defs.push(prepared);
            }
        }
        defs
    }

    /// Call a tool by name.
    ///
    /// # Errors
    ///
    /// Returns `ToolError::NotFound` if no tool with the given name exists.
    pub async fn call(
        &self,
        name: &str,
        ctx: &RunContext<Deps>,
        args: serde_json::Value,
    ) -> Result<ToolReturn, ToolError>
    where
        Deps: Send + Sync,
    {
        let tool = self
            .tools
            .get(name)
            .ok_or_else(|| ToolError::not_found(name))?;

        tool.call(ctx, args).await
    }

    /// Check if a tool exists.
    #[must_use]
    pub fn contains(&self, name: &str) -> bool {
        self.tools.contains_key(name)
    }

    /// Get the number of registered tools.
    #[must_use]
    pub fn len(&self) -> usize {
        self.tools.len()
    }

    /// Check if the registry is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.tools.is_empty()
    }

    /// Get a tool by name.
    #[must_use]
    pub fn get(&self, name: &str) -> Option<&Arc<dyn Tool<Deps>>> {
        self.tools.get(name)
    }

    /// Get all tool names.
    #[must_use]
    pub fn names(&self) -> Vec<&str> {
        self.tools.keys().map(|s| s.as_str()).collect()
    }

    /// Iterate over tools.
    pub fn iter(&self) -> impl Iterator<Item = (&String, &Arc<dyn Tool<Deps>>)> {
        self.tools.iter()
    }

    /// Merge another registry into this one.
    ///
    /// Tools from `other` will replace tools with the same name in `self`.
    pub fn merge(&mut self, other: ToolRegistry<Deps>) {
        self.tools.extend(other.tools);
    }

    /// Clear all registered tools.
    pub fn clear(&mut self) {
        self.tools.clear();
    }

    /// Get max retries for a tool.
    #[must_use]
    pub fn max_retries(&self, name: &str) -> Option<u32> {
        self.tools.get(name).and_then(|t| t.max_retries())
    }
}

impl<Deps> Default for ToolRegistry<Deps> {
    fn default() -> Self {
        Self::new()
    }
}

impl<Deps> std::fmt::Debug for ToolRegistry<Deps> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ToolRegistry")
            .field("tools", &self.names())
            .finish()
    }
}

impl<Deps> Clone for ToolRegistry<Deps> {
    fn clone(&self) -> Self {
        Self {
            tools: self.tools.clone(),
        }
    }
}

/// Trait for types that can provide tools to a registry.
pub trait ToolProvider<Deps> {
    /// Register tools with the given registry.
    fn register_tools(&self, registry: &mut ToolRegistry<Deps>);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{schema::SchemaBuilder, ToolResult};
    use async_trait::async_trait;

    struct EchoTool;

    #[async_trait]
    impl Tool<()> for EchoTool {
        fn definition(&self) -> ToolDefinition {
            ToolDefinition::new("echo", "Echo the message").with_parameters(
                SchemaBuilder::new()
                    .string("message", "Message", true)
                    .build()
                    .expect("SchemaBuilder JSON serialization failed"),
            )
        }

        async fn call(&self, _ctx: &RunContext<()>, args: serde_json::Value) -> ToolResult {
            let msg = args["message"].as_str().unwrap_or("<empty>");
            Ok(ToolReturn::text(msg))
        }
    }

    struct AddTool;

    #[async_trait]
    impl Tool<()> for AddTool {
        fn definition(&self) -> ToolDefinition {
            ToolDefinition::new("add", "Add two numbers")
        }

        async fn call(&self, _ctx: &RunContext<()>, args: serde_json::Value) -> ToolResult {
            let a = args["a"].as_f64().unwrap_or(0.0);
            let b = args["b"].as_f64().unwrap_or(0.0);
            Ok(ToolReturn::text(format!("{}", a + b)))
        }
    }

    #[test]
    fn test_registry_new() {
        let registry = ToolRegistry::<()>::new();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
    }

    #[test]
    fn test_register() {
        let mut registry = ToolRegistry::new();
        registry.register(EchoTool);
        assert_eq!(registry.len(), 1);
        assert!(registry.contains("echo"));
    }

    #[test]
    #[should_panic(expected = "already registered")]
    fn test_register_duplicate_panics() {
        let mut registry = ToolRegistry::new();
        registry.register(EchoTool);
        registry.register(EchoTool); // Should panic
    }

    #[test]
    fn test_register_replace() {
        let mut registry = ToolRegistry::new();
        registry.register(EchoTool);
        registry.register_replace(EchoTool); // Should not panic
        assert_eq!(registry.len(), 1);
    }

    #[test]
    fn test_register_if_absent() {
        let mut registry = ToolRegistry::new();
        assert!(registry.register_if_absent(EchoTool));
        assert!(!registry.register_if_absent(EchoTool));
        assert_eq!(registry.len(), 1);
    }

    #[test]
    fn test_unregister() {
        let mut registry = ToolRegistry::new();
        registry.register(EchoTool);
        let removed = registry.unregister("echo");
        assert!(removed.is_some());
        assert!(registry.is_empty());
    }

    #[test]
    fn test_definitions() {
        let mut registry = ToolRegistry::new();
        registry.register(EchoTool);
        registry.register(AddTool);

        let defs = registry.definitions();
        assert_eq!(defs.len(), 2);
        let names: Vec<_> = defs.iter().map(|d| d.name.as_str()).collect();
        assert!(names.contains(&"echo"));
        assert!(names.contains(&"add"));
    }

    #[tokio::test]
    async fn test_call() {
        let mut registry = ToolRegistry::new();
        registry.register(EchoTool);

        let ctx = RunContext::minimal("test");
        let result = registry
            .call("echo", &ctx, serde_json::json!({"message": "hello"}))
            .await
            .unwrap();
        assert_eq!(result.as_text(), Some("hello"));
    }

    #[tokio::test]
    async fn test_call_not_found() {
        let registry = ToolRegistry::<()>::new();
        let ctx = RunContext::minimal("test");
        let result = registry
            .call("nonexistent", &ctx, serde_json::json!({}))
            .await;
        assert!(matches!(result, Err(ToolError::NotFound(_))));
    }

    #[test]
    fn test_get() {
        let mut registry = ToolRegistry::new();
        registry.register(EchoTool);

        assert!(registry.get("echo").is_some());
        assert!(registry.get("nonexistent").is_none());
    }

    #[test]
    fn test_names() {
        let mut registry = ToolRegistry::new();
        registry.register(EchoTool);
        registry.register(AddTool);

        let names = registry.names();
        assert_eq!(names.len(), 2);
    }

    #[test]
    fn test_merge() {
        let mut registry1 = ToolRegistry::new();
        registry1.register(EchoTool);

        let mut registry2 = ToolRegistry::new();
        registry2.register(AddTool);

        registry1.merge(registry2);
        assert_eq!(registry1.len(), 2);
    }

    #[test]
    fn test_clear() {
        let mut registry = ToolRegistry::new();
        registry.register(EchoTool);
        registry.clear();
        assert!(registry.is_empty());
    }

    #[tokio::test]
    async fn test_prepared_definitions() {
        let mut registry = ToolRegistry::new();
        registry.register(EchoTool);

        let ctx = RunContext::minimal("test");
        let prepared = registry.prepared_definitions(&ctx).await;
        assert_eq!(prepared.len(), 1);
    }

    #[test]
    fn test_clone() {
        let mut registry = ToolRegistry::new();
        registry.register(EchoTool);

        let cloned = registry.clone();
        assert_eq!(cloned.len(), registry.len());
    }

    #[test]
    fn test_debug() {
        let mut registry = ToolRegistry::new();
        registry.register(EchoTool);
        let debug = format!("{:?}", registry);
        assert!(debug.contains("ToolRegistry"));
        assert!(debug.contains("echo"));
    }
}