第一步:安装 OpenCode

🍎 macOS / Linux
🪟 Windows

方法一(推荐):官方脚本

curl -fsSL https://opencode.ai/install | bash

方法二:Homebrew(macOS)

brew install opencode-ai/tap/opencode

方法三:npm

npm install -g opencode-ai

验证:

opencode --version

方法一(推荐):Scoop

# 未安装 Scoop 先执行这两行
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression

# 安装 OpenCode
scoop bucket add opencode https://github.com/opencode-ai/scoop-bucket
scoop install opencode

方法二:npm

npm install -g opencode-ai

验证:

opencode --version

第二步:写入配置文件

创建以下路径的 opencode.json 文件(不存在时手动创建目录):

系统配置文件路径
macOS / Linux~/.config/opencode/opencode.json
Windows%APPDATA%\opencode\opencode.json
(可选)项目级项目根目录下的 opencode.json

文件内容(将 sk-你的密钥 替换为你的 AIHUB API Key):

AIHUB OpenCode 配置参考

AIHUB「使用密钥」→ OpenCode 标签 — 可直接复制下方配置

{
  "provider": {
    "openai": {
      "options": {
        "baseURL": "https://aihub.mangmang.fun/v1",
        "apiKey": "sk-你的密钥"
      },
      "models": {
        "gpt-5.2": { "name": "GPT-5.2", "limit": { "context": 400000, "output": 128000 }, "options": { "store": false }, "variants": { "low": {}, "medium": {}, "high": {}, "xhigh": {} } },
        "gpt-5.5": { "name": "GPT-5.5", "limit": { "context": 1050000, "output": 128000 }, "options": { "store": false }, "variants": { "low": {}, "medium": {}, "high": {}, "xhigh": {} } },
        "gpt-5.4": { "name": "GPT-5.4", "limit": { "context": 1050000, "output": 128000 }, "options": { "store": false }, "variants": { "low": {}, "medium": {}, "high": {}, "xhigh": {} } },
        "gpt-5.4-mini": { "name": "GPT-5.4 Mini", "limit": { "context": 400000, "output": 128000 }, "options": { "store": false }, "variants": { "low": {}, "medium": {}, "high": {}, "xhigh": {} } },
        "gpt-5.3-codex-spark": { "name": "GPT-5.3 Codex Spark", "limit": { "context": 128000, "output": 32000 }, "options": { "store": false }, "variants": { "low": {}, "medium": {}, "high": {}, "xhigh": {} } },
        "gpt-5.3-codex": { "name": "GPT-5.3 Codex", "limit": { "context": 400000, "output": 128000 }, "options": { "store": false }, "variants": { "low": {}, "medium": {}, "high": {}, "xhigh": {} } },
        "codex-mini-latest": { "name": "Codex Mini", "limit": { "context": 200000, "output": 100000 }, "options": { "store": false }, "variants": { "low": {}, "medium": {}, "high": {} } }
      }
    }
  },
  "agent": {
    "build": { "options": { "store": false } },
    "plan": { "options": { "store": false } }
  },
  "$schema": "https://opencode.ai/config.json"
}
🔑

API Key 在 AIHUB → API 密钥 页面获取,点击「使用密钥」→ OpenCode 标签可直接复制完整配置。

macOS / Linux 一键创建

mkdir -p ~/.config/opencode
# 用你自己的 key 替换 sk-xxx
cat > ~/.config/opencode/opencode.json << 'EOF'
{ "provider": { "openai": { "options": { "baseURL": "https://aihub.mangmang.fun/v1", "apiKey": "sk-xxx" } } }, "$schema": "https://opencode.ai/config.json" }
EOF

Windows PowerShell 一键创建

New-Item -Path "$env:APPDATA\opencode" -ItemType Directory -Force
'{ "provider": { "openai": { "options": { "baseURL": "https://aihub.mangmang.fun/v1", "apiKey": "sk-xxx" } } }, "$schema": "https://opencode.ai/config.json" }' | Set-Content "$env:APPDATA\opencode\opencode.json"

⚠️ 一键创建命令只写入基础配置,不含 models 字段。如需指定模型,请手动编辑文件添加上方完整内容。

第三步:运行 OpenCode

# 进入项目目录后直接运行
cd /你的项目目录
opencode

# 指定模型
opencode --model openai/gpt-5.5

# 直接提问(非交互模式)
opencode "分析这个项目的目录结构"

TUI 常用快捷键

快捷键功能
Enter发送
Ctrl + J换行(不发送)
Ctrl + C中止生成
Ctrl + N新对话
q / Ctrl+D退出
/Slash 命令

📋 支持的模型(AIHUB)

模型 ID名称上下文最大输出
openai/gpt-5.5GPT-5.51,050,000128,000
openai/gpt-5.4GPT-5.41,050,000128,000
openai/gpt-5.4-miniGPT-5.4 Mini400,000128,000
openai/gpt-5.3-codexGPT-5.3 Codex400,000128,000
openai/gpt-5.2GPT-5.2400,000128,000
openai/gpt-5.3-codex-sparkGPT-5.3 Codex Spark128,00032,000
openai/codex-mini-latestCodex Mini200,000100,000

❓ 常见问题

「no provider configured」

检查配置文件路径是否正确,JSON 格式是否有效,可用 jsonlint.com 验证。

如何从 AIHUB 获取完整配置?

登录 → API 密钥 → 点击「使用密钥」→ 选 OpenCode 标签 → 复制 → 粘贴到配置文件,替换 API Key 即可。

🔄

多个 API Key 切换?推荐 CC Switch,无需手动修改配置文件。