Custom Commands
Tạo các lệnh tùy chỉnh cho công việc lặp lại thường xuyên.
Tổng quan
Custom Commands cho phép bạn định nghĩa các prompt sẵn để chạy khi gọi lệnh trong TUI. Thay vì gõ lại prompt dài mỗi lần, bạn chỉ cần:
/my-command
Custom commands hoạt động song song với các lệnh built-in như /init, /undo, /redo, /share, /help.
Tạo Command File
Tạo các file markdown trong thư mục commands/ để định nghĩa custom commands.
Cấu trúc thư mục
.opencode/
commands/
test.md
review.md
deploy.md
Ví dụ command đơn giản
Tạo file .opencode/commands/test.md:
---
description: Chạy tests với coverage
agent: build
model: anthropic/claude-3-5-sonnet-20241022
---
Chạy full test suite với coverage report và hiển thị các test failures.
Tập trung vào các test lỗi và đề xuất cách fix.
Sau đó sử dụng bằng cách gõ:
/test
Cấu hình Command
Có 2 cách để định nghĩa custom commands:
1. Dùng Markdown (Khuyến nghị)
Đặt file .md vào:
- Global:
~/.config/opencode/commands/ - Per-project:
.opencode/commands/
Tên file sẽ trở thành tên command. Ví dụ: test.md cho phép chạy /test.
2. Dùng JSON config
Thêm vào file opencode.json:
{
"$schema": "https://opencode.ai/config.json",
"command": {
"test": {
"template": "Chạy full test suite với coverage report.",
"description": "Chạy tests với coverage",
"agent": "build",
"model": "anthropic/claude-3-5-sonnet-20241022"
}
}
}
Prompt Config
Arguments - Truyền tham số
Sử dụng $ARGUMENTS để nhận toàn bộ tham số:
---
description: Tạo component mới
---
Tạo React component mới với tên $ARGUMENTS, bao gồm TypeScript types.
Chạy:
/component Button
$ARGUMENTS sẽ được thay bằng Button.
Positional Arguments
Sử dụng $1, $2, $3... cho từng tham số:
---
description: Tạo file mới với nội dung
---
Tạo file có tên $1 trong thư mục $2
với nội dung: $3
Chạy:
/create-file config.json src "{ \"key\": \"value\" }"
Shell Output - Chèn output lệnh shell
Dùng cú pháp !`command` để chèn output từ bash command:
---
description: Phân tích test coverage
---
Đây là kết quả test hiện tại:
!`npm test`
Dựa trên kết quả, đề xuất cách cải thiện coverage.
File References - Tham chiếu file
Dùng @ kèm đường dẫn file để include nội dung file:
---
description: Review component
---
Review component trong @src/components/Button.tsx.
Kiểm tra performance issues và đề xuất cải thiện.
Options
Template (Bắt buộc)
Prompt sẽ được gửi đến LLM khi command được thực thi.
Description
Mô tả ngắn hiển thị trong TUI khi gõ lệnh.
Agent
Chỉ định agent nào sẽ thực thi command (build, plan, etc.).
{
"command": {
"review": {
"agent": "plan"
}
}
}
Model
Override model mặc định cho command này:
{
"command": {
"analyze": {
"model": "anthropic/claude-3-5-sonnet-20241022"
}
}
}
Subtask
Force command chạy như subagent để không làm ô nhiễm context chính:
{
"command": {
"analyze": {
"subtask": true
}
}
}
Ví dụ thực tế
Command review PR
.opencode/commands/pr-review.md:
---
description: Review Pull Request hiện tại
agent: plan
---
Xem các thay đổi trong PR hiện tại:
!`git diff main...HEAD`
Review code và đề xuất:
1. Potential bugs
2. Performance issues
3. Code style improvements
4. Missing tests
Command generate tests
.opencode/commands/gen-tests.md:
---
description: Generate tests cho file
---
Phân tích file @$1 và generate unit tests bao gồm:
- Happy path tests
- Edge cases
- Error handling tests
Sử dụng testing framework hiện có trong project.
Chạy:
/gen-tests src/utils/calculator.ts
Command deploy check
.opencode/commands/deploy-check.md:
---
description: Kiểm tra trước khi deploy
subtask: true
---
Thực hiện pre-deploy checklist:
1. Build status:
!`npm run build`
2. Test results:
!`npm test`
3. Lint check:
!`npm run lint`
Tổng hợp và báo cáo các issues cần fix trước khi deploy.
Ghi chú
- Custom commands có thể override built-in commands nếu trùng tên
- Sử dụng
subtask: truecho các lệnh không cần giữ lại trong context - Commands được load theo thứ tự: project > global