bamboo-agent-core 2026.4.14

Core agent abstractions and execution primitives for the Bamboo agent framework
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Tool registry for managing and executing tools.
//!
//! This module provides a thread-safe registry for tool management,
//! including registration, lookup, and execution of tools.
//!
//! # Key Types
//!
//! - [`Tool`] - Trait for implementing executable tools
//! - [`ToolRegistry`] - Thread-safe tool registry
//! - [`RegistryError`] - Registration errors
//! - [`SharedTool`] - Reference-counted tool pointer
//!
//! # Usage
//!
//! ```rust,ignore
//! use bamboo_agent::agent::core::tools::registry::*;
//!
//! // Create a registry
//! let registry = ToolRegistry::new();
//!
//! // Register a tool
//! registry.register(MyTool::new())?;
//!
//! // Get tool schema for LLM
//! let schemas = registry.list_tools();
//!
//! // Execute a tool
//! let tool = registry.get("my_tool").unwrap();
//! let result = tool.execute(args).await?;
//! ```
//!
//! # Global Registry
//!
//! For convenience, a global singleton registry is available:
//!
//! ```rust,ignore
//! let registry = global_registry();
//! registry.register(my_tool)?;
//! ```

use std::sync::{Arc, OnceLock};

use async_trait::async_trait;
use dashmap::{mapref::entry::Entry, DashMap};
use thiserror::Error;

use crate::tools::{FunctionSchema, ToolError, ToolExecutionContext, ToolResult, ToolSchema};

/// Trait for implementing executable tools.
///
/// All tools must implement this trait to be registered with the tool registry.
///
/// # Required Methods
///
/// - `name()` - Unique tool identifier
/// - `description()` - Human-readable tool description
/// - `parameters_schema()` - JSON Schema for tool parameters
/// - `execute()` - Async tool execution logic
///
/// # Provided Methods
///
/// - `to_schema()` - Convert tool to LLM-compatible schema
///
/// # Example
///
/// ```rust,ignore
/// struct ReadFileTool;
///
/// #[async_trait]
/// impl Tool for ReadFileTool {
///     fn name(&self) -> &str {
///         "read_file"
///     }
///
///     fn description(&self) -> &str {
///         "Read file contents from disk"
///     }
///
///     fn parameters_schema(&self) -> serde_json::Value {
///         json!({
///             "type": "object",
///             "properties": {
///                 "path": {"type": "string"}
///             },
///             "required": ["path"]
///         })
///     }
///
///     async fn execute(&self, args: Value) -> Result<ToolResult, ToolError> {
///         let path = args["path"].as_str().unwrap();
///         let content = tokio::fs::read_to_string(path).await?;
///         Ok(ToolResult {
///             success: true,
///             result: content,
///             display_preference: None,
///         })
///     }
/// }
/// ```
#[async_trait]
pub trait Tool: Send + Sync {
    fn name(&self) -> &str;
    /// Human-readable tool description for LLM.
    fn description(&self) -> &str;
    /// JSON Schema for tool parameters.
    fn parameters_schema(&self) -> serde_json::Value;
    /// Declares whether this tool is read-only or mutating for orchestration and
    /// parallel scheduling decisions. Defaults to mutating to stay conservative.
    fn mutability(&self) -> crate::tools::ToolMutability {
        crate::tools::ToolMutability::Mutating
    }
    /// Args-aware mutability hook. Defaults to the static mutability declaration.
    fn call_mutability(&self, _args: &serde_json::Value) -> crate::tools::ToolMutability {
        self.mutability()
    }
    /// Declares whether this tool can safely run in parallel with other
    /// read-only tools. Defaults to false so tools remain serialized unless
    /// they opt in explicitly.
    fn concurrency_safe(&self) -> bool {
        false
    }
    /// Args-aware parallel-safety hook. Defaults to the static declaration.
    fn call_concurrency_safe(&self, _args: &serde_json::Value) -> bool {
        self.concurrency_safe()
    }
    /// Execute the tool with given arguments.
    async fn execute(&self, args: serde_json::Value) -> Result<ToolResult, ToolError>;

    /// Execute the tool with a streaming-capable context.
    ///
    /// Default implementation falls back to `execute()` for tools that don't
    /// need streaming.
    async fn execute_with_context(
        &self,
        args: serde_json::Value,
        _ctx: ToolExecutionContext<'_>,
    ) -> Result<ToolResult, ToolError> {
        self.execute(args).await
    }

    /// Convert tool to LLM-compatible schema.
    ///
    /// Creates a [`ToolSchema`] suitable for LLM function calling.
    fn to_schema(&self) -> ToolSchema {
        ToolSchema {
            schema_type: "function".to_string(),
            function: FunctionSchema {
                name: self.name().to_string(),
                description: self.description().to_string(),
                parameters: self.parameters_schema(),
            },
        }
    }
}

/// Reference-counted pointer to a tool.
pub type SharedTool = Arc<dyn Tool>;

/// Errors that can occur during tool registration.
///
/// # Variants
///
/// * `DuplicateTool` - Tool with same name already registered
/// * `InvalidTool` - Tool validation failed (e.g., empty name)
#[derive(Debug, Error, PartialEq, Eq)]
pub enum RegistryError {
    /// Tool with same name already exists in registry.
    #[error("tool with name '{0}' already registered")]
    DuplicateTool(String),

    /// Tool validation failed.
    #[error("invalid tool: {0}")]
    InvalidTool(String),
}

/// Thread-safe tool registry.
///
/// Manages a collection of tools with concurrent access support.
/// Uses a `DashMap` for lock-free concurrent operations.
///
/// # Features
///
/// - Thread-safe registration and lookup
/// - Tool schema generation for LLM
/// - Global singleton registry support
///
/// # Example
///
/// ```rust,ignore
/// let registry = ToolRegistry::new();
///
/// // Register tools
/// registry.register(ReadFileTool::new())?;
/// registry.register(WriteFileTool::new())?;
///
/// // List all tool schemas
/// let schemas = registry.list_tools();
///
/// // Get and execute a tool
/// if let Some(tool) = registry.get("read_file") {
///     let result = tool.execute(json!({"path": "test.txt"})).await?;
/// }
/// ```
pub struct ToolRegistry {
    tools: DashMap<String, SharedTool>,
}

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

impl ToolRegistry {
    /// Create a new empty tool registry.
    pub fn new() -> Self {
        Self {
            tools: DashMap::new(),
        }
    }

    /// Register a tool in the registry.
    ///
    /// # Arguments
    ///
    /// * `tool` - Tool to register
    ///
    /// # Errors
    ///
    /// Returns [`RegistryError::DuplicateTool`] if tool name already exists.
    /// Returns [`RegistryError::InvalidTool`] if tool name is empty.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// registry.register(MyTool::new())?;
    /// ```
    pub fn register<T>(&self, tool: T) -> Result<(), RegistryError>
    where
        T: Tool + 'static,
    {
        self.register_shared(Arc::new(tool))
    }

    /// Register a shared tool reference.
    ///
    /// # Arguments
    ///
    /// * `tool` - Shared tool reference
    ///
    /// # Errors
    ///
    /// Same as [`register`](Self::register).
    pub fn register_shared(&self, tool: SharedTool) -> Result<(), RegistryError> {
        let name = tool.name().trim();

        if name.is_empty() {
            return Err(RegistryError::InvalidTool(
                "tool name cannot be empty".to_string(),
            ));
        }

        match self.tools.entry(name.to_string()) {
            Entry::Occupied(_) => Err(RegistryError::DuplicateTool(name.to_string())),
            Entry::Vacant(entry) => {
                entry.insert(tool);
                Ok(())
            }
        }
    }

    /// Get a tool by name.
    ///
    /// # Arguments
    ///
    /// * `name` - Tool name
    ///
    /// # Returns
    ///
    /// Shared tool reference if found, `None` otherwise.
    pub fn get(&self, name: &str) -> Option<SharedTool> {
        self.tools.get(name).map(|entry| Arc::clone(entry.value()))
    }

    /// Check if a tool exists in the registry.
    pub fn contains(&self, name: &str) -> bool {
        self.tools.contains_key(name)
    }

    /// List all tool schemas.
    ///
    /// Returns schemas sorted alphabetically by tool name.
    pub fn list_tools(&self) -> Vec<ToolSchema> {
        let mut tools: Vec<ToolSchema> = self
            .tools
            .iter()
            .map(|entry| entry.value().to_schema())
            .collect();
        tools.sort_by_key(|t| t.function.name.clone());
        tools
    }

    /// List all tool names.
    ///
    /// Returns names sorted alphabetically.
    pub fn list_tool_names(&self) -> Vec<String> {
        let mut names: Vec<String> = self.tools.iter().map(|entry| entry.key().clone()).collect();
        names.sort();
        names
    }

    /// Remove a tool from the registry.
    ///
    /// # Returns
    ///
    /// `true` if tool was removed, `false` if not found.
    pub fn unregister(&self, name: &str) -> bool {
        self.tools.remove(name).is_some()
    }

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

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

    /// Remove all tools from the registry.
    pub fn clear(&self) {
        self.tools.clear();
    }
}

/// Global tool registry singleton.
static GLOBAL_REGISTRY: OnceLock<ToolRegistry> = OnceLock::new();

/// Get the global tool registry.
///
/// The global registry is a singleton that persists for the lifetime
/// of the application. Useful for sharing tools across components.
///
/// # Example
///
/// ```rust,ignore
/// let registry = global_registry();
/// registry.register(my_tool)?;
/// ```
pub fn global_registry() -> &'static ToolRegistry {
    GLOBAL_REGISTRY.get_or_init(ToolRegistry::new)
}

/// Normalize a tool name by removing namespace prefix.
///
/// # Arguments
///
/// * `name` - Tool name (may include `::` namespace separator)
///
/// # Returns
///
/// Tool name after the last `::`, or the original name if no separator.
///
/// # Example
///
/// ```rust,ignore
/// assert_eq!(normalize_tool_name("bamboo::read_file"), "read_file");
/// assert_eq!(normalize_tool_name("read_file"), "read_file");
/// ```
pub fn normalize_tool_name(name: &str) -> &str {
    name.split("::").last().unwrap_or(name)
}

#[cfg(test)]
mod tests {
    use super::*;

    use serde_json::json;

    struct TestTool {
        name: &'static str,
        description: &'static str,
    }

    #[async_trait]
    impl Tool for TestTool {
        fn name(&self) -> &str {
            self.name
        }

        fn description(&self) -> &str {
            self.description
        }

        fn parameters_schema(&self) -> serde_json::Value {
            json!({
                "type": "object",
                "properties": {}
            })
        }

        async fn execute(&self, _args: serde_json::Value) -> Result<ToolResult, ToolError> {
            Ok(ToolResult {
                success: true,
                result: "ok".to_string(),
                display_preference: None,
            })
        }
    }

    #[test]
    fn register_and_get() {
        let registry = ToolRegistry::new();
        let tool = TestTool {
            name: "test_tool",
            description: "test tool",
        };

        assert!(registry.register(tool).is_ok());
        assert!(registry.get("test_tool").is_some());
        assert!(registry.get("unknown").is_none());
    }

    #[test]
    fn duplicate_tool_registration() {
        let registry = ToolRegistry::new();

        registry
            .register(TestTool {
                name: "dup",
                description: "first",
            })
            .unwrap();

        let duplicate = registry.register(TestTool {
            name: "dup",
            description: "second",
        });

        assert!(matches!(duplicate, Err(RegistryError::DuplicateTool(name)) if name == "dup"));
    }

    #[test]
    fn list_tools_returns_registered_tools() {
        let registry = ToolRegistry::new();

        registry
            .register(TestTool {
                name: "tool_a",
                description: "tool a",
            })
            .unwrap();
        registry
            .register(TestTool {
                name: "tool_b",
                description: "tool b",
            })
            .unwrap();

        let tools = registry.list_tools();

        assert_eq!(tools.len(), 2);
        assert_eq!(tools[0].function.name, "tool_a");
        assert_eq!(tools[1].function.name, "tool_b");
    }

    #[test]
    fn register_rejects_empty_tool_name() {
        let registry = ToolRegistry::new();

        let result = registry.register(TestTool {
            name: "",
            description: "invalid",
        });

        assert!(
            matches!(result, Err(RegistryError::InvalidTool(reason)) if reason == "tool name cannot be empty")
        );
    }

    #[test]
    fn normalize_tool_name_handles_namespaced_inputs() {
        assert_eq!(normalize_tool_name("read_file"), "read_file");
        assert_eq!(normalize_tool_name("default::read_file"), "read_file");
        assert_eq!(normalize_tool_name("a::b::c::read_file"), "read_file");
    }
}