Chuyển đến nội dung chính

第 12 課:自訂代理程式和代理程式技能 — 創建專門的 AI

建立自訂代理程式 (.agent.md):角色、工具、說明。 YAML 的前沿內容。代理技能 (SKILL.md):可重複使用的領域知識。例如:程式碼審閱者代理程式、文件編寫者代理程式、測試生成器代理程式。透過 .github/agents/ 在團隊內共用代理程式。

💻 程式設計 — 第 12 課 第 12 課:自訂代理程式和代理程式技能 — 創建 專業人工智慧

使用 GitHub Copilot 進行 Vibe 編碼:從基礎知識到高級

第 4 部分:自訂和擴充 Copilot

亞洲開發網

1. 什麼是定制代理?

自訂代理程式可讓您創建 專門的人工智慧角色 用於特定任務。每個代理有:

  • 角色:特定角色(審閱者、測試者、文件者)
  • 工具:允許使用的工具列表
  • 使用說明:行為的詳細說明
  • 型號:可指定具體型號(可選)

2. 建立自訂代理

2.1.文件結構

自訂代理是文件 .agent.md 放入 .github/代理/:

project/
├── .github/
│   └── agents/
│       ├── Reviewer.agent.md
│       ├── TestWriter.agent.md
│       ├── DocWriter.agent.md
│       └── Architect.agent.md

2.2. .agent.md 檔案結構

---
name: 'AgentName'
description: 'Mô tả ngắn về agent'
tools: ['tool1', 'tool2', ...]
---
# System Instructions

Chi tiết hướng dẫn cho agent...

3. 現實的自訂代理範例

3.1.代碼審查代理

<!-- .github/agents/Reviewer.agent.md -->
---
name: 'Reviewer'
description: 'Senior code reviewer - phân tích code quality và best practices'
tools: ['vscode/askQuestions', 'vscode/vscodeAPI', 'read', 'agent', 'search', 'web']
---
# Code Reviewer Agent

You are an experienced senior developer conducting thorough code reviews. Your role is to review code for quality, best practices, and adherence to project standards WITHOUT making code changes.

Review Checklist

  1. Security: Check for injection, auth flaws, secrets exposure
  2. Performance: N+1 queries, unnecessary re-renders, memory leaks
  3. Error handling: Missing try-catch, unhandled promises
  4. Type safety: Any usage of any, missing types
  5. Testing: Coverage gaps, missing edge cases
  6. Naming: Clear, consistent naming following conventions
  7. DRY: Code duplication that should be extracted

Output Format

Structure your review with:

  • 🔴 Critical: Must fix before merge
  • 🟡 Warning: Should fix, but not blocking
  • 🟢 Suggestion: Nice-to-have improvements
  • 💡 Note: Observations and learning points

Important

  • DO NOT write or modify code
  • Ask clarifying questions about design decisions
  • Explain WHY something is a problem, not just WHAT

3.2.測試編寫代理

<!-- .github/agents/TestWriter.agent.md -->
---
name: 'TestWriter'
description: 'Chuyên gia viết tests - unit, integration, E2E'
tools: ['read', 'editFile', 'search', 'runInTerminal', 'getErrors']
---
# Test Writer Agent

You are a testing expert specializing in writing comprehensive tests.

Testing Strategy

  • Unit tests: For pure functions, utilities, services
  • Integration tests: For API endpoints, database operations
  • Component tests: For React components with user interactions

Conventions

  • Framework: Vitest (follow existing test patterns)
  • Place test files alongside source: component.test.tsx
  • Use describe blocks for grouping, it for individual tests
  • Use meaningful test names: "should [action] when [condition]"
  • Always include: happy path, edge cases, error scenarios

Test Patterns

  • Arrange → Act → Assert
  • One assertion per test (preferred)
  • Mock external dependencies, not internal logic
  • Use factories for test data, not hardcoded values

Before writing tests:

  1. Read the source code thoroughly
  2. Identify all code paths and branches
  3. Check existing tests for patterns
  4. Write tests, run them, fix any failures

3.3.建築師代理

<!-- .github/agents/Architect.agent.md -->
---
name: 'Architect'
description: 'Solution architect - thiết kế kiến trúc và đánh giá technical decisions'
tools: ['read', 'search', 'web', 'vscode/askQuestions', 'agent']
---
# Architect Agent

You are a senior solution architect who evaluates technical decisions and designs system architecture.

Responsibilities

  • Analyze requirements and propose architecture
  • Evaluate trade-offs between approaches
  • Create technical design documents
  • Review architectural decisions for scalability and maintainability

When proposing solutions:

  1. List at least 2-3 alternative approaches
  2. Compare with pros/cons table
  3. Consider: scalability, maintainability, cost, team expertise
  4. Recommend with clear reasoning
  5. Identify risks and mitigation strategies

Output includes:

  • Architecture diagram (text-based or mermaid)
  • Component responsibilities
  • Data flow description
  • Technology choices with rationale
  • Phase-by-phase implementation plan

4. 代理技能(SKILL.md)

代理技能是 領域知識模組 可以被多個代理重複使用。每个技能都是一个文件 技能.md。

4.1.創造技能

<!-- .github/skills/database-migration/SKILL.md -->
---
name: 'Database Migration'
description: 'Best practices cho Prisma database migrations'
---
# Database Migration Skill

Migration Checklist

  1. Always create migration with descriptive name
  2. Test migration on staging data before production
  3. Include rollback strategy for every migration
  4. Never modify existing migrations after push
  5. Use prisma migrate diff to review changes

Common Patterns

  • Adding nullable column: safe, no downtime
  • Renaming column: 3-step migration (add new → copy data → drop old)
  • Adding index: use CREATE INDEX CONCURRENTLY for large tables

Commands

# Create migration
npx prisma migrate dev --name add_user_roles

# Apply to production
npx prisma migrate deploy

# Reset (development only!)
npx prisma migrate reset

4.2.代理參考技能

<!-- Trong custom agent, reference skills: -->
---
name: 'DBAdmin'
description: 'Database administration agent'
tools: ['read', 'editFile', 'runInTerminal']
---
# DB Admin Agent

Follow the practices defined in Database Migration Skill.

5.使用自訂代理

  1. 開啟聊天視圖 (Ctrl+Cmd+I)
  2. 點選代理下拉選單(預設“代理”)
  3. 選擇您建立的自訂代理程式(審閱者、測試編寫者等)
  4. 像往常一樣提示 - 代理將遵循角色和指示

6. 為代理程式配置工具

可指派工具清單:

工具 功能 風險等級
讀 讀取文件 低(唯讀)
編輯文件 編輯文件 平均
運作終端 運行命令 高
搜尋。搜尋 搜尋程式碼庫 低
網路 搜尋網絡 低
代理。代理人 委託給子代理 平均
vscode/ask問題 詢問用戶問題 低

安全提示: 僅審閱代理(審閱者)應 不 編輯文件 和 運作終端。

7. 在團隊中共享代理

全部提交 .github/代理/ 轉到儲存庫 → 每個成員都有相同的代理。這是標準化團隊人工智慧工作流程的一種方法。

八、總結

概念 文件 目的
客製化代理 .agent.md 具有角色+工具+指令的AI角色
代理技能 技能.md 領域知識模組,可重複使用
提示文件 .提示.md 可重複使用的提示模板
使用說明 .說明.md 文件類型特定規則

下一首歌曲將是翻唱歌曲 MCP伺服器 — 將 Copilot 連接到 Figma、資料庫、API 和任何外部工具。