构建代码审查 bot
在本指南中,你将构建一个使用 Workspace Skill 自动审查 pull request 的代码审查 bot。该 bot 会从 Skill 文件加载编码标准,并提供结构化反馈。你将创建一个带有 Skill 目录的 Workspace,并定义一个包含审查说明和参考文件的 Agent Skill。然后,你会将该 Skill 连接到执行自动审查的 Agent。
前提条件前提条件的直接链接
- 已安装 Node.js
v22.13.0或更高版本 - 受支持的 Model Provider 提供的 API 密钥
- 现有的 Mastra 项目(按照安装指南设置新项目)
创建 Workspace创建 Workspace的直接链接
在 src/mastra/index.ts 文件中导入 Workspace 和 LocalFilesystem 类。在 Workspace 实例上,将 skills 选项配置为指向 Skill 目录。skills 目录位于文件系统的 basePath 内。
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 是包含 SKILL.md 文件的结构化目录,其中存放 Agent 的说明。代码标准 Skill 定义审查流程并引用样式指南。
在 workspace/skills 中新建名为 code-standards 的文件夹。创建 SKILL.md 文件并添加审查说明。
---
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。
# 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创建审查 Agent的直接链接
现在可以创建使用 code-standards Skill 的代码审查 bot Agent。在 src/mastra/agents/code-reviewer.ts 新建文件并定义 Agent:
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 实例:
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测试 bot的直接链接
启动 Studio 并与代码审查 bot 交互,查看它的实际表现。
- npm
- pnpm
- Yarn
- Bun
npm run dev
pnpm run dev
yarn dev
bun run dev
打开 localhost:4111,然后转到代码审查 Agent。
在聊天输入框中提供一段要审查的代码,例如:
Review this code:
function getData(id) {
var result = fetch('/api/data/' + id);
console.log(result);
return result;
}
bot 应激活 code-standards Skill 并提供结构化反馈。由于 Agent 回答并不确定,实际输出可能有所不同,但应该与下面的内容类似:
**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
了解更多: