Skip to main content

matrixcode_core/command/handlers/
mode.rs

1//! /mode 命令实现
2//!
3//! 设置审批模式。
4
5use crate::ApproveMode;
6
7use super::super::command_trait::Command;
8use super::super::backend_context::BackendContext;
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>(&'a self, ctx: &'a mut BackendContext<'_>) 
33        -> std::pin::Pin<Box<dyn std::future::Future<Output = bool> + Send + 'a>> 
34    {
35        Box::pin(async move {
36            let mode_str = ctx.message.strip_prefix("/mode:").unwrap_or("");
37            
38            let mode = match mode_str.to_lowercase().as_str() {
39                "ask" | "a" => ApproveMode::Ask,
40                "auto" => ApproveMode::Auto,
41                "strict" => ApproveMode::Strict,
42                _ => {
43                    let _ = ctx.event_tx.send(crate::AgentEvent::error(
44                        format!("Unknown mode: {}. Use ask/auto/strict", mode_str),
45                        None,
46                        None,
47                    )).await;
48                    return false;
49                }
50            };
51
52            ctx.agent.set_approve_mode(mode);
53            
54            let mode_name = match mode {
55                ApproveMode::Ask => "ask (default)",
56                ApproveMode::Auto => "auto (no confirmation)",
57                ApproveMode::Strict => "strict (confirm all)",
58            };
59            
60            let _ = ctx.event_tx.send(crate::AgentEvent::progress(
61                format!("✓ Mode set to: {}", mode_name),
62                None,
63            )).await;
64
65            false // 不转发给 agent
66        })
67    }
68}