pub struct MessageCompressor;Expand description
Message compressor for reducing context size while preserving important content.
Implementations§
Source§impl MessageCompressor
impl MessageCompressor
Sourcepub fn compress_code_block(code: &str, max_lines: usize) -> String
pub fn compress_code_block(code: &str, max_lines: usize) -> String
Compress a code block by keeping head and tail lines.
When a code block exceeds max_lines, this function keeps approximately
60% of lines from the head and 40% from the tail, with an omission marker
in between.
§Arguments
code- The code content to compressmax_lines- Maximum number of lines to keep (excluding omission marker)
§Returns
The compressed code string. If the code is already within limits, returns the original code unchanged.
§Example
let code = (0..100).map(|i| format!("line {}", i)).collect::<Vec<_>>().join("\n");
let compressed = MessageCompressor::compress_code_block(&code, 50);
// Result: first 30 lines + omission marker + last 20 linesSourcepub fn extract_code_blocks(text: &str) -> Vec<CodeBlock>
pub fn extract_code_blocks(text: &str) -> Vec<CodeBlock>
Sourcepub fn compress_code_blocks_in_text(text: &str, max_lines: usize) -> String
pub fn compress_code_blocks_in_text(text: &str, max_lines: usize) -> String
Sourcepub fn compress_tool_output(content: &str, max_chars: usize) -> String
pub fn compress_tool_output(content: &str, max_chars: usize) -> String
Compress tool output by truncating with head/tail preservation.
When tool output exceeds max_chars, keeps approximately 70% from
the head and 30% from the tail, with an omission marker in between.
§Arguments
content- The tool output content to compressmax_chars- Maximum characters to keep (excluding omission marker)
§Returns
The compressed content string.
Sourcepub fn extract_file_references(text: &str) -> Vec<String>
pub fn extract_file_references(text: &str) -> Vec<String>
Sourcepub fn compress_message(
message: &Message,
config: &CompressionConfig,
) -> Message
pub fn compress_message( message: &Message, config: &CompressionConfig, ) -> Message
Sourcepub fn truncate_messages(
messages: &[Message],
max_tokens: usize,
keep_first: usize,
keep_last: usize,
) -> Vec<Message>
pub fn truncate_messages( messages: &[Message], max_tokens: usize, keep_first: usize, keep_last: usize, ) -> Vec<Message>
Intelligently truncate a message array to fit within token limits.
Keeps the first N and last M messages, removing middle messages to fit within the token budget.
§Arguments
messages- The messages to truncatemax_tokens- Maximum total tokens allowedkeep_first- Number of messages to keep from the startkeep_last- Number of messages to keep from the end
§Returns
A truncated vector of messages fitting within the token limit.
Sourcepub fn calculate_compression_result(
original: &Message,
compressed: &Message,
) -> CompressionResult
pub fn calculate_compression_result( original: &Message, compressed: &Message, ) -> CompressionResult
Calculate compression result for a message.
Sourcepub fn compress_with_pruning(
messages: &[Message],
usage_ratio: f64,
compression_config: &CompressionConfig,
pruning_config: &PruningConfig,
) -> Vec<Message>
pub fn compress_with_pruning( messages: &[Message], usage_ratio: f64, compression_config: &CompressionConfig, pruning_config: &PruningConfig, ) -> Vec<Message>
Apply progressive pruning to messages based on context usage.
This method combines standard compression with progressive pruning to manage context size more effectively.
§Arguments
messages- The messages to processusage_ratio- Current context usage ratio (0.0-1.0)compression_config- Configuration for standard compressionpruning_config- Configuration for progressive pruning
§Returns
A new vector of messages with both compression and pruning applied.
§Example
let messages = vec![...];
let compressed = MessageCompressor::compress_with_pruning(
&messages,
0.4, // 40% context usage
&CompressionConfig::default(),
&PruningConfig::default(),
);Sourcepub fn compress_tool_output_with_pruning(
content: &str,
max_chars: usize,
usage_ratio: f64,
pruning_config: &PruningConfig,
) -> String
pub fn compress_tool_output_with_pruning( content: &str, max_chars: usize, usage_ratio: f64, pruning_config: &PruningConfig, ) -> String
Compress tool output with progressive pruning support.
This method extends the standard tool output compression with progressive pruning based on context usage.
§Arguments
content- The tool output content to compressmax_chars- Maximum characters for standard compressionusage_ratio- Current context usage ratio (0.0-1.0)pruning_config- Configuration for progressive pruning
§Returns
The compressed/pruned content string.
Auto Trait Implementations§
impl Freeze for MessageCompressor
impl RefUnwindSafe for MessageCompressor
impl Send for MessageCompressor
impl Sync for MessageCompressor
impl Unpin for MessageCompressor
impl UnsafeUnpin for MessageCompressor
impl UnwindSafe for MessageCompressor
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> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.