Mastra.getMemory()
.getMemory() 方法按键从 Mastra 注册表获取 Memory 实例。Memory 实例在 Mastra 构造函数中注册,可供已存储的 Agent 引用。
使用示例使用示例的直接链接
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
要获取的 Memory 实例的注册键。必须与在 Mastra 构造函数中注册 Memory 时使用的键匹配。
返回值返回值的直接链接
memory:
TMemory[TMemoryKey]
具有指定键的 Memory 实例。如果未找到该 Memory,则抛出错误。
示例:注册和获取 Memory示例:注册和获取 Memory的直接链接
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')