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 channel event bus for web chat

Steven Enamakel9b603027ad82
diff --git a/crates/openhuman-core/src/web_chat/channel_event.rs b/crates/openhuman-core/src/web_chat/channel_event.rs
index f55b6f44a8..ff186f985c 100644
--- a/crates/openhuman-core/src/web_chat/channel_event.rs
+++ b/crates/openhuman-core/src/web_chat/channel_event.rs
@@ -18,6 +18,11 @@ pub struct WebChannelEvent {
/// owner's stream in SaaS mode and is not part of the wire payload.
#[serde(skip)]
pub agent: Option<String>,
+ /// The tenant (SaaS profile) whose work produced this event, stamped at
+ /// publish time. Never serialized: the `/events` stream of a SaaS user
+ /// carries only events of that user's profile.
+ #[serde(skip)]
+ pub profile: Option<String>,
/// The event name (e.g., `chat_message`, `tool_call`).
pub event: String,
/// Unique identifier for the Socket.IO client.
@@ -237,6 +242,12 @@ impl WebChannelEvent {
pub fn belongs_to(&self, agent: &str) -> bool {
self.agent.as_deref() == Some(agent)
}
+
+ /// Whether a SaaS profile's stream may carry this event: only one stamped
+ /// with that profile. An unstamped event is dropped rather than guessed at.
+ pub fn belongs_to_profile(&self, profile: &str) -> bool {
+ self.profile.as_deref() == Some(profile)
+ }
}
/// Time-to-first-visible timing summary for a completed turn. See
diff --git a/crates/openhuman-core/src/web_chat/event_bus.rs b/crates/openhuman-core/src/web_chat/event_bus.rs
index 1479e9b50d..5f97531d7d 100644
--- a/crates/openhuman-core/src/web_chat/event_bus.rs
+++ b/crates/openhuman-core/src/web_chat/event_bus.rs
@@ -29,11 +29,26 @@ pub fn publish_web_channel_event(mut event: WebChannelEvent) {
if event.ts.is_none() {
event.ts = Some(crate::web_chat::progress_bridge::unix_epoch_ms());
}
+ stamp_tenant(&mut event, crate::core::runtime::current_tenant().ok());
+ let _ = EVENT_BUS.send(event);
+}
+
+/// Stamp `event` with the tenant whose work produced it, where the publisher
+/// left it unset. A SaaS task with no scope (`None`) stamps nothing, so the
+/// event reaches no user's stream.
+pub(crate) fn stamp_tenant(
+ event: &mut WebChannelEvent,
+ tenant: Option<crate::core::runtime::Tenant>,
+) {
+ let Some(tenant) = tenant else {
+ return;
+ };
if event.agent.is_none() {
- event.agent = crate::core::runtime::CoreContext::current()
- .and_then(|context| context.session_agent().map(str::to_owned));
+ event.agent = tenant.agent;
+ }
+ if event.profile.is_none() {
+ event.profile = tenant.profile;
}
- let _ = EVENT_BUS.send(event);
}
static APPROVAL_SURFACE_HANDLE: OnceLock<SubscriptionHandle> = OnceLock::new();