Mastra.getMemory()
.getMemory() 方法依鍵從 Mastra registry 取得 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')