กลับไปหน้าสูตร
#opencode#cli#cheatsheet#ai#terminal#agents#subagent#custom-command#custom-agent
OpenCode Cheatsheet
คู่มือ OpenCode แบบรวมทุกอย่างในหน้าเดียว: CLI, agents, subagents, custom commands, custom agents, MCP และ workflow สำหรับใช้กับ repo จริง
7 เมษายน 2569อ่านประมาณ 5 นาที
สารบัญสูตร
🚀 Core CLI Commands🧭 Session & Navigation Flags🛠 Interactive Commands ที่ใช้บ่อย🧩 Common Subcommands⌨️ TUI Workflow ที่ควรรู้🧪 Automation Recipes📁 Files & Config ที่ควรรู้🌟 จุดเด่นเฉพาะตัวของ OpenCode✅ Daily OpenCode Workflow🧠 Agent Team Mental Model🔍 Subagents ที่ควรรู้🧩 Custom Commands🛠 Custom Agents📌 OpenCode Rule of Thumb
🚀 Core CLI Commands
| Command | การใช้งาน |
|---|---|
opencode | เปิด TUI ของ OpenCode ในโปรเจกต์ปัจจุบัน |
opencode [project] | เปิด OpenCode พร้อมระบุ path ของโปรเจกต์ |
opencode -c | กลับไปคุยต่อในเซสชันล่าสุด |
opencode -s <session-id> | โหลดเซสชันตาม ID ที่ต้องการ |
opencode --prompt "..." | เปิด TUI พร้อม prompt ตั้งต้น |
opencode -m provider/model | เลือกโมเดลตั้งแต่ตอนเริ่มเซสชัน |
opencode run "อธิบายโค้ดส่วนนี้" | รันแบบ non-interactive เหมาะกับงานสั้นหรือ automation |
opencode run -c "ต่อจากงานล่าสุดแล้วสรุป diff" | ต่อเซสชันล่าสุดในโหมด run |
opencode run -f ./mock.png "สร้าง UI ตามภาพนี้" | แนบไฟล์เข้า prompt |
opencode upgrade | อัปเดต OpenCode เป็นเวอร์ชันล่าสุด |
🛠 Interactive Commands ที่ใช้บ่อย
| Command | การใช้งาน |
|---|---|
/init | ให้ OpenCode สแกนโปรเจกต์และสร้าง AGENTS.md |
/connect | เชื่อม provider หรือ account สำหรับเริ่มใช้งานโมเดล |
/undo | ย้อนการเปลี่ยนแปลงล่าสุดที่ OpenCode ทำ |
/redo | ทำการเปลี่ยนแปลงที่เพิ่ง undo กลับมาอีกครั้ง |
/share | แชร์ conversation ปัจจุบันเป็นลิงก์ |
🧩 Common Subcommands
Session & Stats
| Command | การใช้งาน |
|---|---|
opencode session list | ดูรายการ session ล่าสุด |
opencode export <session-id> | export session ออกมาเป็น JSON |
opencode import <file-or-url> | import session จากไฟล์หรือ share URL |
opencode stats | ดูสถิติ token และค่าใช้จ่าย |
Auth / Models / MCP
| Command | การใช้งาน |
|---|---|
opencode auth login | login provider และเก็บ credential |
opencode auth list | ดู provider ที่ login อยู่ |
opencode auth logout | logout provider |
opencode models | ดูโมเดลที่ใช้ได้ทั้งหมด |
opencode models anthropic | กรองดูเฉพาะโมเดลของ provider เดียว |
opencode mcp add | เพิ่ม MCP server |
opencode mcp list | ดู MCP servers ที่ตั้งค่าไว้ |
opencode mcp auth <name> | login MCP server ที่รองรับ OAuth |
opencode mcp debug <name> | debug ปัญหาการเชื่อม MCP |
Server / Web / Attach
| Command | การใช้งาน |
|---|---|
opencode serve | เปิด headless server สำหรับ API access |
opencode web | เปิด server พร้อม web interface |
opencode attach http://localhost:4096 | ใช้ TUI ไปเกาะ backend ที่รันอยู่ |
GitHub Agent
| Command | การใช้งาน |
|---|---|
opencode github install | ติดตั้ง GitHub agent ใน repo |
opencode github run | รัน GitHub agent ใน workflow หรือ CI |
⌨️ TUI Workflow ที่ควรรู้
- เปิด
opencodeใน root ของ repo ที่ต้องการทำงาน - รัน
/initครั้งแรกเพื่อสร้างAGENTS.md - ใช้
@เพื่อแนบไฟล์หรือ fuzzy search path ที่เกี่ยวข้อง - ใช้
Tabเพื่อสลับเข้า Plan mode เวลางานยังไม่ชัด - เมื่อแผนโอเค ค่อยสลับกลับมาที่ Build mode แล้วสั่งให้ลงมือทำ
- ถ้าผลลัพธ์ไม่ตรงใจ ใช้
/undoแล้วยิง prompt ใหม่ให้แคบลง
🧪 Automation Recipes
1) ถามสั้นแบบ non-interactive
opencode run "สรุป architecture ของโปรเจกต์นี้ให้หน่อย"
2) แนบไฟล์เข้า prompt
opencode run -f ./design/mockup.png "ช่วยสร้างหน้า dashboard ตามภาพนี้"
3) ต่อ session เดิมแล้วสรุปสถานะ
opencode run -c "สรุปงานที่ค้างอยู่และเสนอ next steps"
4) ใช้ server เพื่อลด cold start
# terminal แรก
opencode serve
# terminal ที่สอง
opencode run --attach http://localhost:4096 "อธิบาย flow ของ auth ใน repo นี้"
5) ดูโมเดลที่มีให้เลือก
opencode models --refresh
📁 Files & Config ที่ควรรู้
| File / Variable | บทบาท |
|---|---|
AGENTS.md | บอกกติกา โปรเจกต์สไตล์ และ workflow ให้ OpenCode เข้าใจทีมมากขึ้น |
~/.local/share/opencode/auth.json | เก็บ credential ของ provider ที่ login แล้ว |
OPENCODE_CONFIG | ระบุ path config file เอง |
OPENCODE_SERVER_PASSWORD | เปิด basic auth ให้ serve หรือ web |
OPENCODE_DISABLE_AUTOCOMPACT | ปิด auto compact context |
OPENCODE_ENABLE_EXA | เปิดใช้ Exa search tools |
🌟 จุดเด่นเฉพาะตัวของ OpenCode
- เด่นเรื่อง
AGENTS.mdและ workflow แบบ agent-first ตั้งแต่เริ่มโปรเจกต์ผ่าน/init - มี
opencode serve,opencode web, และopencode attachสำหรับแยก backend/session แล้วให้ client หลายแบบมาเกาะใช้งานได้ ซึ่งต่างจาก CLI coding agent ทั่วไปที่เน้น local session เป็นหลัก - มี
opencode github installและopencode github runสำหรับ GitHub agent workflow ตรงตัว - โครง
primary agent + subagent + command + permission.taskชัดมาก เหมาะกับคนที่อยากออกแบบ agent orchestration ใน repo เอง - ใช้
provider/modelได้ตรงๆ ใน CLI ทำให้สลับข้าม provider ได้ชัดเจน
✅ Daily OpenCode Workflow
- เข้า repo แล้วรัน
opencode -cถ้ามีงานค้างจาก session เดิม - ถ้าเป็น repo ใหม่ ให้เริ่มด้วย
/init - อธิบายโจทย์ให้ชัด พร้อมอ้างไฟล์สำคัญด้วย
@ - ใช้ Plan mode กับงานที่แตะหลายไฟล์หรือมี risk สูง
- ตรวจ diff และผลลัพธ์ก่อนค่อยส่งต่อ reviewer หรือ commit
🧠 Agent Team Mental Model
| บทบาท | ใช้ทำอะไร |
|---|---|
| Primary agent | ตัวหลักที่คุยกับเราโดยตรง เช่น build, plan |
| Subagent | ตัวย่อยเฉพาะทางที่ถูกเรียกมาช่วยงาน เช่น general, explore |
| Command | shortcut สำหรับเรียก workflow ซ้ำๆ เช่น /review-security |
| Task permission | กำหนดว่า agent ไหนเรียก subagent ไหนได้บ้าง |
| Hidden subagent | ตัวช่วยภายในที่ไม่อยากให้โผล่ใน autocomplete แต่ยังให้ระบบเรียกได้ |
Team Layout ที่ใช้บ่อย
| Agent | Mode | หน้าที่ |
|---|---|---|
plan | primary | แตกงาน วิเคราะห์ risk และออกแบบลำดับงาน |
build | primary | ลงมือแก้ไฟล์ รันคำสั่ง และสรุปผล |
explore | subagent | ค้นไฟล์ หา keyword และทำ research แบบ read-only |
general | subagent | งานหลายขั้นตอนหรืองานที่ต้องแตกหลายส่วนพร้อมกัน |
Task Permission Pattern
{
"agent": {
"orchestrator": {
"permission": {
"task": {
"*": "deny",
"explore": "allow",
"general": "allow",
"review-*": "allow"
}
}
}
}
}
🔍 Subagents ที่ควรรู้
| Subagent | ใช้ทำอะไร |
|---|---|
general | งานวิจัยหลายขั้นตอน หรืองานที่ต้อง execute หลาย unit พร้อมกัน |
explore | ค้น repo แบบ read-only หาไฟล์ หา symbol หา keyword |
วิธีเรียก Subagent
@explore ช่วยหาไฟล์ที่เกี่ยวกับ auth flow ใน repo นี้
@general ช่วยแตกงานนี้เป็น 3 ส่วนแล้วสรุปผลกลับมา
Navigation ระหว่าง Parent / Child Sessions
| แนวคิด | ใช้ทำอะไร |
|---|---|
session_child_first | เข้า child session ตัวแรกจาก parent |
session_child_cycle | วนดู child session ถัดไป |
session_child_cycle_reverse | วนย้อน child session ก่อนหน้า |
session_parent | กลับขึ้น parent session |
🧩 Custom Commands
เก็บไฟล์ไว้ที่ไหน
| Scope | Path |
|---|---|
| Global | ~/.config/opencode/commands/ |
| Per-project | .opencode/commands/ |
Frontmatter Fields ที่ใช้บ่อย
| Field | ใช้ทำอะไร |
|---|---|
description | คำอธิบายสั้นๆ ที่โชว์ใน UI |
agent | ระบุ agent ที่จะให้ command นี้ใช้ |
model | override โมเดลเฉพาะ command |
subtask | บังคับให้รัน command นี้เป็น subtask |
Placeholder สำคัญ
| Placeholder | ความหมาย |
|---|---|
$ARGUMENTS | รับ argument ทั้งก้อน |
$1, $2, $3 | รับ argument แบบตำแหน่ง |
!\command`` | inject shell output เข้า prompt |
@path/to/file | แนบไฟล์เข้า prompt |
ตัวอย่าง Custom Command
ไฟล์: .opencode/commands/review-changes.md
---
description: Review current git changes
agent: plan
subtask: true
---
Current git diff:
!`git diff`
Review these changes for regressions, risky assumptions, and missing tests.
🛠 Custom Agents
เก็บไฟล์ไว้ที่ไหน
| Scope | Path |
|---|---|
| Global | ~/.config/opencode/agents/ |
| Per-project | .opencode/agents/ |
Modes
| Mode | ใช้เมื่อไร |
|---|---|
primary | ใช้เป็น agent หลักใน session และสลับด้วย Tab ได้ |
subagent | ใช้เป็นตัวช่วยเฉพาะทาง เรียกผ่าน @ หรือ task |
all | ใช้ได้ทั้งสองแบบ |
Fields สำคัญ
| Field | ความหมาย |
|---|---|
description | อธิบายว่า agent นี้ใช้ทำอะไร |
mode | primary / subagent / all |
model | โมเดลเฉพาะของ agent |
prompt | system prompt เฉพาะทาง |
temperature | คุมความ deterministic |
steps | จำกัดจำนวนรอบการทำงาน |
hidden | ซ่อนจาก @ autocomplete |
permission.edit | คุมสิทธิ์เขียนไฟล์ |
permission.bash | คุมสิทธิ์รันคำสั่ง |
permission.task | คุมสิทธิ์เรียก subagent อื่น |
ตัวอย่าง Markdown Agent
ไฟล์: .opencode/agents/review.md
---
description: Reviews code for bugs and maintainability
mode: subagent
permission:
edit: deny
bash:
"*": deny
"git diff*": allow
"git log*": allow
---
Focus on:
- behavioural regressions
- missing edge cases
- maintainability issues
- risky refactors
Hidden Agent Example
{
"agent": {
"internal-risk-checker": {
"description": "Checks risks after edits",
"mode": "subagent",
"hidden": true
}
}
}
📌 OpenCode Rule of Thumb
- งานค้นหา ใช้
explore - งานกระจายหลายส่วน ใช้
general - งานเฉพาะทางซ้ำๆ สร้าง subagent เอง
- ถ้าไม่อยากให้ context หลักรก ใช้ command ที่
subtask: true - Agent = บุคลิก + สิทธิ์ + หน้าที่
- Command = ปุ่มลัดสำหรับ workflow