pub struct TemplateCompactor { /* private fields */ }Expand description
A zero-dependency reference Compactor that produces a textual
rollup of evicted messages without calling an LLM.
The artifact is a single Message::System whose body concatenates a
header, the previous summary (if any), and the textual content of each
newly-evicted message. It is intentionally simple: useful as a default
for tests and examples, and as a placeholder before wiring a real
summarising LLM through a custom Compactor implementation.
§Bounding the summary
By default the summary grows monotonically: every compaction pass
embeds the previous summary verbatim and appends newly-evicted lines.
Long-running conversations should call Self::with_max_bytes to
cap the rolled-up text. When the cap is exceeded, the oldest portion
of the body (after the header) is dropped at a UTF-8 boundary and
replaced with a "[…truncated…]" marker, preserving the most recent
context.
§Example
use rig_memory::TemplateCompactor;
// Default header is "[Conversation summary so far]", unbounded.
let _compactor = TemplateCompactor::new();
// Custom header plus a 4 KiB cap for use with token-budgeted policies.
let _bounded = TemplateCompactor::with_header("Earlier context")
.with_max_bytes(4 * 1024);Implementations§
Source§impl TemplateCompactor
impl TemplateCompactor
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a TemplateCompactor with the default header
"[Conversation summary so far]" and no size cap.
Sourcepub fn with_header(header: impl Into<String>) -> Self
pub fn with_header(header: impl Into<String>) -> Self
Create a TemplateCompactor with a custom header line and no
size cap.
Sourcepub fn with_max_bytes(self, max_bytes: usize) -> Self
pub fn with_max_bytes(self, max_bytes: usize) -> Self
Cap the rolled-up summary at max_bytes bytes (UTF-8). When the
assembled body exceeds the cap, the oldest portion after the
header is dropped at a char boundary and replaced with a
"[…truncated…]" marker.
max_bytes of 0 disables truncation (equivalent to the default
unbounded behaviour). The header line plus the marker are always
preserved even if they exceed the cap.
Trait Implementations§
Source§impl Clone for TemplateCompactor
impl Clone for TemplateCompactor
Source§fn clone(&self) -> TemplateCompactor
fn clone(&self) -> TemplateCompactor
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Compactor for TemplateCompactor
impl Compactor for TemplateCompactor
Source§type Artifact = TextSummary
type Artifact = TextSummary
Compactor::compact. Read more