跳到主要內容
文件
Markdown

指令

建立 .opencode/commands/review.md,將提示詞變成 /review:

.opencode/commands/review.md
Review $ARGUMENTS for bugs and missing tests.

在 TUI 執行指令時,請一併指定目標:

/review src/auth.ts

OpenCode 會將 Review src/auth.ts for bugs and missing tests. 作為使用者提示詞提交。

將全域指令放在 ~/.config/opencode/commands/,專案指令放在 .opencode/commands/。

~/.config/opencode/commands/review.md
.opencode/commands/review.md

只會探索 .md 檔案。巢狀路徑會成為以 / 分隔的指令名稱:

.opencode/commands/team/review.md
Review $ARGUMENTS using the team's checklist.

巢狀指令的執行方式為 /team/review src/auth.ts。舊版的單數目錄 command/ 也會被探索, 但新檔案請使用 commands/。

指令需要中繼資料時,請在文件開頭加入 YAML 前置中繼資料。去除前後空白的 Markdown 本文一律作為提示詞範本。

.opencode/commands/review.md
---
description: Review code for correctness
agent: plan
model: anthropic/claude-sonnet-5-5#high
---

Review $ARGUMENTS. Report bugs first.

在任意 OpenCode JSON 或 JSONC 設定檔的 commands 下定義指令。每個指令都需要 template。

opencode.jsonc
{
  "$schema": "https://opencode.ai/config.json",
  "commands": {
    "review": {
      "description": "Review code for correctness",
      "template": "Review $ARGUMENTS. Report bugs first.",
    },
  },
}

Markdown 的前置中繼資料與 JSON 項目接受相同的欄位,差別在於 Markdown 會從檔案本文取得 template。

欄位必填行為
template僅 JSON提示詞範本。
description否顯示在指令清單與探索中的文字。
agent否執行指令時選取的 agent。
model否以 provider/model 或 provider/model#variant 格式覆寫模型。
subagent否true 會在背景子工作階段中執行;false 會強制使用目前的工作階段。
subtask否subagent 的已棄用別名。

例如,下列 JSON 指令會設定目前所有選用欄位:

opencode.jsonc
{
  "commands": {
    "audit": {
      "template": "Audit $ARGUMENTS.",
      "description": "Audit a package",
      "agent": "general",
      "model": "anthropic/claude-sonnet-5-5#high",
      "subagent": true,
    },
  },
}

請勿將 template 寫在 Markdown 的前置中繼資料中;範本取自文件本文。

使用 $ARGUMENTS 可原樣帶入完整的引數字串。

.opencode/commands/component.md
Create a typed React component named $ARGUMENTS.

執行 /component Account Settings 時,預留位置會替換成 Account Settings。

使用 $1、$2 與更大的數字表示解析後的位置引數。單引號或雙引號會將詞組分組,並在解析後移除。

.opencode/commands/check.md
Check $1. Focus on $2.

對於 /check src/auth.ts "error handling",$1 會變成 src/auth.ts,$2 會變成 error handling。

編號最大的預留位置會帶入該引數以及後續所有內容。未提供的引數會替換成空字串。

.opencode/commands/compare.md
Compare $1 with $2.

對於 /compare api stable branch,OpenCode 會提交 Compare api with stable branch.。

若範本沒有 $ARGUMENTS,也沒有位置引數的預留位置,OpenCode 會空一行再附上非空引數。

.opencode/commands/explain.md
Explain this code clearly.

對於 /explain src/cache.ts,提示詞會變成:

Explain this code clearly.

src/cache.ts

將 shell 指令包在 ! 後接反引號中,即可在提交提示詞前插入其輸出。

.opencode/commands/review-diff.md
Review this diff:

!`git diff --stat && git diff`

OpenCode 會以設定的 shell 在作用中的專案位置執行每個區塊,並插入其合併輸出。 OpenCode 會先展開引數的預留位置:

.opencode/commands/history.md
Summarize these commits:

!`git log --oneline -$1`

對於 /history 5,shell 會收到 git log --oneline -5。請勿將不受信任的引數放入 shell 區塊。

已儲存的範本不會展開 @path;它仍是一般提示詞文字。

.opencode/commands/readme.md
Review @README.md.

若要附加檔案,請在叫用指令時透過輸入框加入。OpenCode 會在提交展開後的提示詞時保留這些 輸入框附件。

指令會在提交持久的使用者提示詞之前展開引數與 shell 區塊。它們預設會在目前的工作階段中 執行。

.opencode/commands/plan.md
---
agent: plan
---

Plan $ARGUMENTS.

執行 /plan migration 會在提交提示詞前將目前的工作階段切換為 plan。

模型選擇遵循下列規則:

  1. 指令的 model 會覆寫其他所有模型。
  2. 若指令未指定模型,就使用該指令所選 agent 設定的模型,而非執行時正在使用的模型。
  3. 若 agent 也未設定模型,就繼續使用目前工作階段的模型。

例如,下列指令會同時選取其 agent 與明確的模型:

.opencode/commands/design.md
---
agent: plan
model: openai/gpt-6.1-sol#high
---

Design $ARGUMENTS.

設定 subagent: true,即可在背景子工作階段執行指令。

.opencode/commands/audit.md
---
description: Audit changes
agent: general
subagent: true
---

Audit $ARGUMENTS for bugs and missing tests.

父工作階段會保持可用,並保留其 agent 與模型。OpenCode 會在子工作階段完成時,將其結果或失敗 傳回父工作階段。

值行為
true一律使用子工作階段,即使所選 agent 具有 mode: primary。
false一律使用目前的工作階段,即使所選 agent 具有 mode: subagent。
省略只有在所選 agent 具有 mode: subagent 時才使用子工作階段。

子工作階段優先使用指令指定的模型,其次是所選 agent 的模型,最後才是父工作階段的模型。仍支援舊版 subtask;若兩個欄位都存在,以 subagent 為準。

YAML
subagent: false
subtask: true

此範例會在目前的工作階段中執行,因為 subagent 優先。

Markdown 與 JSON 指令共用同一個登錄。後面的來源會取代同名的先前指令。

~/.config/opencode/commands/review.md   # Lower priority
.opencode/commands/review.md           # Replaces the global command

專案來源優先於全域來源,較近的專案來源優先於上層來源。 自訂定義也可以取代先前的內建指令。OpenCode 會自動重新載入指令檔案與設定 變更。

.opencode/commands/review.md
Review only the staged changes: !`git diff --cached`

儲存此檔案會更新 /review,不需要重新啟動 OpenCode。