Expand description
Context compression for conversation history.
This module implements intelligent compression of conversation history to reduce token usage while preserving important information.
§Module Structure
- config: Compression configuration and bias settings
- types: Compression strategy, result, and segment types
- compressor: AI compressor and compression functions
- phase_detector: Conversation phase detection
- dependency: Message dependency tracking
- semantic: Semantic compression using AI summarization
- priority: Dynamic priority scoring for messages
- cache: Compression cache for performance optimization
- focus_point: AI-driven focus point management
- focus_extractor: AI-based focus extraction and classification
- focus_manager: Focus tracking and relevance calculation
- coherence: Semantic coherence detection
- progressive: Progressive compression strategy
- complexity: Complexity analysis for adaptive compression
- hierarchical: Hierarchical summarization strategies
Structs§
- AiCompressor
- AI-based compressor using a Provider.
- Cache
Config - Compression cache configuration.
- Cache
Entry - Cache entry for a compressed message.
- Cache
Stats - Statistics for the compression cache.
- Cached
Complexity - Cached complexity level
- Cached
Focus Prediction - Cached focus prediction
- Cached
Priority Score - Cached priority score with validity tracking
- Circuit
Breaker State - State for circuit breaker to prevent infinite retry loops.
- Classification
Result - 分类结果
- Coherence
Detector - Coherence detector for conversation segments.
- Complexity
Analyzer - Analyzes conversation complexity
- Complexity
Config - Complexity analyzer configuration
- Compression
Bias - Compression bias - controls what to prioritize during compression.
- Compression
Cache - Compression cache implementation.
- Compression
Config - Configuration for context compression.
- Compression
History Entry - Compression history entry for session metadata.
- Compression
Outcome - Result of compression with metadata.
- Compression
Pipeline - Compression pipeline that orchestrates all modules.
- Compression
Result - Result of a compression operation.
- Compression
Thresholds - Thresholds for content compression.
- Conversation
Focus - Represents the current focus of the conversation.
- Conversation
Summary - Summary of a conversation segment.
- Dependency
Builder - Builder for dependency graphs.
- Dependency
Graph - Graph of message dependencies for preserving conversation coherence.
- Extended
Cache Config - 扩展缓存配置
- Extended
Cache Stats - 扩展缓存统计
- Extended
Compression Cache - 扩展压缩缓存:支持多种类型的缓存
- Focus
Config - 聚焦点配置
- Focus
Extractor - AI 聚焦点提取器
- Focus
Feedback - 焦点反馈记录
- Focus
Manager - 聚焦点管理器
- Focus
Point - 聚焦点 - 由 AI 动态提取的核心关注点
- Focus
Tracker - Focus tracker that monitors conversation flow.
- Focus
Tracker Config - Focus tracker configuration
- Hardcode
Config - Unified configuration for all hardcoded values in compression system.
- Hierarchical
Config - Hierarchical summarizer configuration
- Hierarchical
Summarizer - Hierarchical summarizer
- Integrated
Long Context Processor - Integrated long context processor that orchestrates all modules.
- Issue
- Record of an issue and its resolution.
- KeyInfo
- Key information extracted from a message.
- Message
Dependency - Dependency relationship between ToolUse and ToolResult.
- Message
Range - 消息范围
- Optimized
Compressor - Optimized compressor with all enhancements including focus tracking.
- Phase
Detector - Detector for conversation phase.
- Phase
Weights - Weight configuration for scoring based on conversation phase.
- Priority
Factors - Factors that contribute to message priority.
- Priority
Score - Priority score for a message (0.0 to 1.0).
- Priority
Scorer - Dynamic priority scorer.
- Priority
Weights - Weights for different priority factors.
- Processed
Result - Result of processing long context.
- Processor
Config - Configuration for the integrated processor.
- Progressive
Compressor - Progressive compression controller.
- Progressive
Config - Configuration for progressive compression.
- Scored
Message - Message with its preservation score.
- Scorer
- Scorer for message preservation decisions.
- Semantic
Compressor - Semantic compressor that uses AI to summarize messages.
- Summarized
Segment - A segment of conversation history that has been summarized.
- Summarizer
- Summarizer for large content.
- Tool
Compressor - Compressor for tool results.
- Tool
Usage - Record of tool usage in a conversation.
- Topic
Transition - Records when the conversation topic changed.
Enums§
- AiCompression
Mode - Mode for AI-assisted compression.
- Complexity
Level - Conversation complexity level
- Compression
Stage - Progressive compression stages.
- Compression
Strategy - Strategy for compressing conversation history.
- Conversation
Phase - Conversation phase detected from message history. Determines which weights to apply during scoring.
- Focus
Feedback Type - 焦点反馈类型
- Focus
Status - 聚焦点状态
- Focus
Type - 焦点类型分类
- Keyword
Type - Keyword type for custom keyword additions (legacy compatibility).
- Semantic
Strategy - Strategy for semantic compression.
- Summary
Level - Summarization level
- Threshold
Level - Threshold level for compression warnings.
- Validation
Error - Validation errors for compression.
Constants§
- AUTOCOMPACT_
BUFFER_ TOKENS - Token buffers for threshold levels (from Claude Code).
- DEFAULT_
COMPRESSION_ THRESHOLD - Compression trigger threshold (percentage of context window). Lowered to 0.5 to compress earlier for long conversations (128K context -> 64K threshold)
- DEFAULT_
COMPRESSOR_ MODEL - Default model for summarization.
- DEFAULT_
TARGET_ RATIO - Target ratio after compression (keep this fraction of tokens).
- ERROR_
THRESHOLD_ BUFFER_ TOKENS - MANUAL_
COMPACT_ BUFFER_ TOKENS - MAX_
CONSECUTIVE_ FAILURES - Maximum consecutive compression failures before stopping retries. Claude Code: “1,279 sessions had 50+ consecutive failures, wasting ~250K API calls/day”
- MIN_
MESSAGES_ TO_ KEEP - Minimum messages to keep after compression. Increased to preserve more recent context for continuity
- TIME_
BASED_ MC_ CLEARED_ MESSAGE - Message to replace cleared tool result content (from Claude Code).
- TIME_
BASED_ MC_ GAP_ THRESHOLD_ MINUTES - Time-based microcompact threshold (minutes since last assistant message). When gap exceeds this, server cache has expired - clear old tool results.
- WARNING_
THRESHOLD_ BUFFER_ TOKENS
Traits§
- Compressor
- Compressor trait for different implementations.
Functions§
- build_
summary_ prompt - Build a prompt for summarization.
- compress_
messages - Compress messages synchronously.
- compress_
messages_ with_ ai - Compress messages with AI assistance (async version).
- compress_
messages_ with_ full_ ai - Compress messages with full AI support (async version).
- compress_
with_ bias - Compress with bias-based scoring.
- compress_
with_ pipeline - Legacy compression function (backward compatible).
- estimate_
tokens - Count tokens for a message using tiktoken (accurate).
- estimate_
total_ tokens - Estimate total tokens for a message list.
- example_
optimized_ compression - Example usage showing all optimizations with focus tracking.
- format_
tokens - Format token count for display.
- score_
by_ rules - Rule-based scoring for a message (public for pipeline use).
- score_
messages_ only - Score messages without compressing (analysis only).
- should_
compress - Check if compression should be triggered.