aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/tools/mod.rs58
1 files changed, 32 insertions, 26 deletions
diff --git a/src/tools/mod.rs b/src/tools/mod.rs
index 27840e9..7c6a921 100644
--- a/src/tools/mod.rs
+++ b/src/tools/mod.rs
@@ -24,13 +24,6 @@ pub fn approval(name: &str) -> Approval {
24 } 24 }
25} 25}
26 26
27/// True when the call cannot be replayed as a real tool call at all: no name.
28/// (Named calls with no arguments are kept so the model can be told what went
29/// wrong and retry.)
30pub fn is_placeholder(arguments: &str) -> bool {
31 arguments.trim().is_empty()
32}
33
34/// Returns the keys the model actually sent, quoted and bounded. 27/// Returns the keys the model actually sent, quoted and bounded.
35fn received_keys(args: &Value) -> String { 28fn received_keys(args: &Value) -> String {
36 let Some(object) = args.as_object() else { 29 let Some(object) = args.as_object() else {
@@ -55,32 +48,45 @@ fn received_keys(args: &Value) -> String {
55/// arrived with no arguments at all (usually argument loss in transit). They 48/// 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. 49/// used to share one message, which pointed callers at the wrong fix.
57pub fn validate(name: &str, args: &Value) -> Result<(), String> { 50pub fn validate(name: &str, args: &Value) -> Result<(), String> {
58 let required = |key: &str| match args.get(key).and_then(|value| value.as_str()) { 51 let missing = |key: &str| {
52 if args.as_object().map(|o| o.is_empty()).unwrap_or(true) {
53 format!(
54 "the call arrived with no arguments at all, so `{key}` is missing. \
55 If you sent one, it was lost in transit — retry the call"
56 )
57 } else {
58 format!(
59 "missing `{key}`; received {}. The parameter is `{key}`",
60 received_keys(args)
61 )
62 }
63 };
64 // Must be present and non-blank.
65 let required_text = |key: &str| match args.get(key).and_then(|value| value.as_str()) {
59 Some(value) if !value.trim().is_empty() => Ok(()), 66 Some(value) if !value.trim().is_empty() => Ok(()),
60 Some(_) => Err(format!("`{key}` is present but empty")), 67 Some(_) => Err(format!("`{key}` is present but empty")),
61 None => { 68 None => Err(missing(key)),
62 if args.as_object().map(|o| o.is_empty()).unwrap_or(true) { 69 };
63 Err(format!( 70 // Must be present, but may be the empty string (an empty file, a deletion).
64 "the call arrived with no arguments at all, so `{key}` is missing. \ 71 let required_string = |key: &str| match args.get(key) {
65 If you sent one, it was lost in transit — retry the call" 72 Some(Value::String(_)) => Ok(()),
66 )) 73 Some(_) => Err(format!("`{key}` must be a string")),
67 } else { 74 None => Err(missing(key)),
68 Err(format!(
69 "missing `{key}`; received {}. The parameter is `{key}`",
70 received_keys(args)
71 ))
72 }
73 }
74 }; 75 };
75 76
76 match name { 77 match name {
77 "bash" => required("cmd"), 78 "bash" => required_text("cmd"),
78 "read" | "write" => required("path"), 79 "read" => required_text("path"),
80 "write" => {
81 required_text("path")?;
82 required_string("content")
83 }
79 "edit" => { 84 "edit" => {
80 required("path")?; 85 required_text("path")?;
81 required("old") 86 required_text("old")?;
87 required_string("new")
82 } 88 }
83 "grep" | "glob" => required("pattern"), 89 "grep" | "glob" => required_text("pattern"),
84 "" => Err("the tool call arrived without a tool name; it was probably truncated in transit — retry the call".into()), 90 "" => Err("the tool call arrived without a tool name; it was probably truncated in transit — retry the call".into()),
85 _ => Ok(()), 91 _ => Ok(()),
86 } 92 }