> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # 构建代码审查 bot 在本指南中,你将构建一个使用 Workspace Skill 自动审查 pull request 的代码审查 bot。该 bot 会从 Skill 文件加载编码标准,并提供结构化反馈。你将创建一个带有 Skill 目录的 Workspace,并定义一个包含审查说明和参考文件的 [Agent Skill](https://agentskills.io)。然后,你会将该 Skill 连接到执行自动审查的 Agent。 ## 前提条件 - 已安装 Node.js `v22.13.0` 或更高版本 - 受支持的 [Model Provider](https://mastra.zisheng.pro/models) 提供的 API 密钥 - 现有的 Mastra 项目(按照[安装指南](https://mastra.zisheng.pro/guides/getting-started/quickstart)设置新项目) ## 创建 Workspace 在 `src/mastra/index.ts` 文件中导入 [`Workspace`](https://mastra.zisheng.pro/reference/workspace/workspace-class) 和 [`LocalFilesystem`](https://mastra.zisheng.pro/reference/workspace/local-filesystem) 类。在 `Workspace` 实例上,将 `skills` 选项配置为指向 Skill 目录。`skills` 目录位于文件系统的 `basePath` 内。 ```typescript import { Mastra } from '@mastra/core' import { resolve } from 'node:path' import { Workspace, LocalFilesystem } from '@mastra/core/workspace' const workspace = new Workspace({ filesystem: new LocalFilesystem({ basePath: resolve(import.meta.dirname, '../../workspace'), }), skills: ['skills'], }) export const mastra = new Mastra({ workspace, }) ``` 在项目根目录新建名为 `workspace` 的文件夹,并在其中创建 `skills` 文件夹。下一步会在这里定义代码标准 Skill。 ## 创建代码标准 Skill Skill 是包含 `SKILL.md` 文件的结构化目录,其中存放 Agent 的说明。代码标准 Skill 定义审查流程并引用样式指南。 在 `workspace/skills` 中新建名为 `code-standards` 的文件夹。创建 `SKILL.md` 文件并添加审查说明。 ```markdown --- name: code-standards description: Automated code review standards and checks --- # Code Review Standards Review code systematically using these steps: 1. **Critical Issues**: Security vulnerabilities, memory leaks, logic bugs, missing error handling 2. **Code Quality**: Functions over 50 lines, code duplication, confusing names, missing types 3. **Style Guide**: Check references/style-guide.md for naming and organization 4. **Linting**: Flag common issues like use of `var`, leftover `console.log` statements, and `debugger` statements Provide feedback in this format: **Summary**: One sentence overview **Critical Issues**: List with line numbers **Suggestions**: Improvements that would help **Positive Notes**: What the code does well ``` 在 `workspace/skills/code-standards` 中创建 `references` 文件夹,用于存放 Skill 的参考资料。编写一份介绍项目编码约定的样式指南,并将文件命名为 `style-guide.md`。 ````markdown # Style Guide ## Naming - Variables/Functions: `camelCase` - Constants: `UPPER_SNAKE_CASE` - Files: `kebab-case.ts` - Booleans: Start with `is`, `has`, `should` ## Code organization ```typescript // 1. Imports import { foo } from 'bar' // 2. Constants const MAX_SIZE = 100 // 3. Types interface User { id: string } // 4. Functions function doSomething() {} // 5. Exports export { doSomething } ``` ## Error handling Always handle errors explicitly - never silently catch. ## Comments Write "why" not "what". ```` ## 创建审查 Agent 现在可以创建使用 code-standards Skill 的代码审查 bot Agent。在 `src/mastra/agents/code-reviewer.ts` 新建文件并定义 Agent: ```typescript import { Agent } from '@mastra/core/agent' export const codeReviewer = new Agent({ id: 'code-reviewer', name: 'Code Review Bot', instructions: `You are an automated code reviewer. When asked to review code: 1. Activate the 'code-standards' skill 2. Follow the review process from the skill 3. Check against the style guide in skill references 4. Be constructive and specific with line numbers`, model: 'openai/gpt-5.6-sol', }) ``` 在 `src/mastra/index.ts` 中导入该 Agent,并将其注册到 `Mastra` 实例: ```typescript import { Mastra } from '@mastra/core' import { resolve } from 'node:path' import { Workspace, LocalFilesystem } from '@mastra/core/workspace' import { codeReviewer } from './agents/code-reviewer' const workspace = new Workspace({ filesystem: new LocalFilesystem({ basePath: resolve(import.meta.dirname, '../../workspace'), }), skills: ['skills'], }) export const mastra = new Mastra({ workspace, agents: { codeReviewer }, }) ``` ## 测试 bot 启动 [Studio](https://mastra.zisheng.pro/docs/studio/overview) 并与代码审查 bot 交互,查看它的实际表现。 **npm**: ```bash npm run dev ``` **pnpm**: ```bash pnpm run dev ``` **Yarn**: ```bash yarn dev ``` **Bun**: ```bash bun run dev ``` 打开 [localhost:4111](http://localhost:4111),然后转到代码审查 Agent。 在聊天输入框中提供一段要审查的代码,例如: ```text Review this code: function getData(id) { var result = fetch('/api/data/' + id); console.log(result); return result; } ``` bot 应激活 `code-standards` Skill 并提供结构化反馈。由于 Agent 回答并不确定,实际输出可能有所不同,但应该与下面的内容类似: ```md **Summary**: Function has several issues with variable declaration, debugging statements, and missing error handling. **Critical Issues**: - Missing error handling for fetch (line 2) - No async/await for asynchronous operation (line 2) **Suggestions**: - Use const instead of var (line 2) - Remove console.log before committing (line 3) - Add TypeScript type for id parameter - Use template literals instead of concatenation **Positive Notes**: - Function name is clear and descriptive ``` ## 后续步骤 你可以扩展此 bot: - 为不同语言或框架添加 Skill - 为安全检查和性能审查创建 Skill - 与 GitHub Actions 集成,自动审查 PR - 构建一个可留下行内反馈的 PR 评论 bot 了解更多: - [Agent Skills 规范](https://agentskills.io)