建立程式碼審查機械人
本指南會教你建立程式碼審查機械人,使用 Workspace Skill 自動審查 pull request。機械人會從 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 的程式碼審查機械人 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 實例,以完成 Agent 定義:
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 },
})
測試機械人測試機械人 的直接連結
啟動 Studio,並與程式碼審查機械人互動,查看實際運作情況。
- 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;
}
機械人應會啟用 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
後續步驟後續步驟 的直接連結
你可以透過以下方式擴充此機械人:
- 為不同語言或框架加入 Skill
- 為安全檢查及效能審查建立 Skill
- 與 GitHub Actions 整合,自動審查 PR
- 建立可留下行內意見的 PR 留言機械人
深入了解: