matrixcode_core/command/handlers/
compact.rs1use std::future::Future;
4use std::pin::Pin;
5
6use crate::command::{BackendContext, Command};
7
8pub struct Compact;
9
10impl Command for Compact {
11 fn name(&self) -> &'static str {
12 "compact"
13 }
14
15 fn aliases(&self) -> &[&'static str] {
16 &["compress"]
17 }
18
19 fn help(&self) -> Option<&'static str> {
20 Some("压缩对话历史以减少 token 使用")
21 }
22
23 fn execute<'a>(
24 &'a self,
25 ctx: &'a mut BackendContext<'_>,
26 ) -> Pin<Box<dyn Future<Output = bool> + Send + 'a>> {
27 Box::pin(async move {
28 let count = ctx.agent.get_messages().len();
30
31 if count == 0 {
32 let _ = ctx
33 .event_tx
34 .send(crate::AgentEvent::progress(
35 "对话历史为空,无需压缩".to_string(),
36 None,
37 ))
38 .await;
39 return false;
40 }
41
42 let _ = ctx
43 .event_tx
44 .send(crate::AgentEvent::progress(
45 format!("当前对话历史:{} 条消息", count),
46 None,
47 ))
48 .await;
49
50 let _ = ctx
53 .event_tx
54 .send(crate::AgentEvent::progress(
55 "💡 提示:压缩功能在对话过程中自动执行\n使用 /clear 清空对话历史".to_string(),
56 None,
57 ))
58 .await;
59
60 false
61 })
62 }
63}