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
refactor(runtime): extract runtime state into dedicated module
Steven Enamakel0b5992b9d975
diff --git a/crates/openhuman-tinyhumans/src/runtime.rs b/crates/openhuman-tinyhumans/src/runtime.rsindex d5272ef93c..8871f687b7 100644--- a/crates/openhuman-tinyhumans/src/runtime.rs+++ b/crates/openhuman-tinyhumans/src/runtime.rs@@ -1,18 +1,38 @@//! [`RuntimeBuilder`]: an [`openhuman_embed::RuntimeBuilder`] that comes up//! connected to the hosted TinyHumans backend.//!-//! Every method delegates to the embed builder unchanged; `build()` installs-//! the SDK transport, binds it to the runtime's context and boots. Use-//! [`RuntimeBuilder::into_embed`] to drop down to the plain builder.+//! This is the configuration path for a TinyHumans-connected core. On+//! [`build`](RuntimeBuilder::build) (or [`connect`](RuntimeBuilder::connect))+//! it sets the product identity and hands the embed builder everything the+//! connection contributes, through embed's own seam options:+//!+//! | Piece | Embed option |+//! |---|---|+//! | the SDK transport ([`SdkBackendTransport`](crate::SdkBackendTransport)) | `backend_transport` (also the process global) |+//! | the hosted RPC proxies ([`hosted::extension`](crate::hosted::extension)) | `controller_extension` |+//! | the Jev `tool_search` ranker (`jev` feature) | `tool_ranker` |+//!+//! Every other method forwards to the embed builder unchanged. Start from a+//! host preset ([`library`](RuntimeBuilder::library),+//! [`desktop`](RuntimeBuilder::desktop), [`cli`](RuntimeBuilder::cli),+//! [`tui`](RuntimeBuilder::tui)) or [`from_embed`](RuntimeBuilder::from_embed);+//! [`into_embed`](RuntimeBuilder::into_embed) drops back to the plain,+//! unconnected builder. [`install`](crate::install) is the compatibility+//! entry for hosts that still boot the core themselves; both resolve the+//! connection through the same helper.+use std::path::PathBuf;use std::sync::Arc;+use openhuman_embed::seams::{+ ControllerExtension, PostTurnHook, SecurityPolicy, ServerLauncher, ToolHook, ToolRanker,+};use openhuman_embed::{- Access, ApiKey, DomainSet, HostKind, Provider, Runtime, RuntimeConfig, ServiceSet, Session,- ToolGroups, Workspace,+ Access, ApiKey, ConfigSource, DomainSet, HostKind, Provider, Runtime, RuntimeConfig,+ ServiceSet, Session, SessionStoreProvider, TokenSource, ToolGroups, Workspace,};-use crate::install::{install, InstallError, InstallOptions};+use crate::install::{wiring, InstallError, InstallOptions};/// Build error: either the backend transport or the embed runtime failed.#[derive(Debug, thiserror::Error)]@@ -32,6 +52,17 @@ pub struct RuntimeBuilder {install: InstallOptions,}+/// Forward a by-value embed builder method unchanged.+macro_rules! forward {+ ($(#[$doc:meta])* $name:ident($($arg:ident: $ty:ty),*)) => {+ $(#[$doc])*+ pub fn $name(mut self, $($arg: $ty),*) -> Self {+ self.inner = self.inner.$name($($arg),*);+ self+ }+ };+}+impl RuntimeBuilder {/// The embed defaults plus a TinyHumans backend connection on build.pub fn new() -> Self {@@ -46,7 +77,28 @@ impl RuntimeBuilder {}}- /// The `x-sdk-name` this runtime reports to the backend.+ /// The library preset, connected; see [`openhuman_embed::RuntimeBuilder::library`].+ pub fn library() -> Self {+ Self::from_embed(openhuman_embed::RuntimeBuilder::library())+ }++ /// The desktop shell preset, connected; see [`openhuman_embed::RuntimeBuilder::desktop`].+ pub fn desktop() -> Self {+ Self::from_embed(openhuman_embed::RuntimeBuilder::desktop())+ }++ /// The CLI server preset, connected; see [`openhuman_embed::RuntimeBuilder::cli`].+ pub fn cli() -> Self {+ Self::from_embed(openhuman_embed::RuntimeBuilder::cli())+ }++ /// The terminal UI preset, connected; see [`openhuman_embed::RuntimeBuilder::tui`].+ pub fn tui() -> Self {+ Self::from_embed(openhuman_embed::RuntimeBuilder::tui())+ }++ /// The `x-sdk-name` this runtime reports to the backend. Set process-wide+ /// on build, before the transport captures its attribution headers.pub fn product_identity(mut self, identity: crate::backend::ProductIdentity) -> Self {self.install.product_identity = Some(identity);self@@ -59,12 +111,100 @@ impl RuntimeBuilder {self}- /// See [`openhuman_embed::RuntimeBuilder::workspace`].- pub fn workspace(mut self, workspace: Workspace) -> Self {- self.inner = self.inner.workspace(workspace);+ /// Whether the Jev `tool_search` ranker is installed (default `true`;+ /// needs the `jev` feature). Off, the harness ranks with BM25 alone+ /// unless [`tool_ranker`](Self::tool_ranker) supplies another.+ pub fn jev_ranker(mut self, enabled: bool) -> Self {+ self.install.tool_ranker = enabled;self}+ /// A `tool_search` ranker of the host's own, in place of the Jev ranker.+ /// See [`openhuman_embed::RuntimeBuilder::tool_ranker`].+ pub fn tool_ranker(mut self, ranker: Arc<dyn ToolRanker>) -> Self {+ self.install.tool_ranker = false;+ self.inner = self.inner.tool_ranker(ranker);+ self+ }++ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::workspace`].+ workspace(workspace: Workspace)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::config_source`].+ config_source(source: ConfigSource)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::provider`].+ provider(provider: Provider)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::access`].+ access(access: Access)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::services`].+ services(services: ServiceSet)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::domains`].+ domains(domains: DomainSet)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::tool_groups`].+ tool_groups(tool_groups: ToolGroups)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::host_kind`].+ host_kind(host_kind: HostKind)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::token`].+ token(token: TokenSource)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::listen_port`].+ listen_port(port: u16)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::session`].+ session(session: Session)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::config`].+ config(config: RuntimeConfig)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::session_store`].+ session_store(provider: Arc<dyn SessionStoreProvider>)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::memory_engine`].+ memory_engine(engine: Arc<dyn openhuman_embed::memory::api::MemoryEngine>)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::controller_extension`].+ /// The hosted proxies are added on build in addition to these.+ controller_extension(extension: ControllerExtension)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::post_turn_hook`].+ post_turn_hook(hook: Arc<dyn PostTurnHook>)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::tool_hook`].+ tool_hook(hook: Arc<dyn ToolHook>)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::server_launcher`].+ server_launcher(launcher: ServerLauncher)+ );+ forward!(+ /// See [`openhuman_embed::RuntimeBuilder::live_policy`].+ live_policy(policy: Arc<SecurityPolicy>)+ );+/// See [`openhuman_embed::RuntimeBuilder::api_key`].pub fn api_key(mut self, key: impl Into<ApiKey>) -> Self {self.inner = self.inner.api_key(key);@@ -77,51 +217,27 @@ impl RuntimeBuilder {self}- /// See [`openhuman_embed::RuntimeBuilder::provider`].- pub fn provider(mut self, provider: Provider) -> Self {- self.inner = self.inner.provider(provider);+ /// See [`openhuman_embed::RuntimeBuilder::workspace_dir`].+ pub fn workspace_dir(mut self, dir: impl Into<PathBuf>) -> Self {+ self.inner = self.inner.workspace_dir(dir);self}- /// See [`openhuman_embed::RuntimeBuilder::access`].- pub fn access(mut self, access: Access) -> Self {- self.inner = self.inner.access(access);+ /// See [`openhuman_embed::RuntimeBuilder::action_dir`].+ pub fn action_dir(mut self, dir: impl Into<PathBuf>) -> Self {+ self.inner = self.inner.action_dir(dir);self}- /// See [`openhuman_embed::RuntimeBuilder::services`].- pub fn services(mut self, services: ServiceSet) -> Self {- self.inner = self.inner.services(services);+ /// See [`openhuman_embed::RuntimeBuilder::listen`].+ pub fn listen(mut self, host: impl Into<String>, port: u16) -> Self {+ self.inner = self.inner.listen(host, port);self}- /// See [`openhuman_embed::RuntimeBuilder::domains`].- pub fn domains(mut self, domains: DomainSet) -> Self {- self.inner = self.inner.domains(domains);- self- }-- /// See [`openhuman_embed::RuntimeBuilder::tool_groups`].- pub fn tool_groups(mut self, tool_groups: ToolGroups) -> Self {- self.inner = self.inner.tool_groups(tool_groups);- self- }-- /// See [`openhuman_embed::RuntimeBuilder::host_kind`].- pub fn host_kind(mut self, host_kind: HostKind) -> Self {- self.inner = self.inner.host_kind(host_kind);- self- }-- /// See [`openhuman_embed::RuntimeBuilder::session`].- pub fn session(mut self, session: Session) -> Self {- self.inner = self.inner.session(session);- self- }-- /// See [`openhuman_embed::RuntimeBuilder::config`].- pub fn config(mut self, config: RuntimeConfig) -> Self {- self.inner = self.inner.config(config);+ /// See [`openhuman_embed::RuntimeBuilder::listen_host`].+ pub fn listen_host(mut self, host: impl Into<String>) -> Self {+ self.inner = self.inner.listen_host(host);self}@@ -131,11 +247,39 @@ impl RuntimeBuilder {self.inner}- /// Install the TinyHumans transport, bind it and boot the runtime.+ /// The embed builder with the TinyHumans connection applied: product+ /// identity set, the SDK transport installed as the process global and+ /// bound with `backend_transport`, the hosted proxies added as a+ /// controller extension and the Jev ranker as the tool ranker. For entry+ /// points that take an embed builder rather than this one.+ pub fn connect(self) -> Result<openhuman_embed::RuntimeBuilder, InstallError> {+ let wiring = wiring(&self.install)?;+ log::debug!("[tinyhumans] connecting embed builder: {wiring:?}");+ let transport: Arc<dyn openhuman_embed::BackendTransport> = wiring.transport;+ let mut inner = self.inner.backend_transport(transport);+ if let Some(extension) = wiring.controllers {+ inner = inner.controller_extension(extension);+ }+ if let Some(ranker) = wiring.ranker {+ inner = inner.tool_ranker(ranker);+ }+ Ok(inner)+ }++ /// Connect, then boot the runtime.pub async fn build(self) -> Result<Runtime, RuntimeError> {- let transport = install(self.install)?;- let transport: Arc<dyn openhuman_embed::BackendTransport> = transport;+ let inner = self.connect()?;log::debug!("[tinyhumans] building embed runtime with backend transport");- Ok(self.inner.backend_transport(transport).build().await?)+ Ok(inner.build().await?)+ }++ /// Connect, then run the core's command-line dispatcher on `args`; see+ /// [`openhuman_embed::RuntimeBuilder::run_from_args`].+ pub fn run_from_args(self, args: &[String]) -> anyhow::Result<()> {+ self.connect()?.run_from_args(args)}}++#[cfg(test)]+#[path = "runtime_tests.rs"]+mod tests;