跳至主要內容

Memory.createThread()

.createThread() 方法會在記憶體系統中建立新的對話執行緒。每個執行緒代表不同的對話或情境,並可包含多則訊息。

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

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

參數
「參數」的直接連結

resourceId:

string
此執行緒所屬資源的識別碼(例如使用者 ID、專案 ID)

threadId?:

string
執行緒的選用自訂 ID。若未提供,系統會自動產生。

title?:

string
執行緒的選用標題

metadata?:

Record<string, unknown>
要與執行緒建立關聯的選用中繼資料

回傳值
「回傳值」的直接連結

id:

string
所建立執行緒的唯一識別碼

resourceId:

string
與執行緒相關聯的資源 ID

title:

string
執行緒標題(若有提供)

createdAt:

Date
建立執行緒的時間戳記

updatedAt:

Date
上次更新執行緒的時間戳記

metadata:

Record<string, unknown>
與執行緒相關聯的其他中繼資料

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

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)