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 / sessions / id.rs
| 1 | use serde::{Deserialize, Serialize}; |
| 2 | use std::fmt; |
| 3 | use uuid::Uuid; |
| 4 | |
| 5 | macro_rules! define_id { |
| 6 | ($name:ident, $prefix:expr) => { |
| 7 | #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] |
| 8 | pub struct $name(String); |
| 9 | |
| 10 | impl $name { |
| 11 | pub fn new() -> Self { |
| 12 | Self(format!("{}_{}", $prefix, Uuid::new_v4().simple())) |
| 13 | } |
| 14 | |
| 15 | pub fn from_string(s: impl Into<String>) -> Self { |
| 16 | Self(s.into()) |
| 17 | } |
| 18 | |
| 19 | pub fn as_str(&self) -> &str { |
| 20 | &self.0 |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl Default for $name { |
| 25 | fn default() -> Self { |
| 26 | Self::new() |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl fmt::Display for $name { |
| 31 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 32 | write!(f, "{}", self.0) |
| 33 | } |
| 34 | } |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | define_id!(SessionId, "sess"); |
| 39 | define_id!(RunId, "run"); |
| 40 | define_id!(ActionId, "act"); |
| 41 | define_id!(ApprovalId, "appr"); |
| 42 | define_id!(MessageId, "msg"); |
| 43 | |
| 44 | impl From<SessionId> for aien_protocol_types::SessionId { |
| 45 | fn from(s: SessionId) -> Self { |
| 46 | let raw = s.as_str().strip_prefix("sess_").unwrap_or(s.as_str()); |
| 47 | if let Ok(u) = uuid::Uuid::parse_str(raw) { |
| 48 | aien_protocol_types::SessionId(u) |
| 49 | } else { |
| 50 | aien_protocol_types::SessionId(uuid::Uuid::new_v5( |
| 51 | &uuid::Uuid::NAMESPACE_OID, |
| 52 | s.as_str().as_bytes(), |
| 53 | )) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | impl From<aien_protocol_types::SessionId> for SessionId { |
| 59 | fn from(id: aien_protocol_types::SessionId) -> Self { |
| 60 | Self::from_string(format!("sess_{}", id.0.simple())) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | impl From<RunId> for aien_protocol_types::RunId { |
| 65 | fn from(r: RunId) -> Self { |
| 66 | let raw = r.as_str().strip_prefix("run_").unwrap_or(r.as_str()); |
| 67 | if let Ok(u) = uuid::Uuid::parse_str(raw) { |
| 68 | aien_protocol_types::RunId(u) |
| 69 | } else { |
| 70 | aien_protocol_types::RunId(uuid::Uuid::new_v5( |
| 71 | &uuid::Uuid::NAMESPACE_OID, |
| 72 | r.as_str().as_bytes(), |
| 73 | )) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl From<aien_protocol_types::RunId> for RunId { |
| 79 | fn from(id: aien_protocol_types::RunId) -> Self { |
| 80 | Self::from_string(format!("run_{}", id.0.simple())) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | impl From<ActionId> for aien_protocol_types::ActionId { |
| 85 | fn from(a: ActionId) -> Self { |
| 86 | let raw = a.as_str().strip_prefix("act_").unwrap_or(a.as_str()); |
| 87 | if let Ok(u) = uuid::Uuid::parse_str(raw) { |
| 88 | aien_protocol_types::ActionId(u) |
| 89 | } else { |
| 90 | aien_protocol_types::ActionId(uuid::Uuid::new_v5( |
| 91 | &uuid::Uuid::NAMESPACE_OID, |
| 92 | a.as_str().as_bytes(), |
| 93 | )) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | impl From<aien_protocol_types::ActionId> for ActionId { |
| 99 | fn from(id: aien_protocol_types::ActionId) -> Self { |
| 100 | Self::from_string(format!("act_{}", id.0.simple())) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | impl From<ApprovalId> for aien_protocol_types::ApprovalId { |
| 105 | fn from(a: ApprovalId) -> Self { |
| 106 | let raw = a.as_str().strip_prefix("appr_").unwrap_or(a.as_str()); |
| 107 | if let Ok(u) = uuid::Uuid::parse_str(raw) { |
| 108 | aien_protocol_types::ApprovalId(u) |
| 109 | } else { |
| 110 | aien_protocol_types::ApprovalId(uuid::Uuid::new_v5( |
| 111 | &uuid::Uuid::NAMESPACE_OID, |
| 112 | a.as_str().as_bytes(), |
| 113 | )) |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | impl From<aien_protocol_types::ApprovalId> for ApprovalId { |
| 119 | fn from(id: aien_protocol_types::ApprovalId) -> Self { |
| 120 | Self::from_string(format!("appr_{}", id.0.simple())) |
| 121 | } |
| 122 | } |