Skip to main content

ai_agent/utils/permissions/
permission_mode.rs

1// Source: ~/claudecode/openclaudecode/src/utils/permissions/PermissionMode.ts
2#![allow(dead_code)]
3
4//! Permission mode utilities — title, symbol, color helpers.
5
6use crate::types::permissions::{
7    EXTERNAL_PERMISSION_MODES, ExternalPermissionMode, INTERNAL_PERMISSION_MODES, PermissionMode,
8};
9
10// Re-exports for backwards compatibility
11
12/// Color key for permission mode display.
13pub enum ModeColorKey {
14    Text,
15    PlanMode,
16    Permission,
17    AutoAccept,
18    Error,
19    Warning,
20}
21
22impl ModeColorKey {
23    pub fn as_str(&self) -> &'static str {
24        match self {
25            ModeColorKey::Text => "text",
26            ModeColorKey::PlanMode => "planMode",
27            ModeColorKey::Permission => "permission",
28            ModeColorKey::AutoAccept => "autoAccept",
29            ModeColorKey::Error => "error",
30            ModeColorKey::Warning => "warning",
31        }
32    }
33}
34
35/// Configuration for a permission mode.
36struct PermissionModeConfig {
37    title: &'static str,
38    short_title: &'static str,
39    symbol: &'static str,
40    color: ModeColorKey,
41    external: &'static str,
42}
43
44const PAUSE_ICON: &str = "\u{23F8}";
45
46const DEFAULT_CONFIG: PermissionModeConfig = PermissionModeConfig {
47    title: "Default",
48    short_title: "Default",
49    symbol: "",
50    color: ModeColorKey::Text,
51    external: "default",
52};
53
54fn get_mode_config(mode: &str) -> PermissionModeConfig {
55    match mode {
56        "default" => DEFAULT_CONFIG,
57        "plan" => PermissionModeConfig {
58            title: "Plan Mode",
59            short_title: "Plan",
60            symbol: PAUSE_ICON,
61            color: ModeColorKey::PlanMode,
62            external: "plan",
63        },
64        "acceptEdits" => PermissionModeConfig {
65            title: "Accept edits",
66            short_title: "Accept",
67            symbol: "\u{23F5}\u{23F5}",
68            color: ModeColorKey::AutoAccept,
69            external: "acceptEdits",
70        },
71        "bypassPermissions" => PermissionModeConfig {
72            title: "Bypass Permissions",
73            short_title: "Bypass",
74            symbol: "\u{23F5}\u{23F5}",
75            color: ModeColorKey::Error,
76            external: "bypassPermissions",
77        },
78        "dontAsk" => PermissionModeConfig {
79            title: "Don't Ask",
80            short_title: "DontAsk",
81            symbol: "\u{23F5}\u{23F5}",
82            color: ModeColorKey::Error,
83            external: "dontAsk",
84        },
85        "auto" => PermissionModeConfig {
86            title: "Auto mode",
87            short_title: "Auto",
88            symbol: "\u{23F5}\u{23F5}",
89            color: ModeColorKey::Warning,
90            external: "default",
91        },
92        _ => DEFAULT_CONFIG,
93    }
94}
95
96/// Type guard to check if a PermissionMode is an ExternalPermissionMode.
97pub fn is_external_permission_mode(mode: &str) -> bool {
98    // External users can't have auto, so always true for them
99    if std::env::var("USER_TYPE").as_deref() != Ok("ant") {
100        return true;
101    }
102    mode != "auto" && mode != "bubble"
103}
104
105/// Converts a PermissionMode to an ExternalPermissionMode.
106pub fn to_external_permission_mode(mode: &str) -> String {
107    get_mode_config(mode).external.to_string()
108}
109
110/// Parses a string into a PermissionMode.
111pub fn permission_mode_from_string(s: &str) -> String {
112    if INTERNAL_PERMISSION_MODES.contains(&s) {
113        s.to_string()
114    } else {
115        "default".to_string()
116    }
117}
118
119/// Gets the display title for a permission mode.
120pub fn permission_mode_title(mode: &str) -> &'static str {
121    get_mode_config(mode).title
122}
123
124/// Checks if a mode is the default mode.
125pub fn is_default_mode(mode: Option<&str>) -> bool {
126    mode.map_or(true, |m| m == "default")
127}
128
129/// Gets the short title for a permission mode.
130pub fn permission_mode_short_title(mode: &str) -> &'static str {
131    get_mode_config(mode).short_title
132}
133
134/// Gets the symbol for a permission mode.
135pub fn permission_mode_symbol(mode: &str) -> &'static str {
136    get_mode_config(mode).symbol
137}
138
139/// Gets the color key for a permission mode.
140pub fn get_mode_color(mode: &str) -> ModeColorKey {
141    get_mode_config(mode).color
142}