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
aegis-runtime-mv / src / persistence / sqlite / actions.rs
| 1 | use crate::execution::action::{Action, ActionStatus}; |
| 2 | use crate::execution::receipt::ActionReceipt; |
| 3 | use crate::persistence::async_db::AsyncDatabase; |
| 4 | use crate::persistence::error::PersistenceError; |
| 5 | use crate::sessions::id::{ActionId, RunId, SessionId}; |
| 6 | use crate::sessions::store::ActionStore; |
| 7 | use async_trait::async_trait; |
| 8 | use rusqlite::params; |
| 9 | |
| 10 | #[derive(Clone)] |
| 11 | pub struct SqliteActionStore { |
| 12 | db: AsyncDatabase, |
| 13 | } |
| 14 | |
| 15 | impl SqliteActionStore { |
| 16 | pub fn new(db: AsyncDatabase) -> Self { |
| 17 | Self { db } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | #[async_trait] |
| 22 | impl ActionStore for SqliteActionStore { |
| 23 | async fn create(&self, action: &Action) -> Result<(), PersistenceError> { |
| 24 | let action = action.clone(); |
| 25 | self.db |
| 26 | .interact(move |conn| { |
| 27 | let params_str = serde_json::to_string(&action.params)?; |
| 28 | let receipt_str = action |
| 29 | .receipt |
| 30 | .as_ref() |
| 31 | .map(serde_json::to_string) |
| 32 | .transpose()?; |
| 33 | |
| 34 | conn.execute( |
| 35 | "INSERT INTO actions (id, run_id, session_id, capability, intent, params_json, status, receipt_json, created_at, completed_at) |
| 36 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)", |
| 37 | params![ |
| 38 | action.id.as_str(), |
| 39 | action.run_id.as_str(), |
| 40 | action.session_id.as_str(), |
| 41 | action.capability, |
| 42 | action.intent, |
| 43 | params_str, |
| 44 | action.status.as_str(), |
| 45 | receipt_str, |
| 46 | action.created_at, |
| 47 | action.completed_at, |
| 48 | ], |
| 49 | )?; |
| 50 | Ok(()) |
| 51 | }) |
| 52 | .await |
| 53 | } |
| 54 | |
| 55 | async fn get(&self, id: &ActionId) -> Result<Option<Action>, PersistenceError> { |
| 56 | let id_str = id.to_string(); |
| 57 | self.db |
| 58 | .interact(move |conn| { |
| 59 | let mut stmt = conn.prepare( |
| 60 | "SELECT id, run_id, session_id, capability, intent, params_json, status, receipt_json, created_at, completed_at |
| 61 | FROM actions WHERE id = ?1", |
| 62 | )?; |
| 63 | let mut rows = stmt.query(params![id_str])?; |
| 64 | if let Some(row) = rows.next()? { |
| 65 | let id_val: String = row.get(0)?; |
| 66 | let run_val: String = row.get(1)?; |
| 67 | let session_val: String = row.get(2)?; |
| 68 | let capability: String = row.get(3)?; |
| 69 | let intent: String = row.get(4)?; |
| 70 | let params_json: String = row.get(5)?; |
| 71 | let status_str: String = row.get(6)?; |
| 72 | let receipt_json: Option<String> = row.get(7)?; |
| 73 | let created_at: i64 = row.get(8)?; |
| 74 | let completed_at: Option<i64> = row.get(9)?; |
| 75 | |
| 76 | let params: serde_json::Value = serde_json::from_str(¶ms_json)?; |
| 77 | let status = ActionStatus::from_str(&status_str) |
| 78 | .ok_or_else(|| PersistenceError::Serialization(format!("Invalid status: {status_str}")))?; |
| 79 | let receipt: Option<ActionReceipt> = receipt_json |
| 80 | .map(|s| serde_json::from_str(&s)) |
| 81 | .transpose()?; |
| 82 | |
| 83 | Ok(Some(Action { |
| 84 | id: ActionId::from_string(id_val), |
| 85 | run_id: RunId::from_string(run_val), |
| 86 | session_id: SessionId::from_string(session_val), |
| 87 | capability, |
| 88 | intent, |
| 89 | params, |
| 90 | status, |
| 91 | receipt, |
| 92 | created_at, |
| 93 | completed_at, |
| 94 | })) |
| 95 | } else { |
| 96 | Ok(None) |
| 97 | } |
| 98 | }) |
| 99 | .await |
| 100 | } |
| 101 | |
| 102 | async fn update(&self, action: &Action) -> Result<(), PersistenceError> { |
| 103 | let action = action.clone(); |
| 104 | self.db |
| 105 | .interact(move |conn| { |
| 106 | let receipt_str = action |
| 107 | .receipt |
| 108 | .as_ref() |
| 109 | .map(serde_json::to_string) |
| 110 | .transpose()?; |
| 111 | |
| 112 | let rows = conn.execute( |
| 113 | "UPDATE actions SET status = ?1, receipt_json = ?2, completed_at = ?3 WHERE id = ?4", |
| 114 | params![ |
| 115 | action.status.as_str(), |
| 116 | receipt_str, |
| 117 | action.completed_at, |
| 118 | action.id.as_str(), |
| 119 | ], |
| 120 | )?; |
| 121 | if rows == 0 { |
| 122 | return Err(PersistenceError::NotFound(action.id.to_string())); |
| 123 | } |
| 124 | Ok(()) |
| 125 | }) |
| 126 | .await |
| 127 | } |
| 128 | |
| 129 | async fn list_by_run(&self, run_id: &RunId) -> Result<Vec<Action>, PersistenceError> { |
| 130 | let run_id = run_id.clone(); |
| 131 | self.db |
| 132 | .interact(move |conn| { |
| 133 | let mut stmt = conn.prepare( |
| 134 | "SELECT id, run_id, session_id, capability, intent, params_json, status, receipt_json, created_at, completed_at |
| 135 | FROM actions WHERE run_id = ?1 ORDER BY created_at ASC", |
| 136 | )?; |
| 137 | let rows = stmt.query_map(params![run_id.as_str()], |row| { |
| 138 | let id_val: String = row.get(0)?; |
| 139 | let run_val: String = row.get(1)?; |
| 140 | let session_val: String = row.get(2)?; |
| 141 | let capability: String = row.get(3)?; |
| 142 | let intent: String = row.get(4)?; |
| 143 | let params_json: String = row.get(5)?; |
| 144 | let status_str: String = row.get(6)?; |
| 145 | let receipt_json: Option<String> = row.get(7)?; |
| 146 | let created_at: i64 = row.get(8)?; |
| 147 | let completed_at: Option<i64> = row.get(9)?; |
| 148 | Ok((id_val, run_val, session_val, capability, intent, params_json, status_str, receipt_json, created_at, completed_at)) |
| 149 | })?; |
| 150 | |
| 151 | let mut actions = Vec::new(); |
| 152 | for r in rows { |
| 153 | let (id_val, run_val, session_val, capability, intent, params_json, status_str, receipt_json, created_at, completed_at) = r?; |
| 154 | let params: serde_json::Value = serde_json::from_str(¶ms_json)?; |
| 155 | let status = ActionStatus::from_str(&status_str) |
| 156 | .ok_or_else(|| PersistenceError::Serialization(format!("Invalid status: {status_str}")))?; |
| 157 | let receipt: Option<ActionReceipt> = receipt_json |
| 158 | .map(|s| serde_json::from_str(&s)) |
| 159 | .transpose()?; |
| 160 | |
| 161 | actions.push(Action { |
| 162 | id: ActionId::from_string(id_val), |
| 163 | run_id: RunId::from_string(run_val), |
| 164 | session_id: SessionId::from_string(session_val), |
| 165 | capability, |
| 166 | intent, |
| 167 | params, |
| 168 | status, |
| 169 | receipt, |
| 170 | created_at, |
| 171 | completed_at, |
| 172 | }); |
| 173 | } |
| 174 | Ok(actions) |
| 175 | }) |
| 176 | .await |
| 177 | } |
| 178 | } |