建置程式碼審查機器人
在本指南中,你將建置一個使用 Workspace Skill 自動審查 PR 的程式碼審查機器人。機器人會從 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 指示檔案的結構化目錄。程式碼標準 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 留言機器人
深入了解: