orra 0.0.2

Context-aware agent session management for any application
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
//! Voice support (TTS/STT).
//!
//! Provides traits and implementations for text-to-speech and
//! speech-to-text capabilities. Designed to work with various
//! backends like OpenAI Whisper, Google Cloud Speech, ElevenLabs, etc.

use async_trait::async_trait;

// ---------------------------------------------------------------------------
// Speech-to-Text
// ---------------------------------------------------------------------------

/// Result of a speech-to-text transcription.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Transcription {
    /// The transcribed text.
    pub text: String,

    /// Detected language code (e.g., "en", "es").
    pub language: Option<String>,

    /// Confidence score from 0.0 to 1.0, if available.
    pub confidence: Option<f64>,

    /// Duration of the audio in seconds.
    pub duration_secs: Option<f64>,

    /// Word-level timestamps, if available.
    pub words: Vec<WordTimestamp>,
}

/// A single word with timing information.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct WordTimestamp {
    pub word: String,
    pub start_secs: f64,
    pub end_secs: f64,
}

/// Options for speech-to-text transcription.
#[derive(Debug, Clone, Default)]
pub struct TranscribeOptions {
    /// Language hint (ISO 639-1 code).
    pub language: Option<String>,

    /// Optional prompt to guide transcription (context/vocabulary hints).
    pub prompt: Option<String>,

    /// Whether to include word-level timestamps.
    pub word_timestamps: bool,

    /// Audio format (e.g., "wav", "mp3", "ogg", "webm").
    pub format: Option<String>,
}

/// Trait for speech-to-text providers.
#[async_trait]
pub trait SpeechToText: Send + Sync {
    /// Transcribe audio data to text.
    async fn transcribe(
        &self,
        audio: &[u8],
        options: &TranscribeOptions,
    ) -> Result<Transcription, VoiceError>;
}

// ---------------------------------------------------------------------------
// Text-to-Speech
// ---------------------------------------------------------------------------

/// Result of a text-to-speech synthesis.
#[derive(Debug, Clone)]
pub struct SynthesizedAudio {
    /// Raw audio data.
    pub data: Vec<u8>,

    /// Audio format (e.g., "mp3", "opus", "wav").
    pub format: String,

    /// Sample rate in Hz.
    pub sample_rate: u32,
}

/// Options for text-to-speech synthesis.
#[derive(Debug, Clone)]
pub struct SynthesizeOptions {
    /// Voice ID or name to use.
    pub voice: String,

    /// Speech speed multiplier (1.0 = normal).
    pub speed: f32,

    /// Output audio format.
    pub format: String,
}

impl Default for SynthesizeOptions {
    fn default() -> Self {
        Self {
            voice: "alloy".into(),
            speed: 1.0,
            format: "mp3".into(),
        }
    }
}

/// Trait for text-to-speech providers.
#[async_trait]
pub trait TextToSpeech: Send + Sync {
    /// Synthesize text into audio.
    async fn synthesize(
        &self,
        text: &str,
        options: &SynthesizeOptions,
    ) -> Result<SynthesizedAudio, VoiceError>;

    /// List available voices.
    async fn list_voices(&self) -> Result<Vec<VoiceInfo>, VoiceError>;
}

/// Information about an available voice.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct VoiceInfo {
    /// Voice identifier.
    pub id: String,

    /// Human-readable name.
    pub name: String,

    /// Language codes this voice supports.
    pub languages: Vec<String>,

    /// Description or tags.
    pub description: Option<String>,
}

// ---------------------------------------------------------------------------
// OpenAI voice implementations
// ---------------------------------------------------------------------------

/// OpenAI Whisper-based speech-to-text.
pub struct WhisperSTT {
    client: reqwest::Client,
    api_key: String,
    model: String,
    api_url: String,
}

impl WhisperSTT {
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            client: reqwest::Client::new(),
            api_key: api_key.into(),
            model: "whisper-1".into(),
            api_url: "https://api.openai.com/v1/audio/transcriptions".into(),
        }
    }

    pub fn with_model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }
}

#[async_trait]
impl SpeechToText for WhisperSTT {
    async fn transcribe(
        &self,
        audio: &[u8],
        options: &TranscribeOptions,
    ) -> Result<Transcription, VoiceError> {
        let format = options.format.as_deref().unwrap_or("wav");

        let mut form = reqwest::multipart::Form::new()
            .text("model", self.model.clone())
            .part(
                "file",
                reqwest::multipart::Part::bytes(audio.to_vec())
                    .file_name(format!("audio.{}", format))
                    .mime_str(&format!("audio/{}", format))
                    .map_err(|e| VoiceError::Request(e.to_string()))?,
            );

        if let Some(lang) = &options.language {
            form = form.text("language", lang.clone());
        }
        if let Some(prompt) = &options.prompt {
            form = form.text("prompt", prompt.clone());
        }
        if options.word_timestamps {
            form = form.text("timestamp_granularities[]", "word".to_string());
            form = form.text("response_format", "verbose_json".to_string());
        }

        let response = self
            .client
            .post(&self.api_url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .multipart(form)
            .send()
            .await
            .map_err(|e| VoiceError::Request(e.to_string()))?;

        if !response.status().is_success() {
            let status = response.status().as_u16();
            let body = response.text().await.unwrap_or_default();
            return Err(VoiceError::Api {
                status,
                message: body,
            });
        }

        let data: serde_json::Value = response
            .json()
            .await
            .map_err(|e| VoiceError::Parse(e.to_string()))?;

        let text = data["text"]
            .as_str()
            .unwrap_or_default()
            .to_string();

        let language = data["language"].as_str().map(String::from);
        let duration_secs = data["duration"].as_f64();

        let words = data["words"]
            .as_array()
            .map(|arr| {
                arr.iter()
                    .filter_map(|w| {
                        Some(WordTimestamp {
                            word: w["word"].as_str()?.to_string(),
                            start_secs: w["start"].as_f64()?,
                            end_secs: w["end"].as_f64()?,
                        })
                    })
                    .collect()
            })
            .unwrap_or_default();

        Ok(Transcription {
            text,
            language,
            confidence: None,
            duration_secs,
            words,
        })
    }
}

/// OpenAI TTS implementation.
pub struct OpenAITTS {
    client: reqwest::Client,
    api_key: String,
    model: String,
    api_url: String,
}

impl OpenAITTS {
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            client: reqwest::Client::new(),
            api_key: api_key.into(),
            model: "tts-1".into(),
            api_url: "https://api.openai.com/v1/audio/speech".into(),
        }
    }

    pub fn with_model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }
}

#[async_trait]
impl TextToSpeech for OpenAITTS {
    async fn synthesize(
        &self,
        text: &str,
        options: &SynthesizeOptions,
    ) -> Result<SynthesizedAudio, VoiceError> {
        let body = serde_json::json!({
            "model": self.model,
            "input": text,
            "voice": options.voice,
            "speed": options.speed,
            "response_format": options.format,
        });

        let response = self
            .client
            .post(&self.api_url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .json(&body)
            .send()
            .await
            .map_err(|e| VoiceError::Request(e.to_string()))?;

        if !response.status().is_success() {
            let status = response.status().as_u16();
            let body = response.text().await.unwrap_or_default();
            return Err(VoiceError::Api {
                status,
                message: body,
            });
        }

        let data = response
            .bytes()
            .await
            .map_err(|e| VoiceError::Request(e.to_string()))?;

        // Default sample rates by format
        let sample_rate = match options.format.as_str() {
            "opus" => 48000,
            "aac" | "flac" => 44100,
            _ => 24000, // mp3, pcm, wav
        };

        Ok(SynthesizedAudio {
            data: data.to_vec(),
            format: options.format.clone(),
            sample_rate,
        })
    }

    async fn list_voices(&self) -> Result<Vec<VoiceInfo>, VoiceError> {
        // OpenAI has a fixed set of voices
        Ok(vec![
            VoiceInfo {
                id: "alloy".into(),
                name: "Alloy".into(),
                languages: vec!["en".into()],
                description: Some("Neutral and balanced".into()),
            },
            VoiceInfo {
                id: "echo".into(),
                name: "Echo".into(),
                languages: vec!["en".into()],
                description: Some("Warm and engaging".into()),
            },
            VoiceInfo {
                id: "fable".into(),
                name: "Fable".into(),
                languages: vec!["en".into()],
                description: Some("Expressive and dynamic".into()),
            },
            VoiceInfo {
                id: "onyx".into(),
                name: "Onyx".into(),
                languages: vec!["en".into()],
                description: Some("Deep and authoritative".into()),
            },
            VoiceInfo {
                id: "nova".into(),
                name: "Nova".into(),
                languages: vec!["en".into()],
                description: Some("Friendly and upbeat".into()),
            },
            VoiceInfo {
                id: "shimmer".into(),
                name: "Shimmer".into(),
                languages: vec!["en".into()],
                description: Some("Clear and precise".into()),
            },
        ])
    }
}

// ---------------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------------

#[derive(Debug, thiserror::Error)]
pub enum VoiceError {
    #[error("request failed: {0}")]
    Request(String),

    #[error("API error (status {status}): {message}")]
    Api { status: u16, message: String },

    #[error("failed to parse response: {0}")]
    Parse(String),

    #[error("unsupported format: {0}")]
    UnsupportedFormat(String),
}

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

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

    #[test]
    fn transcription_basic() {
        let t = Transcription {
            text: "Hello world".into(),
            language: Some("en".into()),
            confidence: Some(0.95),
            duration_secs: Some(1.5),
            words: vec![],
        };
        assert_eq!(t.text, "Hello world");
        assert_eq!(t.language.as_deref(), Some("en"));
    }

    #[test]
    fn transcription_serialization() {
        let t = Transcription {
            text: "test".into(),
            language: None,
            confidence: None,
            duration_secs: None,
            words: vec![WordTimestamp {
                word: "test".into(),
                start_secs: 0.0,
                end_secs: 0.5,
            }],
        };

        let json = serde_json::to_string(&t).unwrap();
        let deser: Transcription = serde_json::from_str(&json).unwrap();
        assert_eq!(deser.text, "test");
        assert_eq!(deser.words.len(), 1);
    }

    #[test]
    fn default_transcribe_options() {
        let opts = TranscribeOptions::default();
        assert!(opts.language.is_none());
        assert!(opts.prompt.is_none());
        assert!(!opts.word_timestamps);
    }

    #[test]
    fn default_synthesize_options() {
        let opts = SynthesizeOptions::default();
        assert_eq!(opts.voice, "alloy");
        assert_eq!(opts.speed, 1.0);
        assert_eq!(opts.format, "mp3");
    }

    #[test]
    fn voice_info_serialization() {
        let info = VoiceInfo {
            id: "alloy".into(),
            name: "Alloy".into(),
            languages: vec!["en".into()],
            description: Some("Neutral".into()),
        };

        let json = serde_json::to_string(&info).unwrap();
        let deser: VoiceInfo = serde_json::from_str(&json).unwrap();
        assert_eq!(deser.id, "alloy");
    }

    #[test]
    fn synthesized_audio_basic() {
        let audio = SynthesizedAudio {
            data: vec![0u8; 100],
            format: "mp3".into(),
            sample_rate: 24000,
        };
        assert_eq!(audio.data.len(), 100);
        assert_eq!(audio.sample_rate, 24000);
    }

    #[test]
    fn voice_error_display() {
        let err = VoiceError::Api {
            status: 400,
            message: "bad request".into(),
        };
        let msg = err.to_string();
        assert!(msg.contains("400"));
        assert!(msg.contains("bad request"));
    }

    #[test]
    fn whisper_construction() {
        let stt = WhisperSTT::new("key").with_model("whisper-large-v3");
        assert_eq!(stt.model, "whisper-large-v3");
    }

    #[test]
    fn openai_tts_construction() {
        let tts = OpenAITTS::new("key").with_model("tts-1-hd");
        assert_eq!(tts.model, "tts-1-hd");
    }

    #[tokio::test]
    async fn openai_tts_list_voices() {
        let tts = OpenAITTS::new("fake-key");
        let voices = tts.list_voices().await.unwrap();
        assert_eq!(voices.len(), 6);
        assert!(voices.iter().any(|v| v.id == "alloy"));
        assert!(voices.iter().any(|v| v.id == "nova"));
    }
}