Skip to main content

CompactingMemory

Struct CompactingMemory 

Source
pub struct CompactingMemory<M, P, C: Compactor> { /* private fields */ }
Expand description

Prepends a rolling summary of demoted history to the policy’s retained window. New evictions are compacted with the previous artifact as carry-over. The summary is outside the policy budget; callers needing a bounded prompt must also bound the compactor’s artifact.

Only one compaction per tracked conversation runs at a time. Concurrent loads return the previous summary and retained window immediately. Errors reach only the compacting caller; unchanged watermarks allow later loads to retry.

Summaries and watermarks are process-local. Restarts or discarded state cause recompaction; compactors with side effects must tolerate repeated input.

use rig_memory::{
    CompactingMemory, InMemoryConversationMemory, SlidingWindowMemory,
    TemplateCompactor,
};

let memory = CompactingMemory::new(
    InMemoryConversationMemory::new(),
    SlidingWindowMemory::last_messages(20),
    TemplateCompactor::new(),
);

Implementations§

Source§

impl<M, P, C: Compactor> CompactingMemory<M, P, C>

Source

pub fn new(inner: M, policy: P, compactor: C) -> Self

Wrap inner so every load runs through policy and demoted messages are summarised by compactor.

Source§

impl<M, P, C: Compactor> CompactingMemory<M, P, C>

Source

pub fn inner(&self) -> &M

Return a reference to the wrapped backend.

Source

pub fn policy(&self) -> &P

Return a reference to the wrapped policy.

Source

pub fn compactor(&self) -> &C

Return a reference to the third component.

Source

pub fn into_inner(self) -> (M, P, C)

Consume the wrapper and return its three components.

Source

pub fn forget(&self, conversation_id: &ConversationId)

Drop the in-process state for conversation_id.

Entries otherwise persist for the wrapper’s lifetime. Does nothing if the state lock is poisoned. In-flight work is not cancelled.

Source

pub fn tracked_conversations(&self) -> usize

Number of conversations currently tracked in the state map. Useful for telemetry and leak detection. Returns 0 if the internal state lock is poisoned.

Trait Implementations§

Source§

impl<M, P, C> ConversationMemory for CompactingMemory<M, P, C>

Source§

fn load<'a>( &'a self, conversation_id: &'a ConversationId, ) -> WasmBoxedFuture<'a, Result<Vec<Message>, MemoryError>>

Load the full conversation history for conversation_id. Read more
Source§

fn append<'a>( &'a self, conversation_id: &'a ConversationId, messages: Vec<Message>, ) -> WasmBoxedFuture<'a, Result<(), MemoryError>>

Append messages to the conversation identified by conversation_id. Read more
Source§

fn clear<'a>( &'a self, conversation_id: &'a ConversationId, ) -> WasmBoxedFuture<'a, Result<(), MemoryError>>

Remove all stored messages for conversation_id.
Source§

impl<M, P, C: Compactor> Debug for CompactingMemory<M, P, C>
where M: Debug, P: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<M, P, C> !Freeze for CompactingMemory<M, P, C>

§

impl<M, P, C> RefUnwindSafe for CompactingMemory<M, P, C>
where M: RefUnwindSafe, P: RefUnwindSafe, C: RefUnwindSafe, Mutex<HashMap<ConversationId, ConversationCompactionState<<C as Compactor>::Artifact>>>: RefUnwindSafe,

§

impl<M, P, C> Send for CompactingMemory<M, P, C>
where M: Send, P: Send, Mutex<HashMap<ConversationId, ConversationCompactionState<<C as Compactor>::Artifact>>>: Send,

§

impl<M, P, C> Sync for CompactingMemory<M, P, C>
where M: Sync, P: Sync, Mutex<HashMap<ConversationId, ConversationCompactionState<<C as Compactor>::Artifact>>>: Sync,

§

impl<M, P, C> Unpin for CompactingMemory<M, P, C>
where M: Unpin, P: Unpin, C: Unpin, Mutex<HashMap<ConversationId, ConversationCompactionState<<C as Compactor>::Artifact>>>: Unpin,

§

impl<M, P, C> UnsafeUnpin for CompactingMemory<M, P, C>
where M: UnsafeUnpin, P: UnsafeUnpin, C: UnsafeUnpin, Mutex<HashMap<ConversationId, ConversationCompactionState<<C as Compactor>::Artifact>>>: UnsafeUnpin,

§

impl<M, P, C> UnwindSafe for CompactingMemory<M, P, C>
where M: UnwindSafe, P: UnwindSafe, C: UnwindSafe, Mutex<HashMap<ConversationId, ConversationCompactionState<<C as Compactor>::Artifact>>>: UnwindSafe,

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,

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