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 system turn handling for web chat sessions

Steven Enamakel6233dc62b8e8
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 c2c9f20bc1..dbc323862b 100644
--- a/crates/openhuman-core/src/web_chat/ops/system_turn.rs
+++ b/crates/openhuman-core/src/web_chat/ops/system_turn.rs
@@ -56,6 +56,7 @@ pub async fn run_system_turn_on_thread(
mut agent,
fingerprint,
} = checkout_session_agent(
+ ctx,
&config,
SYSTEM_CLIENT_ID,
thread_id,
@@ -103,7 +104,7 @@ pub async fn run_system_turn_on_thread(
run_id
);
} else {
- checkin_session_agent_if_vacant(thread_id, agent, fingerprint).await;
+ checkin_session_agent_if_vacant(ctx, thread_id, agent, fingerprint).await;
}
result
diff --git a/crates/openhuman-core/src/web_chat/run_task.rs b/crates/openhuman-core/src/web_chat/run_task.rs
index 8d01439e5d..45d19bc55f 100644
--- a/crates/openhuman-core/src/web_chat/run_task.rs
+++ b/crates/openhuman-core/src/web_chat/run_task.rs
@@ -139,6 +139,7 @@ pub(crate) async fn run_chat_task(
mut agent,
fingerprint: current_fp,
} = checkout_session_agent(
+ ctx,
&config,
client_id,
thread_id,
@@ -214,7 +215,7 @@ pub(crate) async fn run_chat_task(
// A successful turn proves the thread's balance is usable, so drop
// any stale budget-exhausted signal before it could mislabel a
// later genuine empty response. See #3386.
- super::ops::clear_budget_signal(thread_id).await;
+ super::ops::clear_budget_signal(ctx, thread_id).await;
let citations = crate::memory::tools::take_turn_citations(thread_id);
let usage = agent.take_last_turn_usage_totals();
Ok(WebChatTaskResult {
@@ -232,7 +233,7 @@ pub(crate) async fn run_chat_task(
// Only consult the cross-turn signal for an empty response that is
// not already a budget error — avoids taking the lock on every turn.
let has_fresh_signal = if !is_budget && is_empty {
- super::ops::has_fresh_budget_signal(thread_id, &current_fp.provider_binding).await
+ super::ops::has_fresh_budget_signal(ctx, thread_id, &current_fp.provider_binding).await
} else {
false
};
@@ -242,7 +243,7 @@ pub(crate) async fn run_chat_task(
// so a follow-up empty 200 on this thread+provider (managed
// route closes the SSE clean under credit exhaustion, no
// inline marker) reclassifies as budget. #3386.
- super::ops::record_budget_signal(thread_id, &current_fp.provider_binding).await;
+ super::ops::record_budget_signal(ctx, thread_id, &current_fp.provider_binding).await;
log::warn!(
"[web-channel] inference budget exhausted for client={} thread={} request_id={} error_category=budget_exhausted",
client_id,
@@ -429,7 +430,7 @@ pub(crate) async fn run_chat_task(
request_id
);
} else {
- checkin_session_agent(thread_id, agent, current_fp).await;
+ checkin_session_agent(ctx, thread_id, agent, current_fp).await;
}
}
diff --git a/crates/openhuman-core/src/web_chat/session.rs b/crates/openhuman-core/src/web_chat/session.rs
index 8f5de8817c..8ef81a7d0d 100644
--- a/crates/openhuman-core/src/web_chat/session.rs
+++ b/crates/openhuman-core/src/web_chat/session.rs
@@ -378,6 +378,7 @@ pub(crate) struct CheckedOutSession {
/// turns can never drive one agent; `checkin_session_agent` /
/// `checkin_session_agent_if_vacant` put it back.
pub(crate) async fn checkout_session_agent(
+ ctx: &crate::core::runtime::DispatchCtx,
config: &Config,
client_id: &str,
thread_id: &str,
@@ -386,7 +387,7 @@ pub(crate) async fn checkout_session_agent(
locale: Option<&str>,
policy: CheckoutPolicy,
) -> Result<CheckedOutSession, String> {
- let map_key = super::ops::key_for(thread_id);
+ let map_key = super::ops::key_for(ctx, thread_id);
let target_agent_id = pick_target_agent_id(config);
let effective = effective_session_config(config, model_override.as_deref(), temperature);
let provider_role = provider_role_for_model_override(effective.default_model.as_deref());
@@ -514,13 +515,14 @@ pub(crate) async fn checkout_session_agent(
/// The primary user-turn path: it owns the thread's `IN_FLIGHT` slot, so any
/// entry it finds was left by a turn that ran concurrently and is now stale.
pub(crate) async fn checkin_session_agent(
+ ctx: &crate::core::runtime::DispatchCtx,
thread_id: &str,
agent: OpenHumanSessionHost,
fingerprint: SessionCacheFingerprint,
) {
let mut sessions = super::ops::thread_sessions().lock_owned().await;
sessions.insert(
- super::ops::key_for(thread_id),
+ super::ops::key_for(ctx, thread_id),
SessionEntry { agent, fingerprint },
);
}
@@ -531,12 +533,13 @@ pub(crate) async fn checkin_session_agent(
/// agent and cached it; that one carries the user's newer turn and must win.
/// Both turns appended to the thread's durable transcript regardless.
pub(crate) async fn checkin_session_agent_if_vacant(
+ ctx: &crate::core::runtime::DispatchCtx,
thread_id: &str,
agent: OpenHumanSessionHost,
fingerprint: SessionCacheFingerprint,
) -> bool {
let mut sessions = super::ops::thread_sessions().lock_owned().await;
- match sessions.entry(super::ops::key_for(thread_id)) {
+ match sessions.entry(super::ops::key_for(ctx, thread_id)) {
std::collections::hash_map::Entry::Occupied(_) => {
log::info!(
"[web-channel] system turn finished after a newer turn re-cached thread={} — \