Skip to content

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

feat(web_chat): add rpc endpoints to store and read voice note audio

Steven Enamakel65175fde8857
diff --git a/crates/openhuman-core/src/web_chat/schemas.rs b/crates/openhuman-core/src/web_chat/schemas.rs
index 06bbc82ca8..348eb3553a 100644
--- a/crates/openhuman-core/src/web_chat/schemas.rs
+++ b/crates/openhuman-core/src/web_chat/schemas.rs
@@ -14,7 +14,7 @@ use super::ops::{
channel_web_queue_status,
};
use super::types::{
- ChatRequestMetadata, WebCancelParams, WebChatParams, WebQueueParams, WebQueueRemoveParams,
+ ChatRequestMetadata, VoiceNoteReadParams, VoiceNoteStoreParams, WebCancelParams, WebChatParams, WebQueueParams, WebQueueRemoveParams,
};
pub fn all_web_channel_controller_schemas() -> Vec<ControllerSchema> {
@@ -24,6 +24,8 @@ pub fn all_web_channel_controller_schemas() -> Vec<ControllerSchema> {
schemas("queue_status"),
schemas("queue_clear"),
schemas("queue_remove"),
+ schemas("voice_note_store"),
+ schemas("voice_note_read"),
]
}
@@ -49,6 +51,14 @@ pub fn all_web_channel_registered_controllers() -> Vec<RegisteredController> {
schema: schemas("queue_remove"),
handler: handle_queue_remove,
},
+ RegisteredController {
+ schema: schemas("voice_note_store"),
+ handler: handle_voice_note_store,
+ },
+ RegisteredController {
+ schema: schemas("voice_note_read"),
+ handler: handle_voice_note_read,
+ },
]
}
@@ -140,6 +150,24 @@ pub fn schemas(function: &str) -> ControllerSchema {
"{ thread_id, item_id, removed }.",
)],
},
+ "voice_note_store" => ControllerSchema {
+ namespace: "channel",
+ function: "web_voice_note_store",
+ description: "Store the audio of a voice note the user recorded, so the thread row can point at it.",
+ inputs: vec![
+ required_string("note_id", "Caller-chosen id (letters, digits, '-', '_'); storing the same id again replaces the audio."),
+ required_string("audio_base64", "Base64 of the recorded audio."),
+ optional_string("mime", "Audio mime type (default audio/webm)."),
+ ],
+ outputs: vec![json_output("result", "{ note_id, mime }.")],
+ },
+ "voice_note_read" => ControllerSchema {
+ namespace: "channel",
+ function: "web_voice_note_read",
+ description: "Read the audio of a stored voice note (user recording or spoken assistant reply).",
+ inputs: vec![required_string("note_id", "Voice note id from the message metadata.")],
+ outputs: vec![json_output("result", "{ note_id, mime, audio_base64 }; audio_base64 is null when no audio exists.")],
+ },
_ => ControllerSchema {
namespace: "channel",
function: "unknown",
@@ -213,6 +241,30 @@ fn handle_queue_remove(ctx: DispatchCtx, params: Map<String, Value>) -> Controll
})
}
+fn handle_voice_note_store(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
+ Box::pin(async move {
+ let p = deserialize_params::<VoiceNoteStoreParams>(params)?;
+ let config = ctx.load_config().await?;
+ to_json(super::voice_notes::store_from_rpc(
+ &config.workspace_dir,
+ &p.note_id,
+ p.mime.as_deref().unwrap_or("audio/webm"),
+ &p.audio_base64,
+ )?)
+ })
+}
+
+fn handle_voice_note_read(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
+ Box::pin(async move {
+ let p = deserialize_params::<VoiceNoteReadParams>(params)?;
+ let config = ctx.load_config().await?;
+ to_json(super::voice_notes::read_for_rpc(
+ &config.workspace_dir,
+ &p.note_id,
+ )?)
+ })
+}
+
fn handle_cancel(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let p = deserialize_params::<WebCancelParams>(params)?;
diff --git a/crates/openhuman-core/src/web_chat/types.rs b/crates/openhuman-core/src/web_chat/types.rs
index 4cf866af3f..f7e701b4da 100644
--- a/crates/openhuman-core/src/web_chat/types.rs
+++ b/crates/openhuman-core/src/web_chat/types.rs
@@ -225,3 +225,16 @@ pub(super) struct WebCancelParams {
#[serde(default)]
pub(super) request_id: Option<String>,
}
+
+#[derive(Debug, Deserialize)]
+pub(crate) struct VoiceNoteStoreParams {
+ pub(super) note_id: String,
+ pub(super) audio_base64: String,
+ #[serde(default)]
+ pub(super) mime: Option<String>,
+}
+
+#[derive(Debug, Deserialize)]
+pub(crate) struct VoiceNoteReadParams {
+ pub(super) note_id: String,
+}
diff --git a/crates/openhuman-core/src/web_chat/voice_notes.rs b/crates/openhuman-core/src/web_chat/voice_notes.rs
index 2596ccf62a..f9f44da7aa 100644
--- a/crates/openhuman-core/src/web_chat/voice_notes.rs
+++ b/crates/openhuman-core/src/web_chat/voice_notes.rs
@@ -122,6 +122,41 @@ pub(crate) fn reply_note_metadata(workspace: &Path, request_id: &str) -> Option<
})
}
+/// `channel.web_voice_note_store`: decode and store a recorded note.
+pub(crate) fn store_from_rpc(
+ workspace: &Path,
+ note_id: &str,
+ mime: &str,
+ audio_base64: &str,
+) -> Result<crate::core::Outcome<Value>, String> {
+ use base64::Engine as _;
+ let bytes = base64::engine::general_purpose::STANDARD
+ .decode(audio_base64.trim())
+ .map_err(|e| format!("audio_base64 is not valid base64: {e}"))?;
+ let stored = store_note(workspace, note_id, mime, &bytes)?;
+ Ok(crate::core::Outcome::single_log(
+ json!({ "note_id": note_id, "mime": stored }),
+ "voice note stored",
+ ))
+}
+
+/// `channel.web_voice_note_read`: the note's audio, or null audio when absent.
+pub(crate) fn read_for_rpc(
+ workspace: &Path,
+ note_id: &str,
+) -> Result<crate::core::Outcome<Value>, String> {
+ use base64::Engine as _;
+ let value = match read_note(workspace, note_id)? {
+ Some((bytes, mime)) => json!({
+ "note_id": note_id,
+ "mime": mime,
+ "audio_base64": base64::engine::general_purpose::STANDARD.encode(bytes),
+ }),
+ None => json!({ "note_id": note_id, "mime": null, "audio_base64": null }),
+ };
+ Ok(crate::core::Outcome::single_log(value, "voice note read"))
+}
+
/// 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).
diff --git a/crates/openhuman-core/src/web_chat/voice_notes_tests.rs b/crates/openhuman-core/src/web_chat/voice_notes_tests.rs
index 74c61e4172..ea5e7c1c36 100644
--- a/crates/openhuman-core/src/web_chat/voice_notes_tests.rs
+++ b/crates/openhuman-core/src/web_chat/voice_notes_tests.rs
@@ -52,3 +52,37 @@ fn reply_metadata_exists_only_after_the_reply_audio_is_stored() {
assert_eq!(meta["noteId"], "reply-req_1");
assert_eq!(meta["mime"], "audio/mpeg");
}
+
+#[test]
+fn rpc_round_trip_and_bad_base64() {
+ let tmp = tempfile::tempdir().unwrap();
+ let out = store_from_rpc(tmp.path(), "rt", "audio/webm", "YWJj").unwrap();
+ assert_eq!(out.value["mime"], "audio/webm");
+ let read = read_for_rpc(tmp.path(), "rt").unwrap().value;
+ assert_eq!(read["audio_base64"], "YWJj");
+ assert!(read_for_rpc(tmp.path(), "none").unwrap().value["audio_base64"].is_null());
+ assert!(store_from_rpc(tmp.path(), "bad", "audio/webm", "!!!").is_err());
+}
+
+#[test]
+fn synthesized_reply_is_stored_only_with_decodable_audio() {
+ let tmp = tempfile::tempdir().unwrap();
+ assert!(!store_synthesized_reply(tmp.path(), "r1", "", "audio/mpeg"));
+ assert!(!store_synthesized_reply(tmp.path(), "r1", "***", "audio/mpeg"));
+ assert!(store_synthesized_reply(tmp.path(), "r1", "YWJj", "audio/mpeg"));
+ assert!(reply_note_metadata(tmp.path(), "r1").is_some());
+}
+
+#[test]
+fn reply_mode_decides_when_the_reply_is_spoken() {
+ use crate::config::schema::VoiceReplyMode;
+ let mut config = crate::config::Config::default();
+ // Default is mirror: only a voice-note turn is answered aloud.
+ assert!(!should_speak_reply(&config, "t", None, Some("keyboard")));
+ assert!(should_speak_reply(&config, "t", None, Some(VOICE_NOTE_SOURCE)));
+ assert!(should_speak_reply(&config, "t", Some(true), None));
+ config.channel_voice.reply_mode = VoiceReplyMode::Always;
+ assert!(should_speak_reply(&config, "t", None, None));
+ config.channel_voice.threads.insert("t".into(), VoiceReplyMode::Never);
+ assert!(!should_speak_reply(&config, "t", None, Some(VOICE_NOTE_SOURCE)));
+}