use super::{
AudioError, AudioFormat, SpeechProvider, SpeechRequest, SpeechResponse, Voice, VoiceInfo,
VoicePreview, VoiceSource, audio_media,
};
use crate::voice_store::{StoredVoice, VoiceStore};
use agentai_core::message::{Media, MediaSource};
use agentai_core::{HttpClient, HttpError, HttpRequest, ReqwestClient};
use async_trait::async_trait;
use serde::Serialize;
use std::sync::Arc;
pub const DEFAULT_BASE_URL: &str = "http://localhost:8000/v1";
pub const ENDPOINT_ENV: &str = "OMLX_ENDPOINT";
pub const API_KEY_ENV: &str = "OMLX_API_KEY";
#[derive(Clone)]
pub struct OmlxProvider {
model: String,
api_key: String,
base_url: String,
client: Arc<dyn HttpClient>,
store: Option<Arc<dyn VoiceStore>>,
}
impl std::fmt::Debug for OmlxProvider {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OmlxProvider")
.field("model", &self.model)
.field("base_url", &self.base_url)
.finish_non_exhaustive()
}
}
impl OmlxProvider {
pub fn new(
model: impl Into<String>,
api_key: impl Into<String>,
base_url: impl Into<String>,
) -> Self {
Self {
model: model.into(),
api_key: api_key.into(),
base_url: base_url.into().trim_end_matches('/').to_string(),
client: Arc::new(ReqwestClient::new()),
store: None,
}
}
pub fn from_env(model: impl Into<String>) -> Result<Self, AudioError> {
Ok(Self::from_lookup(model, |name| std::env::var(name).ok()))
}
fn from_lookup(model: impl Into<String>, lookup: impl Fn(&str) -> Option<String>) -> Self {
Self::new(
model,
lookup(API_KEY_ENV).unwrap_or_default(),
lookup(ENDPOINT_ENV).unwrap_or_else(|| DEFAULT_BASE_URL.to_string()),
)
}
pub fn with_voice_store(mut self, store: impl VoiceStore) -> Self {
self.store = Some(Arc::new(store));
self
}
#[cfg(test)]
pub(crate) fn with_client(mut self, client: Arc<dyn HttpClient>) -> Self {
self.client = client;
self
}
async fn resolve(&self, request: &SpeechRequest) -> Result<Option<StoredVoice>, AudioError> {
let Some(Voice::Id(id)) = request.single_voice()? else {
return Ok(None);
};
let store = self
.store
.as_ref()
.ok_or(AudioError::Unsupported("voice IDs without a voice store"))?;
let voice = store
.get(id)
.await?
.ok_or_else(|| AudioError::InvalidRequest(format!("no cloned voice with id '{id}'")))?;
Ok(Some(voice))
}
}
#[derive(Serialize)]
struct SpeechBody<'a> {
model: &'a str,
input: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
response_format: Option<&'static str>,
#[serde(skip_serializing_if = "Option::is_none")]
speed: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
language: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
instructions: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
ref_audio: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
ref_text: Option<&'a str>,
}
fn format_name(format: AudioFormat) -> &'static str {
match format {
AudioFormat::Mp3 => "mp3",
AudioFormat::Wav => "wav",
AudioFormat::Opus => "opus",
AudioFormat::Flac => "flac",
AudioFormat::Aac => "aac",
AudioFormat::Pcm => "pcm",
}
}
#[async_trait]
impl SpeechProvider for OmlxProvider {
fn model(&self) -> Option<&str> {
Some(&self.model)
}
async fn generate(&self, request: SpeechRequest) -> Result<SpeechResponse, AudioError> {
request.without_timestamps()?;
let response_format = request.format.map(format_name);
let cloned = self.resolve(&request).await?;
let instructions = request.instructions.clone();
let ref_audio = match cloned.as_ref().map(|voice| &voice.sample.source) {
Some(MediaSource::Base64(data)) => Some(data.as_str()),
Some(MediaSource::Url(_)) => {
return Err(AudioError::InvalidRequest(
"the stored voice sample is not base64".to_string(),
));
}
None => None,
};
let ref_text = cloned
.as_ref()
.and_then(|voice| voice.transcript.as_deref());
let body = SpeechBody {
model: &self.model,
input: &request.text,
response_format,
speed: request.speed,
language: request.language.as_deref(),
instructions,
ref_audio,
ref_text,
};
let http = HttpRequest::post(format!("{}/audio/speech", self.base_url))
.with_json(&body)?
.with_bearer_token(&self.api_key);
let response = self.client.send(http).await?.error_for_status()?;
let format = request.format.unwrap_or(AudioFormat::Wav);
let mut audio = audio_media(&response, format.content_type())?;
audio.name = request.name;
Ok(SpeechResponse::new(audio))
}
fn supports_voice_cloning(&self) -> bool {
self.store.is_some()
}
async fn create_voice(
&self,
name: &str,
source: VoiceSource,
description: Option<&str>,
) -> Result<String, AudioError> {
let VoiceSource::Samples {
samples,
transcript,
} = source
else {
return Err(AudioError::Unsupported("saving a designed voice"));
};
let transcript = transcript.as_deref();
let store = self.store.as_ref().ok_or(AudioError::Unsupported(
"voice cloning without a voice store",
))?;
let [sample] = samples.as_slice() else {
return Err(AudioError::InvalidRequest(format!(
"oMLX clones from exactly one sample, got {}",
samples.len()
)));
};
let bytes = sample.bytes(&*self.client).await.map_err(|err| match err {
HttpError::InvalidMedia(message) => AudioError::InvalidRequest(message),
err => err.into(),
})?;
let mut stored = Media::from_bytes(sample.content_type.as_str(), bytes);
stored.name = sample.name.clone();
let mut voice = StoredVoice::new(name, stored);
voice.description = description.map(str::to_string);
voice.transcript = transcript.map(str::to_string);
Ok(store.save(voice).await?)
}
fn supports_voice_design(&self) -> bool {
true
}
async fn design_voice(
&self,
description: &str,
text: Option<&str>,
count: usize,
) -> Result<Vec<VoicePreview>, AudioError> {
let text = text.ok_or_else(|| {
AudioError::InvalidRequest(
"oMLX designs a voice by speaking a text; give one".to_string(),
)
})?;
let mut previews = Vec::with_capacity(count);
for _ in 0..count {
let response = self
.generate(SpeechRequest::new(text).with_instructions(description))
.await?;
let mut preview = VoicePreview::new(response.audio).with_text(text);
if let Some(rate) = response.sample_rate {
preview = preview.with_sample_rate(rate);
}
previews.push(preview);
}
Ok(previews)
}
async fn list_voices(&self) -> Result<Vec<VoiceInfo>, AudioError> {
let store = self.store.as_ref().ok_or(AudioError::Unsupported(
"listing voices without a voice store",
))?;
Ok(store
.list()
.await?
.into_iter()
.map(|(id, voice)| {
let mut info = VoiceInfo::new(id, voice.name).with_cloned(true);
info.description = voice.description;
info
})
.collect())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::provider::{AudioError, SpeechProvider, SpeechRequest, Voice, VoiceInfo};
use crate::{MemoryVoiceStore, VoiceStore};
use agentai_core::HttpResponse;
use agentai_core::message::{Media, MediaSource};
use agentai_core::mock::{MockHttp, body_json};
use serde_json::json;
use std::sync::Arc;
fn audio() -> HttpResponse {
HttpResponse::new(200, b"audio".to_vec())
}
fn provider(http: &MockHttp) -> OmlxProvider {
OmlxProvider::new("m", "", "http://local/v1").with_client(Arc::new(http.clone()))
}
fn sample() -> Media {
Media::from_bytes("audio/wav", b"WAV")
}
async fn cloned(http: &MockHttp, transcript: Option<&str>) -> OmlxProvider {
let p = provider(http).with_voice_store(MemoryVoiceStore::new());
let id = p
.create_voice(
"Adam",
VoiceSource::samples(vec![sample()], transcript),
None,
)
.await
.unwrap();
assert_eq!(id, "voice-1");
p
}
#[tokio::test]
async fn a_sample_id_is_unsupported() {
let http = MockHttp::new(vec![]);
let err = cloned(&http, None)
.await
.create_voice("a", VoiceSource::SampleId("g1".into()), None)
.await
.unwrap_err();
assert!(matches!(err, AudioError::Unsupported(_)));
assert_eq!(http.request_count(), 0);
}
#[tokio::test]
async fn generate_without_a_voice_sends_no_voice_field() {
let http = MockHttp::new(vec![audio()]);
let r = provider(&http)
.generate(SpeechRequest::new("hi"))
.await
.unwrap();
assert_eq!(http.request(0).url, "http://local/v1/audio/speech");
assert_eq!(
body_json(&http.request(0)),
json!({"model": "m", "input": "hi"})
);
assert_eq!(r.audio.content_type, "audio/wav");
assert_eq!(r.sample_rate, None);
}
#[test]
fn endpoint_keeps_v1() {
let p = OmlxProvider::from_lookup("m", |name| {
(name == ENDPOINT_ENV).then(|| "http://mini:8000/v1/".to_string())
})
.with_client(Arc::new(MockHttp::new(vec![audio()])));
assert_eq!(p.base_url, "http://mini:8000/v1");
assert_eq!(
OmlxProvider::from_lookup("m", |_| None).base_url,
DEFAULT_BASE_URL
);
}
#[tokio::test]
async fn create_voice_then_speak_with_it() {
let http = MockHttp::new(vec![audio()]);
let store = MemoryVoiceStore::new();
let p = provider(&http).with_voice_store(store.clone());
assert!(p.supports_voice_cloning());
let id = p
.create_voice(
"Adam",
VoiceSource::samples(vec![sample()], Some("hello")),
None,
)
.await
.unwrap();
assert_eq!(id, "voice-1");
assert_eq!(http.request_count(), 0);
p.generate(SpeechRequest::new("hi").with_voice(Voice::id(&id)))
.await
.unwrap();
let body = body_json(&http.request(0));
assert_eq!(body["ref_audio"], json!("V0FW"));
assert_eq!(body["ref_text"], json!("hello"));
assert!(body.get("voice").is_none());
}
#[tokio::test]
async fn a_missing_transcript_sends_no_ref_text() {
let http = MockHttp::new(vec![audio()]);
let p = cloned(&http, None).await;
p.generate(SpeechRequest::new("hi").with_voice(Voice::id("voice-1")))
.await
.unwrap();
let body = body_json(&http.request(0));
assert_eq!(body["ref_audio"], json!("V0FW"));
assert!(body.get("ref_text").is_none());
}
#[tokio::test]
async fn the_default_voice_resolves_like_a_voice() {
let http = MockHttp::new(vec![audio()]);
let p = cloned(&http, Some("hello")).await;
p.generate(SpeechRequest::new("hi").with_default_voice(Voice::id("voice-1")))
.await
.unwrap();
assert_eq!(body_json(&http.request(0))["ref_audio"], json!("V0FW"));
}
#[tokio::test]
async fn without_a_store() {
let http = MockHttp::new(vec![audio()]);
let p = provider(&http);
assert!(!p.supports_voice_cloning());
assert!(matches!(
p.create_voice("A", VoiceSource::samples(vec![sample()], None), None)
.await,
Err(AudioError::Unsupported(_))
));
assert!(matches!(
p.generate(SpeechRequest::new("hi").with_voice(Voice::id("x")))
.await,
Err(AudioError::Unsupported(_))
));
assert!(matches!(
p.list_voices().await,
Err(AudioError::Unsupported(_))
));
assert_eq!(http.request_count(), 0);
p.generate(SpeechRequest::new("hi").with_instructions("x"))
.await
.unwrap();
assert_eq!(body_json(&http.request(0))["instructions"], json!("x"));
}
#[tokio::test]
async fn unknown_id_with_a_store() {
let http = MockHttp::new(vec![]);
let p = provider(&http).with_voice_store(MemoryVoiceStore::new());
assert!(matches!(
p.generate(SpeechRequest::new("hi").with_voice(Voice::id("nope")))
.await,
Err(AudioError::InvalidRequest(_))
));
assert_eq!(http.request_count(), 0);
}
#[tokio::test]
async fn exactly_one_sample() {
let http = MockHttp::new(vec![]);
let store = MemoryVoiceStore::new();
let p = provider(&http).with_voice_store(store.clone());
for samples in [vec![], vec![sample(), sample()]] {
assert!(matches!(
p.create_voice("A", VoiceSource::samples(samples.clone(), None), None)
.await,
Err(AudioError::InvalidRequest(_))
));
}
assert!(store.list().await.unwrap().is_empty());
}
#[tokio::test]
async fn url_samples_are_downloaded_once() {
let http = MockHttp::new(vec![HttpResponse::new(200, b"WAV".to_vec()), audio()]);
let store = MemoryVoiceStore::new();
let p = provider(&http).with_voice_store(store.clone());
let url_sample = Media::from_url("audio/wav", "http://x/a.wav");
let id = p
.create_voice("A", VoiceSource::samples(vec![url_sample], None), None)
.await
.unwrap();
let stored = store.get(&id).await.unwrap().unwrap();
assert_eq!(
stored.sample.source,
MediaSource::Base64("V0FW".to_string())
);
p.generate(SpeechRequest::new("hi").with_voice(Voice::id(&id)))
.await
.unwrap();
assert_eq!(http.request_count(), 2);
assert_eq!(http.request(0).url, "http://x/a.wav");
assert_eq!(http.request(1).url, "http://local/v1/audio/speech");
}
#[tokio::test]
async fn failed_download_stores_nothing() {
let http = MockHttp::new(vec![HttpResponse::new(404, b"gone".to_vec())]);
let store = MemoryVoiceStore::new();
let p = provider(&http).with_voice_store(store.clone());
let url_sample = Media::from_url("audio/wav", "http://x/a.wav");
let result = p
.create_voice("A", VoiceSource::samples(vec![url_sample], None), None)
.await;
assert!(matches!(result, Err(AudioError::Api { status: 404, .. })));
assert!(store.list().await.unwrap().is_empty());
}
#[tokio::test]
async fn list_voices_reads_the_store() {
let http = MockHttp::new(vec![]);
let p = provider(&http).with_voice_store(MemoryVoiceStore::new());
p.create_voice(
"Adam",
VoiceSource::samples(vec![sample()], None),
Some("calm"),
)
.await
.unwrap();
assert_eq!(
p.list_voices().await.unwrap(),
vec![
VoiceInfo::new("voice-1", "Adam")
.with_description("calm")
.with_cloned(true)
]
);
}
#[tokio::test]
async fn create_voice_rejects_a_data_url_sample() {
let p = provider(&MockHttp::new(vec![])).with_voice_store(MemoryVoiceStore::new());
let sample = Media::from_base64("audio/wav", "data:audio/wav;base64,V0FW");
let err = p
.create_voice("A", VoiceSource::samples(vec![sample], None), None)
.await
.unwrap_err();
assert!(matches!(err, AudioError::InvalidRequest(_)));
}
#[tokio::test]
async fn design_voice_speaks_the_text_once_per_candidate() {
let http = MockHttp::new(vec![audio(), audio()]);
let previews = provider(&http)
.design_voice("deep calm voice", Some("Hello"), 2)
.await
.unwrap();
assert_eq!(previews.len(), 2);
for (i, preview) in previews.iter().enumerate() {
let req = http.request(i);
assert_eq!(req.url, "http://local/v1/audio/speech");
assert_eq!(
body_json(&req),
json!({"model": "m", "input": "Hello", "instructions": "deep calm voice"})
);
assert_eq!(preview.audio.content_type, "audio/wav");
assert_eq!(preview.audio.bytes(&http).await.unwrap(), b"audio");
assert_eq!(preview.text.as_deref(), Some("Hello"));
assert_eq!(preview.sample_id, None);
}
}
#[tokio::test]
async fn design_voice_needs_a_text() {
let http = MockHttp::new(vec![]);
let err = provider(&http)
.design_voice("x", None, 1)
.await
.unwrap_err();
assert!(matches!(err, AudioError::InvalidRequest(_)));
assert_eq!(http.request_count(), 0);
}
#[tokio::test]
async fn a_designed_voice_can_be_saved_and_spoken() {
let http = MockHttp::new(vec![audio(), audio()]);
let p = provider(&http).with_voice_store(MemoryVoiceStore::new());
let previews = p.design_voice("calm", Some("Hello"), 1).await.unwrap();
let id = p
.create_voice("calm", VoiceSource::from(&previews[0]), None)
.await
.unwrap();
p.generate(SpeechRequest::new("hi").with_voice(Voice::id(&id)))
.await
.unwrap();
let body = body_json(&http.request(1));
assert_eq!(body["ref_text"], json!("Hello"));
assert_eq!(body["ref_audio"], json!("YXVkaW8="));
}
}