iT邦幫忙

2026 iThome 鐵人賽

DAY 13
0
AI Engineering

[ opencode ] 開源 AI coding agent系列 第 13

13-opencode | Custom commands:把常用流程指令化

  • 分享至 

  • xImage
  •  

! 本篇文章將會介紹 custom commands,把你每天重複打的 prompt 變成一個斜線指令 :D

TL;DR: https://dev.benben.me/slides/s/ironman-13-custom-commands

本篇目標

讀完這篇你會學到:

  • .opencode/commands/ 建立 markdown 指令
  • 會用 $ARGUMENTS、shell 輸出注入與 @ 檔案引用
  • 建立自己的指令庫(個人層 + 專案層)

一個 .md 檔 = 一個 / 指令

統計一下你昨天對 AI 打的 prompt,是不是有一大半是重複的?「幫我 review 這個 branch」「依照慣例寫 commit message」「解釋這個函式」……custom commands 就是把這些流程模板化。

存放位置:

  • 專案層:.opencode/commands/
  • 全域層:~/.config/opencode/commands/

檔名即指令名:test.md/test

三分鐘上手:/commit

---
description: 依照慣例生成 commit message
---

查看最近的 staged changes(!`git diff --cached`),
寫出一個符合 conventional commits 格式的 commit message:

- type 從 feat / fix / refactor / docs / chore 挑選
- 標題不超過 50 字元,用英文
- 只輸出 message 本身,不要多餘解釋

存成 .opencode/commands/commit.md,之後只要打 /commit。從此 commit message 品質穩定,不用每次跟 AI 重新解釋什麼是 conventional commits。

模板變數:讓指令帶參數

$ARGUMENTS:整串參數

---
description: 解釋指定檔案或函式
---

解釋 $ARGUMENTS 的用途與運作方式。
先給一段摘要,再逐段說明關鍵邏輯。
最後指出任何可疑之處或潛在 bug。

/explain src/auth/login.ts$ARGUMENTS 就會被換成 src/auth/login.ts

$1$2:位置參數

---
description: 建立檔案
---

建立檔案 $1(放在目錄 $2),內容:$3

/create-file config.json src "{ \"key\": \"value\" }" 一個指令三個參數。

發揮創藝:把 shell 輸出塞進 prompt

上面 /commit 已經用過了。再來一個更兇的——自動 review 最近十個 commit:

---
description: Review 最近的改動
---

最近的 git commits: `git log --oneline -10`

Review 這些改動:找出潛在 bug、不一致的命名、
遺漏的錯誤處理。用清單列出,每項附嚴重度。

/review-commits 指令會在專案根目錄執行,輸出直接成為 prompt 的一部分。

@檔案:引用檔案內容

Review the component in @src/components/Button.tsx.
Check for performance issues.

檔案內容自動展開進 prompt,跟在對話框打 @ 一樣。

front-matter 選項

欄位 用途
description / 時顯示的說明
agent 指定用哪個 agent 執行(例如 plan
model 覆選這次指令用的 model
subtask true 強制以 subagent 執行

subtask: true 這個值得一提:指令會在獨立的 subagent 跑,結果回報但不污染主對話的 context。像「分析覆蓋率報告」這種用完即丟的任務,開 subtask 乾淨多了。

小小測驗:自訂指令可以覆蓋內建指令嗎?——可以!定義一個叫 undo.md 的指令,/undo 就是你的了。不建議沒事這樣玩,但知道會被覆蓋就好。

Command vs Agent vs Rule:三角分工

寫到這裡剛好把三者擺在一起:

觸發方式 適合放什麼 例子
Rule 自動常駐 專案常識 build 指令、命名慣例
Command 你主動打 /xxx 重複流程 /commit/review
Agent Tab 切換或 @ 點名 一個「角色」 reviewer、翻譯員

口訣:常識進 rules、流程進 commands、角色進 agents

常見問題

Q:指令檔可以分享給團隊嗎?
A:可以,這是設計目的之一。.opencode/commands/ commit 進 git,全隊 clone 下來就有同一套指令,AI 工作流直接標準化。

Q:指令跟 skill 有什麼差別?skill 不是也能放流程?
A:command 是「你主動觸發」,一定會執行;skill(Day 15)是「AI 判斷任務相符時自動載入」。確定性 vs 智慧型派遣,各有用武之地。

Q:!shell`` 會有資安風險嗎?
A:指令檔就是程式碼——clone 別人的 repo 前先看看 .opencode/commands/ 裡寫了什麼,跟你看 package.json 的 postinstall 一樣的道理。

小結

  • .opencode/commands/*.md,檔名即指令,front-matter 設定執行方式
  • $ARGUMENTS / $1 帶參數、!`cmd` 注入 shell 輸出、@ 引用檔案
  • subtask: true 讓指令不污染主對話
  • 團隊共享指令庫 = AI 工作流標準化

明日預告

Day 14:MCP servers——把資料庫、瀏覽器、外部文件全都接到 AI 手上。


有任何疑問但沒有 iT 邦幫忙帳號,或是想匿名提問?
歡迎到 https://dev.benben.me/q/P3C5U6 提問或加油打氣,沒意外的話會在完賽之後一起回答 :D


上一篇
12-opencode | Rules 撰寫心法:AGENTS.md 進階配置
下一篇
14-opencode | MCP servers:接上外部工具與資料源
系列文
[ opencode ] 開源 AI coding agent24
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言