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 或 /mode:询问 - Ask 模式(默认,变更前询问)
14/// - /mode:auto 或 /mode:自动 - 自动模式(不询问)
15/// - /mode:strict 或 /mode:严格 - 严格模式(所有操作都询问)
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> 或 /mode:<询问|自动|严格>")
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                // English
41                "ask" | "a" => ApproveMode::Ask,
42                "auto" => ApproveMode::Auto,
43                "strict" => ApproveMode::Strict,
44                // Chinese
45                "询问" => ApproveMode::Ask,
46                "自动" => ApproveMode::Auto,
47                "严格" => ApproveMode::Strict,
48                _ => {
49                    let _ = ctx
50                        .event_tx
51                        .send(crate::AgentEvent::error(
52                            format!("未知模式: {}. 支持: ask/auto/strict 或 询问/自动/严格", mode_str),
53                            None,
54                            None,
55                        ))
56                        .await;
57                    return false;
58                }
59            };
60
61            ctx.agent.set_approve_mode(mode);
62
63            let mode_name = match mode {
64                ApproveMode::Ask => "ask (询问 - 变更前确认)",
65                ApproveMode::Auto => "auto (自动 - 无需确认)",
66                ApproveMode::Strict => "strict (严格 - 所有操作都确认)",
67            };
68
69            let _ = ctx
70                .event_tx
71                .send(crate::AgentEvent::progress(
72                    format!("✓ 模式设置为: {}", mode_name),
73                    None,
74                ))
75                .await;
76
77            false // 不转发给 agent
78        })
79    }
80}