use agentai_core::message::Media;
use agentai_core::storage::StorageError;
use async_trait::async_trait;
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct StoredVoice {
pub name: String,
pub description: Option<String>,
pub sample: Media,
pub transcript: Option<String>,
}
impl StoredVoice {
pub fn new(name: impl Into<String>, sample: Media) -> Self {
Self {
name: name.into(),
description: None,
sample,
transcript: None,
}
}
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn with_transcript(mut self, transcript: impl Into<String>) -> Self {
self.transcript = Some(transcript.into());
self
}
}
#[async_trait]
pub trait VoiceStore: Send + Sync + 'static {
async fn save(&self, voice: StoredVoice) -> Result<String, StorageError>;
async fn get(&self, id: &str) -> Result<Option<StoredVoice>, StorageError>;
async fn list(&self) -> Result<Vec<(String, StoredVoice)>, StorageError>;
}
#[derive(Debug, Clone, Default)]
pub struct MemoryVoiceStore {
voices: Arc<Mutex<Vec<(String, StoredVoice)>>>,
}
impl MemoryVoiceStore {
pub fn new() -> Self {
Self::default()
}
}
#[async_trait]
impl VoiceStore for MemoryVoiceStore {
async fn save(&self, voice: StoredVoice) -> Result<String, StorageError> {
let mut voices = self.voices.lock().unwrap();
let id = format!("voice-{}", voices.len() + 1);
voices.push((id.clone(), voice));
Ok(id)
}
async fn get(&self, id: &str) -> Result<Option<StoredVoice>, StorageError> {
let voices = self.voices.lock().unwrap();
Ok(voices.iter().find(|(i, _)| i == id).map(|(_, v)| v.clone()))
}
async fn list(&self) -> Result<Vec<(String, StoredVoice)>, StorageError> {
Ok(self.voices.lock().unwrap().clone())
}
}