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 controllers module for agent chat runs

Steven Enamakel1ca1eba8759b
diff --git a/crates/openhuman-core/src/agent_runs/agent_chat/controllers.rs b/crates/openhuman-core/src/agent_runs/agent_chat/controllers.rs
new file mode 100644
index 0000000000..28ba45fd57
--- /dev/null
+++ b/crates/openhuman-core/src/agent_runs/agent_chat/controllers.rs
@@ -0,0 +1,145 @@
+use serde::Deserialize;
+use serde_json::{Map, Value};
+
+use crate::agent_runs::agent_chat::params::AgentChatParams;
+use crate::core::all::{ControllerFuture, RegisteredController};
+use crate::core::{ControllerSchema, FieldSchema, TypeSchema};
+
+#[derive(Debug, Deserialize)]
+struct InferenceAgentChatParams {
+ #[serde(flatten)]
+ common: AgentChatParams,
+ #[serde(default)]
+ agent_id: Option<String>,
+ #[serde(default)]
+ inference_url: Option<String>,
+ #[serde(default)]
+ api_key: Option<String>,
+ #[serde(default)]
+ inference_headers: Vec<(String, String)>,
+}
+
+pub(crate) fn schemas(function: &str) -> ControllerSchema {
+ let inputs = vec![
+ required_string("message", "User message."),
+ optional_string("model_override", "Optional model override."),
+ optional_f64("temperature", "Optional temperature override."),
+ optional_string("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; must be an existing directory."),
+ optional_string("inference_url", "Optional OpenAI-compatible endpoint for this call only. Requires api_key; never persisted."),
+ optional_string("api_key", "Bearer for inference_url. Scoped to this call and to that endpoint alone."),
+ FieldSchema {
+ name: "inference_headers",
+ ty: TypeSchema::Option(Box::new(TypeSchema::Array(Box::new(TypeSchema::Json)))),
+ comment: "Custom header name/value pairs for inference_url only; never persisted.",
+ required: false,
+ },
+ ];
+ match function {
+ "agent_chat" => ControllerSchema {
+ namespace: "inference",
+ function: "agent_chat",
+ description: "Run one-shot agent chat with optional model overrides.",
+ inputs,
+ outputs: vec![json_output("response", "Agent response payload with result, logs, hit_cap, and an optional checkpoint.")],
+ },
+ "agent_chat_simple" => ControllerSchema {
+ namespace: "inference",
+ function: "agent_chat_simple",
+ description: "Run one-shot lightweight provider chat.",
+ inputs: vec![
+ required_string("message", "User message."),
+ optional_string("model_override", "Optional model override."),
+ optional_f64("temperature", "Optional temperature override."),
+ optional_string("thread_id", "Optional backend thread id for cache grouping and inference logs."),
+ ],
+ outputs: vec![json_output("response", "Agent response payload.")],
+ },
+ _ => ControllerSchema {
+ namespace: "inference",
+ function: "unknown",
+ description: "Unknown local inference controller function.",
+ inputs: vec![],
+ outputs: vec![FieldSchema {
+ name: "error",
+ ty: TypeSchema::String,
+ comment: "Lookup error details.",
+ required: true,
+ }],
+ },
+ }
+}
+
+pub(crate) fn all_controller_schemas() -> Vec<ControllerSchema> {
+ vec![schemas("agent_chat"), schemas("agent_chat_simple")]
+}
+
+pub(crate) fn all_registered_controllers() -> Vec<RegisteredController> {
+ vec![
+ RegisteredController { schema: schemas("agent_chat"), handler: handle_agent_chat },
+ RegisteredController { schema: schemas("agent_chat_simple"), handler: handle_agent_chat_simple },
+ ]
+}
+
+fn handle_agent_chat(ctx: crate::core::runtime::DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
+ Box::pin(async move {
+ let params = deserialize_params::<InferenceAgentChatParams>(params)?;
+ let mut config = ctx.load_config().await?;
+ let target = match params.agent_id.as_deref().map(str::trim) {
+ Some(id) if !id.is_empty() => super::AgentChatTarget::AgentId(id),
+ _ => super::AgentChatTarget::Orchestrator,
+ };
+ super::agent_chat_reply_for(
+ &mut config,
+ target,
+ &params.common.message,
+ params.common.model_override,
+ params.common.temperature,
+ params.common.thread_id,
+ params.common.cwd,
+ crate::config::schema::EphemeralRoute::from_params(params.inference_url, params.api_key)
+ .map(|route| route.with_headers(params.inference_headers)),
+ super::AgentChatEnv::default(),
+ )
+ .await?
+ .into_rpc_json()
+ })
+}
+
+fn handle_agent_chat_simple(ctx: crate::core::runtime::DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
+ Box::pin(async move {
+ let params = deserialize_params::<AgentChatParams>(params)?;
+ let config = ctx.load_config().await?;
+ crate::inference::host_runtime::ops::agent_chat_simple(
+ &ctx,
+ &config,
+ &params.message,
+ params.model_override,
+ params.temperature,
+ params.thread_id,
+ )
+ .await?
+ .into_cli_compatible_json()
+ })
+}
+
+fn deserialize_params<T: serde::de::DeserializeOwned>(params: Map<String, Value>) -> Result<T, String> {
+ serde_json::from_value(Value::Object(params)).map_err(|e| format!("invalid params: {e}"))
+}
+
+fn required_string(name: &'static str, comment: &'static str) -> FieldSchema {
+ FieldSchema { name, ty: TypeSchema::String, comment, required: true }
+}
+
+fn optional_string(name: &'static str, comment: &'static str) -> FieldSchema {
+ FieldSchema { name, ty: TypeSchema::Option(Box::new(TypeSchema::String)), comment, required: false }
+}
+
+fn optional_f64(name: &'static str, comment: &'static str) -> FieldSchema {
+ FieldSchema { name, ty: TypeSchema::Option(Box::new(TypeSchema::F64)), comment, required: false }
+}
+
+fn json_output(name: &'static str, comment: &'static str) -> FieldSchema {
+ FieldSchema { name, ty: TypeSchema::Json, comment, required: true }
+}