Expand description
Conversation-history compaction.
Rust equivalent of (a self-contained subset of) upstream _compaction.py
(see UPSTREAM_DRIFT.md §9). Upstream’s _compaction.py is a large
(1500+ line), annotation-driven system: it groups messages into logical
spans (system / user / assistant-text / tool-call), stamps grouping and
token-count metadata onto Message.additional_properties, and never
deletes history — it flags messages _excluded and lets the client filter
them out when building the payload sent to the model. It ships seven
strategies (Truncation, SlidingWindow, SelectiveToolCall,
ToolResult, LLM-backed Summarization, TokenBudgetComposed,
ContextWindow) plus a CompactionProvider(ContextProvider) that wires a
strategy into the client’s get_response loop.
This module intentionally delivers a smaller, dependency-free surface:
the Tokenizer and CompactionStrategy abstractions upstream
defines, plus four concrete, non-LLM strategies that mirror upstream’s
Truncation, SlidingWindow, ContextWindow/TokenBudget, and
ToolResult (renamed SelectiveToolResult here to avoid confusion with
Content::FunctionResult… “tool result” is the plain-English name).
Compaction here works by returning a reduced list rather than annotating
messages in place — simpler, and sufficient for the strategies included.
Wiring a strategy into the client’s get_response loop (upstream’s
CompactionProvider) is intentionally out of scope for this change; see
UPSTREAM_DRIFT.md §9.
Compaction never errors on content: given any message list it returns a (possibly unchanged) retained subset that satisfies the strategy’s constraint.
Structs§
- Approx
Tokenizer - A dependency-free default tokenizer using a ~4-characters-per-token
heuristic. Mirrors upstream’s
CharacterEstimatorTokenizer. - Compaction
Provider - A
ContextProviderthat compacts the accumulated message list — typically the run’s history, once aHistoryProviderhas prepended it inbefore_run— down to fit aCompactionStrategy’s constraint before it reaches the model. Rust equivalent of (a subset of) upstream’sCompactionProvider(see module docs andUPSTREAM_DRIFT.md§9). - Selective
Tool Result - Replace the payload of
Content::FunctionResult(tool-result) content in all but the lastkeep_lastmessages that carry tool results — they are the bulkiest and least useful once stale. Text and other content is left intact. - Sliding
Window - Keep leading system message(s) + the last
windownon-system messages. Mirrors upstream’sSlidingWindowstrategy. - Token
Budget - Keep leading system message(s), then walk from the newest message
backward accumulating token counts, keeping messages until adding the
next would exceed
max_tokens. Returns the kept messages in original order. Mirrors upstream’sContextWindow/token-budget strategy. - Truncation
- Keep the most recent
max_messages, always preserving any leading system message(s) at the front. Mirrors upstream’sTruncationstrategy.
Constants§
- OMITTED_
TOOL_ RESULT - Stand-in payload left in place of a tool result this strategy compacts.
Traits§
- Compaction
Strategy - A strategy that reduces a message list to fit some constraint.
- Tokenizer
- Counts tokens for a piece of text. Rust equivalent of upstream
TokenizerProtocol.
Functions§
- compact
- Compact
messageswithstrategyandtokenizer. - count_
message_ tokens - Sum the token counts of a message’s text content (text and reasoning
content items) using
tokenizer.