deepstrike_core/memory/
durable.rs1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5use crate::types::error::{DeepStrikeError, Result};
6use crate::types::message::Message;
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct SessionData {
10 pub session_id: String,
11 pub agent_id: String,
12 pub messages: Vec<Message>,
13 pub metadata: serde_json::Value,
14 pub created_at_ms: u64,
15 pub updated_at_ms: u64,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct SessionMeta {
20 pub session_id: String,
21 pub agent_id: String,
22 pub message_count: usize,
23 pub created_at_ms: u64,
24 pub updated_at_ms: u64,
25}
26
27pub trait SessionStore: Send + Sync {
28 fn save(&self, session_id: &str, data: &SessionData) -> Result<()>;
29 fn load(&self, session_id: &str) -> Result<Option<SessionData>>;
30 fn list(&self, agent_id: &str) -> Result<Vec<SessionMeta>>;
31 fn delete(&self, session_id: &str) -> Result<()>;
32}
33
34pub struct InMemoryStore {
35 data: std::sync::Mutex<HashMap<String, SessionData>>,
36}
37
38impl InMemoryStore {
39 pub fn new() -> Self {
40 Self {
41 data: std::sync::Mutex::new(HashMap::new()),
42 }
43 }
44}
45
46impl Default for InMemoryStore {
47 fn default() -> Self {
48 Self::new()
49 }
50}
51
52impl SessionStore for InMemoryStore {
53 fn save(&self, session_id: &str, data: &SessionData) -> Result<()> {
54 self.data
55 .lock()
56 .map_err(|e| DeepStrikeError::InvalidConfig(e.to_string()))?
57 .insert(session_id.to_string(), data.clone());
58 Ok(())
59 }
60
61 fn load(&self, session_id: &str) -> Result<Option<SessionData>> {
62 Ok(self
63 .data
64 .lock()
65 .map_err(|e| DeepStrikeError::InvalidConfig(e.to_string()))?
66 .get(session_id)
67 .cloned())
68 }
69
70 fn list(&self, agent_id: &str) -> Result<Vec<SessionMeta>> {
71 let guard = self
72 .data
73 .lock()
74 .map_err(|e| DeepStrikeError::InvalidConfig(e.to_string()))?;
75 Ok(guard
76 .values()
77 .filter(|d| d.agent_id == agent_id)
78 .map(|d| SessionMeta {
79 session_id: d.session_id.clone(),
80 agent_id: d.agent_id.clone(),
81 message_count: d.messages.len(),
82 created_at_ms: d.created_at_ms,
83 updated_at_ms: d.updated_at_ms,
84 })
85 .collect())
86 }
87
88 fn delete(&self, session_id: &str) -> Result<()> {
89 self.data
90 .lock()
91 .map_err(|e| DeepStrikeError::InvalidConfig(e.to_string()))?
92 .remove(session_id);
93 Ok(())
94 }
95}