Skip to content

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

refactor(rpc): extract shim helpers into dedicated module

Steven Enamakel9f339c244b4f
diff --git a/crates/openhuman-rpc/src/server/shims.rs b/crates/openhuman-rpc/src/server/shims.rs
index 1c20a728b9..8d9be6bd75 100644
--- a/crates/openhuman-rpc/src/server/shims.rs
+++ b/crates/openhuman-rpc/src/server/shims.rs
@@ -1,9 +1,25 @@
-//! Server entry points kept for the hosts that predate
-//! `CoreBuilder`.
+//! Server entry points kept for the hosts that predate the shared host boot
+//! ([`crate::host`]).
//!
-//! Each `run_server*` function is a thin shim that composes a `CoreBuilder`
-//! and calls [`serve`](super::serve::serve).
+//! Each `run_server*` function is a thin shim: it starts from the embed host
+//! preset that matches the caller ([`RuntimeBuilder::desktop`] for the
+//! embedded desktop core, [`RuntimeBuilder::cli`] for a standalone server),
+//! applies the caller's services, bearer and listener, then builds the
+//! runtime and [`serve`](super::serve::serve)s it.
+//!
+//! The presets carry exactly what these shims used to hand `CoreBuilder` by
+//! hand: every domain family, the `embedded_core` → `HostKind` mapping
+//! (embedded == Tauri shell; standalone splits CLI / Docker via
+//! `HostKind::detect_standalone`), the `OPENHUMAN_E2E` tool-group switch, and
+//! no supplied config (the core discovers the operator's install).
+//!
+//! None of these connect the TinyHumans backend: the hosts calling them
+//! install it themselves (`openhuman_tinyhumans::install`). [`crate::host`]
+//! is the entry that connects it.
+
+use std::sync::Arc;
+use openhuman_tinyhumans::embed::{RuntimeBuilder, ServiceSet, TokenSource};
use tokio_util::sync::CancellationToken;
use super::serve::EmbeddedReadySignal;
@@ -38,7 +54,7 @@ pub async fn run_server(
/// Runs the request/response-only HTTP API without detached background jobs.
pub async fn run_server_headless(host: Option<&str>, port: Option<u16>) -> anyhow::Result<()> {
- let services = crate::core_host::core::runtime::ServiceSet::headless_api();
+ let services = ServiceSet::headless_api();
run_server_with_services(host, port, services, false, None, None, None).await
}
@@ -75,7 +91,7 @@ pub async fn run_server_embedded_with_ready(
socketio_enabled: bool,
shutdown_token: CancellationToken,
ready_tx: tokio::sync::oneshot::Sender<EmbeddedReadySignal>,
- rpc_token: Option<std::sync::Arc<String>>,
+ rpc_token: Option<Arc<String>>,
) -> anyhow::Result<()> {
run_server_inner(
host,
@@ -97,9 +113,9 @@ async fn run_server_inner(
embedded_core: bool,
shutdown_token: Option<CancellationToken>,
ready_tx: Option<tokio::sync::oneshot::Sender<EmbeddedReadySignal>>,
- rpc_token: Option<std::sync::Arc<String>>,
+ rpc_token: Option<Arc<String>>,
) -> anyhow::Result<()> {
- let mut services = crate::core_host::core::runtime::ServiceSet::desktop();
+ let mut services = ServiceSet::desktop();
services.socketio = socketio_enabled;
run_server_with_services(
host,
@@ -116,57 +132,86 @@ async fn run_server_inner(
async fn run_server_with_services(
host: Option<&str>,
port: Option<u16>,
- services: crate::core_host::core::runtime::ServiceSet,
+ services: ServiceSet,
embedded_core: bool,
shutdown_token: Option<CancellationToken>,
ready_tx: Option<tokio::sync::oneshot::Sender<EmbeddedReadySignal>>,
- rpc_token: Option<std::sync::Arc<String>>,
+ rpc_token: Option<Arc<String>>,
) -> anyhow::Result<()> {
- // `run_server_inner` is now a thin shim over the CoreBuilder/CoreRuntime
- // composition (Phase 1). It reproduces the legacy behavior exactly: all
- // background services on (`ServiceSet::desktop`), Socket.IO per the caller
- // flag, and the legacy `embedded_core` → `HostKind` mapping (embedded ==
- // Tauri shell; standalone splits CLI / Docker via `detect_standalone`).
- // See the pluggable-core work (`core::runtime`).
- let host_kind = if embedded_core {
- crate::core_host::core::types::HostKind::TauriShell
+ let preset = if embedded_core {
+ RuntimeBuilder::desktop()
} else {
- crate::core_host::core::types::HostKind::detect_standalone()
+ RuntimeBuilder::cli()
};
- let token = match rpc_token {
- Some(token) => crate::core_host::core::runtime::TokenSource::Fixed(token),
- None => crate::core_host::core::runtime::TokenSource::EnvOrFile,
- };
- let mut builder = crate::core_host::core::runtime::CoreBuilder::new(host_kind)
- .token(token)
- .services(services);
- // The browser E2E harness scripts direct tool calls through its mock model.
- // Keep production's fail-closed packed default, while making those calls
- // visible in the deterministic test core without changing library hosts.
- builder = apply_e2e_tool_groups(builder);
+ let builder = server_builder(preset, services, host, port, rpc_token);
+ build_and_serve(builder, ready_tx, shutdown_token).await
+}
+
+/// Apply a server's services, bearer and listener to a host preset.
+///
+/// `rpc_token` is the in-memory bearer handoff ([`TokenSource::Fixed`]);
+/// `None` keeps the preset's env-or-file token. An unset host or port is left
+/// to [`serve`](super::serve::serve), which falls back to
+/// `OPENHUMAN_CORE_HOST` / `OPENHUMAN_CORE_PORT` and then the defaults.
+pub(crate) fn server_builder(
+ preset: RuntimeBuilder,
+ services: ServiceSet,
+ host: Option<&str>,
+ port: Option<u16>,
+ rpc_token: Option<Arc<String>>,
+) -> RuntimeBuilder {
+ let mut builder = preset.services(services);
+ if let Some(token) = rpc_token {
+ builder = builder.token(TokenSource::Fixed(token));
+ }
if let Some(host) = host {
- builder = builder.host(host);
+ builder = builder.listen_host(host);
}
if let Some(port) = port {
- builder = builder.port(port);
+ builder = builder.listen_port(port);
}
+ builder
+}
+
+/// Build `builder`'s runtime and serve it until shutdown.
+///
+/// The on-disk session store is installed for the life of the process, as the
+/// shims always did, rather than through `RuntimeBuilder::session_store`
+/// (which restores the previous provider when the runtime drops). The desktop
+/// restarts its in-process server in place; a detached turn still writing
+/// across that gap must keep landing in the classic layout, not in the
+/// core's no-provider fallback.
+pub(crate) async fn build_and_serve(
+ builder: RuntimeBuilder,
+ ready_tx: Option<tokio::sync::oneshot::Sender<EmbeddedReadySignal>>,
+ shutdown_token: Option<CancellationToken>,
+) -> anyhow::Result<()> {
+ let summary = builder.summary();
+ log::debug!(
+ "[rpc:server] building runtime host_kind={:?} services={:?} fixed_token={} \
+ listen_host={:?} listen_port={:?} embedded_ready={}",
+ summary.host_kind,
+ summary.services,
+ summary.fixed_token,
+ summary.listen_host,
+ summary.listen_port,
+ ready_tx.is_some()
+ );
// The desktop app and the CLI keep conversations in the classic on-disk
// layout; the core itself carries no storage. Installed before boot so
// its recovery sweep runs.
crate::session_store::install();
- let runtime = builder.build().await?;
- super::serve::serve(&runtime, ready_tx, shutdown_token).await
-}
-
-fn apply_e2e_tool_groups(
- builder: crate::core_host::core::runtime::CoreBuilder,
-) -> crate::core_host::core::runtime::CoreBuilder {
- if std::env::var_os("OPENHUMAN_E2E").is_some() {
- builder.tool_groups(crate::core_host::tools::toolpacks::ToolGroups::advertised())
- } else {
- builder
- }
+ let runtime = builder.build().await.map_err(|error| {
+ log::warn!("[rpc:server] runtime build failed: {error}");
+ anyhow::Error::new(error)
+ })?;
+ let served = super::serve::serve(runtime.core_runtime(), ready_tx, shutdown_token).await;
+ // Dropping the runtime releases the process's embed runtime slot, so the
+ // desktop can restart its server in place.
+ drop(runtime);
+ log::debug!("[rpc:server] runtime released ok={}", served.is_ok());
+ served
}
#[cfg(test)]