克隆工具方法
🌐 Clone Utility Methods
Memory 类提供了用于处理克隆线程的实用方法。这些方法可以帮助你检查克隆状态、获取克隆元数据、导航克隆关系以及跟踪克隆历史。
🌐 The Memory class provides utility methods for working with cloned threads. These methods help you check clone status, retrieve clone metadata, navigate clone relationships, and track clone history.
isClone()Direct link to isClone()
检查一个线程是否是另一个线程的克隆。
🌐 Checks whether a thread is a clone of another thread.
用法Direct link to 用法
🌐 Usage
const isClonedThread = memory.isClone(thread);
参数Direct link to 参数
🌐 Parameters
thread:
返回Direct link to 返回
🌐 Returns
isClone:
示例Direct link to 示例
🌐 Example
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()Direct link to getCloneMetadata()
如果存在,则从线程中检索克隆元数据。
🌐 Retrieves the clone metadata from a thread if it exists.
用法Direct link to 用法
🌐 Usage
const metadata = memory.getCloneMetadata(thread);
参数Direct link to 参数
🌐 Parameters
thread:
返回Direct link to 返回
🌐 Returns
返回 ThreadCloneMetadata | null:
🌐 Returns ThreadCloneMetadata | null:
sourceThreadId:
clonedAt:
lastMessageId?:
示例Direct link to 示例
🌐 Example
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()Direct link to getSourceThread()
检索克隆线程创建自的原始源线程。
🌐 Retrieves the original source thread that a cloned thread was created from.
用法Direct link to 用法
🌐 Usage
const sourceThread = await memory.getSourceThread(threadId);
参数Direct link to 参数
🌐 Parameters
threadId:
返回Direct link to 返回
🌐 Returns
sourceThread:
示例Direct link to 示例
🌐 Example
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()Direct link to listClones()
列出所有从特定源线程克隆而来的线程。
🌐 Lists all threads that were cloned from a specific source thread.
用法Direct link to 用法
🌐 Usage
const clones = await memory.listClones(sourceThreadId);
参数Direct link to 参数
🌐 Parameters
sourceThreadId:
返回Direct link to 返回
🌐 Returns
clones:
示例Direct link to 示例
🌐 Example
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()Direct link to getCloneHistory()
检索线程的完整克隆历史链,追溯到最初的版本。
🌐 Retrieves the full clone history chain for a thread, tracing back to the original.
用法Direct link to 用法
🌐 Usage
const history = await memory.getCloneHistory(threadId);
参数Direct link to 参数
🌐 Parameters
threadId:
返回Direct link to 返回
🌐 Returns
history:
示例Direct link to 示例
🌐 Example
// 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}`);
}
完整示例Direct link to 完整示例
🌐 Complete Example
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.", {
threadId: originalThread.id,
resourceId: "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
}
相关Direct link to 相关
🌐 Related