xybrid-sdk 0.1.1

Developer-facing API for hybrid cloud-edge AI inference: load/run/stream models with declarative routing.
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
//! FFI-safe streaming session types for platform bindings.
//!
//! These types are designed to be wrapped by platform-specific FFI layers
//! (flutter_rust_bridge for Flutter, UniFFI for Kotlin/Swift) without modification.
//!
//! # Design Notes
//!
//! - All types use simple, FFI-compatible field types (primitives, String, Option)
//! - No `#[frb]` or UniFFI attributes - those are added by the binding layer
//! - Serializable via serde for JSON transport across FFI boundaries
//! - Matches the Flutter `streaming.rs` types for easy migration

use serde::{Deserialize, Serialize};

/// Streaming session state (FFI-safe).
///
/// Represents the current state of a streaming ASR session.
/// Used for tracking session lifecycle across FFI boundaries.
///
/// # Example
///
/// ```
/// use xybrid_sdk::streaming::FfiStreamState;
///
/// let state = FfiStreamState::Idle;
/// assert_eq!(serde_json::to_string(&state).unwrap(), "\"Idle\"");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum FfiStreamState {
    /// Session created but not started
    #[default]
    Idle,
    /// Actively receiving audio
    Streaming,
    /// Processing final audio
    Finalizing,
    /// Session completed
    Completed,
    /// Error occurred
    Error,
}

impl FfiStreamState {
    /// Check if the session is in an active state (can receive audio).
    pub fn is_active(&self) -> bool {
        matches!(self, Self::Idle | Self::Streaming)
    }

    /// Check if the session has finished (completed or error).
    pub fn is_finished(&self) -> bool {
        matches!(self, Self::Completed | Self::Error)
    }

    /// Convert to a lowercase string representation for JSON APIs.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Idle => "idle",
            Self::Streaming => "streaming",
            Self::Finalizing => "finalizing",
            Self::Completed => "completed",
            Self::Error => "error",
        }
    }
}

/// Partial transcription result (FFI-safe).
///
/// Represents an intermediate transcription result from streaming ASR.
/// These results may change as more audio is processed.
///
/// # Example
///
/// ```
/// use xybrid_sdk::streaming::FfiPartialResult;
///
/// let result = FfiPartialResult {
///     text: "Hello".to_string(),
///     is_stable: false,
///     chunk_index: 5,
/// };
/// assert!(!result.is_stable);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct FfiPartialResult {
    /// Current partial transcription text
    pub text: String,
    /// Whether this result is stable (unlikely to change with more audio)
    pub is_stable: bool,
    /// Chunk sequence number (increments with each audio chunk)
    pub chunk_index: u64,
}

impl FfiPartialResult {
    /// Create a new partial result.
    pub fn new(text: impl Into<String>, is_stable: bool, chunk_index: u64) -> Self {
        Self {
            text: text.into(),
            is_stable,
            chunk_index,
        }
    }

    /// Create a stable (finalized) partial result.
    pub fn stable(text: impl Into<String>, chunk_index: u64) -> Self {
        Self {
            text: text.into(),
            is_stable: true,
            chunk_index,
        }
    }

    /// Create an unstable (intermediate) partial result.
    pub fn unstable(text: impl Into<String>, chunk_index: u64) -> Self {
        Self {
            text: text.into(),
            is_stable: false,
            chunk_index,
        }
    }
}

/// Stream session statistics (FFI-safe).
///
/// Provides statistics about a streaming session including samples processed,
/// audio duration, and current state.
///
/// # Example
///
/// ```
/// use xybrid_sdk::streaming::{FfiStreamState, FfiStreamStats};
///
/// let stats = FfiStreamStats::default();
/// assert_eq!(stats.state, FfiStreamState::Idle);
/// assert_eq!(stats.samples_received, 0);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FfiStreamStats {
    /// Current state of the stream
    pub state: FfiStreamState,
    /// Number of audio samples received
    pub samples_received: u64,
    /// Number of audio samples processed
    pub samples_processed: u64,
    /// Number of audio chunks processed
    pub chunks_processed: u64,
    /// Total audio duration in milliseconds
    pub audio_duration_ms: u64,
}

impl Default for FfiStreamStats {
    fn default() -> Self {
        Self {
            state: FfiStreamState::Idle,
            samples_received: 0,
            samples_processed: 0,
            chunks_processed: 0,
            audio_duration_ms: 0,
        }
    }
}

/// Streaming session configuration (FFI-safe).
///
/// Configuration for creating a streaming ASR session.
/// Matches the Flutter `StreamingConfig` type for easy migration.
///
/// # Example
///
/// ```
/// use xybrid_sdk::streaming::FfiStreamingConfig;
///
/// // Create default config
/// let config = FfiStreamingConfig::default();
/// assert!(!config.enable_vad);
/// assert!((config.vad_threshold - 0.5).abs() < f32::EPSILON);
///
/// // Create config with VAD
/// let vad_config = FfiStreamingConfig::with_vad("/path/to/model", 0.6);
/// assert!(vad_config.enable_vad);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FfiStreamingConfig {
    /// Model directory path (containing model_metadata.json)
    pub model_dir: String,
    /// Enable VAD (Voice Activity Detection) for smart chunking
    pub enable_vad: bool,
    /// VAD threshold (0.0-1.0, default: 0.5)
    pub vad_threshold: f32,
    /// Language hint (e.g., "en")
    pub language: Option<String>,
}

impl Default for FfiStreamingConfig {
    fn default() -> Self {
        Self {
            model_dir: String::new(),
            enable_vad: false,
            vad_threshold: 0.5,
            language: Some("en".to_string()),
        }
    }
}

impl FfiStreamingConfig {
    /// Create a new streaming config with the given model directory.
    pub fn new(model_dir: impl Into<String>) -> Self {
        Self {
            model_dir: model_dir.into(),
            ..Default::default()
        }
    }

    /// Create a streaming config with VAD enabled.
    pub fn with_vad(model_dir: impl Into<String>, vad_threshold: f32) -> Self {
        Self {
            model_dir: model_dir.into(),
            enable_vad: true,
            vad_threshold,
            language: Some("en".to_string()),
        }
    }

    /// Set the language hint.
    pub fn with_language(mut self, language: impl Into<String>) -> Self {
        self.language = Some(language.into());
        self
    }

    /// Set VAD parameters.
    pub fn with_vad_settings(mut self, enable: bool, threshold: f32) -> Self {
        self.enable_vad = enable;
        self.vad_threshold = threshold;
        self
    }
}

impl FfiStreamStats {
    /// Create new stats in the given state.
    pub fn new(state: FfiStreamState) -> Self {
        Self {
            state,
            ..Default::default()
        }
    }

    /// Create stats with audio metrics.
    pub fn with_audio(
        state: FfiStreamState,
        samples_received: u64,
        samples_processed: u64,
        chunks_processed: u64,
        audio_duration_ms: u64,
    ) -> Self {
        Self {
            state,
            samples_received,
            samples_processed,
            chunks_processed,
            audio_duration_ms,
        }
    }

    /// Calculate the processing ratio (processed / received).
    /// Returns 1.0 if no samples received.
    pub fn processing_ratio(&self) -> f64 {
        if self.samples_received == 0 {
            1.0
        } else {
            self.samples_processed as f64 / self.samples_received as f64
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_stream_state_default() {
        let state = FfiStreamState::default();
        assert_eq!(state, FfiStreamState::Idle);
    }

    #[test]
    fn test_stream_state_is_active() {
        assert!(FfiStreamState::Idle.is_active());
        assert!(FfiStreamState::Streaming.is_active());
        assert!(!FfiStreamState::Finalizing.is_active());
        assert!(!FfiStreamState::Completed.is_active());
        assert!(!FfiStreamState::Error.is_active());
    }

    #[test]
    fn test_stream_state_is_finished() {
        assert!(!FfiStreamState::Idle.is_finished());
        assert!(!FfiStreamState::Streaming.is_finished());
        assert!(!FfiStreamState::Finalizing.is_finished());
        assert!(FfiStreamState::Completed.is_finished());
        assert!(FfiStreamState::Error.is_finished());
    }

    #[test]
    fn test_stream_state_as_str() {
        assert_eq!(FfiStreamState::Idle.as_str(), "idle");
        assert_eq!(FfiStreamState::Streaming.as_str(), "streaming");
        assert_eq!(FfiStreamState::Finalizing.as_str(), "finalizing");
        assert_eq!(FfiStreamState::Completed.as_str(), "completed");
        assert_eq!(FfiStreamState::Error.as_str(), "error");
    }

    #[test]
    fn test_stream_state_serialization() {
        let state = FfiStreamState::Streaming;
        let json = serde_json::to_string(&state).unwrap();
        assert_eq!(json, "\"Streaming\"");

        let deserialized: FfiStreamState = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, state);
    }

    #[test]
    fn test_partial_result_default() {
        let result = FfiPartialResult::default();
        assert!(result.text.is_empty());
        assert!(!result.is_stable);
        assert_eq!(result.chunk_index, 0);
    }

    #[test]
    fn test_partial_result_new() {
        let result = FfiPartialResult::new("hello", true, 5);
        assert_eq!(result.text, "hello");
        assert!(result.is_stable);
        assert_eq!(result.chunk_index, 5);
    }

    #[test]
    fn test_partial_result_stable() {
        let result = FfiPartialResult::stable("world", 10);
        assert_eq!(result.text, "world");
        assert!(result.is_stable);
        assert_eq!(result.chunk_index, 10);
    }

    #[test]
    fn test_partial_result_unstable() {
        let result = FfiPartialResult::unstable("foo", 3);
        assert_eq!(result.text, "foo");
        assert!(!result.is_stable);
        assert_eq!(result.chunk_index, 3);
    }

    #[test]
    fn test_partial_result_serialization() {
        let result = FfiPartialResult::new("test", true, 7);
        let json = serde_json::to_string(&result).unwrap();
        assert!(json.contains("\"text\":\"test\""));
        assert!(json.contains("\"is_stable\":true"));
        assert!(json.contains("\"chunk_index\":7"));

        let deserialized: FfiPartialResult = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.text, result.text);
        assert_eq!(deserialized.is_stable, result.is_stable);
        assert_eq!(deserialized.chunk_index, result.chunk_index);
    }

    #[test]
    fn test_stream_stats_default() {
        let stats = FfiStreamStats::default();
        assert_eq!(stats.state, FfiStreamState::Idle);
        assert_eq!(stats.samples_received, 0);
        assert_eq!(stats.samples_processed, 0);
        assert_eq!(stats.chunks_processed, 0);
        assert_eq!(stats.audio_duration_ms, 0);
    }

    #[test]
    fn test_stream_stats_new() {
        let stats = FfiStreamStats::new(FfiStreamState::Streaming);
        assert_eq!(stats.state, FfiStreamState::Streaming);
        assert_eq!(stats.samples_received, 0);
    }

    #[test]
    fn test_stream_stats_with_audio() {
        let stats = FfiStreamStats::with_audio(FfiStreamState::Streaming, 16000, 15000, 5, 1000);
        assert_eq!(stats.state, FfiStreamState::Streaming);
        assert_eq!(stats.samples_received, 16000);
        assert_eq!(stats.samples_processed, 15000);
        assert_eq!(stats.chunks_processed, 5);
        assert_eq!(stats.audio_duration_ms, 1000);
    }

    #[test]
    fn test_stream_stats_processing_ratio() {
        // No samples - should return 1.0
        let stats = FfiStreamStats::default();
        assert!((stats.processing_ratio() - 1.0).abs() < f64::EPSILON);

        // With samples
        let stats = FfiStreamStats::with_audio(FfiStreamState::Streaming, 100, 80, 1, 10);
        assert!((stats.processing_ratio() - 0.8).abs() < f64::EPSILON);
    }

    #[test]
    fn test_stream_stats_serialization() {
        let stats = FfiStreamStats::with_audio(FfiStreamState::Completed, 1000, 1000, 10, 5000);
        let json = serde_json::to_string(&stats).unwrap();
        assert!(json.contains("\"state\":\"Completed\""));
        assert!(json.contains("\"samples_received\":1000"));
        assert!(json.contains("\"audio_duration_ms\":5000"));

        let deserialized: FfiStreamStats = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.state, stats.state);
        assert_eq!(deserialized.samples_received, stats.samples_received);
        assert_eq!(deserialized.audio_duration_ms, stats.audio_duration_ms);
    }

    // ========================================================================
    // FfiStreamingConfig tests (US-011)
    // ========================================================================

    #[test]
    fn test_streaming_config_default() {
        let config = FfiStreamingConfig::default();
        assert!(config.model_dir.is_empty());
        assert!(!config.enable_vad);
        assert!((config.vad_threshold - 0.5).abs() < f32::EPSILON);
        assert_eq!(config.language, Some("en".to_string()));
    }

    #[test]
    fn test_streaming_config_new() {
        let config = FfiStreamingConfig::new("/path/to/model");
        assert_eq!(config.model_dir, "/path/to/model");
        assert!(!config.enable_vad);
        assert!((config.vad_threshold - 0.5).abs() < f32::EPSILON);
        assert_eq!(config.language, Some("en".to_string()));
    }

    #[test]
    fn test_streaming_config_with_vad() {
        let config = FfiStreamingConfig::with_vad("/path/to/model", 0.7);
        assert_eq!(config.model_dir, "/path/to/model");
        assert!(config.enable_vad);
        assert!((config.vad_threshold - 0.7).abs() < f32::EPSILON);
        assert_eq!(config.language, Some("en".to_string()));
    }

    #[test]
    fn test_streaming_config_with_language() {
        let config = FfiStreamingConfig::new("/path/to/model").with_language("de");
        assert_eq!(config.language, Some("de".to_string()));
    }

    #[test]
    fn test_streaming_config_with_vad_settings() {
        let config = FfiStreamingConfig::new("/path/to/model").with_vad_settings(true, 0.8);
        assert!(config.enable_vad);
        assert!((config.vad_threshold - 0.8).abs() < f32::EPSILON);
    }

    #[test]
    fn test_streaming_config_serialization() {
        let config = FfiStreamingConfig {
            model_dir: "/test/model".to_string(),
            enable_vad: true,
            vad_threshold: 0.6,
            language: Some("fr".to_string()),
        };
        let json = serde_json::to_string(&config).unwrap();
        assert!(json.contains("\"model_dir\":\"/test/model\""));
        assert!(json.contains("\"enable_vad\":true"));
        assert!(json.contains("\"vad_threshold\":0.6"));
        assert!(json.contains("\"language\":\"fr\""));

        let deserialized: FfiStreamingConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.model_dir, config.model_dir);
        assert_eq!(deserialized.enable_vad, config.enable_vad);
        assert!((deserialized.vad_threshold - config.vad_threshold).abs() < f32::EPSILON);
        assert_eq!(deserialized.language, config.language);
    }
}