Skip to content

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

refactor(inference): remove primary cloud provider factory module

Steven Enamakel5b8040c84848
diff --git a/crates/openhuman-core/src/inference/provider/factory/primary_cloud.rs b/crates/openhuman-core/src/inference/provider/factory/primary_cloud.rs
deleted file mode 100644
index cb579b6afe..0000000000
--- a/crates/openhuman-core/src/inference/provider/factory/primary_cloud.rs
+++ /dev/null
@@ -1,187 +0,0 @@
-//! `primary_cloud` / legacy `inference_url` resolution: which cloud entry an
-//! unset (or `"cloud"`) route falls through to, and the BYOK fail-closed sentinel.
-
-use super::*;
-
-pub(super) 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",
- redact_inference_url(config.inference_url.as_deref())
- );
- 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",
- redact_inference_url(config.inference_url.as_deref())
- );
- BYOK_INCOMPLETE_SENTINEL.to_string()
- } else {
- PROVIDER_OPENHUMAN.to_string()
- }
- })
-}
-
-/// Extract the host portion of an inference URL for safe logging.
-///
-/// Returns the host (e.g. `"api.example.com"`) so log lines are grep-friendly
-/// without exposing tokens or credentials that may appear in query-string or
-/// path components of a bearer-auth URL (e.g. `"https://host/v1?key=…"`).
-/// Falls back to `"<redacted>"` when the URL cannot be parsed or is absent.
-pub(super) fn redact_inference_url(url: Option<&str>) -> &str {
- url.and_then(|u| {
- // Minimal host extraction: find the authority after "://".
- let after_scheme = u.find("://").map(|i| &u[i + 3..])?;
- // Authority ends at '/', '?', '#', or end-of-string.
- let host_end = after_scheme
- .find(['/', '?', '#'])
- .unwrap_or(after_scheme.len());
- let authority = &after_scheme[..host_end];
- // Strip optional "user:pass@" and port.
- let host = authority
- .rfind('@')
- .map_or(authority, |i| &authority[i + 1..]);
- let host = host.rfind(':').map_or(host, |i| &host[..i]);
- if host.is_empty() {
- None
- } else {
- Some(host)
- }
- })
- .unwrap_or("<redacted>")
-}
-
-/// Return `true` when the config contains a non-openhuman `inference_url`,
-/// indicating the user intends custom/BYOK routing rather than the managed
-/// backend.
-pub(super) 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(super) 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(super) 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(super) fn cloud_entry_provider_string(
- entry: &crate::config::schema::cloud_providers::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(super) fn is_openhuman_cloud_entry(
- config: &Config,
- entry: &crate::config::schema::cloud_providers::CloudProviderCreds,
-) -> bool {
- entry.slug == PROVIDER_OPENHUMAN
- || matches!(entry.auth_style, AuthStyle::OpenhumanJwt)
- || looks_like_openhuman_backend(config, &entry.endpoint)
-}
-
-pub(super) 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(super) fn looks_like_openhuman_backend(config: &Config, url: &str) -> bool {
- config.resolved_endpoints.is_managed_host(url)
-}