Skip to main content

vtcode_tui/core_tui/app/session/
trust.rs

1use crate::core_tui::app::types::TrustMode;
2use hashbrown::{HashMap, HashSet};
3
4#[allow(dead_code)]
5#[derive(Debug, Clone)]
6pub struct TrustSetting {
7    pub mode: TrustMode,
8    pub updated_at: String,
9}
10
11#[allow(dead_code)]
12pub struct TrustManager {
13    settings: HashMap<String, TrustSetting>,
14    session_cache: HashSet<String>,
15}
16
17#[allow(dead_code)]
18impl TrustManager {
19    pub fn new() -> Self {
20        Self {
21            settings: HashMap::new(),
22            session_cache: HashSet::new(),
23        }
24    }
25
26    pub fn check_auto_approve(&self, file_path: &str) -> bool {
27        if let Some(setting) = self.settings.get(file_path)
28            && matches!(setting.mode, TrustMode::AutoTrust)
29        {
30            return true;
31        }
32        self.session_cache.contains(file_path)
33    }
34
35    pub fn update_trust(&mut self, file_path: String, mode: TrustMode) {
36        let timestamp = chrono::Utc::now().to_rfc3339();
37        let setting = TrustSetting {
38            mode,
39            updated_at: timestamp,
40        };
41
42        match mode {
43            TrustMode::AutoTrust | TrustMode::Always => {
44                self.settings.insert(file_path.clone(), setting);
45            }
46            TrustMode::Session => {
47                self.session_cache.insert(file_path);
48            }
49            TrustMode::Once => {}
50        }
51    }
52
53    pub fn get_trust_mode(&self, file_path: &str) -> Option<TrustMode> {
54        self.settings.get(file_path).map(|s| s.mode)
55    }
56}
57
58impl Default for TrustManager {
59    fn default() -> Self {
60        Self::new()
61    }
62}