命令与 Skills
用 Markdown 文件或 JSON 定义自定义斜杠命令;用 SKILL.md 让 OpenCode 按需发现可复用的指令:位置、frontmatter 与命名规则。
自定义命令
自定义命令让你指定在 TUI 里执行该命令时要运行的提示,例如 /my-command。它们是内置命令(如 /init、/undo、/redo、/share、/help)之外的补充。创建命令文件:在 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 使用它。也可以放在全局的 ~/.config/opencode/commands/。用 JSON 配置:在配置里用 command 选项:
{
"$schema": "https://opencode.ai/config.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": "Run tests with coverage",
"agent": "build",
"model": "anthropic/claude-3-5-sonnet-20241022"
}
}
}命令模板里的参数占位符和 shell 输出注入等细节见官方 Commands 页。
Agent Skills
Agent skills 让 OpenCode 从你的仓库或主目录发现可复用的指令。Skills 通过原生的 skill 工具按需加载:智能体看到可用的 skills,需要时加载完整内容。为每个 skill 名创建一个文件夹并在里面放一个 SKILL.md。OpenCode 搜索这些位置:项目配置 .opencode/skills/<name>/SKILL.md;全局配置 ~/.config/opencode/skills/<name>/SKILL.md;项目 Claude 兼容 .claude/skills/<name>/SKILL.md;全局 Claude 兼容 ~/.claude/skills/<name>/SKILL.md;项目智能体兼容 .agents/skills/<name>/SKILL.md;全局智能体兼容 ~/.agents/skills/<name>/SKILL.md。
发现:对项目本地路径,OpenCode 从当前工作目录向上走到 git 工作树,加载 .opencode/ 里任何匹配的 skills/*/SKILL.md 以及沿途任何匹配的 .claude/skills/*/SKILL.md 或 .agents/skills/*/SKILL.md;全局定义也会从上面三个全局位置加载。frontmatter:每个 SKILL.md 必须以 YAML frontmatter 开头,只识别这些字段:name(必填)、description(必填)、license、compatibility、metadata(字符串到字符串的映射),未知字段被忽略。名称校验:name 必须 1 到 64 个字符、小写字母数字且用单个连字符分隔、不以 - 开头或结尾、不含连续的 --,并与包含 SKILL.md 的目录名一致(等价正则 ^[a-z0-9]+(-[a-z0-9]+)*$)。