azad.compression.core Module¶
azad.compression.core ¶
Core compression framework.
This module defines the core interfaces and classes for the compression system, including type definitions, the CompressionStrategy interface, and the Compressor class for managing the compression process.
Attributes¶
CompressionConfig
module-attribute
¶
CompressionConfig = Union[TruncationConfig, CompactSummarizationConfig, Any]
Classes¶
CompressionStrategyType ¶
Bases: str, Enum
Enum of supported compression strategy types.
BaseCompressionConfig ¶
Bases: BaseModel
Base configuration for all compression strategies.
TruncationConfig ¶
Bases: BaseCompressionConfig
Configuration for truncation compression strategy.
CompactSummarizationConfig ¶
Bases: BaseCompressionConfig
Configuration for compact summarization compression strategy.
Attributes¶
strategy_type
class-attribute
instance-attribute
¶
strategy_type: Literal[COMPACT_SUMMARIZATION] = COMPACT_SUMMARIZATION
CompressionCheckpoint ¶
Bases: BaseModel
Represents a compression checkpoint. A checkpoint marks a point in the message history where compression has been applied. It contains metadata about what was compressed and how.
CompressionStrategy ¶
Bases: ABC
Interface for compression strategies.
A compression strategy is responsible for compressing a task's messages. All strategies must implement the strategy_type property and compress method. Strategies must respect task boundaries and only operate on messages within the current task level.
Attributes¶
strategy_type
abstractmethod
property
¶
strategy_type: CompressionStrategyType
Get the type of this compression strategy.
Functions¶
compress
abstractmethod
async
¶
compress(task: Task, new_checkpoint: Optional[CompressionCheckpoint], config: CompressionConfig) -> List[Message]
Compress the messages in a task.
This method should implement the compression logic for the strategy. It must update the checkpoint with which messages were compressed or kept. It must respect task boundaries and only compress messages within the current task level.
Important: Implementations MUST use task.current_task_messages() instead of task.messages directly to ensure proper handling of nested mindmaps.
Parameters:
-
task(Task) –The task containing messages to compress
-
new_checkpoint(Optional[CompressionCheckpoint]) –The checkpoint to update (None for transform only)
-
config(CompressionConfig) –Configuration for compression
Returns:
-
List[Message]–The compressed messages list for the current task level
Source code in azad/compression/core.py
Compressor ¶
Compressor(strategy: CompressionStrategy, config: CompressionConfig)
Main class for compressing messages in a task.
This class manages the compression process, applying a specific strategy to compress messages in a task. It ensures that only one compression strategy is active at a time and tracks which messages were included in each compression.
It provides utility methods for working with task boundaries to ensure proper handling of nested mindmaps. All methods respect task boundaries and only operate on messages within the current task level.
Initialize the compressor with a strategy.
Parameters:
-
strategy(CompressionStrategy) –The compression strategy to use
-
config(CompressionConfig) –Configuration for the compression
Source code in azad/compression/core.py
Attributes¶
Functions¶
get_task_messages ¶
get_task_messages() -> List[Message]
Get all messages within the current task boundaries.
This utility method ensures that only messages within the current task level are considered for compression, respecting nested mindmap boundaries.
Returns:
-
List[Message]–List of messages within the current task level
Source code in azad/compression/core.py
get_task_boundaries ¶
Get the start and end indices of the current task level.
This utility method is used to determine the proper message indices for the current task level, respecting nested mindmap boundaries.
Performance Note: Boundary calculation can be expensive for large message histories.¶
Consider caching this result if called frequently within the same compression cycle.¶
Returns:
-
Tuple[Optional[int], Optional[int]]–Tuple of (start_index, end_index), where end_index is None if the task hasn't ended yet
Source code in azad/compression/core.py
get_compression_messages_in_task ¶
get_compression_messages_in_task() -> List[CompressionMessage]
Get all compression messages within the current task level.
This utility method filters compression messages to ensure only those relevant to the current task level are considered.
Returns:
-
List[CompressionMessage]–List of compression messages within the current task level
Source code in azad/compression/core.py
load ¶
load(task: Task) -> None
Load existing compression checkpoints from a task.
Only loads compression checkpoints that are relevant to the current task level, respecting nested mindmap boundaries.
Parameters:
-
task(Task) –The task to load checkpoints from
Source code in azad/compression/core.py
compress
async
¶
compress() -> List[Message]
Create a new compression checkpoint.
This method applies the compression strategy to create a new checkpoint and adds a compression message to the task with the checkpoint information. It respects task boundaries and only compresses messages within the current task level.
Returns:
-
List[Message]–The compressed list of messages for the current task level
Source code in azad/compression/core.py
transform
async
¶
transform() -> List[Message]
Transform task messages based on the current checkpoints.
This method runs the compress method using the current strategy and cache, forcing the compression to be applied up to the last checkpoint. It respects task boundaries and only operates on messages within the current task level.
Returns:
-
List[Message]–The transformed list of messages for the current task level only