Skip to main content

matrixcode_core/command/handlers/
save.rs

1//! /save 命令实现
2//!
3//! 保存当前会话。
4
5use super::super::command_trait::Command;
6use super::super::backend_context::BackendContext;
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>(&'a self, ctx: &'a mut BackendContext<'_>) 
25        -> std::pin::Pin<Box<dyn std::future::Future<Output = bool> + Send + 'a>> 
26    {
27        Box::pin(async move {
28            let parts: Vec<&str> = ctx.message.split_whitespace().collect();
29            let name = parts.get(1).copied();
30
31            if let Some(mgr) = ctx.session_mgr {
32                let messages = ctx.agent.get_messages();
33                mgr.set_messages(messages.to_vec());
34                
35                if let Some(n) = name {
36                    if let Err(e) = mgr.rename_current(n) {
37                        let _ = ctx.event_tx.send(crate::AgentEvent::error(
38                            format!("Failed to rename: {}", e), None, None
39                        )).await;
40                    }
41                }
42                
43                if let Err(e) = mgr.save_current() {
44                    let _ = ctx.event_tx.send(crate::AgentEvent::error(
45                        format!("Failed to save: {}", e), None, None
46                    )).await;
47                } else {
48                    let _ = ctx.event_tx.send(crate::AgentEvent::progress(
49                        "✓ Session saved", None
50                    )).await;
51                }
52            } else {
53                let _ = ctx.event_tx.send(crate::AgentEvent::progress(
54                    "❌ Session manager not available", None
55                )).await;
56            }
57
58            false // 不转发给 agent
59        })
60    }
61}