Memory.listThreads()
listThreads() メソッドは、ページネーションに対応し、resourceId、metadata、またはその両方による任意の絞り込みを使用してスレッドを取得します。
使用例使用例への直接リンク
ページネーションを使用してすべてのスレッドを一覧表示するページネーションを使用してすべてのスレッドを一覧表示するへの直接リンク
const result = await memory.listThreads({
page: 0,
perPage: 10,
})
ページネーションを使用せずにすべてのスレッドを取得するページネーションを使用せずにすべてのスレッドを取得するへの直接リンク
一致するすべてのスレッドを一度に取得するには、perPage: false を使用します。
警告
特に大規模なデータセットでは、ページネーションを使用してください。このオプションは慎重に使用してください。
const result = await memory.listThreads({
filter: { resourceId: 'user-123' },
perPage: false,
})
resourceId で絞り込むfilter-by-resourceidへの直接リンク
const result = await memory.listThreads({
filter: { resourceId: 'user-123' },
page: 0,
perPage: 10,
})
メタデータで絞り込むメタデータで絞り込むへの直接リンク
const result = await memory.listThreads({
filter: { metadata: { category: 'support', priority: 'high' } },
page: 0,
perPage: 10,
})
複合フィルター(resourceId と metadata)複合フィルター(resourceId と metadata)への直接リンク
const result = await memory.listThreads({
filter: {
resourceId: 'user-123',
metadata: { status: 'active' },
},
page: 0,
perPage: 10,
})
パラメーターパラメーターへの直接リンク
filter?:
{ resourceId?: string; metadata?: Record<string, unknown> }
任意のフィルターオブジェクト。resourceId はリソース ID でスレッドを絞り込みます。metadata はメタデータのキーと値のペアでスレッドを絞り込みます(AND 条件ですべてが一致する必要があります)
page?:
number
取得するページ番号(0 始まり)
perPage?:
number | false
ページごとに返すスレッドの最大数。すべて取得する場合は false
orderBy?:
{ field: 'createdAt' | 'updatedAt', direction: 'ASC' | 'DESC' }
フィールドと方向を指定する並べ替え設定(デフォルトは { field: 'createdAt', direction: 'DESC' })
戻り値戻り値への直接リンク
result:
Promise<StorageListThreadsOutput>
メタデータを含む、ページ分割されたスレッド結果へ解決される Promise
戻り値のオブジェクトには、次の項目が含まれます。
threads:スレッドオブジェクトの配列total:フィルターに一致するスレッドの総数page:現在のページ番号(入力のpageパラメーターと同じ)perPage:ページあたりの項目数(入力のperPageパラメーターと同じ)hasMore:さらに結果があるかどうかを示す真偽値
詳細な使用例詳細な使用例への直接リンク
src/test-memory.ts
import { mastra } from './mastra'
const agent = mastra.getAgent('agent')
const memory = await agent.getMemory()
let currentPage = 0
const perPage = 25
let hasMorePages = true
// Fetch all active threads for a user, sorted by creation date
while (hasMorePages) {
const result = await memory?.listThreads({
filter: {
resourceId: 'user-123',
metadata: { status: 'active' },
},
page: currentPage,
perPage: perPage,
orderBy: { field: 'createdAt', direction: 'ASC' },
})
if (!result) {
console.log('No threads')
break
}
result.threads.forEach(thread => {
console.log(`Thread: ${thread.id}, Created: ${thread.createdAt}`)
})
hasMorePages = result.hasMore
currentPage++ // Move to next page
}
メタデータによる絞り込みメタデータによる絞り込みへの直接リンク
メタデータフィルターには AND 条件が使用されます。スレッドを結果に含めるには、指定したすべてのキーと値のペアが一致する必要があります。
// This will only return threads where BOTH conditions are true:
// - category === 'support'
// - priority === 'high'
await memory.listThreads({
filter: {
metadata: {
category: 'support',
priority: 'high',
},
},
})