Mastra.getMemory()
.getMemory() 方法會按鍵從 Mastra 註冊表擷取記憶體執行個體。記憶體執行個體會在 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
要擷取的記憶體執行個體的註冊表鍵。必須與在 Mastra 建構函式中註冊記憶體時使用的鍵相符。
傳回值傳回值 的直接連結
memory:
TMemory[TMemoryKey]
具有指定鍵的記憶體執行個體。如找不到記憶體,則會擲回錯誤。
範例:註冊及擷取記憶體範例:註冊及擷取記憶體 的直接連結
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')