Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aegis-runtime-mv/blob/main/src/persistence/sqlite/sessions.rs # aegis-runtime-mv Native Rust agent runtime: Axum WebSocket/SSE/REST gateway, heartbeat scheduler, dependency-aware task workflow, SQLite storage. Attachment of AienOS. Pre-alpha. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aegis-runtime-mv / src / persistence / sqlite / sessions.rs` 196 lines ยท 8699 bytes Raw Blame History 1 use crate::persistence::async_db::AsyncDatabase; 2 use crate::persistence::error::PersistenceError; 3 use crate::sessions::id::SessionId; 4 use crate::sessions::session::{Session, SessionBudget, SessionStatus}; 5 use crate::sessions::store::SessionStore; 6 use async_trait::async_trait; 7 use chrono::Utc; 8 use rusqlite::params; 9 10 #[derive(Clone)] 11 pub struct SqliteSessionStore { 12 db: AsyncDatabase, 13 } 14 15 impl SqliteSessionStore { 16 pub fn new(db: AsyncDatabase) -> Self { 17 Self { db } 18 } 19 } 20 21 #[async_trait] 22 impl SessionStore for SqliteSessionStore { 23 async fn create(&self, session: &Session) -> Result<(), PersistenceError> { 24 let session = session.clone(); 25 self.db 26 .interact(move |conn| { 27 let budget_str = serde_json::to_string(&session.budget)?; 28 conn.execute( 29 "INSERT INTO sessions (id, parent_session_id, status, objective, agent_profile, model_profile, policy_profile, budget_json, created_at, updated_at, version) 30 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)", 31 params![ 32 session.id.as_str(), 33 session.parent_session_id.as_ref().map(|p| p.as_str()), 34 session.status.as_str(), 35 session.objective, 36 session.agent_profile, 37 session.model_profile, 38 session.policy_profile, 39 budget_str, 40 session.created_at, 41 session.updated_at, 42 session.version as i64, 43 ], 44 )?; 45 Ok(()) 46 }) 47 .await 48 } 49 50 async fn get(&self, id: &SessionId) -> Result, PersistenceError> { 51 let id_str = id.to_string(); 52 self.db 53 .interact(move |conn| { 54 let mut stmt = conn.prepare( 55 "SELECT id, parent_session_id, status, objective, agent_profile, model_profile, policy_profile, budget_json, created_at, updated_at, version 56 FROM sessions WHERE id = ?1", 57 )?; 58 let mut rows = stmt.query(params![id_str])?; 59 if let Some(row) = rows.next()? { 60 let id_val: String = row.get(0)?; 61 let parent_val: Option = row.get(1)?; 62 let status_str: String = row.get(2)?; 63 let objective: Option = row.get(3)?; 64 let agent_profile: String = row.get(4)?; 65 let model_profile: String = row.get(5)?; 66 let policy_profile: String = row.get(6)?; 67 let budget_json: String = row.get(7)?; 68 let created_at: i64 = row.get(8)?; 69 let updated_at: i64 = row.get(9)?; 70 let version: i64 = row.get(10)?; 71 72 let status = SessionStatus::from_str(&status_str) 73 .ok_or_else(|| PersistenceError::Serialization(format!("Invalid status: {status_str}")))?; 74 let budget: SessionBudget = serde_json::from_str(&budget_json)?; 75 76 let mut msg_stmt = conn.prepare( 77 "SELECT data_json FROM messages WHERE session_id = ?1 ORDER BY created_at ASC, rowid ASC", 78 )?; 79 let msg_rows = msg_stmt.query_map(params![id_val], |r| { 80 let data: String = r.get(0)?; 81 Ok(data) 82 })?; 83 let mut messages = Vec::new(); 84 for m in msg_rows { 85 let data = m?; 86 let msg: crate::sessions::message::Message = serde_json::from_str(&data)?; 87 messages.push(msg); 88 } 89 90 Ok(Some(Session { 91 id: SessionId::from_string(id_val), 92 parent_session_id: parent_val.map(SessionId::from_string), 93 status, 94 objective, 95 agent_profile, 96 model_profile, 97 policy_profile, 98 messages, 99 budget, 100 created_at, 101 updated_at, 102 version: version as u64, 103 })) 104 } else { 105 Ok(None) 106 } 107 }) 108 .await 109 } 110 111 async fn update(&self, session: &Session) -> Result<(), PersistenceError> { 112 let session = session.clone(); 113 self.db 114 .interact(move |conn| { 115 let budget_str = serde_json::to_string(&session.budget)?; 116 let now = Utc::now().timestamp(); 117 118 let updated_rows = conn.execute( 119 "UPDATE sessions SET parent_session_id = ?1, status = ?2, objective = ?3, 120 agent_profile = ?4, model_profile = ?5, policy_profile = ?6, 121 budget_json = ?7, updated_at = ?8, version = version + 1 122 WHERE id = ?9 AND version = ?10", 123 params![ 124 session.parent_session_id.as_ref().map(|p| p.as_str()), 125 session.status.as_str(), 126 session.objective, 127 session.agent_profile, 128 session.model_profile, 129 session.policy_profile, 130 budget_str, 131 now, 132 session.id.as_str(), 133 session.version as i64, 134 ], 135 )?; 136 137 if updated_rows == 0 { 138 return Err(PersistenceError::ConcurrencyConflict { 139 id: session.id.to_string(), 140 expected_version: session.version, 141 }); 142 } 143 Ok(()) 144 }) 145 .await 146 } 147 148 async fn list(&self, limit: usize, offset: usize) -> Result, PersistenceError> { 149 self.db 150 .interact(move |conn| { 151 let mut stmt = conn.prepare( 152 "SELECT id, parent_session_id, status, objective, agent_profile, model_profile, policy_profile, budget_json, created_at, updated_at, version 153 FROM sessions ORDER BY updated_at DESC LIMIT ?1 OFFSET ?2", 154 )?; 155 let rows = stmt.query_map(params![limit as i64, offset as i64], |row| { 156 let id_val: String = row.get(0)?; 157 let parent_val: Option = row.get(1)?; 158 let status_str: String = row.get(2)?; 159 let objective: Option = row.get(3)?; 160 let agent_profile: String = row.get(4)?; 161 let model_profile: String = row.get(5)?; 162 let policy_profile: String = row.get(6)?; 163 let budget_json: String = row.get(7)?; 164 let created_at: i64 = row.get(8)?; 165 let updated_at: i64 = row.get(9)?; 166 let version: i64 = row.get(10)?; 167 Ok((id_val, parent_val, status_str, objective, agent_profile, model_profile, policy_profile, budget_json, created_at, updated_at, version)) 168 })?; 169 170 let mut sessions = Vec::new(); 171 for r in rows { 172 let (id_val, parent_val, status_str, objective, agent_profile, model_profile, policy_profile, budget_json, created_at, updated_at, version) = r?; 173 let status = SessionStatus::from_str(&status_str) 174 .ok_or_else(|| PersistenceError::Serialization(format!("Invalid status: {status_str}")))?; 175 let budget: SessionBudget = serde_json::from_str(&budget_json)?; 176 177 sessions.push(Session { 178 id: SessionId::from_string(id_val), 179 parent_session_id: parent_val.map(SessionId::from_string), 180 status, 181 objective, 182 agent_profile, 183 model_profile, 184 policy_profile, 185 messages: Vec::new(), 186 budget, 187 created_at, 188 updated_at, 189 version: version as u64, 190 }); 191 } 192 Ok(sessions) 193 }) 194 .await 195 } 196 }