> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # Memory.createThread() `.createThread()` 方法在 memory 系統中建立新的對話 thread。每個 thread 代表一段獨立的對話或情境,並可包含多則訊息。 ## 使用範例 ```typescript await memory?.createThread({ resourceId: 'user-123' }) ``` ## 參數 **resourceId** (`string`): 此 thread 所屬資源的標識符(例如使用者 ID、項目 ID) **threadId** (`string`): thread 的自訂 ID(選填)。如未提供,系統會產生一個 ID。 **title** (`string`): thread 的標題(選填) **metadata** (`Record`): 要與 thread 關聯的 metadata(選填) ## 傳回值 **id** (`string`): 已建立 thread 的唯一標識符 **resourceId** (`string`): 與 thread 關聯的資源 ID **title** (`string`): thread 的標題(如有提供) **createdAt** (`Date`): thread 的建立時間 **updatedAt** (`Date`): thread 上次更新的時間 **metadata** (`Record`): 與 thread 關聯的其他 metadata ## 延伸使用範例 ```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 class 參考](https://mastra.zisheng.pro/zh-HK/reference/memory/memory-class) - [Memory 入門](https://mastra.zisheng.pro/zh-HK/docs/memory/overview)(涵蓋 thread 概念) - [getThreadById](https://mastra.zisheng.pro/zh-HK/reference/memory/getThreadById) - [listThreads](https://mastra.zisheng.pro/zh-HK/reference/memory/listThreads) - [recall](https://mastra.zisheng.pro/zh-HK/reference/memory/recall)