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 agent_chat_for with explicit target and thread-correct resume

Steven Enamakelbdb7447cc435
diff --git a/crates/openhuman-core/src/inference/local/ops.rs b/crates/openhuman-core/src/inference/local/ops.rs
index 7e4c39f203..3005d426e9 100644
--- a/crates/openhuman-core/src/inference/local/ops.rs
+++ b/crates/openhuman-core/src/inference/local/ops.rs
@@ -14,7 +14,7 @@ mod reactions;
mod runtime_ops;
mod turn_guards;
-pub use agent_chat::{agent_chat, agent_chat_simple};
+pub use agent_chat::{agent_chat, agent_chat_for, agent_chat_simple, AgentChatTarget};
pub use chat::{local_ai_chat, LocalAiChatMessage};
pub use reactions::{local_ai_should_react, ReactionDecision};
pub use runtime_ops::{
diff --git a/crates/openhuman-core/src/inference/local/ops/agent_chat.rs b/crates/openhuman-core/src/inference/local/ops/agent_chat.rs
index a8bde330da..0d3be70b02 100644
--- a/crates/openhuman-core/src/inference/local/ops/agent_chat.rs
+++ b/crates/openhuman-core/src/inference/local/ops/agent_chat.rs
@@ -57,6 +57,81 @@ pub async fn agent_chat(
thread_id: Option<String>,
cwd: Option<String>,
route: Option<crate::config::schema::EphemeralRoute>,
+) -> Result<RpcOutcome<String>, String> {
+ agent_chat_for(
+ config,
+ AgentChatTarget::Orchestrator,
+ message,
+ model_override,
+ temperature,
+ thread_id,
+ cwd,
+ route,
+ )
+ .await
+}
+
+/// Which session [`agent_chat_for`] builds the turn on.
+#[derive(Debug, Clone, Copy)]
+pub enum AgentChatTarget<'a> {
+ /// The orchestrator — [`Agent::from_config`], today's `agent_chat`.
+ Orchestrator,
+ /// Resolve `id` the way every other id-keyed entry point does: the
+ /// process registry first, then `config.agent_registry.entries`.
+ AgentId(&'a str),
+ /// A definition the caller already holds; nothing is resolved by id. The
+ /// entry point for a library host running its own per-agent specs — see
+ /// [`Agent::from_config_with_definition`].
+ Definition {
+ definition: &'a crate::agent::harness::definition::AgentDefinition,
+ profile: Option<&'a crate::agent::profiles::AgentProfile>,
+ profile_prompt_suffix: Option<&'a str>,
+ },
+}
+
+fn build_turn_agent(config: &Config, target: &AgentChatTarget<'_>) -> Result<Agent, String> {
+ match target {
+ AgentChatTarget::Orchestrator => Agent::from_config(config),
+ AgentChatTarget::AgentId(id) => {
+ log::debug!("[inference] agent_chat building agent_id={id}");
+ Agent::from_config_for_agent(config, id)
+ }
+ AgentChatTarget::Definition {
+ definition,
+ profile,
+ profile_prompt_suffix,
+ } => Agent::from_config_with_definition(
+ config,
+ definition,
+ *profile,
+ profile_prompt_suffix.map(str::to_string),
+ ),
+ }
+ .map_err(|e| e.to_string())
+}
+
+/// [`agent_chat`] on an explicit [`AgentChatTarget`].
+///
+/// Two differences from the historical `agent_chat` beyond the target:
+///
+/// * A non-empty `thread_id` resumes **that thread's** transcript
+/// (`Agent::seed_resume_from_thread_transcript`). When the thread has no
+/// transcript yet, auto-resume is suppressed for the turn so a fresh thread
+/// never splices in the agent's newest transcript from some other thread —
+/// `Agent::turn` resolves the latest transcript per agent *name*, not per
+/// thread.
+/// * The agent is built by `target`, so a library host can run one booted
+/// core with many independently defined agents.
+#[allow(clippy::too_many_arguments)]
+pub async fn agent_chat_for(
+ config: &mut Config,
+ target: AgentChatTarget<'_>,
+ message: &str,
+ model_override: Option<String>,
+ temperature: Option<f64>,
+ thread_id: Option<String>,
+ cwd: Option<String>,
+ route: Option<crate::config::schema::EphemeralRoute>,
) -> Result<RpcOutcome<String>, String> {
enforce_user_prompt_or_reject(message, "local_ai.ops.agent_chat")?;
@@ -93,7 +168,7 @@ pub async fn agent_chat(
"[inference] agent_chat rooting turn tools at cwd={}",
root.display()
);
- let mut agent = Agent::from_config(&scoped).map_err(|e| e.to_string())?;
+ let mut agent = build_turn_agent(&scoped, &target)?;
// Also thread it as the turn's workspace descriptor so acting tools
// that read `ToolExecutionContext::workspace` (shell) resolve their
// default cwd here, and so spawned sub-agents inherit the same root.
@@ -102,8 +177,28 @@ pub async fn agent_chat(
));
agent
}
- None => Agent::from_config(config).map_err(|e| e.to_string())?,
+ None => build_turn_agent(config, &target)?,
};
+ // Thread-correct resume. `Agent::turn` would otherwise auto-load the
+ // newest transcript for the agent *name*, which is another thread's
+ // history whenever the same agent serves several threads (every library
+ // host does exactly that). Seed from this thread's transcript when it has
+ // one; when it has none, keep the turn from falling back to that autoload.
+ if let Some(id) = thread_id
+ .as_deref()
+ .map(str::trim)
+ .filter(|id| !id.is_empty())
+ {
+ if agent.seed_resume_from_thread_transcript(id) {
+ log::debug!("[inference] agent_chat resumed thread transcript thread_id={id}");
+ } else {
+ log::debug!("[inference] agent_chat fresh thread thread_id={id}; autoload suppressed");
+ agent.set_next_turn_overrides(crate::agent::harness::session::TurnOverrides {
+ suppress_transcript_autoload: true,
+ ..Default::default()
+ });
+ }
+ }
// Live progress for in-process embedders. `Agent::from_config` never
// attaches a sink itself, so there is nothing to clobber here; callers that
// set one explicitly (web chat, platform socket, flows, skills) hold their
diff --git a/crates/openhuman-core/src/inference/local/schemas.rs b/crates/openhuman-core/src/inference/local/schemas.rs
index d9b2e68e1a..aab16d7e40 100644
--- a/crates/openhuman-core/src/inference/local/schemas.rs
+++ b/crates/openhuman-core/src/inference/local/schemas.rs
@@ -25,6 +25,11 @@ struct AgentChatParams {
model_override: Option<String>,
temperature: Option<f64>,
thread_id: Option<String>,
+ /// Which agent definition to run the turn as. Absent runs the
+ /// orchestrator, as before. Resolved through the definition registry and
+ /// then `config.agent_registry.entries`; an unknown id is an error.
+ #[serde(default)]
+ agent_id: Option<String>,
/// Optional per-turn working directory for the agent's filesystem / shell
/// tools. Absent or empty keeps the configured `action_dir`. Ignored by the
/// `*_simple` variant, which runs a bare provider call with no tools.
@@ -152,6 +157,11 @@ pub fn schemas(function: &str) -> ControllerSchema {
"thread_id",
"Optional backend thread id for cache grouping and inference logs.",
),
+ optional_string(
+ "agent_id",
+ "Optional agent definition id to run the turn as. Omit for the \
+ orchestrator.",
+ ),
optional_string(
"cwd",
"Optional working directory for this turn: the agent's file and shell \
@@ -288,9 +298,14 @@ fn handle_agent_chat(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let p = deserialize_params::<AgentChatParams>(params)?;
let mut config = config_rpc::load_config_with_timeout().await?;
+ let target = match p.agent_id.as_deref().map(str::trim) {
+ Some(id) if !id.is_empty() => crate::inference::local::ops::AgentChatTarget::AgentId(id),
+ _ => crate::inference::local::ops::AgentChatTarget::Orchestrator,
+ };
to_json(
- crate::inference::local::ops::agent_chat(
+ crate::inference::local::ops::agent_chat_for(
&mut config,
+ target,
&p.message,
p.model_override,
p.temperature,