1pub const MAX_ENCRYPTED_CREDENTIALS_BYTES: usize = 4096;
25
26pub const MAX_AGENT_PAYLOAD_BYTES: usize = 65536;
29
30use crate::{
31 types::identifiers::{AccountId, DeviceId, SessionId},
32 AuraResult as Result, CapabilityName,
33};
34use async_trait::async_trait;
35use serde::{Deserialize, Serialize};
36use std::collections::HashMap;
37
38#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct DeviceInfo {
41 pub device_id: DeviceId,
43 pub account_id: Option<AccountId>,
45 pub device_name: String,
47 pub hardware_security: bool,
49 pub attestation_available: bool,
51 pub last_sync: Option<u64>,
53 pub storage_usage: u64,
55 pub storage_limit: u64,
57}
58
59#[async_trait]
62pub trait AgentEffects: Send + Sync {
63 async fn initialize(&self) -> Result<()>;
65
66 async fn get_device_info(&self) -> Result<DeviceInfo>;
68
69 async fn shutdown(&self) -> Result<()>;
71
72 async fn sync_distributed_state(&self) -> Result<()>;
74
75 async fn health_check(&self) -> Result<AgentHealthStatus>;
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
81pub struct AgentHealthStatus {
82 pub overall_status: HealthStatus,
83 pub storage_status: HealthStatus,
84 pub network_status: HealthStatus,
85 pub authentication_status: HealthStatus,
86 pub session_status: HealthStatus,
87 pub last_check: u64,
88}
89
90#[derive(Debug, Clone, Serialize, Deserialize)]
91pub enum HealthStatus {
92 Healthy,
93 Degraded { reason: String },
94 Unhealthy { error: String },
95}
96
97#[async_trait]
100pub trait DeviceStorageEffects: Send + Sync {
101 async fn store_credential(&self, key: &str, credential: &[u8]) -> Result<()>;
103
104 async fn retrieve_credential(&self, key: &str) -> Result<Option<Vec<u8>>>;
106
107 async fn delete_credential(&self, key: &str) -> Result<()>;
109
110 async fn list_credentials(&self) -> Result<Vec<String>>;
112
113 async fn store_device_config(&self, config: &[u8]) -> Result<()>;
115
116 async fn retrieve_device_config(&self) -> Result<Option<Vec<u8>>>;
118
119 async fn backup_credentials(&self) -> Result<CredentialBackup>;
121
122 async fn restore_credentials(&self, backup: &CredentialBackup) -> Result<()>;
124
125 async fn secure_wipe(&self) -> Result<()>;
127}
128
129#[derive(Debug, Clone, Serialize, Deserialize)]
131pub struct CredentialBackup {
132 pub device_id: DeviceId,
133 pub timestamp: u64,
134 pub encrypted_credentials: Vec<u8>,
135 pub backup_hash: [u8; 32],
136 pub metadata: HashMap<String, String>,
137}
138
139#[async_trait]
141pub trait AuthenticationEffects: Send + Sync {
142 async fn authenticate_device(&self) -> Result<AuthenticationResult>;
144
145 async fn is_authenticated(&self) -> Result<bool>;
147
148 async fn lock_device(&self) -> Result<()>;
150
151 async fn get_auth_methods(&self) -> Result<Vec<AuthMethod>>;
153
154 async fn enroll_biometric(&self, biometric_type: BiometricType) -> Result<()>;
156
157 async fn remove_biometric(&self, biometric_type: BiometricType) -> Result<()>;
159
160 async fn verify_capability(&self, capability: &[u8]) -> Result<bool>;
162
163 async fn generate_attestation(&self) -> Result<Vec<u8>>;
165}
166
167#[derive(Debug, Clone, Serialize, Deserialize)]
169pub struct AuthenticationResult {
170 pub success: bool,
171 pub method_used: Option<AuthMethod>,
172 pub session_token: Option<Vec<u8>>,
173 pub expires_at: Option<u64>,
174 pub error: Option<String>,
175}
176
177#[derive(Debug, Clone, Serialize, Deserialize)]
179pub enum AuthMethod {
180 Biometric(BiometricType),
181 Pin,
182 Password,
183 HardwareKey,
184 DeviceCredential,
185}
186
187#[derive(Debug, Clone, Serialize, Deserialize)]
189pub enum BiometricType {
190 Fingerprint,
191 FaceId,
192 TouchId,
193 VoiceId,
194}
195
196#[async_trait]
200pub trait SessionManagementEffects: Send + Sync {
201 async fn create_session(&self, session_type: SessionType) -> Result<SessionId>;
203
204 async fn create_choreographic_session(
206 &self,
207 session_type: SessionType,
208 participants: Vec<DeviceId>,
209 choreography_config: ChoreographyConfig,
210 ) -> Result<SessionId>;
211
212 async fn join_session(&self, session_id: SessionId) -> Result<SessionHandle>;
214
215 async fn join_choreographic_session(
217 &self,
218 session_id: SessionId,
219 role: ChoreographicRole,
220 ) -> Result<SessionHandle>;
221
222 async fn leave_session(&self, session_id: SessionId) -> Result<()>;
224
225 async fn end_session(&self, session_id: SessionId) -> Result<()>;
227
228 async fn list_active_sessions(&self) -> Result<Vec<SessionInfo>>;
230
231 async fn get_session_status(&self, session_id: SessionId) -> Result<SessionStatus>;
233
234 async fn send_choreographic_message(
236 &self,
237 session_id: SessionId,
238 message_type: &str,
239 payload: &[u8],
240 target_role: Option<ChoreographicRole>,
241 ) -> Result<()>;
242
243 async fn receive_choreographic_messages(
245 &self,
246 session_id: SessionId,
247 role_filter: Option<ChoreographicRole>,
248 ) -> Result<Vec<ChoreographicMessage>>;
249
250 async fn get_choreography_phase(&self, session_id: SessionId) -> Result<Option<String>>;
252
253 async fn update_choreography_state(
255 &self,
256 session_id: SessionId,
257 phase: &str,
258 state_data: &[u8],
259 ) -> Result<()>;
260
261 async fn validate_choreographic_message(
263 &self,
264 session_id: SessionId,
265 message: &ChoreographicMessage,
266 ) -> Result<bool>;
267}
268
269#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
271pub enum SessionType {
272 Recovery,
273 KeyRotation,
274 ThresholdOperation,
275 Coordination,
276 Backup,
277 Invitation,
278 Rendezvous,
279 Sync,
280 Custom(String),
281}
282
283#[derive(Debug, Clone, Serialize, Deserialize)]
285pub struct SessionHandle {
286 pub session_id: SessionId,
287 pub role: SessionRole,
288 pub participants: Vec<DeviceId>,
289 pub created_at: u64,
290}
291
292#[derive(Debug, Clone, Serialize, Deserialize)]
294pub enum SessionRole {
295 Coordinator,
296 Initiator,
297 Participant,
298 Approver,
299 Observer,
300}
301
302#[derive(Debug, Clone, Serialize, Deserialize)]
304pub struct SessionInfo {
305 pub session_id: SessionId,
306 pub session_type: SessionType,
307 pub role: SessionRole,
308 pub participants: Vec<DeviceId>,
309 pub status: SessionStatus,
310 pub created_at: u64,
311 pub updated_at: u64,
312 pub timeout_at: Option<u64>,
313 pub operation: Option<String>,
314 pub metadata: std::collections::HashMap<String, String>,
315}
316
317#[derive(Debug, Clone, Serialize, Deserialize)]
319pub enum SessionStatus {
320 Created,
321 Active,
322 Paused,
323 WaitingForApprovals,
324 Completed,
325 Failed { error: String },
326 Expired,
327 Cancelled,
328 TimedOut,
329}
330
331#[derive(Debug, Clone, Serialize, Deserialize)]
333pub struct SessionMessage {
334 pub from: DeviceId,
335 pub to: Option<DeviceId>, pub timestamp: u64,
337 pub message_type: String,
338 pub payload: Vec<u8>,
339}
340
341#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
343pub struct ChoreographicRole {
344 pub device_id: uuid::Uuid,
345 pub role_index: u32,
346}
347
348impl ChoreographicRole {
349 #[must_use]
350 pub fn new(device_id: uuid::Uuid, role_index: u32) -> Self {
351 Self {
352 device_id,
353 role_index,
354 }
355 }
356}
357
358#[derive(Debug, Clone, Serialize, Deserialize)]
360pub struct ChoreographyConfig {
361 pub namespace: String,
363 pub guard_capabilities: Vec<CapabilityName>,
365 pub flow_budget: Option<u64>,
367 pub journal_facts: Vec<String>,
369 pub timeout_seconds: u64,
371 pub max_retries: u32,
373}
374
375#[derive(Debug, Clone, Serialize, Deserialize)]
377pub struct ChoreographicMessage {
378 pub from: DeviceId,
379 pub to: Option<DeviceId>,
380 pub source_role: ChoreographicRole,
381 pub target_role: Option<ChoreographicRole>,
382 pub protocol_namespace: String,
383 pub phase: String,
384 pub message_type: String,
385 pub payload: Vec<u8>,
386 pub timestamp: u64,
387 pub sequence_number: u64,
388 pub guard_capabilities: Vec<CapabilityName>,
389}
390
391#[async_trait]
393pub trait ConfigurationEffects: Send + Sync {
394 async fn get_device_config(&self) -> Result<DeviceConfig>;
396
397 async fn update_device_config(&self, config: &DeviceConfig) -> Result<()>;
399
400 async fn reset_to_defaults(&self) -> Result<()>;
402
403 async fn export_config(&self) -> Result<Vec<u8>>;
405
406 async fn import_config(&self, config_data: &[u8]) -> Result<()>;
408
409 async fn validate_config(&self, config: &DeviceConfig) -> Result<Vec<ConfigValidationError>>;
411
412 async fn get_config_json(&self, key: &str) -> Result<Option<serde_json::Value>>;
414
415 async fn set_config_json(&self, key: &str, value: &serde_json::Value) -> Result<()>;
417
418 async fn get_all_config(&self) -> Result<std::collections::HashMap<String, serde_json::Value>>;
420}
421
422#[derive(Debug, Clone, Serialize, Deserialize)]
424pub struct DeviceConfig {
425 pub device_name: String,
426 pub auto_lock_timeout: u32, pub biometric_enabled: bool,
428 pub backup_enabled: bool,
429 pub sync_interval: u32, pub max_storage_size: u64, pub network_timeout: u32, pub log_level: String,
433 pub custom_settings: HashMap<String, serde_json::Value>,
434}
435
436impl Default for DeviceConfig {
437 fn default() -> Self {
438 Self {
439 device_name: "Aura Device".to_string(),
440 auto_lock_timeout: 300, biometric_enabled: false,
442 backup_enabled: true,
443 sync_interval: 3600, max_storage_size: 100 * 1024 * 1024, network_timeout: 5000, log_level: "info".to_string(),
447 custom_settings: HashMap::new(),
448 }
449 }
450}
451
452#[derive(Debug, Clone, Serialize, Deserialize)]
454pub struct ConfigValidationError {
455 pub field: String,
456 pub error: String,
457 pub suggested_value: Option<serde_json::Value>,
458}
459
460#[derive(Debug, Clone, Serialize, Deserialize)]
462pub enum ConfigError {
463 NotFound(String),
465 InvalidJson(String),
467 SerializationError(String),
469 StorageError(String),
471 PermissionDenied(String),
473}
474
475impl std::fmt::Display for ConfigError {
476 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
477 match self {
478 ConfigError::NotFound(key) => write!(f, "Configuration key not found: {key}"),
479 ConfigError::InvalidJson(msg) => write!(f, "Invalid JSON value: {msg}"),
480 ConfigError::SerializationError(msg) => write!(f, "Serialization error: {msg}"),
481 ConfigError::StorageError(msg) => write!(f, "Storage error: {msg}"),
482 ConfigError::PermissionDenied(msg) => write!(f, "Permission denied: {msg}"),
483 }
484 }
485}
486
487impl std::error::Error for ConfigError {}