ในการพัฒนาระบบซอฟต์แวร์ยุคใหม่ การพึ่งพา AI โมเดลเดียว (Single-Agent) ให้เขียนโค้ด ตรวจ bugs วางโครงสร้าง และทำ Test cases ทั้งหมดพร้อมกัน มักนำไปสู่ปัญหา Context Overload และความผิดพลาดที่สูงขึ้น OpenCode จึงออกแบบสถาปัตยกรรม Multi-Agent Systems ขึ้นมาเพื่อแก้ไขปัญหานี้โดยเฉพาะ
ทำไมต้อง Multi-Agent ใน OpenCode? (WHY)
การกระจายงานให้ AI หลายตัวที่มีหน้าที่เฉพาะทาง มีข้อดีเหนือกว่าการใช้ AI ตัวเดียวดังนี้:
- ลด Context Drift & Token Pollution: เอเจนต์ย่อยรับฟังเฉพาะคำสั่งที่เกี่ยวข้อง ทำให้ Context Window ไม่แออัดจนหลงลืมข้อกำหนดหลัก
- ประหยัดต้นทุน (Cost Efficiency): ให้ Primary Agent ตัวหลักวางแผน แล้วส่งงานย่อยให้โมเดลราคาถูก/ความเร็วสูง ทำแทน
- แยกความรับผิดชอบชัดเจน (Separation of Concerns): เอเจนต์สำหรับเขียนโค้ดจะไม่ยุ่งกับการแก้โครงสร้างไฟล์ ขณะที่เอเจนต์ตรวจทาน (Reviewer) จะได้รับสิทธิ์แบบ Read-only เพื่อความปลอดภัย
การคำนวณ Token และ Context Window ในระบบ Multi-Agent
ข้อดีที่สำคัญมากๆ ของการใช้ Subagent ใน OpenCode คือเรื่อง การคิด Token แยกกันอย่างเป็นอิสระ:
- Context Window อิสระ (Isolated Context): เมื่อ Primary Agent สั่งงาน Subagent (เช่น
@frontend-dev) ระบบจะสร้าง Context Window ใหม่ให้ Subagent ตัวนั้นทันที ทำให้ประวัติการคุยยาวๆ ของ Primary Agent ไม่หลุดไปเปลือง Token ใน Subagent - ส่งเฉพาะ Instruction ที่จำเป็น: Subagent จะรับเพียง System Prompt ของตัวเองบวกกับคำสั่งย่อยที่ได้รับมอบหมายเท่านั้น จึงประมวลผลได้รวดเร็วและใช้ Token น้อยมาก
- นับ Token แยกตามรายโมเดล: หากเลือกใช้โมเดลต่างชนิดกัน (เช่น Primary ใช้โมเดลใหญ่ แต่ Subagent ใช้
nvidia/nemotron-3.5-lightning) การตัดโควตาและค่าใช้จ่ายของ Token จะถูกคำนวณแยกกันตามเรตของแต่ละโมเดลอย่างตรงไปตรงมา
OpenCode ทำงานอย่างไร? (HOW)
OpenCode ใช้หลักการกระจายงานผ่านระบบ Primary Agent และ Subagents โดยเราสามารถกำหนดบทบาทให้ทำงานร่วมกันอย่างมีประสิทธิภาพ:
- @lead (Primary Agent): ตัวหลักที่ถูกตั้งเป็นค่าเริ่มต้น รับโจทย์ใหญ่จากผู้ใช้ วิเคราะห์สเปก วางแผน และสั่งการกระจายงานให้ Subagents ทั้งหมด
- @designer (Subagent): ออกแบบ UI/UX, Component Structure, Color Palette และ Style Guide
- @frontend-dev (Subagent): พัฒนาและแก้ไข โค้ดฝั่ง UI/Frontend (เช่น React, Next.js, Vue)
- @backend-dev (Subagent): พัฒนา API, Database Schema และ Business Logic ฝั่ง Server
- @tester (Subagent): เขียน Unit Test และรันการทดสอบครอบคลุมโค้ดที่เพิ่งเขียน
- @sec-check (Subagent): สแกนหาช่องโหว่ความปลอดภัยแบบ Read-Only ก่อนส่งสรุปผลกลับ
การตัดสินใจของ OpenCode CLI และจำนวน Developer
Primary Agent ทำหน้าที่เป็น Orchestrator อัจฉริยะ ที่ตัดสินใจกระจายงานเองตามความเหมาะสม:
- วิเคราะห์และ Spawn งานคู่ขนาน (Dynamic Spawning): หากมีงานเขียน UI และ API ที่ไม่เกี่ยวข้องกัน Primary Agent จะเปิด Subagent
@frontend-devและ@backend-devขึ้นมารันขนานกัน (Parallel Subagents) เพื่อประมวลผลพร้อมกันทันที - ไม่จำเป็นต้องสร้าง Subagent หลายตัวในคอนฟิก: เราสามารถสร้างนิยาม Subagent ไว้เพียงตัวเดียว แล้วปล่อยให้ระบบเรียกใช้งานพร้อมกันหลาย Instance ได้ เว้นแต่ต้องการแบ่งแยกหน้าที่ สิทธิ์ หรือใช้ Model คนละตัวกัน
ตั้งค่า Multi-Agent ใน OpenCode (opencode.json)
กำหนด "default_agent": "lead" พร้อมตั้งค่า lead เป็น Primary Agent โดยแนะนำให้ เว้นการระบุคีย์ model ใน Primary Agent ไว้ เพื่อหลีกเลี่ยงข้อผิดพลาดหรือ Warning เรื่อง Configuration ในกรณีที่ชื่อโมเดลระบุไม่ตรงกับ Provider ซึ่งการเว้นไว้จะช่วยให้ Primary Agent ดึงโมเดลหลักที่ถูกเลือกไว้บน OpenCode TUI มาใช้งานโดยอัตโนมัติ
หมายเหตุ (Remark): สำหรับ Subagents นั้น ไม่จำเป็นต้องระบุคีย์ model ก็ได้เช่นกัน หากไม่ระบุ ระบบจะใช้โมเดลหลักที่เลือกไว้บน TUI เป็นค่าเริ่มต้นทันที แต่ในกรณีที่ต้องการประหยัดต้นทุนหรือเน้นความเร็วเฉพาะงาน สามารถกำหนดชื่อโมเดลเฉพาะเจาะจง (เช่น nvidia/nemotron-3.5-lightning) ให้กับ Subagent แต่ละตัวได้ตามต้องการ:
{
"$schema": "https://opencode.ai/config.json",
"default_agent": "lead",
"agent": {
"lead": {
"mode": "primary",
"description": "Lead Architect for planning, task delegation, and orchestrating workflow",
"prompt": "คุณคือ Lead Architect หน้าที่ของคุณคือรับโจทย์จากผู้ใช้ วิเคราะห์ความต้องการ วางแผนระบบอย่างเป็นขั้นตอน และสั่งการกระจายงานไปยัง Subagents (@designer, @frontend-dev, @backend-dev, @tester, @sec-check) ตามความเชี่ยวชาญ ห้ามเขียนโค้ดเองโดยตรง ให้ส่งต่อคำสั่งย่อยและรวบรวมผลลัพธ์มาสรุปให้ผู้ใช้เท่านั้น"
},
"designer": {
"mode": "subagent",
"description": "UI/UX Designer for designing webpage layouts and style guidelines",
"model": "nvidia/nemotron-3.5-lightning",
"prompt": "คุณคือ UI/UX Designer หน้าที่คือออกแบบหน้าเว็บ Layout, UI components, Color palette และ Style guide ตามคำสั่งเท่านั้น",
"permission": {
"edit": "allow"
}
},
"frontend-dev": {
"mode": "subagent",
"description": "Frontend Developer for building web interfaces and client-side logic",
"model": "nvidia/nemotron-3.5-lightning",
"prompt": "คุณคือ Frontend Developer หน้าที่คือเขียนและแก้ไขโค้ดฝั่ง UI, Client Components และเชื่อมต่อ API ตามคำสั่งเท่านั้น",
"permission": {
"edit": "allow"
}
},
"backend-dev": {
"mode": "subagent",
"description": "Backend Developer for building APIs, server logic, and database schemas",
"model": "nvidia/nemotron-3.5-lightning",
"prompt": "คุณคือ Backend Developer หน้าที่คือเขียน API, Database Schema, Authentication และ Server Logic ตามคำสั่งเท่านั้น",
"permission": {
"edit": "allow"
}
},
"tester": {
"mode": "subagent",
"description": "QA Tester for unit testing",
"model": "nvidia/nemotron-3.5-lightning",
"prompt": "คุณคือ QA Tester หน้าที่คือเขียน Unit Test และ Integration Test ตามคำสั่งเท่านั้น",
"permission": {
"edit": "allow"
}
},
"sec-check": {
"mode": "subagent",
"description": "Security Auditor for checking code vulnerabilities",
"model": "nvidia/nemotron-3.5-lightning",
"prompt": "คุณคือ Security Auditor หน้าที่คือสแกนหาช่องโหว่ความปลอดภัยและวิเคราะห์ความเสี่ยงแบบ Read-only",
"permission": {
"edit": "deny"
}
}
}
}
การสร้าง Subagent: เลือกใช้แบบไหนดีระหว่าง Markdown และ JSON?
นอกจากการตั้งค่า Subagent ผ่านไฟล์ opencode.json แล้ว คุณยังสามารถสร้าง Subagent แยกเป็นไฟล์เดี่ยวๆ เก็บไว้ในโฟลเดอร์ .opencode/agents/ ได้ทั้งสองรูปแบบ โดยมีตัวอย่างการกำหนดค่าดังนี้:
แบบที่ 1: การสร้าง Subagent ด้วยไฟล์ Markdown (.md)
ใช้ YAML Frontmatter ตั้งค่า Metadata และเขียน System Prompt ไว้ที่เนื้อหาด้านล่าง อ่านง่าย เหมาะกับ Prompt ที่มีความซับซ้อน
ตัวอย่าง .opencode/agents/coder.md:
---
name: coder
description: Expert software developer agent for writing and refactoring code
model: claude-3-5-sonnet
temperature: 0.1
tools:
- read_file
- write_file
- execute_command
---
# Role and Responsibilities
You are a senior software engineer. Your main focus is to write clean, maintainable, and highly efficient code.
## Coding Guidelines
1. Always write self-documenting code with clear variable names.
2. Include unit tests for any new functions created.
แบบที่ 2: การสร้าง Subagent ด้วยไฟล์ JSON (.json)
กำหนดค่าทั้งหมดในรูปแบบ Key-Value มาตรฐาน เหมาะสำหรับ Agent ขนาดเล็ก หรือถูกสร้างโดย Script อัตโนมัติ
ตัวอย่าง .opencode/agents/reviewer.json:
{
"name": "reviewer",
"description": "Code review agent for checking code quality, security, and performance",
"model": "claude-3-5-sonnet",
"temperature": 0.2,
"tools": [
"read_file",
"git_operations"
],
"prompt": "You are a code reviewer. Your goal is to analyze source code for bugs, security vulnerabilities, and adherence to clean code standards. Provide constructive feedback with clear examples."
}
| รูปแบบไฟล์ | จุดเด่น | ข้อจำกัด |
|---|---|---|
| Markdown (.md) | อ่านง่าย จัด Format Prompt ยาวๆ ได้ลื่นไหลด้วย Markdown | ต้องใช้ YAML Frontmatter ที่ส่วนหัวของไฟล์ |
| JSON (.json) | โครงสร้างชัดเจน ง่ายต่อการให้ Program/Script อ่านและสร้างไฟล์ | หากมี System Prompt ยาว ต้อง Escape String (เช่น \n) ทำให้อ่านยากขึ้น |
บทสรุป
สถาปัตยกรรม Multi-Agent ใน OpenCode ไม่ได้เพียงช่วยให้ AI เขียนโค้ดได้เร็วขึ้น แต่เป็นการจำลอง "ทีมวิศวกรซอฟต์แวร์ขนาดเล็ก" เข้ามาไว้ในสภาพแวดล้อมการทำงานของคุณ ช่วยให้การพัฒนาซอฟต์แวร์ในระดับโปรดักชันมีความแม่นยำ ปลอดภัย และควบคุมต้นทุนได้จริง
No comments:
Post a Comment