pub struct ContextWindow {
pub max_tokens: usize,
pub system_tokens: Vec<u32>,
pub conversation: Vec<u32>,
pub strategy: TruncationStrategy,
}Expand description
A fixed-capacity token window with configurable truncation.
The window stores a protected system prompt (never truncated) and
a mutable conversation segment. Together they must fit within
max_tokens.
Fields§
§max_tokens: usizeMaximum total token count (system + conversation).
system_tokens: Vec<u32>Tokens belonging to the system prompt — never truncated.
conversation: Vec<u32>Accumulated conversation tokens.
strategy: TruncationStrategyHow to truncate when the window is full.
Implementations§
Source§impl ContextWindow
impl ContextWindow
Sourcepub fn new(max_tokens: usize, strategy: TruncationStrategy) -> Self
pub fn new(max_tokens: usize, strategy: TruncationStrategy) -> Self
Create a new empty context window.
Sourcepub fn set_system_prompt(
&mut self,
tokens: Vec<u32>,
) -> Result<(), ContextError>
pub fn set_system_prompt( &mut self, tokens: Vec<u32>, ) -> Result<(), ContextError>
Set the system prompt tokens.
Returns an error if the system prompt alone exceeds max_tokens.
Sourcepub fn append(&mut self, tokens: &[u32]) -> usize
pub fn append(&mut self, tokens: &[u32]) -> usize
Append tokens to the conversation.
If the new tokens cause the window to overflow, truncation is applied before appending as much of the new tokens as will fit.
Returns the number of tokens actually appended.
Sourcepub fn truncate_to_fit(&mut self) -> usize
pub fn truncate_to_fit(&mut self) -> usize
Truncate the conversation segment to make the total fit within max_tokens.
Applies the configured TruncationStrategy.
Returns the number of tokens removed from the conversation.
Sourcepub fn tokens(&self) -> Vec<u32>
pub fn tokens(&self) -> Vec<u32>
Concatenate system tokens and conversation tokens into a single flat vector.
The result is always within max_tokens.
Sourcepub fn remaining_capacity(&self) -> usize
pub fn remaining_capacity(&self) -> usize
Number of additional tokens that can be appended before truncation.
Sourcepub fn is_at_limit(&self) -> bool
pub fn is_at_limit(&self) -> bool
Returns true if the window is at or beyond its maximum capacity.
Sourcepub fn clear_conversation(&mut self)
pub fn clear_conversation(&mut self)
Clear all conversation tokens (system prompt is preserved).
Sourcepub fn utilization(&self) -> f32
pub fn utilization(&self) -> f32
Fraction of max_tokens currently in use: len / max_tokens.
Returns 0.0 if max_tokens is zero.
Auto Trait Implementations§
impl Freeze for ContextWindow
impl RefUnwindSafe for ContextWindow
impl Send for ContextWindow
impl Sync for ContextWindow
impl Unpin for ContextWindow
impl UnsafeUnpin for ContextWindow
impl UnwindSafe for ContextWindow
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more