Memory API
Memory API 提供用于管理 Mastra 中对话 thread 和消息历史记录的方法。
获取所有 thread获取所有 thread的直接链接
检索特定资源的所有 memory thread:
const threads = await mastraClient.listMemoryThreads({
resourceId: 'resource-1',
agentId: 'agent-1', // Optional - can be omitted if storage is configured
})
如果省略 agentId 且服务器配置了存储,则会直接使用存储检索 thread。当多个 Agent 共享相同 thread 时(例如包含多个 Agent 步骤的 Workflow),这会很有用。
创建新 thread创建新 thread的直接链接
创建新的 memory thread:
const thread = await mastraClient.createMemoryThread({
title: 'New Conversation',
metadata: { category: 'support' },
resourceId: 'resource-1',
agentId: 'agent-1',
})
使用特定 thread使用特定 thread的直接链接
获取特定 memory thread 的实例:
const thread = mastraClient.getMemoryThread({ threadId: 'thread-id', agentId: 'agent-id' })
Thread 方法Thread 方法的直接链接
获取 thread 详情获取 thread 详情的直接链接
检索特定 thread 的详细信息:
const details = await thread.get()
更新 thread更新 thread的直接链接
更新 thread 属性:
const updated = await thread.update({
title: 'Updated Title',
metadata: { status: 'resolved' },
resourceId: 'resource-1',
})
删除 thread删除 thread的直接链接
删除 thread 及其消息:
await thread.delete()
克隆 thread克隆 thread的直接链接
创建包含 thread 所有消息的副本:
const { thread: clonedThread, clonedMessages } = await thread.clone()
使用选项进行克隆:
const { thread: clonedThread, clonedMessages } = await thread.clone({
newThreadId: 'custom-clone-id',
title: 'Cloned Conversation',
metadata: { branch: 'experiment-1' },
options: {
messageLimit: 10, // Only clone last 10 messages
},
})
筛选消息后进行克隆:
const { thread: clonedThread } = await thread.clone({
options: {
messageFilter: {
startDate: new Date('2024-01-01'),
endDate: new Date('2024-01-31'),
},
},
})
克隆响应包含:
thread:新创建的、包含克隆元数据的 threadclonedMessages:具有新 ID 的已克隆消息数组
消息操作消息操作的直接链接
保存消息保存消息的直接链接
将消息保存到 memory:
const result = await mastraClient.saveMessageToMemory({
messages: [
{
role: 'user',
content: 'Hello!',
id: '1',
threadId: 'thread-1',
resourceId: 'resource-1',
createdAt: new Date(),
format: 2,
},
],
agentId: 'agent-1',
})
// result.messages contains the saved messages
console.log(result.messages)
检索 thread 消息检索 thread 消息的直接链接
获取与 memory thread 关联的消息:
// Get all messages in the thread (paginated)
const result = await thread.listMessages()
console.log(result.messages) // Array of messages
console.log(result.total) // Total count
console.log(result.hasMore) // Whether more pages exist
// Get messages with pagination
const result = await thread.listMessages({
page: 0,
perPage: 20,
})
// Get messages with ordering
const result = await thread.listMessages({
orderBy: { field: 'createdAt', direction: 'ASC' },
})
// Get messages with shallow metadata filters
const result = await thread.listMessages({
filter: {
metadata: {
category: 'billing',
escalated: true,
priority: 2,
archivedAt: null,
},
},
})
元数据筛选条件只匹配浅层标量值:string、有限的 number、boolean 和 null。每个键值对都必须以 AND 语义匹配。null 只匹配明确设为 null 的键。元数据键必须以字母或下划线开头,并且只能包含字母数字或下划线字符,长度上限为 128 个字符。不允许使用 __proto__、constructor 和 prototype 等保留的原型键。性能取决于服务器的存储后端,任意元数据筛选条件可能会扫描候选消息。
删除消息删除消息的直接链接
从 thread 中删除一条或多条消息:
// Delete a single message
const result = await thread.deleteMessages('message-id')
// Delete multiple messages
const result = await thread.deleteMessages(['message-1', 'message-2', 'message-3'])
// Returns: { success: true, message: "Message deleted successfully" }
工作记忆工作记忆的直接链接
Working memory 让 Agent 能够跨交互持久保留用户相关信息。其作用域既可以限定为特定 thread,也可以覆盖某一资源(用户)的所有 thread。
获取 Working memory获取 Working memory的直接链接
检索 thread 当前的 Working memory:
const workingMemory = await mastraClient.getWorkingMemory({
agentId: 'agent-1',
threadId: 'thread-1',
resourceId: 'user-123', // Optional, required for resource-scoped memory
})
响应包含:
workingMemory:当前 Working memory 内容(字符串或 null)source:memory 来自"thread"还是"resource"作用域workingMemoryTemplate:用于 Working memory 的模板(如果已配置)threadExists:thread 是否存在
更新 Working memory更新 Working memory的直接链接
更新 thread 的 Working memory 内容:
await mastraClient.updateWorkingMemory({
agentId: 'agent-1',
threadId: 'thread-1',
workingMemory: `# User Profile
- Name: John Doe
- Location: New York
- Preferences: Prefers formal communication
`,
resourceId: 'user-123', // Optional, required for resource-scoped memory
})
// Returns: { success: true }
对于资源作用域的 Working memory,必须提供 resourceId 参数。这样,memory 就能跨该用户的所有对话 thread 持久保留。
获取 Memory 状态获取 Memory 状态的直接链接
检查 memory 系统的状态:
const status = await mastraClient.getMemoryStatus('agent-id')