| name | memory |
| description | BaseMemory trait, memory types (SimpleMemory, WindowBufferMemory, DummyMemory), and Arc<Mutex> construction patterns. |
Memory is Arc>. Use SimpleMemory for basic history, WindowBufferMemory
for a sliding window. Pass .into() to convert — all types implement the conversion.
## BaseMemory Trait
pub trait BaseMemory: Send + Sync {
fn messages(&self) -> Vec<Message>;
fn add_message(&mut self, message: Message);
fn add_user_message(&mut self, message: impl Into<String>);
fn add_ai_message(&mut self, message: impl Into<String>);
fn clear(&mut self);
}
## Memory Types
use langchainx::memory::{SimpleMemory, WindowBufferMemory, DummyMemory};
let mem = SimpleMemory::new();
let mem = WindowBufferMemory::new(5);
let mem = DummyMemory::new();
## Construction: Into>>
All memory types implement Into<Arc<Mutex<dyn BaseMemory>>>. Use .into():
use std::sync::Arc;
use tokio::sync::Mutex;
use langchainx::{
memory::SimpleMemory,
schemas::memory::BaseMemory,
};
let mem: Arc<Mutex<dyn BaseMemory>> = SimpleMemory::new().into();
let mem = Arc::new(Mutex::new(SimpleMemory::new())) as Arc<Mutex<dyn BaseMemory>>;
Pass to chain/agent builders:
ConversationalChainBuilder::new()
.memory(SimpleMemory::new().into())
...
## Reading from Memory
let mem: Arc<Mutex<dyn BaseMemory>> = SimpleMemory::new().into();
let guard = mem.lock().await;
let messages = guard.messages();
drop(guard);
let mut guard = mem.lock().await;
guard.add_user_message("Hello");
guard.add_ai_message("Hi there!");
Never hold the lock across an .await — use a separate scope or drop before await points.