> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # WorkingMemory `WorkingMemory` 是一个**输入 Processor**,会将 working memory 数据作为系统消息注入。它从存储中检索持久化信息,并将其格式化为 LLM 的指令,使 Agent 能够跨对话保留有关用户的上下文。 ## 使用示例 ```typescript 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 数据的存储实例 **options.template** (`WorkingMemoryTemplate`): 定义 working memory 格式和结构的模板 **options.template.format** (`'markdown' | 'json'`): working memory 内容的格式 **options.template.content** (`string`): 定义 working memory 数据结构的模板内容 **options.scope** (`'thread' | 'resource'`): working memory 的作用域。'thread' 将其限制在当前线程,'resource' 则在该资源的所有线程间共享 **options.useVNext** (`boolean`): 使用改进了指导准则的下一代指令格式 **options.readOnly** (`boolean`): 为 true 时,将 working memory 作为只读上下文提供。数据会注入对话,但不会提供 updateWorkingMemory Tool 或更新指令。适用于只应引用 working memory 而不应修改它的 Agent。 **options.templateProvider** (`{ getWorkingMemoryTemplate(args: { memoryConfig?: MemoryConfig }): Promise }`): 用于在运行时解析模板的动态模板 Provider **options.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`): 检索 working memory,并将其作为系统消息添加到消息列表中 ## 扩展使用示例 ```typescript 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 格式示例 ```typescript 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' }, }, }), }, }) ``` ## 行为 ### 输入处理 1. 从请求上下文中检索 `threadId` 和 `resourceId` 2. 根据作用域,从以下位置之一获取 working memory: - 线程元数据(`scope: 'thread'`) - 资源记录(`scope: 'resource'`) 3. 解析模板(来自 Provider、选项或默认值) 4. 根据模式生成系统指令: - **正常模式**:包含存储或更新信息的准则、模板结构和当前数据 - **只读模式**(`readOnly: true`):仅包含作为上下文的当前数据,不包含更新指令 5. 将指令作为带有 `source: '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 ## 相关内容 - [Guardrails](https://mastra.zisheng.pro/docs/agents/guardrails)