メインコンテンツへ移動

クローンされた Thread のユーティリティ

Memory クラスには、クローンされた Thread を操作するためのユーティリティメソッドが用意されています。これらのメソッドを使用すると、クローンの状態の確認、クローンのメタデータの取得、クローン関係のたどり直し、クローン履歴の追跡ができます。

isClone()
iscloneへの直接リンク

Thread が別の Thread のクローンかどうかを確認します。

使用方法
使用方法への直接リンク

const isClonedThread = memory.isClone(thread)

パラメーター
パラメーターへの直接リンク

thread:

StorageThreadType
確認する Thread オブジェクト。

例への直接リンク

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への直接リンク

Thread にクローンのメタデータが存在する場合は、そのメタデータを取得します。

使用方法
使用方法への直接リンク

const metadata = memory.getCloneMetadata(thread)

パラメーター
パラメーターへの直接リンク

thread:

StorageThreadType
クローンのメタデータを抽出する Thread オブジェクト。

例への直接リンク

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への直接リンク

クローンされた Thread の作成元である、元のソース Thread を取得します。

使用方法
使用方法への直接リンク

const sourceThread = await memory.getSourceThread(threadId)

パラメーター
パラメーターへの直接リンク

threadId:

string
クローンされた Thread の 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への直接リンク

指定したソース Thread からクローンされたすべての Thread を一覧表示します。

使用方法
使用方法への直接リンク

const clones = await memory.listClones(sourceThreadId)

パラメーター
パラメーターへの直接リンク

sourceThreadId:

string
クローンを検索するソース Thread の 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への直接リンク

元の Thread までさかのぼり、Thread のクローン履歴チェーン全体を取得します。

使用方法
使用方法への直接リンク

const history = await memory.getCloneHistory(threadId)

パラメーター
パラメーターへの直接リンク

threadId:

string
クローン履歴を取得する Thread の 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
}