> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # Memory.listThreads() `listThreads()` メソッドは、ページネーションに対応し、`resourceId`、`metadata`、またはその両方による任意の絞り込みを使用してスレッドを取得します。 ## 使用例 ### ページネーションを使用してすべてのスレッドを一覧表示する ```typescript const result = await memory.listThreads({ page: 0, perPage: 10, }) ``` ### ページネーションを使用せずにすべてのスレッドを取得する 一致するすべてのスレッドを一度に取得するには、`perPage: false` を使用します。 > **警告:** 特に大規模なデータセットでは、ページネーションを使用してください。このオプションは慎重に使用してください。 ```typescript const result = await memory.listThreads({ filter: { resourceId: 'user-123' }, perPage: false, }) ``` ### `resourceId` で絞り込む ```typescript const result = await memory.listThreads({ filter: { resourceId: 'user-123' }, page: 0, perPage: 10, }) ``` ### メタデータで絞り込む ```typescript const result = await memory.listThreads({ filter: { metadata: { category: 'support', priority: 'high' } }, page: 0, perPage: 10, }) ``` ### 複合フィルター(resourceId と metadata) ```typescript const result = await memory.listThreads({ filter: { resourceId: 'user-123', metadata: { status: 'active' }, }, page: 0, perPage: 10, }) ``` ## パラメーター **filter** (`{ resourceId?: string; metadata?: Record }`): 任意のフィルターオブジェクト。resourceId はリソース ID でスレッドを絞り込みます。metadata はメタデータのキーと値のペアでスレッドを絞り込みます(AND 条件ですべてが一致する必要があります) **page** (`number`): 取得するページ番号(0 始まり) **perPage** (`number | false`): ページごとに返すスレッドの最大数。すべて取得する場合は false **orderBy** (`{ field: 'createdAt' | 'updatedAt', direction: 'ASC' | 'DESC' }`): フィールドと方向を指定する並べ替え設定(デフォルトは { field: 'createdAt', direction: 'DESC' }) ## 戻り値 **result** (`Promise`): メタデータを含む、ページ分割されたスレッド結果へ解決される Promise 戻り値のオブジェクトには、次の項目が含まれます。 - `threads`:スレッドオブジェクトの配列 - `total`:フィルターに一致するスレッドの総数 - `page`:現在のページ番号(入力の `page` パラメーターと同じ) - `perPage`:ページあたりの項目数(入力の `perPage` パラメーターと同じ) - `hasMore`:さらに結果があるかどうかを示す真偽値 ## 詳細な使用例 ```typescript 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 条件が使用されます。スレッドを結果に含めるには、指定したすべてのキーと値のペアが一致する必要があります。 ```typescript // This will only return threads where BOTH conditions are true: // - category === 'support' // - priority === 'high' await memory.listThreads({ filter: { metadata: { category: 'support', priority: 'high', }, }, }) ``` ## 関連項目 - [Memory クラスリファレンス](https://mastra.zisheng.pro/ja/reference/memory/memory-class) - [Memory 入門](https://mastra.zisheng.pro/ja/docs/memory/overview) - [createThread](https://mastra.zisheng.pro/ja/reference/memory/createThread) - [getThreadById](https://mastra.zisheng.pro/ja/reference/memory/getThreadById)