pub struct CompressionMiddleware { /* private fields */ }Expand description
Middleware that compresses conversation history before each LLM call.
Wraps a ContextCompactor and forwards on_pre_llm to its compact
method. When the estimated token count is below CompressionConfig::trigger_tokens,
the middleware is a no-op and the original messages pass through unchanged.
A CompressionPolicy controls whether compression actually proceeds once
the threshold is crossed. Use with_policy to supply
a custom policy (e.g. user confirmation, rate limiting).
§Usage
use agent_works::compression::{CompressionConfig, CompressionMiddleware};
let mw = CompressionMiddleware::new(config, client);
builder.middleware(mw);Implementations§
Source§impl CompressionMiddleware
impl CompressionMiddleware
Sourcepub fn new(config: CompressionConfig, client: Arc<dyn LlmProvider>) -> Self
pub fn new(config: CompressionConfig, client: Arc<dyn LlmProvider>) -> Self
Create a new middleware with the given config and LLM client.
Uses AutoCompressionPolicy (always compress when threshold is hit).
Sourcepub fn from_compactor(compactor: ContextCompactor) -> Self
pub fn from_compactor(compactor: ContextCompactor) -> Self
Create a new middleware from an existing ContextCompactor.
Uses AutoCompressionPolicy.
Sourcepub fn with_policy(
config: CompressionConfig,
client: Arc<dyn LlmProvider>,
policy: Box<dyn CompressionPolicy>,
) -> Self
pub fn with_policy( config: CompressionConfig, client: Arc<dyn LlmProvider>, policy: Box<dyn CompressionPolicy>, ) -> Self
Create a new middleware with a custom CompressionPolicy.
Sourcepub fn compactor(&self) -> &ContextCompactor
pub fn compactor(&self) -> &ContextCompactor
Access the inner compactor (e.g. for /compact or cache clearing).
Sourcepub fn clone_compactor(&self) -> ContextCompactor
pub fn clone_compactor(&self) -> ContextCompactor
Create a cloned handle of the inner compactor.
The clone shares the same cache — clearing it through either handle
affects both. Useful for storing a separate handle outside the
middleware (e.g. in PhiAgent for /compact access).
Sourcepub fn config(&self) -> &CompressionConfig
pub fn config(&self) -> &CompressionConfig
Access the compression config.