Memory.getThreadsByResourceIdPaginated()
.getThreadsByResourceIdPaginated()
メソッドは、特定のリソース ID に紐づくスレッドを、ページネーションに対応して取得します。
使用例
await memory.getThreadsByResourceIdPaginated({
resourceId: "user-123",
page: 0,
perPage: 10
});
パラメータ
resourceId:
string
スレッドを取得するリソースのID
page:
number
取得するページ番号
perPage:
number
1ページあたりのスレッド返却数
orderBy?:
'createdAt' | 'updatedAt'
スレッドの並び替え対象フィールド
sortDirection?:
'ASC' | 'DESC'
並び順の方向
戻り値
result:
Promise<PaginationInfo & { threads: StorageThreadType[] }>
メタデータ付きのページネーション済みスレッド結果を返す Promise
さらに踏み込んだ使用例
src/test-memory.ts
import { mastra } from "./mastra";
const agent = mastra.getAgent("agent");
const memory = await agent.getMemory();
let currentPage = 0;
let hasMorePages = true;
while (hasMorePages) {
const threads = await memory?.getThreadsByResourceIdPaginated({
resourceId: "user-123",
page: currentPage,
perPage: 25,
orderBy: "createdAt",
sortDirection: "ASC"
});
if (!threads) {
console.log("No threads");
break;
}
threads.threads.forEach((thread) => {
console.log(`Thread: ${thread.id}, 作成日時: ${thread.createdAt}`);
});
hasMorePages = threads.hasMore;
currentPage++;
}
関連
- Memory クラスリファレンス
- getThreadsByResourceId - ページネーションなしのバージョン
- Memory のはじめかた(スレッド/リソースの概念を解説)
- createThread
- getThreadById