add chat function
#
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
use dashmap::DashMap;
|
||||
use tokio::sync::broadcast;
|
||||
use crate::models::ChatSession;
|
||||
|
||||
pub struct AppState {
|
||||
pub sessions: DashMap<String, ChatSession>,
|
||||
// broadcast channel: session_id -> sender
|
||||
pub notifiers: DashMap<String, broadcast::Sender<String>>,
|
||||
// agent broadcast for new sessions
|
||||
pub agent_notifier: broadcast::Sender<String>,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new() -> Self {
|
||||
let (agent_tx, _) = broadcast::channel(64);
|
||||
Self {
|
||||
sessions: DashMap::new(),
|
||||
notifiers: DashMap::new(),
|
||||
agent_notifier: agent_tx,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_or_create_notifier(&self, session_id: &str) -> broadcast::Sender<String> {
|
||||
if let Some(tx) = self.notifiers.get(session_id) {
|
||||
tx.clone()
|
||||
} else {
|
||||
let (tx, _) = broadcast::channel(64);
|
||||
self.notifiers.insert(session_id.to_string(), tx.clone());
|
||||
tx
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user