> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # Memory.createThread() `.createThread()` 方法在内存系统中创建新的对话线程。每个线程代表一个独立的对话或上下文,并且可以包含多条消息。 ## 用法示例 ```typescript await memory?.createThread({ resourceId: 'user-123' }) ``` ## 参数 **resourceId** (`string`): 此线程所属资源的标识符(例如用户 ID、项目 ID) **threadId** (`string`): 线程的可选自定义 ID。如果未提供,则会自动生成。 **title** (`string`): 线程的可选标题 **metadata** (`Record`): 与线程关联的可选元数据 ## 返回值 **id** (`string`): 所创建线程的唯一标识符 **resourceId** (`string`): 与线程关联的资源 ID **title** (`string`): 线程标题(如果提供) **createdAt** (`Date`): 线程的创建时间戳 **updatedAt** (`Date`): 线程最后更新的时间戳 **metadata** (`Record`): 与线程关联的其他元数据 ## 扩展用法示例 ```typescript 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) ``` ### 相关内容 - [Memory 类参考](https://mastra.zisheng.pro/reference/memory/memory-class) - [Memory 入门](https://mastra.zisheng.pro/docs/memory/overview)(涵盖线程概念) - [getThreadById](https://mastra.zisheng.pro/reference/memory/getThreadById) - [listThreads](https://mastra.zisheng.pro/reference/memory/listThreads) - [recall](https://mastra.zisheng.pro/reference/memory/recall)