pub mod fs; pub mod shell; use serde_json::{json, Value}; use fs::Change; /// When a tool needs a human before it runs. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Approval { /// Read-only or harmless. Never, /// Prompts on first use per session, then remembers. Once, /// Prompts on every call, no memory. EveryCall, } pub fn approval(name: &str) -> Approval { match name { "write" | "edit" => Approval::Once, "bash" => Approval::EveryCall, _ => Approval::Never, } } /// Tools that execute immediately. pub fn run(name: &str, args: &Value) -> Result { match name { "read" => fs::read(args), "grep" => fs::grep(args), "glob" => fs::glob(args), "bash" => shell::bash(args, std::time::Duration::from_secs(120)), other => Err(unknown(other)), } } /// Tools that produce a change needing approval before it is applied. pub fn plan(name: &str, args: &Value) -> Result { match name { "write" => fs::plan_write(args), "edit" => fs::plan_edit(args), other => Err(unknown(other)), } } fn unknown(name: &str) -> String { format!("unknown tool `{name}`; available: read, write, edit, bash, grep, glob") } /// Static and in a fixed order, so the serialised prefix never changes. pub fn schemas() -> Vec { vec![ json!({ "type": "function", "function": { "name": "read", "description": "Read a text file. Returns numbered lines. Read before editing.", "parameters": { "type": "object", "properties": { "path": { "type": "string", "description": "file path" }, "offset": { "type": "integer", "description": "first line, 1-based (default 1)" }, "limit": { "type": "integer", "description": "maximum lines (default and cap 2000)" } }, "required": ["path"] } } }), json!({ "type": "function", "function": { "name": "write", "description": "Create or replace a whole file. The parent directory must already exist.", "parameters": { "type": "object", "properties": { "path": { "type": "string" }, "content": { "type": "string" } }, "required": ["path", "content"] } } }), json!({ "type": "function", "function": { "name": "edit", "description": "Replace one exact occurrence of `old` with `new`. Fails if `old` is missing or appears more than once, so include enough context to be unique.", "parameters": { "type": "object", "properties": { "path": { "type": "string" }, "old": { "type": "string", "description": "exact existing text, including indentation" }, "new": { "type": "string", "description": "replacement text" } }, "required": ["path", "old", "new"] } } }), json!({ "type": "function", "function": { "name": "bash", "description": "Run a shell command. Each call is a fresh shell at the working directory, so `cd` does not persist; pass cwd instead. Times out after 120s. Returns combined output and the exit code.", "parameters": { "type": "object", "properties": { "cmd": { "type": "string" }, "cwd": { "type": "string", "description": "working directory (default: project root)" } }, "required": ["cmd"] } } }), json!({ "type": "function", "function": { "name": "grep", "description": "Search file contents recursively with a regular expression. Skips .git, target, node_modules and similar. Set fixed=true to match literal text instead.", "parameters": { "type": "object", "properties": { "pattern": { "type": "string", "description": "regular expression, or literal text when fixed=true" }, "path": { "type": "string", "description": "file or directory (default .)" }, "ignore_case": { "type": "boolean" }, "fixed": { "type": "boolean", "description": "treat pattern as literal text" } }, "required": ["pattern"] } } }), json!({ "type": "function", "function": { "name": "glob", "description": "List files matching a glob. Supports *, ? and ** across path segments, e.g. **/*.rs.", "parameters": { "type": "object", "properties": { "pattern": { "type": "string" }, "path": { "type": "string", "description": "directory to search (default .)" } }, "required": ["pattern"] } } }), ] }