Skip to main content

BufferMemory

Struct BufferMemory 

Source
pub struct BufferMemory { /* private fields */ }
Expand description

Bounded conversation buffer.

Each call to append pushes one message and (if the buffer exceeds max_turns) drops the oldest. messages() returns the full retained list.

An optional ConsolidationPolicy can be attached via Self::with_consolidation_policy. When attached, Self::should_consolidate surfaces the policy’s decision so the caller (typically an agent recipe) can run an LLM-driven summarisation pass and write the result into a sibling crate::SummaryMemory before clearing the buffer. The buffer tracks last_consolidated_at itself — recipes call Self::mark_consolidated after a successful summarisation pass and the time-aware policies see the updated timestamp on the next check, so the consolidation loop reduces to:

if buffer.should_consolidate(&ctx).await? {
    run_summariser(&ctx, &buffer, &summary).await?;
    buffer.clear(&ctx).await?;
    buffer.mark_consolidated_now();
}

Implementations§

Source§

impl BufferMemory

Source

pub fn new( store: Arc<dyn Store<Vec<Message>>>, namespace: Namespace, max_turns: usize, ) -> Self

Build a buffer over store scoped to namespace, retaining at most max_turns messages.

Source

pub fn with_consolidation_policy( self, policy: Arc<dyn ConsolidationPolicy>, ) -> Self

Attach a ConsolidationPolicy. The buffer itself never performs the summarisation — the policy only decides when the caller should — but having the policy bound here means agent recipes can ask the buffer directly via Self::should_consolidate without threading the policy through every call site.

Source

pub const fn max_turns(&self) -> usize

Effective retention cap.

Source

pub const fn namespace(&self) -> &Namespace

Borrow the bound namespace.

Source

pub fn last_consolidated_at(&self) -> Option<DateTime<Utc>>

Wall-clock time of the most recent successful consolidation, as recorded via Self::mark_consolidated. Returns None before the first consolidation has been marked.

Source

pub fn mark_consolidated(&self, at: DateTime<Utc>)

Record that a consolidation pass completed at at. Recipes call this after a successful summarise-and-clear cycle so time-aware policies (throttling, “at most once per hour”) observe the new floor on the next check.

Source

pub fn mark_consolidated_now(&self)

Convenience over Self::mark_consolidated using chrono::Utc::now. Use when the caller doesn’t already have a timestamp in hand.

Source

pub async fn append( &self, ctx: &ExecutionContext, message: Message, ) -> Result<()>

Append message, dropping the oldest entries when over capacity.

Source

pub async fn messages(&self, ctx: &ExecutionContext) -> Result<Vec<Message>>

Read the retained messages oldest-first.

Source

pub async fn clear(&self, ctx: &ExecutionContext) -> Result<()>

Clear the buffer.

Source

pub async fn should_consolidate(&self, ctx: &ExecutionContext) -> Result<bool>

Consult the bound ConsolidationPolicy (if any) against the current buffer and the buffer’s tracked last_consolidated_at. Returns Ok(false) when no policy is bound — that path is the explicit “consolidation disabled” answer.

For token-budget policies that need the active model’s usage, use Self::should_consolidate_with and supply the values in PolicyExtras.

Source

pub async fn should_consolidate_with( &self, ctx: &ExecutionContext, extras: PolicyExtras, ) -> Result<bool>

As Self::should_consolidate, but lets the caller layer on model-specific token usage signals or override the buffer’s internally-tracked last_consolidated_at (rare — useful when a downstream system has more authoritative state).

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> Same for T

Source§

type Output = T

Should always be Self
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