Daydreams Logo
Extra reading

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 and VectorStore.
  • The core package provides simple defaults: createMemoryStore() (stores data in memory, lost on restart) and createVectorStore() (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 from base.ts) is used to bundle your chosen store implementations together for the memory option.
import { createDreams, createMemory } from '@daydreamsai/core';
// Import specific store implementations
import { createMongoMemoryStore } from '@daydreamsai/mongo';
import { createChromaVectorStore } from '@daydreamsai/chroma';
 
const agent = createDreams({
  model: /* ... */,
  memory: createMemory(
    // Use MongoDB for context state
    await createMongoMemoryStore({ uri: 'mongodb://...' }),
    // Use ChromaDB for episodic memory/vector search
    createChromaVectorStore('my-agent-episodes')
  ),
  // Optional: Enable automatic episodic memory generation
  // generateMemories: true,
  // vectorModel: openai('text-embedding-3-small') // Model for embeddings
});
  • Episodic memory generation (from utils.ts) happens automatically in the background if you set generateMemories: true in the agent config and provide a VectorStore.

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 your context instances (like ctx.memory). VectorStore is only needed if you want the agent to use episodic memory (learning from past interactions using embeddings). You can use the default createVectorStore() 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. (Requires generateMemories: true and a VectorStore).
  • "Where does ctx.memory get saved?" The agent automatically saves the memory property of your ContextState instances to the configured MemoryStore at the end of each run cycle.

On this page