Skip to content

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

feat(cli): add --mode and --saas-config to openhuman run

Steven Enamakel5e7ea97f2cfd
diff --git a/crates/openhuman-core/src/core/cli.rs b/crates/openhuman-core/src/core/cli.rs
index 19fbd10c50..c82b16aa07 100644
--- a/crates/openhuman-core/src/core/cli.rs
+++ b/crates/openhuman-core/src/core/cli.rs
@@ -316,6 +316,8 @@ fn run_server_command(args: &[String]) -> Result<()> {
let mut host: Option<String> = None;
let mut socketio_enabled = true;
let mut headless_api = false;
+ let mut mode_flag: Option<String> = None;
+ let mut saas_config: Option<std::path::PathBuf> = None;
let mut verbose = false;
let log_scope = CliLogDefault::Global;
let mut i = 0usize;
@@ -354,26 +356,31 @@ fn run_server_command(args: &[String]) -> Result<()> {
verbose = true;
i += 1;
}
+ "--mode" | "--saas-config" => {
+ let value = args
+ .get(i + 1)
+ .ok_or_else(|| anyhow::anyhow!("missing value for {}", args[i]))?
+ .clone();
+ if args[i] == "--mode" {
+ mode_flag = Some(value);
+ } else {
+ saas_config = Some(value.into());
+ }
+ i += 2;
+ }
"-h" | "--help" => {
- println!("Usage: openhuman run [--host <addr>] [--port <u16>] [--jsonrpc-only|--headless-api] [-v|--verbose]");
- println!();
- println!(
- " --host <addr> Bind address (default: 127.0.0.1 or OPENHUMAN_CORE_HOST)"
- );
- println!(
- " --port <u16> Listen address port (default: 7788 or OPENHUMAN_CORE_PORT)"
- );
- println!(" --jsonrpc-only HTTP JSON-RPC only; disable Socket.IO");
- println!(" --headless-api HTTP JSON-RPC only; disable all background services");
- println!(" -v, --verbose Shorthand for RUST_LOG=debug when RUST_LOG is unset");
- println!();
- println!("Logging: set RUST_LOG (e.g. RUST_LOG=debug openhuman run). Default level is info.");
+ println!("{}", crate::core::server_launcher::RUN_HELP);
return Ok(());
}
other => return Err(anyhow::anyhow!("unknown run arg: {other}")),
}
}
+ let (mode, saas_config) = crate::core::server_launcher::resolve_mode(
+ mode_flag.as_deref(),
+ std::env::var("OPENHUMAN_MODE").ok().as_deref(),
+ saas_config,
+ )?;
crate::core::logging::init_for_cli_run(verbose, log_scope);
// Initialize the Tokio multi-threaded runtime.
@@ -401,6 +408,8 @@ fn run_server_command(args: &[String]) -> Result<()> {
port,
socketio_enabled,
headless_api,
+ mode,
+ saas_config,
}))?;
Ok(())
}
diff --git a/crates/openhuman-core/src/core/server_launcher.rs b/crates/openhuman-core/src/core/server_launcher.rs
index 47c44be64b..4451961083 100644
--- a/crates/openhuman-core/src/core/server_launcher.rs
+++ b/crates/openhuman-core/src/core/server_launcher.rs
@@ -28,6 +28,57 @@ pub struct ServeRequest {
pub saas_config: Option<std::path::PathBuf>,
}
+/// `openhuman-core run --help`.
+pub const RUN_HELP: &str = "\
+Usage: openhuman run [--host <addr>] [--port <u16>] [--jsonrpc-only|--headless-api]
+ [--mode single-user|saas] [--saas-config <file>] [-v|--verbose]
+
+ --host <addr> Bind address (default: 127.0.0.1 or OPENHUMAN_CORE_HOST)
+ --port <u16> Listen address port (default: 7788 or OPENHUMAN_CORE_PORT)
+ --jsonrpc-only HTTP JSON-RPC only; disable Socket.IO
+ --headless-api HTTP JSON-RPC only; disable all background services
+ --mode <mode> single-user (default) or saas (or OPENHUMAN_MODE=saas)
+ --saas-config <file> The operator's SaaS config; required with --mode saas
+ -v, --verbose Shorthand for RUST_LOG=debug when RUST_LOG is unset
+
+Logging: set RUST_LOG (e.g. RUST_LOG=debug openhuman run). Default level is info.";
+
+/// Resolve the operating mode from `--mode`, `OPENHUMAN_MODE` and
+/// `--saas-config`.
+///
+/// The environment can only raise the mode to SaaS, never lower it: a
+/// deployment that sets `OPENHUMAN_MODE=saas` cannot be talked back into
+/// single-user by a flag. SaaS needs an operator config, and a config without
+/// SaaS is a mistake worth refusing rather than ignoring.
+pub fn resolve_mode(
+ flag: Option<&str>,
+ env: Option<&str>,
+ saas_config: Option<std::path::PathBuf>,
+) -> anyhow::Result<(crate::core::runtime::Mode, Option<std::path::PathBuf>)> {
+ use crate::core::runtime::Mode;
+ let parse = |raw: &str, source: &str| {
+ raw.parse::<Mode>()
+ .map_err(|e| anyhow::anyhow!("{source}: {e}"))
+ };
+ let from_flag = flag.map(|raw| parse(raw, "--mode")).transpose()?;
+ let from_env = env
+ .filter(|raw| !raw.trim().is_empty())
+ .map(|raw| parse(raw, "OPENHUMAN_MODE"))
+ .transpose()?;
+ let mode = if from_env == Some(Mode::Saas) {
+ Mode::Saas
+ } else {
+ from_flag.unwrap_or_default()
+ };
+ match (mode, &saas_config) {
+ (Mode::Saas, None) => anyhow::bail!("--mode saas needs --saas-config <file>"),
+ (Mode::SingleUser, Some(_)) => {
+ anyhow::bail!("--saas-config is only meaningful with --mode saas")
+ }
+ _ => Ok((mode, saas_config)),
+ }
+}
+
/// Starts a server for a [`ServeRequest`] and resolves when it stops.
pub type ServerLauncher =
fn(ServeRequest) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send>>;
diff --git a/crates/openhuman-core/src/core/server_launcher_tests.rs b/crates/openhuman-core/src/core/server_launcher_tests.rs
index 1b3b2857d0..bf2215947a 100644
--- a/crates/openhuman-core/src/core/server_launcher_tests.rs
+++ b/crates/openhuman-core/src/core/server_launcher_tests.rs
@@ -18,6 +18,47 @@ async fn first_installed_launcher_wins() {
port: None,
socketio_enabled: true,
headless_api: false,
+ mode: crate::core::runtime::Mode::SingleUser,
+ saas_config: None,
};
launcher(request).await.expect("the first launcher runs");
}
+
+#[test]
+fn the_default_is_single_user() {
+ let (mode, config) = resolve_mode(None, None, None).unwrap();
+ assert_eq!(mode, crate::core::runtime::Mode::SingleUser);
+ assert!(config.is_none());
+}
+
+#[test]
+fn saas_needs_an_operator_config() {
+ assert!(resolve_mode(Some("saas"), None, None).is_err());
+ let (mode, config) = resolve_mode(Some("saas"), None, Some("/srv/oh.toml".into())).unwrap();
+ assert_eq!(mode, crate::core::runtime::Mode::Saas);
+ assert_eq!(config, Some(std::path::PathBuf::from("/srv/oh.toml")));
+}
+
+#[test]
+fn the_environment_can_raise_the_mode_but_not_lower_it() {
+ use crate::core::runtime::Mode;
+ let config = Some(std::path::PathBuf::from("/srv/oh.toml"));
+ let (mode, _) = resolve_mode(Some("single-user"), Some("saas"), config.clone()).unwrap();
+ assert_eq!(mode, Mode::Saas);
+ let (mode, _) = resolve_mode(Some("saas"), Some("single-user"), config).unwrap();
+ assert_eq!(mode, Mode::Saas);
+}
+
+#[test]
+fn a_saas_config_without_saas_is_refused() {
+ let err = resolve_mode(None, None, Some("/srv/oh.toml".into())).unwrap_err();
+ assert!(err.to_string().contains("--mode saas"), "{err}");
+}
+
+#[test]
+fn an_unknown_mode_is_refused_with_its_source() {
+ let err = resolve_mode(Some("multi"), None, None).unwrap_err();
+ assert!(err.to_string().contains("--mode"), "{err}");
+ let err = resolve_mode(None, Some("multi"), None).unwrap_err();
+ assert!(err.to_string().contains("OPENHUMAN_MODE"), "{err}");
+}