Skip to main content

matrixcode_core/command/handlers/
compact.rs

1//! /compact 和 /compress 命令处理器
2
3use std::future::Future;
4use std::pin::Pin;
5
6use crate::command::{Command, BackendContext};
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>(&'a self, ctx: &'a mut BackendContext<'_>) 
24        -> Pin<Box<dyn Future<Output = bool> + Send + 'a>> 
25    {
26        Box::pin(async move {
27            // 获取当前消息数量
28            let count = ctx.agent.get_messages().len();
29            
30            if count == 0 {
31                let _ = ctx.event_tx.send(crate::AgentEvent::progress(
32                    "对话历史为空,无需压缩".to_string(),
33                    None,
34                )).await;
35                return false;
36            }
37
38            let _ = ctx.event_tx.send(crate::AgentEvent::progress(
39                format!("当前对话历史:{} 条消息", count),
40                None,
41            )).await;
42            
43            // 注意:完整的压缩功能需要 AI 参与
44            // 这里只提供状态信息
45            let _ = ctx.event_tx.send(crate::AgentEvent::progress(
46                "💡 提示:压缩功能在对话过程中自动执行\n使用 /clear 清空对话历史".to_string(),
47                None,
48            )).await;
49            
50            false
51        })
52    }
53}