跳到主要内容

克隆线程工具方法

Memory 类提供了用于处理克隆线程的工具方法。借助这些方法,你可以检查克隆状态、检索克隆元数据、遍历克隆关系并追踪克隆历史。

isClone()
isclone的直接链接

检查一个线程是否为另一线程的克隆。

用法
用法的直接链接

const isClonedThread = memory.isClone(thread)

参数
参数的直接链接

thread:

StorageThreadType
要检查的线程对象。

示例
示例的直接链接

const thread = await memory.getThreadById({ threadId: 'some-thread-id' })

if (memory.isClone(thread)) {
console.log('This thread was cloned from another thread')
} else {
console.log('This is an original thread')
}

getCloneMetadata()
getclonemetadata的直接链接

如果线程中存在克隆元数据,则检索该元数据。

用法
用法的直接链接

const metadata = memory.getCloneMetadata(thread)

参数
参数的直接链接

thread:

StorageThreadType
要从中提取克隆元数据的线程对象。

示例
示例的直接链接

const thread = await memory.getThreadById({ threadId: 'cloned-thread-id' })
const cloneInfo = memory.getCloneMetadata(thread)

if (cloneInfo) {
console.log(`Cloned from: ${cloneInfo.sourceThreadId}`)
console.log(`Cloned at: ${cloneInfo.clonedAt}`)
}

getSourceThread()
getsourcethread的直接链接

检索创建克隆线程时所基于的原始源线程。

用法
用法的直接链接

const sourceThread = await memory.getSourceThread(threadId)

参数
参数的直接链接

threadId:

string
克隆线程的 ID。

示例
示例的直接链接

const sourceThread = await memory.getSourceThread('cloned-thread-id')

if (sourceThread) {
console.log(`Original thread title: ${sourceThread.title}`)
console.log(`Original thread created: ${sourceThread.createdAt}`)
}

listClones()
listclones的直接链接

列出从指定源线程克隆的所有线程。

用法
用法的直接链接

const clones = await memory.listClones(sourceThreadId)

参数
参数的直接链接

sourceThreadId:

string
要查找其克隆线程的源线程 ID。

示例
示例的直接链接

const clones = await memory.listClones('original-thread-id')

console.log(`Found ${clones.length} clones`)
for (const clone of clones) {
console.log(`- ${clone.id}: ${clone.title}`)
}

getCloneHistory()
getclonehistory的直接链接

检索线程的完整克隆历史链,并向前追溯至原始线程。

用法
用法的直接链接

const history = await memory.getCloneHistory(threadId)

参数
参数的直接链接

threadId:

string
要获取其克隆历史的线程 ID。

示例
示例的直接链接

// If thread-c was cloned from thread-b, which was cloned from thread-a
const history = await memory.getCloneHistory('thread-c')

// history = [thread-a, thread-b, thread-c]
console.log(`Clone depth: ${history.length - 1}`)
console.log(`Original thread: ${history[0].id}`)
console.log(`Current thread: ${history[history.length - 1].id}`)

// Display the clone chain
for (let i = 0; i < history.length; i++) {
const prefix = i === 0 ? 'Original' : `Clone ${i}`
console.log(`${prefix}: ${history[i].title}`)
}

完整示例
完整示例的直接链接

src/clone-management.ts
import { mastra } from './mastra'

async function manageClones() {
const agent = mastra.getAgent('agent')
const memory = await agent.getMemory()

// Create an original conversation
const originalThread = await memory.createThread({
resourceId: 'user-123',
title: 'Original Conversation',
})

// Have a conversation...
await agent.generate("Hello! Let's discuss project options.", {
memory: {
thread: originalThread.id,
resource: 'user-123',
},
})

// Create multiple branches (clones) to explore different paths
const { thread: optionA } = await memory.cloneThread({
sourceThreadId: originalThread.id,
title: 'Option A - Conservative Approach',
})

const { thread: optionB } = await memory.cloneThread({
sourceThreadId: originalThread.id,
title: 'Option B - Aggressive Approach',
})

// Check clone status
console.log(memory.isClone(originalThread)) // false
console.log(memory.isClone(optionA)) // true
console.log(memory.isClone(optionB)) // true

// Get clone metadata
const metadataA = memory.getCloneMetadata(optionA)
console.log(metadataA?.sourceThreadId) // originalThread.id

// List all clones of the original
const allClones = await memory.listClones(originalThread.id)
console.log(`Total alternatives: ${allClones.length}`) // 2

// Get source thread from a clone
const source = await memory.getSourceThread(optionA.id)
console.log(source?.id === originalThread.id) // true

// Create a deeper clone chain
const { thread: optionA2 } = await memory.cloneThread({
sourceThreadId: optionA.id,
title: 'Option A - Variant 2',
})

// Get the full history
const history = await memory.getCloneHistory(optionA2.id)
// history = [originalThread, optionA, optionA2]
console.log(`Clone depth: ${history.length - 1}`) // 2
}