跳至主要內容

Memory.createThread()

.createThread() 方法在 memory 系統中建立新的對話 thread。每個 thread 代表一段獨立的對話或情境,並可包含多則訊息。

使用範例
使用範例 的直接連結

await memory?.createThread({ resourceId: 'user-123' })

參數
參數 的直接連結

resourceId:

string
此 thread 所屬資源的標識符(例如使用者 ID、項目 ID)

threadId?:

string
thread 的自訂 ID(選填)。如未提供,系統會產生一個 ID。

title?:

string
thread 的標題(選填)

metadata?:

Record<string, unknown>
要與 thread 關聯的 metadata(選填)

傳回值
傳回值 的直接連結

id:

string
已建立 thread 的唯一標識符

resourceId:

string
與 thread 關聯的資源 ID

title:

string
thread 的標題(如有提供)

createdAt:

Date
thread 的建立時間

updatedAt:

Date
thread 上次更新的時間

metadata:

Record<string, unknown>
與 thread 關聯的其他 metadata

延伸使用範例
延伸使用範例 的直接連結

src/test-memory.ts
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)