Skip to main content

matrixcode_core/command/handlers/
load.rs

1//! /load 命令实现
2//!
3//! 加载指定会话。
4
5use super::super::backend_context::BackendContext;
6use super::super::command_trait::Command;
7
8/// Load 命令
9///
10/// 用法:
11/// - /load <session_id> - 加载指定会话
12pub struct Load;
13
14impl Command for Load {
15    fn name(&self) -> &'static str {
16        "load"
17    }
18
19    fn help(&self) -> Option<&'static str> {
20        Some("加载指定会话。用法: /load <session_id>")
21    }
22
23    fn execute<'a>(
24        &'a self,
25        ctx: &'a mut BackendContext<'_>,
26    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = bool> + Send + 'a>> {
27        Box::pin(async move {
28            let session_id = ctx.message.strip_prefix("/load ").unwrap_or("");
29
30            if let Some(mgr) = ctx.session_mgr {
31                if mgr.resume(session_id).is_ok() {
32                    if let Some(msgs) = mgr.messages() {
33                        let messages = msgs.to_vec();
34                        ctx.agent.set_messages(messages.clone());
35                        let _ = ctx
36                            .event_tx
37                            .send(crate::AgentEvent::progress(
38                                format!(
39                                    "✓ Session '{}' loaded ({} messages)",
40                                    session_id,
41                                    messages.len()
42                                ),
43                                None,
44                            ))
45                            .await;
46                    }
47                } else {
48                    let _ = ctx
49                        .event_tx
50                        .send(crate::AgentEvent::progress(
51                            format!("❌ Session '{}' not found", session_id),
52                            None,
53                        ))
54                        .await;
55                }
56            } else {
57                let _ = ctx
58                    .event_tx
59                    .send(crate::AgentEvent::progress(
60                        "❌ Session manager not available",
61                        None,
62                    ))
63                    .await;
64            }
65
66            false // 不转发给 agent
67        })
68    }
69}