trustformers-mobile 0.1.1

Mobile deployment support for TrustformeRS (iOS, Android)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! App State Management Types
//!
//! This module contains types for managing app lifecycle states, transitions,
//! and related context information.

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::time::Instant;

/// App states
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AppState {
    /// App is starting up
    Launching,
    /// App is active and in foreground
    Active,
    /// App is in background but still executing
    Background,
    /// App is inactive (transitioning)
    Inactive,
    /// App is suspended (not executing)
    Suspended,
    /// App is terminating
    Terminating,
    /// Unknown state
    Unknown,
}

/// State transitions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum StateTransition {
    LaunchToActive,
    ActiveToBackground,
    BackgroundToActive,
    ActiveToInactive,
    InactiveToActive,
    BackgroundToSuspended,
    SuspendedToBackground,
    AnyToTerminating,
}

/// State transition event
#[derive(Debug, Clone)]
pub struct StateTransitionEvent {
    pub from_state: AppState,
    pub to_state: AppState,
    pub transition: StateTransition,
    pub timestamp: Instant,
    pub reason: TransitionReason,
    pub context: TransitionContext,
}

/// Reasons for state transitions
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TransitionReason {
    UserAction,
    SystemRequest,
    LowMemory,
    LowBattery,
    ThermalPressure,
    NetworkInterruption,
    Timeout,
    Error,
    SystemSuspend,
    SystemResume,
}

/// Context information for state transitions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransitionContext {
    /// Available memory (MB)
    pub available_memory_mb: usize,
    /// Battery level (%)
    pub battery_level_percent: u8,
    /// CPU temperature (°C)
    pub cpu_temperature_celsius: f32,
    /// Network connectivity status
    pub network_connected: bool,
    /// Active background tasks count
    pub active_background_tasks: usize,
    /// Time since last user interaction (seconds)
    pub time_since_user_interaction_seconds: u64,
    /// Foreground app duration (seconds)
    pub foreground_duration_seconds: u64,
    /// Background app duration (seconds)
    pub background_duration_seconds: u64,
    /// System pressure indicators
    pub system_pressure: SystemPressureIndicators,
    /// Resource usage at transition
    pub resource_usage: ResourceUsageSnapshot,
}

/// System pressure indicators
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemPressureIndicators {
    /// Memory pressure level
    pub memory_pressure: crate::lifecycle::config::MemoryPressureLevel,
    /// Thermal state
    pub thermal_state: crate::lifecycle::config::ThermalLevel,
    /// Battery state
    pub battery_state: crate::battery::BatteryLevel,
    /// Network quality
    pub network_quality: NetworkQuality,
}

/// Network quality levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NetworkQuality {
    Excellent,
    Good,
    Fair,
    Poor,
    Disconnected,
}

/// Resource usage snapshot
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceUsageSnapshot {
    /// CPU usage percentage (0-100)
    pub cpu_usage_percent: f32,
    /// Memory usage in MB
    pub memory_usage_mb: usize,
    /// GPU usage percentage (0-100)
    pub gpu_usage_percent: Option<f32>,
    /// Network usage in MB/s
    pub network_usage_mbps: f32,
    /// Storage I/O in MB/s
    pub storage_io_mbps: f32,
    /// Active model count
    pub active_models: usize,
    /// Inference queue size
    pub inference_queue_size: usize,
}

/// App checkpoint for state persistence
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppCheckpoint {
    /// App state at checkpoint
    pub app_state: AppState,
    /// Timestamp of checkpoint
    pub timestamp: u64, // Using u64 for serialization
    /// Model states
    pub model_states: Vec<ModelState>,
    /// Cache states
    pub cache_states: HashMap<String, CacheState>,
    /// User session state
    pub user_session: UserSessionState,
}

/// Model state information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelState {
    /// Model identifier
    pub model_id: String,
    /// Model load state
    pub load_state: ModelLoadState,
    /// Model configuration
    pub configuration: ModelConfiguration,
    /// Performance metrics
    pub performance_metrics: ModelPerformanceMetrics,
    /// Last access timestamp
    pub last_access_timestamp: u64,
    /// Usage count
    pub usage_count: usize,
}

/// Model load states
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ModelLoadState {
    Unloaded,
    Loading,
    Loaded,
    Error,
    Cached,
    Compressed,
    Offloaded,
}

/// Cache state information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheState {
    /// Cache size in bytes
    pub size_bytes: usize,
    /// Number of cached items
    pub item_count: usize,
    /// Last access timestamp
    pub last_access_timestamp: u64,
    /// Hit rate percentage
    pub hit_rate_percent: f32,
}

/// Model configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelConfiguration {
    /// Model precision
    pub precision: ModelPrecision,
    /// Optimization level
    pub optimization_level: OptimizationLevel,
    /// Backend in use
    pub backend: ModelBackend,
}

/// Model precision levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ModelPrecision {
    FP32,
    FP16,
    INT8,
    INT4,
    Dynamic,
}

/// Model optimization levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OptimizationLevel {
    None,
    Basic,
    Aggressive,
    UltraLowMemory,
}

/// Model backend types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ModelBackend {
    CPU,
    CoreML,
    NNAPI,
    Metal,
    Vulkan,
    Custom,
}

/// Model performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelPerformanceMetrics {
    /// Average inference time (ms)
    pub avg_inference_time_ms: f32,
    /// Memory usage (MB)
    pub memory_usage_mb: usize,
    /// Throughput (inferences/second)
    pub throughput_per_second: f32,
    /// Error rate percentage
    pub error_rate_percent: f32,
}

/// Task state information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskState {
    /// Task identifier
    pub task_id: String,
    /// Task type
    pub task_type: crate::lifecycle::config::TaskType,
    /// Current status
    pub status: LifecycleTaskStatus,
    /// Priority level
    pub priority: crate::lifecycle::config::TaskPriority,
    /// Progress percentage (0-100)
    pub progress_percent: u8,
    /// Estimated completion time (seconds)
    pub estimated_completion_seconds: Option<u64>,
}

/// Task execution status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LifecycleTaskStatus {
    Pending,
    Running,
    Paused,
    Completed,
    Failed,
    Cancelled,
}

/// User session state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserSessionState {
    /// Session start timestamp
    pub session_start_timestamp: u64,
    /// Total interaction time (seconds)
    pub total_interaction_time_seconds: u64,
    /// Recent user interactions
    pub recent_interactions: Vec<UserInteraction>,
    /// Session statistics
    pub session_stats: SessionStatistics,
}

/// User interaction record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserInteraction {
    /// Interaction timestamp
    pub timestamp: u64,
    /// Type of interaction
    pub interaction_type: InteractionType,
    /// Interaction outcome
    pub outcome: InteractionOutcome,
    /// Duration of interaction (ms)
    pub duration_ms: u64,
}

/// Types of user interactions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum InteractionType {
    Touch,
    Voice,
    Gesture,
    TextInput,
    ModelInference,
    Navigation,
    Settings,
}

/// Interaction outcomes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum InteractionOutcome {
    Success,
    Failure,
    Cancelled,
    Timeout,
    Error,
}

/// Session statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionStatistics {
    /// Total inferences performed
    pub total_inferences: usize,
    /// Total models loaded
    pub total_models_loaded: usize,
    /// Total background tasks executed
    pub total_background_tasks: usize,
    /// Average response time (ms)
    pub avg_response_time_ms: f32,
    /// Error count
    pub error_count: usize,
}

/// System context information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemContext {
    /// Current timestamp
    pub timestamp: u64,
    /// Device information
    pub device_info: SystemDeviceInfo,
    /// Resource availability
    pub resource_availability: ResourceAvailability,
    /// Network status
    pub network_status: NetworkStatus,
    /// Power status
    pub power_status: PowerStatus,
    /// Thermal status
    pub thermal_status: ThermalStatus,
    /// Active processes
    pub active_processes: ProcessInfo,
    /// System load
    pub system_load: SystemLoad,
}

/// System device information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemDeviceInfo {
    /// Device model
    pub device_model: String,
    /// OS version
    pub os_version: String,
    /// Available cores
    pub cpu_cores: usize,
    /// Total RAM (MB)
    pub total_ram_mb: usize,
    /// Available storage (MB)
    pub available_storage_mb: usize,
}

/// Resource availability information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceAvailability {
    /// Available CPU percentage
    pub available_cpu_percent: f32,
    /// Available memory (MB)
    pub available_memory_mb: usize,
    /// Available GPU percentage
    pub available_gpu_percent: Option<f32>,
    /// Available network bandwidth (Mbps)
    pub available_network_mbps: f32,
}

/// Network status information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkStatus {
    /// Network connectivity
    pub connected: bool,
    /// Connection type
    pub connection_type: NetworkConnectionType,
    /// Signal strength (0-100)
    pub signal_strength_percent: u8,
    /// Network quality
    pub quality: NetworkQuality,
    /// Data usage (MB)
    pub data_usage_mb: f32,
}

/// Network connection types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NetworkConnectionType {
    WiFi,
    Cellular,
    Ethernet,
    Bluetooth,
    Unknown,
}

/// Power status information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PowerStatus {
    /// Battery level (%)
    pub battery_level_percent: u8,
    /// Charging status
    pub charging_status: ChargingStatus,
    /// Power save mode enabled
    pub power_save_mode: bool,
    /// Estimated battery life (minutes)
    pub estimated_battery_life_minutes: Option<u32>,
}

/// Charging status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChargingStatus {
    Charging,
    Discharging,
    Full,
    NotCharging,
    Unknown,
}

/// Thermal status information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThermalStatus {
    /// CPU temperature (°C)
    pub cpu_temperature_celsius: f32,
    /// GPU temperature (°C)
    pub gpu_temperature_celsius: Option<f32>,
    /// Thermal state
    pub thermal_state: crate::lifecycle::config::ThermalLevel,
    /// Throttling active
    pub throttling_active: bool,
}

/// Process information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessInfo {
    /// Total processes
    pub total_processes: usize,
    /// Active processes
    pub active_processes: usize,
    /// Background processes
    pub background_processes: usize,
    /// Memory usage by processes (MB)
    pub process_memory_usage_mb: usize,
}

/// System load information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemLoad {
    /// 1-minute load average
    pub load_1min: f32,
    /// 5-minute load average
    pub load_5min: f32,
    /// 15-minute load average
    pub load_15min: f32,
    /// CPU utilization (%)
    pub cpu_utilization_percent: f32,
    /// Memory utilization (%)
    pub memory_utilization_percent: f32,
}

/// App state manager for lifecycle transitions
pub struct AppStateManager {
    current_state: AppState,
    previous_state: AppState,
    state_history: VecDeque<StateTransition>,
    state_listeners: Vec<Box<dyn StateListener>>,
    transition_handlers: HashMap<StateTransition, Box<dyn TransitionHandler>>,
}

/// Trait for state change listeners
pub trait StateListener: Send + Sync {
    fn on_state_change(&self, event: &StateTransitionEvent);
}

/// Trait for state transition handlers
pub trait TransitionHandler: Send + Sync {
    fn handle_transition(
        &self,
        event: &StateTransitionEvent,
    ) -> Result<(), Box<dyn std::error::Error>>;
}

impl AppStateManager {
    /// Create new state manager
    pub fn new() -> Self {
        Self {
            current_state: AppState::Unknown,
            previous_state: AppState::Unknown,
            state_history: VecDeque::new(),
            state_listeners: Vec::new(),
            transition_handlers: HashMap::new(),
        }
    }

    /// Get current app state
    pub fn current_state(&self) -> AppState {
        self.current_state
    }

    /// Get previous app state
    pub fn previous_state(&self) -> AppState {
        self.previous_state
    }

    /// Transition to new state
    pub fn transition_to_state(
        &mut self,
        new_state: AppState,
        reason: TransitionReason,
        context: TransitionContext,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let transition = self.determine_transition(self.current_state, new_state)?;

        let event = StateTransitionEvent {
            from_state: self.current_state,
            to_state: new_state,
            transition,
            timestamp: Instant::now(),
            reason,
            context,
        };

        // Execute transition handler if registered
        if let Some(handler) = self.transition_handlers.get(&transition) {
            handler.handle_transition(&event)?;
        }

        // Update state
        self.previous_state = self.current_state;
        self.current_state = new_state;
        self.state_history.push_back(transition);

        // Keep history bounded
        if self.state_history.len() > 100 {
            self.state_history.pop_front();
        }

        // Notify listeners
        for listener in &self.state_listeners {
            listener.on_state_change(&event);
        }

        Ok(())
    }

    /// Register state change listener
    pub fn add_state_listener(&mut self, listener: Box<dyn StateListener>) {
        self.state_listeners.push(listener);
    }

    /// Register transition handler
    pub fn add_transition_handler(
        &mut self,
        transition: StateTransition,
        handler: Box<dyn TransitionHandler>,
    ) {
        self.transition_handlers.insert(transition, handler);
    }

    /// Get state transition history
    pub fn get_state_history(&self) -> &VecDeque<StateTransition> {
        &self.state_history
    }

    /// Determine transition type between states
    fn determine_transition(
        &self,
        from: AppState,
        to: AppState,
    ) -> Result<StateTransition, Box<dyn std::error::Error>> {
        match (from, to) {
            (AppState::Launching, AppState::Active) => Ok(StateTransition::LaunchToActive),
            (AppState::Active, AppState::Background) => Ok(StateTransition::ActiveToBackground),
            (AppState::Background, AppState::Active) => Ok(StateTransition::BackgroundToActive),
            (AppState::Active, AppState::Inactive) => Ok(StateTransition::ActiveToInactive),
            (AppState::Inactive, AppState::Active) => Ok(StateTransition::InactiveToActive),
            (AppState::Background, AppState::Suspended) => {
                Ok(StateTransition::BackgroundToSuspended)
            },
            (AppState::Suspended, AppState::Background) => {
                Ok(StateTransition::SuspendedToBackground)
            },
            (_, AppState::Terminating) => Ok(StateTransition::AnyToTerminating),
            _ => Err(format!("Invalid state transition from {:?} to {:?}", from, to).into()),
        }
    }
}

impl Default for AppStateManager {
    fn default() -> Self {
        Self::new()
    }
}