1use std::collections::VecDeque;
2use std::sync::Arc;
3use tokio::sync::RwLock;
4
5use crate::app::Config;
6use crate::models::{Model, ProjectContext};
7
8pub struct AppState {
10 pub config: Arc<RwLock<Config>>,
12 pub model: Arc<RwLock<Box<dyn Model>>>,
14 pub context: Arc<RwLock<ProjectContext>>,
16 pub history: Arc<RwLock<VecDeque<(String, String)>>>,
18}
19
20impl AppState {
21 pub fn new(config: Config, model: Box<dyn Model>, context: ProjectContext) -> Self {
23 Self {
24 config: Arc::new(RwLock::new(config)),
25 model: Arc::new(RwLock::new(model)),
26 context: Arc::new(RwLock::new(context)),
27 history: Arc::new(RwLock::new(VecDeque::new())),
28 }
29 }
30
31 pub async fn switch_model(&self, new_model: Box<dyn Model>) {
33 let mut model = self.model.write().await;
34 *model = new_model;
35 }
36
37 pub async fn update_config(&self, new_config: Config) {
39 let mut config = self.config.write().await;
40 *config = new_config;
41 }
42
43 pub async fn add_to_history(&self, user_msg: String, assistant_msg: String) {
45 let mut history = self.history.write().await;
46 history.push_back((user_msg, assistant_msg));
47
48 if history.len() > 10 {
50 history.pop_front();
51 }
52 }
53
54 pub async fn clear_history(&self) {
56 let mut history = self.history.write().await;
57 history.clear();
58 }
59}