aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/mod.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-18 12:01:38 +0100
committersalmonik <salmonik@tuta.io>2026-11-18 12:01:38 +0100
commit6df7a6403d88a4492ab6ea1c519dcae3a202be96 (patch)
tree216f5d009881c2bccff122a6779397c502fb228b /src/tools/mod.rs
parent0.8.2: comp.sh defaults to this CPU, --portable for travel (diff)
downloadagent686-6df7a6403d88a4492ab6ea1c519dcae3a202be96.tar.gz
agent686-6df7a6403d88a4492ab6ea1c519dcae3a202be96.zip
0.9.0: distinguishable tool errors, separated stderr, configurable limits
From a field report porting a Neovim config to Helix: - validate() now separates 'empty value', 'wrong key (here is what you sent)' and 'no arguments at all, retry' instead of one false message for all four causes - named calls with no arguments are answered rather than dropped silently - bash keeps stdout and stderr in labelled sections when both produce output - bash gains timeout_seconds (1-600) and max_bytes, and says when output was cut - tool lines/results carry [step.call] labels when a step has several calls - invalid JSON errors include a preview of what arrived
Diffstat (limited to '')
-rw-r--r--src/tools/mod.rs74
1 files changed, 54 insertions, 20 deletions
diff --git a/src/tools/mod.rs b/src/tools/mod.rs
index ddb1004..27840e9 100644
--- a/src/tools/mod.rs
+++ b/src/tools/mod.rs
@@ -24,28 +24,52 @@ pub fn approval(name: &str) -> Approval {
24 } 24 }
25} 25}
26 26
27/// A call with no arguments at all. Every tool here requires at least one, so 27/// True when the call cannot be replayed as a real tool call at all: no name.
28/// this is a placeholder some backends emit next to a real call, not an attempt. 28/// (Named calls with no arguments are kept so the model can be told what went
29/// wrong and retry.)
29pub fn is_placeholder(arguments: &str) -> bool { 30pub fn is_placeholder(arguments: &str) -> bool {
30 let trimmed = arguments.trim(); 31 arguments.trim().is_empty()
31 if trimmed.is_empty() { 32}
32 return true; 33
34/// Returns the keys the model actually sent, quoted and bounded.
35fn received_keys(args: &Value) -> String {
36 let Some(object) = args.as_object() else {
37 return "no arguments".to_string();
38 };
39 if object.is_empty() {
40 return "no arguments".to_string();
33 } 41 }
34 matches!( 42 let mut keys: Vec<String> = object.keys().map(|key| format!("`{key}`")).collect();
35 serde_json::from_str::<Value>(trimmed), 43 keys.sort();
36 Ok(Value::Object(map)) if map.is_empty() 44 if keys.len() > 6 {
37 ) 45 keys.truncate(6);
46 keys.push("…".to_string());
47 }
48 keys.join(", ")
38} 49}
39 50
40/// Rejects a call that cannot possibly run, before a human is asked to approve 51/// Rejects a call that cannot run, before a human is asked to approve it.
41/// it. An empty `cmd` used to mean an approval prompt for `run: `. 52///
53/// The message has to distinguish three different failures: the argument is
54/// empty, the argument is missing because the key is wrong, and the call
55/// arrived with no arguments at all (usually argument loss in transit). They
56/// used to share one message, which pointed callers at the wrong fix.
42pub fn validate(name: &str, args: &Value) -> Result<(), String> { 57pub fn validate(name: &str, args: &Value) -> Result<(), String> {
43 let text = |key: &str| args.get(key).and_then(|value| value.as_str()).unwrap_or(""); 58 let required = |key: &str| match args.get(key).and_then(|value| value.as_str()) {
44 let required = |key: &str| { 59 Some(value) if !value.trim().is_empty() => Ok(()),
45 if text(key).trim().is_empty() { 60 Some(_) => Err(format!("`{key}` is present but empty")),
46 Err(format!("`{key}` is required and must not be empty")) 61 None => {
47 } else { 62 if args.as_object().map(|o| o.is_empty()).unwrap_or(true) {
48 Ok(()) 63 Err(format!(
64 "the call arrived with no arguments at all, so `{key}` is missing. \
65 If you sent one, it was lost in transit — retry the call"
66 ))
67 } else {
68 Err(format!(
69 "missing `{key}`; received {}. The parameter is `{key}`",
70 received_keys(args)
71 ))
72 }
49 } 73 }
50 }; 74 };
51 75
@@ -57,6 +81,7 @@ pub fn validate(name: &str, args: &Value) -> Result<(), String> {
57 required("old") 81 required("old")
58 } 82 }
59 "grep" | "glob" => required("pattern"), 83 "grep" | "glob" => required("pattern"),
84 "" => Err("the tool call arrived without a tool name; it was probably truncated in transit — retry the call".into()),
60 _ => Ok(()), 85 _ => Ok(()),
61 } 86 }
62} 87}
@@ -67,7 +92,14 @@ pub fn run(name: &str, args: &Value, cancel: &std::sync::atomic::AtomicBool) ->
67 "read" => fs::read(args), 92 "read" => fs::read(args),
68 "grep" => fs::grep(args), 93 "grep" => fs::grep(args),
69 "glob" => fs::glob(args), 94 "glob" => fs::glob(args),
70 "bash" => shell::bash(args, std::time::Duration::from_secs(120), cancel), 95 "bash" => {
96 let seconds = args
97 .get("timeout_seconds")
98 .and_then(|value| value.as_u64())
99 .unwrap_or(120)
100 .clamp(1, 600);
101 shell::bash(args, std::time::Duration::from_secs(seconds), cancel)
102 }
71 other => Err(unknown(other)), 103 other => Err(unknown(other)),
72 } 104 }
73} 105}
@@ -139,12 +171,14 @@ pub fn schemas() -> Vec<Value> {
139 "type": "function", 171 "type": "function",
140 "function": { 172 "function": {
141 "name": "bash", 173 "name": "bash",
142 "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.", 174 "description": "Run a shell command. Each call is a fresh shell at the working directory, so `cd` does not persist; pass cwd instead. stdout and stderr are reported in separate labelled sections when both produce output. Times out after timeout_seconds (default 120, maximum 600). Large output is truncated head+tail with an explicit marker; raise max_bytes if the match you need is in the middle.",
143 "parameters": { 175 "parameters": {
144 "type": "object", 176 "type": "object",
145 "properties": { 177 "properties": {
146 "cmd": { "type": "string" }, 178 "cmd": { "type": "string" },
147 "cwd": { "type": "string", "description": "working directory (default: project root)" } 179 "cwd": { "type": "string", "description": "working directory (default: project root)" },
180 "timeout_seconds": { "type": "integer", "description": "1-600, default 120" },
181 "max_bytes": { "type": "integer", "description": "per-stream output budget, 1024-16777216, default 81920" }
148 }, 182 },
149 "required": ["cmd"] 183 "required": ["cmd"]
150 } 184 }