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 voice notes support

Steven Enamakel84747dbc43f0
diff --git a/crates/openhuman-core/src/web_chat/voice_notes.rs b/crates/openhuman-core/src/web_chat/voice_notes.rs
new file mode 100644
index 0000000000..0994ce2a80
--- /dev/null
+++ b/crates/openhuman-core/src/web_chat/voice_notes.rs
@@ -0,0 +1,125 @@
+//! Durable audio for in-app voice notes.
+//!
+//! A voice note is an audio file the thread row points at. The user's
+//! recording and the assistant's spoken reply are both stored under
+//! `<workspace>/voice_notes/<note_id>.<ext>`; the row only carries
+//! `{ noteId, mime }` in its metadata, so a reload (or a second window) can
+//! fetch the bytes again through `channel.web_voice_note_read`.
+//!
+//! Assistant replies use [`reply_note_id`], derived from the turn's
+//! `request_id`, which is what lets reply persistence find the audio the turn
+//! synthesized without threading it through the turn result.
+
+use std::path::{Path, PathBuf};
+
+use serde_json::{json, Value};
+
+const LOG_PREFIX: &str = "[web_channel][voice_note]";
+const DIR: &str = "voice_notes";
+/// Largest note the store accepts (decoded bytes).
+pub(crate) const MAX_NOTE_BYTES: usize = 25 * 1024 * 1024;
+
+/// `(extension, mime)` pairs the store writes and serves.
+const KNOWN: &[(&str, &str)] = &[
+ ("webm", "audio/webm"),
+ ("ogg", "audio/ogg"),
+ ("m4a", "audio/mp4"),
+ ("mp3", "audio/mpeg"),
+ ("wav", "audio/wav"),
+];
+
+/// Id of the audio a turn's spoken reply is stored under.
+pub(crate) fn reply_note_id(request_id: &str) -> String {
+ let safe: String = request_id
+ .chars()
+ .map(|c| if c.is_ascii_alphanumeric() || c == '-' { c } else { '_' })
+ .collect();
+ format!("reply-{safe}")
+}
+
+/// Ids become file names, so only a conservative alphabet is accepted.
+pub(crate) fn is_valid_note_id(id: &str) -> bool {
+ !id.is_empty()
+ && id.len() <= 128
+ && id
+ .chars()
+ .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
+}
+
+fn extension_for_mime(mime: &str) -> &'static str {
+ let base = mime.split(';').next().unwrap_or("").trim().to_ascii_lowercase();
+ match base.as_str() {
+ "audio/ogg" => "ogg",
+ "audio/mp4" | "audio/x-m4a" | "audio/aac" => "m4a",
+ "audio/mpeg" | "audio/mp3" => "mp3",
+ "audio/wav" | "audio/x-wav" | "audio/wave" => "wav",
+ _ => "webm",
+ }
+}
+
+fn dir(workspace: &Path) -> PathBuf {
+ workspace.join(DIR)
+}
+
+/// Writes `bytes` as note `id`, replacing any earlier file for that id.
+/// Returns the stored mime type.
+pub(crate) fn store_note(
+ workspace: &Path,
+ id: &str,
+ mime: &str,
+ bytes: &[u8],
+) -> Result<&'static str, String> {
+ if !is_valid_note_id(id) {
+ return Err("invalid voice note id".to_string());
+ }
+ if bytes.is_empty() {
+ return Err("voice note audio is empty".to_string());
+ }
+ if bytes.len() > MAX_NOTE_BYTES {
+ return Err(format!("voice note exceeds {MAX_NOTE_BYTES} bytes"));
+ }
+ let ext = extension_for_mime(mime);
+ let root = dir(workspace);
+ std::fs::create_dir_all(&root).map_err(|e| format!("create voice_notes dir: {e}"))?;
+ for (other, _) in KNOWN.iter().filter(|(e, _)| *e != ext) {
+ let _ = std::fs::remove_file(root.join(format!("{id}.{other}")));
+ }
+ std::fs::write(root.join(format!("{id}.{ext}")), bytes)
+ .map_err(|e| format!("write voice note: {e}"))?;
+ log::debug!("{LOG_PREFIX} stored id={id} ext={ext} bytes={}", bytes.len());
+ Ok(KNOWN.iter().find(|(e, _)| *e == ext).map(|(_, m)| *m).unwrap_or("audio/webm"))
+}
+
+/// Reads note `id`; `Ok(None)` when no file exists.
+pub(crate) fn read_note(
+ workspace: &Path,
+ id: &str,
+) -> Result<Option<(Vec<u8>, &'static str)>, String> {
+ if !is_valid_note_id(id) {
+ return Err("invalid voice note id".to_string());
+ }
+ let root = dir(workspace);
+ for (ext, mime) in KNOWN {
+ let path = root.join(format!("{id}.{ext}"));
+ if path.is_file() {
+ let bytes = std::fs::read(&path).map_err(|e| format!("read voice note: {e}"))?;
+ return Ok(Some((bytes, mime)));
+ }
+ }
+ Ok(None)
+}
+
+/// The `voiceNote` metadata for a reply whose audio was stored, if any.
+pub(crate) fn reply_note_metadata(workspace: &Path, request_id: &str) -> Option<Value> {
+ let id = reply_note_id(request_id);
+ let root = dir(workspace);
+ KNOWN.iter().find_map(|(ext, mime)| {
+ root.join(format!("{id}.{ext}"))
+ .is_file()
+ .then(|| json!({ "noteId": id, "mime": mime }))
+ })
+}
+
+#[cfg(test)]
+#[path = "voice_notes_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/web_chat/voice_notes_tests.rs b/crates/openhuman-core/src/web_chat/voice_notes_tests.rs
new file mode 100644
index 0000000000..74c61e4172
--- /dev/null
+++ b/crates/openhuman-core/src/web_chat/voice_notes_tests.rs
@@ -0,0 +1,54 @@
+use super::*;
+
+#[test]
+fn stored_note_reads_back_with_mime() {
+ let tmp = tempfile::tempdir().unwrap();
+ let mime = store_note(tmp.path(), "note-1", "audio/ogg;codecs=opus", b"abc").unwrap();
+ assert_eq!(mime, "audio/ogg");
+ let (bytes, mime) = read_note(tmp.path(), "note-1").unwrap().unwrap();
+ assert_eq!(bytes, b"abc");
+ assert_eq!(mime, "audio/ogg");
+}
+
+#[test]
+fn restoring_under_another_mime_drops_the_old_file() {
+ let tmp = tempfile::tempdir().unwrap();
+ store_note(tmp.path(), "n", "audio/webm", b"one").unwrap();
+ store_note(tmp.path(), "n", "audio/mpeg", b"two").unwrap();
+ let (bytes, mime) = read_note(tmp.path(), "n").unwrap().unwrap();
+ assert_eq!((bytes.as_slice(), mime), (b"two".as_slice(), "audio/mpeg"));
+ assert!(!tmp.path().join("voice_notes/n.webm").exists());
+}
+
+#[test]
+fn missing_note_is_none() {
+ let tmp = tempfile::tempdir().unwrap();
+ assert!(read_note(tmp.path(), "nope").unwrap().is_none());
+}
+
+#[test]
+fn path_traversal_ids_are_rejected() {
+ let tmp = tempfile::tempdir().unwrap();
+ assert!(store_note(tmp.path(), "../evil", "audio/webm", b"x").is_err());
+ assert!(read_note(tmp.path(), "a/b").is_err());
+ assert!(!is_valid_note_id(""));
+ assert!(!is_valid_note_id(&"a".repeat(129)));
+}
+
+#[test]
+fn empty_and_oversized_audio_are_rejected() {
+ let tmp = tempfile::tempdir().unwrap();
+ assert!(store_note(tmp.path(), "e", "audio/webm", b"").is_err());
+ let big = vec![0u8; MAX_NOTE_BYTES + 1];
+ assert!(store_note(tmp.path(), "big", "audio/webm", &big).is_err());
+}
+
+#[test]
+fn reply_metadata_exists_only_after_the_reply_audio_is_stored() {
+ let tmp = tempfile::tempdir().unwrap();
+ assert!(reply_note_metadata(tmp.path(), "req/1").is_none());
+ store_note(tmp.path(), &reply_note_id("req/1"), "audio/mpeg", b"mp3").unwrap();
+ let meta = reply_note_metadata(tmp.path(), "req/1").unwrap();
+ assert_eq!(meta["noteId"], "reply-req_1");
+ assert_eq!(meta["mime"], "audio/mpeg");
+}