Skip to main content

matrixcode_core/command/handlers/
save.rs

1//! /save 命令实现
2//!
3//! 保存当前会话。
4
5use super::super::backend_context::BackendContext;
6use super::super::command_trait::Command;
7
8/// Save 命令
9///
10/// 用法:
11/// - /save - 保存当前会话
12/// - /save <name> - 保存并重命名会话
13pub struct Save;
14
15impl Command for Save {
16    fn name(&self) -> &'static str {
17        "save"
18    }
19
20    fn help(&self) -> Option<&'static str> {
21        Some("保存当前会话。用法: /save [name]")
22    }
23
24    fn execute<'a>(
25        &'a self,
26        ctx: &'a mut BackendContext<'_>,
27    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = bool> + Send + 'a>> {
28        Box::pin(async move {
29            let parts: Vec<&str> = ctx.message.split_whitespace().collect();
30            let name = parts.get(1).copied();
31
32            if let Some(mgr) = ctx.session_mgr {
33                let messages = ctx.agent.get_messages();
34                mgr.set_messages(messages.to_vec());
35
36                if let Some(n) = name {
37                    if let Err(e) = mgr.rename_current(n) {
38                        let _ = ctx
39                            .event_tx
40                            .send(crate::AgentEvent::error(
41                                format!("Failed to rename: {}", e),
42                                None,
43                                None,
44                            ))
45                            .await;
46                    }
47                }
48
49                if let Err(e) = mgr.save_current() {
50                    let _ = ctx
51                        .event_tx
52                        .send(crate::AgentEvent::error(
53                            format!("Failed to save: {}", e),
54                            None,
55                            None,
56                        ))
57                        .await;
58                } else {
59                    let _ = ctx
60                        .event_tx
61                        .send(crate::AgentEvent::progress("✓ Session saved", None))
62                        .await;
63                }
64            } else {
65                let _ = ctx
66                    .event_tx
67                    .send(crate::AgentEvent::progress(
68                        "❌ Session manager not available",
69                        None,
70                    ))
71                    .await;
72            }
73
74            false // 不转发给 agent
75        })
76    }
77}