Skip to content

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

refactor(session_host): replace custom session hooks with runtime FnSessionHooks

Steven Enamakelad6cc4adb743
diff --git a/crates/openhuman-core/src/agent/session_host/hooks.rs b/crates/openhuman-core/src/agent/session_host/hooks.rs
deleted file mode 100644
index ca38c0be01..0000000000
--- a/crates/openhuman-core/src/agent/session_host/hooks.rs
+++ /dev/null
@@ -1,131 +0,0 @@
-//! OpenHuman lifecycle observations for a neutral TinyAgents session.
-
-use async_trait::async_trait;
-use std::{future::Future, pin::Pin, sync::Arc};
-use tinyagents_runtime::{
- CommitReceipt, ResumePreparation, RuntimeError, SessionHooks, SessionStateView,
- SessionTerminal, SessionTurnOutcome, SessionTurnRequest, TranscriptTurnOptions, TurnOptions,
- TurnPreparation,
-};
-
-use crate::agent::tinyagents::host::OpenHumanRunContext;
-
-/// Product callbacks attached to one runtime session.
-///
-/// The runtime makes the durable commit before `after_commit` and guarantees a
-/// single terminal callback. OpenHuman adapters use these points for progress,
-/// BUS projection, memory/experience work and usage finalization.
-#[allow(clippy::type_complexity)]
-pub struct OpenHumanSessionHooks {
- before_resume: Arc<
- dyn for<'a> Fn(
- &'a mut SessionTurnRequest,
- &'a mut TurnOptions<OpenHumanRunContext>,
- SessionStateView<'a>,
- ) -> HookFuture<'a, ResumePreparation>
- + Send
- + Sync,
- >,
- before: Arc<
- dyn for<'a> Fn(
- &'a mut SessionTurnRequest,
- &'a mut TurnOptions<OpenHumanRunContext>,
- SessionStateView<'a>,
- ) -> HookFuture<'a, TurnPreparation>
- + Send
- + Sync,
- >,
- before_commit: Arc<
- dyn for<'a> Fn(
- &'a SessionTurnOutcome,
- &'a TranscriptTurnOptions<OpenHumanRunContext>,
- ) -> HookFuture<'a, ()>
- + Send
- + Sync,
- >,
- committed:
- Arc<dyn Fn(CommitReceipt<OpenHumanRunContext>) -> HookFuture<'static, ()> + Send + Sync>,
- terminal: Arc<dyn Fn(SessionTerminal) -> HookFuture<'static, ()> + Send + Sync>,
-}
-
-type HookFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T, RuntimeError>> + Send + 'a>>;
-
-impl OpenHumanSessionHooks {
- /// Installs product callbacks. Callbacks are intentionally synchronous;
- /// expensive memory and learning work should be spawned by `committed` so
- /// it cannot relabel an already durable turn.
- pub fn new(
- before_resume: impl for<'a> Fn(
- &'a mut SessionTurnRequest,
- &'a mut TurnOptions<OpenHumanRunContext>,
- SessionStateView<'a>,
- ) -> HookFuture<'a, ResumePreparation>
- + Send
- + Sync
- + 'static,
- before: impl for<'a> Fn(
- &'a mut SessionTurnRequest,
- &'a mut TurnOptions<OpenHumanRunContext>,
- SessionStateView<'a>,
- ) -> HookFuture<'a, TurnPreparation>
- + Send
- + Sync
- + 'static,
- before_commit: impl for<'a> Fn(
- &'a SessionTurnOutcome,
- &'a TranscriptTurnOptions<OpenHumanRunContext>,
- ) -> HookFuture<'a, ()>
- + Send
- + Sync
- + 'static,
- committed: impl Fn(CommitReceipt<OpenHumanRunContext>) -> HookFuture<'static, ()>
- + Send
- + Sync
- + 'static,
- terminal: impl Fn(SessionTerminal) -> HookFuture<'static, ()> + Send + Sync + 'static,
- ) -> Self {
- Self {
- before_resume: Arc::new(before_resume),
- before: Arc::new(before),
- before_commit: Arc::new(before_commit),
- committed: Arc::new(committed),
- terminal: Arc::new(terminal),
- }
- }
-}
-
-#[async_trait]
-impl SessionHooks<OpenHumanRunContext> for OpenHumanSessionHooks {
- async fn before_resume(
- &self,
- request: &mut SessionTurnRequest,
- options: &mut TurnOptions<OpenHumanRunContext>,
- state: SessionStateView<'_>,
- ) -> Result<ResumePreparation, RuntimeError> {
- (self.before_resume)(request, options, state).await
- }
- async fn before_turn(
- &self,
- request: &mut SessionTurnRequest,
- options: &mut TurnOptions<OpenHumanRunContext>,
- state: SessionStateView<'_>,
- ) -> Result<TurnPreparation, RuntimeError> {
- (self.before)(request, options, state).await
- }
- async fn before_commit(
- &self,
- outcome: &SessionTurnOutcome,
- options: &TranscriptTurnOptions<OpenHumanRunContext>,
- ) -> Result<(), RuntimeError> {
- (self.before_commit)(outcome, options).await
- }
- async fn after_commit(
- &self,
- receipt: CommitReceipt<OpenHumanRunContext>,
- ) -> Result<(), RuntimeError> {
- (self.committed)(receipt).await
- }
- async fn on_terminal(&self, terminal: SessionTerminal) -> Result<(), RuntimeError> {
- (self.terminal)(terminal).await
- }
-}
diff --git a/crates/openhuman-core/src/agent/session_host/mod.rs b/crates/openhuman-core/src/agent/session_host/mod.rs
index 733c9e1c81..b81ab8e380 100644
--- a/crates/openhuman-core/src/agent/session_host/mod.rs
+++ b/crates/openhuman-core/src/agent/session_host/mod.rs
@@ -36,7 +36,6 @@ pub use builder::{HostOnlyToolPolicy, HostTools, HostTurnTools, TurnContext};
mod builder;
mod codec;
mod driver;
-mod hooks;
// Tool surface for live voice sessions (`voice::live`).
#[cfg(feature = "voice")]
mod live_tools;
@@ -56,7 +55,9 @@ pub(crate) mod turn_checkpoint;
mod types;
pub use codec::OpenHumanTranscriptCodec;
-pub use hooks::OpenHumanSessionHooks;
+pub type OpenHumanSessionHooks = tinyagents_runtime::FnSessionHooks<
+ crate::agent::tinyagents::host::OpenHumanRunContext,
+>;
pub use types::{OpenHumanSessionHost, SessionHostBuilder, TurnOverrides};
// Re-export the duplicate-tool-spec guard for sibling harness modules