pub struct Compactor { /* private fields */ }Expand description
Compactor - applies compaction strategies to segments
Implementations§
Source§impl Compactor
impl Compactor
Sourcepub fn new(strategy: CompactionStrategy) -> Self
pub fn new(strategy: CompactionStrategy) -> Self
Create a new compactor with the given strategy
Sourcepub fn sliding_window(target_tokens: usize, window_size: usize) -> Self
pub fn sliding_window(target_tokens: usize, window_size: usize) -> Self
Create a sliding window compactor
Sourcepub fn strategy(&self) -> &CompactionStrategy
pub fn strategy(&self) -> &CompactionStrategy
Get the strategy
Sourcepub fn compact_truncate(
&self,
segments: &mut Vec<ContextSegment>,
current_tokens: usize,
) -> Result<usize, CompactionError>
pub fn compact_truncate( &self, segments: &mut Vec<ContextSegment>, current_tokens: usize, ) -> Result<usize, CompactionError>
Compact segments using truncation strategy
Removes oldest segments (lowest priority first) until target is reached.
Sourcepub fn compact_sliding_window(
&self,
segments: &mut Vec<ContextSegment>,
) -> Result<usize, CompactionError>
pub fn compact_sliding_window( &self, segments: &mut Vec<ContextSegment>, ) -> Result<usize, CompactionError>
Compact using sliding window strategy
Keeps only the most recent N messages in the history.
Sourcepub fn summarize(target_tokens: usize, summary_max_tokens: usize) -> Self
pub fn summarize(target_tokens: usize, summary_max_tokens: usize) -> Self
Create a summarize compactor
Sourcepub fn compact_summarize(
&self,
segments: &mut Vec<ContextSegment>,
current_tokens: usize,
) -> Result<usize, CompactionError>
pub fn compact_summarize( &self, segments: &mut Vec<ContextSegment>, current_tokens: usize, ) -> Result<usize, CompactionError>
Compact using summarization strategy
This performs extractive summarization by:
- Identifying compactible segments (History, ToolResults)
- Extracting key sentences from each segment
- Truncating to fit within the summary token budget
Note: For true abstractive summarization (using an LLM), integrate with an external summarization service. This implementation provides a local extractive approach that doesn’t require external API calls.
Sourcepub fn compact_extract_key_points(
&self,
_segments: &mut Vec<ContextSegment>,
_current_tokens: usize,
) -> Result<usize, CompactionError>
pub fn compact_extract_key_points( &self, _segments: &mut Vec<ContextSegment>, _current_tokens: usize, ) -> Result<usize, CompactionError>
Compact using key points extraction strategy (not yet implemented)
§Future Implementation
This strategy would:
- Identify key decision points, outcomes, and learnings
- Extract structured bullet points from conversation
- Preserve causal chains and reasoning
Requires integration with an LLM for semantic understanding.
Sourcepub fn compact_importance_weighted(
&self,
_segments: &mut Vec<ContextSegment>,
_current_tokens: usize,
) -> Result<usize, CompactionError>
pub fn compact_importance_weighted( &self, _segments: &mut Vec<ContextSegment>, _current_tokens: usize, ) -> Result<usize, CompactionError>
Compact using importance-weighted strategy (not yet implemented)
§Future Implementation
This strategy would:
- Score each segment based on semantic importance
- Use embedding similarity to current task
- Weight by recency, reference count, and explicit importance markers
- Remove lowest-scored segments until target is reached
Requires embedding model integration for semantic scoring.
Sourcepub fn compact_hybrid(
&self,
_segments: &mut Vec<ContextSegment>,
_current_tokens: usize,
) -> Result<usize, CompactionError>
pub fn compact_hybrid( &self, _segments: &mut Vec<ContextSegment>, _current_tokens: usize, ) -> Result<usize, CompactionError>
Compact using hybrid strategy (not yet implemented)
§Future Implementation
This strategy would combine multiple approaches:
- First pass: Remove lowest-importance segments (truncate)
- Second pass: Summarize remaining compressible content
- Third pass: Apply sliding window to history if needed
Requires all component strategies to be implemented.