WorkingMemory
WorkingMemory 是一个输入 Processor,会将 working memory 数据作为系统消息注入。它从存储中检索持久化信息,并将其格式化为 LLM 的指令,使 Agent 能够跨对话保留有关用户的上下文。
使用示例使用示例的直接链接
import { WorkingMemory } from '@mastra/core/processors'
const processor = new WorkingMemory({
storage: memoryStorage,
scope: 'resource',
template: {
format: 'markdown',
content: `# User Profile
- **Name**:
- **Preferences**:
- **Goals**:
`,
},
})
构造函数参数构造函数参数的直接链接
options:
Options
working memory Processor 的配置选项
Options
storage:
MemoryStorage
用于检索 working memory 数据的存储实例
template?:
WorkingMemoryTemplate
定义 working memory 格式和结构的模板
WorkingMemoryTemplate
format:
'markdown' | 'json'
working memory 内容的格式
content:
string
定义 working memory 数据结构的模板内容
scope?:
'thread' | 'resource'
working memory 的作用域。'thread' 将其限制在当前线程,'resource' 则在该资源的所有线程间共享
useVNext?:
boolean
使用改进了指导准则的下一代指令格式
readOnly?:
boolean
为 true 时,将 working memory 作为只读上下文提供。数据会注入对话,但不会提供 updateWorkingMemory Tool 或更新指令。适用于只应引用 working memory 而不应修改它的 Agent。
templateProvider?:
{ getWorkingMemoryTemplate(args: { memoryConfig?: MemoryConfig }): Promise<WorkingMemoryTemplate | null> }
用于在运行时解析模板的动态模板 Provider
logger?:
IMastraLogger
用于结构化日志记录的可选 logger 实例
返回值返回值的直接链接
id:
string
设为 'working-memory' 的 Processor 标识符
name:
string
设为 'WorkingMemory' 的 Processor 显示名称
defaultWorkingMemoryTemplate:
string
未提供自定义模板时使用的默认 Markdown 模板
processInput:
(args: { messages: MastraDBMessage[]; messageList: MessageList; abort: (reason?: string) => never; requestContext?: RequestContext }) => Promise<MessageList | MastraDBMessage[]>
检索 working memory,并将其作为系统消息添加到消息列表中
扩展使用示例扩展使用示例的直接链接
src/mastra/agents/personalized-agent.ts
import { Agent } from '@mastra/core/agent'
import { WorkingMemory, MessageHistory } from '@mastra/core/processors'
import { PostgresStorage } from '@mastra/pg'
const storage = new PostgresStorage({
connectionString: process.env.DATABASE_URL,
})
export const agent = new Agent({
id: 'personalized-agent',
name: 'personalized-agent',
instructions: 'You are a helpful assistant that remembers user preferences',
model: 'openai/gpt-5.6-sol',
inputProcessors: [
new WorkingMemory({
storage,
scope: 'resource',
template: {
format: 'markdown',
content: `# User Information
- **Name**:
- **Location**:
- **Preferences**:
- **Communication Style**:
- **Current Projects**:
`,
},
}),
new MessageHistory({ storage, lastMessages: 50 }),
],
outputProcessors: [new MessageHistory({ storage })],
})
JSON 格式示例JSON 格式示例的直接链接
import { WorkingMemory } from '@mastra/core/processors'
const processor = new WorkingMemory({
storage: memoryStorage,
scope: 'resource',
template: {
format: 'json',
content: JSON.stringify({
user: {
name: { type: 'string' },
preferences: { type: 'object' },
goals: { type: 'array' },
},
}),
},
})
行为行为的直接链接
输入处理输入处理的直接链接
- 从请求上下文中检索
threadId和resourceId - 根据作用域,从以下位置之一获取 working memory:
- 线程元数据(
scope: 'thread') - 资源记录(
scope: 'resource')
- 线程元数据(
- 解析模板(来自 Provider、选项或默认值)
- 根据模式生成系统指令:
- 正常模式:包含存储或更新信息的准则、模板结构和当前数据
- 只读模式(
readOnly: true):仅包含作为上下文的当前数据,不包含更新指令
- 将指令作为带有
source: 'memory'标签的系统消息添加
working memory 更新working memory 更新的直接链接
working memory 通过 Memory 类提供的 updateWorkingMemory Tool 进行更新,而不是通过此 Processor。此 Processor 只负责将 working memory 的当前状态注入对话。
默认模板默认模板的直接链接
如果未提供模板,Processor 会使用默认的 Markdown 模板,其中包含以下字段:
- First Name、Last Name
- Location、Occupation
- Interests、Goals
- Events、Facts、Projects