Skip to content

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

feat(agent): add session host builder for host agents

Steven Enamakel4c09dadf6dad
diff --git a/crates/openhuman-core/src/agent/host_agents.rs b/crates/openhuman-core/src/agent/host_agents.rs
index 51a6ae38a6..4611221508 100644
--- a/crates/openhuman-core/src/agent/host_agents.rs
+++ b/crates/openhuman-core/src/agent/host_agents.rs
@@ -70,13 +70,12 @@ impl HostAgent {
host_tools = self.host_tools.is_some(),
"[host_agents] building session for host agent"
);
+ let dispatch = DispatchCtx::new(Arc::clone(&self.context));
let mut session = CoreContext::sync_scope(Arc::clone(&self.context), || {
- if crate::profiles::capabilities::ProfileCapabilities::in_ctx(&DispatchCtx::new(
- Arc::clone(&self.context),
- ))
- .is_some()
+ if crate::profiles::capabilities::ProfileCapabilities::in_ctx(&dispatch).is_some()
{
return OpenHumanSessionHost::from_config_host_only(
+ &dispatch,
config,
&self.definition,
self.host_tools.as_ref(),
@@ -85,12 +84,13 @@ impl HostAgent {
}
match &self.host_tools {
Some(host) => OpenHumanSessionHost::from_config_with_host_tools(
+ &dispatch,
config,
&self.definition,
host,
session_id,
),
- None => OpenHumanSessionHost::from_config_with_definition(config, &self.definition),
+ None => OpenHumanSessionHost::from_config_with_definition(&dispatch, config, &self.definition),
}
})?;
// The agent's own callbacks ride the session explicitly: its tool and
diff --git a/crates/openhuman-core/src/agent/session_host/builder/factory.rs b/crates/openhuman-core/src/agent/session_host/builder/factory.rs
index 38e841057f..232ab6b581 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/factory.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/factory.rs
@@ -41,8 +41,8 @@ impl OpenHumanSessionHost {
/// time (for example the Tauri web chat path, which routes to the
/// welcome agent pre-onboarding) should call
/// [`OpenHumanSessionHost::from_config_for_agent`] directly.
- pub fn from_config(config: &Config) -> Result<Self> {
- Self::from_config_for_agent(config, "orchestrator")
+ pub fn from_config(ctx: &DispatchCtx, config: &Config) -> Result<Self> {
+ Self::from_config_for_agent(ctx, config, "orchestrator")
}
/// Constructs an `OpenHumanSessionHost` instance scoped to a specific agent
@@ -77,11 +77,14 @@ impl OpenHumanSessionHost {
///
/// The welcome agent uses this entry point when routed from the
/// Tauri web channel (see `channels::provider::web::build_session_agent`).
- pub fn from_config_for_agent(config: &Config, agent_id: &str) -> Result<Self> {
+ pub fn from_config_for_agent(
+ ctx: &DispatchCtx,
+ config: &Config,
+ agent_id: &str,
+ ) -> Result<Self> {
// A hosted profile never receives the ordinary config-derived belt.
- let ambient = DispatchCtx::ambient();
- if crate::profiles::capabilities::ProfileCapabilities::in_ctx(&ambient).is_some() {
- return Self::from_config_for_agent_session(config, agent_id, None);
+ if crate::profiles::capabilities::ProfileCapabilities::in_ctx(ctx).is_some() {
+ return Self::from_config_for_agent_session(ctx, config, agent_id, None);
}
let target_def = resolve_target_definition(config, agent_id)?;
@@ -108,7 +111,15 @@ impl OpenHumanSessionHost {
.unwrap_or(config.default_temperature)
);
- Self::build_session_agent_inner(config, agent_id, target_def.as_ref(), false, None, None)
+ Self::build_session_agent_inner(
+ ctx,
+ config,
+ agent_id,
+ target_def.as_ref(),
+ false,
+ None,
+ None,
+ )
}
/// Build a session agent from a definition the caller already holds,
@@ -122,6 +133,7 @@ impl OpenHumanSessionHost {
/// library host uses to run many independently specified agents on one
/// booted core.
pub fn from_config_with_definition(
+ ctx: &DispatchCtx,
config: &Config,
definition: &crate::agent::harness::host_definition::AgentDefinition,
) -> Result<Self> {
@@ -130,7 +142,15 @@ impl OpenHumanSessionHost {
definition.id,
definition.sandbox_mode,
);
- Self::build_session_agent_inner(config, &definition.id, Some(definition), false, None, None)
+ Self::build_session_agent_inner(
+ ctx,
+ config,
+ &definition.id,
+ Some(definition),
+ false,
+ None,
+ None,
+ )
}
/// Internal constructor that consumes the optionally-resolved agent
@@ -144,6 +164,7 @@ impl OpenHumanSessionHost {
// of the crate's public API.
#[allow(clippy::too_many_arguments)]
pub(crate) fn build_session_agent_inner(
+ ctx: &DispatchCtx,
config: &Config,
agent_id: &str,
target_def: Option<&crate::agent::harness::host_definition::AgentDefinition>,
@@ -389,7 +410,7 @@ impl OpenHumanSessionHost {
None => SystemPromptBuilder::with_defaults(),
};
let post_turn_hooks: Vec<Arc<dyn crate::agent::hooks::PostTurnHook>> =
- crate::agent::hooks::embedder_post_turn_hooks();
+ crate::agent::hooks::embedder_post_turn_hooks_in(ctx);
// Best-effort prewarm from the shared Composio cache. This avoids
// building the session with a knowingly stale `&[]` integration view
@@ -787,7 +808,7 @@ impl OpenHumanSessionHost {
super::host_only::session_policy(host_only, &tools, merged_host_tools.policy);
let withheld_tool_names = merged_host_tools.withheld;
let mut builder = OpenHumanSessionHost::builder()
- .crate_native_provider(provider_role, Arc::clone(&base_config))
+ .crate_native_provider(ctx, provider_role, Arc::clone(&base_config))
.tools(tools)
.synthesized_tools(delegation_tools)
.visible_tool_names(visible)
diff --git a/crates/openhuman-core/src/agent/session_host/builder/host_only.rs b/crates/openhuman-core/src/agent/session_host/builder/host_only.rs
index 1f916aa7e3..4b095d96a4 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/host_only.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/host_only.rs
@@ -129,6 +129,7 @@ impl OpenHumanSessionHost {
///
/// As [`OpenHumanSessionHost::from_config_with_definition`].
pub fn from_config_host_only(
+ ctx: &crate::core::runtime::DispatchCtx,
config: &Config,
definition: &AgentDefinition,
host: Option<&super::HostTools>,
@@ -136,6 +137,7 @@ impl OpenHumanSessionHost {
) -> Result<Self> {
let definition = host_only_definition(definition);
let mut agent = OpenHumanSessionHost::build_session_agent_inner(
+ ctx,
config,
&definition.id,
Some(&definition),
diff --git a/crates/openhuman-core/src/agent/session_host/builder/host_tools.rs b/crates/openhuman-core/src/agent/session_host/builder/host_tools.rs
index 2013b161f0..4c42e0e46c 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/host_tools.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/host_tools.rs
@@ -345,12 +345,14 @@ impl OpenHumanSessionHost {
/// `session_id` is the conversation this turn runs in, reaching the
/// factory as [`TurnContext::session_id`]; `None` when none is named yet.
pub fn from_config_with_host_tools(
+ ctx: &crate::core::runtime::DispatchCtx,
config: &Config,
definition: &crate::agent::harness::host_definition::AgentDefinition,
host: &super::HostTools,
session_id: Option<&str>,
) -> Result<Self> {
OpenHumanSessionHost::build_session_agent_inner(
+ ctx,
config,
&definition.id,
Some(definition),
diff --git a/crates/openhuman-core/src/agent/session_host/builder/profile_factory.rs b/crates/openhuman-core/src/agent/session_host/builder/profile_factory.rs
index 61e66a1b74..2f9860b534 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/profile_factory.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/profile_factory.rs
@@ -12,19 +12,20 @@ impl OpenHumanSessionHost {
/// # Errors
/// As [`Self::from_config_for_agent`], or when a profile has no definition.
pub fn from_config_for_agent_session(
+ ctx: &DispatchCtx,
config: &Config,
agent_id: &str,
session_id: Option<&str>,
) -> anyhow::Result<Self> {
if let Some(capabilities) =
- crate::profiles::capabilities::ProfileCapabilities::in_ctx(&DispatchCtx::ambient())
+ crate::profiles::capabilities::ProfileCapabilities::in_ctx(ctx)
{
let mut definition = super::factory::resolve_target_definition(config, agent_id)?
.ok_or_else(|| anyhow::anyhow!("profile agent definition is unavailable"))?;
capabilities.apply_system_prompt(&mut definition);
let factory = capabilities.factory(config);
- return Self::from_config_host_only(config, &definition, Some(&factory), session_id);
+ return Self::from_config_host_only(ctx, config, &definition, Some(&factory), session_id);
}
- Self::from_config_for_agent(config, agent_id)
+ Self::from_config_for_agent(ctx, config, agent_id)
}
}
diff --git a/crates/openhuman-core/src/agent/session_host/builder/setters.rs b/crates/openhuman-core/src/agent/session_host/builder/setters.rs
index 29dc99e8b8..0a4c48a195 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/setters.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/setters.rs
@@ -79,11 +79,12 @@ impl SessionHostBuilder {
/// inject a mock they observe.
pub fn crate_native_provider(
mut self,
+ ctx: &crate::core::runtime::DispatchCtx,
role: impl Into<String>,
config: Arc<crate::config::Config>,
) -> Self {
self.turn_model_source = Some(crate::agent::tinyagents::TurnModelSource::new_crate_native(
- &crate::core::runtime::DispatchCtx::ambient(),
+ ctx,
role,
config,
));