matrixcode_core/command/handlers/
memory.rs1use std::future::Future;
4use std::pin::Pin;
5
6use crate::command::{Command, BackendContext};
7
8pub struct Memory;
9
10impl Command for Memory {
11 fn name(&self) -> &'static str {
12 "memory"
13 }
14
15 fn help(&self) -> Option<&'static str> {
16 Some("管理记忆存储")
17 }
18
19 fn execute<'a>(&'a self, ctx: &'a mut BackendContext<'_>)
20 -> Pin<Box<dyn Future<Output = bool> + Send + 'a>>
21 {
22 Box::pin(async move {
23 let msg = ctx.message.trim();
24
25 if msg == "/memory" || msg == "/memory status" {
27 self.show_status(ctx).await
28 } else if msg.starts_with("/memory search ") {
29 let query = msg.strip_prefix("/memory search ").unwrap_or("");
30 self.search(ctx, query).await
31 } else if msg == "/memory clear" {
32 self.clear(ctx).await
33 } else {
34 let _ = ctx.event_tx.send(crate::AgentEvent::progress(
35 "用法:/memory [status|search <query>|clear]".to_string(),
36 None,
37 )).await;
38 false
39 }
40 })
41 }
42}
43
44impl Memory {
45 async fn show_status(&self, ctx: &mut BackendContext<'_>) -> bool {
46 let mut info = "🧠 记忆状态:\n\n".to_string();
47
48 if let Some(_storage) = &ctx.memory_storage {
49 info.push_str(&format!("记忆存储已初始化\n"));
51 } else {
53 info.push_str("记忆存储未初始化\n");
54 }
55
56 let _ = ctx.event_tx.send(crate::AgentEvent::progress(info, None)).await;
57 false
58 }
59
60 async fn search(&self, ctx: &mut BackendContext<'_>, query: &str) -> bool {
61 if query.is_empty() {
62 let _ = ctx.event_tx.send(crate::AgentEvent::progress(
63 "请提供搜索关键词".to_string(),
64 None,
65 )).await;
66 return false;
67 }
68
69 let mut info = format!("🔍 搜索记忆:\"{}\"\n\n", query);
70
71 if let Some(_storage) = &ctx.memory_storage {
72 info.push_str("搜索功能待实现\n");
74 } else {
75 info.push_str("记忆存储未初始化\n");
76 }
77
78 let _ = ctx.event_tx.send(crate::AgentEvent::progress(info, None)).await;
79 false
80 }
81
82 async fn clear(&self, ctx: &mut BackendContext<'_>) -> bool {
83 if let Some(_storage) = ctx.memory_storage {
84 let _ = ctx.event_tx.send(crate::AgentEvent::progress(
86 "记忆已清空".to_string(),
87 None,
88 )).await;
89 } else {
90 let _ = ctx.event_tx.send(crate::AgentEvent::progress(
91 "记忆存储未初始化".to_string(),
92 None,
93 )).await;
94 }
95 false
96 }
97}