Skip to content

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

feat(config): extract cloud provider settings into ops module

Steven Enamakele621aea178dd
diff --git a/crates/openhuman-core/src/config/ops/cloud_providers.rs b/crates/openhuman-core/src/config/ops/cloud_providers.rs
new file mode 100644
index 0000000000..a5882463d3
--- /dev/null
+++ b/crates/openhuman-core/src/config/ops/cloud_providers.rs
@@ -0,0 +1,102 @@
+//! RPC input conversion for cloud-provider settings.
+
+use crate::config::schema::cloud_providers::{
+ builtin_cloud_provider, generate_provider_id, is_slug_reserved, AuthStyle, CloudProviderCreds,
+};
+use serde::Deserialize;
+
+#[derive(Debug, Deserialize)]
+pub struct CloudProviderUpdate {
+ pub(crate) id: Option<String>,
+ pub(crate) slug: String,
+ #[serde(default)]
+ pub(crate) label: Option<String>,
+ pub(crate) endpoint: String,
+ #[serde(default)]
+ pub(crate) auth_style: Option<String>,
+ #[serde(default)]
+ pub(crate) ca_cert_pem: Option<String>,
+ // keep until v0.66 (review after 2027-01-11): incoming RPC discriminator
+ // remains accepted until shipped-client parameter coverage exists.
+ #[serde(rename = "type", default)]
+ pub(crate) legacy_type: Option<String>,
+ #[serde(default)]
+ pub(crate) default_model: Option<String>,
+}
+
+/// Convert cloud-provider settings RPC inputs into validated config entries.
+///
+/// Reserved built-in slugs are dropped here because the config update operation
+/// re-injects their stored entries after replacing the user-provided list.
+pub fn build_cloud_provider_creds(
+ entries: Vec<CloudProviderUpdate>,
+) -> Result<Vec<CloudProviderCreds>, String> {
+ let reserved_count = entries
+ .iter()
+ .filter(|entry| {
+ let slug = entry.slug.trim();
+ !slug.is_empty() && is_slug_reserved(slug)
+ })
+ .count();
+ if reserved_count > 0 {
+ log::debug!(
+ "[inference] update_model_settings: dropping {} reserved cloud provider slug(s)",
+ reserved_count
+ );
+ }
+
+ entries
+ .into_iter()
+ // Silently drop entries whose (non-empty) slug is reserved —
+ // typically migration-seeded built-ins that the frontend echoes back.
+ // Empty slugs still fall through so validation reports frontend bugs.
+ .filter(|entry| {
+ let slug = entry.slug.trim();
+ slug.is_empty() || !is_slug_reserved(slug)
+ })
+ .map(|entry| {
+ let slug = entry.slug.trim().to_string();
+ if slug.is_empty() {
+ return Err("cloud provider slug must not be empty".to_string());
+ }
+
+ let legacy_provider = entry
+ .legacy_type
+ .as_deref()
+ .map(str::trim)
+ .and_then(builtin_cloud_provider);
+ let legacy_auth = legacy_provider
+ .map(|provider| provider.auth_style.as_str())
+ .unwrap_or("bearer");
+ let endpoint = if entry.endpoint.is_empty() {
+ legacy_provider
+ .map(|provider| provider.endpoint.to_string())
+ .unwrap_or(entry.endpoint)
+ } else {
+ entry.endpoint
+ };
+ let auth_style = AuthStyle::parse(entry.auth_style.as_deref().unwrap_or(legacy_auth))?;
+ let id = entry
+ .id
+ .filter(|value| !value.trim().is_empty())
+ .unwrap_or_else(|| generate_provider_id(&slug));
+ let label = entry
+ .label
+ .filter(|value| !value.trim().is_empty())
+ .unwrap_or_else(|| slug.clone());
+
+ Ok(CloudProviderCreds {
+ id,
+ slug,
+ label,
+ endpoint,
+ auth_style,
+ default_model: entry.default_model,
+ })
+ })
+ .collect()
+}
+
+#[cfg(test)]
+#[path = "cloud_providers_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/config/ops/mod.rs b/crates/openhuman-core/src/config/ops/mod.rs
index db05b02d8c..a98e83cd11 100644
--- a/crates/openhuman-core/src/config/ops/mod.rs
+++ b/crates/openhuman-core/src/config/ops/mod.rs
@@ -2,8 +2,10 @@
mod agent;
mod agent_paths;
+mod cloud_providers;
mod loader;
mod model;
+mod primary_cloud;
mod privacy;
mod sandbox;
mod search;
@@ -114,6 +116,14 @@ pub use model::{
RuntimeSettingsPatch,
};
+pub use cloud_providers::{build_cloud_provider_creds, CloudProviderUpdate};
+pub(crate) use primary_cloud::{
+ cloud_entry_provider_string, has_custom_inference_intent, is_openhuman_cloud_entry,
+ legacy_custom_inference_provider_string, legacy_inference_slug,
+ looks_like_openhuman_backend, normalize_endpoint_for_compare,
+ resolve_primary_cloud_provider_string,
+};
+
pub use privacy::{
apply_privacy_settings, get_privacy_mode, load_and_apply_privacy_settings, PrivacySettingsPatch,
};
diff --git a/crates/openhuman-core/src/config/ops/primary_cloud.rs b/crates/openhuman-core/src/config/ops/primary_cloud.rs
new file mode 100644
index 0000000000..7ac6b53912
--- /dev/null
+++ b/crates/openhuman-core/src/config/ops/primary_cloud.rs
@@ -0,0 +1,157 @@
+//! Primary cloud-provider selection and legacy direct-inference compatibility.
+
+use crate::config::schema::cloud_providers::{AuthStyle, CloudProviderCreds};
+use crate::config::Config;
+use crate::inference::provider::factory::{BYOK_INCOMPLETE_SENTINEL, PROVIDER_OPENHUMAN};
+
+pub(crate) fn resolve_primary_cloud_provider_string(config: &Config) -> String {
+ let primary = config
+ .primary_cloud
+ .as_deref()
+ .and_then(|id| config.cloud_providers.iter().find(|entry| entry.id == id));
+
+ if primary.is_some_and(|entry| is_openhuman_cloud_entry(config, entry)) {
+ if let Some(legacy) = legacy_custom_inference_provider_string(config) {
+ return legacy;
+ }
+ // Primary is explicitly OpenHuman but inference_url points at a custom
+ // endpoint with no matching provider entry — this is a half-migrated BYOK
+ // config. Fail closed so the user sees an actionable error rather than
+ // silently routing through the managed backend.
+ if has_custom_inference_intent(config) {
+ log::debug!(
+ "[providers][chat-factory] BYOK intent detected (host={}) \
+ but no matching cloud_providers entry found; returning fail-closed sentinel",
+ tinyinference_core::sanitize::redact_url(
+ config.inference_url.as_deref().unwrap_or_default()
+ )
+ );
+ return BYOK_INCOMPLETE_SENTINEL.to_string();
+ }
+ }
+
+ if let Some(entry) = primary {
+ return cloud_entry_provider_string(entry, config);
+ }
+
+ // No explicit primary configured. If inference_url signals custom intent but
+ // no matching provider entry exists, fail closed instead of falling back to
+ // the managed backend.
+ legacy_custom_inference_provider_string(config).unwrap_or_else(|| {
+ if has_custom_inference_intent(config) {
+ log::debug!(
+ "[providers][chat-factory] BYOK intent detected (host={}) \
+ with no primary_cloud and no matching provider entry; returning fail-closed sentinel",
+ tinyinference_core::sanitize::redact_url(
+ config.inference_url.as_deref().unwrap_or_default()
+ )
+ );
+ BYOK_INCOMPLETE_SENTINEL.to_string()
+ } else {
+ PROVIDER_OPENHUMAN.to_string()
+ }
+ })
+}
+
+/// Return `true` when the config contains a non-openhuman `inference_url`,
+/// indicating the user intends custom/BYOK routing rather than the managed
+/// backend.
+pub(crate) fn has_custom_inference_intent(config: &Config) -> bool {
+ config
+ .inference_url
+ .as_deref()
+ .map(str::trim)
+ .filter(|url| !url.is_empty())
+ .is_some_and(|url| !looks_like_openhuman_backend(config, url))
+}
+
+pub(crate) fn legacy_custom_inference_provider_string(config: &Config) -> Option<String> {
+ let inference_url = config
+ .inference_url
+ .as_deref()
+ .map(str::trim)
+ .filter(|url| !url.is_empty())?;
+
+ if looks_like_openhuman_backend(config, inference_url) {
+ return None;
+ }
+
+ let normalized_inference = normalize_endpoint_for_compare(inference_url);
+ config
+ .cloud_providers
+ .iter()
+ .find(|entry| {
+ !is_openhuman_cloud_entry(config, entry)
+ && normalize_endpoint_for_compare(&entry.endpoint) == normalized_inference
+ })
+ .map(|entry| cloud_entry_provider_string(entry, config))
+}
+
+/// Resolve the slug of the cloud-provider entry that represents the legacy
+/// direct-inference route — the entry whose endpoint matches the configured
+/// custom `inference_url`.
+///
+/// Top-level `config.api_key` was historically paired with `inference_url`
+/// for direct endpoint routing, so it is scoped to this single provider. The
+/// `lookup_key_for_slug` fallback uses this to avoid leaking the global key to
+/// any other provider slug whose auth-profile lookup returned empty.
+pub(crate) fn legacy_inference_slug(config: &Config) -> Option<&str> {
+ let inference_url = config
+ .inference_url
+ .as_deref()
+ .map(str::trim)
+ .filter(|url| !url.is_empty())?;
+
+ if looks_like_openhuman_backend(config, inference_url) {
+ return None;
+ }
+
+ let normalized_inference = normalize_endpoint_for_compare(inference_url);
+ config
+ .cloud_providers
+ .iter()
+ .find(|entry| {
+ !is_openhuman_cloud_entry(config, entry)
+ && normalize_endpoint_for_compare(&entry.endpoint) == normalized_inference
+ })
+ .map(|entry| entry.slug.as_str())
+}
+
+pub(crate) fn cloud_entry_provider_string(entry: &CloudProviderCreds, config: &Config) -> String {
+ if is_openhuman_cloud_entry(config, entry) {
+ return PROVIDER_OPENHUMAN.to_string();
+ }
+
+ let model = entry
+ .default_model
+ .as_deref()
+ .map(str::trim)
+ .filter(|model| !model.is_empty())
+ .or_else(|| {
+ config
+ .default_model
+ .as_deref()
+ .map(str::trim)
+ .filter(|model| !model.is_empty())
+ })
+ .unwrap_or(crate::config::DEFAULT_MODEL);
+
+ format!("{}:{model}", entry.slug)
+}
+
+pub(crate) fn is_openhuman_cloud_entry(config: &Config, entry: &CloudProviderCreds) -> bool {
+ entry.slug == PROVIDER_OPENHUMAN
+ || matches!(entry.auth_style, AuthStyle::OpenhumanJwt)
+ || looks_like_openhuman_backend(config, &entry.endpoint)
+}
+
+pub(crate) fn normalize_endpoint_for_compare(url: &str) -> String {
+ url.trim().trim_end_matches('/').to_ascii_lowercase()
+}
+
+/// Whether `url` names one of the managed hosts the installed host resolved
+/// onto the config (`resolved_endpoints.managed_hosts`). With no host
+/// registered nothing is managed, so every endpoint is a custom one.
+pub(crate) fn looks_like_openhuman_backend(config: &Config, url: &str) -> bool {
+ config.resolved_endpoints.is_managed_host(url)
+}
diff --git a/crates/openhuman-core/src/config/schema/cloud_providers.rs b/crates/openhuman-core/src/config/schema/cloud_providers.rs
index 8037a93861..e8cf5d029b 100644
--- a/crates/openhuman-core/src/config/schema/cloud_providers.rs
+++ b/crates/openhuman-core/src/config/schema/cloud_providers.rs
@@ -202,6 +202,20 @@ pub enum AuthStyle {
}
impl AuthStyle {
+ /// Parse the RPC compatibility spellings for a provider authentication style.
+ pub fn parse(value: &str) -> Result<Self, String> {
+ match value.to_ascii_lowercase().as_str() {
+ "bearer" => Ok(Self::Bearer),
+ "anthropic" => Ok(Self::Anthropic),
+ "openhuman_jwt" | "openhumanjwt" => Ok(Self::OpenhumanJwt),
+ "none" => Ok(Self::None),
+ other => Err(format!(
+ "unknown auth_style '{}'; valid: bearer, anthropic, openhuman_jwt, none",
+ other
+ )),
+ }
+ }
+
pub fn as_str(&self) -> &'static str {
match self {
Self::Bearer => "bearer",
diff --git a/crates/openhuman-core/src/inference/schemas/settings_handlers.rs b/crates/openhuman-core/src/inference/schemas/settings_handlers.rs
index 3e8e7505f9..a7b24d4563 100644
--- a/crates/openhuman-core/src/inference/schemas/settings_handlers.rs
+++ b/crates/openhuman-core/src/inference/schemas/settings_handlers.rs
@@ -23,25 +23,6 @@ pub(super) struct InferenceModelRouteUpdate {
model: String,
}
-#[derive(Debug, Deserialize)]
-pub(super) struct InferenceCloudProviderUpdate {
- id: Option<String>,
- slug: String,
- #[serde(default)]
- label: Option<String>,
- endpoint: String,
- #[serde(default)]
- auth_style: Option<String>,
- #[serde(default)]
- ca_cert_pem: Option<String>,
- // keep until v0.66 (review after 2027-01-11): incoming RPC discriminator
- // remains accepted until shipped-client parameter coverage exists.
- #[serde(rename = "type", default)]
- legacy_type: Option<String>,
- #[serde(default)]
- default_model: Option<String>,
-}
-
#[derive(Debug, Deserialize)]
pub(super) struct InferenceUpdateModelSettingsParams {
api_url: Option<String>,
@@ -50,7 +31,7 @@ pub(super) struct InferenceUpdateModelSettingsParams {
default_model: Option<String>,
default_temperature: Option<f64>,
model_routes: Option<Vec<InferenceModelRouteUpdate>>,
- cloud_providers: Option<Vec<InferenceCloudProviderUpdate>>,
+ cloud_providers: Option<Vec<crate::config::ops::CloudProviderUpdate>>,
#[serde(default)]
model_registry: Option<Vec<crate::config::schema::ModelRegistryEntry>>,
primary_cloud: Option<String>,
@@ -158,94 +139,7 @@ pub(super) fn handle_inference_update_model_settings(
}),
cloud_providers: update
.cloud_providers
- .map(|entries| {
- use crate::config::schema::cloud_providers::{
- generate_provider_id, is_slug_reserved, builtin_cloud_provider, AuthStyle,
- CloudProviderCreds,
- };
- let reserved_count = entries
- .iter()
- .filter(|e| {
- let t = e.slug.trim();
- !t.is_empty() && is_slug_reserved(t)
- })
- .count();
- if reserved_count > 0 {
- log::debug!(
- "[inference] update_model_settings: dropping {} reserved cloud provider slug(s)",
- reserved_count
- );
- }
- entries
- .into_iter()
- // Silently drop entries whose (non-empty) slug is reserved —
- // typically the migration-seeded "openhuman" / "cloud" /
- // "pid" built-ins that the frontend echoes back on every
- // save (see `migrations::unify_ai_provider_settings`).
- // Empty slugs still fall through so the explicit
- // validation error below fires for actual frontend
- // bugs. `apply_model_settings` re-injects the existing
- // reserved entries from the stored config so they
- // aren't dropped on save.
- .filter(|entry| {
- let trimmed = entry.slug.trim();
- trimmed.is_empty() || !is_slug_reserved(trimmed)
- })
- .map(|entry| {
- let slug = entry.slug.trim().to_string();
- if slug.is_empty() {
- return Err("cloud provider slug must not be empty".to_string());
- }
- let legacy_provider = entry.legacy_type.as_deref()
- .map(str::trim)
- .and_then(builtin_cloud_provider);
- let legacy_auth = legacy_provider
- .map(|provider| provider.auth_style.as_str())
- .unwrap_or("bearer");
- let endpoint = if entry.endpoint.is_empty() {
- legacy_provider.map(|provider| provider.endpoint.to_string())
- .unwrap_or(entry.endpoint)
- } else {
- entry.endpoint
- };
- let auth_style = match entry
- .auth_style
- .as_deref()
- .unwrap_or(legacy_auth)
- .to_ascii_lowercase()
- .as_str()
- {
- "bearer" => AuthStyle::Bearer,
- "anthropic" => AuthStyle::Anthropic,
- "openhuman_jwt" | "openhumanjwt" => AuthStyle::OpenhumanJwt,
- "none" => AuthStyle::None,
- other => {
- return Err(format!(
- "unknown auth_style '{}'; valid: bearer, anthropic, openhuman_jwt, none",
- other
- ))
- }
- };
- let id = entry
- .id
- .filter(|s| !s.trim().is_empty())
- .unwrap_or_else(|| generate_provider_id(&slug));
- let label = entry
- .label
- .filter(|s| !s.trim().is_empty())
- .unwrap_or_else(|| slug.clone());
- let provider = CloudProviderCreds {
- id,
- slug,
- label,
- endpoint,
- auth_style,
- default_model: entry.default_model,
- };
- Ok(provider)
- })
- .collect::<Result<Vec<_>, String>>()
- })
+ .map(crate::config::ops::build_cloud_provider_creds)
.transpose()?,
model_registry: update.model_registry,
primary_cloud: update.primary_cloud,