Skip to main content

MemorySynthesizer

Struct MemorySynthesizer 

Source
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, appends out.text (when present, non-empty) to an internal buffer.
  • On TaskCompleted, tokio::spawns a synthesis task: calls synth_model.complete() with a fixed prompt that asks for a JSON array of {content, tags} objects, parses the response, and writes each one via Memory::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 (no TaskCompleted fires), 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

Source

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.

Source

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 []."
)
Source

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.

Source

pub fn with_source(self, source: impl Into<String>) -> Self

Source

pub fn with_base_tags( self, tags: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Source

pub fn with_max_facts(self, n: usize) -> Self

Cap how many facts the synthesizer is allowed to emit. Default 3.

Trait Implementations§

Source§

impl Hook for MemorySynthesizer

Source§

fn name(&self) -> &str

Source§

fn matches(&self, ev: &Event<'_>) -> bool

Source§

fn fire(&self, ev: &Event<'_>, _w: &mut World) -> HookOutcome

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more