#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum WindowSource {
Served,
Cached,
Configured,
Community,
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct WindowFacts {
pub(crate) kind: newt_core::BackendKind,
pub(crate) served: Option<u32>,
pub(crate) cached_window: Option<u32>,
pub(crate) cached_hard_window: Option<u32>,
pub(crate) cached_safe_context: Option<u32>,
pub(crate) cached_max_ok_input: Option<u32>,
pub(crate) configured_window: Option<u32>,
pub(crate) community_window: Option<u32>,
pub(crate) recovered_window: Option<u32>,
pub(crate) input_ceiling_pct: u32,
pub(crate) configured_num_ctx: Option<u32>,
pub(crate) context_size_override: Option<u32>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ContextWindow {
pub(crate) full_window: Option<u32>,
pub(crate) window_source: Option<WindowSource>,
pub(crate) recovered_hard_window: Option<u32>,
pub(crate) safe_context: Option<u32>,
pub(crate) safe_context_source: Option<LimitSource>,
pub(crate) max_ok_input: Option<u32>,
pub(crate) max_ok_input_source: Option<LimitSource>,
pub(crate) num_ctx: Option<u32>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LimitSource {
PercentOfWindow,
Cached,
ConfiguredWindow,
SessionOverride,
}
pub(crate) fn input_ceiling_pct(cfg: &newt_core::Config) -> u32 {
newt_core::config::normalize_input_ceiling_pct(cfg.context.as_ref().map_or_else(
|| newt_core::config::ContextConfig::default().input_ceiling_pct,
|c| c.input_ceiling_pct,
))
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn facts_for(
cfg: &newt_core::Config,
kind: newt_core::BackendKind,
model: &str,
served: Option<u32>,
cached: &crate::probe::CapabilityEntry,
community: &newt_core::tuning::CommunityTunings,
recovered_window: Option<u32>,
context_size_override: Option<u32>,
) -> WindowFacts {
let tune = cfg.find_model_tuning(model);
WindowFacts {
kind,
served,
cached_window: cached.context_window,
cached_hard_window: cached.hard_context_window,
cached_safe_context: cached.safe_context,
cached_max_ok_input: cached.max_ok_input,
configured_window: tune.and_then(|t| t.context_window),
community_window: community.find(model).and_then(|p| p.context_window),
recovered_window,
input_ceiling_pct: input_ceiling_pct(cfg),
configured_num_ctx: tune.and_then(|t| t.num_ctx).or_else(|| crate::num_ctx(cfg)),
context_size_override,
}
}
pub(crate) fn resolve(f: WindowFacts) -> ContextWindow {
let (requested_full_window, window_source) = selected_model_context_window(
f.served.or(f.cached_window).map(|w| {
let source = if f.served.is_some() {
WindowSource::Served
} else {
WindowSource::Cached
};
(w, source)
}),
f.configured_window.map(|w| (w, WindowSource::Configured)),
f.community_window.map(|w| (w, WindowSource::Community)),
)
.unzip();
let recovered_hard_window =
cap_context_window_by_recovery(f.recovered_window, f.cached_hard_window);
let full_window = cap_context_window_by_recovery(requested_full_window, recovered_hard_window);
let ceiling = |w| {
(
newt_core::config::input_percentage_ceiling(w, f.input_ceiling_pct),
LimitSource::PercentOfWindow,
)
};
let cached = f.cached_safe_context.map(|n| (n, LimitSource::Cached));
let safe_context = if f.kind == newt_core::BackendKind::Openai {
full_window.map(ceiling).or(cached)
} else {
recovered_hard_window
.or(f.served)
.map(ceiling)
.or(cached)
.or(f
.configured_window
.map(|n| (n, LimitSource::ConfiguredWindow)))
};
let (safe_context, max_ok_input) = match f.context_size_override {
Some(n) => (
Some((n, LimitSource::SessionOverride)),
Some((n, LimitSource::SessionOverride)),
),
None => (
safe_context,
f.cached_max_ok_input.map(|n| (n, LimitSource::Cached)),
),
};
let (safe_context, safe_context_source) = safe_context.unzip();
let (max_ok_input, max_ok_input_source) = max_ok_input.unzip();
let requested_num_ctx = f
.configured_num_ctx
.or_else(|| context_window_for_core(f.kind, full_window, safe_context));
ContextWindow {
full_window,
window_source,
recovered_hard_window,
safe_context,
safe_context_source,
max_ok_input,
max_ok_input_source,
num_ctx: cap_context_window_by_recovery(requested_num_ctx, recovered_hard_window),
}
}
pub(crate) fn context_window_for_core(
kind: newt_core::BackendKind,
full_context_window: Option<u32>,
safe_context: Option<u32>,
) -> Option<u32> {
match kind {
newt_core::BackendKind::Openai | newt_core::BackendKind::Anthropic => full_context_window,
newt_core::BackendKind::Ollama | newt_core::BackendKind::Embedded => safe_context,
}
}
pub(crate) fn selected_model_context_window<T>(
live: Option<T>,
configured: Option<T>,
community: Option<T>,
) -> Option<T> {
live.or(configured).or(community)
}
pub(crate) fn cap_context_window_by_recovery(
requested: Option<u32>,
recovered_hard_window: Option<u32>,
) -> Option<u32> {
match (requested, recovered_hard_window) {
(Some(requested), Some(recovered)) => Some(requested.min(recovered)),
(requested, recovered) => requested.or(recovered),
}
}
#[cfg(test)]
#[path = "context_window_tests.rs"]
mod tests;