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

Bài 12: Custom Agents & Agent Skills — Tạo AI chuyên biệt

Tạo custom agent (.agent.md): role, tools, instructions. YAML frontmatter. Agent Skills (SKILL.md): domain knowledge có thể tái sử dụng. Ví dụ: Code Reviewer agent, Documentation Writer agent, Test Generator agent. Chia sẻ agents trong team qua .github/agents/.

💻 Lập trình — Bài 12 Bài 12: Custom Agents & Agent Skills — Tạo AI chuyên biệt

Vibe Coding với GitHub Copilot: Từ Cơ bản đến Nâng cao

Phần 4: Customize & Extend Copilot

xdev.asia

1. Custom Agents là gì?

Custom Agents cho phép bạn tạo AI personas chuyên biệt cho các tasks cụ thể. Mỗi agent có:

  • Role: vai trò cụ thể (reviewer, tester, documenter)
  • Tools: danh sách tools được phép sử dụng
  • Instructions: hướng dẫn chi tiết cho behavior
  • Model: có thể chỉ định model cụ thể (optional)

2. Tạo Custom Agent

2.1. File structure

Custom agents là file .agent.md đặt trong .github/agents/:

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

2.2. Cấu trúc file .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. Ví dụ Custom Agents thực tế

3.1. Code Reviewer Agent

<!-- .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. Test Writer Agent

<!-- .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. Architect Agent

<!-- .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. Agent Skills (SKILL.md)

Agent Skills là domain knowledge modules có thể được tái sử dụng bởi nhiều agents. Mỗi skill là một file SKILL.md.

4.1. Tạo Skill

<!-- .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. Reference Skill trong Agent

<!-- 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. Sử dụng Custom Agents

  1. Mở Chat view (Ctrl+Cmd+I)
  2. Click agent dropdown (mặc định "Agent")
  3. Chọn custom agent bạn đã tạo (Reviewer, TestWriter, etc.)
  4. Prompt như bình thường — agent sẽ follow role và instructions

6. Cấu hình Tools cho Agents

Danh sách tools có thể assign:

Tool Chức năng Risk level
read Đọc files Thấp (read-only)
editFile Chỉnh sửa files Trung bình
runInTerminal Chạy commands Cao
search Search codebase Thấp
web Tìm kiếm web Thấp
agent Delegate to sub-agent Trung bình
vscode/askQuestions Hỏi user questions Thấp

Security tip: Review-only agents (Reviewer) nên không có editFile và runInTerminal.

7. Chia sẻ Agents trong Team

Commit toàn bộ .github/agents/ vào repository → mọi thành viên đều có cùng agents. Đây là cách chuẩn hóa AI workflow cho team.

8. Tổng kết

Concept File Purpose
Custom Agent .agent.md AI persona với role + tools + instructions
Agent Skill SKILL.md Domain knowledge module, reusable
Prompt File .prompt.md Reusable prompt template
Instructions .instructions.md File-type specific rules

Bài tiếp theo sẽ cover MCP Servers — kết nối Copilot với Figma, databases, APIs và mọi tool bên ngoài.