Memory.createThread()
.createThread()
メソッドは、メモリシステム内に新しい会話スレッドを作成します。各スレッドは、個別の会話またはコンテキストを表し、複数のメッセージを含むことができます。
使い方の例
await memory?.createThread({ resourceId: "user-123" });
パラメータ
resourceId:
string
このスレッドが属するリソースの識別子(例:ユーザーID、プロジェクトID)
threadId?:
string
スレッドのカスタムID(任意)。指定しない場合は自動生成されます。
title?:
string
スレッドのタイトル(任意)
metadata?:
Record<string, unknown>
スレッドに関連付けるメタデータ(任意)
戻り値
id:
string
作成されたスレッドの固有識別子
resourceId:
string
スレッドに関連付けられたリソースID
title:
string
スレッドのタイトル(指定されている場合)
createdAt:
Date
スレッドの作成時刻のタイムスタンプ
updatedAt:
Date
スレッドの最終更新時刻のタイムスタンプ
metadata:
Record<string, unknown>
スレッドに関連する追加のメタデータ
応用使用例
src/test-memory.ts
import { mastra } from "./mastra";
const agent = mastra.getAgent("agent");
const memory = await agent.getMemory();
const thread = await memory?.createThread({
resourceId: "user-123",
title: "Memory Test Thread",
metadata: {
source: "test-script",
purpose: "memory-testing"
}
});
const response = await agent.generate("message for agent", {
memory: {
thread: thread!.id,
resource: thread!.resourceId
}
});
console.log(response.text);