1use std::sync::Arc;
2use tokio::sync::RwLock;
3
4use crate::app::Config;
5use crate::models::{Model, ProjectContext};
6
7pub struct AppState {
9 pub config: Arc<RwLock<Config>>,
11 pub model: Arc<RwLock<Box<dyn Model>>>,
13 pub context: Arc<RwLock<ProjectContext>>,
15 pub history: Arc<RwLock<Vec<(String, String)>>>,
17}
18
19impl AppState {
20 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 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 pub async fn update_config(&self, new_config: Config) {
38 let mut config = self.config.write().await;
39 *config = new_config;
40 }
41
42 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 if history.len() > 10 {
49 history.drain(0..1);
50 }
51 }
52
53 pub async fn clear_history(&self) {
55 let mut history = self.history.write().await;
56 history.clear();
57 }
58}