Skip to content

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

refactor(inference): enhance provider factory with context awareness

Steven Enamakel9c781798cbd7
diff --git a/crates/openhuman-core/src/config/schema/cloud_providers.rs b/crates/openhuman-core/src/config/schema/cloud_providers.rs
index f7ba144dd6..7ea37584ad 100644
--- a/crates/openhuman-core/src/config/schema/cloud_providers.rs
+++ b/crates/openhuman-core/src/config/schema/cloud_providers.rs
@@ -84,6 +84,16 @@ pub fn is_hosted_auth_style(style: AuthStyle) -> bool {
.is_some_and(|classify| classify(style))
}
+/// Whether the selected runtime or process host owns this compatibility auth style.
+pub(crate) fn is_hosted_auth_style_for(
+ extensions: Option<&crate::core::slots::Extensions>,
+ style: AuthStyle,
+) -> bool {
+ HOSTED_AUTH_STYLE
+ .get_for(extensions, "default")
+ .is_some_and(|classify| classify(style))
+}
+
impl From<&'static tinyinference_providers::builtin::BuiltinCloudProvider>
for BuiltinCloudProvider
{
diff --git a/crates/openhuman-core/src/inference/context_window.rs b/crates/openhuman-core/src/inference/context_window.rs
index a452596fb1..58c1225934 100644
--- a/crates/openhuman-core/src/inference/context_window.rs
+++ b/crates/openhuman-core/src/inference/context_window.rs
@@ -218,9 +218,14 @@ async fn resolve_context_window_with_inner(
) -> ResolvedWindow {
let local_kind = tinyinference_local::profile::kind_from_provider_string(provider);
let request = match local_kind {
- None => {
- crate::inference::provider::factory::model_limits_request(role, provider, model, config)
- }
+ None => match extensions {
+ Some(extensions) => crate::inference::provider::factory::model_limits_request_for(
+ role, provider, model, config, extensions,
+ ),
+ None => crate::inference::provider::factory::model_limits_request(
+ role, provider, model, config,
+ ),
+ },
Some(tinyinference_local::profile::LocalProviderKind::Ollama) => {
ollama_limits_request(model, config)
}
diff --git a/crates/openhuman-core/src/inference/provider/factory.rs b/crates/openhuman-core/src/inference/provider/factory.rs
index d6b9114dd3..a8096574f3 100644
--- a/crates/openhuman-core/src/inference/provider/factory.rs
+++ b/crates/openhuman-core/src/inference/provider/factory.rs
@@ -58,7 +58,7 @@ pub use chat_model::{
};
pub(crate) use credentials::openai_bearer_is_oauth;
pub use credentials::{auth_key_for_slug, lookup_key_for_slug, redact_endpoint};
-pub(crate) use discovery::model_limits_request;
+pub(crate) use discovery::{model_limits_request, model_limits_request_for};
pub(crate) use local_runtime::create_local_chat_model_from_string;
pub(crate) use managed_backend::make_openhuman_backend_model;
pub(crate) use routing::role_uses_implicit_cloud_fallback;
@@ -74,9 +74,11 @@ pub(crate) use turn_model::{
use access_gates::{emit_inference_egress, enforce_local_only_inference};
use chat_model::{unresolved_chat_model_error, with_default_temperature};
use cloud_slug::{
- resolve_cloud_slug, try_create_cloud_slug_chat_model,
+ resolve_cloud_slug, resolve_cloud_slug_for, try_create_cloud_slug_chat_model,
try_create_cloud_slug_chat_model_from_string,
+ try_create_cloud_slug_chat_model_from_string_with_native_tools_for,
try_create_cloud_slug_chat_model_with_native_tools,
+ try_create_cloud_slug_chat_model_with_native_tools_for,
};
use local_runtime::{
try_create_local_runtime_chat_model, try_create_local_runtime_chat_model_from_string,
diff --git a/crates/openhuman-core/src/inference/provider/factory/cloud_slug.rs b/crates/openhuman-core/src/inference/provider/factory/cloud_slug.rs
index fa9d5ab015..84e7b3359f 100644
--- a/crates/openhuman-core/src/inference/provider/factory/cloud_slug.rs
+++ b/crates/openhuman-core/src/inference/provider/factory/cloud_slug.rs
@@ -27,6 +27,17 @@ pub(super) fn resolve_cloud_slug<'a>(
slug: &str,
model: &str,
config: &'a Config,
+) -> anyhow::Result<CloudSlugResolution<'a>> {
+ let extensions = crate::core::runtime::CoreContext::current_extensions();
+ resolve_cloud_slug_for(role, slug, model, config, extensions.as_deref())
+}
+
+pub(super) fn resolve_cloud_slug_for<'a>(
+ role: &str,
+ slug: &str,
+ model: &str,
+ config: &'a Config,
+ extensions: Option<&crate::core::slots::Extensions>,
) -> anyhow::Result<CloudSlugResolution<'a>> {
let entry = config.cloud_providers.iter().find(|e| e.slug == slug);
@@ -64,7 +75,10 @@ pub(super) fn resolve_cloud_slug<'a>(
// OpenhumanJwt entries are exempt: they always delegate to
// make_openhuman_backend which derives the model from config.default_model,
// ignoring whatever effective_model we computed here.
- let hosted = crate::config::schema::cloud_providers::is_hosted_auth_style(entry.auth_style);
+ let hosted = crate::config::schema::cloud_providers::is_hosted_auth_style_for(
+ extensions,
+ entry.auth_style,
+ );
if !hosted && effective_model.trim().is_empty() {
log::warn!(
"[providers][chat-factory] role={} slug={} resolved to empty model — \
@@ -84,11 +98,19 @@ pub(super) fn resolve_cloud_slug<'a>(
}
if !hosted
- && crate::inference::provider_model::is_abstract_model(PROVIDER_OPENHUMAN, &effective_model)
+ && crate::inference::provider_model::is_abstract_model_for(
+ extensions,
+ PROVIDER_OPENHUMAN,
+ &effective_model,
+ )
{
if let Some(default_model) = entry.default_model.as_deref().map(str::trim).filter(|m| {
!m.is_empty()
- && !crate::inference::provider_model::is_abstract_model(PROVIDER_OPENHUMAN, m)
+ && !crate::inference::provider_model::is_abstract_model_for(
+ extensions,
+ PROVIDER_OPENHUMAN,
+ m,
+ )
}) {
log::info!(
"[providers][chat-factory] role={} slug={} remapping abstract model {} -> {}",
@@ -221,6 +243,23 @@ pub(super) fn try_create_cloud_slug_chat_model_with_native_tools(
config: &Config,
native_tool_calling: bool,
egress: &EgressLedger,
+) -> OptionalChatModelResult {
+ let extensions = crate::core::runtime::CoreContext::current_extensions();
+ try_create_cloud_slug_chat_model_with_native_tools_for(
+ role,
+ config,
+ native_tool_calling,
+ egress,
+ extensions.as_deref(),
+ )
+}
+
+pub(super) fn try_create_cloud_slug_chat_model_with_native_tools_for(
+ role: &str,
+ config: &Config,
+ native_tool_calling: bool,
+ egress: &EgressLedger,
+ extensions: Option<&crate::core::slots::Extensions>,
) -> OptionalChatModelResult {
// Resolve the role's provider string, expanding the empty / "cloud" sentinel
// to the primary cloud target.
@@ -228,12 +267,13 @@ pub(super) fn try_create_cloud_slug_chat_model_with_native_tools(
if resolved.trim().is_empty() || resolved.trim() == "cloud" {
resolved = resolve_primary_cloud_provider_string(config);
}
- try_create_cloud_slug_chat_model_from_string_with_native_tools(
+ try_create_cloud_slug_chat_model_from_string_with_native_tools_for(
role,
&resolved,
config,
native_tool_calling,
egress,
+ extensions,
)
}
@@ -254,6 +294,25 @@ pub(super) fn try_create_cloud_slug_chat_model_from_string_with_native_tools(
config: &Config,
native_tool_calling: bool,
egress: &EgressLedger,
+) -> OptionalChatModelResult {
+ let extensions = crate::core::runtime::CoreContext::current_extensions();
+ try_create_cloud_slug_chat_model_from_string_with_native_tools_for(
+ role,
+ provider,
+ config,
+ native_tool_calling,
+ egress,
+ extensions.as_deref(),
+ )
+}
+
+pub(super) fn try_create_cloud_slug_chat_model_from_string_with_native_tools_for(
+ role: &str,
+ provider: &str,
+ config: &Config,
+ native_tool_calling: bool,
+ egress: &EgressLedger,
+ extensions: Option<&crate::core::slots::Extensions>,
) -> OptionalChatModelResult {
let p = provider.trim().to_string();
@@ -294,7 +353,7 @@ pub(super) fn try_create_cloud_slug_chat_model_from_string_with_native_tools(
effective_model,
key,
codex,
- } = match resolve_cloud_slug(role, &slug, &raw_model, config) {
+ } = match resolve_cloud_slug_for(role, &slug, &raw_model, config, extensions) {
Ok(r) => r,
Err(e) => return Some(Err(e)),
};
@@ -333,14 +392,15 @@ pub(super) fn try_create_cloud_slug_chat_model_from_string_with_native_tools(
);
}
AuthStyle::Anthropic | AuthStyle::None => {}
- style if crate::config::schema::cloud_providers::is_hosted_auth_style(style) => {
+ style if crate::config::schema::cloud_providers::is_hosted_auth_style_for(extensions, style) => {
#[cfg(not(test))]
if let Err(error) = verify_backend_session_active(config) {
return Some(Err(error));
}
let model_override =
(!effective_model.trim().is_empty()).then_some(effective_model.as_str());
- return Some(managed_chat_model(
+ return Some(managed_chat_model_for(
+ extensions,
role,
config,
model_override,
diff --git a/crates/openhuman-core/src/inference/provider/factory/discovery.rs b/crates/openhuman-core/src/inference/provider/factory/discovery.rs
index e08d624cca..06515a43a4 100644
--- a/crates/openhuman-core/src/inference/provider/factory/discovery.rs
+++ b/crates/openhuman-core/src/inference/provider/factory/discovery.rs
@@ -22,10 +22,21 @@ pub(crate) fn model_limits_request(
provider: &str,
model: &str,
config: &Config,
+) -> Option<DiscoveryRequest> {
+ let extensions = crate::core::runtime::CoreContext::current_extensions();
+ model_limits_request_for(role, provider, model, config, extensions.as_deref())
+}
+
+pub(crate) fn model_limits_request_for(
+ role: &str,
+ provider: &str,
+ model: &str,
+ config: &Config,
+ extensions: Option<&crate::core::slots::Extensions>,
) -> Option<DiscoveryRequest> {
let p = provider.trim();
if p.is_empty() || p == "cloud" || p == PROVIDER_OPENHUMAN {
- return managed_limits_request(model, config);
+ return managed_limits_request(model, config, extensions);
}
if p == BYOK_INCOMPLETE_SENTINEL
|| p == CLAUDE_AGENT_SDK_PROVIDER
@@ -39,18 +50,18 @@ pub(crate) fn model_limits_request(
let (slug, raw) = p.split_once(':')?;
let slug = slug.trim();
let entry = config.cloud_providers.iter().find(|e| e.slug == slug)?;
- if crate::config::schema::cloud_providers::is_hosted_auth_style(entry.auth_style) {
+ if crate::config::schema::cloud_providers::is_hosted_auth_style_for(extensions, entry.auth_style) {
let (raw_model, _) = split_model_and_temperature(raw);
let model = if raw_model.trim().is_empty() {
model
} else {
raw_model.trim()
};
- return managed_limits_request(model, config);
+ return managed_limits_request(model, config, extensions);
}
let (raw_model, _) = split_model_and_temperature(raw);
- let resolution = match resolve_cloud_slug(role, slug, &raw_model, config) {
+ let resolution = match resolve_cloud_slug_for(role, slug, &raw_model, config, extensions) {
Ok(resolution) => resolution,
Err(error) => {
log::debug!(
@@ -85,9 +96,13 @@ pub(crate) fn model_limits_request(
/// The managed backend's catalogue listing request, from the registered
/// catalog's limits function (`inference::registry`). With none registered the
/// managed route has no listing to ask.
-fn managed_limits_request(model: &str, config: &Config) -> Option<DiscoveryRequest> {
- let Some(limits) =
- crate::inference::registry::catalog(PROVIDER_OPENHUMAN).and_then(|entry| entry.limits)
+fn managed_limits_request(
+ model: &str,
+ config: &Config,
+ extensions: Option<&crate::core::slots::Extensions>,
+) -> Option<DiscoveryRequest> {
+ let Some(limits) = crate::inference::registry::catalog_for(extensions, PROVIDER_OPENHUMAN)
+ .and_then(|entry| entry.limits)
else {
log::debug!("[model_limits] no managed catalog registered");
return None;
diff --git a/crates/openhuman-core/src/inference/provider_model.rs b/crates/openhuman-core/src/inference/provider_model.rs
index 2f080eb30c..cc2113b3d7 100644
--- a/crates/openhuman-core/src/inference/provider_model.rs
+++ b/crates/openhuman-core/src/inference/provider_model.rs
@@ -34,6 +34,21 @@ pub fn is_abstract_model(provider_id: &str, model: &str) -> bool {
|| crate::inference::registry::provider_model_is_alias(provider_id, model)
}
+/// Context-selected counterpart to [`is_abstract_model`]. Generic `hint:*`
+/// markers remain abstract even when no host provider is registered.
+pub(crate) fn is_abstract_model_for(
+ extensions: Option<&crate::core::slots::Extensions>,
+ provider_id: &str,
+ model: &str,
+) -> bool {
+ model.trim().starts_with("hint:")
+ || crate::inference::registry::provider_model_is_alias_for(
+ extensions,
+ provider_id,
+ model,
+ )
+}
+
/// Resolve a model hint according to the configured provider route and the
/// route's registered provider model policy.
pub fn resolve_provider_model_hint(provider_id: &str, hint: &str, config: &Config) -> String {
diff --git a/crates/openhuman-core/src/inference/registry.rs b/crates/openhuman-core/src/inference/registry.rs
index 148531d57c..a5ae93e45c 100644
--- a/crates/openhuman-core/src/inference/registry.rs
+++ b/crates/openhuman-core/src/inference/registry.rs
@@ -246,6 +246,16 @@ pub fn provider_model_is_alias(provider_id: &str, model: &str) -> bool {
provider_model_hooks(provider_id).is_some_and(|hooks| (hooks.is_alias)(model))
}
+pub(crate) fn provider_model_is_alias_for(
+ extensions: Option<&Extensions>,
+ provider_id: &str,
+ model: &str,
+) -> bool {
+ chat_model_for(extensions, provider_id)
+ .and_then(|entry| entry.models)
+ .is_some_and(|hooks| (hooks.is_alias)(model))
+}
+
/// Whether the provider regards `model` as a concrete id for pass-through.
pub fn provider_model_is_raw(provider_id: &str, model: &str) -> bool {
let model = model.trim();
@@ -473,6 +483,13 @@ pub(crate) fn catalog(provider_id: &str) -> Option<CatalogEntry> {
CATALOGS.get(provider_id)
}
+pub(crate) fn catalog_for(
+ extensions: Option<&Extensions>,
+ provider_id: &str,
+) -> Option<CatalogEntry> {
+ CATALOGS.get_for(extensions, provider_id)
+}
+
#[cfg(test)]
#[path = "registry_tests.rs"]
mod tests;