memory.ts
These files define the agent's memory system. base.ts
provides the fundamental
building blocks: MemoryStore
for saving and loading the persistent state of
your contexts (like chat history), and VectorStore
for storing "episodic
memories" (learned experiences) using vector embeddings for later recall.
utils.ts
contains helpers, primarily for automatically generating those
episodic memories using an LLM.
How to Use
You configure the agent's memory system via the memory
option when calling
createDreams
.
- You typically provide implementations for
MemoryStore
andVectorStore
. - The core package provides simple defaults:
createMemoryStore()
(stores data in memory, lost on restart) andcreateVectorStore()
(does nothing). - For real persistence, you'll import and use implementations from other
Daydreams packages, like
@daydreamsai/mongo
for MongoDB (createMongoMemoryStore
) or@daydreamsai/chroma
for ChromaDB (createChromaVectorStore
). - The
createMemory
function (exported frombase.ts
) is used to bundle your chosen store implementations together for thememory
option.
- Episodic memory generation (from
utils.ts
) happens automatically in the background if you setgenerateMemories: true
in the agent config and provide aVectorStore
.
Benefit
Allows your agent to have both persistent state (remembering conversations or
task progress across restarts via MemoryStore
) and the ability to learn from
past interactions (recalling relevant experiences via VectorStore
and episodic
memory). You can choose storage backends suitable for your needs (simple
in-memory for testing, robust databases for production).
Anticipated Questions
- "Do I need both MemoryStore and VectorStore?"
MemoryStore
is essential for saving the state of yourcontext
instances (likectx.memory
).VectorStore
is only needed if you want the agent to use episodic memory (learning from past interactions using embeddings). You can use the defaultcreateVectorStore()
if you don't need episodic memory. - "What is episodic memory?" It's a feature where the agent summarizes
sequences of thought -> action -> result into "episodes". These are stored as
vector embeddings. When the agent encounters a new situation, it can search
its
VectorStore
for similar past episodes to potentially inform its current reasoning. (RequiresgenerateMemories: true
and aVectorStore
). - "Where does
ctx.memory
get saved?" The agent automatically saves thememory
property of yourContextState
instances to the configuredMemoryStore
at the end of each run cycle.