Skip to content

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

refactor(inference): update cloud provider factory implementation

Steven Enamakel0eebf81b3173
diff --git a/crates/openhuman-core/src/inference/provider/factory/cloud_spec.rs b/crates/openhuman-core/src/inference/provider/factory/cloud_spec.rs
index d3bcfd29ee..df97fd5dce 100644
--- a/crates/openhuman-core/src/inference/provider/factory/cloud_spec.rs
+++ b/crates/openhuman-core/src/inference/provider/factory/cloud_spec.rs
@@ -1,24 +1,16 @@
-//! `CloudProviderCreds` → `ProviderSpec` → `ProviderRegistry` mapping.
-//!
-//! The host owns saved configuration, routing and access gates; this module
-//! hands the resolved route to `tinyinference-llm`'s [`ProviderRegistry`],
-//! which owns adapter construction (Chat Completions, Anthropic Messages,
-//! Codex Responses, OpenRouter routing options) and listing discovery. Only
-//! host policy is expressed here: which auth style a slug uses, which extra
-//! headers the ephemeral route and Codex OAuth add, and the TLS client for a
-//! provider with a custom CA bundle.
+//! Host-resolved cloud provider inputs for TinyInference's provider registry.
use super::*;
use tinyinference_llm::providers::openai::codex::OpenAiCodexRouting;
-use tinyinference_llm::providers::registry::{ProviderBuildOptions, ProviderRegistry};
+use tinyinference_llm::providers::registry::{
+ CredentialResolver, ProviderBuildOptions, ProviderRegistry,
+};
use tinyinference_llm::{ProviderKind, ProviderSpec};
use crate::config::schema::cloud_providers::CloudProviderCreds;
/// Maps a configured cloud entry and its effective model onto the registry's
-/// spec. Credentials are resolved by the host beforehand, so no env var is
-/// named and a key is not required at the spec level (a blank key is a host
-/// decision, made in `resolve_cloud_slug`).
+/// spec without resolving credentials or changing the configured endpoint.
pub(super) fn cloud_provider_spec(entry: &CloudProviderCreds, model: &str) -> ProviderSpec {
ProviderSpec {
kind: match entry.auth_style {
@@ -33,86 +25,91 @@ pub(super) fn cloud_provider_spec(entry: &CloudProviderCreds, model: &str) -> Pr
}
}
-/// The registry auth style for a host [`AuthStyle`]; `None` for the managed
-/// JWT style, which never reaches the registry.
-pub(super) fn registry_auth_style(style: AuthStyle) -> Option<CompatAuthStyle> {
- match style {
- AuthStyle::Bearer => Some(CompatAuthStyle::Bearer),
- AuthStyle::Anthropic => Some(CompatAuthStyle::Anthropic),
- AuthStyle::None => Some(CompatAuthStyle::None),
- _ => None,
- }
-}
+/// A credential already selected by OpenHuman's auth-profile and OAuth policy.
+pub(super) struct ResolvedCloudCredential<'a>(pub(super) &'a str);
-/// Host-resolved inputs for [`build_cloud_model`].
-pub(super) struct CloudBuild<'a> {
- pub(super) spec: ProviderSpec,
- pub(super) auth_style: CompatAuthStyle,
- pub(super) key: &'a str,
- pub(super) temperature_override: Option<f64>,
- pub(super) temperature_unsupported_models: &'a [String],
- pub(super) native_tool_calling: bool,
- pub(super) extra_headers: &'a [(String, String)],
- pub(super) extra_query_params: &'a [(String, String)],
- pub(super) user_agent: Option<&'a str>,
- pub(super) codex: Option<&'a OpenAiCodexRouting>,
- pub(super) http: Option<reqwest::Client>,
+impl CredentialResolver for ResolvedCloudCredential<'_> {
+ fn resolve(&self, _spec: &ProviderSpec) -> tinyinference_llm::Result<String> {
+ Ok(self.0.to_owned())
+ }
}
-impl<'a> CloudBuild<'a> {
- fn options(&self) -> ProviderBuildOptions<'a> {
- ProviderBuildOptions {
- auth_style: Some(self.auth_style.clone()),
- temperature_override: self.temperature_override,
- temperature_unsupported_models: self.temperature_unsupported_models,
- native_tool_calling: Some(self.native_tool_calling),
- extra_headers: self.extra_headers,
- extra_query_params: self.extra_query_params,
- user_agent: self.user_agent,
- codex: self.codex,
- http: self.http.clone(),
- ..ProviderBuildOptions::default()
+impl From<AuthStyle> for tinyinference_llm::providers::openai::AuthStyle {
+ fn from(style: AuthStyle) -> Self {
+ match style {
+ AuthStyle::Bearer => Self::Bearer,
+ AuthStyle::Anthropic => Self::Anthropic,
+ AuthStyle::None | AuthStyle::OpenhumanJwt => Self::None,
}
}
-
- fn registry(
- &self,
- ) -> ProviderRegistry<impl tinyinference_llm::providers::registry::CredentialResolver + '_>
- {
- let key = self.key.to_string();
- ProviderRegistry::new(move |_: &ProviderSpec| Ok(key.clone()))
- }
}
-/// Builds the chat model for a resolved cloud route through the registry.
-pub(super) fn build_cloud_model(build: CloudBuild<'_>) -> anyhow::Result<Arc<dyn ChatModel<()>>> {
+/// Builds a cloud provider through the owner registry with host-resolved policy.
+pub(super) fn build_cloud_model(
+ spec: &ProviderSpec,
+ key: &str,
+ auth_style: AuthStyle,
+ temperature_override: Option<f64>,
+ temperature_unsupported_models: &[String],
+ native_tool_calling: bool,
+ extra_headers: &[(String, String)],
+ extra_query_params: &[(String, String)],
+ user_agent: Option<&str>,
+ codex: Option<&OpenAiCodexRouting>,
+ http: Option<reqwest::Client>,
+) -> anyhow::Result<Arc<dyn ChatModel<()>>> {
log::debug!(
"[providers][cloud-spec] build slug={} kind={} auth={:?}",
- build.spec.provider,
- build.spec.kind.as_str(),
- build.auth_style
+ spec.provider,
+ spec.kind.as_str(),
+ auth_style
);
- build
- .registry()
- .build_with(&build.spec, build.options())
+ ProviderRegistry::new(ResolvedCloudCredential(key))
+ .build_with(
+ spec,
+ ProviderBuildOptions {
+ auth_style: Some(auth_style.into()),
+ temperature_override,
+ temperature_unsupported_models,
+ native_tool_calling: Some(native_tool_calling),
+ extra_headers,
+ extra_query_params,
+ user_agent,
+ codex,
+ http,
+ ..ProviderBuildOptions::default()
+ },
+ )
.map_err(|error| anyhow::anyhow!("[chat-factory] {error}"))
}
-/// The registry's listing request for a resolved cloud route (headers, pinned
-/// OpenRouter providers), or `None` when the route has no discoverable
-/// listing.
+/// The registry's listing request for a resolved route, or `None` when the
+/// route has no discoverable listing.
pub(super) fn cloud_discovery_request(
- build: &CloudBuild<'_>,
+ spec: &ProviderSpec,
+ key: &str,
+ auth_style: AuthStyle,
+ extra_headers: &[(String, String)],
+ extra_query_params: &[(String, String)],
+ user_agent: Option<&str>,
+ codex: Option<&OpenAiCodexRouting>,
) -> Option<tinyinference_llm::model::discover::DiscoveryRequest> {
- match build
- .registry()
- .discovery_request(&build.spec, &build.options())
- {
- Ok(request) => request,
+ match ProviderRegistry::new(ResolvedCloudCredential(key)).discovery_request(
+ spec,
+ &ProviderBuildOptions {
+ auth_style: Some(auth_style.into()),
+ extra_headers,
+ extra_query_params,
+ user_agent,
+ codex,
+ ..ProviderBuildOptions::default()
+ },
+ ) {
+ Ok(request) => Some(request),
Err(error) => {
log::debug!(
"[model_limits] registry discovery unavailable slug={}: {error}",
- build.spec.provider
+ spec.provider
);
None
}
diff --git a/crates/openhuman-core/src/inference/provider/factory/cloud_spec_tests.rs b/crates/openhuman-core/src/inference/provider/factory/cloud_spec_tests.rs
index 15bce6e6c8..34ca5a9ebf 100644
--- a/crates/openhuman-core/src/inference/provider/factory/cloud_spec_tests.rs
+++ b/crates/openhuman-core/src/inference/provider/factory/cloud_spec_tests.rs
@@ -11,20 +11,21 @@ fn entry(slug: &str, endpoint: &str, auth_style: AuthStyle) -> CloudProviderCred
}
}
-fn build<'a>(entry: &CloudProviderCreds, model: &str, key: &'a str) -> CloudBuild<'a> {
- CloudBuild {
- spec: cloud_provider_spec(entry, model),
- auth_style: registry_auth_style(entry.auth_style).expect("not managed"),
+fn build(entry: &CloudProviderCreds, model: &str, key: &str) {
+ build_cloud_model(
+ &cloud_provider_spec(entry, model),
key,
- temperature_override: None,
- temperature_unsupported_models: &[],
- native_tool_calling: true,
- extra_headers: &[],
- extra_query_params: &[],
- user_agent: None,
- codex: None,
- http: None,
- }
+ entry.auth_style,
+ None,
+ &[],
+ true,
+ &[],
+ &[],
+ None,
+ None,
+ None,
+ )
+ .unwrap_or_else(|error| panic!("{} failed to build: {error}", entry.slug));
}
#[test]
@@ -51,8 +52,11 @@ fn spec_keeps_the_slug_as_provider_name_and_picks_the_kind_from_auth() {
#[test]
fn managed_jwt_style_never_maps_onto_the_registry() {
- assert!(registry_auth_style(AuthStyle::OpenhumanJwt).is_none());
- assert!(registry_auth_style(AuthStyle::Bearer).is_some());
+ let _: tinyinference_llm::providers::openai::AuthStyle = AuthStyle::Bearer.into();
+ assert_eq!(
+ tinyinference_llm::providers::openai::AuthStyle::from(AuthStyle::OpenhumanJwt),
+ tinyinference_llm::providers::openai::AuthStyle::None
+ );
}
#[test]
@@ -62,8 +66,7 @@ fn registry_builds_a_model_for_every_unmanaged_builtin_preset() {
continue;
}
let creds = entry(preset.slug, preset.endpoint, preset.auth_style);
- build_cloud_model(build(&creds, "some-model", "sk-test"))
- .unwrap_or_else(|error| panic!("{} failed to build: {error}", preset.slug));
+ build(&creds, "some-model", "sk-test");
}
}
@@ -74,8 +77,17 @@ fn discovery_request_carries_bearer_auth_and_pins_openrouter_sorting() {
"https://openrouter.ai/api/v1",
AuthStyle::Bearer,
);
- let request =
- cloud_discovery_request(&build(&creds, "m", "sk-or")).expect("openrouter has a listing");
+ let spec = cloud_provider_spec(&creds, "m");
+ let request = cloud_discovery_request(
+ &spec,
+ "sk-or",
+ AuthStyle::Bearer,
+ &[],
+ &[],
+ None,
+ None,
+ )
+ .expect("openrouter has a listing");
assert_eq!(request.endpoint, "https://openrouter.ai/api/v1");
assert!(request
.headers
@@ -90,7 +102,17 @@ fn discovery_request_uses_x_api_key_for_anthropic() {
"https://api.anthropic.com/v1",
AuthStyle::Anthropic,
);
- let request = cloud_discovery_request(&build(&claude, "claude", "sk-ant")).expect("listing");
+ let spec = cloud_provider_spec(&claude, "claude");
+ let request = cloud_discovery_request(
+ &spec,
+ "sk-ant",
+ AuthStyle::Anthropic,
+ &[],
+ &[],
+ None,
+ None,
+ )
+ .expect("listing");
assert!(request.headers.iter().any(|(name, _)| name == "x-api-key"));
}
@@ -103,12 +125,17 @@ fn discovery_request_builds_codex_oauth_listing_request() {
account_id: Some("account-123".to_string()),
};
let query = [("client_version".to_string(), "0.130.0".to_string())];
- let mut with_oauth = build(&openai, "gpt", "tok");
- with_oauth.codex = Some(&oauth);
- with_oauth.extra_query_params = &query;
- with_oauth.user_agent = Some("codex_cli_rs/0.0.0 (OpenHuman 0.64.18)");
-
- let request = cloud_discovery_request(&with_oauth).expect("Codex OAuth has a listing");
+ let spec = cloud_provider_spec(&openai, "gpt");
+ let request = cloud_discovery_request(
+ &spec,
+ "tok",
+ AuthStyle::Bearer,
+ &[],
+ &query,
+ Some("codex_cli_rs/0.0.0 (OpenHuman 0.64.18)"),
+ Some(&oauth),
+ )
+ .expect("Codex OAuth has a listing");
assert_eq!(request.endpoint, "https://chatgpt.com/backend-api/codex");
assert_eq!(
request.effective_listing_url(),