Files
openhuman/examples/embed_headless.rs
T

52 lines
2.0 KiB
Rust

//! Embed the OpenHuman core as a library — no HTTP, no background services.
//!
//! Demonstrates the Phase-1 pluggable-core API: build a fully-initialized core
//! with [`ServiceSet::none`] (no ports bound, no cron/channels/heartbeat) and
//! dispatch RPC methods in-process through [`CoreRuntime::invoke`] — the exact
//! same path the HTTP `/rpc` handler and the CLI use.
//!
//! Run with:
//!
//! ```bash
//! GGML_NATIVE=OFF cargo run --example embed_headless
//! ```
//!
//! To instead expose the core over HTTP for a single-core cloud deployment,
//! swap `ServiceSet::none()` for `ServiceSet::headless_api()`, keep a
//! `CancellationToken`, and call `runtime.serve(None, Some(token)).await` — it
//! binds `127.0.0.1:7788` (override with `.host(..)` / `.port(..)` on the
//! builder, or `OPENHUMAN_CORE_HOST` / `OPENHUMAN_CORE_PORT`) and serves until
//! the token is cancelled.
use openhuman_core::{CoreBuilder, HostKind, ServiceSet};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Library embedders own logging; a simple env_logger keeps the example
// self-contained. (`RUST_LOG=info cargo run --example embed_headless`)
let _ = env_logger::builder().is_test(false).try_init();
// Initialize the core against the local workspace. `HostKind::Cli` selects
// the standalone (non-desktop) bootstrap path; `ServiceSet::none()` means no
// transport and no background services are started.
let runtime = CoreBuilder::new(HostKind::Cli)
.services(ServiceSet::none())
.build()
.await?;
// Dispatch a couple of RPC methods in-process — no network involved.
let version = runtime
.invoke("core.version", serde_json::json!({}))
.await
.map_err(|e| anyhow::anyhow!("core.version failed: {e}"))?;
println!("core.version -> {version}");
let ping = runtime
.invoke("openhuman.ping", serde_json::json!({}))
.await
.map_err(|e| anyhow::anyhow!("openhuman.ping failed: {e}"))?;
println!("openhuman.ping -> {ping}");
Ok(())
}