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 turn host for streaming chat turns

Steven Enamakel7cb6a154e3b1
diff --git a/crates/openhuman-core/src/web_chat/ops/start_chat.rs b/crates/openhuman-core/src/web_chat/ops/start_chat.rs
index 1169e967c5..6144b87c1c 100644
--- a/crates/openhuman-core/src/web_chat/ops/start_chat.rs
+++ b/crates/openhuman-core/src/web_chat/ops/start_chat.rs
@@ -288,28 +288,28 @@ async fn start_chat_inner(
let (runner, shared) = (rt.runner.clone(), rt.shared.clone());
let id = request_id.clone();
ctx.spawn(async move {
- let outcome = runner.run(request).await;
- // The first answer wins: a turn that started already told its
- // submitter, and the refusals told theirs at the door.
- match &outcome {
- Err(TurnRunnerError::Queued) => {
- shared.admit(&id, Admission::Queued);
- shared.mark_queued(&id);
- }
- Err(TurnRunnerError::Rejected(reason) | TurnRunnerError::Failed(reason)) => {
- shared.admit(
- &id,
- Admission::Rejected(StartChatError::Other(reason.clone())),
- );
- }
- Err(TurnRunnerError::Consumed | TurnRunnerError::Cancelled) | Ok(_) => {
- shared.admit(&id, Admission::Started);
- }
+ let outcome = runner.run(request).await;
+ // The first answer wins: a turn that started already told its
+ // submitter, and the refusals told theirs at the door.
+ match &outcome {
+ Err(TurnRunnerError::Queued) => {
+ shared.admit(&id, Admission::Queued);
+ shared.mark_queued(&id);
}
- log::debug!(
- "[web-channel] turn finished request_id={id} ok={}",
- outcome.is_ok()
- );
+ Err(TurnRunnerError::Rejected(reason) | TurnRunnerError::Failed(reason)) => {
+ shared.admit(
+ &id,
+ Admission::Rejected(StartChatError::Other(reason.clone())),
+ );
+ }
+ Err(TurnRunnerError::Consumed | TurnRunnerError::Cancelled) | Ok(_) => {
+ shared.admit(&id, Admission::Started);
+ }
+ }
+ log::debug!(
+ "[web-channel] turn finished request_id={id} ok={}",
+ outcome.is_ok()
+ );
});
match admission_rx.await.unwrap_or(Admission::Started) {
diff --git a/crates/openhuman-core/src/web_chat/ops/test_hooks.rs b/crates/openhuman-core/src/web_chat/ops/test_hooks.rs
index 4139183b00..9c27b23c61 100644
--- a/crates/openhuman-core/src/web_chat/ops/test_hooks.rs
+++ b/crates/openhuman-core/src/web_chat/ops/test_hooks.rs
@@ -176,7 +176,7 @@ pub(crate) async fn track_parallel_turn_for_test(
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
}
set_test_run_chat_task_block(None).await;
- tokio::spawn(async move {
+ ctx.clone().spawn(async move {
while !block.dropped.load(std::sync::atomic::Ordering::SeqCst) {
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
}
diff --git a/crates/openhuman-core/src/web_chat/turn_events.rs b/crates/openhuman-core/src/web_chat/turn_events.rs
index 18ea08deae..5f401ac1c8 100644
--- a/crates/openhuman-core/src/web_chat/turn_events.rs
+++ b/crates/openhuman-core/src/web_chat/turn_events.rs
@@ -477,30 +477,30 @@ impl EventSink<QueuedPayload> for WebEvents {
request.thread_id
);
let shared = self.shared.clone();
- ctx.clone().spawn(async move {
- for item in items {
- shared.forget_queued(&item.request_id);
- if let Err(err) = super::ops::start_chat(
- &ctx,
- &item.payload.client_id,
- &item.thread_id,
- &item.text,
- item.payload.model_override.clone(),
- item.payload.temperature,
- item.payload.locale.clone(),
- Some("followup".to_owned()),
- item.payload.metadata.clone(),
- )
- .await
- {
- log::warn!(
- "[web-channel] failed to dispatch collected message thread_id={} err={err}",
- item.thread_id
- );
- }
+ let spawner = ctx.clone();
+ spawner.spawn(async move {
+ for item in items {
+ shared.forget_queued(&item.request_id);
+ if let Err(err) = super::ops::start_chat(
+ &ctx,
+ &item.payload.client_id,
+ &item.thread_id,
+ &item.text,
+ item.payload.model_override.clone(),
+ item.payload.temperature,
+ item.payload.locale.clone(),
+ Some("followup".to_owned()),
+ item.payload.metadata.clone(),
+ )
+ .await
+ {
+ log::warn!(
+ "[web-channel] failed to dispatch collected message thread_id={} err={err}",
+ item.thread_id
+ );
}
- },
- ));
+ }
+ });
}
}
diff --git a/crates/openhuman-core/src/web_chat/turn_host.rs b/crates/openhuman-core/src/web_chat/turn_host.rs
index f7ca824ec6..fb9bfb1c8e 100644
--- a/crates/openhuman-core/src/web_chat/turn_host.rs
+++ b/crates/openhuman-core/src/web_chat/turn_host.rs
@@ -227,9 +227,9 @@ impl WebHost {
output: None,
});
let (drained_tx, mut drained_rx) = watch::channel(false);
- let tap = tap_progress(progress_rx, progress, activity, drained_tx);
+ let tap = tap_progress(&ctx, progress_rx, progress, activity, drained_tx);
let _tap = AbortOnDrop(tap);
- let _beat = AbortOnDrop(spawn_heartbeat(projection.clone()));
+ let _beat = AbortOnDrop(spawn_heartbeat(&ctx, projection.clone()));
agent.set_on_progress(Some(progress_tx));
agent.set_run_queue(Some(queue));
@@ -525,12 +525,13 @@ fn settle_turn_snapshot(
/// idle record and the work tracker on the way. Signals `drained` once the
/// parent's `TurnCompleted` was forwarded or the stream ended.
fn tap_progress(
+ ctx: &DispatchCtx,
mut rx: mpsc::Receiver<AgentProgress>,
progress: mpsc::Sender<TurnSignal>,
activity: TurnActivity,
drained: watch::Sender<bool>,
) -> tokio::task::JoinHandle<()> {
- tokio::spawn(async move {
+ ctx.spawn(async move {
let mut work = WorkTracker::default();
while let Some(event) = rx.recv().await {
activity.observe(&event);
diff --git a/crates/openhuman-core/src/web_chat/turn_host_tests.rs b/crates/openhuman-core/src/web_chat/turn_host_tests.rs
index 8c15850d4d..e692075045 100644
--- a/crates/openhuman-core/src/web_chat/turn_host_tests.rs
+++ b/crates/openhuman-core/src/web_chat/turn_host_tests.rs
@@ -46,7 +46,13 @@ async fn the_tap_forwards_in_order_brackets_open_work_and_signals_the_drain() {
let (runner_tx, mut runner_rx) = mpsc::channel(16);
let (drained_tx, mut drained_rx) = watch::channel(false);
let activity = TurnActivity::default();
- let _tap = tap_progress(agent_rx, runner_tx, activity.clone(), drained_tx);
+ let _tap = tap_progress(
+ &DispatchCtx::ambient(),
+ agent_rx,
+ runner_tx,
+ activity.clone(),
+ drained_tx,
+ );
for event in [
iteration(),
@@ -82,7 +88,13 @@ async fn the_drain_is_released_when_the_stream_ends_without_a_completion() {
let (agent_tx, agent_rx) = mpsc::channel(8);
let (runner_tx, mut runner_rx) = mpsc::channel(16);
let (drained_tx, mut drained_rx) = watch::channel(false);
- let _tap = tap_progress(agent_rx, runner_tx, TurnActivity::default(), drained_tx);
+ let _tap = tap_progress(
+ &DispatchCtx::ambient(),
+ agent_rx,
+ runner_tx,
+ TurnActivity::default(),
+ drained_tx,
+ );
agent_tx.send(AgentProgress::TurnStarted).await.unwrap();
assert_eq!(kind(&runner_rx.recv().await.unwrap()), "other");
assert!(!*drained_rx.borrow());
@@ -98,7 +110,13 @@ async fn a_dropped_runner_stops_the_tap() {
let (agent_tx, agent_rx) = mpsc::channel(8);
let (runner_tx, runner_rx) = mpsc::channel(1);
let (drained_tx, drained_rx) = watch::channel(false);
- let tap = tap_progress(agent_rx, runner_tx, TurnActivity::default(), drained_tx);
+ let tap = tap_progress(
+ &DispatchCtx::ambient(),
+ agent_rx,
+ runner_tx,
+ TurnActivity::default(),
+ drained_tx,
+ );
drop(runner_rx);
agent_tx.send(AgentProgress::TurnStarted).await.unwrap();
tokio::time::timeout(Duration::from_secs(5), tap)
@@ -124,7 +142,7 @@ async fn the_heartbeat_beats_only_for_a_live_turn() {
crate::config::Config::default(),
std::sync::Arc::new(tinyagents_session::port::InMemoryTurnStates::default()),
)));
- let beat = AbortOnDrop(spawn_heartbeat(projection.clone()));
+ let beat = AbortOnDrop(spawn_heartbeat(&DispatchCtx::ambient(), projection.clone()));
// Before `TurnStarted` the turn is not live: no beat, however long it waits.
tokio::time::sleep(Duration::from_secs(INFERENCE_HEARTBEAT_SECS * 3)).await;