กลับไปหน้าสูตร
#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

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 เป็นเวอร์ชันล่าสุด

🧭 Session & Navigation Flags

Flagความหมาย
--continue, -cคุยต่อจากเซสชันล่าสุด
--session, -sระบุ session ID ที่ต้องการต่อ
--forkแตก session ใหม่จาก session เดิมแทนการทับต่อ
--agentเลือก agent ที่ต้องการใช้
--model, -mระบุโมเดลในรูปแบบ provider/model
--file, -fแนบไฟล์เข้า prompt ใน opencode run
--format jsonให้ผลลัพธ์จาก opencode run ออกเป็น JSON
--attach <url>ให้ opencode run ไปเกาะกับ server ที่เปิดไว้แล้ว

🛠 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 loginlogin provider และเก็บ credential
opencode auth listดู provider ที่ login อยู่
opencode auth logoutlogout 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 ที่ควรรู้

  1. เปิด opencode ใน root ของ repo ที่ต้องการทำงาน
  2. รัน /init ครั้งแรกเพื่อสร้าง AGENTS.md
  3. ใช้ @ เพื่อแนบไฟล์หรือ fuzzy search path ที่เกี่ยวข้อง
  4. ใช้ Tab เพื่อสลับเข้า Plan mode เวลางานยังไม่ชัด
  5. เมื่อแผนโอเค ค่อยสลับกลับมาที่ Build mode แล้วสั่งให้ลงมือทำ
  6. ถ้าผลลัพธ์ไม่ตรงใจ ใช้ /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

  1. เข้า repo แล้วรัน opencode -c ถ้ามีงานค้างจาก session เดิม
  2. ถ้าเป็น repo ใหม่ ให้เริ่มด้วย /init
  3. อธิบายโจทย์ให้ชัด พร้อมอ้างไฟล์สำคัญด้วย @
  4. ใช้ Plan mode กับงานที่แตะหลายไฟล์หรือมี risk สูง
  5. ตรวจ diff และผลลัพธ์ก่อนค่อยส่งต่อ reviewer หรือ commit

🧠 Agent Team Mental Model

บทบาทใช้ทำอะไร
Primary agentตัวหลักที่คุยกับเราโดยตรง เช่น build, plan
Subagentตัวย่อยเฉพาะทางที่ถูกเรียกมาช่วยงาน เช่น general, explore
Commandshortcut สำหรับเรียก workflow ซ้ำๆ เช่น /review-security
Task permissionกำหนดว่า agent ไหนเรียก subagent ไหนได้บ้าง
Hidden subagentตัวช่วยภายในที่ไม่อยากให้โผล่ใน autocomplete แต่ยังให้ระบบเรียกได้

Team Layout ที่ใช้บ่อย

AgentModeหน้าที่
planprimaryแตกงาน วิเคราะห์ risk และออกแบบลำดับงาน
buildprimaryลงมือแก้ไฟล์ รันคำสั่ง และสรุปผล
exploresubagentค้นไฟล์ หา keyword และทำ research แบบ read-only
generalsubagentงานหลายขั้นตอนหรืองานที่ต้องแตกหลายส่วนพร้อมกัน

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

เก็บไฟล์ไว้ที่ไหน

ScopePath
Global~/.config/opencode/commands/
Per-project.opencode/commands/

Frontmatter Fields ที่ใช้บ่อย

Fieldใช้ทำอะไร
descriptionคำอธิบายสั้นๆ ที่โชว์ใน UI
agentระบุ agent ที่จะให้ command นี้ใช้
modeloverride โมเดลเฉพาะ 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

เก็บไฟล์ไว้ที่ไหน

ScopePath
Global~/.config/opencode/agents/
Per-project.opencode/agents/

Modes

Modeใช้เมื่อไร
primaryใช้เป็น agent หลักใน session และสลับด้วย Tab ได้
subagentใช้เป็นตัวช่วยเฉพาะทาง เรียกผ่าน @ หรือ task
allใช้ได้ทั้งสองแบบ

Fields สำคัญ

Fieldความหมาย
descriptionอธิบายว่า agent นี้ใช้ทำอะไร
modeprimary / subagent / all
modelโมเดลเฉพาะของ agent
promptsystem 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

  1. งานค้นหา ใช้ explore
  2. งานกระจายหลายส่วน ใช้ general
  3. งานเฉพาะทางซ้ำๆ สร้าง subagent เอง
  4. ถ้าไม่อยากให้ context หลักรก ใช้ command ที่ subtask: true
  5. Agent = บุคลิก + สิทธิ์ + หน้าที่
  6. Command = ปุ่มลัดสำหรับ workflow

สูตรอื่นๆ

สูตรก่อนหน้า

RG Command Cheatsheet

ไม่มีสูตรถัดไป