Skip to content

Sub Agent

Sub Agent คือ AI Agent เฉพาะทางที่ถูกสร้างขึ้นเพื่อทำงานเฉพาะอย่าง แทนที่จะใช้ Agent หลักทำทุกอย่าง เราสามารถแบ่งงานให้ Agent ที่เชี่ยวชาญแต่ละด้านทำ

ประเภทของ Agent ใน OpenCode

Section titled “ประเภทของ Agent ใน OpenCode”
  • Build — ทำงานได้ทุกอย่าง (default)
  • Plan — อ่านอย่างเดียว เหมาะวางแผน
  • @general — ทำงานหลายอย่าง มีเครื่องมือครบ
  • @explore — หาข้อมูลใน codebase (read-only)
  • @scout — หาข้อมูลจากข้างนอก/dependency
Avatar blackCat
Sub Agent ก็เหมือนทีม specialist ที่พร้อมช่วยคุณในงานเฉพาะทาง!

วิธีเรียกใช้ Sub Agent

Section titled “วิธีเรียกใช้ Sub Agent”

1. @Mention (เรียกด้วยตัวเอง)

Section titled “1. @Mention (เรียกด้วยตัวเอง)”

พิมพ์ @ ตามด้วยชื่อ agent เพื่อเรียกใช้โดยตรง:

@explore หาว่า function calculateTotal อยู่ที่ไหน
@general ช่วย research เรื่อง React Server Components
@scout ดูว่า library นี้ใช้งานยังไง

2. Automatic (ให้ Primary Agent ตัดสินใจ)

Section titled “2. Automatic (ให้ Primary Agent ตัดสินใจ)”

ถ้าคำถามของคุณตรงกับ description ของ Sub Agent, Primary Agent จะเรียกใช้เองโดยอัตโนมัติ

"หาไฟล์ที่เกี่ยวกับ authentication ทั้งหมด"
→ Agent อาจเรียก @explore ให้อัตโนมัติ
"ช่วยตรวจ code นี้หน่อย"
→ Agent อาจเรียก Sub Agent ที่เป็น code reviewer

วิธีสร้าง Sub Agent ใหม่

Section titled “วิธีสร้าง Sub Agent ใหม่”

วิธีที่ 1: ใช้คำสั่ง (แนะนำ)

Section titled “วิธีที่ 1: ใช้คำสั่ง (แนะนำ)”
Terminal window
opencode agent create

ระบบจะถามทีละขั้น:

  1. บันทึกที่ไหน - global หรือ project-specific
  2. อธิบายว่า agent ทำอะไร - ระบบจะสร้าง description ให้
  3. เลือก permissions - อะไรที่อนุญาต/ไม่อนุญาต
  4. สร้างไฟล์ - สร้างไฟล์ .md ให้อัตโนมัติ

วิธีที่ 2: สร้างไฟล์เอง

Section titled “วิธีที่ 2: สร้างไฟล์เอง”

ตำแหน่งไฟล์:

Global: ~/.config/opencode/agents/review.md
Project: .opencode/agents/review.md

โครงสร้างไฟล์ (frontmatter):

---
description: ตรวจ code หาปัญหาและ bug
mode: subagent
permission:
edit: deny
bash: deny
---
คุณคือ Code Reviewer ที่เน้น:
- ความปลอดภัย
- Performance
- Best practices

วิธีที่ 3: ผ่าน JSON config

Section titled “วิธีที่ 3: ผ่าน JSON config”
{
"agent": {
"reviewer": {
"description": "ตรวจ code สำหรับ best practices",
"mode": "subagent",
"permission": {
"edit": "deny",
"bash": "deny"
}
}
}
}

Optionคำอธิบาย
descriptionบอกว่า agent ทำอะไร (จำเป็น)
modeprimary, subagent, หรือ all
modelเปลี่ยน model เฉพาะ agent นี้
temperature0.0-1.0 (ต่ำ = แม่นยำ, สูง = สร้างสรรค์)
stepsจำกัดจำนวน iterations
hiddenซ่อนจาก @mention autocomplete
colorสีใน UI เช่น #FF5733

ไอเดีย Sub Agent สำหรับ Software Dev

Section titled “ไอเดีย Sub Agent สำหรับ Software Dev”
---
description: ตรวจ code ก่อน merge, หาปัญหาด้านความปลอดภัยและ performance
mode: subagent
permission:
edit: deny
bash: deny
---
คุณคือ Senior Code Reviewer เน้นหา:
- Security vulnerabilities (SQL injection, XSS, ฯลฯ)
- Performance issues (N+1 query, memory leak)
- Code quality และ best practices
- Potential bugs และ edge cases
ให้ feedback ที่ชัดเจนพร้อมตัวอย่างการแก้ไข

ใช้เมื่อ: ก่อน commit หรือ merge PR

@reviewer ตรวจโค้ดนี้หน่อย มี security issue ไหม?

---
description: สร้าง unit test จาก code ที่มี
mode: subagent
---
คุณคือ QA Engineer ที่เชี่ยวชาญด้านการเขียน test
สร้าง test cases ที่:
- ครอบคลุม happy path
- ครอบคลุม edge cases
- ใช้ pattern ที่ดี (AAA - Arrange, Act, Assert)
- มี descriptive naming
รองรับ: Jest, Vitest, React Testing Library

ใช้เมื่อ: อยากได้ test เพิ่ม

@test-generator สร้าง test สำหรับ calculateTotal function

---
description: เขียน documentation จาก code
mode: subagent
permission:
bash: deny
---
คุณคือ Technical Writer
สร้าง documentation ที่:
- JSDoc/TSDoc สำหรับ functions/components
- README.md สำหรับ project
- API docs ที่ชัดเจน
- Usage examples ที่เข้าใจง่าย
ใช้ภาษาที่กระชับแต่ครบถ้วน

ใช้เมื่อ: อยากได้ doc จาก function

@docs-writer สร้าง doc สำหรับ component นี้

---
description: หา outdated dependencies และ vulnerabilities
mode: subagent
---
คุณคือ DevOps Engineer ที่ดูแล dependencies
ทำ:
- ตรวจสอบ package.json/package-lock.json
- หา version ล่าสุดของแต่ละ package
- เช็คว่ามี breaking changes ไหม
- หา known vulnerabilities (CVE)
- แนะนำ update order ที่ปลอดภัย
ใช้ npm outdated, ncu, atau อื่นๆ

ใช้เมื่อ: อยาก update dependencies

@dep-checker มี dependency ไหนต้อง update บ้าง

---
description: ช่วย migrate code จาก version เก่าไปใหม่
mode: subagent
---
คุณคือ Migration Specialist
ช่วย migrate ในสิ่งต่างๆ:
- JavaScript → TypeScript
- React → Next.js / Remix
- Class components → Hooks
- CSS → Tailwind / UnoCSS
- REST → GraphQL
ทำทีละขั้นตอน และอธิบายว่าทำไมต้องเปลี่ยน

ใช้เมื่อ: upgrade tech stack

@migration ช่วย convert จาก JavaScript เป็น TypeScript

---
description: วิเคราะห์ code และหาทาง optimize
mode: subagent
permission:
bash: deny
---
คุณคือ Performance Engineer
หาและแก้ไข:
- Bottlenecks ใน code
- Unnecessary re-renders
- Heavy computations ที่ตั้งได้
- Memory leaks
- Large bundle sizes
ใช้ React Profiler, Lighthouse, Chrome DevTools

ใช้เมื่อ: เว็บช้า

@perf-analyzer วิเคราะห์โค้ดส่วนนี้หน่อย

---
description: ออกแบบ REST/GraphQL API
mode: subagent
---
คุณคือ API Architect
สร้าง:
- REST endpoints ที่ follow best practices
- GraphQL schema และ resolvers
- Request/Response schemas (OpenAPI/Swagger)
- Error handling patterns
ใช้ naming conventions ที่ดี และ versioning

ใช้เมื่อ: อยากได้ API design

@api-designer ออกแบบ API สำหรับ todo list

---
description: ออกแบบ database schema และ queries
mode: subagent
---
คุณคือ Database Architect
สร้าง:
- SQL schemas (PostgreSQL, MySQL, SQLite)
- Prisma/Drizzle models
- MongoDB schemas
- Indexes ที่เหมาะสม
- Migration files
เน้น normalization, performance, และ data integrity

ใช้เมื่อ: ออกแบบ database ใหม่

@db-designer ออกแบบ schema สำหรับระบบ e-commerce

---
description: ช่วยจัดการ git workflow
mode: subagent
permission:
bash:
"git *": "allow"
---
คุณคือ Git Expert
ช่วย:
- เขียน commit messages ที่ดี
- Resolve merge conflicts
- สร้าง PR description
- จัดการ branches
- ทำ rebase/cherry-pick
Follow conventional commits format

ใช้เมื่อ: งงเรื่อง git

@git-helper ช่วย resolve conflict นี้หน่อย

เปลี่ยน Model สำหรับ Sub Agent

Section titled “เปลี่ยน Model สำหรับ Sub Agent”
---
description: ตรวจ code ด้วย model ราคาถูก
mode: subagent
model: anthropic/claude-haiku-4-20250514
temperature: 0.1
---
สำหรับงานที่ไม่ต้องการ model แพง
---
description: Internal helper สำหรับ agent อื่นเรียกใช้
mode: subagent
hidden: true
---
ไม่แสดงใน @mention autocomplete
แต่ Agent อื่นยังเรียกใช้ได้ผ่าน Task tool

จำกัดสิทธิ์ Task (Sub Agent ที่ Primary เรียกได้)

Section titled “จำกัดสิทธิ์ Task (Sub Agent ที่ Primary เรียกได้)”
{
"agent": {
"build": {
"permission": {
"task": {
"*": "deny",
"reviewer": "ask",
"docs-*": "allow"
}
}
}
}
}

หมายความว่า:

  • ห้ามเรียกทุกอย่าง (deny ก่อน)
  • ยกเว้น reviewer ต้องถามก่อน (ask)
  • docs-* อนุญาตหมด (allow)

วิธีตัวอย่าง
@mention@reviewer ตรวจโค้ดนี้
automaticAgent ตัดสินใจเรียกเอง
วิธีคำสั่ง/ตำแหน่ง
CLIopencode agent create
Markdown.opencode/agents/name.md
JSONopencode.json > agent
Agentใช้เมื่อ
@generalงานทั่วไปที่ต้องทำหลายอย่าง
@exploreหาข้อมูลใน codebase
@scoutดู dependency / docs ภายนอก
Avatar whiteCat
ลองสร้าง Sub Agent สักตัวดูสิ! มันจะช่วยให้งานของคุณเร็วขึ้นมาก

ทดสอบความเข้าใจเกี่ยวกับ Sub Agent

ข้อ 1 / 50%

Sub Agent คืออะไร?