【AI编程】opencode连接火山引擎coding plan

下面我把安装脚本配置脚本 以及完整的操作流程(包括获取正确的 API Key、认证和启动)全部总结在一起。你可以按顺序一步步执行。


📦 一、安装 OpenCode(install_opencode.ps1)

将下面的代码保存为 install_opencode.ps1右键 → 使用 PowerShell 运行

powershell 复制代码
# install_opencode.ps1 - OpenCode Installer for Windows 11

Write-Host "OpenCode Installer for Windows 11" -ForegroundColor Green
Write-Host ""

Write-Host "Select installation method:" -ForegroundColor Cyan
Write-Host "  1) npm (Node.js required, recommended)"
Write-Host "  2) Scoop (requires Scoop)"
Write-Host "  3) Chocolatey (requires Chocolatey)"
Write-Host "  4) Download desktop installer (.exe)"
Write-Host ""

$choice = Read-Host "Enter option (1/2/3/4)"

switch ($choice) {
    "1" {
        Write-Host ""
        Write-Host "Checking Node.js..." -ForegroundColor Yellow
        $nodeVersion = node --version 2>$null
        if ($nodeVersion) {
            Write-Host "Node.js found: $nodeVersion" -ForegroundColor Green
        } else {
            Write-Host "Node.js not found!" -ForegroundColor Red
            Write-Host "Please install Node.js from https://nodejs.org/" -ForegroundColor Yellow
            pause
            exit 1
        }
        
        Write-Host "Installing opencode-ai via npm..." -ForegroundColor Yellow
        npm install -g opencode-ai@latest --registry=https://registry.npmmirror.com
        
        if ($LASTEXITCODE -eq 0) {
            Write-Host "Installation successful!" -ForegroundColor Green
        } else {
            Write-Host "Installation failed." -ForegroundColor Red
            pause
            exit 1
        }
    }
    "2" {
        Write-Host ""
        Write-Host "Checking Scoop..." -ForegroundColor Yellow
        $scoopVersion = scoop --version 2>$null
        if ($scoopVersion) {
            Write-Host "Scoop found" -ForegroundColor Green
        } else {
            Write-Host "Scoop not found!" -ForegroundColor Red
            Write-Host "Please install Scoop first:" -ForegroundColor Yellow
            Write-Host "Set-ExecutionPolicy RemoteSigned -Scope CurrentUser" -ForegroundColor Cyan
            Write-Host "irm get.scoop.sh | iex" -ForegroundColor Cyan
            pause
            exit 1
        }
        
        Write-Host "Installing opencode via Scoop..." -ForegroundColor Yellow
        scoop install opencode
        
        if ($LASTEXITCODE -eq 0) {
            Write-Host "Installation successful!" -ForegroundColor Green
        } else {
            Write-Host "Installation failed." -ForegroundColor Red
            pause
            exit 1
        }
    }
    "3" {
        Write-Host ""
        Write-Host "Checking Chocolatey..." -ForegroundColor Yellow
        $chocoVersion = choco --version 2>$null
        if ($chocoVersion) {
            Write-Host "Chocolatey found" -ForegroundColor Green
        } else {
            Write-Host "Chocolatey not found!" -ForegroundColor Red
            Write-Host "Please install Chocolatey first:" -ForegroundColor Yellow
            Write-Host "Set-ExecutionPolicy Bypass -Scope Process -Force" -ForegroundColor Cyan
            Write-Host "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072" -ForegroundColor Cyan
            Write-Host "iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))" -ForegroundColor Cyan
            pause
            exit 1
        }
        
        Write-Host "Installing opencode via Chocolatey..." -ForegroundColor Yellow
        choco install opencode -y
        
        if ($LASTEXITCODE -eq 0) {
            Write-Host "Installation successful!" -ForegroundColor Green
        } else {
            Write-Host "Installation failed." -ForegroundColor Red
            pause
            exit 1
        }
    }
    "4" {
        Write-Host ""
        Write-Host "Opening download page..." -ForegroundColor Yellow
        Write-Host "Please download the desktop installer from:" -ForegroundColor Yellow
        Write-Host "https://opencode.ai/download" -ForegroundColor Cyan
        Start-Process "https://opencode.ai/download"
        pause
        exit 0
    }
    default {
        Write-Host "Invalid option" -ForegroundColor Red
        pause
        exit 1
    }
}

Write-Host ""
Write-Host "Verifying installation..." -ForegroundColor Yellow
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
$opencodeVersion = opencode --version 2>$null
if ($opencodeVersion) {
    Write-Host "OpenCode version: $opencodeVersion" -ForegroundColor Green
} else {
    Write-Host "opencode command not found. Please restart PowerShell." -ForegroundColor Yellow
}

Write-Host ""
Write-Host "Installation completed!" -ForegroundColor Green
Write-Host "Next step: Run setup_opencode.ps1 to configure Volcano Engine API Key" -ForegroundColor Cyan
pause

使用说明

  • 右键脚本 → "使用 PowerShell 运行"
  • 推荐选 1) npm(需要先安装 Node.js)
  • 如果 npm 安装卡住,脚本已经内置了国内镜像源(--registry=https://registry.npmmirror.com

⚙️ 二、配置 OpenCode 使用火山引擎(setup_opencode.ps1)

将下面的代码保存为 setup_opencode.ps1暂时不要运行,我们先去获取正确的 API Key。

powershell 复制代码
# setup_opencode.ps1 - Configure OpenCode with Volcano Engine API Key (Safe JSON generation)

Write-Host "OpenCode Volcano Engine Configuration" -ForegroundColor Green
Write-Host ""

$VOLC_API_KEY = Read-Host "Enter your Volcano Engine API Key"

if (-not $VOLC_API_KEY) {
    Write-Host "Error: API Key cannot be empty" -ForegroundColor Red
    pause
    exit 1
}

$CONFIG_DIR = Join-Path $env:USERPROFILE ".config\opencode"
$CONFIG_FILE = Join-Path $CONFIG_DIR "opencode.json"

Write-Host ""
Write-Host "Config file location: $CONFIG_FILE" -ForegroundColor Cyan

if (-not (Test-Path $CONFIG_DIR)) {
    Write-Host "Creating config directory..." -ForegroundColor Yellow
    New-Item -ItemType Directory -Path $CONFIG_DIR -Force | Out-Null
}

if (Test-Path $CONFIG_FILE) {
    $timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
    $BACKUP_FILE = "$CONFIG_FILE.backup_$timestamp"
    Write-Host "Backing up existing config to: $BACKUP_FILE" -ForegroundColor Yellow
    Copy-Item $CONFIG_FILE $BACKUP_FILE
}

Write-Host "Generating config file..." -ForegroundColor Yellow

$config = @{
    "`$schema" = "https://opencode.ai/config.json"
    provider = @{
        "volcengine-codingplan" = @{
            npm = "@ai-sdk/openai-compatible"
            name = "volcengine-codingplan"
            options = @{
                baseURL = "https://ark.cn-beijing.volces.com/api/coding/v3"
                apiKey = $VOLC_API_KEY
            }
            models = @{
                "ark-code-latest" = @{
                    name = "ark-code-latest"
                    options = @{
                        thinking = @{
                            type = "enabled"
                        }
                    }
                }
                "kimi-k2.5" = @{
                    name = "kimi-k2.5"
                    modalities = @{
                        input = @("text", "image")
                        output = @("text")
                    }
                    options = @{
                        thinking = @{
                            type = "enabled"
                        }
                    }
                }
            }
        }
    }
}

$configJson = $config | ConvertTo-Json -Depth 10
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
[System.IO.File]::WriteAllText($CONFIG_FILE, $configJson, $utf8NoBom)

if (Test-Path $CONFIG_FILE) {
    Write-Host "Config file created successfully" -ForegroundColor Green
    Write-Host "Preview:" -ForegroundColor Cyan
    Get-Content $CONFIG_FILE -TotalCount 10
} else {
    Write-Host "Failed to create config file" -ForegroundColor Red
    pause
    exit 1
}

Write-Host ""
Write-Host "========== Next Steps ==========" -ForegroundColor Green
Write-Host "1. Run in the same PowerShell window:" -ForegroundColor Yellow
Write-Host "   opencode auth login" -ForegroundColor Cyan
Write-Host "2. Select 'other' from the list" -ForegroundColor Yellow
Write-Host "3. Enter identifier: volcengine-codingplan" -ForegroundColor Yellow
Write-Host "4. Enter API Key (paste with right-click)" -ForegroundColor Yellow
Write-Host "5. Restart OpenCode" -ForegroundColor Yellow
Write-Host "=================================" -ForegroundColor DarkGray

pause

修改opencode.json

yaml 复制代码
{
  "$schema": "https://opencode.ai/config.json",
  "model": "volcengine-plan/ark-code-latest",
  "provider": {
    "volcengine-plan": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Volcano Engine",
      "options": {
        "baseURL": "https://ark.cn-beijing.volces.com/api/coding/v3",
        "apiKey": "api-key"
      },
      "models": {
        "ark-code-latest": {
          "name": "ark-code-latest",
          "limit": {
            "context": 256000,
            "output": 4096
          },
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        },
        "doubao-seed-code": {
          "name": "doubao-seed-code",
          "limit": {
            "context": 256000,
            "output": 4096
          },
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        },
        "glm-5.1": {
          "name": "glm-5.1",
          "limit": {
            "context": 200000,
            "output": 4096
          },
          "modalities": {
            "input": [
              "text"
            ],
            "output": [
              "text"
            ]
          }
        },
        "deepseek-v3.2": {
          "name": "deepseek-v3.2",
          "limit": {
            "context": 128000,
            "output": 4096
          }
        },
        "deepseek-v4-flash": {
          "name": "deepseek-v4-flash",
          "limit": {
            "context": 1024000,
            "output": 4096
          }
        },
        "deepseek-v4-pro": {
          "name": "deepseek-v4-pro",
          "limit": {
            "context": 1024000,
            "output": 4096
          }
        },
        "doubao-seed-2.0-code": {
          "name": "doubao-seed-2.0-code",
          "limit": {
            "context": 256000,
            "output": 4096
          },
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        },
        "doubao-seed-2.0-pro": {
          "name": "doubao-seed-2.0-pro",
          "limit": {
            "context": 256000,
            "output": 4096
          },
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        },
        "doubao-seed-2.0-lite": {
          "name": "doubao-seed-2.0-lite",
          "limit": {
            "context": 256000,
            "output": 4096
          },
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        },
        "minimax-latest": {
          "name": "minimax-latest",
          "limit": {
            "context": 200000,
            "output": 4096
          },
          "modalities": {
            "input": [
              "text"
            ],
            "output": [
              "text"
            ]
          }
        },
        "kimi-k2.6": {
          "name": "kimi-k2.6",
          "limit": {
            "context": 256000,
            "output": 4096
          },
          "modalities": {
            "input": [
              "text",
              "image"
            ],
            "output": [
              "text"
            ]
          }
        }
      }
    }
  }
}

🔑 三、获取正确的 API Key(关键步骤)

你之前用的 ark-xxx 格式的 Key 是 推理接入点专用 的,OpenCode 需要 Coding Plan 套餐专用的 API Key (以 sk-sp-sk-ark- 开头)。请按以下步骤获取:

  1. 打开火山引擎控制台 ,在顶部搜索框输入 "开通管理",点击进入。
  2. 在已开通的服务列表中找到 Coding Plan ,点击右侧的 "配置""管理"
  3. 进入 Coding Plan 后台后,找到 "快速配置""管理API Key" ,点击 生成 API Key
  4. 系统会生成一个专属 Key(格式类似 sk-sp-xxxxxx)。立即复制并保存(关闭页面后不会再完整显示)。
  5. 删除旧凭据 :在 PowerShell 中执行 opencode auth logout volcengine-codingplan(忽略"不存在"的提示)。

🚀 四、运行配置脚本并完成认证

  1. 运行配置脚本 :右键 setup_opencode.ps1 → "使用 PowerShell 运行",提示时输入刚才获取的 sk-sp- 开头的 Key。

  2. 运行认证 :在同一个 PowerShell 窗口中执行:

    复制代码
    opencode auth login
    • 选择 other
    • 输入标识符:volcengine-codingplan
    • 输入 API Key:粘贴你的 sk-sp-xxx Key
    • 看到 Done 即成功

✨ 五、启动使用

进入你的项目目录(例如 cd my-project),然后运行:

复制代码
opencode

首次使用建议输入 /init 让 OpenCode 分析项目。之后就可以像聊天一样提出编程需求了。


❓ 常见问题速查

问题 解决方法
安装卡住 脚本已内置国内镜像,若仍卡住按 Ctrl+C 终止,重新运行
脚本报语法错误 用记事本另存为 UTF-8 with BOM 编码
opencode auth login 报 JSON 错误 用新版 setup_opencode.ps1 重新生成配置
API key format incorrect 你用了 ark- 开头的 Key,必须从 Coding Plan 后台获取 sk-sp- 开头的专用 Key
认证时找不到 other 用方向键 ↓ 滚动到底部

以上就是完整的安装配置流程。如果还有任何一步卡住,请告诉我你执行到哪一步、看到了什么报错。

相关推荐
2601_955759625 小时前
如何借助 Claude Opus 5 自动整理日报、周报和项目进展
ai编程
桦说编程6 小时前
炮轰SDD:Spec驱动开发为何不适合绝大多数项目
llm·ai编程·代码规范
东小西6 小时前
第15篇:《检索效果太烂?我用Query改写和重排序把准确率翻了一倍》
openai·ai编程
旋生万物7 小时前
五条几何公理重构元素周期表:从量子力学到螺旋拓扑的降维(AI 编程时代的微观启示)
重构·ai编程·代码审计·cursor·ai agent·mcp协议·claudecode
ServBay8 小时前
AI Gateway 与直连 LLM API 的应该怎么选,一篇文章说明白
aigc·ai编程
JavaGuide8 小时前
GitHub 9.8 万 Star!把整个代码仓库变成知识图谱,这个 AI Coding 工具太适合 Claude Code / Codex 了
前端·后端·ai编程
Staticy8 小时前
Claude Code 接国产模型不能识图?一个 MCP 让纯文本模型也能看图
人工智能·ai编程·全栈
东小西9 小时前
第14篇:《公司制度问答机器人上线:老板问"能加薪吗",AI回答"请看第三章第四条"》
openai·ai编程
程序员鱼皮9 小时前
Claude Opus 5 全新发布,7 大项目实测,夯还是拉?半价吊打 Fable 5?
前端·后端·ai编程
众人皆醒我独醉10 小时前
为什么 AI 每次回答不一样?—— 温度参数是 AI 的"创意调节旋钮"
面试·ai编程