agent-context
Backend-agnostic LLM conversation context manager built on the kameo actor model.
Three-Zone + Scratch Memory
Messages are split into three storage zones plus an optional transient scratch layer:
| Zone | Mutability | Purpose |
|---|---|---|
| immutable | Read-only | System prompts, preset context |
| compressed | Write-once (during compression) | Summaries of older messages |
| incremental | Full CRUD | Active conversation |
| scratch | Optional, not stored | Per-turn metadata (time, cwd, etc.) via CommonOpts |
Concatenation order: immutable → compressed → incremental → scratch (if present).
Compression Notification
Use RequestSubscribeCompressed to register a subscriber that can modify the summary and kept messages after compression. Unsubscribe anytime with RequestUnsubscribeCompressed.
Usage
use ;
use StreamExt;
// 1. Implement ContextBackend for your LLM backend
// 2. Spawn the actor
let ctx = new;
let actor_ref = spawn;
// 3. Non-streaming conversation
actor_ref.ask.await?;
let response = actor_ref.ask.await?;
// 4. Streaming conversation
let mut stream = actor_ref.ask.await??;
while let Some = stream.next.await
let chunks = stream.take_chunks;
let msg = backend.merge_chunks.expect;
let msgs = backend.to_request_messages?;
for m in msgs
// 5. Read all messages
let all: = actor_ref.ask.await?;
ContextBackend
The core trait — implement it to support any LLM provider (DeepSeek, Zhipu, OpenAI, etc.):
- Message factories:
user_message,system_message,tool_message - Format conversion:
to_system_message,to_request_messages - Response parsing:
extract_messages,merge_chunks - Model interaction:
send,send_stream,estimate_tokens
Each backend's Opts type must embed CommonOpts (model, context_window, max_tokens, auto_compress, scratch) via AsRef<CommonOpts>.
Compression
Automatic compression is triggered before each RequestSend/RequestSendStream when CommonOpts::auto_compress is true and the estimated tokens exceed context_window. Use RequestCompress with CompressStrategy::Summarize for manual compression. Register a compressed subscriber via RequestSubscribeCompressed to customize the result.
License
MIT OR Apache-2.0