Skip to content

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

fix(runtime): handle missing JWT secret during installation

Steven Enamakelb1c16449ffaf
diff --git a/crates/openhuman-tinyhumans/src/install.rs b/crates/openhuman-tinyhumans/src/install.rs
new file mode 100644
index 0000000000..c3475ab3bd
--- /dev/null
+++ b/crates/openhuman-tinyhumans/src/install.rs
@@ -0,0 +1,91 @@
+//! Process-level installation: give a core booted by *any* host — the desktop
+//! shell, the TUI, the CLI, a test — its TinyHumans backend connection.
+//!
+//! Hosts that build the core through [`crate::RuntimeBuilder`] do not need
+//! this; it is for hosts that boot the core themselves
+//! (`run_server_embedded_with_ready`, `run_core_from_args`, `CoreBuilder`)
+//! and for test fixtures. Idempotent: calling it again re-installs an
+//! equivalent transport and is harmless.
+
+use std::sync::{Arc, Mutex, OnceLock};
+
+use openhuman_core::api::transport::{install_backend_transport, installed_backend_transport};
+use openhuman_core::api::{set_product_identity, ProductIdentity};
+
+use crate::transport::SdkBackendTransport;
+
+/// What [`install`] sets up.
+#[derive(Debug, Default, Clone)]
+pub struct InstallOptions {
+ /// The `x-sdk-name` this process reports on every backend request. `None`
+ /// keeps whatever identity is already set (the core's default is
+ /// `"openhuman"`). Set it here, before the transport is built, because the
+ /// transport captures the attribution headers once.
+ pub product_identity: Option<ProductIdentity>,
+}
+
+impl InstallOptions {
+ /// Report `identity` as this process's product on every backend request.
+ pub fn product_identity(mut self, identity: ProductIdentity) -> Self {
+ self.product_identity = Some(identity);
+ self
+ }
+}
+
+/// Why [`install`] could not set the process up.
+#[derive(Debug, thiserror::Error)]
+pub enum InstallError {
+ /// The SDK transport's HTTP client could not be built.
+ #[error("failed to build the TinyHumans backend transport: {0:#}")]
+ Transport(anyhow::Error),
+}
+
+static INSTALLED: OnceLock<Mutex<Option<Arc<SdkBackendTransport>>>> = OnceLock::new();
+
+/// Install the SDK-backed backend transport as the process-global transport
+/// (and optionally set the product identity first).
+///
+/// Returns the transport so a host that also builds the core through
+/// `CoreBuilder` can bind it there explicitly with
+/// `CoreBuilder::backend_transport`; binding is optional because the core
+/// resolves the process global when a context carries none.
+pub fn install(options: InstallOptions) -> Result<Arc<SdkBackendTransport>, InstallError> {
+ let slot = INSTALLED.get_or_init(|| Mutex::new(None));
+ let mut guard = slot.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
+
+ if let Some(identity) = options.product_identity.clone() {
+ log::debug!("[tinyhumans] install: product identity {}", identity.as_str());
+ set_product_identity(identity);
+ // A new identity means new attribution headers; rebuild below.
+ *guard = None;
+ }
+
+ if let Some(existing) = guard.as_ref() {
+ // Re-install into the core slot in case something cleared it (tests).
+ if installed_backend_transport().is_none() {
+ install_backend_transport(existing.clone());
+ }
+ log::trace!("[tinyhumans] install: already installed");
+ return Ok(existing.clone());
+ }
+
+ let transport = Arc::new(SdkBackendTransport::new().map_err(InstallError::Transport)?);
+ install_backend_transport(transport.clone());
+ *guard = Some(transport.clone());
+ log::info!("[tinyhumans] install: backend transport installed");
+ Ok(transport)
+}
+
+/// Whether [`install`] has run in this process (and its transport is still
+/// the core's global one).
+pub fn is_installed() -> bool {
+ INSTALLED
+ .get()
+ .and_then(|slot| {
+ slot.lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .as_ref()
+ .map(|_| installed_backend_transport().is_some())
+ })
+ .unwrap_or(false)
+}
diff --git a/crates/openhuman-tinyhumans/src/jwt.rs b/crates/openhuman-tinyhumans/src/jwt.rs
new file mode 100644
index 0000000000..5653381ebc
--- /dev/null
+++ b/crates/openhuman-tinyhumans/src/jwt.rs
@@ -0,0 +1,8 @@
+//! JWT helpers for TinyHumans session tokens, from the SDK.
+//!
+//! The core carries its own copy of these three functions in
+//! `openhuman_core::api::jwt` because it needs them with no backend
+//! dependency; this module exposes the SDK's originals for hosts that already
+//! depend on this crate.
+
+pub use tinyhumans_sdk::jwt::{bearer_authorization_value, decode_jwt_exp_unix, decode_jwt_payload};
diff --git a/crates/openhuman-tinyhumans/src/lib.rs b/crates/openhuman-tinyhumans/src/lib.rs
new file mode 100644
index 0000000000..1d63accc1b
--- /dev/null
+++ b/crates/openhuman-tinyhumans/src/lib.rs
@@ -0,0 +1,43 @@
+//! OpenHuman on the hosted TinyHumans backend.
+//!
+//! Layering, bottom up:
+//!
+//! - `openhuman-core` runs agents, memory, tools and RPC and knows the backend
+//! only through a port, [`BackendTransport`]. It has no SDK dependency and
+//! runs without any TinyHumans connection.
+//! - `openhuman-embed` is the library facade over the core.
+//! - **this crate** implements the port with the vendored `tinyhumans-sdk`
+//! ([`SdkBackendTransport`]), installs it into a process ([`install`]) and
+//! offers a [`RuntimeBuilder`] that boots an embed runtime already
+//! connected.
+//!
+//! ```no_run
+//! # async fn demo() -> anyhow::Result<()> {
+//! use openhuman_tinyhumans::{embed::Workspace, RuntimeBuilder};
+//!
+//! let runtime = RuntimeBuilder::new()
+//! .workspace(Workspace::Ephemeral)
+//! .api_key("th_...")
+//! .build()
+//! .await?;
+//! # let _ = runtime;
+//! # Ok(())
+//! # }
+//! ```
+//!
+//! Hosts that boot the core themselves (the desktop shell's
+//! `run_server_embedded_with_ready`, the CLI's `run_core_from_args`, a
+//! `CoreBuilder`) call [`install`] once before the first backend-touching
+//! dispatch instead.
+
+pub use openhuman_embed as embed;
+
+mod install;
+pub mod jwt;
+mod runtime;
+pub mod transport;
+
+pub use install::{install, is_installed, InstallError, InstallOptions};
+pub use openhuman_embed::{BackendRequest, BackendTransport, BackendTransportError, TransportProfile};
+pub use runtime::{RuntimeBuilder, RuntimeError};
+pub use transport::{map_sdk_error, SdkBackendTransport};
diff --git a/crates/openhuman-tinyhumans/src/runtime.rs b/crates/openhuman-tinyhumans/src/runtime.rs
new file mode 100644
index 0000000000..7d14f94b51
--- /dev/null
+++ b/crates/openhuman-tinyhumans/src/runtime.rs
@@ -0,0 +1,134 @@
+//! [`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.
+
+use std::sync::Arc;
+
+use openhuman_embed::{
+ Access, ApiKey, Config, DomainSet, HostKind, Provider, Runtime, ServiceSet, Session,
+ ToolGroups, Workspace,
+};
+
+use crate::install::{install, InstallError, InstallOptions};
+
+/// Build error: either the backend transport or the embed runtime failed.
+#[derive(Debug, thiserror::Error)]
+pub enum RuntimeError {
+ /// The TinyHumans transport could not be installed.
+ #[error(transparent)]
+ Install(#[from] InstallError),
+ /// The embed runtime failed to build.
+ #[error(transparent)]
+ Embed(#[from] openhuman_embed::RuntimeError),
+}
+
+/// See the module docs.
+#[derive(Default)]
+pub struct RuntimeBuilder {
+ inner: openhuman_embed::RuntimeBuilder,
+ install: InstallOptions,
+}
+
+impl RuntimeBuilder {
+ /// The embed defaults plus a TinyHumans backend connection on build.
+ pub fn new() -> Self {
+ Self::default()
+ }
+
+ /// Start from an already-configured embed builder.
+ pub fn from_embed(inner: openhuman_embed::RuntimeBuilder) -> Self {
+ Self {
+ inner,
+ install: InstallOptions::default(),
+ }
+ }
+
+ /// The `x-sdk-name` this runtime reports to the backend.
+ pub fn product_identity(mut self, identity: openhuman_embed::ProductIdentity) -> Self {
+ self.install.product_identity = Some(identity);
+ self
+ }
+
+ /// See [`openhuman_embed::RuntimeBuilder::workspace`].
+ pub fn workspace(mut self, workspace: Workspace) -> Self {
+ self.inner = self.inner.workspace(workspace);
+ self
+ }
+
+ /// See [`openhuman_embed::RuntimeBuilder::api_key`].
+ pub fn api_key(mut self, key: impl Into<ApiKey>) -> Self {
+ self.inner = self.inner.api_key(key);
+ self
+ }
+
+ /// See [`openhuman_embed::RuntimeBuilder::backend_url`].
+ pub fn backend_url(mut self, url: impl Into<String>) -> Self {
+ self.inner = self.inner.backend_url(url);
+ self
+ }
+
+ /// See [`openhuman_embed::RuntimeBuilder::provider`].
+ pub fn provider(mut self, provider: Provider) -> Self {
+ self.inner = self.inner.provider(provider);
+ self
+ }
+
+ /// See [`openhuman_embed::RuntimeBuilder::access`].
+ pub fn access(mut self, access: Access) -> Self {
+ self.inner = self.inner.access(access);
+ self
+ }
+
+ /// See [`openhuman_embed::RuntimeBuilder::services`].
+ pub fn services(mut self, services: ServiceSet) -> Self {
+ self.inner = self.inner.services(services);
+ 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: Config) -> Self {
+ self.inner = self.inner.config(config);
+ self
+ }
+
+ /// The plain embed builder, with everything set so far but no backend
+ /// connection.
+ pub fn into_embed(self) -> openhuman_embed::RuntimeBuilder {
+ self.inner
+ }
+
+ /// Install the TinyHumans transport, bind it and boot the runtime.
+ pub async fn build(self) -> Result<Runtime, RuntimeError> {
+ let transport = install(self.install)?;
+ let transport: Arc<dyn openhuman_embed::BackendTransport> = transport;
+ log::debug!("[tinyhumans] building embed runtime with backend transport");
+ Ok(self.inner.backend_transport(transport).build().await?)
+ }
+}