Skip to main content

bamboo_agent_core/tools/
mod.rs

1//! Tool execution system for Bamboo agents.
2//!
3//! This module provides a comprehensive framework for defining, registering, and executing
4//! tools that can be used by AI agents to interact with external systems.
5//!
6//! # Architecture
7//!
8//! The tools system is built around several key components:
9//!
10//! - **accumulator**: Accumulates partial tool calls from streaming responses
11//! - **agentic**: Agentic tool execution with multi-step capabilities
12//! - **executor**: Core tool execution logic
13//! - **registry**: Tool registration and lookup
14//! - **result_handler**: Processes tool results and handles agentic support
15//! - **smart_code_review**: Specialized tool for intelligent code review
16//! - **types**: Core type definitions for tools
17//!
18//! # Key Concepts
19//!
20//! ## Tool Registry
21//!
22//! Tools are registered in a central [`ToolRegistry`] that maps tool names to their
23//! implementations. The registry supports:
24//!
25//! - Dynamic tool registration
26//! - Tool name normalization
27//! - Global singleton access via [`global_registry`]
28//!
29//! ## Tool Execution
30//!
31//! Tools implement the [`ToolExecutor`] trait and can be executed via [`execute_tool_call`].
32//! The execution flow:
33//!
34//! 1. Parse tool arguments from JSON
35//! 2. Execute the tool logic
36//! 3. Return a [`ToolResult`] with success/failure status
37//!
38//! ## Agentic Tools
39//!
40//! Some tools support "agentic" behavior, allowing multi-step execution:
41//!
42//! - [`AgenticTool`]: Marker trait for agentic tools
43//! - [`AgenticContext`]: Context for agentic execution
44//! - [`AgenticToolResult`]: Extended result type with sub-actions
45//!
46//! # Example
47//!
48//! ```rust,ignore
49//! use async_trait::async_trait;
50//! use bamboo_agent::agent::core::tools::{
51//!     execute_tool_call, FunctionCall, ToolCall, ToolError, ToolExecutor, ToolResult, ToolSchema,
52//! };
53//!
54//! struct NoopExecutor;
55//!
56//! #[async_trait]
57//! impl ToolExecutor for NoopExecutor {
58//!     async fn execute(&self, call: &ToolCall) -> Result<ToolResult, ToolError> {
59//!         Err(ToolError::NotFound(call.function.name.clone()))
60//!     }
61//!
62//!     fn list_tools(&self) -> Vec<ToolSchema> {
63//!         Vec::new()
64//!     }
65//! }
66//!
67//! #[tokio::main]
68//! async fn main() {
69//!     // Execute a tool call (this example uses a no-op executor).
70//!     let call = ToolCall {
71//!         id: "call-1".to_string(),
72//!         tool_type: "function".to_string(),
73//!         function: FunctionCall {
74//!             name: "read_file".to_string(),
75//!             arguments: r#"{\"path\":\"/tmp/test.txt\"}"#.to_string(),
76//!         },
77//!     };
78//!
79//!     let _ = execute_tool_call(&call, &NoopExecutor, None).await;
80//! }
81//! ```
82//!
83//! # Re-exports
84//!
85//! Key types and functions re-exported for convenience:
86//!
87//! - Accumulator: [`ToolCallAccumulator`], [`PartialToolCall`], [`finalize_tool_calls`]
88//! - Agentic: [`AgenticTool`], [`AgenticContext`], [`AgenticToolResult`], [`ToolGoal`]
89//! - Executor: [`ToolExecutor`], [`execute_tool_call`], [`ToolError`]
90//! - Registry: [`ToolRegistry`], [`Tool`], [`global_registry`]
91//! - Types: [`ToolCall`], [`ToolResult`], [`ToolSchema`]
92
93pub mod accumulator;
94pub mod agentic;
95pub mod context;
96pub mod executor;
97pub mod registry;
98pub mod result_handler;
99pub mod smart_code_review;
100pub mod types;
101
102pub use accumulator::{
103    finalize_tool_calls, update_partial_tool_call, PartialToolCall, ToolCallAccumulator,
104};
105pub use agentic::{
106    convert_from_standard_result, convert_to_standard_result, AgenticContext, AgenticTool,
107    AgenticToolExecutor, AgenticToolResult, Interaction, InteractionRole, ToolGoal,
108};
109pub use context::ToolExecutionContext;
110pub use executor::{execute_tool_call, execute_tool_call_with_context, ToolError, ToolExecutor};
111pub use registry::{
112    global_registry, normalize_tool_name, RegistryError, SharedTool, Tool, ToolRegistry,
113};
114pub use result_handler::{
115    execute_sub_actions, handle_tool_result_with_agentic_support, parse_tool_args,
116    parse_tool_args_best_effort, send_clarification_request, try_parse_agentic_result,
117    ToolHandlingOutcome, MAX_SUB_ACTIONS,
118};
119pub use smart_code_review::SmartCodeReviewTool;
120pub use types::{FunctionCall, FunctionSchema, ToolCall, ToolResult, ToolSchema};
121
122/// Classification of a tool call for approval purposes.
123#[derive(Debug, Clone, Copy, PartialEq, Eq)]
124pub enum ToolMutability {
125    ReadOnly,
126    Mutating,
127}
128
129/// Read-only tools that don't require user approval.
130const READ_ONLY_TOOLS: &[&str] = &[
131    "Read",
132    "GetFileInfo",
133    "Glob",
134    "Grep",
135    "WebFetch",
136    "WebSearch",
137    "Workspace",
138    "BashOutput",
139    "session_note",
140    "memory_note",
141    "session_history",
142    "recall",
143    "session_inspector",
144    "compact_context",
145    "Sleep",
146];
147
148/// Classify a tool call as read-only or mutating.
149pub fn classify_tool(tool_name: &str) -> ToolMutability {
150    if READ_ONLY_TOOLS
151        .iter()
152        .any(|&t| t.eq_ignore_ascii_case(tool_name))
153    {
154        ToolMutability::ReadOnly
155    } else {
156        ToolMutability::Mutating
157    }
158}
159
160#[cfg(test)]
161mod tests {
162    use super::*;
163
164    #[test]
165    fn classify_compact_context_as_read_only() {
166        assert_eq!(classify_tool("compact_context"), ToolMutability::ReadOnly);
167    }
168
169    #[test]
170    fn classify_compact_context_case_insensitive() {
171        assert_eq!(classify_tool("Compact_Context"), ToolMutability::ReadOnly);
172        assert_eq!(classify_tool("COMPACT_CONTEXT"), ToolMutability::ReadOnly);
173    }
174
175    #[test]
176    fn classify_write_as_mutating() {
177        assert_eq!(classify_tool("Write"), ToolMutability::Mutating);
178    }
179
180    #[test]
181    fn classify_unknown_as_mutating() {
182        assert_eq!(
183            classify_tool("totally_unknown_tool"),
184            ToolMutability::Mutating
185        );
186    }
187
188    #[test]
189    fn classify_all_read_only_tools() {
190        for name in READ_ONLY_TOOLS {
191            assert_eq!(
192                classify_tool(name),
193                ToolMutability::ReadOnly,
194                "{name} should be classified as read-only"
195            );
196        }
197    }
198}