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 and turn event plumbing

Steven Enamakel3900013b7b38
diff --git a/crates/openhuman-core/src/web_chat/channel_event.rs b/crates/openhuman-core/src/web_chat/channel_event.rs
index 9c4754af18..83bfa99c2c 100644
--- a/crates/openhuman-core/src/web_chat/channel_event.rs
+++ b/crates/openhuman-core/src/web_chat/channel_event.rs
@@ -1,7 +1,7 @@
//! The in-process form of the events the web channel publishes to connected
//! clients.
//!
-//! The wire vocabulary is owned by `tinychannels-bus` ([`bus_web`]): a typed,
+//! The wire vocabulary is owned by `tinychannels-bus` (`bus_web`): a typed,
//! tagged enum plus the payload structs re-exported below. Domains still build
//! this flat struct and hand it to
//! [`publish_web_channel_event`](super::publish_web_channel_event) (the
@@ -215,7 +215,78 @@ pub struct WebChannelEvent {
/// original turn's request context).
#[serde(skip_serializing_if = "Option::is_none")]
pub turn_request_id: Option<String>,
- impl From<bus_web::WebChannelEvent> for WebChannelEvent {
+ /// Time-to-first-visible timing summary, carried on `chat_done`. See
+ /// `web_chat::turn_timing`.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub timing: Option<TurnTimingPayload>,
+ /// Follow-up prompt suggestions offered to the user after a turn.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub suggestions: Option<Vec<ChatSuggestion>>,
+ /// Guardrail verdict attached to a blocked/flagged turn.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub guardrail: Option<GuardrailPayload>,
+ /// Run-queue item this event reports on (queued/delivered/removed).
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub queue_item: Option<QueueItemPayload>,
+ /// Session goal snapshot, carried on goal-lifecycle events. Left as a
+ /// raw `Value` because the goal shape is owned by `tinyagents-graph`,
+ /// not this crate.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub goal: Option<serde_json::Value>,
+ /// Session todo-list snapshot, carried on todo-lifecycle events. Raw
+ /// `Value` for the same reason as `goal`.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub todos: Option<serde_json::Value>,
+ /// Human-readable reason a turn/queue item was cancelled.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub cancel_reason: Option<String>,
+ /// Id of the turn/request that superseded this one (e.g. a steer
+ /// requeue), when applicable.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub superseded_by: Option<String>,
+ /// `Some(true)` on an `approval_request` whose park can outlive the chat
+ /// turn it is shown on (an async-delegated sub-agent). The client keeps
+ /// such a card across that turn's `chat_done` and clears it on
+ /// `approval_decided` instead. Absent for an ordinary in-turn park.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub detached: Option<bool>,
+ /// Typed content parts (media, voice notes) of `chat_done` /
+ /// `chat_message` / `proactive_message`, as the web adapter projects them.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub parts: Option<serde_json::Value>,
+}
+
+impl WebChannelEvent {
+ /// Whether a SaaS user agent's stream may carry this event: only one
+ /// stamped with that agent. An unstamped event (published outside any
+ /// agent scope) is dropped rather than guessed at.
+ 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)
+ }
+
+ /// Stamp the tenant whose work produced this event where the publisher
+ /// left it unset. A SaaS task with no scope (`None`) stamps nothing, so
+ /// the event reaches no user's stream.
+ pub fn stamp_tenant(&mut self, tenant: Option<crate::core::runtime::Tenant>) {
+ let Some(tenant) = tenant else {
+ return;
+ };
+ if self.agent.is_none() {
+ self.agent = tenant.agent;
+ }
+ if self.profile.is_none() {
+ self.profile = tenant.profile;
+ }
+ }
+}
+
+impl From<bus_web::WebChannelEvent> for WebChannelEvent {
/// The wire form of a typed event is, by construction, the flat struct's
/// serialization, so the conversion goes through it. Two spellings the
/// typed projection cannot omit are normalised to the deployed wire: a
diff --git a/crates/openhuman-core/src/web_chat/event_bus_channel.rs b/crates/openhuman-core/src/web_chat/event_bus_channel.rs
index 214912336a..be625606aa 100644
--- a/crates/openhuman-core/src/web_chat/event_bus_channel.rs
+++ b/crates/openhuman-core/src/web_chat/event_bus_channel.rs
@@ -27,7 +27,7 @@ pub fn publish_web_channel_event(event: WebChannelEvent) {
/// the calling turn runs under, rather than the calling task's own scope.
pub fn publish_web_channel_event_in(ctx: &DispatchCtx, mut event: WebChannelEvent) {
if event.ts.is_none() {
- event.ts = Some(crate::web_chat::progress_bridge::unix_epoch_ms());
+ event.ts = Some(crate::web_chat::progress::unix_epoch_ms());
}
event.stamp_tenant(ctx.tenant().ok());
let _ = EVENT_BUS.send(event);
diff --git a/crates/openhuman-core/src/web_chat/ops/system_turn.rs b/crates/openhuman-core/src/web_chat/ops/system_turn.rs
index dbc323862b..018c53e7e4 100644
--- a/crates/openhuman-core/src/web_chat/ops/system_turn.rs
+++ b/crates/openhuman-core/src/web_chat/ops/system_turn.rs
@@ -19,7 +19,6 @@ use tinyagents_harness::run_queue::RunQueue;
use crate::agent::turn_origin::{with_origin, AgentTurnOrigin};
-use super::super::run_task::turn_error_discards_session;
use super::super::session::{
checkin_session_agent_if_vacant, checkout_session_agent, CheckedOutSession, CheckoutPolicy,
};
@@ -96,7 +95,7 @@ pub async fn run_system_turn_on_thread(
// Same rule as a user turn: a failed harness turn can leave the session
// partially advanced, so let it drop and cold-boot from the durable
// transcript on the next request.
- if matches!(&result, Err(err) if turn_error_discards_session(err)) {
+ if result.is_err() {
log::warn!(
"[web-channel] dropping session agent after failed system \
turn thread={} run_id={}",
diff --git a/crates/openhuman-core/src/web_chat/presentation_test_support_tests.rs b/crates/openhuman-core/src/web_chat/presentation_test_support_tests.rs
index 2d99b48097..fabba8983b 100644
--- a/crates/openhuman-core/src/web_chat/presentation_test_support_tests.rs
+++ b/crates/openhuman-core/src/web_chat/presentation_test_support_tests.rs
@@ -30,7 +30,7 @@ pub(crate) async fn deliver_response_with_timing_for_test(
full_response: &str,
user_message: &str,
usage: Option<&super::LastTurnUsage>,
- timing: Option<super::super::turn_timing::TurnTimingSnapshot>,
+ timing: Option<tinychannels_bus::web::TurnTimingSnapshot>,
) {
super::deliver_response(
&crate::core::runtime::DispatchCtx::ambient(),
diff --git a/crates/openhuman-core/src/web_chat/progress.rs b/crates/openhuman-core/src/web_chat/progress.rs
index de7c4ccbab..0253829e2a 100644
--- a/crates/openhuman-core/src/web_chat/progress.rs
+++ b/crates/openhuman-core/src/web_chat/progress.rs
@@ -83,7 +83,7 @@ pub(crate) struct ProgressProjection {
/// task id -> the parent tool call that spawned it, remembered from
/// `SubagentSpawned` so the later lifecycle events still carry it.
subagent_parent_call_ids: HashMap<String, Option<String>>,
- turn_state: TurnStateMirror,
+ turn_state: Option<TurnStateMirror>,
span_collector: Option<SpanCollector>,
journal_trace_ctx: Option<tinyagents_harness::observability::trace_export::TraceContext>,
timing_snapshot: Option<TurnTimingSnapshot>,
@@ -120,11 +120,11 @@ impl ProgressProjection {
.unzip();
Self {
surface: Surface::new(ctx, ids.clone()),
- turn_state: TurnStateMirror::new(
+ turn_state: Some(TurnStateMirror::new(
turn_states,
ids.thread_id.clone(),
ids.request_id.clone(),
- ),
+ )),
ids,
config,
ctx: ctx.clone(),
@@ -201,7 +201,9 @@ impl ProgressProjection {
/// Project one event of the turn, in order.
pub(crate) async fn on_event(&mut self, event: AgentProgress) {
self.events_seen += 1;
- self.turn_state.observe(&event);
+ if let Some(mirror) = self.turn_state.as_mut() {
+ mirror.observe(&event);
+ }
if let Some(collector) = self.span_collector.as_mut() {
collector.record(&event, unix_epoch_ms());
}
@@ -677,8 +679,10 @@ impl ProgressProjection {
/// The stream is over: close the turn-state mirror, record an interrupted
/// run if the parent never completed, and hand the run's trace to the
/// configured sink. Best-effort and gated; never affects the turn outcome.
- pub(crate) async fn finish(mut self) {
- self.turn_state.finish();
+ pub(crate) async fn finish(&mut self) {
+ if let Some(mirror) = self.turn_state.take() {
+ mirror.finish();
+ }
if !self.parent_completed {
self.ledger().turn_interrupted(self.events_seen);
}
diff --git a/crates/openhuman-core/src/web_chat/turn_events.rs b/crates/openhuman-core/src/web_chat/turn_events.rs
index 37f6bc4587..3de371770b 100644
--- a/crates/openhuman-core/src/web_chat/turn_events.rs
+++ b/crates/openhuman-core/src/web_chat/turn_events.rs
@@ -25,7 +25,6 @@ use super::web_errors::{
turn_timeout_error_message, ClassifiedError,
};
use crate::agent::queued_turn::QueuedPayload;
-use crate::core::runtime::DispatchCtx;
/// The runner's wall-clock failure, as the marker string the classifier and the
/// Sentry policy recognise.