OpenCode 安裝指南
OpenCode 是開源社群維護的 AI 程式碼編輯器,基於 VSCode 構建,專注於提供透明、可自訂的 AI 輔助開發體驗,並完整支援 Superpowers 技能系統。
平台特色
OpenCode 作為開源替代方案,提供:
- 🔓 完全開源:程式碼完全透明,可自訂和審查
- 🤖 多模型支援:支援 Claude、GPT-4、本地模型等
- 🔌 VSCode 相容:完全相容 VSCode 擴展生態
- 🛠️ 彈性配置:高度可自訂的配置選項
- 📚 Superpowers 支援:完整技能生態系統整合
系統需求
硬體需求
- 處理器:x64_64 或 ARM64 架構
- 記憶體:至少 4 GB RAM(建議 8 GB 以上)
- 硬碟空間:至少 500 MB 可用空間
軟體需求
- 作業系統:
- Windows 10/11(x64)
- macOS 11+ (Big Sur 或更高版本)
- Linux(Ubuntu 20.04+、Debian 11+、Fedora 35+)
- 網路:穩定的網際網路連線
- Node.js:v18 或更高版本
安裝步驟
從源碼安裝
# 第 1 步:複製原始碼
git clone https://github.com/opencode/opencode.git
cd opencode
# 第 2 步:安裝相依套件
npm install
# 第 3 步:建置專案
npm run build
# 第 4 步:連結全域
npm link --global
# 第 5 步:驗證安裝
opencode --version
使用安裝腳本
# 下載並執行安裝腳本
curl -fsSL https://get.opencode.dev | sh
# 或使用 wget
wget -qO- https://get.opencode.dev | sh
# 驗證安裝
opencode --version
套件管理器安裝
# 使用 npm 安裝
npm install -g @opencode/editor
# 使用 pnpm 安裝(推薦)
pnpm add -g @opencode/editor
# 使用 yarn 安裝
yarn global add @opencode/editor
配置設定
基礎配置
# 建立配置目錄
mkdir -p ~/.opencode
# 建立配置檔案
cat > ~/.opencode/config.json << 'EOF'
{
"editor": {
"theme": "dark",
"fontSize": 14,
"tabSize": 2
},
"ai": {
"defaultModel": "claude-fable-5",
"maxTokens": 200000,
"temperature": 0.7
},
"superpowers": {
"enabled": true,
"skillsPath": "~/.superpowers/skills",
"autoLoad": true
}
}
EOF
環境變數設定
# 設定 API 金鑰
export ANTHROPIC_API_KEY="your-api-key"
export OPENAI_API_KEY="your-api-key" # 如果使用 GPT
# 設定模型偏好
export OPENCODE_DEFAULT_MODEL="claude-fable-5"
export OPENCODE_MAX_TOKENS="200000"
# 設定 Superpowers 路徑
export SUPERPOWERS_SKILLS_PATH="$HOME/.superpowers/skills"
專案配置
# 在專案根目錄建立 .opencode 目錄
mkdir -p .opencode
# 建立專案配置
cat > .opencode/config.json << 'EOF'
{
"project": {
"name": "my-awesome-project",
"type": "web-app",
"techStack": ["react", "typescript", "tailwindcss"]
},
"ai": {
"contextFiles": ["README.md", "CLAUDE.md", "package.json"],
"ignorePatterns": ["node_modules", "dist", "build"]
},
"superpowers": {
"enabled": true,
"autoSkills": {
"onSave": "verification-before-completion",
"onCommit": "requesting-code-review"
}
}
}
EOF
使用方式
CLI 使用
# 啟動 OpenCode 編輯器
opencode
# 開啟專案
opencode open my-project
# 使用 Superpowers 技能
opencode ai --skill brainstorming "探索微服務架構"
# 批次處理檔案
opencode ai --batch src/**/*.ts --skill test-driven-development
# 互動式模式
opencode chat
擴展使用
# 安裝 VSCode 擴展
code --install-extension opencode.opencode
# 或在 OpenCode 中直接使用 VSCode 擴展市集
# Ctrl+Shift+X -> 搜尋擴展 -> 安裝
Superpowers 整合
安裝 Superpowers 擴展
# 在 OpenCode 中
# 1. 開啟擴展面板
# 2. 搜尋 "Superpowers for OpenCode"
# 3. 點擊 "Install"
# 或透過 CLI 安裝
opencode extensions install superpowers
配置 Superpowers
{
"superpowers.enabled": true,
"superpowers.apiKey": "your-api-key",
"superpowers.model": "claude-fable-5",
"superpowers.fallbackModel": "claude-opus-4-8",
"superpowers.skillsPath": "~/.superpowers/skills",
"superpowers.contextFiles": ["*.{md,mdx,txt}"],
"superpowers.ignorePatterns": ["node_modules", "dist", ".git"]
}
開發流程整合
Git 整合
# 建立 git hooks
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/bash
# OpenCode 自動執行驗證
opencode ai --skill verification-before-completion --cached
if [ $? -ne 0 ]; then
echo "驗證失敗,提交中止"
exit 1
fi
EOF
chmod +x .git/hooks/pre-commit
CI/CD 整合
# GitHub Actions 整合
# .github/workflows/opencode-review.yml
name: OpenCode Superpowers Review
on: [pull_request]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '18'
- name: Install OpenCode
run: npm install -g @opencode/cli
- name: Run Superpowers Review
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
opencode ai --skill requesting-code-review \
--diff origin/main...HEAD \
--output review-report.md
- name: Upload Review
uses: actions/upload-artifact@v2
with:
name: review-report
path: review-report.md
進階配置
自訂模型設定
{
"ai.models": {
"claude-fable-5": {
"api": "anthropic",
"maxTokens": 200000,
"timeout": 120000
},
"claude-opus-4-8": {
"api": "anthropic",
"maxTokens": 100000,
"timeout": 60000
},
"gpt-4": {
"api": "openai",
"maxTokens": 8000,
"timeout": 30000
}
}
}
技能熱鍵
# 在 keybindings.json 中設定
{
"key": "ctrl+shift+b",
"command": "opencode.ai.runSkill",
"args": {
"skill": "brainstorming"
}
}
{
"key": "ctrl+shift+t",
"command": "opencode.ai.runSkill",
"args": {
"skill": "test-driven-development"
}
}
常見問題
安裝問題
Q1: 原始碼安裝失敗
A:
# 檢查 Node.js 版本
node --version
# 需要 v18 或更高版本
# 清除 npm cache
npm cache clean --force
# 重新安裝
rm -rf node_modules package-lock.json
npm install
Q2: 全域連結失敗
A:
# 檢查 npm 全域路徑
npm config get prefix
# 手動加入 PATH
export PATH=$(npm config get prefix)/bin:$PATH
# 或使用 npx
npx @opencode/editor
Q3: 環境變數無效
A:
# 確認環境變數設定位置
# 在 ~/.bashrc 或 ~/.zshrc 中加入
export ANTHROPIC_API_KEY="your-key"
# 重新載入 shell 配置
source ~/.bashrc # bash
source ~/.zshrc # zsh
# 驗證設定
echo $ANTHROPIC_API_KEY
配置問題
Q4: Superpowers 無法載入
A:
# 檢查技能路徑
ls -la ~/.superpowers/skills/
# 重新設定路徑
export SUPERPOWERS_SKILLS_PATH="$HOME/.superpowers/skills"
# 重新載入 OpenCode
opencode reload
Q5: 模型切換無效
A:
// 在配置中明確指定預設模型
{
"ai": {
"defaultModel": "claude-fable-5",
"forceModel": true
}
}
// 或透過環境變數
export OPENCODE_FORCE_MODEL="claude-fable-5"
使用問題
Q6: AI 回應品質不佳
A:
// 調整模型參數
{
"ai": {
"temperature": 0.3,
"topP": 0.9,
"frequencyPenalty": 0.5
},
"superpowers": {
"contextFiles": ["*"],
"maxContextTokens": 8000
}
}
效能優化
快取優化
# 啟用快取功能
{
"ai.cache.enabled": true,
"ai.cache.maxSize": 1000,
"ai.cache.ttl": 3600
}
# 清除快取
opencode cache clear
並行處理
# 啟用並行處理
{
"ai.parallel.enabled": true,
"ai.parallel.maxWorkers": 4
}
記憶體優化
# 限制記憶體使用
{
"ai.maxTokens": 50000,
"ai.maxContextFiles": 10,
"ai.maxFileSize": 100000
}
資源連結
官方資源
- GitHub:https://github.com/opencode/opencode
- 文件:https://docs.opencode.dev
- Discord:https://discord.gg/opencode
- 部落格:https://blog.opencode.dev
學習資源
- 教程:https://docs.opencode.dev/tutorials
- API 文件:https://docs.opencode.dev/api
- 範例:https://github.com/opencode/examples
社群資源
- GitHub Discussions:https://github.com/opencode/opencode/discussions
- Stack Overflow:https://stackoverflow.com/questions/tagged/opencode
- Reddit:https://reddit.com/r/opencode
下一步
安裝完成後,建議:
- 探索功能:嘗試 AI 聊天和程式碼補全
- 安裝 Superpowers:安裝並配置技能系統
- 自訂配置:根據需求調整配置
- 建立專案:建立新專案並配置 OpenCode
- 參與社群:加入 Discord 和 GitHub 討論
開始使用 OpenCode 和 Superpowers,體驗開源 AI 編輯器的自由和強大!