OpenCode Autocomplete 指南

OpenCode Autocomplete 指南

Skill 與 Slash Command 是兩套機制:

路徑 用途
Skill ~/.agents/skills/<name>/SKILL.md 提供 Agent 工作流程
Command ~/.config/opencode/commands/<name>.md 提供 / autocomplete 入口

要讓 Skill 出現在 autocomplete,需要建立對應的 Command wrapper。


建立單一 Command

cat > ~/.config/opencode/commands/wiki-query.md <<'EOF'
---
description: Query the configured Obsidian wiki
---

Load the `wiki-query` skill using the native skill tool, then follow its instructions exactly.

User request:

$ARGUMENTS
EOF

重新啟動 OpenCode 後輸入 /wiki 即可看到 /wiki-query


批次建立所有 Skill 的 Commands

#!/usr/bin/env bash
set -euo pipefail

SKILLS_DIR="${SKILLS_DIR:-$HOME/.agents/skills}"
COMMANDS_DIR="${COMMANDS_DIR:-$HOME/.config/opencode/commands}"
FORCE="${1:-}"

mkdir -p "$COMMANDS_DIR"

[ -d "$SKILLS_DIR" ] || { echo "Skills directory not found: $SKILLS_DIR"; exit 1; }

created=0; skipped=0

for skill_file in "$SKILLS_DIR"/*/SKILL.md; do
  [ -e "$skill_file" ] || continue

  skill_name="$(basename "$(dirname "$skill_file")")"
  command_file="$COMMANDS_DIR/$skill_name.md"

  if [ -e "$command_file" ] && [ "$FORCE" != "--force" ]; then
    echo "Skip existing: /$skill_name"
    skipped=$((skipped + 1))
    continue
  fi

  cat > "$command_file" <<EOF
---
description: Invoke the $skill_name skill
---

Load the \`$skill_name\` skill using the native skill tool, then follow its instructions exactly.

User request:

\$ARGUMENTS
EOF

  echo "Created: /$skill_name"
  created=$((created + 1))
done

echo; echo "Created: $created  Skipped: $skipped"
echo "Commands directory: $COMMANDS_DIR"
chmod +x ~/.local/bin/generate-opencode-skill-commands
generate-opencode-skill-commands           # 保留既有
generate-opencode-skill-commands --force   # 強制覆寫

Skills 若為 symlink,find 預設 -P(不跟隨)會找不到 SKILL.md

上面腳本使用 Shell glob("$SKILLS_DIR"/*/SKILL.md),OS 在解析路徑時會跟隨中間的 symlink,不需要特別處理。

若要改用 find,需加 -L

find -L ~/.agents/skills -name SKILL.md

詳見 Hard Link、Symbolic Link 與 find 指令指南


排查 Command 沒有出現在 autocomplete

  1. 路徑:必須是 ~/.config/opencode/commands/,不是 ~/.agents/commands/
  2. 副檔名:必須是 .md
  3. Frontmatter:加入 description 欄位
  4. 重啟:完全離開後重新執行 opencode
ls -1 ~/.config/opencode/commands          # 確認檔案存在
cat ~/.config/opencode/commands/wiki-query.md  # 確認內容正確

延伸閱讀