Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(voice): add factory registry for voice backends

Steven Enamakel0873bf681902
diff --git a/crates/openhuman-core/src/voice/factory/entry.rs b/crates/openhuman-core/src/voice/factory/entry.rs
index bd678557ec..cad3270dcc 100644
--- a/crates/openhuman-core/src/voice/factory/entry.rs
+++ b/crates/openhuman-core/src/voice/factory/entry.rs
@@ -7,6 +7,7 @@ use log::debug;
use super::helpers::{
create_stt_provider_by_slug, create_tts_provider_by_slug, split_slug_model, LOG_PREFIX,
};
+use super::registry::{stt_factory, tts_factory};
use super::stt_providers::CloudSttProvider;
use super::traits::{SttProvider, TtsProvider};
use super::tts_providers::{CloudTtsProvider, PiperTtsProvider};
@@ -45,6 +46,17 @@ pub fn create_stt_provider(
config: &Config,
) -> anyhow::Result<Box<dyn SttProvider>> {
debug!("{LOG_PREFIX} create_stt_provider provider={provider} model={model}");
+ // A registered provider (see `registry`) wins over the hard-coded routes.
+ let (registered_slug, registered_model) = split_slug_model(provider.trim());
+ if let Some(factory) = stt_factory(registered_slug) {
+ let effective_model = if registered_model.is_empty() {
+ model.trim()
+ } else {
+ registered_model
+ };
+ debug!("{LOG_PREFIX} create_stt_provider using registered provider slug={registered_slug}");
+ return factory(effective_model, config);
+ }
match provider.trim() {
"cloud" | "openhuman" | "backend" => Ok(Box::new(CloudSttProvider::new(
super::super::cloud_transcribe_default_model(),
@@ -101,6 +113,12 @@ pub fn create_tts_provider(
"{LOG_PREFIX} create_tts_provider resolved provider={provider} voice={}",
resolved.unwrap_or("<provider default>")
);
+ // A registered provider (see `registry`) wins over the hard-coded routes.
+ let (registered_slug, _) = split_slug_model(provider);
+ if let Some(factory) = tts_factory(registered_slug) {
+ debug!("{LOG_PREFIX} create_tts_provider using registered provider slug={registered_slug}");
+ return factory(resolved, config);
+ }
match provider {
"cloud" | "openhuman" => Ok(Box::new(CloudTtsProvider::new(
resolved.map(str::to_string),
diff --git a/crates/openhuman-core/src/voice/factory/mod.rs b/crates/openhuman-core/src/voice/factory/mod.rs
index 3a86fa1546..e1bc029384 100644
--- a/crates/openhuman-core/src/voice/factory/mod.rs
+++ b/crates/openhuman-core/src/voice/factory/mod.rs
@@ -44,6 +44,7 @@
mod entry;
mod helpers;
+pub mod registry;
mod stt_providers;
mod traits;
mod tts_providers;
diff --git a/crates/openhuman-core/src/voice/factory/registry.rs b/crates/openhuman-core/src/voice/factory/registry.rs
new file mode 100644
index 0000000000..b6fd8382d3
--- /dev/null
+++ b/crates/openhuman-core/src/voice/factory/registry.rs
@@ -0,0 +1,82 @@
+//! Registered speech providers, keyed by provider slug.
+//!
+//! [`create_stt_provider`](super::create_stt_provider) and
+//! [`create_tts_provider`](super::create_tts_provider) ask these registries
+//! first, so an outside crate can supply a provider under any slug (including
+//! the built-in `"cloud"`, which it then replaces). A TTS provider returns the
+//! same [`ReplySpeech`](tinyinference_voice::reply::ReplySpeech) envelope the
+//! built-ins do, optional visemes included. With nothing registered the
+//! hard-coded routes below apply unchanged.
+
+use std::sync::Arc;
+
+use super::traits::{SttProvider, TtsProvider};
+use crate::config::Config;
+use crate::core::slots::{Extensions, HookSlot};
+
+/// Builds an STT provider: `(model, config)`; an empty `model` means "the
+/// provider's default".
+pub type SttProviderFactory =
+ Arc<dyn Fn(&str, &Config) -> anyhow::Result<Box<dyn SttProvider>> + Send + Sync>;
+
+/// Builds a TTS provider: `(voice, config)`; `None` means "the provider picks
+/// its own default voice".
+pub type TtsProviderFactory =
+ Arc<dyn Fn(Option<&str>, &Config) -> anyhow::Result<Box<dyn TtsProvider>> + Send + Sync>;
+
+static STT_PROVIDERS: HookSlot<SttProviderFactory> = HookSlot::new("voice::factory::stt");
+static TTS_PROVIDERS: HookSlot<TtsProviderFactory> = HookSlot::new("voice::factory::tts");
+
+/// Registers the STT provider `slug` for the whole process.
+pub fn register_stt_provider<F>(slug: impl Into<String>, factory: F)
+where
+ F: Fn(&str, &Config) -> anyhow::Result<Box<dyn SttProvider>> + Send + Sync + 'static,
+{
+ STT_PROVIDERS.register(slug, Arc::new(factory));
+}
+
+/// Registers the STT provider `slug` for the context built from `extensions`.
+pub fn register_stt_provider_in<F>(extensions: &Extensions, slug: impl Into<String>, factory: F)
+where
+ F: Fn(&str, &Config) -> anyhow::Result<Box<dyn SttProvider>> + Send + Sync + 'static,
+{
+ STT_PROVIDERS.register_in(extensions, slug, Arc::new(factory));
+}
+
+/// Removes the process-wide STT provider `slug`.
+pub fn unregister_stt_provider(slug: &str) -> bool {
+ STT_PROVIDERS.unregister(slug)
+}
+
+/// Registers the TTS provider `slug` for the whole process.
+pub fn register_tts_provider<F>(slug: impl Into<String>, factory: F)
+where
+ F: Fn(Option<&str>, &Config) -> anyhow::Result<Box<dyn TtsProvider>> + Send + Sync + 'static,
+{
+ TTS_PROVIDERS.register(slug, Arc::new(factory));
+}
+
+/// Registers the TTS provider `slug` for the context built from `extensions`.
+pub fn register_tts_provider_in<F>(extensions: &Extensions, slug: impl Into<String>, factory: F)
+where
+ F: Fn(Option<&str>, &Config) -> anyhow::Result<Box<dyn TtsProvider>> + Send + Sync + 'static,
+{
+ TTS_PROVIDERS.register_in(extensions, slug, Arc::new(factory));
+}
+
+/// Removes the process-wide TTS provider `slug`.
+pub fn unregister_tts_provider(slug: &str) -> bool {
+ TTS_PROVIDERS.unregister(slug)
+}
+
+pub(super) fn stt_factory(slug: &str) -> Option<SttProviderFactory> {
+ STT_PROVIDERS.get(slug)
+}
+
+pub(super) fn tts_factory(slug: &str) -> Option<TtsProviderFactory> {
+ TTS_PROVIDERS.get(slug)
+}
+
+#[cfg(test)]
+#[path = "registry_tests.rs"]
+mod tests;