Skip to content

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

feat(web_chat): persist assistant replies and add voice note support

Steven Enamakelc8f4e4bad7d8
diff --git a/crates/openhuman-core/src/web_chat/mod.rs b/crates/openhuman-core/src/web_chat/mod.rs
index 1b56642498..f46f1d5c5b 100644
--- a/crates/openhuman-core/src/web_chat/mod.rs
+++ b/crates/openhuman-core/src/web_chat/mod.rs
@@ -48,6 +48,7 @@ mod turn_close;
mod turn_timing;
mod types;
+mod voice_notes;
mod web_errors;
pub(crate) use web_errors::classify_inference_error;
#[cfg(any(test, debug_assertions))]
diff --git a/crates/openhuman-core/src/web_chat/reply_persistence.rs b/crates/openhuman-core/src/web_chat/reply_persistence.rs
index ada4666bbb..5d48eeacf8 100644
--- a/crates/openhuman-core/src/web_chat/reply_persistence.rs
+++ b/crates/openhuman-core/src/web_chat/reply_persistence.rs
@@ -71,6 +71,10 @@ pub(crate) fn persist_delivered_reply(
// renders identical chips to one the client had appended itself.
extra_metadata["citations"] = json!(citations);
}
+ // The audio a spoken reply was synthesized into, if the turn stored any.
+ if let Some(note) = super::voice_notes::reply_note_metadata(workspace_dir, request_id) {
+ extra_metadata["voiceNote"] = note;
+ }
conversations::append_message(
ctx,
workspace_dir.to_path_buf(),
diff --git a/crates/openhuman-core/src/web_chat/run_task.rs b/crates/openhuman-core/src/web_chat/run_task.rs
index 6f37fa2469..74a0c6349d 100644
--- a/crates/openhuman-core/src/web_chat/run_task.rs
+++ b/crates/openhuman-core/src/web_chat/run_task.rs
@@ -289,10 +289,21 @@ pub(crate) async fn run_chat_task(
let turn_elapsed = turn_started.elapsed();
if let Ok(ref task_result) = result {
- let speak_reply = matches!(metadata.speak_reply, Some(true));
+ let speak_reply = super::voice_notes::should_speak_reply(
+ &config,
+ thread_id,
+ metadata.speak_reply,
+ metadata.source.as_deref(),
+ );
let trimmed_response = task_result.full_response.trim();
if speak_reply && !trimmed_response.is_empty() {
- let opts = crate::voice::reply_speech::ReplySpeechOptions::default();
+ let opts = crate::voice::reply_speech::ReplySpeechOptions {
+ voice_id: config
+ .channel_voice
+ .resolve_voice("web")
+ .map(str::to_string),
+ ..Default::default()
+ };
match crate::voice::reply_speech::synthesize_reply(
&config,
&task_result.full_response,
@@ -300,13 +311,22 @@ pub(crate) async fn run_chat_task(
)
.await
{
- Ok(_) => log::debug!(
- "[web_channel] reply_speech dispatched chars={} client_id={} thread_id={} request_id={}",
- task_result.full_response.len(),
- client_id,
- thread_id,
- request_id,
- ),
+ Ok(outcome) => {
+ let stored = super::voice_notes::store_synthesized_reply(
+ &config.workspace_dir,
+ request_id,
+ &outcome.value.audio_base64,
+ &outcome.value.audio_mime,
+ );
+ log::debug!(
+ "[web_channel] reply_speech dispatched chars={} stored={} client_id={} thread_id={} request_id={}",
+ task_result.full_response.len(),
+ stored,
+ client_id,
+ thread_id,
+ request_id,
+ );
+ }
Err(err) => log::warn!(
"[web_channel] reply_speech failed: {err} client_id={} thread_id={} request_id={}",
client_id,
diff --git a/crates/openhuman-core/src/web_chat/voice_notes.rs b/crates/openhuman-core/src/web_chat/voice_notes.rs
index 0994ce2a80..2596ccf62a 100644
--- a/crates/openhuman-core/src/web_chat/voice_notes.rs
+++ b/crates/openhuman-core/src/web_chat/voice_notes.rs
@@ -15,6 +15,8 @@ use std::path::{Path, PathBuf};
use serde_json::{json, Value};
const LOG_PREFIX: &str = "[web_channel][voice_note]";
+/// `source` the composer stamps on a turn that began as a voice note.
+pub(crate) const VOICE_NOTE_SOURCE: &str = "voice_note";
const DIR: &str = "voice_notes";
/// Largest note the store accepts (decoded bytes).
pub(crate) const MAX_NOTE_BYTES: usize = 25 * 1024 * 1024;
@@ -120,6 +122,54 @@ pub(crate) fn reply_note_metadata(workspace: &Path, request_id: &str) -> Option<
})
}
+/// Whether this turn's reply is also spoken: an explicit `speak_reply` always
+/// wins, otherwise the `[channel_voice]` policy for the `web` channel decides
+/// (`always`, or `mirror` when the user sent a voice note).
+pub(crate) fn should_speak_reply(
+ config: &crate::config::Config,
+ thread_id: &str,
+ speak_reply: Option<bool>,
+ source: Option<&str>,
+) -> bool {
+ use crate::config::schema::VoiceReplyMode;
+ if speak_reply == Some(true) {
+ return true;
+ }
+ match config.channel_voice.resolve_mode("web", thread_id) {
+ VoiceReplyMode::Always => true,
+ VoiceReplyMode::Mirror => source == Some(VOICE_NOTE_SOURCE),
+ VoiceReplyMode::Never | VoiceReplyMode::SpeakReply => false,
+ }
+}
+
+/// Decodes and stores the audio a turn synthesized. `false` when the backend
+/// returned no audio (or it could not be stored); the reply then stays text.
+pub(crate) fn store_synthesized_reply(
+ workspace: &Path,
+ request_id: &str,
+ audio_base64: &str,
+ audio_mime: &str,
+) -> bool {
+ use base64::Engine as _;
+ if audio_base64.is_empty() {
+ return false;
+ }
+ let bytes = match base64::engine::general_purpose::STANDARD.decode(audio_base64) {
+ Ok(b) => b,
+ Err(err) => {
+ log::warn!("{LOG_PREFIX} reply audio is not base64 request_id={request_id}: {err}");
+ return false;
+ }
+ };
+ match store_note(workspace, &reply_note_id(request_id), audio_mime, &bytes) {
+ Ok(_) => true,
+ Err(err) => {
+ log::warn!("{LOG_PREFIX} could not store reply audio request_id={request_id}: {err}");
+ false
+ }
+ }
+}
+
#[cfg(test)]
#[path = "voice_notes_tests.rs"]
mod tests;