Skip to content

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

feat(voice): add dictation listener and server runtime

Steven Enamakel8f13b8bbae1a
diff --git a/crates/openhuman-core/src/voice/dictation_listener.rs b/crates/openhuman-core/src/voice/dictation_listener.rs
index b88872ed30..3b7d03fd4d 100644
--- a/crates/openhuman-core/src/voice/dictation_listener.rs
+++ b/crates/openhuman-core/src/voice/dictation_listener.rs
@@ -106,7 +106,7 @@ pub fn publish_transcription(text: String) -> usize {
/// Tauri host already registers the shortcut via `tauri-plugin-global-shortcut`
/// (main-thread-safe) and emits `dictation://toggle` to the frontend, making
/// the core-side rdev listener redundant on macOS. (#2677)
-pub async fn start_if_enabled(config: &Config) {
+pub async fn start_if_enabled(ctx: &crate::core::runtime::DispatchCtx, config: &Config) {
if !config.components.voice {
log::info!("{LOG_PREFIX} [components].voice is off, skipping hotkey listener");
return;
@@ -136,11 +136,15 @@ pub async fn start_if_enabled(config: &Config) {
// Non-macOS: start the TinyVoice-owned native listener.
#[cfg(not(target_os = "macos"))]
- start_module_listener(hotkey_str, config).await;
+ start_module_listener(ctx, hotkey_str, config).await;
}
#[cfg(not(target_os = "macos"))]
-async fn start_module_listener(hotkey_str: String, config: &Config) {
+async fn start_module_listener(
+ ctx: &crate::core::runtime::DispatchCtx,
+ hotkey_str: String,
+ config: &Config,
+) {
// Map DictationActivationMode to our hotkey ActivationMode.
let mode = match config.dictation.activation_mode {
crate::config::DictationActivationMode::Push => ActivationMode::Push,
@@ -157,6 +161,7 @@ async fn start_module_listener(hotkey_str: String, config: &Config) {
let cancel = CancellationToken::new();
let (listener, mut hotkey_rx) =
match crate::voice::server::hotkey_listener::start_hotkey_listener(
+ ctx,
config,
&normalized,
mode,
diff --git a/crates/openhuman-core/src/voice/server/runtime.rs b/crates/openhuman-core/src/voice/server/runtime.rs
index 0aad5ff205..3a5ae91898 100644
--- a/crates/openhuman-core/src/voice/server/runtime.rs
+++ b/crates/openhuman-core/src/voice/server/runtime.rs
@@ -70,7 +70,11 @@ impl VoiceServer {
/// Run the voice server. Blocks until stopped.
///
/// This is the main entry point for both embedded and standalone modes.
- pub async fn run(&self, app_config: &Config) -> Result<(), String> {
+ pub async fn run(
+ &self,
+ ctx: &crate::core::runtime::DispatchCtx,
+ app_config: &Config,
+ ) -> Result<(), String> {
// Atomically transition Stopped → Idle to prevent concurrent run() calls.
// The globe listener compilation can take several seconds; without this
// guard the RPC handler sees "Stopped" and spawns a duplicate run().
@@ -114,6 +118,7 @@ impl VoiceServer {
// rdev's CGEventTap can see it. Use the Swift-based globe listener
// instead, which monitors NSEvent.flagsChanged for the .function flag.
let (listener_handle, mut hotkey_rx) = match start_hotkey_listener(
+ ctx,
app_config,
&self.config.hotkey,
self.config.activation_mode,
@@ -210,7 +215,7 @@ impl VoiceServer {
// event loop remains responsive to Release.
let (tx, rx) = tokio::sync::oneshot::channel();
let capture_config = app_config.clone();
- crate::core::runtime::DispatchCtx::ambient().spawn(async move {
+ ctx.spawn(async move {
let expected_app = capture_expected_app_name(&capture_config).await;
let result = audio_capture::start_recording(&capture_config).await
.map(|handle| (handle, expected_app));
@@ -223,6 +228,7 @@ impl VoiceServer {
if let Some(handle) = recording.take() {
debug!("{LOG_PREFIX} hotkey stop → stopping recording generation={generation}");
self.spawn_process_recording(
+ ctx,
handle,
app_config,
&recorder,
@@ -303,6 +309,7 @@ impl VoiceServer {
if let Some(handle) = recording.take() {
info!("{LOG_PREFIX} deferred stop deadline reached, stopping recording");
self.spawn_process_recording(
+ ctx,
handle,
app_config,
&recorder,
@@ -368,6 +375,7 @@ impl VoiceServer {
/// consecutive Fn presses are never missed.
fn spawn_process_recording(
&self,
+ ctx: &crate::core::runtime::DispatchCtx,
handle: RecordingHandle,
config: &Config,
recorder: &Recorder,
@@ -386,7 +394,7 @@ impl VoiceServer {
"{LOG_PREFIX} [pipeline={pipeline_id}] spawning process_recording (generation={generation})"
);
- crate::core::runtime::DispatchCtx::ambient().spawn(async move {
+ ctx.spawn(async move {
process_recording_bg(
&pipeline_id,
handle,
diff --git a/crates/openhuman-core/src/voice/server/singleton.rs b/crates/openhuman-core/src/voice/server/singleton.rs
index 321c6e3590..86f3261084 100644
--- a/crates/openhuman-core/src/voice/server/singleton.rs
+++ b/crates/openhuman-core/src/voice/server/singleton.rs
@@ -32,7 +32,7 @@ pub fn try_global_server() -> Option<Arc<VoiceServer>> {
/// This is intended for core process startup. The server runs in the background
/// and reuses the process-global singleton so RPC status/stop calls continue to
/// operate on the same instance.
-pub async fn start_if_enabled(app_config: &Config) {
+pub async fn start_if_enabled(ctx: &crate::core::runtime::DispatchCtx, app_config: &Config) {
if !app_config.components.voice {
info!("{LOG_PREFIX} [components].voice is off, skipping embedded voice server");
return;
@@ -74,8 +74,9 @@ pub async fn start_if_enabled(app_config: &Config) {
let server = global_server(server_config);
let config_for_run = app_config.clone();
let server_for_err = server.clone();
+ let run_ctx = ctx.clone();
tokio::spawn(async move {
- if let Err(e) = server.run(&config_for_run).await {
+ if let Err(e) = server.run(&run_ctx, &config_for_run).await {
warn!("{LOG_PREFIX} embedded voice server exited with error: {e}");
server_for_err.set_last_error(&e).await;
}
@@ -88,6 +89,7 @@ pub async fn start_if_enabled(app_config: &Config) {
/// singleton used by `voice_server_status` RPC. This keeps CLI-started
/// instances isolated from the core RPC lifecycle.
pub async fn run_standalone(
+ ctx: &crate::core::runtime::DispatchCtx,
app_config: Config,
server_config: VoiceServerConfig,
) -> Result<(), String> {
@@ -111,5 +113,5 @@ pub async fn run_standalone(
// This is safe because we hold the Arc and nothing else moves it.
// The server.run() borrows &self, and we await it to completion.
- server_arc.run(&app_config).await
+ server_arc.run(ctx, &app_config).await
}
diff --git a/crates/openhuman-core/src/voice/stub.rs b/crates/openhuman-core/src/voice/stub.rs
index d5c0ae5615..58fdad2dc1 100644
--- a/crates/openhuman-core/src/voice/stub.rs
+++ b/crates/openhuman-core/src/voice/stub.rs
@@ -120,7 +120,7 @@ pub mod server {
}
/// No-op: there is no voice server to start.
- pub async fn start_if_enabled(_config: &Config) {}
+ pub async fn start_if_enabled(_ctx: &crate::core::runtime::DispatchCtx, _config: &Config) {}
/// Always `None`: no global voice server exists.
pub fn try_global_server() -> Option<Arc<VoiceServer>> {
@@ -168,7 +168,7 @@ pub mod dictation_listener {
}
/// No-op: no hotkey listener is started.
- pub async fn start_if_enabled(_config: &Config) {}
+ pub async fn start_if_enabled(_ctx: &crate::core::runtime::DispatchCtx, _config: &Config) {}
/// No-op: nothing to stop.
pub async fn stop() {}
@@ -182,7 +182,7 @@ pub mod always_on {
use crate::config::Config;
/// No-op: always-on listening does not exist when voice is disabled.
- pub async fn start_if_enabled(_config: &Config) {}
+ pub async fn start_if_enabled(_ctx: &crate::core::runtime::DispatchCtx, _config: &Config) {}
/// No-op: nothing to stop.
pub fn stop() {}