Skip to content

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

refactor(voice): restructure voice handlers to use controller pattern

Steven Enamakel4d8c6eb09479
diff --git a/crates/openhuman-core/src/voice/schemas/handlers/transcribe_tts.rs b/crates/openhuman-core/src/voice/schemas/handlers/transcribe_tts.rs
index 1626bcd3bd..acdb1eee69 100644
--- a/crates/openhuman-core/src/voice/schemas/handlers/transcribe_tts.rs
+++ b/crates/openhuman-core/src/voice/schemas/handlers/transcribe_tts.rs
@@ -1,10 +1,10 @@
//! Handlers for transcription, TTS synthesis, and factory-dispatch RPCs.
+use crate::config::Config;
+use crate::core::all::{BoundRegisteredController, ControllerFuture, ControllerTask};
use crate::core::runtime::DispatchCtx;
use serde_json::{Map, Value};
-use crate::core::all::ControllerFuture;
-
use crate::voice::schemas::helpers::{
deserialize_params, effective_stt_provider, effective_tts_provider, to_json,
};
@@ -13,23 +13,91 @@ use crate::voice::schemas::params::{
TtsDispatchParams, TtsParams,
};
-pub(crate) fn handle_voice_status(
- ctx: DispatchCtx,
- _params: Map<String, Value>,
+fn load_voice_config(ctx: DispatchCtx) -> ControllerTask<Config> {
+ Box::pin(async move { ctx.load_config().await })
+}
+
+fn ignored_params(_params: Map<String, Value>) -> Result<(), String> {
+ Ok(())
+}
+
+pub(crate) fn voice_status_controller() -> BoundRegisteredController {
+ BoundRegisteredController::after_gate(
+ super::registry::voice_schemas("voice_status"),
+ load_voice_config,
+ ignored_params,
+ run_voice_status,
+ )
+}
+
+pub(crate) fn voice_transcribe_controller() -> BoundRegisteredController {
+ BoundRegisteredController::after_gate(
+ super::registry::voice_schemas("voice_transcribe"),
+ load_voice_config,
+ deserialize_params::<TranscribeParams>,
+ run_voice_transcribe,
+ )
+}
+
+pub(crate) fn voice_transcribe_bytes_controller() -> BoundRegisteredController {
+ BoundRegisteredController::after_gate(
+ super::registry::voice_schemas("voice_transcribe_bytes"),
+ load_voice_config,
+ deserialize_params::<TranscribeBytesParams>,
+ run_voice_transcribe_bytes,
+ )
+}
+
+pub(crate) fn voice_tts_controller() -> BoundRegisteredController {
+ BoundRegisteredController::after_gate(
+ super::registry::voice_schemas("voice_tts"),
+ load_voice_config,
+ deserialize_params::<TtsParams>,
+ run_voice_tts,
+ )
+}
+
+pub(crate) fn voice_reply_synthesize_controller() -> BoundRegisteredController {
+ BoundRegisteredController::after_gate(
+ super::registry::voice_schemas("voice_reply_synthesize"),
+ load_voice_config,
+ deserialize_params::<ReplySynthesizeParams>,
+ run_voice_reply_synthesize,
+ )
+}
+
+pub(crate) fn voice_stt_dispatch_controller() -> BoundRegisteredController {
+ BoundRegisteredController::after_gate(
+ super::registry::voice_schemas("voice_stt_dispatch"),
+ load_voice_config,
+ deserialize_params::<SttDispatchParams>,
+ run_voice_stt_dispatch,
+ )
+}
+
+pub(crate) fn voice_tts_dispatch_controller() -> BoundRegisteredController {
+ BoundRegisteredController::after_gate(
+ super::registry::voice_schemas("voice_tts_dispatch"),
+ load_voice_config,
+ deserialize_params::<TtsDispatchParams>,
+ run_voice_tts_dispatch,
+ )
+}
+
+fn run_voice_status(
+ _ctx: DispatchCtx,
+ config: Config,
+ (): (),
) -> ControllerFuture {
- Box::pin(async move {
- let config = ctx.load_config().await?;
- to_json(crate::voice::voice_status(&config).await?)
- })
+ Box::pin(async move { to_json(crate::voice::voice_status(&config).await?) })
}
-pub(crate) fn handle_voice_transcribe(
- ctx: DispatchCtx,
- params: Map<String, Value>,
+fn run_voice_transcribe(
+ _ctx: DispatchCtx,
+ config: Config,
+ p: TranscribeParams,
) -> ControllerFuture {
Box::pin(async move {
- let config = ctx.load_config().await?;
- let p = deserialize_params::<TranscribeParams>(params)?;
to_json(
crate::voice::voice_transcribe(
&config,
@@ -42,13 +110,12 @@ pub(crate) fn handle_voice_transcribe(
})
}
-pub(crate) fn handle_voice_transcribe_bytes(
- ctx: DispatchCtx,
- params: Map<String, Value>,
+fn run_voice_transcribe_bytes(
+ _ctx: DispatchCtx,
+ config: Config,
+ p: TranscribeBytesParams,
) -> ControllerFuture {
Box::pin(async move {
- let config = ctx.load_config().await?;
- let p = deserialize_params::<TranscribeBytesParams>(params)?;
to_json(
crate::voice::voice_transcribe_bytes(
&config,
@@ -62,21 +129,18 @@ pub(crate) fn handle_voice_transcribe_bytes(
})
}
-pub(crate) fn handle_voice_tts(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
+fn run_voice_tts(_ctx: DispatchCtx, config: Config, p: TtsParams) -> ControllerFuture {
Box::pin(async move {
- let config = ctx.load_config().await?;
- let p = deserialize_params::<TtsParams>(params)?;
to_json(crate::voice::voice_tts(&config, &p.text, p.output_path.as_deref()).await?)
})
}
-pub(crate) fn handle_voice_reply_synthesize(
- ctx: DispatchCtx,
- params: Map<String, Value>,
+fn run_voice_reply_synthesize(
+ _ctx: DispatchCtx,
+ config: Config,
+ p: ReplySynthesizeParams,
) -> ControllerFuture {
Box::pin(async move {
- let config = ctx.load_config().await?;
- let p = deserialize_params::<ReplySynthesizeParams>(params)?;
// Dispatch through the TTS factory so the user's `tts_provider`
// setting (cloud / piper / …) is honored on the spoken-reply path,
// not just the dedicated `voice_tts_dispatch` RPC. Without this
@@ -118,13 +182,12 @@ pub(crate) fn handle_voice_reply_synthesize(
})
}
-pub(crate) fn handle_voice_stt_dispatch(
- ctx: DispatchCtx,
- params: Map<String, Value>,
+fn run_voice_stt_dispatch(
+ _ctx: DispatchCtx,
+ config: Config,
+ p: SttDispatchParams,
) -> ControllerFuture {
Box::pin(async move {
- let config = ctx.load_config().await?;
- let p = deserialize_params::<SttDispatchParams>(params)?;
let provider_name = p
.provider
.as_deref()
@@ -162,13 +225,12 @@ pub(crate) fn handle_voice_stt_dispatch(
})
}
-pub(crate) fn handle_voice_tts_dispatch(
- ctx: DispatchCtx,
- params: Map<String, Value>,
+fn run_voice_tts_dispatch(
+ _ctx: DispatchCtx,
+ config: Config,
+ p: TtsDispatchParams,
) -> ControllerFuture {
Box::pin(async move {
- let config = ctx.load_config().await?;
- let p = deserialize_params::<TtsDispatchParams>(params)?;
let provider_name = p
.provider
.as_deref()
@@ -208,3 +270,49 @@ pub(crate) fn handle_voice_tts_dispatch(
to_json(outcome)
})
}
+
+pub(crate) fn handle_voice_status(
+ ctx: DispatchCtx,
+ params: Map<String, Value>,
+) -> ControllerFuture {
+ voice_status_controller().invoke(ctx, params)
+}
+
+pub(crate) fn handle_voice_transcribe(
+ ctx: DispatchCtx,
+ params: Map<String, Value>,
+) -> ControllerFuture {
+ voice_transcribe_controller().invoke(ctx, params)
+}
+
+pub(crate) fn handle_voice_transcribe_bytes(
+ ctx: DispatchCtx,
+ params: Map<String, Value>,
+) -> ControllerFuture {
+ voice_transcribe_bytes_controller().invoke(ctx, params)
+}
+
+pub(crate) fn handle_voice_tts(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
+ voice_tts_controller().invoke(ctx, params)
+}
+
+pub(crate) fn handle_voice_reply_synthesize(
+ ctx: DispatchCtx,
+ params: Map<String, Value>,
+) -> ControllerFuture {
+ voice_reply_synthesize_controller().invoke(ctx, params)
+}
+
+pub(crate) fn handle_voice_stt_dispatch(
+ ctx: DispatchCtx,
+ params: Map<String, Value>,
+) -> ControllerFuture {
+ voice_stt_dispatch_controller().invoke(ctx, params)
+}
+
+pub(crate) fn handle_voice_tts_dispatch(
+ ctx: DispatchCtx,
+ params: Map<String, Value>,
+) -> ControllerFuture {
+ voice_tts_dispatch_controller().invoke(ctx, params)
+}