日本語
ドキュメント
コマンド

Commands

繰り返し作業のためのカスタムコマンドを作成します。

カスタムコマンドを使うと、そのコマンドが TUI で実行されたときに実行したいプロンプトを指定できます。

/my-command

カスタムコマンドは、/init/undo/redo/share/help のような組み込みコマンドに加えて使えます。詳しくはこちら


Create command files

commands/ ディレクトリに markdown ファイルを作成して、カスタムコマンドを定義します。

.opencode/commands/test.md を作成します:

.opencode/commands/test.md
---
description: Run tests with coverage
agent: build
model: anthropic/claude-3-5-sonnet-20241022
---
 
Run the full test suite with coverage report and show any failures.
Focus on the failing tests and suggest fixes.

frontmatter はコマンドのプロパティを定義します。本文がテンプレートになります。

/ に続けてコマンド名を入力して、コマンドを使用します。

"/test"

Configure

OpenCode 設定を通じて、または commands/ ディレクトリに markdown ファイルを作成して、カスタムコマンドを追加できます。


JSON

OpenCode の設定command オプションを使用します:

opencode.jsonc
{
  "$schema": "https://opencode.ai/config.json",
  "command": {
    // This becomes the name of the command
    "test": {
      // This is the prompt that will be sent to the LLM
      "template": "Run the full test suite with coverage report and show any failures.\nFocus on the failing tests and suggest fixes.",
      // This is shown as the description in the TUI
      "description": "Run tests with coverage",
      "agent": "build",
      "model": "anthropic/claude-3-5-sonnet-20241022"
    }
  }
}

これで、このコマンドを TUI で実行できます:

/test

Markdown

markdown ファイルを使ってコマンドを定義することもできます。次の場所に配置します:

  • グローバル: ~/.config/opencode/commands/
  • プロジェクトごと: .opencode/commands/
~/.config/opencode/commands/test.md
---
description: Run tests with coverage
agent: build
model: anthropic/claude-3-5-sonnet-20241022
---
 
Run the full test suite with coverage report and show any failures.
Focus on the failing tests and suggest fixes.

markdown ファイル名がコマンド名になります。例えば、test.md を使うと次を実行できます:

/test

Prompt config

カスタムコマンドのプロンプトは、いくつかの特別なプレースホルダーと構文をサポートしています。


Arguments

$ARGUMENTS プレースホルダーを使って、コマンドに引数を渡します。

.opencode/commands/component.md
---
description: Create a new component
---
 
Create a new React component named $ARGUMENTS with TypeScript support.
Include proper typing and basic structure.

引数を付けてコマンドを実行します:

/component Button

すると、$ARGUMENTSButton に置き換えられます。

位置パラメータを使って、個々の引数にアクセスすることもできます:

  • $1 - 第 1 引数
  • $2 - 第 2 引数
  • $3 - 第 3 引数
  • 以下同様...

例えば:

.opencode/commands/create-file.md
---
description: Create a new file with content
---
 
Create a file named $1 in the directory $2
with the following content: $3

コマンドを実行します:

/create-file config.json src "{ \"key\": \"value\" }"

これは次のように置き換えられます:

  • $1config.json
  • $2src
  • $3{ "key": "value" }

Shell output

!command を使って、bash コマンドの出力をプロンプトに挿入します。

例えば、テストカバレッジを分析するカスタムコマンドを作成するには:

.opencode/commands/analyze-coverage.md
---
description: Analyze test coverage
---
 
Here are the current test results:
!`npm test`
 
Based on these results, suggest improvements to increase coverage.

または、最近の変更をレビューするには:

.opencode/commands/review-changes.md
---
description: Review recent changes
---
 
Recent git commits:
!`git log --oneline -10`
 
Review these changes and suggest any improvements.

コマンドはプロジェクトのルートディレクトリで実行され、その出力がプロンプトの一部になります。


File references

@ に続けてファイル名を指定して、コマンドにファイルをインクルードします。

.opencode/commands/review-component.md
---
description: Review component
---
 
Review the component in @src/components/Button.tsx.
Check for performance issues and suggest improvements.

ファイルの内容は自動的にプロンプトに含まれます。


Options

設定オプションを詳しく見てみましょう。


Template

template オプションは、コマンドが実行されたときに LLM に送信されるプロンプトを定義します。

opencode.json
{
  "command": {
    "test": {
      "template": "Run the full test suite with coverage report and show any failures.\nFocus on the failing tests and suggest fixes."
    }
  }
}

これは必須の設定オプションです。


Description

description オプションを使って、コマンドが何をするかの簡単な説明を提供します。

opencode.json
{
  "command": {
    "test": {
      "description": "Run tests with coverage"
    }
  }
}

これは、コマンドを入力したときに TUI に説明として表示されます。


Agent

agent 設定を使って、このコマンドを実行すべきエージェントを任意で指定します。 これがサブエージェントの場合、コマンドはデフォルトでサブエージェントの呼び出しをトリガーします。 この動作を無効にするには、subtaskfalse に設定します。

opencode.json
{
  "command": {
    "review": {
      "agent": "plan"
    }
  }
}

これは任意の設定オプションです。指定しない場合、現在のエージェントがデフォルトになります。


Subtask

subtask ブール値を使って、コマンドがサブエージェントの呼び出しをトリガーするよう強制します。 これは、コマンドがプライマリコンテキストを汚染しないようにしたい場合に便利で、エージェント設定で modeprimary に設定されていても、エージェントをサブエージェントとして動作させるよう強制します。

opencode.json
{
  "command": {
    "analyze": {
      "subtask": true
    }
  }
}

これは任意の設定オプションです。


Model

model 設定を使って、このコマンドのデフォルトモデルをオーバーライドします。

opencode.json
{
  "command": {
    "analyze": {
      "model": "anthropic/claude-3-5-sonnet-20241022"
    }
  }
}

これは任意の設定オプションです。


Built-in

opencode には、/init/undo/redo/share/help のような組み込みコマンドがいくつか含まれています。詳しくはこちら

注記: カスタムコマンドは組み込みコマンドをオーバーライドできます。

同じ名前のカスタムコマンドを定義すると、組み込みコマンドをオーバーライドします。