Skip to content

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

refactor(inference): introduce context-aware context window resolution

Steven Enamakel454220b07d0d
diff --git a/crates/openhuman-core/src/inference/context_window.rs b/crates/openhuman-core/src/inference/context_window.rs
index ca6b13850f..a452596fb1 100644
--- a/crates/openhuman-core/src/inference/context_window.rs
+++ b/crates/openhuman-core/src/inference/context_window.rs
@@ -156,17 +156,43 @@ pub(crate) async fn resolve_context_window(
model: &str,
config: &Config,
) -> Option<u64> {
+ resolve_context_window_inner(role, provider, model, config, None)
+ .await
+ .window
+}
+
+/// Resolve a context window using provider metadata from an explicitly
+/// selected runtime context.
+pub(crate) async fn resolve_context_window_in(
+ role: &str,
+ provider: &str,
+ model: &str,
+ config: &Config,
+ extensions: Option<&crate::core::slots::Extensions>,
+) -> Option<u64> {
+ resolve_context_window_inner(role, provider, model, config, Some(extensions))
+ .await
+ .window
+}
+
+async fn resolve_context_window_inner(
+ role: &str,
+ provider: &str,
+ model: &str,
+ config: &Config,
+ extensions: Option<Option<&crate::core::slots::Extensions>>,
+) -> ResolvedWindow {
let fetcher = default_fetcher(config, provider);
- resolve_context_window_with(
+ resolve_context_window_with_inner(
fetcher.as_ref(),
model_limits_cache(),
role,
provider,
model,
config,
+ extensions,
)
.await
- .window
}
/// [`resolve_context_window`] with an explicit fetcher and cache.
@@ -177,6 +203,18 @@ pub(crate) async fn resolve_context_window_with(
provider: &str,
model: &str,
config: &Config,
+) -> ResolvedWindow {
+ resolve_context_window_with_inner(fetcher, cache, role, provider, model, config, None).await
+}
+
+async fn resolve_context_window_with_inner(
+ fetcher: &dyn ModelListingFetcher,
+ cache: &ModelLimitsCache,
+ role: &str,
+ provider: &str,
+ model: &str,
+ config: &Config,
+ extensions: Option<Option<&crate::core::slots::Extensions>>,
) -> ResolvedWindow {
let local_kind = tinyinference_local::profile::kind_from_provider_string(provider);
let request = match local_kind {
@@ -188,7 +226,12 @@ pub(crate) async fn resolve_context_window_with(
}
Some(_) => None,
};
- let static_window = super::model_context::static_context_window_for_model(model);
+ let static_window = match extensions {
+ Some(extensions) => {
+ super::model_context::static_context_window_for_model_in(extensions, model)
+ }
+ None => super::model_context::static_context_window_for_model(model),
+ };
let local_window = local_kind.and_then(|kind| {
tinyinference_local::profile::context_window_with_local_fallback(
model,
diff --git a/crates/openhuman-core/src/inference/model_context.rs b/crates/openhuman-core/src/inference/model_context.rs
index 7967023c15..2d3a6d47b5 100644
--- a/crates/openhuman-core/src/inference/model_context.rs
+++ b/crates/openhuman-core/src/inference/model_context.rs
@@ -33,15 +33,41 @@ pub(crate) fn context_window_for_route(
/// then the generic id-pattern hints. These never override a provider-reported
/// window; [`crate::inference::context_window`] consults them last.
pub(crate) fn static_context_window_for_model(model: &str) -> Option<u64> {
+ static_context_window_for_model_with_provider_window(
+ model,
+ crate::inference::registry::provider_model_context_window(
+ crate::inference::provider::factory::PROVIDER_OPENHUMAN,
+ model.trim(),
+ ),
+ )
+}
+
+/// Context-aware counterpart used when a dispatch carries its own provider
+/// registry. An empty context does not borrow model metadata from ambient work.
+pub(crate) fn static_context_window_for_model_in(
+ extensions: Option<&crate::core::slots::Extensions>,
+ model: &str,
+) -> Option<u64> {
+ static_context_window_for_model_with_provider_window(
+ model,
+ crate::inference::registry::provider_model_context_window_for(
+ extensions,
+ crate::inference::provider::factory::PROVIDER_OPENHUMAN,
+ model.trim(),
+ ),
+ )
+}
+
+fn static_context_window_for_model_with_provider_window(
+ model: &str,
+ provider_window: Option<u64>,
+) -> Option<u64> {
let normalized = model.trim();
if normalized.is_empty() {
return None;
}
- if let Some(window) = crate::inference::registry::provider_model_context_window(
- crate::inference::provider::factory::PROVIDER_OPENHUMAN,
- normalized,
- ) {
+ if let Some(window) = provider_window {
return Some(window);
}