use std::collections::HashMap;
use std::sync::Mutex;
use aidaemon_core::RunId;
use async_trait::async_trait;
#[async_trait]
pub trait SessionIndex: Send + Sync {
async fn latest_run(&self, session_id: &str) -> Option<RunId>;
async fn set_latest(&self, session_id: &str, run_id: RunId);
async fn clear(&self, session_id: &str);
}
#[derive(Default)]
pub struct MemorySessionIndex {
latest: Mutex<HashMap<String, RunId>>,
}
impl MemorySessionIndex {
pub fn new() -> Self {
Self::default()
}
}
#[async_trait]
impl SessionIndex for MemorySessionIndex {
async fn latest_run(&self, session_id: &str) -> Option<RunId> {
self.latest
.lock()
.ok()
.and_then(|map| map.get(session_id).copied())
}
async fn set_latest(&self, session_id: &str, run_id: RunId) {
if let Ok(mut map) = self.latest.lock() {
map.insert(session_id.to_string(), run_id);
}
}
async fn clear(&self, session_id: &str) {
if let Ok(mut map) = self.latest.lock() {
map.remove(session_id);
}
}
}
pub fn session_id(channel_name: &str, conversation_id: &str) -> String {
format!("{channel_name}:{conversation_id}")
}