Skip to main content

TokenBudgetState

Enum TokenBudgetState 

Source
pub enum TokenBudgetState {
    Normal {
        percent_remaining: f64,
    },
    Warning {
        percent_remaining: f64,
    },
    Error {
        percent_remaining: f64,
    },
    Blocking,
}
Expand description

Token 用量状态 — 当前上下文占用量的分级警告。

UI 层用此枚举渲染进度条颜色和警告提示。 Agent loop 用此判断是否触发自动压缩。

§阈值对照(来源: claude-code)

|------ Normal ------|-- Warning --|-- Error --|-- Blocking --|
0%                 ~70%          ~85%        ~95%          100%

§Examples

use katu_core::compaction::TokenBudgetState;

let state = TokenBudgetState::from_usage(150_000, 200_000, 13_000);

Variants§

§

Normal

正常 — 充足余量。

Fields

§percent_remaining: f64

剩余可用百分比 (0.0 ~ 1.0)。

§

Warning

警告 — 接近阈值,UI 显示黄色提示。

Fields

§percent_remaining: f64
§

Error

危险 — 非常接近上限,UI 显示红色提示。

Fields

§percent_remaining: f64
§

Blocking

阻塞 — 已达到上限,应阻止新消息发送。

Implementations§

Source§

impl TokenBudgetState

Source

pub fn from_usage( used_tokens: u64, context_window: u64, auto_compact_buffer: u64, ) -> Self

根据当前 token 用量计算状态。

§Arguments
  • used_tokens: 当前已使用的 token 数
  • context_window: 模型 context window 大小
  • auto_compact_buffer: 自动压缩缓冲区大小(reserve_tokens)
§阈值计算
effective_window = context_window - summary_reserve (通常 20K)
auto_compact_threshold = effective_window - auto_compact_buffer
warning_threshold = auto_compact_threshold - 20K
error_threshold = effective_window - 20K
Source

pub fn should_auto_compact(&self) -> bool

是否应触发自动压缩。

Source

pub fn is_blocking(&self) -> bool

是否应阻止新消息发送。

Source

pub fn is_warning_or_worse(&self) -> bool

是否处于警告或更严重状态。

Trait Implementations§

Source§

impl Clone for TokenBudgetState

Source§

fn clone(&self) -> TokenBudgetState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TokenBudgetState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for TokenBudgetState

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for TokenBudgetState

Source§

fn eq(&self, other: &TokenBudgetState) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for TokenBudgetState

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for TokenBudgetState

Source§

impl StructuralPartialEq for TokenBudgetState

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,