Skip to main content

Mastra.getMemory()

.getMemory() 方法通过其键从 Mastra 注册表中检索内存实例。内存实例在 Mastra 构造函数中注册,并且可以被存储的代理引用。

🌐 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.

使用示例
Direct link to 使用示例

🌐 Usage example

const memory = mastra.getMemory("conversationMemory");

// Use the memory instance
const thread = await memory.createThread({
resourceId: "user-123",
title: "New Conversation",
});

参数
Direct link to 参数

🌐 Parameters

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.

返回
Direct link to 返回

🌐 Returns

memory:

TMemory[TMemoryKey]
The memory instance with the specified key. Throws an error if the memory is not found.

示例:注册和检索内存
Direct link to 示例:注册和检索内存

🌐 Example: Registering and Retrieving 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");

🌐 Related