rustvani 0.2.4

Voice AI framework for Rust — real-time speech pipelines with STT, LLM, TTS, and Dhara conversation flows
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
//! Deepgram Speech-to-Text WebSocket service.
//!
//! Connects to Deepgram's streaming API and pushes TranscriptionFrames
//! downstream when final transcripts arrive.
//!
//! Pipeline position:
//!   transport.input() → DeepgramSttHandler → llm → tts → transport.output()
//!
//! Wiring:
//!   let stt = DeepgramSttHandler::new(DeepgramSttConfig {
//!       api_key: std::env::var("DEEPGRAM_API_KEY").unwrap(),
//!       ..Default::default()
//!   })
//!   .into_processor();
//!
//! Frames consumed:
//!   - StartFrame             → connects WebSocket
//!   - InputAudioRaw          → send raw binary PCM to Deepgram
//!   - VADUserStoppedSpeaking → send Finalize to force final transcript
//!   - EndFrame / CancelFrame → disconnect (CloseStream + task abort)
//!
//! Frames produced:
//!   - TranscriptionFrame (downstream) on final transcript
//!   - ErrorFrame (upstream) on connection / parse errors
//!
//! Auth: Authorization: Token <api_key> header
//! URL:  wss://api.deepgram.com/v1/listen

use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};

use async_trait::async_trait;
use chrono::Utc;
use futures::{SinkExt, StreamExt};
use log;
use serde::Deserialize;
use tokio::sync::{mpsc, Mutex};
use tokio::task::JoinHandle;
use tokio_tungstenite::tungstenite::http::Request;
use tokio_tungstenite::tungstenite::Message;

use crate::billing::{BillingCollector, BillingEvent};
use crate::error::Result;
use crate::frames::{
    ControlFrame, Frame, FrameDirection, FrameHandler, FrameInner, FrameProcessor,
    SystemFrame, TranscriptionData,
};

const DEEPGRAM_WSS_URL: &str = "wss://api.deepgram.com/v1/listen";

// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------

/// Configuration for [`DeepgramSttHandler`].
#[derive(Debug, Clone)]
pub struct DeepgramSttConfig {
    /// Deepgram API key.
    pub api_key: String,

    /// Model name. e.g. "nova-3", "nova-2", "base", "enhanced".
    pub model: String,

    /// BCP-47 language code. e.g. "en-US", "en", "multi".
    pub language: String,

    /// Audio encoding. Use "linear16" for raw PCM i16 LE.
    pub encoding: String,

    /// Audio sample rate in Hz. Must match what the transport sends.
    pub sample_rate: u32,

    /// Number of audio channels.
    pub channels: u32,

    /// Emit interim (non-final) transcription results.
    /// Final results are always emitted.
    pub interim_results: bool,

    /// Add punctuation to transcripts.
    pub punctuate: bool,

    /// Apply smart formatting (numbers, dates, etc.).
    pub smart_format: bool,

    /// Endpointing silence threshold in ms before Deepgram finalises a
    /// transcript automatically. `None` disables server-side endpointing.
    pub endpointing: Option<u32>,

    /// Utterance-end silence in ms. Enables `utterance_end` events.
    pub utterance_end_ms: Option<u32>,

    /// Override the default WebSocket URL (for on-prem / proxy deployments).
    pub base_url: Option<String>,
}

impl Default for DeepgramSttConfig {
    fn default() -> Self {
        Self {
            api_key: String::new(),
            model: "nova-3".to_string(),
            language: "en-US".to_string(),
            encoding: "linear16".to_string(),
            sample_rate: 16_000,
            channels: 1,
            interim_results: true,
            punctuate: true,
            smart_format: false,
            endpointing: Some(300),
            utterance_end_ms: None,
            base_url: None,
        }
    }
}

impl DeepgramSttConfig {
    fn ws_url(&self) -> String {
        let base = self.base_url.as_deref().unwrap_or(DEEPGRAM_WSS_URL);
        let mut params = vec![
            format!("model={}", self.model),
            format!("language={}", self.language),
            format!("encoding={}", self.encoding),
            format!("sample_rate={}", self.sample_rate),
            format!("channels={}", self.channels),
            format!("interim_results={}", self.interim_results),
            format!("punctuate={}", self.punctuate),
            format!("smart_format={}", self.smart_format),
        ];
        if let Some(ep) = self.endpointing {
            params.push(format!("endpointing={}", ep));
        }
        if let Some(uem) = self.utterance_end_ms {
            params.push(format!("utterance_end_ms={}", uem));
        }
        format!("{}?{}", base, params.join("&"))
    }
}

// ---------------------------------------------------------------------------
// Deepgram WebSocket response types
// ---------------------------------------------------------------------------

#[derive(Debug, Deserialize)]
struct DgEnvelope {
    #[serde(rename = "type")]
    msg_type: String,
}

#[derive(Debug, Deserialize)]
struct DgResults {
    is_final: bool,
    channel: DgChannel,
    #[serde(default)]
    from_finalize: bool,
}

#[derive(Debug, Deserialize)]
struct DgChannel {
    alternatives: Vec<DgAlternative>,
}

#[derive(Debug, Deserialize)]
struct DgAlternative {
    transcript: String,
}

// ---------------------------------------------------------------------------
// Internal channel message type
// ---------------------------------------------------------------------------

enum OutgoingMsg {
    Binary(Vec<u8>),
    Text(String),
}

// ---------------------------------------------------------------------------
// Internal state
// ---------------------------------------------------------------------------

struct DeepgramSttState {
    ws_tx:          Option<mpsc::Sender<OutgoingMsg>>,
    send_task:      Option<JoinHandle<()>>,
    receive_task:   Option<JoinHandle<()>>,
    keepalive_task: Option<JoinHandle<()>>,
}

impl DeepgramSttState {
    fn new() -> Self {
        Self {
            ws_tx: None,
            send_task: None,
            receive_task: None,
            keepalive_task: None,
        }
    }
}

// ---------------------------------------------------------------------------
// DeepgramSttHandler
// ---------------------------------------------------------------------------

pub struct DeepgramSttHandler {
    config:      DeepgramSttConfig,
    state:       Arc<Mutex<DeepgramSttState>>,
    billing:     Option<Arc<dyn BillingCollector>>,
    audio_bytes: Arc<AtomicU64>,
}

impl DeepgramSttHandler {
    pub fn new(config: DeepgramSttConfig) -> Self {
        Self {
            config,
            state: Arc::new(Mutex::new(DeepgramSttState::new())),
            billing: None,
            audio_bytes: Arc::new(AtomicU64::new(0)),
        }
    }

    pub fn with_billing(mut self, billing: Arc<dyn BillingCollector>) -> Self {
        self.billing = Some(billing);
        self
    }

    pub fn into_processor(self) -> FrameProcessor {
        FrameProcessor::new("DeepgramStt", Box::new(self), false)
    }
}

// ---------------------------------------------------------------------------
// Connection / disconnection
// ---------------------------------------------------------------------------

impl DeepgramSttHandler {
    async fn connect(&self, processor: FrameProcessor) {
        let url = self.config.ws_url();
        log::info!("DeepgramStt: connecting to {}", url);

        let host = url
            .trim_start_matches("wss://")
            .trim_start_matches("ws://")
            .split('/')
            .next()
            .unwrap_or("api.deepgram.com");

        let request = match Request::builder()
            .uri(&url)
            .header("Host", host)
            .header("Authorization", format!("Token {}", self.config.api_key))
            .header("Connection", "Upgrade")
            .header("Upgrade", "websocket")
            .header("Sec-WebSocket-Version", "13")
            .header(
                "Sec-WebSocket-Key",
                tokio_tungstenite::tungstenite::handshake::client::generate_key(),
            )
            .body(())
        {
            Ok(r) => r,
            Err(e) => {
                let _ = processor
                    .push_error(format!("DeepgramStt: request build failed: {}", e), false)
                    .await;
                return;
            }
        };

        let ws_stream = match tokio_tungstenite::connect_async(request).await {
            Ok((stream, _)) => stream,
            Err(e) => {
                let _ = processor
                    .push_error(format!("DeepgramStt: connect failed: {}", e), false)
                    .await;
                return;
            }
        };

        let (sink, stream) = ws_stream.split();
        let (ws_tx, ws_rx) = mpsc::channel::<OutgoingMsg>(128);

        let send_task = tokio::spawn(run_send_task(sink, ws_rx));

        let billing     = self.billing.clone();
        let audio_bytes = self.audio_bytes.clone();
        let sample_rate = self.config.sample_rate;
        let receive_task =
            tokio::spawn(run_receive_task(stream, processor, billing, audio_bytes, sample_rate));

        let ka_tx = ws_tx.clone();
        let keepalive_task = tokio::spawn(run_keepalive_task(ka_tx));

        let mut state = self.state.lock().await;
        state.ws_tx          = Some(ws_tx);
        state.send_task      = Some(send_task);
        state.receive_task   = Some(receive_task);
        state.keepalive_task = Some(keepalive_task);

        log::info!("DeepgramStt: connected");
    }

    async fn disconnect(&self) {
        let mut state = self.state.lock().await;

        if let Some(tx) = &state.ws_tx {
            let _ = tx
                .send(OutgoingMsg::Text(r#"{"type":"CloseStream"}"#.to_string()))
                .await;
        }

        if let Some(h) = state.keepalive_task.take() { h.abort(); }
        if let Some(h) = state.receive_task.take()   { h.abort(); }
        if let Some(h) = state.send_task.take()       { h.abort(); }
        state.ws_tx = None;

        log::info!("DeepgramStt: disconnected");
    }

    async fn send_audio(&self, audio: &[u8]) {
        self.audio_bytes.fetch_add(audio.len() as u64, Ordering::Relaxed);
        let tx = { self.state.lock().await.ws_tx.clone() };
        if let Some(tx) = tx {
            let _ = tx.send(OutgoingMsg::Binary(audio.to_vec())).await;
        }
    }

    async fn send_finalize(&self) {
        let tx = { self.state.lock().await.ws_tx.clone() };
        if let Some(tx) = tx {
            let _ = tx
                .send(OutgoingMsg::Text(r#"{"type":"Finalize"}"#.to_string()))
                .await;
        }
    }
}

// ---------------------------------------------------------------------------
// FrameHandler impl
// ---------------------------------------------------------------------------

#[async_trait]
impl FrameHandler for DeepgramSttHandler {
    async fn on_process_frame(
        &self,
        processor: &FrameProcessor,
        frame: Frame,
        direction: FrameDirection,
    ) -> Result<()> {
        match &frame.inner {
            FrameInner::System(SystemFrame::Start(_)) => {
                processor.push_frame(frame, direction).await?;
                self.connect(processor.clone()).await;
            }

            FrameInner::System(SystemFrame::InputAudioRaw(ref audio)) => {
                processor.push_frame(frame.clone(), direction).await?;
                self.send_audio(&audio.audio).await;
            }

            FrameInner::System(SystemFrame::VADUserStoppedSpeaking { .. }) => {
                processor.push_frame(frame, direction).await?;
                self.send_finalize().await;
            }

            FrameInner::Control(ControlFrame::End { .. })
            | FrameInner::System(SystemFrame::Cancel { .. }) => {
                self.disconnect().await;
                processor.push_frame(frame, direction).await?;
            }

            _ => {
                processor.push_frame(frame, direction).await?;
            }
        }
        Ok(())
    }

    fn can_generate_metrics(&self) -> bool {
        true
    }
}

// ---------------------------------------------------------------------------
// Background tasks
// ---------------------------------------------------------------------------

type WsSink = futures::stream::SplitSink<
    tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>,
    Message,
>;

type WsStream = futures::stream::SplitStream<
    tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>,
>;

async fn run_send_task(mut sink: WsSink, mut rx: mpsc::Receiver<OutgoingMsg>) {
    while let Some(msg) = rx.recv().await {
        let ws_msg = match msg {
            OutgoingMsg::Binary(bytes) => Message::Binary(bytes.into()),
            OutgoingMsg::Text(text)    => Message::Text(text.into()),
        };
        if sink.send(ws_msg).await.is_err() {
            log::warn!("DeepgramStt: send failed — closing send task");
            break;
        }
    }
    let _ = sink.close().await;
    log::debug!("DeepgramStt: send task exited");
}

async fn run_receive_task(
    mut stream:  WsStream,
    processor:   FrameProcessor,
    billing:     Option<Arc<dyn BillingCollector>>,
    audio_bytes: Arc<AtomicU64>,
    sample_rate: u32,
) {
    log::debug!("DeepgramStt: receive task started");

    while let Some(result) = stream.next().await {
        match result {
            Ok(Message::Text(text)) => {
                handle_message(text.as_str(), &processor, &billing, &audio_bytes, sample_rate).await;
            }
            Ok(Message::Close(_)) => {
                log::info!("DeepgramStt: server closed WebSocket");
                break;
            }
            Err(e) => {
                let _ = processor
                    .push_error(format!("DeepgramStt: receive error: {}", e), false)
                    .await;
                break;
            }
            _ => {}
        }
    }

    log::debug!("DeepgramStt: receive task exited");
}

// Deepgram closes idle connections after ~10 s; send KeepAlive every 8 s.
async fn run_keepalive_task(tx: mpsc::Sender<OutgoingMsg>) {
    loop {
        tokio::time::sleep(tokio::time::Duration::from_secs(8)).await;
        if tx
            .send(OutgoingMsg::Text(r#"{"type":"KeepAlive"}"#.to_string()))
            .await
            .is_err()
        {
            break;
        }
        log::trace!("DeepgramStt: sent KeepAlive");
    }
}

async fn handle_message(
    text:        &str,
    processor:   &FrameProcessor,
    billing:     &Option<Arc<dyn BillingCollector>>,
    audio_bytes: &Arc<AtomicU64>,
    sample_rate: u32,
) {
    log::debug!("DeepgramStt: raw message: {}", text);

    let envelope: DgEnvelope = match serde_json::from_str(text) {
        Ok(e)  => e,
        Err(e) => {
            log::warn!("DeepgramStt: parse error: {} — raw: {}", e, text);
            return;
        }
    };

    match envelope.msg_type.as_str() {
        "Results" => {
            let result: DgResults = match serde_json::from_str(text) {
                Ok(r)  => r,
                Err(e) => {
                    log::warn!("DeepgramStt: Results parse: {}", e);
                    return;
                }
            };

            if !result.is_final {
                return;
            }

            let transcript = match result.channel.alternatives.first() {
                Some(a) => a.transcript.trim().to_string(),
                None    => return,
            };

            if transcript.is_empty() {
                return;
            }

            let bytes = audio_bytes.swap(0, Ordering::Relaxed);
            if bytes > 0 {
                if let Some(bc) = billing {
                    // PCM i16 LE: 2 bytes/sample, mono
                    let duration_ms = (bytes as f64) / (2.0 * sample_rate as f64) * 1000.0;
                    bc.record(BillingEvent::SttUsage {
                        session_id:        bc.session_id(),
                        provider:          "deepgram".to_string(),
                        audio_duration_ms: duration_ms,
                        occurred_at:       Utc::now(),
                    });
                }
            }

            let mut frame_data = TranscriptionData::new(transcript.clone(), "", time_now_iso8601());
            frame_data.finalized = true;

            log::info!(
                "DeepgramStt: transcript='{}' from_finalize={}",
                transcript,
                result.from_finalize,
            );

            let _ = processor
                .push_frame(Frame::transcription(frame_data), FrameDirection::Downstream)
                .await;
        }

        "Metadata"      => log::debug!("DeepgramStt: metadata received"),
        "SpeechStarted" => log::debug!("DeepgramStt: speech started"),
        "UtteranceEnd"  => log::debug!("DeepgramStt: utterance end"),

        "Error" => {
            log::warn!("DeepgramStt: server error: {}", text);
            let _ = processor
                .push_error(format!("DeepgramStt: server error: {}", text), false)
                .await;
        }

        other => log::debug!("DeepgramStt: unknown message type: {}", other),
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn time_now_iso8601() -> String {
    let d = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default();
    format!("{}.{:03}", d.as_secs(), d.subsec_millis())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn ws_url_contains_required_params() {
        let cfg = DeepgramSttConfig::default();
        let url = cfg.ws_url();
        assert!(url.starts_with("wss://api.deepgram.com/v1/listen"), "base url: {url}");
        assert!(url.contains("model=nova-3"),     "model missing: {url}");
        assert!(url.contains("language=en-US"),   "language missing: {url}");
        assert!(url.contains("sample_rate=16000"), "sample_rate missing: {url}");
        assert!(url.contains("encoding=linear16"), "encoding missing: {url}");
        assert!(url.contains("endpointing=300"),   "endpointing missing: {url}");
    }

    #[test]
    fn ws_url_custom_base_url() {
        let cfg = DeepgramSttConfig {
            base_url: Some("wss://my.proxy.example.com/v1/listen".to_string()),
            ..Default::default()
        };
        assert!(cfg.ws_url().starts_with("wss://my.proxy.example.com"));
    }

    #[test]
    fn ws_url_no_endpointing_when_none() {
        let cfg = DeepgramSttConfig {
            endpointing: None,
            ..Default::default()
        };
        assert!(!cfg.ws_url().contains("endpointing"), "endpointing should be absent: {}", cfg.ws_url());
    }

    #[test]
    fn audio_duration_formula_16khz_1000ms() {
        let bytes: u64 = 32_000;
        let sample_rate: u32 = 16_000;
        let ms = (bytes as f64) / (2.0 * sample_rate as f64) * 1000.0;
        assert!((ms - 1000.0).abs() < 0.001, "expected 1000ms, got {ms}");
    }

    #[test]
    fn audio_bytes_counter_resets_on_swap() {
        let counter = Arc::new(AtomicU64::new(0));
        counter.fetch_add(16_000, Ordering::Relaxed);
        let total = counter.swap(0, Ordering::Relaxed);
        assert_eq!(total, 16_000);
        assert_eq!(counter.load(Ordering::Relaxed), 0);
    }

    #[test]
    fn with_billing_sets_field() {
        use crate::billing::NoopBillingCollector;
        let h = DeepgramSttHandler::new(DeepgramSttConfig::default())
            .with_billing(Arc::new(NoopBillingCollector));
        assert!(h.billing.is_some());
    }
}