1use super::family::public_params;
11use super::*;
12
13pub(super) const ACTIONS: &[&str] = &["list", "read", "write", "remove", "reach", "history", "diff", "revert"];
14
15pub(super) fn handler(action: &str, scope: &str) -> Result<(&'static str, &'static [(&'static str, &'static str)])> {
19 Ok(match (scope, action) {
20 ("personal", "list") => ("MemoryList", &[]),
21 ("personal", "read") => ("MemoryRead", &[]),
22 ("personal", "write") => ("MemoryWrite", &[]),
23 ("personal", "history") => ("MemoryHistory", &[]),
24 ("personal", "diff") => ("MemoryDiff", &[]),
25 ("personal", "revert") => ("MemoryRevert", &[("version_id", "target_version_id")]),
26 ("personal", "reach") => anyhow::bail!(
27 "Memory: reach is for scope \"global\" (whether an entry goes to the other machines); Personal Memory stays \
28 with you"
29 ),
30 ("personal", "remove") => anyhow::bail!(
31 "Memory: remove is for scope \"global\" (taking an entry out of Global Memory); a Personal Memory file is \
32 rewritten with write"
33 ),
34 ("global", "list") => ("GlobalMemoryList", &[]),
35 ("global", "read") => ("GlobalMemoryRead", &[]),
36 ("global", "write") => ("GlobalMemoryWrite", &[]),
37 ("global", "remove") => ("GlobalMemoryRemove", &[]),
38 ("global", "reach") => ("GlobalMemoryReach", &[]),
39 ("global", "history") => ("GlobalMemoryHistory", &[]),
40 ("global", "diff") => ("GlobalMemoryDiff", &[]),
41 ("global", "revert") => ("GlobalMemoryRevert", &[]),
42 ("personal" | "global", "") => {
43 anyhow::bail!("Memory: missing required parameter: action (one of {})", ACTIONS.join(", "))
44 }
45 ("personal" | "global", other) => {
46 anyhow::bail!("Memory: unknown action {other:?}: use one of {}", ACTIONS.join(", "))
47 }
48 (other, _) => anyhow::bail!("Memory: unknown scope {other:?}: use \"personal\" or \"global\""),
49 })
50}
51
52
53pub(super) async fn call(name: &str, arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
54 match name {
55 "Memory" => {
56 let action = arguments.get("action").and_then(|v| v.as_str()).unwrap_or("");
57 let scope = arguments.get("scope").and_then(|v| v.as_str()).unwrap_or("personal");
58 let (tool, rename) = handler(action, scope)?;
59 let mut args = match arguments {
60 Value::Object(m) => m.clone(),
61 _ => serde_json::Map::new(),
62 };
63 args.remove("action");
64 args.remove("scope");
65 for (ours, theirs) in rename {
66 if let Some(v) = args.remove(*ours) {
67 args.insert((*theirs).to_string(), v);
68 }
69 }
70 let args = Value::Object(args);
71 let result = match scope {
72 "global" => global_memory::call(tool, &args, cx).await,
73 _ => memory::call(tool, &args, cx).await,
74 };
75 result.map_err(|e| anyhow::anyhow!(public_params(&format!("{e:#}"), rename)))
76 }
77 _ => Err(not_in_family()),
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn each_action_and_scope_has_its_handler() {
87 assert_eq!(handler("list", "personal").unwrap().0, "MemoryList");
88 assert_eq!(handler("write", "global").unwrap().0, "GlobalMemoryWrite");
89 assert_eq!(handler("revert", "personal").unwrap(), ("MemoryRevert", &[("version_id", "target_version_id")][..]));
90 assert_eq!(handler("revert", "global").unwrap().0, "GlobalMemoryRevert");
91 for a in ACTIONS {
92 assert!(handler(a, "global").is_ok(), "{a}");
93 }
94 assert!(handler("remove", "personal").unwrap_err().to_string().contains("scope \"global\""));
95 assert_eq!(handler("reach", "global").unwrap().0, "GlobalMemoryReach");
96 assert!(handler("reach", "personal").unwrap_err().to_string().contains("scope \"global\""));
97 assert!(handler("drop", "personal").unwrap_err().to_string().contains("unknown action"));
98 assert!(handler("list", "team").unwrap_err().to_string().contains("unknown scope"));
99 }
100
101 #[test]
102 fn a_missing_parameter_is_named_as_memory_takes_it() {
103 let rename = &[("version_id", "target_version_id")][..];
104 assert_eq!(
105 public_params("missing required parameter: target_version_id", rename),
106 "missing required parameter: version_id"
107 );
108 assert_eq!(public_params("missing required parameter: filename", rename), "missing required parameter: filename");
109 }
110
111 #[test]
112 fn every_action_is_in_the_schema() {
113 let schema: Value = serde_json::from_str(crate::tool_schemas::MEMORY_TOOL).unwrap();
114 let mut listed: Vec<&str> = schema["inputSchema"]["properties"]["action"]["enum"]
115 .as_array()
116 .unwrap()
117 .iter()
118 .filter_map(|v| v.as_str())
119 .collect();
120 let mut want = ACTIONS.to_vec();
121 listed.sort();
122 want.sort();
123 assert_eq!(listed, want);
124 }
125}