Skip to content

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

feat(web_chat): validate typed voice-note attachments on chat turns

Steven Enamakel10f9c6da72dd
diff --git a/crates/openhuman-core/src/web_chat/ops/channel_ops.rs b/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
index 4c72d92a67..a0eb5512b8 100644
--- a/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
+++ b/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
@@ -13,7 +13,7 @@ use crate::web_chat::event_context;
use super::super::event_bus::publish_web_channel_event_in;
use super::super::runner::{CancelNote, runtime, scope_of};
use super::super::types::ChatRequestMetadata;
-use super::start_chat::start_chat;
+use super::start_chat::start_chat_with_voice_note;
/// Cancel the in-flight turn(s) for a thread.
///
@@ -200,6 +200,7 @@ pub async fn channel_web_chat(
queue_mode: Option<String>,
run_mode: Option<String>,
metadata: ChatRequestMetadata,
+ voice_note_id: Option<String>,
) -> Result<Outcome<Value>, String> {
// A SaaS user chooses the thread id; the same rules as `threads_upsert`
// apply (no reserved prefixes, no path-like ids). No-op outside SaaS.
@@ -221,7 +222,7 @@ pub async fn channel_web_chat(
}
}
- let result = start_chat(
+ let result = start_chat_with_voice_note(
ctx,
client_id,
thread_id,
@@ -231,6 +232,7 @@ pub async fn channel_web_chat(
locale,
queue_mode,
metadata,
+ voice_note_id,
)
.await?;
diff --git a/crates/openhuman-core/src/web_chat/schemas.rs b/crates/openhuman-core/src/web_chat/schemas.rs
index ae534b453d..5700c2a4ec 100644
--- a/crates/openhuman-core/src/web_chat/schemas.rs
+++ b/crates/openhuman-core/src/web_chat/schemas.rs
@@ -199,6 +199,7 @@ pub fn schemas(function: &str) -> ControllerSchema {
fn handle_chat(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let p = deserialize_params::<WebChatParams>(params)?;
+ let voice_note_id = validate_voice_note_attachment(&ctx, &p).await?;
let message =
crate::agent::attachments::parts::fold_into_message(&p.message, &p.attachments)
.map_err(|e| e.to_string())?;
@@ -230,12 +231,51 @@ fn handle_chat(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture
// arm is known.
hold_text_stream: false,
},
+ voice_note_id,
)
.await?,
)
})
}
+async fn validate_voice_note_attachment(
+ ctx: &DispatchCtx,
+ params: &WebChatParams,
+) -> Result<Option<String>, String> {
+ let voice_notes = params
+ .attachments
+ .iter()
+ .filter_map(|part| match part {
+ tinychannels_bus::ContentPart::VoiceNote { voice_note } => Some(voice_note),
+ _ => None,
+ })
+ .collect::<Vec<_>>();
+ if voice_notes.len() > 1 {
+ return Err("a chat message can carry only one typed voice note".into());
+ }
+ let Some(voice_note) = voice_notes.first() else {
+ if params.source.as_deref() == Some(super::voice_notes::VOICE_NOTE_SOURCE) {
+ return Err("voice-note turns require their typed audio part".into());
+ }
+ return Ok(None);
+ };
+ if params.source.as_deref() != Some(super::voice_notes::VOICE_NOTE_SOURCE) {
+ return Err("typed voice-note audio requires source=voice_note".into());
+ }
+ let note_id = voice_note
+ .media
+ .opaque_id
+ .as_deref()
+ .ok_or_else(|| "typed voice-note audio requires its stored note id".to_string())?;
+ let workspace_dir = ctx
+ .load_config()
+ .await
+ .map_err(|error| format!("load voice note workspace: {error}"))?
+ .workspace_dir;
+ super::voice_notes::validate_typed_media(&workspace_dir, note_id, &voice_note.media)?;
+ Ok(Some(note_id.to_string()))
+}
+
fn handle_queue_status(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let p = deserialize_params::<WebQueueParams>(params)?;