The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(voice): add voice channel host support
Steven Enamakel5f258c72609c
diff --git a/crates/openhuman-core/src/channels/host/voice.rs b/crates/openhuman-core/src/channels/host/voice.rsnew file mode 100644index 0000000000..be4b9ba82a--- /dev/null+++ b/crates/openhuman-core/src/channels/host/voice.rs@@ -0,0 +1,259 @@+//! Voice host ports for the TinyChannels module: scoped speech-to-text,+//! text-to-speech and the per-turn spoken-reply policy.+//!+//! The module calls these over the `ChannelsHost` callback object. Everything+//! here logs request ids, sizes and durations; transcripts, reply text and+//! audio are never logged.++use crate::config::schema::{ChannelVoiceConfig, VoiceReplyMode};+use crate::config::Config;+use parking_lot::Mutex;+use std::collections::VecDeque;+use std::sync::Arc;+use std::time::Instant;+use tinychannels_bus::{+ ChannelInboundEnvelope, ContentPart, HostSpeechSynthesisRequest, HostSpeechSynthesisResult,+ HostTranscriptionRequest, HostTranscriptionResult, HostVoiceReplyMode, HostVoiceReplyPolicy,+ HostVoiceScope, HostVoiceTranscriptionRequest,+};++const LOG_PREFIX: &str = "[channels_voice]";+/// Conservative speaking rate used to bound synthesis before the backend call.+const SPOKEN_CHARS_PER_SEC: usize = 15;+const MAX_TRACKED_TURNS: usize = 1024;++/// Verified facts about an inbound turn, recorded by the dispatcher.+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]+pub(crate) struct VoiceTurnFacts {+ pub inbound_has_voice: bool,+ pub explicitly_requested: bool,+}++static TURNS: Mutex<VecDeque<(String, VoiceTurnFacts)>> = Mutex::new(VecDeque::new());++fn turn_key(conversation_id: &str, turn_id: &str) -> String {+ format!("{conversation_id}\u{1f}{turn_id}")+}++/// Remember the facts of an inbound turn so the reply policy can read them.+pub(crate) fn record_turn(conversation_id: &str, turn_id: &str, facts: VoiceTurnFacts) {+ let key = turn_key(conversation_id, turn_id);+ let mut turns = TURNS.lock();+ turns.retain(|(existing, _)| existing != &key);+ if turns.len() >= MAX_TRACKED_TURNS {+ turns.pop_front();+ }+ turns.push_back((key, facts));+}++fn turn_facts(conversation_id: &str, turn_id: &str) -> VoiceTurnFacts {+ let key = turn_key(conversation_id, turn_id);+ TURNS+ .lock()+ .iter()+ .find(|(existing, _)| existing == &key)+ .map(|(_, facts)| *facts)+ .unwrap_or_default()+}++/// Record an inbound envelope's voice facts. The inbound message id is the+/// turn id the module echoes back in its voice scope.+pub(crate) fn record_inbound_envelope(envelope: &ChannelInboundEnvelope) {+ let inbound_has_voice = envelope+ .parts+ .iter()+ .any(|part| matches!(part, ContentPart::VoiceNote { .. }));+ record_turn(+ &envelope.conversation.id,+ &envelope.message_id,+ VoiceTurnFacts {+ inbound_has_voice,+ explicitly_requested: false,+ },+ );+}++fn thread_key(scope: &HostVoiceScope) -> &str {+ scope+ .conversation+ .thread_id+ .as_deref()+ .filter(|id| !id.is_empty())+ .unwrap_or(&scope.conversation.id)+}++fn wire_mode(mode: VoiceReplyMode) -> HostVoiceReplyMode {+ match mode {+ VoiceReplyMode::Mirror => HostVoiceReplyMode::Mirror,+ VoiceReplyMode::Always => HostVoiceReplyMode::Always,+ VoiceReplyMode::Never => HostVoiceReplyMode::Never,+ VoiceReplyMode::SpeakReply => HostVoiceReplyMode::SpeakReply,+ }+}++/// Resolve the spoken-reply policy for one scoped turn.+pub(crate) fn resolve_policy(+ voice: &ChannelVoiceConfig,+ scope: &HostVoiceScope,+) -> HostVoiceReplyPolicy {+ let mode = voice.resolve_mode(&scope.configured_instance, thread_key(scope));+ let facts = turn_facts(&scope.conversation.id, &scope.turn_id);+ HostVoiceReplyPolicy {+ mode: wire_mode(mode),+ inbound_has_voice: facts.inbound_has_voice,+ explicitly_requested: facts.explicitly_requested,+ }+}++fn validate_scope(scope: &HostVoiceScope) -> anyhow::Result<()> {+ if scope.configured_instance.trim().is_empty()+ || scope.conversation.id.trim().is_empty()+ || scope.turn_id.trim().is_empty()+ {+ anyhow::bail!("voice scope is incomplete");+ }+ Ok(())+}++/// Reject replies that cannot fit the channel's outbound duration cap.+pub(crate) fn check_outbound_length(+ voice: &ChannelVoiceConfig,+ channel: &str,+ text: &str,+) -> anyhow::Result<()> {+ let max_secs = voice.resolve_max_outbound_secs(channel) as usize;+ if text.chars().count() > max_secs.saturating_mul(SPOKEN_CHARS_PER_SEC) {+ anyhow::bail!("reply exceeds the maximum spoken duration ({max_secs}s)");+ }+ Ok(())+}++/// Speech-to-text and text-to-speech over OpenHuman's voice domain.+pub(crate) struct ChannelVoiceHost {+ pub config: Arc<Config>,+}++impl ChannelVoiceHost {+ /// Unscoped transcription, honouring the user's local/cloud STT setting.+ pub(crate) async fn transcribe_host(+ &self,+ request: HostTranscriptionRequest,+ ) -> anyhow::Result<HostTranscriptionResult> {+ let request_id = uuid::Uuid::new_v4();+ let started = Instant::now();+ let provider = crate::voice::effective_stt_provider(&self.config);+ tracing::debug!(+ "{LOG_PREFIX} stt start request_id={request_id} provider={provider} bytes_b64={}",+ request.audio_base64.len()+ );+ let stt = crate::voice::create_stt_provider(&provider, "", &self.config)?;+ let outcome = stt+ .transcribe(+ &self.config,+ &request.audio_base64,+ request.mime_type.as_deref(),+ request.file_name.as_deref(),+ request.language.as_deref(),+ )+ .await+ .map_err(|e| {+ tracing::warn!(+ "{LOG_PREFIX} stt failed request_id={request_id} elapsed_ms={}",+ started.elapsed().as_millis()+ );+ anyhow::anyhow!(e)+ })?;+ tracing::debug!(+ "{LOG_PREFIX} stt done request_id={request_id} elapsed_ms={} chars={}",+ started.elapsed().as_millis(),+ outcome.value.text.chars().count()+ );+ Ok(HostTranscriptionResult {+ text: outcome.value.text,+ language: request.language,+ duration_secs: None,+ })+ }++ /// Scoped transcription used by the shared voice pipeline.+ pub(crate) async fn transcribe_voice(+ &self,+ request: HostVoiceTranscriptionRequest,+ ) -> anyhow::Result<HostTranscriptionResult> {+ validate_scope(&request.scope)?;+ self.transcribe_host(HostTranscriptionRequest {+ audio_base64: request.audio_base64,+ mime_type: request.mime_type,+ file_name: request.file_name,+ language: request.language,+ })+ .await+ }++ /// Scoped synthesis through the hosted reply-speech endpoint.+ pub(crate) async fn synthesize_speech(+ &self,+ request: HostSpeechSynthesisRequest,+ ) -> anyhow::Result<HostSpeechSynthesisResult> {+ validate_scope(&request.scope)?;+ let channel = request.scope.configured_instance.as_str();+ let voice_cfg = &self.config.channel_voice;+ check_outbound_length(voice_cfg, channel, &request.text)?;+ let request_id = uuid::Uuid::new_v4();+ let started = Instant::now();+ let voice = request+ .voice+ .clone()+ .or_else(|| voice_cfg.resolve_voice(channel).map(str::to_owned));+ tracing::debug!(+ "{LOG_PREFIX} tts start request_id={request_id} channel={channel} chars={}",+ request.text.chars().count()+ );+ let opts = crate::voice::reply_speech::ReplySpeechOptions {+ voice_id: voice,+ model_id: None,+ output_format: request.format,+ voice_settings: None,+ };+ let outcome =+ crate::voice::reply_speech::synthesize_reply(&self.config, &request.text, &opts)+ .await+ .map_err(|e| {+ tracing::warn!(+ "{LOG_PREFIX} tts failed request_id={request_id} elapsed_ms={}",+ started.elapsed().as_millis()+ );+ anyhow::anyhow!(e)+ })?;+ let speech = outcome.value;+ if speech.audio_base64.is_empty() {+ anyhow::bail!("speech synthesis returned no audio");+ }+ tracing::debug!(+ "{LOG_PREFIX} tts done request_id={request_id} elapsed_ms={} bytes_b64={}",+ started.elapsed().as_millis(),+ speech.audio_base64.len()+ );+ Ok(HostSpeechSynthesisResult {+ audio_base64: speech.audio_base64,+ mime_type: speech.audio_mime,+ duration_ms: None,+ })+ }++ /// Host-authoritative reply policy for the scoped turn.+ pub(crate) fn reply_policy(&self, scope: &HostVoiceScope) -> HostVoiceReplyPolicy {+ let policy = resolve_policy(&self.config.channel_voice, scope);+ tracing::debug!(+ "{LOG_PREFIX} policy channel={} mode={:?} inbound_voice={}",+ scope.configured_instance,+ policy.mode,+ policy.inbound_has_voice+ );+ policy+ }+}++#[cfg(test)]+#[path = "voice_tests.rs"]+mod tests;diff --git a/crates/openhuman-core/src/channels/host/voice_tests.rs b/crates/openhuman-core/src/channels/host/voice_tests.rsnew file mode 100644index 0000000000..96f8cd942f--- /dev/null+++ b/crates/openhuman-core/src/channels/host/voice_tests.rs@@ -0,0 +1,142 @@+use super::*;+use crate::config::schema::ChannelVoiceOverride;+use tinychannels_bus::{ChannelRef, ConversationRef, VoiceNote};++fn scope(channel: &str, conversation: &str, turn: &str) -> HostVoiceScope {+ HostVoiceScope {+ configured_instance: channel.into(),+ channel: ChannelRef::default(),+ conversation: ConversationRef {+ id: conversation.into(),+ ..ConversationRef::default()+ },+ turn_id: turn.into(),+ }+}++fn envelope(conversation: &str, message: &str, voice: bool) -> ChannelInboundEnvelope {+ let mut envelope = ChannelInboundEnvelope {+ message_id: message.into(),+ conversation: ConversationRef {+ id: conversation.into(),+ ..ConversationRef::default()+ },+ ..ChannelInboundEnvelope::default()+ };+ envelope.parts.push(if voice {+ ContentPart::VoiceNote {+ voice_note: VoiceNote::default(),+ }+ } else {+ ContentPart::Text {+ text: "hi".into(),+ markdown_dialect: None,+ }+ });+ envelope+}++#[test]+fn mirror_policy_reports_inbound_voice_from_recorded_envelope() {+ record_inbound_envelope(&envelope("c-voice", "m1", true));+ record_inbound_envelope(&envelope("c-text", "m2", false));+ let cfg = ChannelVoiceConfig::default();+ let voice = resolve_policy(&cfg, &scope("telegram", "c-voice", "m1"));+ assert_eq!(voice.mode, HostVoiceReplyMode::Mirror);+ assert!(voice.inbound_has_voice);+ let text = resolve_policy(&cfg, &scope("telegram", "c-text", "m2"));+ assert!(!text.inbound_has_voice);+}++#[test]+fn unknown_turn_defaults_to_no_voice() {+ let policy = resolve_policy(&ChannelVoiceConfig::default(), &scope("t", "nope", "nope"));+ assert!(!policy.inbound_has_voice);+ assert!(!policy.explicitly_requested);+}++#[test]+fn policy_mode_follows_thread_then_channel_overrides() {+ let mut cfg = ChannelVoiceConfig::default();+ cfg.channels.insert(+ "telegram".into(),+ ChannelVoiceOverride {+ reply_mode: Some(VoiceReplyMode::Always),+ ..Default::default()+ },+ );+ assert_eq!(+ resolve_policy(&cfg, &scope("telegram", "c", "t")).mode,+ HostVoiceReplyMode::Always+ );+ cfg.threads.insert("c".into(), VoiceReplyMode::Never);+ assert_eq!(+ resolve_policy(&cfg, &scope("telegram", "c", "t")).mode,+ HostVoiceReplyMode::Never+ );+ assert_eq!(+ resolve_policy(&cfg, &scope("slack", "other", "t")).mode,+ HostVoiceReplyMode::Mirror+ );+}++#[test]+fn registry_is_bounded() {+ for index in 0..(MAX_TRACKED_TURNS + 10) {+ record_turn("bounded", &index.to_string(), VoiceTurnFacts::default());+ }+ assert!(TURNS.lock().len() <= MAX_TRACKED_TURNS);+}++#[test]+fn outbound_length_respects_channel_cap() {+ let mut cfg = ChannelVoiceConfig::default();+ cfg.channels.insert(+ "telegram".into(),+ ChannelVoiceOverride {+ max_outbound_secs: Some(2),+ ..Default::default()+ },+ );+ assert!(check_outbound_length(&cfg, "telegram", &"a".repeat(30)).is_ok());+ assert!(check_outbound_length(&cfg, "telegram", &"a".repeat(31)).is_err());+ assert!(check_outbound_length(&cfg, "slack", &"a".repeat(31)).is_ok());+}++#[tokio::test]+async fn incomplete_scope_is_rejected_before_any_voice_work() {+ let host = ChannelVoiceHost {+ config: Arc::new(Config::default()),+ };+ let bad = HostSpeechSynthesisRequest {+ scope: scope("telegram", "", "t"),+ text: "hello".into(),+ voice: None,+ format: None,+ };+ assert!(host.synthesize_speech(bad).await.is_err());+ let bad = HostVoiceTranscriptionRequest {+ scope: scope("", "c", "t"),+ ..Default::default()+ };+ assert!(host.transcribe_voice(bad).await.is_err());+}++#[tokio::test]+async fn overlong_reply_is_rejected_without_calling_the_backend() {+ let mut config = Config::default();+ config.channel_voice.max_outbound_secs = 1;+ let host = ChannelVoiceHost {+ config: Arc::new(config),+ };+ let err = host+ .synthesize_speech(HostSpeechSynthesisRequest {+ scope: scope("telegram", "c", "t"),+ text: "x".repeat(100),+ voice: None,+ format: None,+ })+ .await+ .unwrap_err();+ assert!(err.to_string().contains("maximum spoken duration"));+}