Memory API
Memory APIは、Mastraにおける会話スレッドとメッセージ履歴を管理するためのメソッドを提供します。
すべてのスレッドを取得
特定のリソースのすべてのメモリスレッドを取得します:
const threads = await mastraClient.getMemoryThreads({
resourceId: "resource-1",
agentId: "agent-1",
});
新しいスレッドを作成
新しいメモリスレッドを作成します:
const thread = await mastraClient.createMemoryThread({
title: "New Conversation",
metadata: { category: "support" },
resourceId: "resource-1",
agentId: "agent-1",
});
特定のスレッドでの作業
特定のメモリスレッドのインスタンスを取得します:
const thread = mastraClient.getMemoryThread("thread-id", "agent-id");
Thread Methods
Get Thread Details
特定のスレッドの詳細を取得します:
const details = await thread.get();
Update Thread
スレッドのプロパティを更新します:
const updated = await thread.update({
title: "Updated Title",
metadata: { status: "resolved" },
resourceid: "resource-1",
});
Delete Thread
スレッドとそのメッセージを削除します:
await thread.delete();
メッセージ操作
メッセージの保存
メッセージをメモリに保存する:
const savedMessages = await mastraClient.saveMessageToMemory({
messages: [
{
role: "user",
content: "Hello!",
id: "1",
threadId: "thread-1",
createdAt: new Date(),
type: "text",
},
],
agentId: "agent-1",
});
スレッドメッセージの取得
メモリスレッドに関連付けられたメッセージを取得する:
// スレッド内のすべてのメッセージを取得
const { messages } = await thread.getMessages();
// 取得するメッセージ数を制限
const { messages } = await thread.getMessages({ limit: 10 });
メッセージの削除
スレッドから特定のメッセージを削除する:
const result = await thread.deleteMessage("message-id");
// 戻り値: { success: true, message: "Message deleted successfully" }
複数メッセージの削除
単一の操作でスレッドから複数のメッセージを削除する:
const result = await thread.deleteMessages(["message-1", "message-2", "message-3"]);
// 戻り値: { success: true, message: "3 messages deleted successfully" }
メモリステータスの取得
メモリシステムのステータスを確認する:
const status = await mastraClient.getMemoryStatus("agent-id");