Skip to main content

matrixcode_core/command/handlers/
memory.rs

1//! /memory 命令处理器
2
3use std::future::Future;
4use std::pin::Pin;
5
6use crate::command::{BackendContext, Command};
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>(
20        &'a self,
21        ctx: &'a mut BackendContext<'_>,
22    ) -> Pin<Box<dyn Future<Output = bool> + Send + 'a>> {
23        Box::pin(async move {
24            let msg = ctx.message.trim();
25
26            // 解析子命令
27            if msg == "/memory" || msg == "/memory status" {
28                self.show_status(ctx).await
29            } else if msg.starts_with("/memory search ") {
30                let query = msg.strip_prefix("/memory search ").unwrap_or("");
31                self.search(ctx, query).await
32            } else if msg == "/memory clear" {
33                self.clear(ctx).await
34            } else {
35                let _ = ctx
36                    .event_tx
37                    .send(crate::AgentEvent::progress(
38                        "用法:/memory [status|search <query>|clear]".to_string(),
39                        None,
40                    ))
41                    .await;
42                false
43            }
44        })
45    }
46}
47
48impl Memory {
49    async fn show_status(&self, ctx: &mut BackendContext<'_>) -> bool {
50        let mut info = "🧠 记忆状态:\n\n".to_string();
51
52        if let Some(_storage) = &ctx.memory_storage {
53            // 显示记忆统计
54            info.push_str(&format!("记忆存储已初始化\n"));
55            // TODO: 添加更多统计信息
56        } else {
57            info.push_str("记忆存储未初始化\n");
58        }
59
60        let _ = ctx
61            .event_tx
62            .send(crate::AgentEvent::progress(info, None))
63            .await;
64        false
65    }
66
67    async fn search(&self, ctx: &mut BackendContext<'_>, query: &str) -> bool {
68        if query.is_empty() {
69            let _ = ctx
70                .event_tx
71                .send(crate::AgentEvent::progress(
72                    "请提供搜索关键词".to_string(),
73                    None,
74                ))
75                .await;
76            return false;
77        }
78
79        let mut info = format!("🔍 搜索记忆:\"{}\"\n\n", query);
80
81        if let Some(_storage) = &ctx.memory_storage {
82            // TODO: 实现记忆搜索
83            info.push_str("搜索功能待实现\n");
84        } else {
85            info.push_str("记忆存储未初始化\n");
86        }
87
88        let _ = ctx
89            .event_tx
90            .send(crate::AgentEvent::progress(info, None))
91            .await;
92        false
93    }
94
95    async fn clear(&self, ctx: &mut BackendContext<'_>) -> bool {
96        if let Some(_storage) = ctx.memory_storage {
97            // TODO: 实现清空记忆
98            let _ = ctx
99                .event_tx
100                .send(crate::AgentEvent::progress("记忆已清空".to_string(), None))
101                .await;
102        } else {
103            let _ = ctx
104                .event_tx
105                .send(crate::AgentEvent::progress(
106                    "记忆存储未初始化".to_string(),
107                    None,
108                ))
109                .await;
110        }
111        false
112    }
113}