agentai-audio 0.7.0

Text-to-speech and voice cloning for AgentAI agents.
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
//! Agent tools (feature `toolbox`): [`SpeechToolBox`].

use crate::generator::SpeechGenerator;
use crate::provider::{AudioError, SpeechRequest, SpeechResponse, Voice, VoiceInfo};
use agentai::tool::{Tool, ToolBox, ToolError, ToolOutput, ToolResult, toolbox};
use agentai_core::storage::MediaStorage;
use serde::Serialize;

/// What [`SpeechToolBox`] sends back to the model.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum SpeechToolOutput {
    /// Only a JSON text with what was made (ID, type, size in bytes, model, voice, ...),
    /// without the audio. Most models cannot listen to audio, so this is the default.
    #[default]
    Metadata,
    /// The audio itself and a short text with its ID. The model must accept audio.
    Media,
}

/// [`ToolBox`] that lets an agent speak texts with a [`SpeechGenerator`].
///
/// Tools:
/// - `generate_speech`: speaks a text and saves the audio in the storage.
/// - `list_voices`: lists the voices the model can choose from.
///
/// No tool clones voices: that stays in your code ([`SpeechGenerator::create_voice`]).
///
/// The model names the audio (`name`), and the storage chooses the ID. What else the model
/// gets is set with [`with_output`](SpeechToolBox::with_output). The voices it may use are
/// limited with [`with_voices`](SpeechToolBox::with_voices); without it any voice ID or
/// description goes to the provider.
///
/// ```rust,no_run
/// # use agentai_audio::{SpeechGenerator, SpeechToolBox, Voice};
/// # use agentai_audio::storage::MemoryStorage;
/// # fn main() -> Result<(), agentai_audio::AudioError> {
/// let generator = SpeechGenerator::new("openai::gpt-4o-mini-tts")?
///     .with_default_voice(Voice::id("alloy"));
/// let speech = SpeechToolBox::new(generator, MemoryStorage::new());
/// # Ok(())
/// # }
/// ```
pub struct SpeechToolBox {
    generator: SpeechGenerator,
    voices: Option<Vec<VoiceInfo>>,
    output: SpeechToolOutput,
}

impl SpeechToolBox {
    /// Creates the toolbox. Every audio is saved in `storage`.
    pub fn new(generator: SpeechGenerator, storage: impl MediaStorage) -> Self {
        Self {
            generator: generator.with_storage(storage),
            voices: None,
            output: SpeechToolOutput::default(),
        }
    }

    /// Limits the voices to this list: `list_voices` returns it, and a voice the model gives
    /// that is not in it is used as a description of the voice, or rejected when the provider
    /// takes none. Use it whenever cloned voices exist: without it the model can use any stored
    /// or account voice by its ID.
    pub fn with_voices(mut self, voices: Vec<VoiceInfo>) -> Self {
        self.voices = Some(voices);
        self
    }

    /// Sets what the model gets back, see [`SpeechToolOutput`].
    pub fn with_output(mut self, output: SpeechToolOutput) -> Self {
        self.output = output;
        self
    }

    /// The voice for what the model wrote in `voice`.
    fn voice(&self, voice: Option<&str>) -> ToolResult<Option<Voice>> {
        let Some(voice) = voice.map(str::trim).filter(|v| !v.is_empty()) else {
            return Ok(None);
        };
        let Some(voices) = &self.voices else {
            return Ok(Some(Voice::auto(voice)));
        };
        if voices.iter().any(|v| v.id == voice) {
            Ok(Some(Voice::id(voice)))
        } else if self.generator.supports_voice_description() {
            Ok(Some(Voice::description(voice)))
        } else {
            Err(ToolError::Failed(format!(
                "'{voice}' is not a known voice ID; pick one from `list_voices`"
            )))
        }
    }

    fn output(&self, response: SpeechResponse, voice: Option<&str>) -> ToolResult<ToolOutput> {
        match self.output {
            SpeechToolOutput::Metadata => {
                let metadata = SpeechMetadata {
                    id: response.id.as_deref(),
                    content_type: &response.audio.content_type,
                    bytes: response.audio.byte_len(),
                    model: self.generator.model(),
                    voice,
                    sample_rate: response.sample_rate,
                };
                let json = serde_json::to_string(&metadata).map_err(ToolError::fatal)?;
                Ok(ToolOutput::from_text(json))
            }
            SpeechToolOutput::Media => {
                let text = match &response.id {
                    Some(id) => format!("Created the audio with ID {id}."),
                    None => "Created the audio.".to_string(),
                };
                Ok(ToolOutput::from_text(text).with_media(response.audio))
            }
        }
    }
}

#[toolbox]
impl SpeechToolBox {
    /// Generates speech from text and saves it as an audio file.
    #[tool]
    async fn generate_speech(
        &self,
        /// The text to speak.
        text: String,
        /// Short file name for the new audio, for example `greeting`. The storage may add to it
        /// to make the ID unique.
        name: String,
        /// A voice ID from `list_voices`, or, when `descriptions_supported` is true, a
        /// description of the voice such as `warm low female`. Leave it out for the default
        /// voice.
        voice: Option<String>,
        /// How to speak, for example the tone, pace or emotion. Leave it out when not needed.
        instructions: Option<String>,
    ) -> ToolResult<ToolOutput> {
        let voice_text = voice.as_deref().map(str::trim).filter(|v| !v.is_empty());
        let mut request = SpeechRequest::new(text).with_name(name);
        if let Some(voice) = self.voice(voice.as_deref())? {
            request = request.with_voice(voice);
        }
        if let Some(instructions) = instructions.filter(|i| !i.trim().is_empty()) {
            request = request.with_instructions(instructions);
        }
        let response = self
            .generator
            .generate_with(request)
            .await
            .map_err(tool_error)?;
        self.output(response, voice_text)
    }

    /// Lists the voices you can use in `generate_speech`, and tells whether a description of a
    /// voice works there too.
    #[tool]
    async fn list_voices(&self) -> ToolResult<String> {
        let voices = match &self.voices {
            Some(voices) => voices.clone(),
            None => match self.generator.list_voices().await {
                Ok(voices) => voices,
                Err(AudioError::Unsupported(_)) => Vec::new(),
                Err(err) => return Err(tool_error(err)),
            },
        };
        let list = VoiceList {
            voices: &voices,
            descriptions_supported: self.generator.supports_voice_description(),
        };
        serde_json::to_string(&list).map_err(ToolError::fatal)
    }
}

/// What [`SpeechToolOutput::Metadata`] sends; `None` fields are left out.
#[derive(Serialize)]
struct SpeechMetadata<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    id: Option<&'a str>,
    content_type: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    bytes: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    model: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    voice: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    sample_rate: Option<u32>,
}

/// What `list_voices` sends.
#[derive(Serialize)]
struct VoiceList<'a> {
    voices: &'a [VoiceInfo],
    descriptions_supported: bool,
}

/// Rejected credentials cannot be fixed by the model; anything else (a missing voice, a
/// refused text, a failed request or save) is reported to it.
fn tool_error(err: AudioError) -> ToolError {
    if err.is_auth() || matches!(err, AudioError::MissingApiKey(_)) {
        ToolError::Configuration(err.to_string())
    } else {
        ToolError::Failed(err.to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::provider::elevenlabs::ElevenLabsProvider;
    use crate::provider::omlx::OmlxProvider;
    use crate::provider::openai::OpenAiSpeechProvider;
    use agentai::tool::ToolReturn;
    use agentai_core::HttpResponse;
    use agentai_core::message::Part;
    use agentai_core::mock::{MockHttp, body_json, json_response};
    use agentai_core::storage::MemoryStorage;
    use serde_json::{Value, json};
    use std::sync::Arc;

    fn tool_call(name: &str, arguments: Value) -> agentai::ToolCall {
        agentai::ToolCall::new("call_1", name, arguments)
    }

    fn audio() -> HttpResponse {
        HttpResponse::new(200, b"audio".to_vec())
    }

    fn openai(http: &MockHttp, base_url: &str) -> OpenAiSpeechProvider {
        OpenAiSpeechProvider::new("gpt-4o-mini-tts", "key", base_url)
            .with_client(Arc::new(http.clone()))
    }

    fn official(http: &MockHttp) -> OpenAiSpeechProvider {
        openai(http, "https://api.openai.com/v1")
    }

    fn toolbox(provider: impl crate::provider::SpeechProvider) -> SpeechToolBox {
        SpeechToolBox::new(
            SpeechGenerator::from_provider(provider),
            MemoryStorage::new(),
        )
    }

    async fn call_done(toolbox: &impl ToolBox, name: &str, arguments: Value) -> ToolOutput {
        match toolbox
            .call_tool(&tool_call(name, arguments))
            .await
            .unwrap()
        {
            ToolReturn::Done(output) => output,
            other => panic!("expected ToolReturn::Done, got {other:?}"),
        }
    }

    fn text_json(output: &ToolOutput) -> Value {
        serde_json::from_str(&output.text().unwrap()).unwrap()
    }

    #[tokio::test]
    async fn generate_speech_returns_metadata_by_default() {
        let http = MockHttp::new(vec![audio()]);
        let toolbox = toolbox(official(&http));

        let output = call_done(
            &toolbox,
            "generate_speech",
            json!({"text": "hi", "name": "greeting", "voice": "alloy"}),
        )
        .await;

        assert!(output.media().is_empty());
        assert_eq!(output.parts.len(), 1);
        assert_eq!(
            text_json(&output),
            json!({
                "id": "1-greeting.mp3",
                "content_type": "audio/mpeg",
                "bytes": 5,
                "model": "gpt-4o-mini-tts",
                "voice": "alloy",
            })
        );
    }

    #[tokio::test]
    async fn media_output_includes_the_audio() {
        let http = MockHttp::new(vec![audio()]);
        let toolbox = toolbox(official(&http)).with_output(SpeechToolOutput::Media);

        let output = call_done(
            &toolbox,
            "generate_speech",
            json!({"text": "hi", "name": "greeting", "voice": "alloy"}),
        )
        .await;

        assert!(output.text().unwrap().contains("1-greeting.mp3"));
        assert!(matches!(output.parts.last(), Some(Part::Media(_))));
        assert_eq!(output.media().len(), 1);
    }

    #[tokio::test]
    async fn a_blank_voice_means_the_default() {
        let blanks = [
            json!({"text": "hi", "name": "x", "voice": ""}),
            json!({"text": "hi", "name": "x", "voice": "   "}),
            json!({"text": "hi", "name": "x"}),
            json!({"text": "hi", "name": "x", "voice": null}),
        ];
        for arguments in blanks {
            let http = MockHttp::new(vec![audio()]);
            let generator = SpeechGenerator::from_provider(official(&http))
                .with_default_voice(Voice::id("nova"));
            let toolbox = SpeechToolBox::new(generator, MemoryStorage::new());

            toolbox
                .call_tool(&tool_call("generate_speech", arguments.clone()))
                .await
                .unwrap();

            assert_eq!(
                body_json(&http.request(0))["voice"],
                json!("nova"),
                "{arguments}"
            );
        }
    }

    #[tokio::test]
    async fn the_voice_list_is_an_allowlist() {
        let http = MockHttp::new(vec![audio(), audio()]);
        let generator =
            SpeechGenerator::from_provider(official(&http)).with_default_voice(Voice::id("nova"));
        let toolbox = SpeechToolBox::new(generator, MemoryStorage::new())
            .with_voices(vec![VoiceInfo::new("voice_ok", "OK")]);

        for voice in ["voice_ok", "voice_hidden"] {
            toolbox
                .call_tool(&tool_call(
                    "generate_speech",
                    json!({"text": "hi", "name": "x", "voice": voice}),
                ))
                .await
                .unwrap();
        }

        let listed = body_json(&http.request(0));
        assert_eq!(listed["voice"], json!({"id": "voice_ok"}));
        let hidden = body_json(&http.request(1));
        assert_eq!(hidden["voice"], json!("nova"));
        assert_eq!(hidden["instructions"], json!("voice_hidden"));
    }

    #[tokio::test]
    async fn an_unlisted_id_fails_without_descriptions() {
        let http = MockHttp::new(vec![]);
        let provider = ElevenLabsProvider::new("eleven_v3", "key", "https://api.test")
            .with_client(Arc::new(http.clone()));
        let toolbox = toolbox(provider).with_voices(vec![VoiceInfo::new("abc", "Abc")]);

        let result = toolbox
            .call_tool(&tool_call(
                "generate_speech",
                json!({"text": "hi", "name": "x", "voice": "hidden"}),
            ))
            .await;

        assert!(matches!(result, Err(ToolError::Failed(m)) if m.contains("list_voices")));
        assert_eq!(http.request_count(), 0);
    }

    #[tokio::test]
    async fn instructions_reach_the_provider() {
        let http = MockHttp::new(vec![audio()]);
        let toolbox = toolbox(official(&http));

        toolbox
            .call_tool(&tool_call(
                "generate_speech",
                json!({"text": "hi", "name": "x", "voice": "alloy", "instructions": "slowly"}),
            ))
            .await
            .unwrap();

        assert_eq!(body_json(&http.request(0))["instructions"], json!("slowly"));
    }

    #[tokio::test]
    async fn provider_errors_are_recoverable_except_auth() {
        let http = MockHttp::new(vec![json_response(
            401,
            json!({"error": {"message": "bad"}}),
        )]);
        let generator =
            SpeechGenerator::from_provider(official(&http)).with_default_voice(Voice::id("nova"));
        let toolbox = SpeechToolBox::new(generator, MemoryStorage::new());
        let result = toolbox
            .call_tool(&tool_call(
                "generate_speech",
                json!({"text": "hi", "name": "x", "voice": "nova"}),
            ))
            .await;
        assert!(matches!(result, Err(ToolError::Configuration(_))));

        let no_default = toolbox_without_default();
        let result = no_default
            .call_tool(&tool_call(
                "generate_speech",
                json!({"text": "hi", "name": "x"}),
            ))
            .await;
        assert!(matches!(result, Err(ToolError::Failed(_))));
    }

    fn toolbox_without_default() -> SpeechToolBox {
        let http = MockHttp::new(vec![]);
        toolbox(official(&http))
    }

    #[tokio::test]
    async fn list_voices_prefers_the_app_list() {
        let http = MockHttp::new(vec![]);
        let all = toolbox(official(&http));
        let output = call_done(&all, "list_voices", json!({})).await;
        let json = text_json(&output);
        assert_eq!(json["voices"].as_array().unwrap().len(), 13);
        assert_eq!(json["descriptions_supported"], json!(true));

        let own = toolbox(official(&http)).with_voices(vec![VoiceInfo::new("a", "A")]);
        let output = call_done(&own, "list_voices", json!({})).await;
        let voices = text_json(&output)["voices"].clone();
        assert_eq!(voices.as_array().unwrap().len(), 1);
        assert_eq!(voices[0]["id"], json!("a"));
    }

    #[tokio::test]
    async fn list_voices_without_support_is_empty_not_an_error() {
        let http = MockHttp::new(vec![]);
        let provider =
            OmlxProvider::new("model", "", "http://local/v1").with_client(Arc::new(http));
        let output = call_done(&toolbox(provider), "list_voices", json!({})).await;

        let json = text_json(&output);
        assert_eq!(json["voices"], json!([]));
        assert_eq!(json["descriptions_supported"], json!(true));
    }

    #[test]
    fn no_tool_can_clone() {
        let http = MockHttp::new(vec![]);
        let names: Vec<_> = toolbox(official(&http))
            .tools_definitions()
            .unwrap()
            .into_iter()
            .map(|t| t.name)
            .collect();
        assert_eq!(names, ["generate_speech", "list_voices"]);
    }

    #[tokio::test]
    async fn an_unlisted_clone_is_a_description_not_a_reference() {
        use crate::voice_store::{MemoryVoiceStore, StoredVoice, VoiceStore};
        let http = MockHttp::new(vec![audio()]);
        let store = MemoryVoiceStore::new();
        let id = store
            .save(StoredVoice::new(
                "Adam",
                agentai_core::message::Media::from_bytes("audio/wav", b"WAV"),
            ))
            .await
            .unwrap();
        let provider = OmlxProvider::new("model", "", "http://local/v1")
            .with_client(Arc::new(http.clone()))
            .with_voice_store(store);
        let toolbox = toolbox(provider).with_voices(vec![VoiceInfo::new("other", "Other")]);
        toolbox
            .call_tool(&tool_call(
                "generate_speech",
                json!({"text": "hi", "name": "x", "voice": id}),
            ))
            .await
            .unwrap();
        let body = body_json(&http.request(0));
        assert!(body.get("ref_audio").is_none());
        assert_eq!(body["instructions"], json!(id));
    }
}