Skip to content

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

feat(agent): add conversation compaction options and outcome types

Steven Enamakel804d684bb476
diff --git a/crates/openhuman-embed/src/agent/compact.rs b/crates/openhuman-embed/src/agent/compact.rs
new file mode 100644
index 0000000000..28408c340a
--- /dev/null
+++ b/crates/openhuman-embed/src/agent/compact.rs
@@ -0,0 +1,86 @@
+//! [`Agent::compact_thread`]: compact a stored conversation while its user is
+//! idle, so the next turn resends a summary instead of the full history.
+
+use std::sync::Arc;
+
+use super::{Agent, AgentError};
+use crate::CoreError;
+
+pub use openhuman_core::agent::{CompactOptions, CompactOutcome};
+
+/// The method name a compaction's errors carry.
+const COMPACT_THREAD: &str = "agent.compact_thread";
+
+impl Agent {
+ /// Compact `thread_id`'s stored conversation now, with no model turn.
+ ///
+ /// The thread is resolved exactly as [`Turn::session`](crate::Turn::session)
+ /// resolves it, and its head transcript generation is read. Below
+ /// [`CompactOptions::min_tokens`] (the compression middleware's own
+ /// estimate) nothing is written and [`CompactOutcome::Skipped`] reports
+ /// the size. Otherwise the agent's compaction runs as a turn's would
+ /// (`[context] compaction_strategy`, `summarizer_model`,
+ /// `summarizer_tool_rendering`, `summarizer_max_output_tokens`): the head
+ /// generation is sealed unchanged and the next one opens with the
+ /// system prompt, the summary as a user-role message, and the kept tail.
+ /// The system prompt and recorded tool declarations carry over
+ /// byte-identical, so the provider's prefix cache survives.
+ ///
+ /// A compaction holds the session's turn lock from read to write: a turn
+ /// sent on the same thread meanwhile waits for it, then resumes from the
+ /// compacted head.
+ ///
+ /// # Errors
+ ///
+ /// [`AgentError::Call`] when the agent was removed, inference is not in
+ /// this runtime's domains, the thread id is blank, or the compaction
+ /// fails (stores, summarizer models, or the transcript write).
+ pub async fn compact_thread(
+ &self,
+ thread_id: &str,
+ options: CompactOptions,
+ ) -> Result<CompactOutcome, AgentError> {
+ let inner = Arc::clone(&self.inner);
+ if !inner.ctx.domains().inference {
+ return Err(CoreError::Unavailable {
+ method: COMPACT_THREAD,
+ }
+ .into());
+ }
+ let ctx = inner.ctx.clone();
+ let runtime = inner.runtime.clone();
+ let thread_id = thread_id.to_owned();
+ let agent = Arc::clone(&inner);
+ let compaction = Box::pin(async move {
+ use openhuman_core::inference::host_runtime::ops::AgentChatTarget;
+ let host = agent.composed_host_tools();
+ let target = AgentChatTarget::Definition {
+ definition: &agent.definition,
+ host: host.as_ref(),
+ seed: None,
+ usage: None,
+ host_only: agent.host_only,
+ untrusted_input: false,
+ shape: None,
+ observer: None,
+ attachments: &[],
+ };
+ openhuman_core::agent_runs::agent_chat::compact_thread_for(
+ &agent.config,
+ target,
+ &thread_id,
+ options,
+ )
+ .await
+ .map_err(|error| CoreError::Rpc {
+ method: COMPACT_THREAD,
+ message: error.to_string(),
+ })
+ });
+ inner
+ .lifecycle
+ .admit(&inner.id, COMPACT_THREAD, runtime.run_in(ctx, compaction))
+ .await
+ .map_err(Into::into)
+ }
+}
diff --git a/crates/openhuman-embed/src/agent/mod.rs b/crates/openhuman-embed/src/agent/mod.rs
index 61272444b9..088eba56ee 100644
--- a/crates/openhuman-embed/src/agent/mod.rs
+++ b/crates/openhuman-embed/src/agent/mod.rs
@@ -18,6 +18,8 @@ mod approval_handler;
mod approvals;
pub use approval_handler::{ApprovalHandler, ApprovalSubscription};
mod attachments;
+mod compact;
+pub use compact::{CompactOptions, CompactOutcome};
pub(crate) mod lifecycle;
pub(crate) mod mcp_cleanup;
pub use approvals::{ApprovalDecision, Approvals, ApprovalsError, PendingApproval};
diff --git a/crates/openhuman-embed/src/lib.rs b/crates/openhuman-embed/src/lib.rs
index 71fb751dcc..973141cf12 100644
--- a/crates/openhuman-embed/src/lib.rs
+++ b/crates/openhuman-embed/src/lib.rs
@@ -176,7 +176,7 @@ pub use openhuman_core::voice::VOICE_COMPILED_IN;
pub use agent::ToolAttachmentError;
pub use agent::{
Agent, AgentDefinitionSpec, AgentError, AgentLayout, AgentSpec, ApprovalDecision, Approvals,
- ApprovalsError, DefinitionBase, MemoryBinding, MemoryBlockSpec, PendingApproval,
+ ApprovalsError, CompactOptions, CompactOutcome, DefinitionBase, MemoryBinding, MemoryBlockSpec, PendingApproval,
SandboxModeSpec, ToolScopeSpec,
};
pub use auth::{Auth, AuthState, Session};