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 a 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 | Not stored | Per-turn metadata (time, cwd, etc.) via CommonOpts |
Concatenation order: immutable → compressed → incremental → scratch.
Change Notification
Register a callback via with_on_change to receive ChangeEvent notifications. Every mutation to the incremental zone triggers a notification — there are no silent bypasses.
Usage
use ;
use StreamExt;
// 1. Implement ContextBackend for your LLM backend
// 2. Spawn the actor
let ctx = new;
let actor = spawn;
// 3. Non-streaming conversation
actor.ask.await?;
let response = actor.ask.await?;
// 4. Streaming conversation
let mut stream = actor.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
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 SendMsg/SendStreamMsg when CommonOpts::auto_compress is true and the estimated tokens exceed context_window. Use CompressMsg with CompressStrategy::Summarize for manual compression.
License
MIT OR Apache-2.0