Skip to main content

matrixcode_core/command/handlers/
mode.rs

1//! /mode 命令实现
2//!
3//! 设置审批模式。
4
5use crate::ApproveMode;
6
7use super::super::backend_context::BackendContext;
8use super::super::command_trait::Command;
9
10/// Mode 命令
11///
12/// 用法:
13/// - /mode:ask - Ask 模式(默认,变更前询问)
14/// - /mode:auto - 自动模式(不询问)
15/// - /mode:strict - 严格模式(所有操作都询问)
16pub struct Mode;
17
18impl Command for Mode {
19    fn name(&self) -> &'static str {
20        "mode"
21    }
22
23    fn help(&self) -> Option<&'static str> {
24        Some("设置审批模式。用法: /mode:<ask|auto|strict>")
25    }
26
27    /// 特殊匹配:/mode:xxx 前缀
28    fn matches(&self, msg: &str) -> bool {
29        msg.starts_with("/mode:")
30    }
31
32    fn execute<'a>(
33        &'a self,
34        ctx: &'a mut BackendContext<'_>,
35    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = bool> + Send + 'a>> {
36        Box::pin(async move {
37            let mode_str = ctx.message.strip_prefix("/mode:").unwrap_or("");
38
39            let mode = match mode_str.to_lowercase().as_str() {
40                "ask" | "a" => ApproveMode::Ask,
41                "auto" => ApproveMode::Auto,
42                "strict" => ApproveMode::Strict,
43                _ => {
44                    let _ = ctx
45                        .event_tx
46                        .send(crate::AgentEvent::error(
47                            format!("Unknown mode: {}. Use ask/auto/strict", mode_str),
48                            None,
49                            None,
50                        ))
51                        .await;
52                    return false;
53                }
54            };
55
56            ctx.agent.set_approve_mode(mode);
57
58            let mode_name = match mode {
59                ApproveMode::Ask => "ask (default)",
60                ApproveMode::Auto => "auto (no confirmation)",
61                ApproveMode::Strict => "strict (confirm all)",
62            };
63
64            let _ = ctx
65                .event_tx
66                .send(crate::AgentEvent::progress(
67                    format!("✓ Mode set to: {}", mode_name),
68                    None,
69                ))
70                .await;
71
72            false // 不转发给 agent
73        })
74    }
75}