Configure OpenCode against the TokenHub OpenAI-compatible gateway for terminal-based coding flows.
什么是 OpenCode
OpenCode 是一款命令行 AI 编程工具,支持多种 AI 模型和 Provider,可在终端中辅助编写和调试代码。
系统要求
快速开始
1. 安装 OpenCode
npm install -g opencode2. 创建配置文件
在项目根目录创建 opencode.json:
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"local": {
"npm": "@ai-sdk/openai-compatible",
"name": "local",
"options": {
"baseURL": "https://tokenhub.local/v1"
},
"models": {
"gpt-4o": {
"name": "gpt-4o"
},
"claude-sonnet-4-20250514": {
"name": "claude-sonnet-4-20250514"
}
}
}
}
}3. 设置 API Key
export OPENAI_COMPATIBLE_API_KEY="sk-your-api-key"4. 启动
cd your-project
opencode添加更多模型
在 opencode.json 的 models 对象中添加更多模型即可:
{
"models": {
"gpt-4o": { "name": "gpt-4o" },
"claude-sonnet-4-20250514": { "name": "claude-sonnet-4-20250514" },
"deepseek-chat": { "name": "deepseek-chat" }
}
}常见问题
连接失败?
请检查:
baseURL 是否为 https://tokenhub.local/v1当前公开文档仅覆盖已验证的 OpenAI 兼容接口;其他扩展能力仍需单独验证。