use std::collections::VecDeque;
use std::sync::Arc;
use tokio::sync::RwLock;
use crate::app::Config;
use crate::models::{Model, ProjectContext};
pub struct AppState {
pub config: Arc<RwLock<Config>>,
pub model: Arc<RwLock<Box<dyn Model>>>,
pub context: Arc<RwLock<ProjectContext>>,
pub history: Arc<RwLock<VecDeque<(String, String)>>>,
}
impl AppState {
pub fn new(config: Config, model: Box<dyn Model>, context: ProjectContext) -> Self {
Self {
config: Arc::new(RwLock::new(config)),
model: Arc::new(RwLock::new(model)),
context: Arc::new(RwLock::new(context)),
history: Arc::new(RwLock::new(VecDeque::new())),
}
}
pub async fn switch_model(&self, new_model: Box<dyn Model>) {
let mut model = self.model.write().await;
*model = new_model;
}
pub async fn update_config(&self, new_config: Config) {
let mut config = self.config.write().await;
*config = new_config;
}
pub async fn add_to_history(&self, user_msg: String, assistant_msg: String) {
let mut history = self.history.write().await;
history.push_back((user_msg, assistant_msg));
if history.len() > 10 {
history.pop_front();
}
}
pub async fn clear_history(&self) {
let mut history = self.history.write().await;
history.clear();
}
}