Memory.cloneThread()
.cloneThread() 方法會建立現有對話 thread 的副本,並包含其中所有訊息。它支援從對話中的指定位置建立不同的對話路徑。啟用 semantic recall 時,此方法亦會為 cloned message 建立 vector embedding。
使用範例使用範例 的直接連結
以下範例會建立 Memory instance,並 clone 現有 thread。
import { Memory } from '@mastra/memory'
import { LibSQLStore } from '@mastra/libsql'
const memory = new Memory({
storage: new LibSQLStore({ id: 'memory-store', url: 'file:./memory.db' }),
})
const { thread, clonedMessages } = await memory.cloneThread({
sourceThreadId: 'original-thread-123',
})
參數參數 的直接連結
sourceThreadId:
newThreadId?:
resourceId?:
title?:
Clone of ${sourceThread.title};否則標題為空白。metadata?:
options?:
messageLimit?:
messageFilter?:
startDate?:
endDate?:
messageIds?:
傳回值傳回值 的直接連結
thread:
clonedMessages:
messageIdMap?:
Clone metadataClone metadata 的直接連結
Cloned thread 的 metadata 包含一個 clone property,其中包括:
sourceThreadId:
clonedAt:
lastMessageId?:
進階使用範例進階使用範例 的直接連結
import { mastra } from './mastra'
const agent = mastra.getAgent('agent')
const memory = await agent.getMemory()
// Clone a thread with all messages
const { thread: fullClone } = await memory.cloneThread({
sourceThreadId: 'original-thread-123',
title: 'Alternative Conversation Path',
})
// Clone with a custom ID
const { thread: customIdClone } = await memory.cloneThread({
sourceThreadId: 'original-thread-123',
newThreadId: 'my-custom-clone-id',
})
// Clone only the last 5 messages
const { thread: partialClone, clonedMessages } = await memory.cloneThread({
sourceThreadId: 'original-thread-123',
options: {
messageLimit: 5,
},
})
// Clone messages from a specific date range
const { thread: dateFilteredClone } = await memory.cloneThread({
sourceThreadId: 'original-thread-123',
options: {
messageFilter: {
startDate: new Date('2024-01-01'),
endDate: new Date('2024-01-31'),
},
},
})
// Clone specific messages
const { thread: selectedMessagesClone } = await memory.cloneThread({
sourceThreadId: 'original-thread-123',
options: {
messageFilter: {
messageIds: ['message-1', 'message-2'],
},
},
})
// Continue conversation on the cloned thread
const response = await agent.generate('Try a different approach', {
memory: {
thread: fullClone.id,
resource: fullClone.resourceId,
},
})
將 cloned thread.id 及 thread.resourceId 傳給 agent.generate(),即可從 cloned thread 繼續對話。
Vector embeddingVector embedding 的直接連結
當 Memory instance 已啟用 semantic recall,並已設定 vector store 及 embedder 時,cloneThread() 會自動為所有 cloned message 建立 vector embedding,確保語意搜尋可在 cloned thread 上正常運作。
在此範例中,embeddingModel 是項目所設定的 embedding model。
import { Memory } from '@mastra/memory'
import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
const memory = new Memory({
storage: new LibSQLStore({ id: 'memory-store', url: 'file:./memory.db' }),
vector: new LibSQLVector({ id: 'vector-store', url: 'file:./vector.db' }),
embedder: embeddingModel,
options: {
semanticRecall: true,
},
})
// Clone will also create embeddings for cloned messages
const { thread } = await memory.cloneThread({
sourceThreadId: 'original-thread',
})
// Semantic search works on the cloned thread
const results = await memory.recall({
threadId: thread.id,
vectorSearchString: 'search query',
})
Working memoryWorking memory 的直接連結
啟用 working memory 後,cloneThread() 會根據 working memory scope 及 clone 的 resourceId 複製或共用 working memory:
- Thread scope 的 working memory:Working memory 會複製至 cloned thread。
- Resource scope 的 working memory,且
resourceId相同:由於 source thread 與 cloned thread 屬於同一個 resource,因此會共用 working memory。 - Resource scope 的 working memory,且
resourceId不同:Working memory 會複製至 cloned thread 的 resource。
Observational MemoryObservational Memory 的直接連結
啟用 Observational Memory 後,cloneThread() 會自動 clone 與 source thread 相關的 OM 記錄。其行為視乎 OM scope:
- Thread scope 的 OM:OM 記錄會 clone 至新 thread。所有內部訊息 ID 參照都會重新映射至 cloned message。
- Resource scope 的 OM(
resourceId相同):由於 source thread 與 cloned thread 屬於同一個 resource,因此會共用 OM 記錄,不會建立副本。 - Resource scope 的 OM(
resourceId不同):OM 記錄會 clone 至新 resource。訊息 ID 會重新映射,而 observation 中所有用於識別 thread 的 tag 都會更新為參照 cloned thread。
只會 clone 目前(最新)的 OM generation,不會複製較舊的歷史 generation。暫時性處理狀態(observation/reflection 進行中 flag)會在 cloned record 上重設。