关于 Convex Agents Threads
Convex Agents Threads 是帮助开发者使用 Convex 管理 Agent 对话的技能,涵盖线程的创建、续接、查询和删除,并通过用户关联和元数据整理对话。
主要功能
- 01创建对话线程
- 02续接现有线程
- 03按用户列出线程
- 04跟踪线程元数据
- 05同步或异步删除线程
适用场景
- 01为用户开启对话
- 02继续已有对话
- 03查找用户的对话历史
- 04删除已完成或过时的线程
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ sstobo/ convex-skills --skill convex-agents-threads安装后在 Claude Code 中输入/convex-agents-threads确认已加载。
详细介绍
用途
Thread 将相关消息分组为有序的线性对话历史。Agent 组件中的每条消息都属于一个 thread。
何时使用此技能
- 为用户创建新对话
- 管理对话历史和元数据
- 继续现有对话
- 查询 thread 内的消息历史
- 按用户或上下文组织对话
- 清理旧对话或已完成的对话
创建 Thread
import { createThread } from "@convex-dev/agent";
export const startNewThread = mutation({
args: { userId: v.string() },
handler: async (ctx, { userId }) => {
const threadId = await createThread(ctx, components.agent, {
userId,
title: "New Conversation",
summary: "Conversation summary",
});
return { threadId };
},
});
继续 Thread(仅 Actions)
在 actions 中,获取 thread 对象:
export const continueConversation = action({
args: { threadId: v.string(), prompt: v.string() },
handler: async (ctx, { threadId, prompt }) => {
const { thread } = await myAgent.continueThread(ctx, { threadId });
const metadata = thread.getMetadata();
const response = await thread.generateText({ prompt });
return response.text;
},
});
列出用户的 Thread
export const getUserThreads = query({
args: { userId: v.string(), paginationOpts: paginationOptsValidator },
handler: async (ctx, { userId, paginationOpts }) => {
return await ctx.runQuery(
components.agent.threads.listThreadsByUserId,
{ userId, paginationOpts }
);
},
});
删除 Thread
// Async deletion (non-blocking)
await myAgent.deleteThreadAsync(ctx, { threadId });
// Sync deletion (atomic)
await myAgent.deleteThreadSync(ctx, { threadId });
// Delete all for user
await ctx.runMutation(components.agent.users.deleteAllForUserId, { userId });
关键原则
- 用户关联:Thread 与 userId 关联
- 元数据:使用 title 和 summary 进行组织
- 消息排序:thread 内的消息保持顺序
- 异步 vs 同步:使用异步实现非阻塞,使用同步实现原子操作
后续步骤
- 参见 messages 以保存和检索消息
- 参见 fundamentals 以了解基本 agent 设置
评论
支持 Markdown