Skip to main content

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