agentai_audio/
voice_store.rs1use crate::provider::{VoiceId, VoiceInfo, VoiceOrigin};
4use agentai_core::message::Media;
5use agentai_core::storage::StorageError;
6use async_trait::async_trait;
7use std::sync::{Arc, Mutex};
8
9#[derive(Debug, Clone, PartialEq)]
11#[non_exhaustive]
12pub struct StoredVoice {
13 pub name: String,
15 pub description: Option<String>,
17 pub sample: Media,
19 pub transcript: Option<String>,
21 pub language: Option<String>,
23}
24
25impl StoredVoice {
26 pub fn new(name: impl Into<String>, sample: Media) -> Self {
28 Self {
29 name: name.into(),
30 description: None,
31 sample,
32 transcript: None,
33 language: None,
34 }
35 }
36
37 pub fn with_description(mut self, description: impl Into<String>) -> Self {
39 self.description = Some(description.into());
40 self
41 }
42
43 pub fn with_language(mut self, language: impl Into<String>) -> Self {
45 self.language = Some(language.into());
46 self
47 }
48
49 pub fn with_transcript(mut self, transcript: impl Into<String>) -> Self {
51 self.transcript = Some(transcript.into());
52 self
53 }
54}
55
56impl StoredVoice {
57 pub(crate) fn into_info(self, id: VoiceId) -> VoiceInfo {
59 let mut info = VoiceInfo::new(id, self.name, VoiceOrigin::Store).with_preview(self.sample);
60 info.description = self.description;
61 info.language = self.language;
62 info
63 }
64}
65
66#[async_trait]
68pub trait VoiceStore: Send + Sync + 'static {
69 async fn save(&self, voice: StoredVoice) -> Result<VoiceId, StorageError>;
72
73 async fn get(&self, id: &str) -> Result<Option<StoredVoice>, StorageError>;
79
80 async fn list(&self) -> Result<Vec<(VoiceId, StoredVoice)>, StorageError>;
82}
83
84#[derive(Debug, Clone, Default)]
88pub struct MemoryVoiceStore {
89 voices: Arc<Mutex<Vec<(VoiceId, StoredVoice)>>>,
90}
91
92impl MemoryVoiceStore {
93 pub fn new() -> Self {
95 Self::default()
96 }
97}
98
99#[async_trait]
100impl VoiceStore for MemoryVoiceStore {
101 async fn save(&self, voice: StoredVoice) -> Result<VoiceId, StorageError> {
102 let mut voices = self.voices.lock().unwrap();
103 let id = format!("voice-{}", voices.len() + 1);
104 voices.push((id.clone(), voice));
105 Ok(id)
106 }
107
108 async fn get(&self, id: &str) -> Result<Option<StoredVoice>, StorageError> {
109 let voices = self.voices.lock().unwrap();
110 Ok(voices.iter().find(|(i, _)| i == id).map(|(_, v)| v.clone()))
111 }
112
113 async fn list(&self) -> Result<Vec<(VoiceId, StoredVoice)>, StorageError> {
114 Ok(self.voices.lock().unwrap().clone())
115 }
116}