WorkingMemory
WorkingMemory は、working memory のデータを system message として挿入する input processor です。ストレージから永続的な情報を取得して LLM 向けの instruction に整形し、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' は現在の thread に限定し、'resource' はその resource のすべての thread で共有します
useVNext?:
boolean
ガイドラインを改善した次世代の instruction 形式を使用します
readOnly?:
boolean
true の場合、working memory を読み取り専用のコンテキストとして提供します。データは会話に挿入されますが、updateWorkingMemory Tool や更新用 instruction は含まれません。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 を取得し、system message としてメッセージリストに追加します
詳細な使用例詳細な使用例への直接リンク
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' },
},
}),
},
})
動作動作への直接リンク
入力処理入力処理への直接リンク
- request context から
threadIdとresourceIdを取得する - scope に基づき、次のいずれかから working memory を取得する
- thread のメタデータ(
scope: 'thread') - resource レコード(
scope: 'resource')
- thread のメタデータ(
- Provider、オプション、またはデフォルトからテンプレートを解決する
- モードに基づいて system instruction を生成する
- 通常モード: 情報の保存/更新に関するガイドライン、テンプレート構造、現在のデータを含める
- 読み取り専用モード(
readOnly: true): 更新用 instruction を含めず、現在のデータだけをコンテキストとして含める
source: 'memory'タグを付けた system message として instruction を追加する
Working memory の更新Working memory の更新への直接リンク
working memory の更新は、この processor ではなく Memory class が提供する updateWorkingMemory Tool を通じて行われます。この processor は、現在の working memory の状態を会話へ挿入する処理だけを担います。
デフォルトテンプレートデフォルトテンプレートへの直接リンク
テンプレートを指定しない場合、processor は次のフィールドを持つデフォルトの Markdown テンプレートを使用します。
- 名、姓
- 所在地、職業
- 興味、目標
- イベント、事実、プロジェクト