Skip to main content

agent_engine/tools/
mod.rs

1//! Tool system — trait, registry, and built-in tool implementations.
2//!
3//! All tools implement the `Tool` trait and are registered in `ToolRegistry`.
4//! Subagents get `ToolRegistry::without_subagent()` to prevent recursion.
5use crate::Result;
6use serde_json::Value;
7use std::path::PathBuf;
8use std::sync::{Arc, Mutex};
9
10// ── Module declarations ──────────────────────────────────────────────────────────
11
12mod bash;
13mod edit;
14mod extension;
15mod find;
16mod grep;
17mod ls;
18mod read;
19mod secret_prompt;
20mod subagent;
21mod write;
22
23mod agent;
24mod registry;
25pub mod respond;
26pub mod send_channel;
27pub mod shell;
28pub(crate) mod util;
29pub mod watcher_exit;
30
31// ── Re-exports ──────────────────────────────────────────────────────────────────
32
33pub use crate::runtime::subagent::{
34    SubagentHandle, SubagentRegistry, SubagentResult, SubagentState, SubagentStatus,
35};
36pub use agent::resolve_agent_prompt;
37pub use bash::BashTool;
38pub use edit::EditTool;
39pub use extension::ExtensionTool;
40pub use find::FindTool;
41pub use grep::GrepTool;
42pub use ls::LsTool;
43pub use read::ReadTool;
44pub use registry::ToolRegistry;
45pub use respond::RespondTool;
46pub use secret_prompt::SecretPromptQueue;
47pub use secret_prompt::{SecretPromptHandle, SecretPromptRequest};
48pub use send_channel::SendChannelTool;
49pub use shell::{ShellEndTool, ShellSendTool, ShellStartTool};
50pub use subagent::{
51    SubagentCollectTool, SubagentResumeTool, SubagentStartTool, SubagentStatusTool,
52    SubagentSteerTool, SubagentTool,
53};
54pub use watcher_exit::WatcherExitTool;
55pub use write::WriteTool;
56
57// Re-export util items used by sibling tool modules via `super::`
58pub(crate) use util::{expand_path, strip_ansi, NEXT_SUBAGENT_ID};
59
60// ── Tool Trait ──────────────────────────────────────────────────────────────────
61
62/// Streaming channels — carry partial tool output and stream events.
63pub struct ToolChannels {
64    pub tx_delta: Option<tokio::sync::mpsc::UnboundedSender<String>>,
65    pub tx_events: Option<tokio::sync::mpsc::UnboundedSender<crate::StreamEvent>>,
66}
67
68/// Runtime capability handles — shared services a tool may require.
69pub struct ToolCapabilities {
70    pub watcher_exit_path: Option<PathBuf>,
71    pub tool_register_tx: Option<tokio::sync::mpsc::UnboundedSender<Vec<Arc<dyn Tool>>>>,
72    pub session_manager: Option<std::sync::Arc<crate::tools::shell::SessionManager>>,
73    pub subagent_registry: Option<Arc<Mutex<SubagentRegistry>>>,
74    pub event_queue: Option<Arc<crate::events::EventQueue>>,
75    pub secret_prompt: Option<SecretPromptHandle>,
76}
77
78/// Configuration limits and timeouts.
79pub struct ToolLimits {
80    /// Context-budget ceiling — final tool output entering history is truncated
81    /// to this. Applied AFTER the `after_tool_call` hook so compression
82    /// extensions see the FULL buffered output and can decide what to keep.
83    pub max_tool_output: usize,
84    /// Memory/pipe safety bound — the maximum bytes a tool may buffer
85    /// internally before truncating with a marker. Sized well above
86    /// `max_tool_output` so transform extensions are never starved.
87    pub max_tool_buffer: usize,
88    pub bash_timeout: u64,
89    pub bash_max_timeout: u64,
90    pub subagent_timeout: u64,
91}
92
93/// Context passed to tool execution — composition of channels, capabilities, and limits.
94pub struct ToolContext {
95    pub channels: ToolChannels,
96    pub capabilities: ToolCapabilities,
97    pub limits: ToolLimits,
98}
99
100/// The core trait for all tools. Implement this to add a new tool.
101#[async_trait::async_trait]
102pub trait Tool: Send + Sync {
103    /// Tool name as it appears in the API (e.g. "bash", "read").
104    fn name(&self) -> &str;
105
106    /// Human-readable description sent to the model.
107    fn description(&self) -> &str;
108
109    /// JSON Schema for the tool's parameters.
110    fn parameters(&self) -> Value;
111
112    /// Execute the tool with the given parameters.
113    async fn execute(&self, params: Value, ctx: ToolContext) -> Result<String>;
114
115    /// Owning extension id for tools registered by an extension. Built-in tools return `None`.
116    fn extension_id(&self) -> Option<&str> {
117        None
118    }
119}
120
121#[cfg(test)]
122mod test_helpers;