summaryrefslogtreecommitdiffstats
path: root/src/tools/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/mod.rs')
-rw-r--r--src/tools/mod.rs148
1 files changed, 148 insertions, 0 deletions
diff --git a/src/tools/mod.rs b/src/tools/mod.rs
new file mode 100644
index 0000000..503de08
--- /dev/null
+++ b/src/tools/mod.rs
@@ -0,0 +1,148 @@
1pub mod fs;
2pub mod shell;
3
4use serde_json::{json, Value};
5
6use fs::Change;
7
8/// When a tool needs a human before it runs.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum Approval {
11 /// Read-only or harmless.
12 Never,
13 /// Prompts on first use per session, then remembers.
14 Once,
15 /// Prompts on every call, no memory.
16 EveryCall,
17}
18
19pub fn approval(name: &str) -> Approval {
20 match name {
21 "write" | "edit" => Approval::Once,
22 "bash" => Approval::EveryCall,
23 _ => Approval::Never,
24 }
25}
26
27/// Tools that execute immediately.
28pub fn run(name: &str, args: &Value) -> Result<String, String> {
29 match name {
30 "read" => fs::read(args),
31 "grep" => fs::grep(args),
32 "glob" => fs::glob(args),
33 "bash" => shell::bash(args, std::time::Duration::from_secs(120)),
34 other => Err(unknown(other)),
35 }
36}
37
38/// Tools that produce a change needing approval before it is applied.
39pub fn plan(name: &str, args: &Value) -> Result<Change, String> {
40 match name {
41 "write" => fs::plan_write(args),
42 "edit" => fs::plan_edit(args),
43 other => Err(unknown(other)),
44 }
45}
46
47fn unknown(name: &str) -> String {
48 format!("unknown tool `{name}`; available: read, write, edit, bash, grep, glob")
49}
50
51/// Static and in a fixed order, so the serialised prefix never changes.
52pub fn schemas() -> Vec<Value> {
53 vec![
54 json!({
55 "type": "function",
56 "function": {
57 "name": "read",
58 "description": "Read a text file. Returns numbered lines. Read before editing.",
59 "parameters": {
60 "type": "object",
61 "properties": {
62 "path": { "type": "string", "description": "file path" },
63 "offset": { "type": "integer", "description": "first line, 1-based (default 1)" },
64 "limit": { "type": "integer", "description": "maximum lines (default and cap 2000)" }
65 },
66 "required": ["path"]
67 }
68 }
69 }),
70 json!({
71 "type": "function",
72 "function": {
73 "name": "write",
74 "description": "Create or replace a whole file. The parent directory must already exist.",
75 "parameters": {
76 "type": "object",
77 "properties": {
78 "path": { "type": "string" },
79 "content": { "type": "string" }
80 },
81 "required": ["path", "content"]
82 }
83 }
84 }),
85 json!({
86 "type": "function",
87 "function": {
88 "name": "edit",
89 "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.",
90 "parameters": {
91 "type": "object",
92 "properties": {
93 "path": { "type": "string" },
94 "old": { "type": "string", "description": "exact existing text, including indentation" },
95 "new": { "type": "string", "description": "replacement text" }
96 },
97 "required": ["path", "old", "new"]
98 }
99 }
100 }),
101 json!({
102 "type": "function",
103 "function": {
104 "name": "bash",
105 "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.",
106 "parameters": {
107 "type": "object",
108 "properties": {
109 "cmd": { "type": "string" },
110 "cwd": { "type": "string", "description": "working directory (default: project root)" }
111 },
112 "required": ["cmd"]
113 }
114 }
115 }),
116 json!({
117 "type": "function",
118 "function": {
119 "name": "grep",
120 "description": "Search file contents for a literal string, recursively. Skips .git, target, node_modules and similar.",
121 "parameters": {
122 "type": "object",
123 "properties": {
124 "pattern": { "type": "string" },
125 "path": { "type": "string", "description": "file or directory (default .)" },
126 "ignore_case": { "type": "boolean" }
127 },
128 "required": ["pattern"]
129 }
130 }
131 }),
132 json!({
133 "type": "function",
134 "function": {
135 "name": "glob",
136 "description": "List files matching a glob. Supports *, ? and ** across path segments, e.g. **/*.rs.",
137 "parameters": {
138 "type": "object",
139 "properties": {
140 "pattern": { "type": "string" },
141 "path": { "type": "string", "description": "directory to search (default .)" }
142 },
143 "required": ["pattern"]
144 }
145 }
146 }),
147 ]
148}