capri-tools: add receipts module

This commit is contained in:
nocode committed 2026-11-11 20:20:48 +01:00
1 parent a2b97a3455
commit c140b2af6b
1 file changed
+90
+90
View File
@@ -0,0 +1,90 @@
//! Factual action receipts (Capri `action-replies.mjs`). English keys; the app translates them.
use serde_json::Value;
/// The fixed sentence for a successful mutation, or `None`. `result` is what the model received
/// (for `spotify_control`, the `model_result` projection; Capri's raw `data.playing` maps to `playback`).
pub fn factual_action_reply(tool: &str, args: &Value, result: &Value) -> Option<String> {
let action = args["action"].as_str().unwrap_or("");
if tool == "desktop_control" && action == "browser_input" && (result["typed"] != true || result["submitted"] != false) {
return None;
}
if result["ok"] != true {
return None;
}
if tool == "application_control" && action == "calculate" {
let value = result["displayResult"].as_str().or(result["result"].as_str())?;
return Some(match result["unit"].as_str() {
Some(unit) => format!("Result: {value} {unit}"),
None => format!("Result: {value}"),
});
}
if tool == "spotify_control" {
let data = &result["data"];
if action == "volume" {
return data["volumePercent"].as_f64().map(|v| format!("Spotify volume: {v}%."));
}
let playing = data["playing"].as_bool().or_else(|| data["playback"].as_str().map(|p| p == "playing"));
return match (["pause", "resume", "toggle", "play", "next", "previous"].contains(&action), playing) {
(true, Some(true)) => Some("Resumed.".into()),
(true, Some(false)) => Some("Paused.".into()),
_ => None,
};
}
if result["cancelled"] == true || result["needsWindow"] == true {
return None;
}
let mutation = match tool {
"filesystem_control" => ["write_text", "replace_text", "create_directory", "copy", "move", "undo", "trash"].contains(&action),
"time_control" => ["remind", "timer", "stopwatch", "pause", "resume", "reset", "cancel", "snooze"].contains(&action),
"desktop_control" => ["type_text", "open_url", "trash", "browser_input"].contains(&action),
_ => false,
};
if !mutation {
return None;
}
Some(
match action {
"write_text" => "Created the text file.",
"replace_text" => "Replaced the existing file.",
"create_directory" => "Created the folder.",
"copy" => "Copied the file.",
"move" => "Moved the file.",
"undo" => "Undid the file action.",
"trash" => "Moved the file to the Trash.",
"stopwatch" => "Stopwatch started.",
"pause" => "Paused.",
"resume" => "Resumed.",
"reset" => "Stopwatch reset.",
"cancel" => "Cancelled.",
"remind" => "Reminder set.",
"timer" => "Timer started.",
"snooze" => "Reminder snoozed.",
"open_url" => "Opened the address in the browser.",
_ => "Entered the text without sending it.",
}
.into(),
)
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn receipts() {
let r = |tool: &str, args: Value, result: Value| factual_action_reply(tool, &args, &result);
assert_eq!(r("application_control", json!({"action":"calculate"}), json!({"ok":true,"result":"212","unit":"degF"})).unwrap(), "Result: 212 degF");
assert_eq!(r("spotify_control", json!({"action":"volume"}), json!({"ok":true,"data":{"volumePercent":35}})).unwrap(), "Spotify volume: 35%.");
assert_eq!(r("spotify_control", json!({"action":"pause"}), json!({"ok":true,"data":{"playback":"paused"}})).unwrap(), "Paused.");
assert_eq!(r("spotify_control", json!({"action":"play","query":"x"}), json!({"ok":true,"data":{"app":"Spotify"}})), None);
assert_eq!(r("filesystem_control", json!({"action":"trash"}), json!({"ok":true})).unwrap(), "Moved the file to the Trash.");
assert_eq!(r("filesystem_control", json!({"action":"trash"}), json!({"ok":true,"cancelled":true})), None);
assert_eq!(r("filesystem_control", json!({"action":"read_text"}), json!({"ok":true})), None);
assert_eq!(r("time_control", json!({"action":"remind"}), json!({"ok":true})).unwrap(), "Reminder set.");
assert_eq!(r("time_control", json!({"action":"remind"}), json!({"ok":false})), None);
assert_eq!(r("desktop_control", json!({"action":"launch"}), json!({"ok":true})), None);
assert_eq!(r("desktop_control", json!({"action":"browser_input"}), json!({"ok":true,"typed":true,"submitted":false})).unwrap(), "Entered the text without sending it.");
assert_eq!(r("desktop_control", json!({"action":"browser_input"}), json!({"ok":true,"typed":true})), None);
}
}