资讯动态

如何用 Cline SDK 的 createTool 与 zod 构建读取 git diff 的代码审查机器人?

发布时间:2026/9/11 5:54:25 来源:尧图企业网站定制
如何用 Cline SDK 的 createTool 与 zod 构建读取 git diff 的代码审查机器人【免费下载链接】clineAutonomous coding agent as an SDK, IDE extension, or CLI assistant.项目地址: https://gitcode.com/GitHub_Trending/cl/cline这篇教程解决一个具体任务用 Cline SDK 的createTool与 zod 构建一个代码审查机器人它从本地 git 仓库读取 diff逐条输出带严重级别的审查意见并在结束时给出总结与 approve/reject 决策。教程基于仓库中的 Building an Agent 文档配套示例位于 apps/examples/code-review-bot。完成后的机器人做四件事从本地仓库读取 git diff按需读取完整文件内容作为上下文产出带严重级别的结构化审查意见以总结和 approve/reject 决策结束本次运行准备条件文档给出的前提Node.js 22一个 Anthropic API key一个至少包含一次提交的 git 仓库获取示例代码git clone https://gitcode.com/GitHub_Trending/cl/cline.git cd cline/apps/examples/code-review-bot bun install也可以不运行示例直接对照 示例源码 阅读。在自己的项目中使用 SDK 时按 examples 说明 安装公开包即可npm add cline/sdk。工具的写法参考 Creating Custom Tools 文档。每个工具有四个部分name唯一标识推荐 snake_case、description模型据此决定何时调用是最重要的字段、inputSchemazod 或 JSON Schema、execute真正干活的函数。SDK 会自动把 zod schema 转成 JSON Schemaexecute里的input是完全类型化的。用 createTool 与 zod 定义审查意见工具机器人用createTool加 zod schema 做类型安全的工具定义。审查意见工具的代码如下来自教程import { Agent, createTool } from cline/sdk import { z } from zod createTool({ name: add_review_comment, description: Add a review comment on a specific file and line., inputSchema: z.object({ file: z.string().describe(File path), line: z.number().describe(Line number (approximate is fine)), severity: z.enum([critical, warning, suggestion]), comment: z.string().describe(The review comment), }), async execute(input) { reviews.push(input) return Comment added (${reviews.length} total) }, })三个关键点z.enum把severity约束在固定取值集合内文档指出这会提升模型调用的准确性每个字段上的.describe()告诉模型该提供什么execute里把结果累积到数组reviews供运行结束后统一处理。用完成工具结束 Agent 循环没有显式的结束信号时agent 会一直循环到maxIterations。submit_review工具用lifecycle: { completesRun: true }标记为完成工具调用成功后 agent 循环结束createTool({ name: submit_review, description: Submit the completed review with a summary., inputSchema: z.object({ summary: z.string().describe(Brief overall assessment of the changes), approve: z.boolean().describe(Whether the changes look good to merge), }), lifecycle: { completesRun: true }, async execute(input) { return JSON.stringify({ summary: input.summary, approve: input.approve }) }, })两个工具通过Agent的tools数组注册注册写法见 Creating Custom Toolsconst agent new Agent({ tools: [addReviewComment, submitReview], // ... })系统提示词固定审查工作流教程用一段结构化的系统提示词规定审查范围和流程。明确告诉 agent 用哪个工具、何时用可以保持工作流可预测const agent new Agent({ systemPrompt: You are a senior code reviewer. Analyze the git diff provided and leave review comments using the add_review_comment tool. Focus on: - Bugs and logic errors (critical) - Security issues (critical) - Performance problems (warning) - Style and readability improvements (suggestion) When you are done reviewing, call submit_review with a brief summary., // ... })事件流运行中即时看到审查意见通过subscribe订阅事件可以在 agent 工作时流式输出进度而不是等整个 run 结束agent.subscribe((event) { switch (event.type) { case assistant-text-delta: process.stdout.write(event.text ?? ) break case tool-started: if (event.toolCall.toolName add_review_comment) { const input event.toolCall.input console.log( [${input.severity}] ${input.file}:${input.line} - ${input.comment}) } break } })教程的用法是tool-started事件里按工具名过滤每产生一条意见就立刻打印出来。运行与验证运行阶段把 diff 交给 agent结束后按严重级别分组汇总。diff变量即本地仓库的 git diff 内容const result await agent.run(Review this git diff:\n\n\\\diff\n${diff}\n\\\) const critical reviews.filter((r) r.severity critical) const warnings reviews.filter((r) r.severity warning) const suggestions reviews.filter((r) r.severity suggestion)在示例目录中启动sk-ant-...处替换为你自己的 Anthropic API keyANTHROPIC_API_KEYsk-ant-... bun dev # review last commit ANTHROPIC_API_KEYsk-ant-... bun dev main # review against main文档描述的预期行为运行过程中每条审查意见随tool-started事件流式打印格式为[severity] file:line - comment运行结束后程序把reviews数组按critical/warning/suggestion分组打印汇总agent 调用submit_review后本次运行干净结束返回值包含总结与 approve/reject 布尔值。一个需要留意的差异仓库当前 code-review-bot 示例 已演进为面向真实 GitHub PR 的审查面板使用CLINE_API_KEYbun run build:sdk后bun dev打开 http://localhost:3457 粘贴 PR URL 运行它的工具集是get_file_context、add_review_finding、submit_review。上面的ANTHROPIC_API_KEY命令与本地 git diff 流程对应的是教程描述的早期版本。如果你只需要本地 diff 两个工具的最小实现按本文代码自行搭一个工程即可两个版本共用的模式是createTool zod schema、lifecycle: { completesRun: true }和事件订阅。可选延伸教程给出的后续方向均需要自行实现加一个工具把审查意见通过 GitHub API 发回 PR用continue()对具体发现做追问加一个checkstyle工具对改动文件跑 linter接 webhook 实现 PR 自动审查。工具本身的测试与错误处理细节如把错误作为结构化数据返回而不是抛异常见 Creating Custom Tools。【免费下载链接】clineAutonomous coding agent as an SDK, IDE extension, or CLI assistant.项目地址: https://gitcode.com/GitHub_Trending/cl/cline创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价