pub struct WorkingMemoryBuffer { /* private fields */ }Expand description
Bounded in-memory buffer for agent short-term memory.
Stores the N most recent entries (text + embedding). When full, the oldest
entry is evicted on each push. Supports brute-force flat scan (search)
and draining all entries to an AI-Lake table (drain_to_table).
§Cascade pattern
Short-term agents use only WorkingMemoryBuffer. Long-term agents cascade:
searchqueries the buffer first (fast, recent).- When full,
drain_to_tablepersists to AI-Lake; continue searching viaailake::searchfor historical context.
§Example
let mut wm = WorkingMemoryBuffer::new(100);
wm.push("Meeting notes: …", embedding, 0.8);
let top3 = wm.search(&query_vec, 3);
if wm.is_full() {
wm.drain_to_table(&mut writer).await?;
writer.commit().await?;
}Implementations§
Source§impl WorkingMemoryBuffer
impl WorkingMemoryBuffer
Sourcepub fn new(max_rows: usize) -> Self
pub fn new(max_rows: usize) -> Self
Create buffer with at most max_rows entries. Evicts oldest on overflow.
Sourcepub fn push(
&mut self,
text: impl Into<String>,
embedding: Vec<f32>,
importance: f32,
)
pub fn push( &mut self, text: impl Into<String>, embedding: Vec<f32>, importance: f32, )
Add entry. If at capacity, evicts the oldest entry (FIFO).
Sourcepub fn search(
&self,
query: &[f32],
top_k: usize,
) -> Vec<(f32, &WorkingMemoryEntry)>
pub fn search( &self, query: &[f32], top_k: usize, ) -> Vec<(f32, &WorkingMemoryEntry)>
Brute-force cosine similarity scan. Returns (distance, entry) pairs
sorted ascending (smallest distance = most similar). Distances in [0, 2].
Sourcepub async fn drain_to_table(
&mut self,
writer: &mut TableWriter,
) -> AilakeResult<()>
pub async fn drain_to_table( &mut self, writer: &mut TableWriter, ) -> AilakeResult<()>
Write all buffered entries to an AI-Lake table and clear the buffer.
Creates a RecordBatch with a chunk_text column. Call writer.commit()
after to persist the snapshot.
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Auto Trait Implementations§
impl Freeze for WorkingMemoryBuffer
impl RefUnwindSafe for WorkingMemoryBuffer
impl Send for WorkingMemoryBuffer
impl Sync for WorkingMemoryBuffer
impl Unpin for WorkingMemoryBuffer
impl UnsafeUnpin for WorkingMemoryBuffer
impl UnwindSafe for WorkingMemoryBuffer
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more