Skip to main content

aidaemon_tools/
lib.rs

1//! # aidaemon-tools (experimental)
2//!
3//! A small default tool set for [`aidaemon_core`] agents, built on lessons
4//! from agent-computer interfaces (SWE-agent, Claude Code, Aider): few
5//! non-overlapping tools, bounded output, actionable errors, and validation
6//! before mutation.
7//!
8//! | Tool | Name | Annotations |
9//! |---|---|---|
10//! | [`ReadFile`] | `read_file` | read-only |
11//! | [`WriteFile`] | `write_file` | write (idempotent) |
12//! | [`EditFile`] | `edit_file` | write |
13//! | [`Glob`] | `glob` | read-only |
14//! | [`Grep`] | `grep` | read-only |
15//! | [`Shell`] | `shell` | destructive |
16//! | [`WebFetch`] | `web_fetch` | read-only |
17//! | [`Checklist`] | `track_requirements` | write (agent state only) |
18//!
19//! Every file tool is confined to a [`Workspace`]. The shell runs through a
20//! [`Sandbox`]; containment (sandboxing) lives there, while approval
21//! stays a policy concern for [`aidaemon_core::Hook`]s. No tool contains
22//! approval logic.
23//!
24//! ```no_run
25//! use aidaemon_tools::{default_tools, Checklist, Workspace};
26//! # fn demo(provider: impl aidaemon_core::Provider + 'static) -> Result<(), Box<dyn std::error::Error>> {
27//! let workspace = Workspace::new("./my-project")?;
28//! let (checklist, gate) = Checklist::with_gate();
29//! let mut builder = aidaemon_core::Agent::builder("coder").allow_unguarded()
30//!     .model("some-model")
31//!     .provider(provider)
32//!     .tool(checklist)
33//!     .hook(gate);
34//! for tool in default_tools(workspace) {
35//!     builder = builder.tool_arc(tool);
36//! }
37//! let agent = builder.build()?;
38//! # let _ = agent; Ok(()) }
39//! ```
40
41use std::sync::Arc;
42
43use aidaemon_core::Tool;
44
45pub mod backend;
46pub mod background_deliverable;
47pub mod backoff;
48pub mod check_environment;
49mod checklist;
50#[cfg(feature = "checkpoints")]
51pub mod checkpoints;
52pub mod cli_agent_flags;
53pub mod command_effects;
54pub mod command_risk;
55pub mod config_file;
56pub mod daemon_guard;
57pub mod duration_format;
58mod exec;
59pub mod execution;
60pub mod file_delivery;
61mod fs;
62pub mod fs_utils;
63pub mod http_request;
64pub mod internal_markers;
65pub mod local_backend;
66pub mod local_sites;
67pub mod outcome;
68pub mod owner_answers;
69pub mod path_class;
70pub mod permission_bypass;
71pub mod platform_shell;
72pub mod process_control;
73pub mod result_spill;
74pub mod sanitize;
75mod search;
76mod search_files;
77pub mod semantics;
78pub mod services;
79mod shell;
80pub mod shown;
81pub mod status;
82pub mod stt_setup;
83pub mod system;
84#[cfg(any(test, feature = "testing"))]
85pub mod testing;
86pub mod text;
87pub mod tls;
88mod util;
89pub mod web;
90pub mod web_search;
91mod workspace;
92
93#[cfg(test)]
94mod testutil;
95
96pub use backend::{
97    BackendDirEntry, BackendFileType, BackendKind, BackendMetadata, BackendPath, CommandSpec,
98    ExecutionBackend, ExecutionOutput, ExecutionRequest, LexicalPaths, ProcessHandle,
99    SpawnedProcess, WriteMode, shell_quote,
100};
101pub use checklist::{Checklist, ChecklistGate, ChecklistItem, ChecklistState, ItemStatus};
102pub use exec::{
103    BubblewrapSandbox, BubblewrapScope, CommandOutput, DockerSandbox, ExecError, HOME_SECRET_PATHS,
104    LocalSandbox, SANDBOX_ENV, Sandbox, SandboxCommand, SandboxKind, SandboxSelection,
105    SeatbeltSandbox, TerminalConfinement, best_available_sandbox, default_deny_read,
106    home_secret_paths, sandbox_preference_from_env, select_sandbox,
107};
108pub use fs::{
109    DEFAULT_IGNORE_DIRS, EditFile, FilePolicy, HostFilePolicy, HostFilePolicySettings,
110    MAX_READ_CHARS, ProtectedPathGuard, ReadFile, ToolText, WorkspaceBackend, WorkspaceFilePolicy,
111    WriteFile, format_with_line_numbers, render_read_file_output, render_read_file_output_within,
112};
113pub use outcome::{
114    CallValidator, KernelAdapter, NothingSentError, OutcomeRunner, OutcomeTool, TargetNotFound,
115    ToolTimedOut, failed_without_effect, timed_out,
116};
117pub use path_class::PathClassifier;
118pub use search::{Glob, Grep};
119pub use search_files::SearchFiles;
120pub use shell::{DEFAULT_ENV_ALLOWLIST, Shell};
121pub use util::sniff_image_mime;
122pub use web::{NetworkGuard, WebFetch};
123pub use workspace::{PathError, Workspace};
124
125/// The standard set: `read_file`, `write_file`, `edit_file`, `glob`, `grep`,
126/// `shell` (with [`best_available_sandbox`]: bubblewrap on Linux, seatbelt
127/// on macOS) and `web_fetch`.
128///
129/// `track_requirements` is not included because it is only useful together
130/// with its stop hook; add both with [`Checklist::with_gate`].
131pub fn default_tools(workspace: Workspace) -> Vec<Arc<dyn Tool>> {
132    default_tools_with_sandbox(workspace, best_available_sandbox())
133}
134
135/// [`default_tools`] with the shell on an explicit backend, for example
136/// `SandboxKind::Local.backend()` to run commands on the host on purpose.
137pub fn default_tools_with_sandbox(
138    workspace: Workspace,
139    sandbox: Arc<dyn Sandbox>,
140) -> Vec<Arc<dyn Tool>> {
141    vec![
142        Arc::new(ReadFile::new(workspace.clone())),
143        Arc::new(WriteFile::new(workspace.clone())),
144        Arc::new(EditFile::new(workspace.clone())),
145        Arc::new(Glob::new(workspace.clone())),
146        Arc::new(Grep::new(workspace.clone())),
147        Arc::new(Shell::new(workspace, sandbox)),
148        Arc::new(WebFetch::new()),
149    ]
150}
151
152#[cfg(test)]
153mod tests {
154    use super::*;
155
156    #[test]
157    fn default_set_has_strict_unique_specs() {
158        let (_d, ws) = testutil::ws();
159        let tools = default_tools(ws);
160        let mut names: Vec<String> = tools.iter().map(|t| t.spec().name).collect();
161        names.push(Checklist::default().spec().name);
162        let mut sorted = names.clone();
163        sorted.sort();
164        sorted.dedup();
165        assert_eq!(sorted.len(), names.len());
166        for tool in tools
167            .iter()
168            .cloned()
169            .chain([Arc::new(Checklist::default()) as Arc<dyn Tool>])
170        {
171            let spec = tool.spec();
172            assert!(!spec.description.is_empty(), "{}", spec.name);
173            assert_eq!(spec.parameters["type"], "object", "{}", spec.name);
174            assert_eq!(
175                spec.parameters["additionalProperties"],
176                serde_json::json!(false),
177                "{}",
178                spec.name
179            );
180            // Every required key is a declared property.
181            for key in spec.parameters["required"].as_array().unwrap() {
182                assert!(
183                    spec.parameters["properties"]
184                        .get(key.as_str().unwrap())
185                        .is_some(),
186                    "{}: {key}",
187                    spec.name
188                );
189            }
190        }
191        let read_only: Vec<bool> = tools.iter().map(|t| t.annotations().read_only).collect();
192        assert_eq!(read_only, [true, false, false, true, true, false, true]);
193    }
194}