use super::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ContextBudgetPhase {
Initial,
Continuation,
}
#[derive(Debug)]
pub(crate) struct ContextBudgetError {
pub(super) phase: ContextBudgetPhase,
pub(super) estimated_tokens: usize,
pub(super) threshold_tokens: usize,
pub(super) provider_rejected: bool,
pub(super) partial_output: Option<AgentRunOutput>,
pub(super) message: String,
}
impl ContextBudgetError {
pub(super) fn new(
phase: ContextBudgetPhase,
estimated_tokens: usize,
threshold_tokens: usize,
max_tokens: usize,
reserve_tokens: usize,
) -> Self {
Self {
phase,
estimated_tokens,
threshold_tokens,
provider_rejected: false,
partial_output: None,
message: format!(
"full session history request is estimated at {estimated_tokens} tokens, exceeding threshold {threshold_tokens} tokens (max_tokens={max_tokens}, reserve_tokens={reserve_tokens}); no history was omitted; run /compact, choose a larger compaction model, or start /new"
),
}
}
pub(super) fn with_partial_output(mut self, output: AgentRunOutput) -> Self {
self.partial_output = Some(output);
self
}
pub(crate) fn phase(&self) -> ContextBudgetPhase {
self.phase
}
pub(crate) fn estimated_tokens(&self) -> usize {
self.estimated_tokens
}
pub(crate) fn threshold_tokens(&self) -> usize {
self.threshold_tokens
}
pub(crate) fn provider_rejected(&self) -> bool {
self.provider_rejected
}
pub(crate) fn into_partial_output(self) -> Option<AgentRunOutput> {
self.partial_output
}
}
impl std::fmt::Display for ContextBudgetError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "{}", self.message)
}
}
impl std::error::Error for ContextBudgetError {}