EasyXAI 文档
客户端集成

OpenCode

通过 opencode.json 与环境变量将 OpenCode 接入 EasyXAI,支持多模型配置与原生 Anthropic 协议。

OpenCode 通过配置文件 opencode.json 管理自定义接入配置与模型列表,支持 OpenAI 兼容与 Anthropic 协议。

  • 全局配置路径:~/.config/opencode/opencode.json(Windows 为 %USERPROFILE%\.config\opencode\opencode.json)
  • 项目级配置路径:项目根目录 opencode.json
  • API Key 变量:推荐通过 {env:EASYXAI_API_KEY} 引用环境变量,避免将明文密钥提交进代码仓库

下文示例使用国际站域名 https://api.easyx.ai;中国大陆站请替换为 https://api.easyxai.cn。

1. 修改前先备份配置

macOS / Linux

mkdir -p ~/.config/opencode && [ -f ~/.config/opencode/opencode.json ] && cp ~/.config/opencode/opencode.json ~/.config/opencode/opencode.json.backup-$(date +%Y%m%d-%H%M%S)

Windows(PowerShell)

New-Item -ItemType Directory -Force "$env:USERPROFILE\.config\opencode" | Out-Null
if (Test-Path "$env:USERPROFILE\.config\opencode\opencode.json") {
  Copy-Item "$env:USERPROFILE\.config\opencode\opencode.json" "$env:USERPROFILE\.config\opencode\opencode.json.backup-$(Get-Date -Format 'yyyyMMdd-HHmmss')"
}

2. 编辑 opencode.json

在 provider 下添加 EasyXAI 配置。如果文件已存在,仅合并 provider 中的对应条目,保留原有配置:

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "easyxai": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "EasyXAI",
      "options": {
        "baseURL": "https://api.easyx.ai/v1",
        "apiKey": "{env:EASYXAI_API_KEY}"
      },
      "models": {
        "gpt-5.4": {
          "name": "GPT-5.4"
        },
        "gpt-4.1": {
          "name": "GPT-4.1"
        },
        "claude-sonnet-4.5": {
          "name": "Claude Sonnet 4.5"
        },
        "gemini-2.5-flash": {
          "name": "Gemini 2.5 Flash"
        }
      }
    }
  }
}

可选:为 Claude 模型单独配置 Anthropic 原生协议

如果需要针对 Claude 系列使用 Anthropic 原生消息格式与提示词缓存,可以额外增加一个使用 @ai-sdk/anthropic 的配置项。注意 @ai-sdk/anthropic 会直接在 baseURL 后拼接 /messages,因此这里同样保留 /v1:

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "easyxai-anthropic": {
      "npm": "@ai-sdk/anthropic",
      "name": "EasyXAI Anthropic",
      "options": {
        "baseURL": "https://api.easyx.ai/v1",
        "apiKey": "{env:EASYXAI_API_KEY}"
      },
      "models": {
        "claude-sonnet-4.5": {
          "name": "Claude Sonnet 4.5"
        },
        "claude-opus-4.5": {
          "name": "Claude Opus 4.5"
        }
      }
    }
  }
}

models 下的键名必须与你当前 API Key 在模型列表中返回的 data[].id 完全一致。

3. 设置环境变量并启动

将 easyx_ws_xxx 替换为你的 EasyXAI API Key:

macOS / Linux(Bash / Zsh)

export EASYXAI_API_KEY="easyx_ws_xxx"

Windows(PowerShell)

$env:EASYXAI_API_KEY="easyx_ws_xxx"

Windows(CMD)

set "EASYXAI_API_KEY=easyx_ws_xxx"

设置完成后启动 OpenCode:

opencode

在交互界面中输入 /models,选择 EasyXAI 下的模型即可开始对话。

常见问题排查

现象常见原因处理方式
启动后提示缺少 API Key 或 401当前终端未导出 EASYXAI_API_KEY,或环境变量未写入 shell 启动脚本。确认已执行 export / $env:,或写入 ~/.zshrc / ~/.bashrc 后重开终端。
/models 找不到目标模型opencode.json 的 models 中未声明该模型 ID。先查模型列表确认可用 ID,再补入 models 字段。
请求返回 403 或 429超出 Key 模型范围、所选线路不包含该模型,或触发周期限额。对照错误处理检查 Key 权限与限额配置。