The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
chore: files changed crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs,crates/op
Steven Enamakelb83b0ce7397c
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rsindex a49754e1ad..2c856dd9de 100644--- a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs+++ b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs@@ -1,8 +1,45 @@//! Channel turns answered by a host-registered agent.+//!+//! `config.agent.channel_agents` binds a channel to an agent id+//! (`telegram = "teeny-chat"`). A message on a bound channel is resolved+//! through the host agent resolver ([`crate::agent::host_agents`]) and run as+//! that agent -- its definition, its host tools, its context -- instead of+//! going over the bus to the orchestrator. Everything else about the channel+//! stays: per-chat history, typing, drafts, the reply back to the chat.+//!+//! A binding the resolver cannot satisfy (the embedder has not created the+//! agent yet, or dropped it) is **refused**, with a short reply to the chat+//! and an error log. It is never widened to the orchestrator: the binding+//! exists because the channel is meant to reach one particular agent, and+//! the orchestrator holds the operator's full tool belt.+//!+//! The turn runs under the channel's `ExternalChannel` origin, passed down as+//! a parameter, and [`posture`] caps what the agent may do on it.+use std::sync::Arc;++use crate::agent::host_agents::HostAgent;+use crate::channels::context::ChannelRuntimeContext;+use crate::channels::traits;+use crate::channels::{ChannelSendExt, SendMessage};use crate::config::Config;+use crate::core::bus::BUS;+use crate::core::events::DomainEvent;pub(crate) mod posture;+mod turn;++pub(crate) use turn::{run_host_agent_turn, HostChannelTurn};++/// What a channel's binding resolves to for one message.+pub(crate) enum HostRoute {+ /// No binding: the orchestrator answers, as before.+ Unbound,+ /// The bound agent, resolved.+ Agent(Box<HostAgent>),+ /// Bound to an agent id nobody answers for.+ Missing(String),+}/// The agent `channel` is bound to in `config.agent.channel_agents`, if any.pub(crate) fn bound_agent_id<'a>(config: &'a Config, channel: &str) -> Option<&'a str> {@@ -14,6 +51,74 @@ pub(crate) fn bound_agent_id<'a>(config: &'a Config, channel: &str) -> Option<&'.filter(|id| !id.is_empty())}+/// Resolve `channel`'s binding against the installed host agents.+pub(crate) fn route(ctx: &ChannelRuntimeContext, channel: &str) -> HostRoute {+ let Some(agent_id) = ctx+ .config+ .as_deref()+ .and_then(|config| bound_agent_id(config, channel))+ else {+ return HostRoute::Unbound;+ };+ match crate::agent::host_agents::resolve(agent_id) {+ Some(agent) => {+ tracing::debug!(+ channel,+ agent_id,+ "[channels::dispatch::host_agent] channel bound to host agent"+ );+ HostRoute::Agent(Box::new(agent))+ }+ None => {+ tracing::error!(+ channel,+ agent_id,+ "[channels::dispatch::host_agent] channel is bound to an agent no host has \+ registered; refusing the message instead of routing it to the orchestrator"+ );+ HostRoute::Missing(agent_id.to_string())+ }+ }+}++/// The reply a bound channel gets when its agent is missing.+pub(crate) const AGENT_UNAVAILABLE_REPLY: &str =+ "⚠️ This chat's assistant is not available right now. Please try again later.";++/// Answer `msg` with [`AGENT_UNAVAILABLE_REPLY`] and record the refusal.+pub(crate) async fn refuse_missing_agent(+ ctx: &ChannelRuntimeContext,+ msg: &traits::ChannelMessage,+ target_channel: Option<&Arc<dyn crate::channels::Channel>>,+ agent_id: &str,+) {+ if let Some(channel) = target_channel {+ let reply = SendMessage::new(AGENT_UNAVAILABLE_REPLY, &msg.reply_target)+ .in_thread(msg.thread_ts.clone());+ if let Err(error) = channel.send_with_outbound_intent(&reply).await {+ tracing::warn!(+ channel = %msg.channel,+ %error,+ "[channels::dispatch::host_agent] failed to send the unavailable reply"+ );+ }+ }+ BUS.publish(DomainEvent::ChannelMessageProcessed {+ channel: msg.channel.clone(),+ message_id: msg.id.clone(),+ sender: msg.sender.clone(),+ reply_target: msg.reply_target.clone(),+ content: msg.content.clone(),+ thread_ts: msg.thread_ts.clone(),+ response: AGENT_UNAVAILABLE_REPLY.to_string(),+ provider: format!("agent:{agent_id}"),+ model: String::new(),+ elapsed_ms: 0,+ success: false,+ workspace_dir: ctx.workspace_dir.as_ref().clone(),+ });+}+#[cfg(test)]#[path = "mod_tests.rs"]mod tests;diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rs b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rsnew file mode 100644index 0000000000..55338b6b60--- /dev/null+++ b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rs@@ -0,0 +1,83 @@+//! Running one channel turn as a host agent.++use tokio::sync::mpsc;++use super::posture;+use crate::agent::bus::AgentTurnResponse;+use crate::agent::host_agents::HostAgent;+use crate::agent::progress::AgentProgress;+use crate::agent::turn_origin::AgentTurnOrigin;++/// One channel message, as a host agent's turn needs it.+pub(crate) struct HostChannelTurn {+ /// The channel the message came in on (`"telegram"`).+ pub(crate) channel: String,+ /// The chat's history key: the session the host belt is told about and+ /// the event session id.+ pub(crate) history_key: String,+ /// This chat's prior turns, `(role, content)`, oldest first. The channel+ /// owns them, as it does for the orchestrator, so the session is seeded+ /// from them rather than from a durable transcript.+ pub(crate) prior_turns: Vec<(String, String)>,+ /// The message the agent answers.+ pub(crate) message: String,+ /// Who asked: the channel's `ExternalChannel` origin.+ pub(crate) origin: AgentTurnOrigin,+ /// Live progress for the channel's draft updates.+ pub(crate) progress: Option<mpsc::Sender<AgentProgress>>,+}++/// Run `turn` as `host`: its definition and host tools, inside its context,+/// under `turn.origin` and the posture that origin calls for.+pub(crate) async fn run_host_agent_turn(+ mut host: HostAgent,+ turn: HostChannelTurn,+) -> anyhow::Result<AgentTurnResponse> {+ let HostChannelTurn {+ channel,+ history_key,+ prior_turns,+ message,+ origin,+ progress,+ } = turn;+ let mut config = host.config.clone();+ if let Some(ceiling) = posture::tool_ceiling(&origin) {+ posture::cap_channel(&mut config, &channel, ceiling);+ host.host_tools = host+ .host_tools+ .take()+ .map(|tools| posture::guard_host_tools(tools, ceiling));+ }+ let agent_id = host.definition.id.clone();+ let model = config.default_model.clone().unwrap_or_default();+ tracing::info!(+ channel = %channel,+ agent_id = %agent_id,+ prior_turns = prior_turns.len(),+ "[channels::dispatch::host_agent] running channel turn as host agent"+ );++ let mut session = host.session_host(&config, Some(&history_key))?;+ // The channel names the session, which also selects the channel's+ // permission ceiling in the session's tool policy.+ session.set_event_context(history_key, channel);+ // The chat's history is the channel's, so the session holds exactly that+ // and never resumes a transcript of its own.+ session.clear_history();+ session.seed_resume_from_messages(prior_turns, &message)?;+ session.set_next_turn_overrides(crate::agent::TurnOverrides {+ suppress_transcript_autoload: true,+ ..Default::default()+ });+ session.set_on_progress(progress);++ let text = host+ .scope(session.run_single_with_origin(&message, Some(origin)))+ .await?;+ Ok(AgentTurnResponse::with_resolved_route(+ text,+ format!("agent:{agent_id}"),+ model,+ ))+}