Memory API
Memory API は、Mastra の会話スレッドとメッセージ履歴を管理するためのメソッドを提供します。
すべてのスレッドを取得すべてのスレッドを取得への直接リンク
特定のリソースに属するすべてのメモリスレッドを取得します。
const threads = await mastraClient.listMemoryThreads({
resourceId: 'resource-1',
agentId: 'agent-1', // Optional - can be omitted if storage is configured
})
agentId を省略し、サーバーにストレージが設定されている場合、スレッドはストレージから直接取得されます。これは、複数の Agent が同じスレッドを共有する場合(複数の Agent ステップを含む Workflow など)に便利です。
新しいスレッドを作成新しいスレッドを作成への直接リンク
新しいメモリスレッドを作成します。
const thread = await mastraClient.createMemoryThread({
title: 'New Conversation',
metadata: { category: 'support' },
resourceId: 'resource-1',
agentId: 'agent-1',
})
特定のスレッドを操作特定のスレッドを操作への直接リンク
特定のメモリスレッドのインスタンスを取得します。
const thread = mastraClient.getMemoryThread({ threadId: 'thread-id', agentId: 'agent-id' })
スレッドのメソッドスレッドのメソッドへの直接リンク
スレッドの詳細を取得スレッドの詳細を取得への直接リンク
特定のスレッドの詳細を取得します。
const details = await thread.get()
スレッドを更新スレッドを更新への直接リンク
スレッドのプロパティを更新します。
const updated = await thread.update({
title: 'Updated Title',
metadata: { status: 'resolved' },
resourceId: 'resource-1',
})
スレッドを削除スレッドを削除への直接リンク
スレッドとそのメッセージを削除します。
await thread.delete()
スレッドを複製スレッドを複製への直接リンク
すべてのメッセージを含むスレッドのコピーを作成します。
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:複製メタデータを持つ、新しく作成された複製スレッドclonedMessages:新しい ID が付与された複製メッセージの配列
メッセージ操作メッセージ操作への直接リンク
メッセージを保存メッセージを保存への直接リンク
メッセージをメモリに保存します。
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)
スレッドのメッセージを取得スレッドのメッセージを取得への直接リンク
メモリスレッドに関連付けられたメッセージを取得します。
// 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 など、プロトタイプで予約されているキーは使用できません。パフォーマンスはサーバー側のストレージバックエンドに依存し、任意のメタデータフィルターによって候補メッセージのスキャンが発生する場合があります。
メッセージを削除メッセージを削除への直接リンク
スレッドから 1 件以上のメッセージを削除します。
// 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" }
ワーキングメモリワーキングメモリへの直接リンク
ワーキングメモリを使用すると、Agent は複数回の対話にわたってユーザーに関する情報を保持できます。特定のスレッドに限定することも、あるリソース(ユーザー)のすべてのスレッドを対象にすることもできます。
ワーキングメモリを取得ワーキングメモリを取得への直接リンク
スレッドの現在のワーキングメモリを取得します。
const workingMemory = await mastraClient.getWorkingMemory({
agentId: 'agent-1',
threadId: 'thread-1',
resourceId: 'user-123', // Optional, required for resource-scoped memory
})
レスポンスには以下が含まれます。
workingMemory:現在のワーキングメモリの内容(文字列または null)source:メモリのスコープが"thread"と"resource"のどちらかworkingMemoryTemplate:ワーキングメモリに使用するテンプレート(設定されている場合)threadExists:スレッドが存在するかどうか
ワーキングメモリを更新ワーキングメモリを更新への直接リンク
スレッドのワーキングメモリの内容を更新します。
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 }
リソースをスコープとするワーキングメモリでは、resourceId パラメーターを指定する必要があります。これにより、そのユーザーのすべての会話スレッドにわたってメモリを保持できます。
メモリの状態を取得メモリの状態を取得への直接リンク
メモリシステムの状態を確認します。
const status = await mastraClient.getMemoryStatus('agent-id')