Memory.createThread()
.createThread() 方法在内存系统中创建一个新的对话线程。每个线程代表一个独立的对话或情境,并且可以包含多条消息。
🌐 The .createThread() method creates a new conversation thread in the memory system. Each thread represents a distinct conversation or context and can contain multiple messages.
使用示例Direct link to 使用示例
🌐 Usage Example
await memory?.createThread({ resourceId: "user-123" });
参数Direct link to 参数
🌐 Parameters
resourceId:
string
Identifier for the resource this thread belongs to (e.g., user ID, project ID)
threadId?:
string
Optional custom ID for the thread. If not provided, one will be generated.
title?:
string
Optional title for the thread
metadata?:
Record<string, unknown>
Optional metadata to associate with the thread
返回Direct link to 返回
🌐 Returns
id:
string
Unique identifier of the created thread
resourceId:
string
Resource ID associated with the thread
title:
string
Title of the thread (if provided)
createdAt:
Date
Timestamp when the thread was created
updatedAt:
Date
Timestamp when the thread was last updated
metadata:
Record<string, unknown>
Additional metadata associated with the thread
扩展使用示例Direct link to 扩展使用示例
🌐 Extended usage example
src/test-memory.ts
import { mastra } from "./mastra";
const agent = mastra.getAgent("agent");
const memory = await agent.getMemory();
const thread = await memory?.createThread({
resourceId: "user-123",
title: "Memory Test Thread",
metadata: {
source: "test-script",
purpose: "memory-testing",
},
});
const response = await agent.generate("message for agent", {
memory: {
thread: thread!.id,
resource: thread!.resourceId,
},
});
console.log(response.text);
相关Direct link to 相关
🌐 Related
- 内存类参考
- 内存入门(涵盖线程概念)
- getThreadById
- listThreads
- recall