Skip to content

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

feat(inference): add context-aware model resolution

Steven Enamakelee788c1a07a4
diff --git a/crates/openhuman-core/src/core/slots.rs b/crates/openhuman-core/src/core/slots.rs
index 8b93a5b169..1efca93d32 100644
--- a/crates/openhuman-core/src/core/slots.rs
+++ b/crates/openhuman-core/src/core/slots.rs
@@ -290,6 +290,16 @@ impl<V: Clone + Send + Sync + 'static> HookSlot<V> {
.or_else(|| self.with_global(|table| table.get(id)))
}
+ /// The value for `id` in an explicitly selected context, then process
+ /// registration. `None` deliberately means process-only and never consults
+ /// an unrelated ambient task context.
+ pub fn get_for(&self, extensions: Option<&Extensions>, id: &str) -> Option<V> {
+ extensions
+ .and_then(|extensions| extensions.existing::<V>(self.name))
+ .and_then(|table| table.get(id))
+ .or_else(|| self.with_global(|table| table.get(id)))
+ }
+
/// Every registration in order: process-wide first, a context entry
/// replacing a process entry with the same id, context-only ids last.
pub fn entries(&self) -> Vec<(String, V)> {
diff --git a/crates/openhuman-core/src/inference/provider/factory.rs b/crates/openhuman-core/src/inference/provider/factory.rs
index 5bb2f5bc85..51038cc5e1 100644
--- a/crates/openhuman-core/src/inference/provider/factory.rs
+++ b/crates/openhuman-core/src/inference/provider/factory.rs
@@ -85,6 +85,7 @@ use local_runtime::{
use managed_backend::managed_chat_model;
pub(crate) use managed_backend::{
make_openhuman_backend_model_for_options, make_openhuman_backend_model_for_thread,
+ make_openhuman_backend_model_for_thread_in,
};
use subprocess_providers::{
prepare_claude_agent_sdk_chat_model, try_create_claude_agent_sdk_chat_model,
diff --git a/crates/openhuman-core/src/inference/provider/factory/managed_backend.rs b/crates/openhuman-core/src/inference/provider/factory/managed_backend.rs
index b2efb36232..79f3637c3f 100644
--- a/crates/openhuman-core/src/inference/provider/factory/managed_backend.rs
+++ b/crates/openhuman-core/src/inference/provider/factory/managed_backend.rs
@@ -35,9 +35,22 @@ pub(super) fn managed_chat_model(
native_tool_calling: bool,
thread_id: Option<&str>,
egress: &EgressLedger,
+) -> anyhow::Result<ManagedChatModel> {
+ managed_chat_model_for(None, role, config, model_override, default_model, native_tool_calling, thread_id, egress)
+}
+
+pub(super) fn managed_chat_model_for(
+ extensions: Option<&crate::core::slots::Extensions>,
+ role: &str,
+ config: &Config,
+ model_override: Option<&str>,
+ default_model: Option<&str>,
+ native_tool_calling: bool,
+ thread_id: Option<&str>,
+ egress: &EgressLedger,
) -> anyhow::Result<ManagedChatModel> {
enforce_local_only_inference(role, PROVIDER_OPENHUMAN)?;
- let Some(entry) = crate::inference::registry::chat_model(PROVIDER_OPENHUMAN) else {
+ let Some(entry) = crate::inference::registry::chat_model_for(extensions, PROVIDER_OPENHUMAN) else {
log::debug!("[providers][chat-factory] role={role} managed provider is not registered");
return Err(managed_unavailable_error());
};
@@ -90,6 +103,28 @@ pub(crate) fn make_openhuman_backend_model_for_thread(
)
}
+/// Explicit-context counterpart for turn construction.
+pub(crate) fn make_openhuman_backend_model_for_thread_in(
+ extensions: Option<&crate::core::slots::Extensions>,
+ role: &str,
+ config: &Config,
+ model: Option<&str>,
+ native_tool_calling: bool,
+ thread_id: Option<&str>,
+ egress: &EgressLedger,
+) -> anyhow::Result<ManagedChatModel> {
+ managed_chat_model_for(
+ extensions,
+ role,
+ config,
+ model,
+ None,
+ native_tool_calling,
+ thread_id,
+ egress,
+ )
+}
+
/// The managed backend as a crate-native host `ChatModel`.
pub(crate) fn make_openhuman_backend_model(
role: &str,
diff --git a/crates/openhuman-core/src/inference/registry.rs b/crates/openhuman-core/src/inference/registry.rs
index b49c20b832..148531d57c 100644
--- a/crates/openhuman-core/src/inference/registry.rs
+++ b/crates/openhuman-core/src/inference/registry.rs
@@ -200,6 +200,15 @@ pub(crate) fn chat_model(provider_id: &str) -> Option<ChatModelEntry> {
CHAT_MODELS.get(provider_id)
}
+/// The chat-model provider selected by an explicit context, falling back only
+/// to process registration when that context has no entry.
+pub(crate) fn chat_model_for(
+ extensions: Option<&Extensions>,
+ provider_id: &str,
+) -> Option<ChatModelEntry> {
+ CHAT_MODELS.get_for(extensions, provider_id)
+}
+
/// The registered provider's model hooks, if it has any.
pub(crate) fn provider_model_hooks(provider_id: &str) -> Option<ProviderModelHooks> {
chat_model(provider_id)?.models
@@ -223,6 +232,15 @@ pub fn provider_model_role(provider_id: &str, model: &str) -> Option<String> {
(hooks.role_for_model)(model)
}
+pub(crate) fn provider_model_role_for(
+ extensions: Option<&Extensions>,
+ provider_id: &str,
+ model: &str,
+) -> Option<String> {
+ let hooks = chat_model_for(extensions, provider_id)?.models?;
+ (hooks.role_for_model)(model)
+}
+
/// Whether the provider recognizes `model` as an abstract alias.
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))
@@ -241,6 +259,16 @@ pub fn provider_model_supports_vision(provider_id: &str, model: &str) -> bool {
provider_model_hooks(provider_id).is_some_and(|hooks| (hooks.supports_vision)(model))
}
+pub(crate) fn provider_model_supports_vision_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.supports_vision)(model))
+}
+
/// Provider-specific resolution of a role alias, configured default, or pin.
pub fn resolve_provider_model(
provider_id: &str,
@@ -258,6 +286,15 @@ pub fn provider_model_context_window(provider_id: &str, model: &str) -> Option<u
(hooks.context_window)(model)
}
+pub(crate) fn provider_model_context_window_for(
+ extensions: Option<&Extensions>,
+ provider_id: &str,
+ model: &str,
+) -> Option<u64> {
+ let hooks = chat_model_for(extensions, provider_id)?.models?;
+ (hooks.context_window)(model)
+}
+
/// Applies a provider-specific no-reasoning hint when the provider is installed.
pub fn provider_request_without_reasoning(
provider_id: &str,
diff --git a/crates/openhuman-core/src/inference/turn_models.rs b/crates/openhuman-core/src/inference/turn_models.rs
index 3f98367863..f1995f63ba 100644
--- a/crates/openhuman-core/src/inference/turn_models.rs
+++ b/crates/openhuman-core/src/inference/turn_models.rs
@@ -132,6 +132,10 @@ fn build_turn_models_crate(
// P2, PR #4812). The ledger collapses same-destination repeats to one
// disclosure per turn while still surfacing each distinct tier model.
let egress = crate::security::egress::EgressLedger::per_turn();
+ let resolved_context = ctx.resolved();
+ let context_extensions = resolved_context
+ .as_deref()
+ .map(crate::core::runtime::CoreContext::extensions);
// The primary honours an explicit provider-string override when the producer's
// effective provider differs from `provider_for_role(role)` (triage #1257).
@@ -146,7 +150,8 @@ fn build_turn_models_crate(
})
.unwrap_or_else(|| factory::resolves_to_managed_backend(role, config));
if managed {
- let (backend, _) = factory::make_openhuman_backend_model_for_thread(
+ let (backend, _) = factory::make_openhuman_backend_model_for_thread_in(
+ context_extensions,
role,
config,
Some(m),
@@ -209,10 +214,15 @@ fn build_turn_models_crate(
if tier == model {
continue;
}
- let tier_role = crate::inference::provider_model::role_for_provider_model(
+ let tier_role = crate::inference::registry::provider_model_role_for(
+ context_extensions,
factory::PROVIDER_OPENHUMAN,
tier,
- );
+ )
+ .unwrap_or_else(|| crate::inference::provider_model::role_for_provider_model(
+ factory::PROVIDER_OPENHUMAN,
+ tier,
+ ));
if let Some(custom) = ctx.host_overrides().and_then(|local| {
local
.role_models
@@ -231,7 +241,8 @@ fn build_turn_models_crate(
continue;
}
let route = if factory::resolves_to_managed_backend(tier_role, config) {
- factory::make_openhuman_backend_model_for_thread(
+ factory::make_openhuman_backend_model_for_thread_in(
+ context_extensions,
tier_role,
config,
Some(tier),