@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
49 lines (34 loc) • 1.37 kB
Markdown
The `.getMemory()` method retrieves a memory instance from the Mastra registry by its key. Memory instances are registered in the Mastra constructor and can be referenced by stored agents.
```typescript
const memory = mastra.getMemory('conversationMemory')
// Use the memory instance
const thread = await memory.createThread({
resourceId: 'user-123',
title: 'New Conversation',
})
```
**key** (`TMemoryKey extends keyof TMemory`): The registry key of the memory instance to retrieve. Must match a key used when registering memory in the Mastra constructor.
**memory** (`TMemory[TMemoryKey]`): The memory instance with the specified key. Throws an error if the memory is not found.
```typescript
import { Mastra } from '@mastra/core'
import { Memory } from '@mastra/memory'
import { LibSQLStore } from '@mastra/libsql'
const conversationMemory = new Memory({
storage: new LibSQLStore({ id: 'conversation-store', url: ':memory:' }),
})
const mastra = new Mastra({
memory: {
conversationMemory,
},
})
// Later, retrieve the memory instance
const memory = mastra.getMemory('conversationMemory')
```
- [Mastra.listMemory()](https://mastra.ai/reference/core/listMemory)
- [Memory overview](https://mastra.ai/docs/memory/overview)