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 / session.rs
| 1 | use super::id::SessionId; |
| 2 | use super::message::Message; |
| 3 | use chrono::Utc; |
| 4 | use serde::{Deserialize, Serialize}; |
| 5 | |
| 6 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 7 | #[serde(rename_all = "lowercase")] |
| 8 | pub enum SessionStatus { |
| 9 | Active, |
| 10 | Suspended, |
| 11 | Closed, |
| 12 | } |
| 13 | |
| 14 | impl SessionStatus { |
| 15 | pub fn as_str(&self) -> &'static str { |
| 16 | match self { |
| 17 | Self::Active => "active", |
| 18 | Self::Suspended => "suspended", |
| 19 | Self::Closed => "closed", |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | #[allow(clippy::should_implement_trait)] |
| 24 | pub fn from_str(s: &str) -> Option<Self> { |
| 25 | match s { |
| 26 | "active" => Some(Self::Active), |
| 27 | "suspended" => Some(Self::Suspended), |
| 28 | "closed" => Some(Self::Closed), |
| 29 | _ => None, |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 35 | pub struct SessionBudget { |
| 36 | pub max_total_tokens: u64, |
| 37 | pub max_wall_time_secs: u64, |
| 38 | } |
| 39 | |
| 40 | impl Default for SessionBudget { |
| 41 | fn default() -> Self { |
| 42 | Self { |
| 43 | max_total_tokens: 128_000, |
| 44 | max_wall_time_secs: 3600, |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 50 | pub struct Session { |
| 51 | pub id: SessionId, |
| 52 | pub parent_session_id: Option<SessionId>, |
| 53 | pub status: SessionStatus, |
| 54 | pub objective: Option<String>, |
| 55 | pub agent_profile: String, |
| 56 | pub model_profile: String, |
| 57 | pub policy_profile: String, |
| 58 | pub messages: Vec<Message>, |
| 59 | pub budget: SessionBudget, |
| 60 | pub created_at: i64, |
| 61 | pub updated_at: i64, |
| 62 | pub version: u64, |
| 63 | } |
| 64 | |
| 65 | impl Session { |
| 66 | pub fn new(objective: Option<String>) -> Self { |
| 67 | let now = Utc::now().timestamp(); |
| 68 | Self { |
| 69 | id: SessionId::new(), |
| 70 | parent_session_id: None, |
| 71 | status: SessionStatus::Active, |
| 72 | objective, |
| 73 | agent_profile: "default".to_string(), |
| 74 | model_profile: "default".to_string(), |
| 75 | policy_profile: "default".to_string(), |
| 76 | messages: Vec::new(), |
| 77 | budget: SessionBudget::default(), |
| 78 | created_at: now, |
| 79 | updated_at: now, |
| 80 | version: 1, |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | pub fn branch(&self) -> Self { |
| 85 | let now = Utc::now().timestamp(); |
| 86 | Self { |
| 87 | id: SessionId::new(), |
| 88 | parent_session_id: Some(self.id.clone()), |
| 89 | status: SessionStatus::Active, |
| 90 | objective: self.objective.clone(), |
| 91 | agent_profile: self.agent_profile.clone(), |
| 92 | model_profile: self.model_profile.clone(), |
| 93 | policy_profile: self.policy_profile.clone(), |
| 94 | messages: self.messages.clone(), |
| 95 | budget: self.budget.clone(), |
| 96 | created_at: now, |
| 97 | updated_at: now, |
| 98 | version: 1, |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | impl Session { |
| 104 | pub fn to_agent_state( |
| 105 | &self, |
| 106 | agent_id: &aien_protocol_types::AgentId, |
| 107 | model_profile: &str, |
| 108 | ) -> aien_agent_state_abi::AgentState { |
| 109 | use aien_agent_state_abi::*; |
| 110 | use aien_protocol_types::*; |
| 111 | |
| 112 | let closed_at = if self.status == SessionStatus::Closed { |
| 113 | Some(Timestamp::from_micros(self.updated_at as u64 * 1_000_000)) |
| 114 | } else { |
| 115 | None |
| 116 | }; |
| 117 | |
| 118 | AgentState { |
| 119 | abi_version: ProtocolVersion::new(1, 0), |
| 120 | agent: AgentIdentity { |
| 121 | agent_id: *agent_id, |
| 122 | name: self.agent_profile.clone(), |
| 123 | model_profile: model_profile.to_string(), |
| 124 | created_at: Timestamp::from_micros(self.created_at as u64 * 1_000_000), |
| 125 | }, |
| 126 | session: SessionState { |
| 127 | session_id: self.id.clone().into(), |
| 128 | title: self |
| 129 | .objective |
| 130 | .clone() |
| 131 | .unwrap_or_else(|| "AEGIS Session".to_string()), |
| 132 | created_at: Timestamp::from_micros(self.created_at as u64 * 1_000_000), |
| 133 | closed_at, |
| 134 | }, |
| 135 | objective: self.objective.as_ref().map(|obj| Objective { |
| 136 | objective_id: uuid::Uuid::new_v4(), |
| 137 | description: obj.clone(), |
| 138 | success_criteria: Vec::new(), |
| 139 | }), |
| 140 | context: ContextState { |
| 141 | conversation: Vec::new(), |
| 142 | pinned: Vec::new(), |
| 143 | summaries: Vec::new(), |
| 144 | cortex: Vec::new(), |
| 145 | inference: None, |
| 146 | }, |
| 147 | authority: AuthorityState { |
| 148 | principal: uuid::Uuid::new_v4(), |
| 149 | policy_profile: self.policy_profile.clone(), |
| 150 | capabilities: Vec::new(), |
| 151 | restrictions: Vec::new(), |
| 152 | containment: aien_action_protocol::ContainmentState::default(), |
| 153 | }, |
| 154 | budget: ResourceBudget { |
| 155 | max_tokens: Some(self.budget.max_total_tokens), |
| 156 | max_cost_micro_usd: None, |
| 157 | max_duration_seconds: Some(self.budget.max_wall_time_secs), |
| 158 | max_subagents: None, |
| 159 | }, |
| 160 | execution: ExecutionState { |
| 161 | active_run: None, |
| 162 | status: match self.status { |
| 163 | SessionStatus::Active => AgentExecutionStatus::Idle, |
| 164 | SessionStatus::Suspended => AgentExecutionStatus::Suspended, |
| 165 | SessionStatus::Closed => AgentExecutionStatus::Terminated, |
| 166 | }, |
| 167 | pending_actions: Vec::new(), |
| 168 | pending_approvals: Vec::new(), |
| 169 | last_event: None, |
| 170 | }, |
| 171 | parent: self.parent_session_id.as_ref().map(|p| StateRef { |
| 172 | state_id: aien_protocol_types::SessionId::from(p.clone()).0, |
| 173 | digest: Digest32::ZERO, |
| 174 | }), |
| 175 | sequence: SequenceNumber(self.version), |
| 176 | } |
| 177 | } |
| 178 | } |