Skip to content

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

refactor(dispatch): rework inbound batch merging around envelope fields

Steven Enamakel2e7e7b69dec8
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/inbound_batch.rs b/crates/openhuman-core/src/channels/runtime/dispatch/inbound_batch.rs
index 15e5413ccc..11a551b42d 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/inbound_batch.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/inbound_batch.rs
@@ -1,5 +1,6 @@
-//! Channel messages as [`crate::inbound_debounce`] items, and the merge that
-//! turns one debounced batch into the single message its turn runs on.
+//! Admitted channel messages as [`crate::inbound_debounce`] items, and the
+//! merge that turns one debounced batch into the single message its turn runs
+//! on.
use super::processor::AdmittedInbound;
use crate::inbound_debounce::DebounceItem;
@@ -8,55 +9,68 @@ use tinychannels_bus::channel::ContentPart;
const MEDIA_MARKERS: [&str; 5] = ["[IMAGE:", "[FILE:", "[VIDEO:", "[DOCUMENT:", "[Attachment:"];
/// An admitted message with its receive order on the dispatch queue.
-#[derive(Debug, Clone)]
pub(crate) struct QueuedInbound {
pub(crate) seq: u64,
pub(crate) inbound: AdmittedInbound,
}
+fn part_is_media(part: &ContentPart) -> bool {
+ !matches!(part, ContentPart::Text { .. })
+}
+
impl DebounceItem for QueuedInbound {
fn chars(&self) -> usize {
- self.inbound.msg.text.chars().count()
+ self.inbound.msg.content.chars().count()
}
fn carries_media(&self) -> bool {
- let content = &self.inbound.msg.text;
+ let content = &self.inbound.msg.content;
MEDIA_MARKERS.iter().any(|marker| content.contains(marker))
+ || self.inbound.envelope.parts.iter().any(part_is_media)
}
fn mergeable(&self) -> bool {
- let trimmed = self.inbound.msg.text.trim_start();
- !trimmed.is_empty() && !trimmed.starts_with('/')
+ let inbound = &self.inbound;
+ let trimmed = inbound.msg.content.trim_start();
+ // Imported history and hosted-relay callback targets belong to exactly
+ // one message, so such a message always runs as its own batch.
+ !trimmed.is_empty()
+ && !trimmed.starts_with('/')
+ && inbound.imported_history.is_empty()
+ && inbound.legacy_history.is_none()
+ && inbound.native_target.is_none()
}
}
/// Merge a batch into one message: ordered by (timestamp, receive order),
/// addressed like the newest message, with the non-blank bodies joined by
-/// newlines.
+/// newlines and the typed content parts concatenated in the same order.
pub(crate) fn merge_channel_batch(mut batch: Vec<QueuedInbound>) -> Option<AdmittedInbound> {
- batch.sort_by_key(|item| {
- (
- item.inbound.msg.timestamp_unix_ms.unwrap_or_default(),
- item.seq,
- )
- });
+ batch.sort_by_key(|item| (item.inbound.msg.timestamp, item.seq));
if batch.len() <= 1 {
return batch.pop().map(|item| item.inbound);
}
let sender_name = batch
.iter()
.rev()
- .find_map(|item| item.inbound.msg.sender.name.clone());
+ .find_map(|item| item.inbound.msg.sender_name.clone());
let content = batch
.iter()
- .map(|item| item.inbound.msg.text.as_str())
+ .map(|item| item.inbound.msg.content.as_str())
.filter(|content| !content.trim().is_empty())
.collect::<Vec<_>>()
.join("\n");
+ let text = batch
+ .iter()
+ .map(|item| item.inbound.envelope.text.as_str())
+ .filter(|text| !text.trim().is_empty())
+ .collect::<Vec<_>>()
+ .join("\n");
+ let typed = batch.iter().any(|item| item.inbound.typed_envelope_input);
let mut parts = Vec::new();
let mut has_content = false;
for item in &batch {
- if item.inbound.msg.text.trim().is_empty() {
+ if item.inbound.msg.content.trim().is_empty() && item.inbound.envelope.parts.is_empty() {
continue;
}
if has_content {
@@ -65,17 +79,19 @@ pub(crate) fn merge_channel_batch(mut batch: Vec<QueuedInbound>) -> Option<Admit
markdown_dialect: None,
});
}
- parts.extend(item.inbound.msg.parts.iter().cloned());
+ parts.extend(item.inbound.envelope.parts.iter().cloned());
has_content = true;
}
let count = batch.len();
let mut merged = batch.pop()?.inbound;
- merged.msg.text = content;
- merged.msg.parts = parts;
- merged.msg.sender.name = sender_name;
+ merged.msg.content = content;
+ merged.msg.sender_name = sender_name;
+ merged.envelope.text = text;
+ merged.envelope.parts = parts;
+ merged.typed_envelope_input = typed;
tracing::debug!(
- channel = %merged.msg.channel.id,
- message_id = %merged.msg.message_id,
+ channel = %merged.msg.channel,
+ message_id = %merged.msg.id,
merged = count,
"[dispatch][debounce] merged inbound batch"
);
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs
index 9853146806..02dd0bcf42 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs
@@ -14,8 +14,10 @@
//! [`process_channel_runtime_message`] with a bounded set of host workers
//! and joins them when the inbound queue closes.
+mod admit;
mod approval;
mod bus_turn;
+mod in_order;
mod native_media;
pub(crate) mod progressive;
mod reset;
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
index f272051c21..ffdcc65ab0 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
@@ -4,11 +4,10 @@
use crate::channels::context::{is_context_window_overflow_error, ChannelRuntimeContext};
#[cfg(test)]
use crate::channels::routes::get_or_create_turn_model_source;
-use crate::channels::routes::{get_route_selection, handle_runtime_command_with_reset};
+use crate::channels::routes::get_route_selection;
use crate::channels::SendMessage;
use crate::core::bus::BUS;
use crate::core::events::DomainEvent;
-use crate::util::truncate_with_ellipsis;
use std::sync::Arc;
use std::time::Instant;
use tokio_util::sync::CancellationToken;
@@ -19,39 +18,10 @@ use super::super::helpers::{
REPLY_LOG_TRUNCATE_CHARS,
};
use super::super::host_agent::{self, HostRoute};
-use super::approval::{channel_has_approval_surface, try_route_approval_reply};
+use super::admit::{admit_runtime_message, AdmittedInbound};
+use super::approval::channel_has_approval_surface;
use super::RuntimeChannelMessage;
-fn channel_inbound_preparation_request(
- source: tinychannels_bus::module_inbound::ChannelInboundSource,
- preparation_mode: tinychannels_bus::module_inbound::ChannelInboundPreparationMode,
-) -> tinychannels_bus::module_inbound::PrepareChannelInboundRequest {
- tinychannels_bus::module_inbound::PrepareChannelInboundRequest {
- source,
- namespace: "main".into(),
- session_policy: Default::default(),
- preparation_mode,
- }
-}
-
-fn inbound_console_preview(
- envelope: &tinychannels_bus::ChannelInboundEnvelope,
- text: &str,
-) -> String {
- let contains_audio = envelope.parts.iter().any(|part| {
- matches!(
- part,
- tinychannels_bus::ContentPart::VoiceNote { .. }
- | tinychannels_bus::ContentPart::Audio { .. }
- )
- });
- if contains_audio {
- "[voice message]".to_owned()
- } else {
- truncate_with_ellipsis(text, 80)
- }
-}
-
pub(crate) async fn process_channel_runtime_message(
ctx: Arc<ChannelRuntimeContext>,
runtime_msg: RuntimeChannelMessage,