pub struct MemorySynthesizer { /* private fields */ }Expand description
Smarter alternative to MemoryWriter — distil the session’s assistant
turns into 1..=max_facts atomic durable facts using a cheap
“synthesizer” model, instead of persisting the verbatim final answer.
Wire either MemoryWriter or MemorySynthesizer, not both —
MemorySynthesizer is a superset of the writer’s behaviour with the
extra distillation step.
Behaviour:
- On
PostModel, appendsout.text(when present, non-empty) to an internal buffer. - On
TaskCompleted,tokio::spawns a synthesis task: callssynth_model.complete()with a fixed prompt that asks for a JSON array of{content, tags}objects, parses the response, and writes each one viaMemory::write. - Model errors / parse failures fall back to saving the raw response
as a single entry tagged
"synth-raw"so the session’s information isn’t lost entirely. - On
BudgetExhausted(noTaskCompletedfires), nothing is written.
The synth model should be cheap (deepseek-v4-flash, gpt-5-nano, etc.).
Constructed independently from the main model so you can use a small
summariser even when the reasoning model is large.
Implementations§
Source§impl MemorySynthesizer
impl MemorySynthesizer
Sourcepub fn new(memory: Arc<dyn Memory>, synth_model: Arc<dyn Model>) -> Self
pub fn new(memory: Arc<dyn Memory>, synth_model: Arc<dyn Model>) -> Self
Construct a synthesizer that uses synth_model to distil the
session into at most 3 facts.
Sourcepub fn with_extra_instructions(self, instructions: impl Into<String>) -> Self
pub fn with_extra_instructions(self, instructions: impl Into<String>) -> Self
Prepend domain-specific guidance to the synthesizer’s prompt. The extra text shows up BEFORE the standard “extract durable facts” instructions, so it sets context for what the model should consider durable in this application.
Example for a personal-accounting app:
.with_extra_instructions(
"This is a personal-accounting agent. Transaction flows like \
'¥199 火锅 microwave' are stored in the txns table — do NOT \
re-store them as facts. ONLY record: stable user preferences \
(payment habits, category conventions), repeated behaviour \
patterns (≥2 mentions), or long-term decisions (subscription \
cadences, investment policies). If unsure, prefer empty []."
)Sourcepub async fn flush_pending(&self)
pub async fn flush_pending(&self)
Await all background synthesis tasks that have been kicked off so far. Call this before your process exits if you want to guarantee the last session’s memory is on disk — otherwise the tokio runtime may be dropped while the spawn is mid-flight.
pub fn with_source(self, source: impl Into<String>) -> Self
Sourcepub fn with_max_facts(self, n: usize) -> Self
pub fn with_max_facts(self, n: usize) -> Self
Cap how many facts the synthesizer is allowed to emit. Default 3.