Memory.getThreadsByResourceId()
The .getThreadsByResourceId()
function retrieves all threads associated with a specific resource ID from storage. Threads can be sorted by creation or modification time in ascending or descending order.
Usage Example
await memory?.getThreadsByResourceId({ resourceId: "user-123" });
Parameters
resourceId:
string
The ID of the resource whose threads are to be retrieved.
orderBy?:
ThreadOrderBy
Field to sort threads by. Accepts 'createdAt' or 'updatedAt'. Default: 'createdAt'
sortDirection?:
ThreadSortDirection
Sort order direction. Accepts 'ASC' or 'DESC'. Default: 'DESC'
Returns
StorageThreadType[]:
Promise
A promise that resolves to an array of threads associated with the given resource ID.
Extended usage example
src/test-memory.ts
import { mastra } from "./mastra";
const agent = mastra.getAgent("agent");
const memory = await agent.getMemory();
const thread = await memory?.getThreadsByResourceId({
resourceId: "user-123",
orderBy: "updatedAt",
sortDirection: "ASC"
});
console.log(thread);
Related
- Memory Class Reference
- getThreadsByResourceIdPaginated - Paginated version
- Getting Started with Memory (Covers threads/resources concept)
- createThread
- getThreadById