Sub Agent
Sub Agent คืออะไร
Section titled “Sub Agent คืออะไร”Sub Agent คือ AI Agent เฉพาะทางที่ถูกสร้างขึ้นเพื่อทำงานเฉพาะอย่าง แทนที่จะใช้ Agent หลักทำทุกอย่าง เราสามารถแบ่งงานให้ Agent ที่เชี่ยวชาญแต่ละด้านทำ
ประเภทของ Agent ใน OpenCode
Section titled “ประเภทของ Agent ใน OpenCode”Primary Agents (ตัวหลัก)
Section titled “Primary Agents (ตัวหลัก)”- Build — ทำงานได้ทุกอย่าง (default)
- Plan — อ่านอย่างเดียว เหมาะวางแผน
Sub Agents (ตัวช่วย)
Section titled “Sub Agents (ตัวช่วย)”- @general — ทำงานหลายอย่าง มีเครื่องมือครบ
- @explore — หาข้อมูลใน codebase (read-only)
- @scout — หาข้อมูลจากข้างนอก/dependency
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: ใช้คำสั่ง (แนะนำ)”opencode agent createระบบจะถามทีละขั้น:
- บันทึกที่ไหน - global หรือ project-specific
- อธิบายว่า agent ทำอะไร - ระบบจะสร้าง description ให้
- เลือก permissions - อะไรที่อนุญาต/ไม่อนุญาต
- สร้างไฟล์ - สร้างไฟล์ .md ให้อัตโนมัติ
วิธีที่ 2: สร้างไฟล์เอง
Section titled “วิธีที่ 2: สร้างไฟล์เอง”ตำแหน่งไฟล์:
Global: ~/.config/opencode/agents/review.mdProject: .opencode/agents/review.mdโครงสร้างไฟล์ (frontmatter):
---description: ตรวจ code หาปัญหาและ bugmode: subagentpermission: 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" } } }}Options สำหรับ Agent
Section titled “Options สำหรับ Agent”| Option | คำอธิบาย |
|---|---|
description | บอกว่า agent ทำอะไร (จำเป็น) |
mode | primary, subagent, หรือ all |
model | เปลี่ยน model เฉพาะ agent นี้ |
temperature | 0.0-1.0 (ต่ำ = แม่นยำ, สูง = สร้างสรรค์) |
steps | จำกัดจำนวน iterations |
hidden | ซ่อนจาก @mention autocomplete |
color | สีใน UI เช่น #FF5733 |
ไอเดีย Sub Agent สำหรับ Software Dev
Section titled “ไอเดีย Sub Agent สำหรับ Software Dev”1. Code Reviewer
Section titled “1. Code Reviewer”---description: ตรวจ code ก่อน merge, หาปัญหาด้านความปลอดภัยและ performancemode: subagentpermission: 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 ไหม?2. Test Generator
Section titled “2. Test Generator”---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 function3. Docs Writer
Section titled “3. Docs Writer”---description: เขียน documentation จาก codemode: subagentpermission: bash: deny---
คุณคือ Technical Writer
สร้าง documentation ที่:- JSDoc/TSDoc สำหรับ functions/components- README.md สำหรับ project- API docs ที่ชัดเจน- Usage examples ที่เข้าใจง่าย
ใช้ภาษาที่กระชับแต่ครบถ้วนใช้เมื่อ: อยากได้ doc จาก function
@docs-writer สร้าง doc สำหรับ component นี้4. Dependency Checker
Section titled “4. Dependency Checker”---description: หา outdated dependencies และ vulnerabilitiesmode: 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 บ้าง5. Migration Helper
Section titled “5. Migration Helper”---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 เป็น TypeScript6. Performance Analyzer
Section titled “6. Performance Analyzer”---description: วิเคราะห์ code และหาทาง optimizemode: subagentpermission: bash: deny---
คุณคือ Performance Engineer
หาและแก้ไข:- Bottlenecks ใน code- Unnecessary re-renders- Heavy computations ที่ตั้งได้- Memory leaks- Large bundle sizes
ใช้ React Profiler, Lighthouse, Chrome DevToolsใช้เมื่อ: เว็บช้า
@perf-analyzer วิเคราะห์โค้ดส่วนนี้หน่อย7. API Designer
Section titled “7. API Designer”---description: ออกแบบ REST/GraphQL APImode: 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 list8. Database Schema Designer
Section titled “8. Database Schema Designer”---description: ออกแบบ database schema และ queriesmode: 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-commerce9. Git Helper
Section titled “9. Git Helper”---description: ช่วยจัดการ git workflowmode: subagentpermission: 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 นี้หน่อยการตั้งค่า Advanced
Section titled “การตั้งค่า Advanced”เปลี่ยน Model สำหรับ Sub Agent
Section titled “เปลี่ยน Model สำหรับ Sub Agent”---description: ตรวจ code ด้วย model ราคาถูกmode: subagentmodel: anthropic/claude-haiku-4-20250514temperature: 0.1---
สำหรับงานที่ไม่ต้องการ model แพงซ่อน Sub Agent
Section titled “ซ่อน Sub Agent”---description: Internal helper สำหรับ agent อื่นเรียกใช้mode: subagenthidden: 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)
วิธีเรียกใช้
Section titled “วิธีเรียกใช้”| วิธี | ตัวอย่าง |
|---|---|
| @mention | @reviewer ตรวจโค้ดนี้ |
| automatic | Agent ตัดสินใจเรียกเอง |
วิธีสร้าง
Section titled “วิธีสร้าง”| วิธี | คำสั่ง/ตำแหน่ง |
|---|---|
| CLI | opencode agent create |
| Markdown | .opencode/agents/name.md |
| JSON | opencode.json > agent |
Built-in Sub Agents
Section titled “Built-in Sub Agents”| Agent | ใช้เมื่อ |
|---|---|
@general | งานทั่วไปที่ต้องทำหลายอย่าง |
@explore | หาข้อมูลใน codebase |
@scout | ดู dependency / docs ภายนอก |
ลองสร้าง Sub Agent สักตัวดูสิ! มันจะช่วยให้งานของคุณเร็วขึ้นมาก
ทดสอบความเข้าใจเกี่ยวกับ Sub Agent
ข้อ 1 / 50%