Skip to content

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

feat(agent_chat): add thread compaction entry point

Steven Enamakel32bf0f59b377
diff --git a/crates/openhuman-core/src/agent/mod.rs b/crates/openhuman-core/src/agent/mod.rs
index 1d718ad9c9..d3f1fde2ef 100644
--- a/crates/openhuman-core/src/agent/mod.rs
+++ b/crates/openhuman-core/src/agent/mod.rs
@@ -75,5 +75,6 @@ pub(crate) mod turn_policy;
pub(crate) mod turn_progress;
#[allow(unused_imports)]
pub use session_host::{
- HostTools, HostTurnTools, OpenHumanSessionHost, SessionHostBuilder, TurnContext, TurnOverrides,
+ CompactOptions, CompactOutcome, HostTools, HostTurnTools, OpenHumanSessionHost,
+ SessionHostBuilder, TurnContext, TurnOverrides,
};
diff --git a/crates/openhuman-core/src/agent_runs/agent_chat/compact.rs b/crates/openhuman-core/src/agent_runs/agent_chat/compact.rs
new file mode 100644
index 0000000000..a0c4a70015
--- /dev/null
+++ b/crates/openhuman-core/src/agent_runs/agent_chat/compact.rs
@@ -0,0 +1,45 @@
+//! Compacting a thread's stored conversation with no model turn.
+
+use crate::agent::session_host::{CompactOptions, CompactOutcome};
+use crate::config::Config;
+
+use super::session_turn::{build_turn_agent, AgentChatTarget};
+
+/// Compact `thread_id`'s stored conversation on `target`'s session now, so
+/// its next turn resends a summary instead of the full history.
+///
+/// The session is built and bound exactly as [`agent_chat_for`]
+/// (super::agent_chat_for) builds a turn's, so the compaction addresses the
+/// transcript that turn would resume and holds the same per-session turn
+/// lock. See [`OpenHumanSessionHost::compact_bound_history`]
+/// (crate::agent::OpenHumanSessionHost::compact_bound_history).
+///
+/// Returned boxed and `#[inline(never)]` for the reason `agent_chat_for` is:
+/// the future is awaited from other crates.
+///
+/// # Errors
+///
+/// When `thread_id` is blank, the session cannot be built, or the compaction
+/// fails.
+#[inline(never)]
+pub fn compact_thread_for<'a>(
+ config: &'a Config,
+ target: AgentChatTarget<'a>,
+ thread_id: &'a str,
+ options: CompactOptions,
+) -> futures::future::BoxFuture<'a, anyhow::Result<CompactOutcome>> {
+ Box::pin(async move {
+ let thread_id = thread_id.trim();
+ if thread_id.is_empty() {
+ anyhow::bail!("compact_thread needs a thread id");
+ }
+ // The context a turn without a bound host environment uses: the
+ // caller's ambient one.
+ let ctx = crate::core::runtime::DispatchCtx::from_weak(None);
+ let mut session = build_turn_agent(&ctx, config, &target, Some(thread_id), None, None)
+ .map_err(anyhow::Error::msg)?;
+ session.set_thread_id(Some(thread_id));
+ tracing::debug!(thread_id, ?options, "[compact_thread] session bound");
+ session.compact_bound_history(options).await
+ })
+}
diff --git a/crates/openhuman-core/src/agent_runs/agent_chat/mod.rs b/crates/openhuman-core/src/agent_runs/agent_chat/mod.rs
index 917847d816..7609444a90 100644
--- a/crates/openhuman-core/src/agent_runs/agent_chat/mod.rs
+++ b/crates/openhuman-core/src/agent_runs/agent_chat/mod.rs
@@ -1,12 +1,14 @@
//! Full agent chat turns and their shared turn guards.
mod attachments;
+mod compact;
mod controllers;
mod envelope;
pub(crate) mod guards;
pub(crate) mod params;
mod session_turn;
+pub use compact::compact_thread_for;
pub use envelope::AgentChatReply;
pub use session_turn::{
agent_chat, agent_chat_for, agent_chat_reply_for, agent_chat_reply_for_controlled,
diff --git a/crates/openhuman-core/src/agent_runs/agent_chat/session_turn.rs b/crates/openhuman-core/src/agent_runs/agent_chat/session_turn.rs
index beca39f60e..5ec2299524 100644
--- a/crates/openhuman-core/src/agent_runs/agent_chat/session_turn.rs
+++ b/crates/openhuman-core/src/agent_runs/agent_chat/session_turn.rs
@@ -222,7 +222,7 @@ fn definition_for_turn(
}
}
-fn build_turn_agent(
+pub(super) fn build_turn_agent(
ctx: &crate::core::runtime::DispatchCtx,
config: &Config,
target: &AgentChatTarget<'_>,