본문으로 건너뛰기

복제된 스레드 유틸리티

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
}