mermaid_cli/app/
state.rs

1use std::sync::Arc;
2use tokio::sync::RwLock;
3
4use crate::app::Config;
5use crate::models::{Model, ProjectContext};
6
7/// Global application state
8pub struct AppState {
9    /// Configuration
10    pub config: Arc<RwLock<Config>>,
11    /// Current model
12    pub model: Arc<RwLock<Box<dyn Model>>>,
13    /// Project context
14    pub context: Arc<RwLock<ProjectContext>>,
15    /// Conversation history (for context)
16    pub history: Arc<RwLock<Vec<(String, String)>>>,
17}
18
19impl AppState {
20    /// Create new app state
21    pub fn new(config: Config, model: Box<dyn Model>, context: ProjectContext) -> Self {
22        Self {
23            config: Arc::new(RwLock::new(config)),
24            model: Arc::new(RwLock::new(model)),
25            context: Arc::new(RwLock::new(context)),
26            history: Arc::new(RwLock::new(Vec::new())),
27        }
28    }
29
30    /// Switch to a different model
31    pub async fn switch_model(&self, new_model: Box<dyn Model>) {
32        let mut model = self.model.write().await;
33        *model = new_model;
34    }
35
36    /// Update configuration
37    pub async fn update_config(&self, new_config: Config) {
38        let mut config = self.config.write().await;
39        *config = new_config;
40    }
41
42    /// Add to conversation history
43    pub async fn add_to_history(&self, user_msg: String, assistant_msg: String) {
44        let mut history = self.history.write().await;
45        history.push((user_msg, assistant_msg));
46
47        // Keep history manageable (last 10 exchanges)
48        if history.len() > 10 {
49            history.drain(0..1);
50        }
51    }
52
53    /// Clear conversation history
54    pub async fn clear_history(&self) {
55        let mut history = self.history.write().await;
56        history.clear();
57    }
58}