Opencode一键增加Aiberm接入以及安装oh-my-opencode

执行一下命令

bash -lc 'set -euo pipefail

# 0) 依赖检查
command -v curl >/dev/null
command -v python3 >/dev/null

# 1) 读取 key:优先用环境变量(方便自动化),否则交互输入一次
if [ -n "${AIBERM_API_KEY:-}" ]; then
  KEY="$AIBERM_API_KEY"
else
  read -s -p "Enter AIBERM_API_KEY (input hidden): " KEY; echo
fi

# 2) 把 key 持久化到文件(推荐用 {file:...} 引用,避免依赖环境变量) 
CONF_DIR="$HOME/.config/opencode"
SECRET_DIR="$CONF_DIR/secrets"
KEY_FILE="$SECRET_DIR/aiberm.key"
mkdir -p "$SECRET_DIR"
( umask 177; printf "%s" "$KEY" >"$KEY_FILE" )
chmod 600 "$KEY_FILE"

# 3) 拉取 aiberm 可用模型列表(OpenAI-compatible 通常提供 /v1/models)
MODELS_JSON="$(curl -fsSL "https://aiberm.com/v1/models" \
  -H "Authorization: Bearer ${KEY}" \
  -H "Content-Type: application/json")"

# 4) 合并写入全局 opencode.json(保留你现有 google/openrouter/openai 等配置)
CFG="$CONF_DIR/opencode.json"
test -f "$CFG" || echo "{}" > "$CFG"

MODELS_JSON="$MODELS_JSON" python3 - <<'"'"'PY'"'"'
import json, os, sys

models = json.loads(os.environ["MODELS_JSON"])
data = models.get("data") or []
ids = [m.get("id") for m in data if isinstance(m, dict) and m.get("id")]

if not ids:
    print("ERROR: https://aiberm.com/v1/models 没有返回 data[].id,无法导入全部模型。", file=sys.stderr)
    print("Response head:", json.dumps(models, ensure_ascii=False)[:300], file=sys.stderr)
    sys.exit(2)

cfg_path = os.path.expanduser("~/.config/opencode/opencode.json")
cfg = json.load(open(cfg_path))
cfg.setdefault("provider", {})

cfg["provider"]["aiberm"] = {
  "npm": "@ai-sdk/openai-compatible",
  "name": "AiBerm",
  "options": {
    "baseURL": "https://aiberm.com/v1",
    "apiKey": "{file:secrets/aiberm.key}"
  },
  "models": {mid: {} for mid in ids}
}

json.dump(cfg, open(cfg_path, "w"), indent=2, ensure_ascii=False)
print(f"OK: imported {len(ids)} models into {cfg_path}")
PY

# 5) 刷新 OpenCode 模型缓存(可选,但建议)
opencode models --refresh >/dev/null 2>&1 || true

echo
echo "DONE."
echo "验证:opencode models --verbose | grep \"^aiberm/\" | head"
echo "进入TUI后:/models 选择 aiberm/<model_id>"
'

安装oh-my-opencode

bash -lc 'command -v bun >/dev/null 2>&1 || curl -fsSL https://bun.sh/install | bash; export BUN_INSTALL="$HOME/.bun"; export PATH="$BUN_INSTALL/bin:$PATH"; bunx oh-my-opencode install'

 

滚动至顶部