oxirs-chat 0.2.4

RAG chat API with LLM integration and natural language to SPARQL translation
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
//! Voice Interface Module
//!
//! Provides Speech-to-Text (STT) and Text-to-Speech (TTS) capabilities for voice interactions
//! with the chat system. Supports multiple providers and streaming audio.

use anyhow::{Context, Result};
use async_openai::{
    config::OpenAIConfig,
    types::audio::{
        AudioResponseFormat, CreateSpeechRequest, CreateTranscriptionRequestArgs, SpeechModel,
        SpeechResponseFormat, Voice,
    },
    Client,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tokio::sync::Mutex;
use tracing::{debug, info, warn};

/// Voice interface manager
pub struct VoiceInterface {
    config: VoiceConfig,
    stt_provider: Arc<Mutex<dyn SpeechToTextProvider>>,
    tts_provider: Arc<Mutex<dyn TextToSpeechProvider>>,
}

/// Voice interface configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VoiceConfig {
    /// Enable speech-to-text
    pub enable_stt: bool,
    /// Enable text-to-speech
    pub enable_tts: bool,
    /// STT provider selection
    pub stt_provider: SttProviderType,
    /// TTS provider selection
    pub tts_provider: TtsProviderType,
    /// Audio sample rate (Hz)
    pub sample_rate: u32,
    /// Audio channels (1 = mono, 2 = stereo)
    pub channels: u16,
    /// Maximum audio duration (seconds)
    pub max_duration_secs: u64,
    /// Language code (e.g., "en-US", "ja-JP")
    pub language: String,
    /// Voice/speaker selection for TTS
    pub voice: String,
}

impl Default for VoiceConfig {
    fn default() -> Self {
        Self {
            enable_stt: true,
            enable_tts: true,
            stt_provider: SttProviderType::OpenAI,
            tts_provider: TtsProviderType::OpenAI,
            sample_rate: 16000,
            channels: 1,
            max_duration_secs: 300, // 5 minutes
            language: "en-US".to_string(),
            voice: "alloy".to_string(),
        }
    }
}

/// STT provider types
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum SttProviderType {
    /// OpenAI Whisper API
    OpenAI,
    /// Google Speech-to-Text
    Google,
    /// Azure Speech Services
    Azure,
    /// Local Whisper model
    LocalWhisper,
}

/// TTS provider types
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum TtsProviderType {
    /// OpenAI TTS API
    OpenAI,
    /// Google Text-to-Speech
    Google,
    /// Azure Speech Services
    Azure,
    /// Local TTS engine
    LocalEngine,
}

/// Speech-to-text provider trait
#[async_trait::async_trait]
pub trait SpeechToTextProvider: Send + Sync {
    /// Transcribe audio file to text
    async fn transcribe(&self, audio_data: &[u8], _config: &VoiceConfig) -> Result<SttResult>;

    /// Transcribe audio stream to text (real-time)
    async fn transcribe_stream(
        &self,
        audio_stream: tokio::sync::mpsc::Receiver<Vec<u8>>,
        config: &VoiceConfig,
    ) -> Result<tokio::sync::mpsc::Receiver<SttStreamResult>>;
}

/// Text-to-speech provider trait
#[async_trait::async_trait]
pub trait TextToSpeechProvider: Send + Sync {
    /// Synthesize text to audio
    async fn synthesize(&self, text: &str, _config: &VoiceConfig) -> Result<TtsResult>;

    /// Synthesize text to audio stream (real-time)
    async fn synthesize_stream(
        &self,
        _text: &str,
        _config: &VoiceConfig,
    ) -> Result<tokio::sync::mpsc::Receiver<Vec<u8>>>;
}

/// Speech-to-text result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SttResult {
    /// Transcribed text
    pub text: String,
    /// Confidence score (0.0 - 1.0)
    pub confidence: f32,
    /// Language detected
    pub language: Option<String>,
    /// Processing duration (milliseconds)
    pub duration_ms: u64,
    /// Word-level timestamps
    pub word_timestamps: Vec<WordTimestamp>,
}

/// Streaming STT result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SttStreamResult {
    /// Partial or final transcribed text
    pub text: String,
    /// Is this a final result (vs. partial)
    pub is_final: bool,
    /// Confidence score
    pub confidence: f32,
}

/// Word-level timestamp information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WordTimestamp {
    pub word: String,
    pub start_ms: u64,
    pub end_ms: u64,
}

/// Text-to-speech result
#[derive(Debug, Clone)]
pub struct TtsResult {
    /// Audio data
    pub audio_data: Vec<u8>,
    /// Audio format
    pub format: AudioFormat,
    /// Duration (milliseconds)
    pub duration_ms: u64,
}

/// Audio format
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum AudioFormat {
    Wav,
    Mp3,
    Opus,
    Pcm,
}

impl VoiceInterface {
    /// Create a new voice interface
    pub fn new(config: VoiceConfig) -> Self {
        let stt_provider: Arc<Mutex<dyn SpeechToTextProvider>> = match config.stt_provider {
            SttProviderType::OpenAI => Arc::new(Mutex::new(OpenAISttProvider::new(config.clone()))),
            SttProviderType::Google => Arc::new(Mutex::new(GoogleSttProvider::new(config.clone()))),
            SttProviderType::Azure => Arc::new(Mutex::new(AzureSttProvider::new(config.clone()))),
            SttProviderType::LocalWhisper => {
                Arc::new(Mutex::new(LocalWhisperProvider::new(config.clone())))
            }
        };

        let tts_provider: Arc<Mutex<dyn TextToSpeechProvider>> = match config.tts_provider {
            TtsProviderType::OpenAI => Arc::new(Mutex::new(OpenAITtsProvider::new(config.clone()))),
            TtsProviderType::Google => Arc::new(Mutex::new(GoogleTtsProvider::new(config.clone()))),
            TtsProviderType::Azure => Arc::new(Mutex::new(AzureTtsProvider::new(config.clone()))),
            TtsProviderType::LocalEngine => {
                Arc::new(Mutex::new(LocalTtsEngine::new(config.clone())))
            }
        };

        Self {
            config,
            stt_provider,
            tts_provider,
        }
    }

    /// Transcribe audio to text
    pub async fn transcribe(&self, audio_data: &[u8]) -> Result<SttResult> {
        if !self.config.enable_stt {
            anyhow::bail!("Speech-to-text is disabled");
        }

        let provider = self.stt_provider.lock().await;
        provider.transcribe(audio_data, &self.config).await
    }

    /// Synthesize text to speech
    pub async fn synthesize(&self, text: &str) -> Result<TtsResult> {
        if !self.config.enable_tts {
            anyhow::bail!("Text-to-speech is disabled");
        }

        let provider = self.tts_provider.lock().await;
        provider.synthesize(text, &self.config).await
    }
}

// ========== Provider Implementations ==========

/// OpenAI STT Provider (Whisper API)
struct OpenAISttProvider {
    config: VoiceConfig,
    client: Client<OpenAIConfig>,
}

impl OpenAISttProvider {
    fn new(config: VoiceConfig) -> Self {
        let client = Client::new();
        Self { config, client }
    }
}

#[async_trait::async_trait]
impl SpeechToTextProvider for OpenAISttProvider {
    async fn transcribe(&self, audio_data: &[u8], config: &VoiceConfig) -> Result<SttResult> {
        info!(
            "Transcribing audio with OpenAI Whisper (size: {} bytes)",
            audio_data.len()
        );

        let start_time = std::time::Instant::now();

        // Create transcription request using OpenAI Whisper
        let request = CreateTranscriptionRequestArgs::default()
            .file(async_openai::types::audio::AudioInput {
                source: async_openai::types::InputSource::Bytes {
                    filename: "audio.mp3".to_string(),
                    bytes: audio_data.to_vec().into(),
                },
            })
            .model("whisper-1")
            .language(&config.language[..2]) // Convert "en-US" to "en"
            .response_format(AudioResponseFormat::VerboseJson)
            .build()
            .context("Failed to build transcription request")?;

        // Call OpenAI Whisper API
        let response = self
            .client
            .audio()
            .transcription()
            .create(request)
            .await
            .context("Failed to transcribe audio with OpenAI Whisper")?;

        let duration_ms = start_time.elapsed().as_millis() as u64;

        debug!(
            "OpenAI Whisper transcription completed: '{}' (duration: {}ms)",
            response.text, duration_ms
        );

        // Extract word timestamps if available (Whisper verbose JSON includes them)
        let word_timestamps = vec![]; // OpenAI Whisper API doesn't provide word-level timestamps in the current API

        Ok(SttResult {
            text: response.text,
            confidence: 0.95, // OpenAI doesn't provide confidence scores
            language: Some(config.language.clone()),
            duration_ms,
            word_timestamps,
        })
    }

    async fn transcribe_stream(
        &self,
        mut audio_stream: tokio::sync::mpsc::Receiver<Vec<u8>>,
        config: &VoiceConfig,
    ) -> Result<tokio::sync::mpsc::Receiver<SttStreamResult>> {
        let (tx, rx) = tokio::sync::mpsc::channel(100);

        let client = self.client.clone();
        let language = config.language.clone();

        // Spawn background task for streaming transcription
        // Note: OpenAI Whisper doesn't natively support streaming, so we accumulate chunks
        tokio::spawn(async move {
            let mut accumulated_audio = Vec::new();

            while let Some(audio_chunk) = audio_stream.recv().await {
                accumulated_audio.extend_from_slice(&audio_chunk);

                // Process accumulated audio every 5 seconds worth of data (approx)
                // Assuming 16kHz, 1 channel, 16-bit = 32KB per second
                if accumulated_audio.len() >= 160_000 {
                    // Create transcription request
                    match CreateTranscriptionRequestArgs::default()
                        .file(async_openai::types::audio::AudioInput {
                            source: async_openai::types::InputSource::Bytes {
                                filename: "audio_chunk.mp3".to_string(),
                                bytes: accumulated_audio.clone().into(),
                            },
                        })
                        .model("whisper-1")
                        .language(&language[..2])
                        .response_format(AudioResponseFormat::Json)
                        .build()
                    {
                        Ok(request) => {
                            if let Ok(response) =
                                client.audio().transcription().create(request).await
                            {
                                let _ = tx
                                    .send(SttStreamResult {
                                        text: response.text,
                                        is_final: false,
                                        confidence: 0.95,
                                    })
                                    .await;
                            }
                        }
                        Err(e) => {
                            warn!("Failed to create transcription request: {}", e);
                        }
                    }

                    // Clear accumulated audio after processing
                    accumulated_audio.clear();
                }
            }

            // Process any remaining audio
            if !accumulated_audio.is_empty() {
                if let Ok(request) = CreateTranscriptionRequestArgs::default()
                    .file(async_openai::types::audio::AudioInput {
                        source: async_openai::types::InputSource::Bytes {
                            filename: "audio_final.mp3".to_string(),
                            bytes: accumulated_audio.into(),
                        },
                    })
                    .model("whisper-1")
                    .language(&language[..2])
                    .response_format(AudioResponseFormat::Json)
                    .build()
                {
                    if let Ok(response) = client.audio().transcription().create(request).await {
                        let _ = tx
                            .send(SttStreamResult {
                                text: response.text,
                                is_final: true,
                                confidence: 0.95,
                            })
                            .await;
                    }
                }
            }
        });

        Ok(rx)
    }
}

/// Google STT Provider (placeholder)
struct GoogleSttProvider {
    config: VoiceConfig,
}

impl GoogleSttProvider {
    fn new(config: VoiceConfig) -> Self {
        Self { config }
    }
}

#[async_trait::async_trait]
impl SpeechToTextProvider for GoogleSttProvider {
    async fn transcribe(&self, _audio_data: &[u8], _config: &VoiceConfig) -> Result<SttResult> {
        warn!("Google STT integration not yet implemented");
        Ok(SttResult {
            text: "[Google STT placeholder]".to_string(),
            confidence: 0.90,
            language: Some("en-US".to_string()),
            duration_ms: 1000,
            word_timestamps: vec![],
        })
    }

    async fn transcribe_stream(
        &self,
        _audio_stream: tokio::sync::mpsc::Receiver<Vec<u8>>,
        _config: &VoiceConfig,
    ) -> Result<tokio::sync::mpsc::Receiver<SttStreamResult>> {
        let (_tx, rx) = tokio::sync::mpsc::channel(100);
        Ok(rx)
    }
}

/// Azure STT Provider (placeholder)
struct AzureSttProvider {
    config: VoiceConfig,
}

impl AzureSttProvider {
    fn new(config: VoiceConfig) -> Self {
        Self { config }
    }
}

#[async_trait::async_trait]
impl SpeechToTextProvider for AzureSttProvider {
    async fn transcribe(&self, _audio_data: &[u8], _config: &VoiceConfig) -> Result<SttResult> {
        warn!("Azure STT integration not yet implemented");
        Ok(SttResult {
            text: "[Azure STT placeholder]".to_string(),
            confidence: 0.92,
            language: Some("en-US".to_string()),
            duration_ms: 1000,
            word_timestamps: vec![],
        })
    }

    async fn transcribe_stream(
        &self,
        _audio_stream: tokio::sync::mpsc::Receiver<Vec<u8>>,
        _config: &VoiceConfig,
    ) -> Result<tokio::sync::mpsc::Receiver<SttStreamResult>> {
        let (_tx, rx) = tokio::sync::mpsc::channel(100);
        Ok(rx)
    }
}

/// Local Whisper Provider (placeholder)
struct LocalWhisperProvider {
    config: VoiceConfig,
}

impl LocalWhisperProvider {
    fn new(config: VoiceConfig) -> Self {
        Self { config }
    }
}

#[async_trait::async_trait]
impl SpeechToTextProvider for LocalWhisperProvider {
    async fn transcribe(&self, _audio_data: &[u8], _config: &VoiceConfig) -> Result<SttResult> {
        warn!("Local Whisper integration not yet implemented");
        Ok(SttResult {
            text: "[Local Whisper placeholder]".to_string(),
            confidence: 0.88,
            language: Some("en-US".to_string()),
            duration_ms: 1000,
            word_timestamps: vec![],
        })
    }

    async fn transcribe_stream(
        &self,
        _audio_stream: tokio::sync::mpsc::Receiver<Vec<u8>>,
        _config: &VoiceConfig,
    ) -> Result<tokio::sync::mpsc::Receiver<SttStreamResult>> {
        let (_tx, rx) = tokio::sync::mpsc::channel(100);
        Ok(rx)
    }
}

/// OpenAI TTS Provider
struct OpenAITtsProvider {
    config: VoiceConfig,
    client: Client<OpenAIConfig>,
}

impl OpenAITtsProvider {
    fn new(config: VoiceConfig) -> Self {
        let client = Client::new();
        Self { config, client }
    }
}

#[async_trait::async_trait]
impl TextToSpeechProvider for OpenAITtsProvider {
    async fn synthesize(&self, text: &str, config: &VoiceConfig) -> Result<TtsResult> {
        info!(
            "Synthesizing speech with OpenAI TTS (text length: {} chars)",
            text.len()
        );

        let start_time = std::time::Instant::now();

        // Map voice string to OpenAI Voice enum
        let voice = match config.voice.as_str() {
            "alloy" => Voice::Alloy,
            "echo" => Voice::Echo,
            "fable" => Voice::Fable,
            "onyx" => Voice::Onyx,
            "nova" => Voice::Nova,
            "shimmer" => Voice::Shimmer,
            _ => Voice::Alloy, // Default fallback
        };

        // Create TTS request
        let request = CreateSpeechRequest {
            model: SpeechModel::Tts1,
            input: text.to_string(),
            voice,
            instructions: None,
            response_format: Some(SpeechResponseFormat::Mp3),
            speed: Some(1.0),
            stream_format: None,
        };

        // Call OpenAI TTS API
        let response = self
            .client
            .audio()
            .speech()
            .create(request)
            .await
            .context("Failed to synthesize speech with OpenAI TTS")?;

        let duration_ms = start_time.elapsed().as_millis() as u64;

        // Read audio bytes from response
        let audio_data = response.bytes.to_vec();

        debug!(
            "OpenAI TTS synthesis completed: {} bytes (duration: {}ms)",
            audio_data.len(),
            duration_ms
        );

        Ok(TtsResult {
            audio_data,
            format: AudioFormat::Mp3,
            duration_ms,
        })
    }

    async fn synthesize_stream(
        &self,
        text: &str,
        config: &VoiceConfig,
    ) -> Result<tokio::sync::mpsc::Receiver<Vec<u8>>> {
        let (tx, rx) = tokio::sync::mpsc::channel(100);

        let client = self.client.clone();
        let text = text.to_string();
        let voice_str = config.voice.clone();

        // Spawn background task for streaming TTS
        tokio::spawn(async move {
            // Map voice string to OpenAI Voice enum
            let voice = match voice_str.as_str() {
                "alloy" => Voice::Alloy,
                "echo" => Voice::Echo,
                "fable" => Voice::Fable,
                "onyx" => Voice::Onyx,
                "nova" => Voice::Nova,
                "shimmer" => Voice::Shimmer,
                _ => Voice::Alloy,
            };

            // For streaming, we split text into sentences and synthesize each separately
            let sentences: Vec<&str> = text
                .split(['.', '!', '?'])
                .filter(|s| !s.trim().is_empty())
                .collect();

            for sentence in sentences {
                let request = CreateSpeechRequest {
                    model: SpeechModel::Tts1,
                    input: sentence.trim().to_string(),
                    voice: voice.clone(),
                    instructions: None,
                    response_format: Some(SpeechResponseFormat::Mp3),
                    speed: Some(1.0),
                    stream_format: None,
                };

                match client.audio().speech().create(request).await {
                    Ok(response) => {
                        let audio_chunk = response.bytes.to_vec();
                        if tx.send(audio_chunk).await.is_err() {
                            break; // Receiver dropped
                        }
                    }
                    Err(e) => {
                        warn!("Failed to synthesize sentence in streaming mode: {}", e);
                        break;
                    }
                }
            }
        });

        Ok(rx)
    }
}

/// Google TTS Provider (placeholder)
struct GoogleTtsProvider {
    config: VoiceConfig,
}

impl GoogleTtsProvider {
    fn new(config: VoiceConfig) -> Self {
        Self { config }
    }
}

#[async_trait::async_trait]
impl TextToSpeechProvider for GoogleTtsProvider {
    async fn synthesize(&self, text: &str, _config: &VoiceConfig) -> Result<TtsResult> {
        warn!("Google TTS integration not yet implemented");
        Ok(TtsResult {
            audio_data: vec![],
            format: AudioFormat::Mp3,
            duration_ms: (text.len() as u64) * 100,
        })
    }

    async fn synthesize_stream(
        &self,
        _text: &str,
        _config: &VoiceConfig,
    ) -> Result<tokio::sync::mpsc::Receiver<Vec<u8>>> {
        let (_tx, rx) = tokio::sync::mpsc::channel(100);
        Ok(rx)
    }
}

/// Azure TTS Provider (placeholder)
struct AzureTtsProvider {
    config: VoiceConfig,
}

impl AzureTtsProvider {
    fn new(config: VoiceConfig) -> Self {
        Self { config }
    }
}

#[async_trait::async_trait]
impl TextToSpeechProvider for AzureTtsProvider {
    async fn synthesize(&self, text: &str, _config: &VoiceConfig) -> Result<TtsResult> {
        warn!("Azure TTS integration not yet implemented");
        Ok(TtsResult {
            audio_data: vec![],
            format: AudioFormat::Wav,
            duration_ms: (text.len() as u64) * 100,
        })
    }

    async fn synthesize_stream(
        &self,
        _text: &str,
        _config: &VoiceConfig,
    ) -> Result<tokio::sync::mpsc::Receiver<Vec<u8>>> {
        let (_tx, rx) = tokio::sync::mpsc::channel(100);
        Ok(rx)
    }
}

/// Local TTS Engine (placeholder)
struct LocalTtsEngine {
    config: VoiceConfig,
}

impl LocalTtsEngine {
    fn new(config: VoiceConfig) -> Self {
        Self { config }
    }
}

#[async_trait::async_trait]
impl TextToSpeechProvider for LocalTtsEngine {
    async fn synthesize(&self, text: &str, _config: &VoiceConfig) -> Result<TtsResult> {
        warn!("Local TTS engine not yet implemented");
        Ok(TtsResult {
            audio_data: vec![],
            format: AudioFormat::Wav,
            duration_ms: (text.len() as u64) * 100,
        })
    }

    async fn synthesize_stream(
        &self,
        _text: &str,
        _config: &VoiceConfig,
    ) -> Result<tokio::sync::mpsc::Receiver<Vec<u8>>> {
        let (_tx, rx) = tokio::sync::mpsc::channel(100);
        Ok(rx)
    }
}

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

    #[tokio::test]
    async fn test_voice_interface_creation() {
        let config = VoiceConfig::default();
        let interface = VoiceInterface::new(config);
        assert!(interface.config.enable_stt);
        assert!(interface.config.enable_tts);
    }

    #[tokio::test]
    async fn test_transcribe_disabled() {
        let config = VoiceConfig {
            enable_stt: false,
            ..Default::default()
        };

        let interface = VoiceInterface::new(config);

        let audio_data = vec![0u8; 1000];
        let result = interface.transcribe(&audio_data).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("disabled"));
    }

    #[tokio::test]
    async fn test_synthesize_disabled() {
        let config = VoiceConfig {
            enable_tts: false,
            ..Default::default()
        };

        let interface = VoiceInterface::new(config);

        let text = "Hello, world!";
        let result = interface.synthesize(text).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("disabled"));
    }

    #[test]
    fn test_voice_config_custom() {
        let config = VoiceConfig {
            enable_stt: true,
            enable_tts: false,
            stt_provider: SttProviderType::Google,
            tts_provider: TtsProviderType::Azure,
            sample_rate: 48000,
            channels: 2,
            max_duration_secs: 600,
            language: "ja-JP".to_string(),
            voice: "echo".to_string(),
        };

        assert_eq!(config.sample_rate, 48000);
        assert_eq!(config.channels, 2);
        assert_eq!(config.language, "ja-JP");
        assert_eq!(config.voice, "echo");
        assert!(!config.enable_tts);
    }

    #[test]
    fn test_audio_format_variants() {
        assert!(matches!(AudioFormat::Wav, AudioFormat::Wav));
        assert!(matches!(AudioFormat::Mp3, AudioFormat::Mp3));
    }

    #[test]
    fn test_stt_provider_serialization() {
        let provider = SttProviderType::OpenAI;
        let serialized = serde_json::to_string(&provider).expect("should succeed");
        assert_eq!(serialized, "\"open_a_i\""); // Snake case serialization

        let deserialized: SttProviderType =
            serde_json::from_str(&serialized).expect("should succeed");
        assert_eq!(deserialized, SttProviderType::OpenAI);
    }

    #[test]
    fn test_tts_provider_serialization() {
        let provider = TtsProviderType::Google;
        let serialized = serde_json::to_string(&provider).expect("should succeed");
        assert_eq!(serialized, "\"google\"");

        let deserialized: TtsProviderType =
            serde_json::from_str(&serialized).expect("should succeed");
        assert_eq!(deserialized, TtsProviderType::Google);
    }
}