pub struct ContextCompressor { /* private fields */ }Expand description
Adaptively compresses a set of memories to fit within a token budget.
Implementations§
Source§impl ContextCompressor
impl ContextCompressor
Sourcepub fn estimate_tokens(text: &str) -> usize
pub fn estimate_tokens(text: &str) -> usize
Estimate the token count for text using the heuristic chars / 4.
Sourcepub fn compress_light(text: &str) -> String
pub fn compress_light(text: &str) -> String
Light compression: remove filler phrases (case-insensitive), then drop standalone stopwords and collapse extra whitespace.
Sourcepub fn compress_medium(text: &str) -> String
pub fn compress_medium(text: &str) -> String
Medium compression: keep the first sentence of each paragraph and any sentence that contains a word starting with a capital letter (entity heuristic).
Sourcepub fn compress_heavy(text: &str) -> String
pub fn compress_heavy(text: &str) -> String
Heavy compression: extract lines that look like key facts — “Entity: something”, lines containing numbers, or dates.
Sourcepub fn compress_single(content: &str, level: CompressionLevel) -> String
pub fn compress_single(content: &str, level: CompressionLevel) -> String
Apply the specified compression level to content.
Sourcepub fn compress_for_context(
memories: &[MemoryInput],
budget: usize,
) -> Vec<CompressedEntry>
pub fn compress_for_context( memories: &[MemoryInput], budget: usize, ) -> Vec<CompressedEntry>
Adaptively compress a slice of memories to fit within budget tokens.
Algorithm:
- Sort memories by importance (descending) — most important get processed first and receive lighter compression.
- For each memory, try compression levels in order:
None → Light → Medium → Heavy. - The first level that fits the remaining budget is used.
- If even
Heavydoesn’t fit, the memory is skipped.
Returns the ordered list of successfully compressed entries.
Sourcepub fn budget(&self) -> TokenBudget
pub fn budget(&self) -> TokenBudget
Current budget state of this compressor instance.
Note: compress_for_context is a free function that does not mutate
the compressor. Call this after manually tracking usage with
estimate_tokens, or use it to inspect the configured budget.
Auto Trait Implementations§
impl Freeze for ContextCompressor
impl RefUnwindSafe for ContextCompressor
impl Send for ContextCompressor
impl Sync for ContextCompressor
impl Unpin for ContextCompressor
impl UnsafeUnpin for ContextCompressor
impl UnwindSafe for ContextCompressor
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