vona-mlx 0.2.0

Native MLX audio adapter hooks for Vona on Apple Silicon
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{path::PathBuf, sync::Arc};
use thiserror::Error;
use vona_core::{
    AudioInputFrame, AudioOutputFrame, AudioProcessingError, AudioStreamingTranscriber,
    AudioSynthesisConfig, AudioSynthesizer, AudioTranscriber, BackendCapabilities, BackendError,
    BackendStep, ControlEvent, ExternalContextEvent, SessionConfig, SpeechToSpeechBackend,
    StreamingTranscriptKind, StreamingTranscriptUpdate, StreamingTranscriptionConfig,
    StreamingTranscriptionSession,
};

#[cfg(feature = "native-mlx")]
pub type MlxArray = mlx_rs::Array;

#[cfg(not(feature = "native-mlx"))]
#[derive(Debug, Clone)]
pub struct MlxArray {
    samples: Vec<f32>,
    shape: Vec<i32>,
}

#[cfg(not(feature = "native-mlx"))]
impl MlxArray {
    fn from_samples(samples: &[f32]) -> Result<Self, MlxAudioError> {
        let len = i32::try_from(samples.len()).map_err(|_| {
            MlxAudioError::InvalidInput("audio frame is too large for mlx shape".to_string())
        })?;
        Ok(Self {
            samples: samples.to_vec(),
            shape: vec![len],
        })
    }

    pub fn eval(&self) -> Result<(), MlxAudioError> {
        Ok(())
    }

    pub fn as_slice<T>(&self) -> &[f32] {
        let _ = std::marker::PhantomData::<T>;
        &self.samples
    }

    pub fn shape(&self) -> &[i32] {
        &self.shape
    }
}

pub const DEFAULT_STT_MODEL_ID: &str = "distil-whisper/distil-large-v3";
pub const DEFAULT_TTS_MODEL_ID: &str = "mlx-community/Qwen3-TTS-12Hz-0.6B-Base-bf16";
pub const DEFAULT_SAMPLE_RATE_HZ: u32 = 24_000;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MlxAudioConfig {
    pub stt_model_id: String,
    pub tts_model_id: String,
    pub output_sample_rate_hz: u32,
}

impl Default for MlxAudioConfig {
    fn default() -> Self {
        Self {
            stt_model_id: DEFAULT_STT_MODEL_ID.to_string(),
            tts_model_id: DEFAULT_TTS_MODEL_ID.to_string(),
            output_sample_rate_hz: DEFAULT_SAMPLE_RATE_HZ,
        }
    }
}

impl MlxAudioConfig {
    pub fn from_env() -> Self {
        Self {
            stt_model_id: std::env::var("VONA_MLX_STT_MODEL")
                .unwrap_or_else(|_| DEFAULT_STT_MODEL_ID.to_string()),
            tts_model_id: std::env::var("VONA_MLX_TTS_MODEL")
                .unwrap_or_else(|_| DEFAULT_TTS_MODEL_ID.to_string()),
            output_sample_rate_hz: std::env::var("VONA_MLX_OUTPUT_SAMPLE_RATE")
                .ok()
                .and_then(|value| value.parse().ok())
                .unwrap_or(DEFAULT_SAMPLE_RATE_HZ),
        }
    }
}

pub trait MlxSpeechModel: Send + Sync {
    fn transcribe(&self, audio: &MlxArray, sample_rate_hz: u32) -> Result<String, MlxAudioError>;
    fn synthesize(&self, text: &str, sample_rate_hz: u32) -> Result<MlxArray, MlxAudioError>;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MlxModelKind {
    Speech,
    WhisperSpeech,
    Qwen3TtsSpeech,
    TransformerText,
    Qwen3NextText,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MlxModelLoadRequest {
    pub model_id: String,
    pub local_path: Option<PathBuf>,
    pub kind: MlxModelKind,
}

impl MlxModelLoadRequest {
    pub fn local(
        kind: MlxModelKind,
        model_id: impl Into<String>,
        local_path: impl Into<PathBuf>,
    ) -> Self {
        Self {
            model_id: model_id.into(),
            local_path: Some(local_path.into()),
            kind,
        }
    }
}

pub enum LoadedMlxModel {
    Speech(Arc<dyn MlxSpeechModel>),
    #[cfg(feature = "mlx-models-loader")]
    TransformerText {
        model: mlx_models::Model,
        tokenizer: tokenizers::Tokenizer,
    },
    #[cfg(feature = "mlx-models-loader")]
    Qwen3NextText {
        model: mlx_models::Qwen3NextCausalLM,
        tokenizer: tokenizers::Tokenizer,
    },
}

pub trait MlxModelLoader: Send + Sync {
    fn load_model(&self, request: MlxModelLoadRequest) -> Result<LoadedMlxModel, MlxAudioError>;
}

#[derive(Debug, Clone, Copy, Default)]
pub struct MlxModelsLoader;

impl MlxModelLoader for MlxModelsLoader {
    fn load_model(&self, request: MlxModelLoadRequest) -> Result<LoadedMlxModel, MlxAudioError> {
        load_with_mlx_models(request)
    }
}

#[derive(Clone)]
pub struct MlxAudioEngine {
    config: MlxAudioConfig,
    device_label: String,
    model: Arc<dyn MlxSpeechModel>,
}

impl MlxAudioEngine {
    pub fn init() -> Result<Self, MlxAudioError> {
        Self::with_model(MlxAudioConfig::default(), Arc::new(UnloadedMlxSpeechModel))
    }

    pub fn from_env() -> Result<Self, MlxAudioError> {
        Self::with_model(MlxAudioConfig::from_env(), Arc::new(UnloadedMlxSpeechModel))
    }

    pub fn with_model(
        config: MlxAudioConfig,
        model: Arc<dyn MlxSpeechModel>,
    ) -> Result<Self, MlxAudioError> {
        let device_label = assert_mlx_gpu_available()?;
        Ok(Self {
            config,
            device_label,
            model,
        })
    }

    pub fn with_loader(
        config: MlxAudioConfig,
        loader: &dyn MlxModelLoader,
        request: MlxModelLoadRequest,
    ) -> Result<Self, MlxAudioError> {
        match loader.load_model(request)? {
            LoadedMlxModel::Speech(model) => Self::with_model(config, model),
            #[cfg(feature = "mlx-models-loader")]
            LoadedMlxModel::TransformerText { .. } | LoadedMlxModel::Qwen3NextText { .. } => {
                Err(MlxAudioError::ModelUnavailable(
                    "loaded mlx-models text model cannot satisfy Vona speech traits".to_string(),
                ))
            }
        }
    }

    pub fn config(&self) -> &MlxAudioConfig {
        &self.config
    }

    pub fn device_label(&self) -> &str {
        &self.device_label
    }

    pub fn audio_array_from_frame(frame: &AudioInputFrame) -> Result<MlxArray, MlxAudioError> {
        #[cfg(feature = "native-mlx")]
        {
            let len = i32::try_from(frame.samples.len()).map_err(|_| {
                MlxAudioError::InvalidInput("audio frame is too large for mlx shape".to_string())
            })?;
            return Ok(MlxArray::from_slice(&frame.samples, &[len]));
        }

        #[cfg(not(feature = "native-mlx"))]
        {
            MlxArray::from_samples(&frame.samples)
        }
    }
}

#[cfg(feature = "mlx-models-loader")]
fn load_with_mlx_models(request: MlxModelLoadRequest) -> Result<LoadedMlxModel, MlxAudioError> {
    let local_path = request.local_path.ok_or_else(|| {
        MlxAudioError::InvalidInput(
            "mlx-models loader currently requires a local model directory".to_string(),
        )
    })?;

    match request.kind {
        MlxModelKind::Speech | MlxModelKind::WhisperSpeech | MlxModelKind::Qwen3TtsSpeech => {
            Err(MlxAudioError::ModelUnavailable(
                "mlx-models 0.1.x does not expose speech model loaders; use a Vona speech-model crate"
                    .to_string(),
            ))
        }
        MlxModelKind::TransformerText => {
            let model = mlx_models::transformer::load_model(&local_path)
                .map_err(|error| MlxAudioError::Inference(error.to_string()))?;
            let tokenizer = mlx_models::load_tokenizer(&local_path)
                .map_err(|error| MlxAudioError::Inference(error.to_string()))?;
            Ok(LoadedMlxModel::TransformerText { model, tokenizer })
        }
        MlxModelKind::Qwen3NextText => {
            let model = mlx_models::qwen3_next::load_qwen3_next_model(&local_path)
                .map_err(|error| MlxAudioError::Inference(error.to_string()))?;
            let tokenizer = mlx_models::load_tokenizer(&local_path)
                .map_err(|error| MlxAudioError::Inference(error.to_string()))?;
            Ok(LoadedMlxModel::Qwen3NextText { model, tokenizer })
        }
    }
}

#[cfg(not(feature = "mlx-models-loader"))]
fn load_with_mlx_models(_request: MlxModelLoadRequest) -> Result<LoadedMlxModel, MlxAudioError> {
    Err(MlxAudioError::Runtime(
        "enable the mlx-models-loader feature to use mlx-models loading".to_string(),
    ))
}

#[cfg(feature = "native-mlx")]
fn assert_mlx_gpu_available() -> Result<String, MlxAudioError> {
    use mlx_rs::{Array, Device};
    use std::panic::AssertUnwindSafe;

    let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
        let device = Device::gpu();
        Device::set_default(&device);
        let probe = Array::from_slice(&[0.0_f32], &[1]);
        probe.eval()?;
        mlx_rs::error::Result::Ok(format!("{device}"))
    }))
    .map_err(|_| MlxAudioError::Runtime("MLX GPU initialization panicked".to_string()))?;

    result.map_err(|error| MlxAudioError::Runtime(error.to_string()))
}

#[cfg(not(feature = "native-mlx"))]
fn assert_mlx_gpu_available() -> Result<String, MlxAudioError> {
    Ok("native-mlx feature disabled".to_string())
}

#[cfg(feature = "native-mlx")]
fn eval_array(array: &MlxArray) -> Result<(), MlxAudioError> {
    array
        .eval()
        .map_err(|error| MlxAudioError::Inference(error.to_string()))
}

#[cfg(not(feature = "native-mlx"))]
fn eval_array(array: &MlxArray) -> Result<(), MlxAudioError> {
    array.eval()
}

fn samples_from_array(array: &MlxArray) -> Vec<f32> {
    array.as_slice::<f32>().to_vec()
}

#[cfg(test)]
fn test_array_from_samples(samples: &[f32]) -> MlxArray {
    #[cfg(feature = "native-mlx")]
    {
        let len = i32::try_from(samples.len()).unwrap();
        return MlxArray::from_slice(samples, &[len]);
    }

    #[cfg(not(feature = "native-mlx"))]
    {
        MlxArray::from_samples(samples).unwrap()
    }
}

#[derive(Debug, Clone)]
pub struct MlxAudioSession {
    pub config: SessionConfig,
    pub pending_events: Vec<ExternalContextEvent>,
}

pub struct MlxStreamingTranscriptionSession {
    engine: MlxAudioEngine,
    config: StreamingTranscriptionConfig,
    pcm_buffer: Vec<f32>,
    last_inference_samples: usize,
    recent_hypotheses: Vec<String>,
    committed_prefix: String,
    latest_update: Option<StreamingTranscriptUpdate>,
    pending_decode: Option<tokio::task::JoinHandle<Result<String, AudioProcessingError>>>,
    pending_decode_samples: usize,
}

#[derive(Debug, Error)]
pub enum MlxAudioError {
    #[error("MLX runtime is unavailable: {0}")]
    Runtime(String),
    #[error("MLX model is not loaded: {0}")]
    ModelUnavailable(String),
    #[error("MLX input is invalid: {0}")]
    InvalidInput(String),
    #[error("MLX inference failed: {0}")]
    Inference(String),
}

impl From<MlxAudioError> for BackendError {
    fn from(value: MlxAudioError) -> Self {
        match value {
            MlxAudioError::Runtime(message) | MlxAudioError::ModelUnavailable(message) => {
                BackendError::Start(message)
            }
            MlxAudioError::InvalidInput(message) | MlxAudioError::Inference(message) => {
                BackendError::Step(message)
            }
        }
    }
}

impl From<MlxAudioError> for AudioProcessingError {
    fn from(value: MlxAudioError) -> Self {
        match value {
            MlxAudioError::Runtime(message) => AudioProcessingError::Runtime(message),
            MlxAudioError::ModelUnavailable(message) => {
                AudioProcessingError::ModelUnavailable(message)
            }
            MlxAudioError::InvalidInput(message) => AudioProcessingError::InvalidInput(message),
            MlxAudioError::Inference(message) => AudioProcessingError::Inference(message),
        }
    }
}

struct UnloadedMlxSpeechModel;

impl MlxSpeechModel for UnloadedMlxSpeechModel {
    fn transcribe(&self, _audio: &MlxArray, _sample_rate_hz: u32) -> Result<String, MlxAudioError> {
        Err(MlxAudioError::ModelUnavailable(
            "Distil-Whisper MLX graph loader is not implemented in mlx-models 0.1.x".to_string(),
        ))
    }

    fn synthesize(&self, _text: &str, _sample_rate_hz: u32) -> Result<MlxArray, MlxAudioError> {
        Err(MlxAudioError::ModelUnavailable(
            "Qwen3-TTS MLX graph loader is not implemented in mlx-models 0.1.x".to_string(),
        ))
    }
}

fn event_text(events: &[ExternalContextEvent]) -> Option<String> {
    events.iter().find_map(|event| match event.source.as_str() {
        "vona.plan_result" | "vona.precomputed_reply" => event
            .spoken_summary
            .as_ref()
            .map(|value| value.trim().to_string())
            .filter(|value| !value.is_empty()),
        "vona.tts_text" => event
            .payload
            .as_str()
            .map(|value| value.trim().to_string())
            .filter(|value| !value.is_empty()),
        _ => None,
    })
}

#[async_trait]
impl AudioTranscriber for MlxAudioEngine {
    async fn transcribe_audio(
        &self,
        input: AudioInputFrame,
    ) -> Result<String, AudioProcessingError> {
        let audio = Self::audio_array_from_frame(&input).map_err(AudioProcessingError::from)?;
        self.model
            .transcribe(&audio, input.sample_rate_hz)
            .map_err(AudioProcessingError::from)
    }
}

#[async_trait]
impl AudioStreamingTranscriber for MlxAudioEngine {
    async fn start_streaming_transcription(
        &self,
        config: StreamingTranscriptionConfig,
    ) -> Result<Box<dyn StreamingTranscriptionSession>, AudioProcessingError> {
        if config.sample_rate_hz == 0 {
            return Err(AudioProcessingError::InvalidInput(
                "streaming transcription sample rate must be non-zero".to_string(),
            ));
        }
        if config.channels == 0 {
            return Err(AudioProcessingError::InvalidInput(
                "streaming transcription channel count must be non-zero".to_string(),
            ));
        }

        Ok(Box::new(MlxStreamingTranscriptionSession {
            engine: self.clone(),
            config,
            pcm_buffer: Vec::new(),
            last_inference_samples: 0,
            recent_hypotheses: Vec::new(),
            committed_prefix: String::new(),
            latest_update: None,
            pending_decode: None,
            pending_decode_samples: 0,
        }))
    }
}

#[async_trait]
impl StreamingTranscriptionSession for MlxStreamingTranscriptionSession {
    async fn push_audio(
        &mut self,
        input: AudioInputFrame,
    ) -> Result<Option<StreamingTranscriptUpdate>, AudioProcessingError> {
        if input.sample_rate_hz != self.config.sample_rate_hz {
            return Err(AudioProcessingError::InvalidInput(format!(
                "streaming transcription sample rate changed from {} to {}",
                self.config.sample_rate_hz, input.sample_rate_hz
            )));
        }
        if input.channels != self.config.channels {
            return Err(AudioProcessingError::InvalidInput(format!(
                "streaming transcription channel count changed from {} to {}",
                self.config.channels, input.channels
            )));
        }
        if input.samples.is_empty() {
            return Ok(None);
        }

        self.pcm_buffer.extend(input.samples);
        self.enforce_buffer_limit();

        if let Some(update) = self.collect_finished_decode(false).await? {
            return Ok(Some(update));
        }

        let min_samples = samples_for_ms(self.config.sample_rate_hz, self.config.min_buffer_ms);
        if self.pcm_buffer.len() < min_samples {
            return Ok(None);
        }

        let step_samples = samples_for_ms(self.config.sample_rate_hz, self.config.step_ms);
        if self
            .pcm_buffer
            .len()
            .saturating_sub(self.last_inference_samples)
            < step_samples
        {
            return Ok(None);
        }

        if self.pending_decode.is_none() {
            self.last_inference_samples = self.pcm_buffer.len();
            self.pending_decode_samples = self.pcm_buffer.len();
            self.pending_decode = Some(spawn_mlx_streaming_decode(
                self.engine.clone(),
                self.config.sample_rate_hz,
                self.config.channels,
                self.pcm_buffer.clone(),
            ));
        }
        Ok(None)
    }

    async fn finish(&mut self) -> Result<Option<StreamingTranscriptUpdate>, AudioProcessingError> {
        if self.pcm_buffer.is_empty() {
            return Ok(None);
        }
        if let Some(update) = self.collect_finished_decode(false).await?
            && !update.text.trim().is_empty()
        {
            self.latest_update = Some(update);
        }

        if self.pending_decode.is_some() {
            if self.pending_decode_samples == self.pcm_buffer.len() {
                if let Some(update) = self.collect_finished_decode(true).await? {
                    return Ok(Some(StreamingTranscriptUpdate {
                        kind: StreamingTranscriptKind::Final,
                        text: update.text,
                        stability_passes: update.stability_passes,
                        total_audio_ms: self.total_audio_ms(),
                    }));
                }
            } else {
                self.pending_decode = None;
                self.pending_decode_samples = 0;
            }
        }
        self.transcribe_current(true).await
    }
}

impl MlxStreamingTranscriptionSession {
    async fn collect_finished_decode(
        &mut self,
        wait: bool,
    ) -> Result<Option<StreamingTranscriptUpdate>, AudioProcessingError> {
        let should_collect = self
            .pending_decode
            .as_ref()
            .is_some_and(|handle| wait || handle.is_finished());
        if !should_collect {
            return Ok(None);
        }

        let handle = self.pending_decode.take().expect("checked pending decode");
        self.pending_decode_samples = 0;
        let transcript = handle.await.map_err(|err| {
            AudioProcessingError::Runtime(format!("MLX streaming STT task join failed: {err}"))
        })??;
        self.accept_transcript(transcript, false)
    }

    fn enforce_buffer_limit(&mut self) {
        let max_samples = samples_for_ms(self.config.sample_rate_hz, self.config.max_buffer_ms);
        if self.pcm_buffer.len() <= max_samples {
            return;
        }

        let overflow = self.pcm_buffer.len().saturating_sub(max_samples);
        self.pcm_buffer.drain(0..overflow);
        self.last_inference_samples = self.last_inference_samples.saturating_sub(overflow);
        self.pending_decode_samples = self.pending_decode_samples.saturating_sub(overflow);
    }

    async fn transcribe_current(
        &mut self,
        is_final: bool,
    ) -> Result<Option<StreamingTranscriptUpdate>, AudioProcessingError> {
        let transcript = self.decode_current_buffer().await?;
        self.last_inference_samples = self.pcm_buffer.len();
        self.accept_transcript(transcript, is_final)
    }

    async fn decode_current_buffer(&self) -> Result<String, AudioProcessingError> {
        self.engine
            .transcribe_audio(AudioInputFrame {
                sequence: 0,
                sample_rate_hz: self.config.sample_rate_hz,
                channels: self.config.channels,
                samples: self.pcm_buffer.clone(),
            })
            .await
            .map(|value| value.split_whitespace().collect::<Vec<_>>().join(" "))
    }

    fn accept_transcript(
        &mut self,
        transcript: String,
        is_final: bool,
    ) -> Result<Option<StreamingTranscriptUpdate>, AudioProcessingError> {
        self.recent_hypotheses.push(transcript.clone());
        let max_hypotheses = self.config.stability_passes.max(1) as usize;
        if self.recent_hypotheses.len() > max_hypotheses {
            self.recent_hypotheses.remove(0);
        }

        if is_final {
            self.committed_prefix = transcript.clone();
            let update = StreamingTranscriptUpdate {
                kind: StreamingTranscriptKind::Final,
                text: transcript,
                stability_passes: self.recent_hypotheses.len() as u32,
                total_audio_ms: self.total_audio_ms(),
            };
            self.latest_update = Some(update.clone());
            return Ok(Some(update));
        }

        if self.recent_hypotheses.len() < max_hypotheses {
            return Ok(None);
        }

        let committed_candidate = longest_common_word_prefix(&self.recent_hypotheses);
        if committed_candidate.is_empty() || committed_candidate == self.committed_prefix {
            return Ok(None);
        }
        if !self.committed_prefix.is_empty()
            && !committed_candidate.starts_with(&self.committed_prefix)
        {
            return Ok(None);
        }

        self.committed_prefix = committed_candidate.clone();
        let update = StreamingTranscriptUpdate {
            kind: StreamingTranscriptKind::Partial,
            text: committed_candidate,
            stability_passes: self.recent_hypotheses.len() as u32,
            total_audio_ms: self.total_audio_ms(),
        };
        self.latest_update = Some(update.clone());
        Ok(Some(update))
    }

    fn total_audio_ms(&self) -> u64 {
        ((self.pcm_buffer.len() as u64) * 1000) / self.config.sample_rate_hz as u64
    }
}

fn spawn_mlx_streaming_decode(
    engine: MlxAudioEngine,
    sample_rate_hz: u32,
    channels: u16,
    samples: Vec<f32>,
) -> tokio::task::JoinHandle<Result<String, AudioProcessingError>> {
    tokio::spawn(async move {
        engine
            .transcribe_audio(AudioInputFrame {
                sequence: 0,
                sample_rate_hz,
                channels,
                samples,
            })
            .await
            .map(|value| value.split_whitespace().collect::<Vec<_>>().join(" "))
    })
}

fn samples_for_ms(sample_rate_hz: u32, ms: u32) -> usize {
    ((sample_rate_hz as u64 * ms as u64) / 1000) as usize
}

fn longest_common_word_prefix(hypotheses: &[String]) -> String {
    let Some(first) = hypotheses.first() else {
        return String::new();
    };
    let mut prefix: Vec<&str> = first.split_whitespace().collect();
    for hypothesis in hypotheses.iter().skip(1) {
        let words: Vec<&str> = hypothesis.split_whitespace().collect();
        let common_len = prefix
            .iter()
            .zip(words.iter())
            .take_while(|(left, right)| left.eq_ignore_ascii_case(right))
            .count();
        prefix.truncate(common_len);
        if prefix.is_empty() {
            break;
        }
    }
    prefix.join(" ")
}

#[async_trait]
impl AudioSynthesizer for MlxAudioEngine {
    async fn synthesize_audio(
        &self,
        text: String,
        config: AudioSynthesisConfig,
    ) -> Result<AudioOutputFrame, AudioProcessingError> {
        let speech = self
            .model
            .synthesize(&text, config.sample_rate_hz)
            .map_err(AudioProcessingError::from)?;
        eval_array(&speech).map_err(AudioProcessingError::from)?;

        Ok(AudioOutputFrame {
            sequence: config.sequence,
            sample_rate_hz: config.sample_rate_hz,
            channels: config.channels,
            samples: samples_from_array(&speech),
            is_filler: false,
        })
    }
}

#[async_trait]
impl SpeechToSpeechBackend for MlxAudioEngine {
    type Session = MlxAudioSession;

    fn capabilities(&self) -> BackendCapabilities {
        BackendCapabilities {
            supports_context_injection: true,
            supports_style_conditioning: false,
            supports_word_timestamps: false,
            ..BackendCapabilities::default()
        }
    }

    async fn start_session(&self, config: SessionConfig) -> Result<Self::Session, BackendError> {
        Ok(MlxAudioSession {
            config,
            pending_events: Vec::new(),
        })
    }

    async fn step(
        &self,
        session: &mut Self::Session,
        input: AudioInputFrame,
    ) -> Result<BackendStep, BackendError> {
        let sequence = input.sequence;
        let transcript = self
            .transcribe_audio(input)
            .await
            .map_err(|error| BackendError::Step(error.to_string()))?;

        let pending_events = std::mem::take(&mut session.pending_events);
        let reply_text = event_text(&pending_events).unwrap_or_else(|| transcript.clone());
        let output_audio = self
            .synthesize_audio(
                reply_text,
                AudioSynthesisConfig {
                    sequence,
                    sample_rate_hz: self.config.output_sample_rate_hz,
                    channels: session.config.channels,
                },
            )
            .await
            .map_err(|error| BackendError::Step(error.to_string()))?;

        Ok(BackendStep {
            output_audio: vec![output_audio],
            control_events: vec![ControlEvent::Diagnostic {
                message: format!("vona-mlx device {}", self.device_label),
            }],
            transcript: Some(transcript),
            finished: false,
            debug_payload: Some(json!({
                "stt_model_id": self.config.stt_model_id,
                "tts_model_id": self.config.tts_model_id,
            })),
        })
    }

    async fn inject_event(
        &self,
        session: &mut Self::Session,
        event: ExternalContextEvent,
    ) -> Result<(), BackendError> {
        session.pending_events.push(event);
        Ok(())
    }

    async fn end_session(&self, _session: Self::Session) -> Result<(), BackendError> {
        Ok(())
    }
}

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

    struct EchoModel;

    impl MlxSpeechModel for EchoModel {
        fn transcribe(
            &self,
            _audio: &MlxArray,
            _sample_rate_hz: u32,
        ) -> Result<String, MlxAudioError> {
            Ok("hello".to_string())
        }

        fn synthesize(&self, _text: &str, _sample_rate_hz: u32) -> Result<MlxArray, MlxAudioError> {
            Ok(test_array_from_samples(&[0.0_f32, 0.25, -0.25]))
        }
    }

    struct LengthAwareModel;

    impl MlxSpeechModel for LengthAwareModel {
        fn transcribe(
            &self,
            audio: &MlxArray,
            _sample_rate_hz: u32,
        ) -> Result<String, MlxAudioError> {
            if audio.shape()[0] < 3_000 {
                Ok("stale partial".to_string())
            } else {
                Ok("final transcript".to_string())
            }
        }

        fn synthesize(&self, _text: &str, _sample_rate_hz: u32) -> Result<MlxArray, MlxAudioError> {
            Ok(test_array_from_samples(&[0.0]))
        }
    }

    #[test]
    fn builds_mlx_audio_array_from_vona_frame() {
        let frame = AudioInputFrame {
            sequence: 0,
            sample_rate_hz: 16_000,
            channels: 1,
            samples: vec![0.1, 0.2],
        };

        let array = MlxAudioEngine::audio_array_from_frame(&frame).unwrap();
        assert_eq!(array.shape(), &[2]);
    }

    #[test]
    fn extracts_tts_text_from_events() {
        let events = vec![ExternalContextEvent {
            source: "vona.tts_text".to_string(),
            spoken_summary: None,
            payload: json!("speak this"),
        }];

        assert_eq!(event_text(&events), Some("speak this".to_string()));
    }

    #[test]
    fn mlx_models_loader_is_feature_gated() {
        #[cfg(not(feature = "mlx-models-loader"))]
        {
            let request = MlxModelLoadRequest::local(
                MlxModelKind::TransformerText,
                "local-test-model",
                "/tmp/model",
            );
            let result = MlxModelsLoader.load_model(request);
            assert!(matches!(result, Err(MlxAudioError::Runtime(_))));
        }
    }

    #[test]
    fn streaming_common_prefix_is_word_stable() {
        let hypotheses = vec![
            "focus on the first task today".to_string(),
            "focus on the first useful task".to_string(),
        ];

        assert_eq!(
            longest_common_word_prefix(&hypotheses),
            "focus on the first"
        );
    }

    #[tokio::test]
    async fn injected_model_runs_backend_step() {
        let engine = MlxAudioEngine {
            config: MlxAudioConfig::default(),
            device_label: "test".to_string(),
            model: Arc::new(EchoModel),
        };
        let mut session = engine
            .start_session(SessionConfig::default())
            .await
            .unwrap();
        let step = engine
            .step(
                &mut session,
                AudioInputFrame {
                    sequence: 7,
                    sample_rate_hz: 16_000,
                    channels: 1,
                    samples: vec![0.0, 1.0],
                },
            )
            .await
            .unwrap();

        assert_eq!(step.transcript, Some("hello".to_string()));
        assert_eq!(step.output_audio[0].samples, vec![0.0, 0.25, -0.25]);
    }

    #[tokio::test]
    async fn streaming_finish_decodes_current_buffer_instead_of_stale_partial() {
        let engine = MlxAudioEngine {
            config: MlxAudioConfig::default(),
            device_label: "test".to_string(),
            model: Arc::new(LengthAwareModel),
        };
        let mut session = engine
            .start_streaming_transcription(StreamingTranscriptionConfig {
                sample_rate_hz: 16_000,
                channels: 1,
                step_ms: 50,
                min_buffer_ms: 50,
                max_buffer_ms: 30_000,
                stability_passes: 1,
            })
            .await
            .unwrap();

        let _ = session
            .push_audio(AudioInputFrame {
                sequence: 1,
                sample_rate_hz: 16_000,
                channels: 1,
                samples: vec![0.0; 1_000],
            })
            .await
            .unwrap();
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        let _ = session
            .push_audio(AudioInputFrame {
                sequence: 2,
                sample_rate_hz: 16_000,
                channels: 1,
                samples: vec![0.0; 3_000],
            })
            .await
            .unwrap();

        let final_update = session.finish().await.unwrap().unwrap();
        assert_eq!(final_update.kind, StreamingTranscriptKind::Final);
        assert_eq!(final_update.text, "final transcript");
    }
}