Memory.cloneThread()
.cloneThread() 方法会创建现有对话线程的副本,包括其中的所有消息。这使得可以从对话中的特定点创建不同的对话路径。当启用语义回忆时,该方法还会为克隆的消息创建向量嵌入。
🌐 The .cloneThread() method creates a copy of an existing conversation thread, including all its messages. This enables creating divergent conversation paths from a specific point in a conversation. When semantic recall is enabled, the method also creates vector embeddings for the cloned messages.
使用示例Direct link to 使用示例
🌐 Usage Example
const { thread, clonedMessages } = await memory.cloneThread({
sourceThreadId: "original-thread-123",
});
参数Direct link to 参数
🌐 Parameters
sourceThreadId:
newThreadId?:
resourceId?:
title?:
metadata?:
options?:
选项参数Direct link to 选项参数
🌐 Options Parameters
messageLimit?:
messageFilter?:
消息过滤器参数Direct link to 消息过滤器参数
🌐 MessageFilter Parameters
startDate?:
endDate?:
messageIds?:
返回Direct link to 返回
🌐 Returns
thread:
clonedMessages:
克隆元数据Direct link to 克隆元数据
🌐 Clone Metadata
克隆线程的元数据包括一个具有以下内容的 clone 属性:
🌐 The cloned thread's metadata includes a clone property with:
sourceThreadId:
clonedAt:
lastMessageId?:
扩展使用示例Direct link to 扩展使用示例
🌐 Extended Usage Example
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"),
},
},
});
// Continue conversation on the cloned thread
const response = await agent.generate("Let's try a different approach", {
threadId: fullClone.id,
resourceId: fullClone.resourceId,
});
向量嵌入Direct link to 向量嵌入
🌐 Vector Embeddings
当 Memory 实例启用语义回忆(并配置了向量存储和嵌入器)时,cloneThread() 会自动为所有克隆的消息创建向量嵌入。这确保了在克隆线程上语义搜索能够正常工作。
🌐 When the Memory instance has semantic recall enabled (with a vector store and embedder configured), cloneThread() automatically creates vector embeddings for all cloned messages. This ensures that semantic search works correctly on the cloned thread.
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",
});
相关Direct link to 相关
🌐 Related