Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions src/command/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,28 @@ async fn autocomplete_voice_name(
ctx: Context<'_>,
partial: &str,
) -> impl Iterator<Item = AutocompleteChoice> {
let candidates = ctx.data().registry.find_prefixed_all(partial);
candidates.map(|(id, package)| {
AutocompleteChoice::new(
match package.detail.description.as_ref() {
Some(description) => format!(
"{} | {} ({})",
package.detail.provider, package.detail.name, description
),
None => format!("{} | {}", package.detail.provider, package.detail.name),
},
id,
)
})
let keywords: Vec<&str> = partial.split_whitespace().filter(|s| *s != "|").collect();
let candidates = ctx
.data()
.registry
.find_matching_keywords(keywords.as_ref());

candidates
.map(|(id, package)| {
AutocompleteChoice::new(
match package.detail.description.as_ref() {
Some(description) => format!(
"{} | {} ({})",
package.detail.provider, package.detail.name, description
),
None => format!("{} | {}", package.detail.provider, package.detail.name),
},
id,
)
})
.take(25)
.collect::<Vec<_>>()
.into_iter()
}

async fn common_choose(ctx: Context<'_>, scope: Scope, name: String) -> Result<()> {
Expand Down
79 changes: 73 additions & 6 deletions src/tts/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ use std::sync::Arc;
pub struct VoicePackage {
pub voice: Arc<dyn Voice>,
pub detail: VoiceDetail,
pub search_index: String,
}

impl VoicePackage {
fn matches_keywords(&self, keywords: &[String]) -> bool {
keywords
.iter()
.all(|keyword| self.search_index.contains(keyword))
}
}

#[derive(Clone)]
Expand Down Expand Up @@ -45,6 +54,17 @@ impl VoicePackageRegistry {
.filter(move |&(_, package)| package.detail.name.starts_with(prefix))
.map(|(id, voice)| (id.as_str(), voice))
}

pub fn find_matching_keywords(
&self,
keywords: &[&str],
) -> impl Iterator<Item = (&str, &VoicePackage)> {
let normalized_keywords: Vec<String> = keywords.iter().map(|s| s.to_lowercase()).collect();
self.packages
.iter()
.filter(move |&(_, package)| package.matches_keywords(&normalized_keywords))
.map(|(id, package)| (id.as_str(), package))
}
}

pub struct VoiceRegistryBuilder {
Expand Down Expand Up @@ -112,7 +132,22 @@ impl VoiceRegistryBuilder {
}
};

voices.insert(id.to_string(), VoicePackage { voice, detail });
let search_index = format!(
"{} {} {}",
detail.name,
detail.provider,
detail.description.as_deref().unwrap_or("")
)
.to_lowercase();

voices.insert(
id.to_string(),
VoicePackage {
voice,
detail,
search_index,
},
);
}

Ok(VoicePackageRegistry::new(voices))
Expand All @@ -137,6 +172,7 @@ mod tests {
use super::*;
use crate::config::{
CacheConfig, DatabaseConfig, DatabaseKind, InMemoryCacheConfig, ProfileConfig,
VoiceDetailConfig,
};
use crate::tts::google_cloud::GoogleCloudVoiceConfig;

Expand All @@ -145,7 +181,10 @@ mod tests {
profiles.insert(
"test_preset".to_string(),
ProfileConfig {
note: Default::default(),
note: Some(VoiceDetailConfig {
name: Some("ja-JP-Wavenet-A".to_string()),
description: Some("test description".to_string()),
}),
voice_backend: ProfileBackendConfig::GoogleCloudVoice(GoogleCloudVoiceConfig {
language_code: "ja-JP".to_string(),
name: Some("ja-JP-Wavenet-A".to_string()),
Expand Down Expand Up @@ -226,11 +265,39 @@ mod tests {
}

#[tokio::test]
async fn test_build_fail_missing_client() {
async fn test_find_matching_keywords() {
let config = create_test_config(CacheConfig::Disabled);
let client = create_dummy_client().await;

let registry = VoicePackageRegistry::builder(config)
.google_cloud(client)
.build()
.expect("Should build successfully");

// build without client
let result = VoicePackageRegistry::builder(config).build();
assert!(result.is_err());
// "test" should match "test_preset" name
let keywords = vec!["test"];
let results: Vec<_> = registry.find_matching_keywords(&keywords).collect();
assert_eq!(results.len(), 1);
assert_eq!(results[0].0, "test_preset");

// "WAVENET" (uppercase) should match "ja-JP-Wavenet-A" (case-insensitive)
let keywords = vec!["WAVENET"];
let results: Vec<_> = registry.find_matching_keywords(&keywords).collect();
assert_eq!(results.len(), 1);

// "google" should match provider
let keywords = vec!["google"];
let results: Vec<_> = registry.find_matching_keywords(&keywords).collect();
assert_eq!(results.len(), 1);

// multiple keywords (AND)
let keywords = vec!["test", "google"];
let results: Vec<_> = registry.find_matching_keywords(&keywords).collect();
assert_eq!(results.len(), 1);

// "nonexistent" should not match
let keywords = vec!["nonexistent"];
let results: Vec<_> = registry.find_matching_keywords(&keywords).collect();
assert_eq!(results.is_empty(), true);
}
}
Loading