matrixcode_core/command/handlers/
sessions.rs1use super::super::backend_context::BackendContext;
6use super::super::command_trait::Command;
7
8pub struct Sessions;
16
17impl Command for Sessions {
18 fn name(&self) -> &'static str {
19 "sessions"
20 }
21
22 fn aliases(&self) -> &[&'static str] {
23 &["resume"]
24 }
25
26 fn help(&self) -> Option<&'static str> {
27 Some("列出和管理会话。用法: /sessions [cleanup|stats]")
28 }
29
30 fn execute<'a>(
31 &'a self,
32 ctx: &'a mut BackendContext<'_>,
33 ) -> std::pin::Pin<Box<dyn std::future::Future<Output = bool> + Send + 'a>> {
34 Box::pin(async move {
35 const SESSION_CLEANUP_DAYS: u64 = 30;
36 const DISPLAY_SESSIONS_LIMIT: usize = 20;
37
38 let subcmd = if ctx.message.starts_with("/sessions ") {
39 ctx.message.strip_prefix("/sessions ").unwrap_or("")
40 } else {
41 ""
42 };
43
44 if let Some(mgr) = ctx.session_mgr {
45 if subcmd == "cleanup" {
46 let removed = mgr.cleanup_old_sessions(SESSION_CLEANUP_DAYS).unwrap_or(0);
47 let _ = ctx
48 .event_tx
49 .send(crate::AgentEvent::progress(
50 format!("✓ Removed {} old sessions", removed),
51 None,
52 ))
53 .await;
54 } else if subcmd == "stats" {
55 let sessions = mgr.list_sessions();
56 let _ = ctx
57 .event_tx
58 .send(crate::AgentEvent::progress(
59 format!("📊 {} sessions total", sessions.len()),
60 None,
61 ))
62 .await;
63 } else {
64 let sessions = mgr.list_sessions();
65 if sessions.is_empty() {
66 let _ = ctx
67 .event_tx
68 .send(crate::AgentEvent::progress("No saved sessions", None))
69 .await;
70 } else {
71 let mut info = format!("📚 Sessions ({}):\n", sessions.len());
72 for session in sessions.iter().take(DISPLAY_SESSIONS_LIMIT) {
73 info.push_str(&format!(
74 "• {} - {} msgs\n",
75 session.short_id(),
76 session.message_count
77 ));
78 }
79 let _ = ctx
80 .event_tx
81 .send(crate::AgentEvent::progress(info, None))
82 .await;
83 }
84 }
85 } else {
86 let _ = ctx
87 .event_tx
88 .send(crate::AgentEvent::progress(
89 "❌ Session manager not available",
90 None,
91 ))
92 .await;
93 }
94
95 false })
97 }
98}