Commands
繰り返し作業のためのカスタムコマンドを作成します。
カスタムコマンドを使うと、そのコマンドが TUI で実行されたときに実行したいプロンプトを指定できます。
/my-commandカスタムコマンドは、/init、/undo、/redo、/share、/help のような組み込みコマンドに加えて使えます。詳しくはこちら。
Create command files
commands/ ディレクトリに markdown ファイルを作成して、カスタムコマンドを定義します。
.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 オプションを使用します:
{
"$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 で実行できます:
/testMarkdown
markdown ファイルを使ってコマンドを定義することもできます。次の場所に配置します:
- グローバル:
~/.config/opencode/commands/ - プロジェクトごと:
.opencode/commands/
---
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 を使うと次を実行できます:
/testPrompt config
カスタムコマンドのプロンプトは、いくつかの特別なプレースホルダーと構文をサポートしています。
Arguments
$ARGUMENTS プレースホルダーを使って、コマンドに引数を渡します。
---
description: Create a new component
---
Create a new React component named $ARGUMENTS with TypeScript support.
Include proper typing and basic structure.引数を付けてコマンドを実行します:
/component Buttonすると、$ARGUMENTS は Button に置き換えられます。
位置パラメータを使って、個々の引数にアクセスすることもできます:
$1- 第 1 引数$2- 第 2 引数$3- 第 3 引数- 以下同様...
例えば:
---
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\" }"これは次のように置き換えられます:
$1はconfig.jsonに$2はsrcに$3は{ "key": "value" }に
Shell output
!command を使って、bash コマンドの出力をプロンプトに挿入します。
例えば、テストカバレッジを分析するカスタムコマンドを作成するには:
---
description: Analyze test coverage
---
Here are the current test results:
!`npm test`
Based on these results, suggest improvements to increase coverage.または、最近の変更をレビューするには:
---
description: Review recent changes
---
Recent git commits:
!`git log --oneline -10`
Review these changes and suggest any improvements.コマンドはプロジェクトのルートディレクトリで実行され、その出力がプロンプトの一部になります。
File references
@ に続けてファイル名を指定して、コマンドにファイルをインクルードします。
---
description: Review component
---
Review the component in @src/components/Button.tsx.
Check for performance issues and suggest improvements.ファイルの内容は自動的にプロンプトに含まれます。
Options
設定オプションを詳しく見てみましょう。
Template
template オプションは、コマンドが実行されたときに LLM に送信されるプロンプトを定義します。
{
"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 オプションを使って、コマンドが何をするかの簡単な説明を提供します。
{
"command": {
"test": {
"description": "Run tests with coverage"
}
}
}これは、コマンドを入力したときに TUI に説明として表示されます。
Agent
agent 設定を使って、このコマンドを実行すべきエージェントを任意で指定します。
これがサブエージェントの場合、コマンドはデフォルトでサブエージェントの呼び出しをトリガーします。
この動作を無効にするには、subtask を false に設定します。
{
"command": {
"review": {
"agent": "plan"
}
}
}これは任意の設定オプションです。指定しない場合、現在のエージェントがデフォルトになります。
Subtask
subtask ブール値を使って、コマンドがサブエージェントの呼び出しをトリガーするよう強制します。
これは、コマンドがプライマリコンテキストを汚染しないようにしたい場合に便利で、エージェント設定で mode が primary に設定されていても、エージェントをサブエージェントとして動作させるよう強制します。
{
"command": {
"analyze": {
"subtask": true
}
}
}これは任意の設定オプションです。
Model
model 設定を使って、このコマンドのデフォルトモデルをオーバーライドします。
{
"command": {
"analyze": {
"model": "anthropic/claude-3-5-sonnet-20241022"
}
}
}これは任意の設定オプションです。
Built-in
opencode には、/init、/undo、/redo、/share、/help のような組み込みコマンドがいくつか含まれています。詳しくはこちら。
注記: カスタムコマンドは組み込みコマンドをオーバーライドできます。
同じ名前のカスタムコマンドを定義すると、組み込みコマンドをオーバーライドします。