| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | set -euo pipefail |
| 10 | |
| 11 | export DEBIAN_FRONTEND=noninteractive |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | apt_get() { |
| 28 | local status before='' retries=3 timeout=60 |
| 29 | if [[ "${CLAUDE_CAGE:-}" == 1 ]]; then |
| 30 | before="$(awk '$1 == "oom_kill" {print $2}' /proc/vmstat)" |
| 31 | retries=1 |
| 32 | timeout=15 |
| 33 | fi |
| 34 | if sudo env DEBIAN_FRONTEND=noninteractive apt-get -o Acquire::Retries="$retries" -o Acquire::http::Timeout="$timeout" -o Acquire::https::Timeout="$timeout" "$@" </dev/null; then |
| 35 | return 0 |
| 36 | else |
| 37 | status=$? |
| 38 | fi |
| 39 | if [[ "${CLAUDE_CAGE:-}" == 1 ]]; then |
| 40 | printf 'APT failed (exit %s); guest memory evidence follows. oom_kill before=%s\n' "$status" "$before" >&2 |
| 41 | apt-get --version | head -n 1 >&2 || true |
| 42 | awk '/^(MemTotal|MemAvailable|SwapTotal|SwapFree):/' /proc/meminfo >&2 || true |
| 43 | awk '$1 == "oom_kill" {print "oom_kill after=" $2}' /proc/vmstat >&2 || true |
| 44 | ps -eo pid,rss,comm --sort=-rss | head -n 12 >&2 || true |
| 45 | sudo timeout 5 journalctl -k -b --no-pager -n 40 --grep='Out of memory|Killed process|oom-kill' >&2 || true |
| 46 | printf 'Retry preserves the build VM and package cache. Export diagnostics if installation fails again.\n' >&2 |
| 47 | fi |
| 48 | return "$status" |
| 49 | } |
| 50 | |
| 51 | |
| 52 | |
| 53 | install_repository_key() ( |
| 54 | set -euo pipefail |
| 55 | local url="$1" target="$2" stage replacement='' attempt status=1 |
| 56 | stage="$(mktemp)" |
| 57 | trap 'rm -f "$stage"; if [ -n "$replacement" ]; then sudo rm -f "$replacement"; fi' EXIT |
| 58 | if [[ "${CLAUDE_CAGE:-}" == 1 ]]; then |
| 59 | for attempt in 1 2 3 4; do |
| 60 | if cage_curl_download "$stage" --proto '=https' --proto-redir '=https' --tlsv1.2 --fail --location --show-error --silent \ |
| 61 | --connect-timeout 15 "$url" -o "$stage" </dev/null; then |
| 62 | status=0 |
| 63 | break |
| 64 | else |
| 65 | status=$? |
| 66 | fi |
| 67 | if [ "$attempt" != 4 ]; then sleep "$attempt"; fi |
| 68 | done |
| 69 | [ "$status" = 0 ] || return "$status" |
| 70 | else |
| 71 | curl --proto '=https' --proto-redir '=https' --tlsv1.2 --fail --location --show-error --silent \ |
| 72 | --retry 3 --retry-all-errors --retry-max-time 120 --connect-timeout 15 --max-time 30 "$url" -o "$stage" </dev/null |
| 73 | fi |
| 74 | test -s "$stage" |
| 75 | if [[ "$target" == *.asc ]]; then |
| 76 | grep -q '^-----BEGIN PGP PUBLIC KEY BLOCK-----' "$stage" |
| 77 | grep -q '^-----END PGP PUBLIC KEY BLOCK-----' "$stage" |
| 78 | fi |
| 79 | replacement="$(sudo mktemp "${target}.XXXXXX")" |
| 80 | sudo install -m 0644 -o root -g root "$stage" "$replacement" |
| 81 | sudo mv -f "$replacement" "$target" |
| 82 | replacement='' |
| 83 | ) |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | INSTALL_PYTHON="${AGENT_VM_INSTALL_PYTHON:-1}" |
| 91 | INSTALL_NODE="${AGENT_VM_INSTALL_NODE:-1}" |
| 92 | INSTALL_RUBY="${AGENT_VM_INSTALL_RUBY:-0}" |
| 93 | INSTALL_RUST="${AGENT_VM_INSTALL_RUST:-0}" |
| 94 | INSTALL_GOLANG="${AGENT_VM_INSTALL_GOLANG:-0}" |
| 95 | INSTALL_DOCKER="${AGENT_VM_INSTALL_DOCKER:-1}" |
| 96 | INSTALL_CHROMIUM="${AGENT_VM_INSTALL_CHROMIUM:-1}" |
| 97 | INSTALL_GH="${AGENT_VM_INSTALL_GH:-1}" |
| 98 | INSTALL_CLAUDE="${AGENT_VM_INSTALL_CLAUDE:-1}" |
| 99 | INSTALL_OPENCODE="${AGENT_VM_INSTALL_OPENCODE:-1}" |
| 100 | INSTALL_CODEX="${AGENT_VM_INSTALL_CODEX:-1}" |
| 101 | INSTALL_VIBE="${AGENT_VM_INSTALL_VIBE:-1}" |
| 102 | INSTALL_PI="${AGENT_VM_INSTALL_PI:-0}" |
| 103 | INSTALL_CODE_SERVER="${AGENT_VM_INSTALL_CODE_SERVER:-0}" |
| 104 | |
| 105 | INSTALL_CODE_CLAUDE="${AGENT_VM_INSTALL_CODE_CLAUDE:-0}" |
| 106 | INSTALL_CODE_OPENCODE="${AGENT_VM_INSTALL_CODE_OPENCODE:-0}" |
| 107 | INSTALL_CODE_CODEX="${AGENT_VM_INSTALL_CODE_CODEX:-0}" |
| 108 | INSTALL_CODE_VIBE="${AGENT_VM_INSTALL_CODE_VIBE:-0}" |
| 109 | |
| 110 | |
| 111 | if [[ "$INSTALL_CODE_CLAUDE$INSTALL_CODE_OPENCODE$INSTALL_CODE_CODEX$INSTALL_CODE_VIBE" == *1* ]]; then |
| 112 | INSTALL_CODE_SERVER=1 |
| 113 | fi |
| 114 | |
| 115 | INSTALL_MCP_CHROME="${AGENT_VM_INSTALL_MCP_CHROME:-1}" |
| 116 | INSTALL_MCP_PLAYWRIGHT="${AGENT_VM_INSTALL_MCP_PLAYWRIGHT:-0}" |
| 117 | |
| 118 | |
| 119 | |
| 120 | HAS_CLAUDE=0 HAS_OPENCODE=0 HAS_CODEX=0 HAS_VIBE=0 |
| 121 | [[ "$INSTALL_CLAUDE$INSTALL_CODE_CLAUDE" == *1* ]] && HAS_CLAUDE=1 |
| 122 | [[ "$INSTALL_OPENCODE$INSTALL_CODE_OPENCODE" == *1* ]] && HAS_OPENCODE=1 |
| 123 | [[ "$INSTALL_CODEX$INSTALL_CODE_CODEX" == *1* ]] && HAS_CODEX=1 |
| 124 | [[ "$INSTALL_VIBE$INSTALL_CODE_VIBE" == *1* ]] && HAS_VIBE=1 |
| 125 | |
| 126 | |
| 127 | |
| 128 | export PATH="$HOME/.local/bin:$PATH" |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | printf 'Defaults env_keep += "http_proxy https_proxy ftp_proxy no_proxy HTTP_PROXY HTTPS_PROXY FTP_PROXY NO_PROXY"\n' \ |
| 137 | > /tmp/agent-vm-proxy.sudoers |
| 138 | sudo visudo -cqf /tmp/agent-vm-proxy.sudoers |
| 139 | sudo install -m 0440 -o root -g root /tmp/agent-vm-proxy.sudoers /etc/sudoers.d/10-agent-vm-proxy |
| 140 | rm -f /tmp/agent-vm-proxy.sudoers |
| 141 | |
| 142 | |
| 143 | sudo mkdir -p /etc/needrestart/conf.d |
| 144 | echo '$nrconf{restart} = '"'"'a'"'"';' | sudo tee /etc/needrestart/conf.d/no-prompt.conf > /dev/null |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | echo "Installing base packages..." |
| 157 | if [[ "${CLAUDE_CAGE:-}" == 1 ]]; then |
| 158 | |
| 159 | package_host=$(. /etc/os-release; if [ "$ID" = ubuntu ]; then printf '%s' archive.ubuntu.com; else printf '%s' deb.debian.org; fi) |
| 160 | echo "Checking guest DNS for system packages..." |
| 161 | if ! timeout 15 getent ahostsv4 mirrors.nju.edu.cn >/dev/null && |
| 162 | ! timeout 15 getent ahostsv4 mirrors.ustc.edu.cn >/dev/null && |
| 163 | ! timeout 15 getent ahostsv4 "$package_host" >/dev/null; then |
| 164 | echo 'Guest DNS could not resolve the system download sources. Check Cage Network & Proxy / DNS settings, apply and retry.' >&2 |
| 165 | exit 1 |
| 166 | fi |
| 167 | fi |
| 168 | apt_get update |
| 169 | apt_get install -y --no-install-recommends \ |
| 170 | git curl jq zsh \ |
| 171 | wget build-essential pkgconf patch \ |
| 172 | ripgrep fd-find htop \ |
| 173 | unzip zip \ |
| 174 | ca-certificates sshfs \ |
| 175 | libssl-dev libreadline-dev zlib1g-dev libyaml-dev libffi-dev |
| 176 | |
| 177 | |
| 178 | |
| 179 | if [[ "${CLAUDE_CAGE:-}" == 1 ]]; then |
| 180 | apt_get install -y --no-install-recommends python3 |
| 181 | fi |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | sudo tee /usr/local/bin/sshfs > /dev/null <<'EOF' |
| 190 | |
| 191 | |
| 192 | if /usr/bin/sshfs -h 2>&1 | grep -q no_contain_symlinks; then |
| 193 | exec /usr/bin/sshfs "$@" -o no_contain_symlinks |
| 194 | fi |
| 195 | exec /usr/bin/sshfs "$@" |
| 196 | EOF |
| 197 | sudo chmod 755 /usr/local/bin/sshfs |
| 198 | |
| 199 | if [[ "$INSTALL_PYTHON" == "1" ]]; then |
| 200 | echo "Installing Python 3..." |
| 201 | |
| 202 | apt_get install -y --no-install-recommends python3 python3-pip python3-venv python3-dev |
| 203 | fi |
| 204 | |
| 205 | if [[ "$INSTALL_RUBY" == "1" ]]; then |
| 206 | echo "Installing Ruby..." |
| 207 | apt_get install -y --no-install-recommends ruby-full |
| 208 | fi |
| 209 | |
| 210 | if [[ "$INSTALL_GOLANG" == "1" ]]; then |
| 211 | echo "Installing Go..." |
| 212 | apt_get install -y --no-install-recommends golang-go |
| 213 | fi |
| 214 | |
| 215 | if [[ "$INSTALL_RUST" == "1" ]]; then |
| 216 | |
| 217 | |
| 218 | echo "Installing Rust..." |
| 219 | curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path --default-toolchain stable |
| 220 | echo 'export PATH=$HOME/.cargo/bin:$PATH' >> ~/.zshenv |
| 221 | fi |
| 222 | |
| 223 | |
| 224 | sudo chsh -s /usr/bin/zsh "$(whoami)" |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | echo 'export PS1="vm:%1~%% "' >> ~/.zshrc |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | cat >> ~/.zshrc <<'ZSHRC' |
| 237 | HISTFILE=~/.zsh_history |
| 238 | HISTSIZE=10000 |
| 239 | SAVEHIST=10000 |
| 240 | setopt INC_APPEND_HISTORY HIST_IGNORE_DUPS HIST_IGNORE_SPACE |
| 241 | ZSHRC |
| 242 | echo 'export PATH=$HOME/.local/bin:$PATH' >> ~/.zshenv |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | echo '[ -f "$HOME/.agent-vm.env" ] && { set -a; . "$HOME/.agent-vm.env"; set +a; }' >> ~/.zshenv |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | echo "Installing mise..." |
| 254 | if [[ "${CLAUDE_CAGE:-}" == "1" ]]; then |
| 255 | cage_install_tool mise |
| 256 | else |
| 257 | curl -fsSL https://mise.run | sh |
| 258 | fi |
| 259 | echo 'eval "$(~/.local/bin/mise activate zsh)"' >> ~/.zshenv |
| 260 | |
| 261 | if [[ "$INSTALL_DOCKER" == "1" ]]; then |
| 262 | |
| 263 | echo "Installing Docker..." |
| 264 | sudo install -m 0755 -d /etc/apt/keyrings |
| 265 | docker_distribution=$(. /etc/os-release && printf '%s' "$ID") |
| 266 | case "$docker_distribution" in debian|ubuntu) ;; *) echo "Unsupported Docker distribution: $docker_distribution" >&2; exit 1;; esac |
| 267 | install_repository_key "https://download.docker.com/linux/$docker_distribution/gpg" /etc/apt/keyrings/docker.asc |
| 268 | sudo chmod a+r /etc/apt/keyrings/docker.asc |
| 269 | echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/$docker_distribution $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null |
| 270 | apt_get update |
| 271 | |
| 272 | |
| 273 | |
| 274 | apt_get install -y --no-install-recommends docker-ce docker-ce-cli containerd.io \ |
| 275 | docker-compose-plugin docker-buildx-plugin pigz |
| 276 | sudo usermod -aG docker "$(whoami)" |
| 277 | fi |
| 278 | |
| 279 | if [[ "$INSTALL_NODE" == "1" ]]; then |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | echo "Installing Node.js 24..." |
| 286 | sudo install -m 0755 -d /etc/apt/keyrings |
| 287 | install_repository_key https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key /etc/apt/keyrings/nodesource.asc |
| 288 | sudo chmod a+r /etc/apt/keyrings/nodesource.asc |
| 289 | echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/nodesource.asc] https://deb.nodesource.com/node_24.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list > /dev/null |
| 290 | printf 'Package: nodejs\nPin: origin deb.nodesource.com\nPin-Priority: 600\n' | sudo tee /etc/apt/preferences.d/nodejs > /dev/null |
| 291 | apt_get update |
| 292 | apt_get install -y --no-install-recommends nodejs |
| 293 | fi |
| 294 | |
| 295 | if [[ "$INSTALL_CHROMIUM" == "1" ]]; then |
| 296 | |
| 297 | echo "Installing Chromium..." |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | if [[ "${CLAUDE_CAGE:-}" == 1 ]] && cage_is_ubuntu; then |
| 304 | cage_install_chromium |
| 305 | else |
| 306 | chromium_packages=(chromium chromium-l10n fonts-liberation fonts-dejavu-core xvfb xauth) |
| 307 | if [[ "${CLAUDE_CAGE:-}" == 1 ]]; then |
| 308 | cage_cache_chromium --no-install-recommends "${chromium_packages[@]}" |
| 309 | fi |
| 310 | apt_get install -y --no-install-recommends "${chromium_packages[@]}" |
| 311 | fi |
| 312 | sudo ln -sf /usr/bin/chromium /usr/bin/google-chrome |
| 313 | sudo ln -sf /usr/bin/chromium /usr/bin/google-chrome-stable |
| 314 | sudo mkdir -p /opt/google/chrome |
| 315 | sudo ln -sf /usr/bin/chromium /opt/google/chrome/chrome |
| 316 | fi |
| 317 | |
| 318 | if [[ "$INSTALL_GH" == "1" ]]; then |
| 319 | |
| 320 | echo "Installing GitHub CLI..." |
| 321 | sudo mkdir -p -m 755 /etc/apt/keyrings |
| 322 | install_repository_key https://cli.github.com/packages/githubcli-archive-keyring.gpg /etc/apt/keyrings/githubcli-archive-keyring.gpg |
| 323 | sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg |
| 324 | echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null |
| 325 | apt_get update |
| 326 | apt_get install -y --no-install-recommends gh |
| 327 | fi |
| 328 | |
| 329 | if [[ "$INSTALL_CLAUDE" == "1" ]]; then |
| 330 | echo "Installing Claude Code..." |
| 331 | if [[ "${CLAUDE_CAGE:-}" == "1" ]]; then |
| 332 | |
| 333 | |
| 334 | cage_install_tool claude |
| 335 | else |
| 336 | curl -fsSL https://claude.ai/install.sh | bash |
| 337 | fi |
| 338 | echo 'export PATH=$HOME/.claude/local/bin:$PATH' >> ~/.zshenv |
| 339 | fi |
| 340 | |
| 341 | if [[ "$HAS_CLAUDE" == "1" ]]; then |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | echo "Configuring Claude managed settings (bypass permissions + fullscreen)..." |
| 355 | sudo mkdir -p /etc/claude-code |
| 356 | cat << 'JSON' | sudo tee /etc/claude-code/managed-settings.json > /dev/null |
| 357 | { |
| 358 | "permissions": { "defaultMode": "bypassPermissions" }, |
| 359 | "tui": "fullscreen", |
| 360 | "channelsEnabled": true |
| 361 | } |
| 362 | JSON |
| 363 | fi |
| 364 | |
| 365 | if [[ "$INSTALL_OPENCODE" == "1" || "$INSTALL_CODE_OPENCODE" == "1" ]]; then |
| 366 | echo "Installing OpenCode..." |
| 367 | curl -fsSL https://opencode.ai/install | bash |
| 368 | echo 'export PATH=$HOME/.opencode/bin:$PATH' >> ~/.zshenv |
| 369 | fi |
| 370 | |
| 371 | if [[ "$INSTALL_CODEX" == "1" ]]; then |
| 372 | if [[ "$INSTALL_NODE" != "1" ]]; then |
| 373 | echo "Skipping Codex CLI: requires Node.js (re-run setup with Node.js enabled)." >&2 |
| 374 | else |
| 375 | echo "Installing Codex CLI..." |
| 376 | sudo npm i -g @openai/codex </dev/null |
| 377 | fi |
| 378 | fi |
| 379 | |
| 380 | if [[ "$INSTALL_VIBE" == "1" ]]; then |
| 381 | |
| 382 | |
| 383 | |
| 384 | echo "Installing Mistral Vibe..." |
| 385 | curl -LsSf https://mistral.ai/vibe/install.sh | bash |
| 386 | fi |
| 387 | |
| 388 | if [[ "$INSTALL_PI" == "1" ]]; then |
| 389 | if [[ "$INSTALL_NODE" != "1" ]]; then |
| 390 | echo "Skipping Pi: requires Node.js (re-run setup with Node.js enabled)." >&2 |
| 391 | else |
| 392 | |
| 393 | |
| 394 | echo "Installing Pi..." |
| 395 | sudo npm i -g --ignore-scripts @earendil-works/pi-coding-agent </dev/null |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | mkdir -p "$HOME/.pi/agent" |
| 401 | cat > "$HOME/.pi/agent/settings.json" << 'JSON' |
| 402 | { |
| 403 | "defaultProjectTrust": "always", |
| 404 | "enableInstallTelemetry": false |
| 405 | } |
| 406 | JSON |
| 407 | fi |
| 408 | fi |
| 409 | |
| 410 | |
| 411 | |
| 412 | toml_prepend() { |
| 413 | local file="$1" key="$2" value="$3" |
| 414 | mkdir -p "$(dirname "$file")" |
| 415 | if [[ -f "$file" ]] && grep -q "^$key *=" "$file"; then |
| 416 | return 0 |
| 417 | fi |
| 418 | { printf '%s = %s\n' "$key" "$value"; [[ ! -f "$file" ]] || cat "$file"; } > "$file.tmp" |
| 419 | mv "$file.tmp" "$file" |
| 420 | } |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | schema_url_resolve() { |
| 427 | local base="${1%%#*}" ref="${2%%#*}" scheme rest host path seg |
| 428 | local -a segs=() kept=() |
| 429 | case "$ref" in |
| 430 | http://*|https://*) base="$ref" ref="" ;; |
| 431 | *://*) return 1 ;; |
| 432 | esac |
| 433 | scheme="${base%%://*}" |
| 434 | rest="${base#*://}" |
| 435 | host="${rest%%/*}" |
| 436 | path=/ |
| 437 | [[ "$rest" != */* ]] || path="/${rest#*/}" |
| 438 | case "$ref" in |
| 439 | "") ;; |
| 440 | //*) |
| 441 | host="${ref#//}" |
| 442 | path=/ |
| 443 | if [[ "$host" == */* ]]; then path="/${host#*/}"; host="${host%%/*}"; fi ;; |
| 444 | /*) path="$ref" ;; |
| 445 | *) path="${path%/*}/$ref" ;; |
| 446 | esac |
| 447 | IFS=/ read -ra segs <<< "$path" |
| 448 | for seg in ${segs[@]+"${segs[@]}"}; do |
| 449 | case "$seg" in |
| 450 | ""|.) ;; |
| 451 | ..) [[ ${#kept[@]} -eq 0 ]] || unset 'kept[-1]' ;; |
| 452 | *) kept+=("$seg") ;; |
| 453 | esac |
| 454 | done |
| 455 | local IFS=/ |
| 456 | printf '%s://%s/%s\n' "$scheme" "$host" "${kept[*]}" |
| 457 | } |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | code_server_schemas() { |
| 474 | local ext_dir="$1" out="$2" dir u f r base i n=0 |
| 475 | local -a level next fetch |
| 476 | local -A seen urls |
| 477 | dir="$(mktemp -d)" |
| 478 | : > "$dir/entries" |
| 479 | mapfile -t level < <(jq -r '.contributes.jsonValidation[]?.url | select(test("^https?://"))' \ |
| 480 | "$ext_dir"/*/package.json | sort -u) |
| 481 | while [[ ${#level[@]} -gt 0 ]]; do |
| 482 | fetch=() urls=() next=() |
| 483 | for u in "${level[@]}"; do |
| 484 | u="${u%%#*}" |
| 485 | [[ -z "${seen[$u]:-}" ]] || continue |
| 486 | seen[$u]=1 |
| 487 | n=$((n + 1)) |
| 488 | urls[$n]="$u" |
| 489 | |
| 490 | |
| 491 | fetch+=(-o "$dir/$n.json" "${u/#https:\/\/json.schemastore.org\//https://www.schemastore.org/}") |
| 492 | done |
| 493 | |
| 494 | [[ ${#fetch[@]} -eq 0 ]] || curl -fsSL --max-time 30 --parallel --parallel-max 8 "${fetch[@]}" </dev/null || true |
| 495 | for i in "${!urls[@]}"; do |
| 496 | u="${urls[$i]}" |
| 497 | f="$dir/$i.json" |
| 498 | if ! jq -c --arg u "$u" '{url: $u, schema: .}' "$f" > "$f.entry" 2>/dev/null; then |
| 499 | echo "Warning: could not download the JSON schema $u: files using it are not validated." >&2 |
| 500 | continue |
| 501 | fi |
| 502 | cat "$f.entry" >> "$dir/entries" |
| 503 | |
| 504 | base="$(jq -r 'objects | (."$id" // .id // empty) | strings | select(test("^https?://"))' "$f")" |
| 505 | [[ -n "$base" ]] || base="$u" |
| 506 | while IFS= read -r r; do |
| 507 | r="$(schema_url_resolve "$base" "$r")" && next+=("$r") |
| 508 | done < <(jq -r '[.. | objects | ."$ref"? | strings | select(startswith("#") | not)] | unique[]' "$f") |
| 509 | done |
| 510 | level=(${next[@]+"${next[@]}"}) |
| 511 | done |
| 512 | mkdir -p "$(dirname "$out")" |
| 513 | jq -sc '{"json.schemas": .}' "$dir/entries" > "$out" |
| 514 | rm -rf "$dir" |
| 515 | } |
| 516 | |
| 517 | if [[ "$INSTALL_CODE_SERVER" == "1" ]]; then |
| 518 | |
| 519 | |
| 520 | echo "Installing code-server..." |
| 521 | if [[ "${CLAUDE_CAGE:-}" == "1" ]]; then |
| 522 | cage_install_tool code-server |
| 523 | else |
| 524 | curl -fsSL https://code-server.dev/install.sh | sh |
| 525 | fi |
| 526 | extensions=() |
| 527 | [[ "$INSTALL_CODE_CLAUDE" == "1" ]] && extensions+=(--install-extension anthropic.claude-code) |
| 528 | [[ "$INSTALL_CODE_OPENCODE" == "1" ]] && extensions+=(--install-extension sst-dev.opencode) |
| 529 | [[ "$INSTALL_CODE_CODEX" == "1" ]] && extensions+=(--install-extension openai.chatgpt) |
| 530 | [[ "$INSTALL_CODE_VIBE" == "1" ]] && extensions+=(--install-extension mistralai.mistral-vibe-code) |
| 531 | if [[ ${#extensions[@]} -gt 0 ]]; then |
| 532 | echo "Installing the agents' extensions..." |
| 533 | code-server "${extensions[@]}" </dev/null |
| 534 | fi |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | mkdir -p "$HOME/.local/share/code-server/User" |
| 546 | jq -n --arg claude "$INSTALL_CODE_CLAUDE" ' |
| 547 | { |
| 548 | "workbench.colorTheme": "Dark 2026", |
| 549 | "chat.disableAIFeatures": true, |
| 550 | "chat.mcp.gallery.enabled": false, |
| 551 | "telemetry.telemetryLevel": "off", |
| 552 | "telemetry.feedback.enabled": false, |
| 553 | "json.schemaDownload.enable": false, |
| 554 | "workbench.enableExperiments": false, |
| 555 | "workbench.settings.enableNaturalLanguageSearch": false, |
| 556 | "workbench.settings.showAISearchToggle": false, |
| 557 | "extensions.ignoreRecommendations": true, |
| 558 | "workbench.startupEditor": "none", |
| 559 | "workbench.tips.enabled": false, |
| 560 | "workbench.welcomePage.walkthroughs.openOnInstall": false, |
| 561 | "workbench.secondarySideBar.defaultVisibility": "hidden", |
| 562 | "remote.autoForwardPorts": false, |
| 563 | "update.mode": "none", |
| 564 | "update.showReleaseNotes": false |
| 565 | } + if $claude == "1" then { |
| 566 | "claudeCode.hideOnboarding": true |
| 567 | } else {} end' > "$HOME/.local/share/code-server/User/settings.json" |
| 568 | |
| 569 | |
| 570 | cs_root=/usr/lib/code-server |
| 571 | if [[ ! -d "$cs_root/lib/vscode/extensions" ]]; then |
| 572 | cs_root="$(dirname "$(dirname "$(readlink -f "$(command -v code-server)")")")" |
| 573 | fi |
| 574 | echo "Downloading the JSON schemas the editor uses..." |
| 575 | cs_machine="$HOME/.local/share/code-server/Machine/settings.json" |
| 576 | code_server_schemas "$cs_root/lib/vscode/extensions" "$cs_machine" |
| 577 | |
| 578 | |
| 579 | |
| 580 | if [[ "$INSTALL_CODE_CLAUDE" == "1" ]]; then |
| 581 | jq -c '. + { |
| 582 | "claudeCode.allowDangerouslySkipPermissions": true, |
| 583 | "claudeCode.initialPermissionMode": "bypassPermissions" |
| 584 | }' "$cs_machine" > "$cs_machine.tmp" |
| 585 | mv "$cs_machine.tmp" "$cs_machine" |
| 586 | fi |
| 587 | |
| 588 | |
| 589 | |
| 590 | if [[ "$INSTALL_CODE_CODEX" == "1" ]]; then |
| 591 | toml_prepend "$HOME/.codex/config.toml" sandbox_mode '"danger-full-access"' |
| 592 | toml_prepend "$HOME/.codex/config.toml" approval_policy '"never"' |
| 593 | fi |
| 594 | if [[ "$INSTALL_CODE_VIBE" == "1" ]]; then |
| 595 | toml_prepend "$HOME/.vibe/config.toml" default_agent '"auto-approve"' |
| 596 | fi |
| 597 | |
| 598 | |
| 599 | if [[ "$INSTALL_CODE_OPENCODE" == "1" ]]; then |
| 600 | mkdir -p "$HOME/.config/opencode" |
| 601 | oc_config="$HOME/.config/opencode/opencode.json" |
| 602 | [ -f "$oc_config" ] || echo '{"$schema": "https://opencode.ai/config.json"}' > "$oc_config" |
| 603 | jq '.permission = "allow"' "$oc_config" > "$oc_config.tmp" && mv "$oc_config.tmp" "$oc_config" |
| 604 | fi |
| 605 | |
| 606 | rm -rf "$HOME/.config/code-server" |
| 607 | fi |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | configure_mcp() { |
| 623 | |
| 624 | |
| 625 | local name="$1" cmd="$2" |
| 626 | shift 2 |
| 627 | |
| 628 | |
| 629 | local args_json |
| 630 | args_json="$(printf '%s\n' "$@" | jq -R . | jq -sc .)" |
| 631 | |
| 632 | if [[ "$HAS_CLAUDE" == "1" ]]; then |
| 633 | echo "Configuring $name MCP server for Claude..." |
| 634 | local config="$HOME/.claude.json" |
| 635 | [ -f "$config" ] || echo '{}' > "$config" |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | if ! { jq --arg n "$name" --arg c "$cmd" --argjson a "$args_json" \ |
| 641 | '.mcpServers[$n] = {"command": $c, "args": $a}' \ |
| 642 | "$config" > "$config.tmp" && mv "$config.tmp" "$config"; }; then |
| 643 | rm -f "$config.tmp" |
| 644 | echo "Error: failed to write '$name' into $config" >&2 |
| 645 | return 1 |
| 646 | fi |
| 647 | fi |
| 648 | |
| 649 | if [[ "$HAS_OPENCODE" == "1" ]]; then |
| 650 | echo "Configuring $name MCP server for OpenCode..." |
| 651 | mkdir -p "$HOME/.config/opencode" |
| 652 | local config="$HOME/.config/opencode/opencode.json" |
| 653 | [ -f "$config" ] || echo '{"$schema": "https://opencode.ai/config.json"}' > "$config" |
| 654 | if ! { jq --arg n "$name" --arg c "$cmd" --argjson a "$args_json" \ |
| 655 | '.mcp[$n] = {"type": "local", "command": ([$c] + $a), "enabled": true}' \ |
| 656 | "$config" > "$config.tmp" && mv "$config.tmp" "$config"; }; then |
| 657 | rm -f "$config.tmp" |
| 658 | echo "Error: failed to write '$name' into $config" >&2 |
| 659 | return 1 |
| 660 | fi |
| 661 | fi |
| 662 | |
| 663 | if [[ "$HAS_VIBE" == "1" ]]; then |
| 664 | |
| 665 | |
| 666 | echo "Configuring $name MCP server for Vibe..." |
| 667 | mkdir -p "$HOME/.vibe" |
| 668 | local config="$HOME/.vibe/config.toml" |
| 669 | if ! grep -qF "name = \"$name\"" "$config" 2>/dev/null; then |
| 670 | { |
| 671 | printf '\n[[mcp_servers]]\n' |
| 672 | printf 'name = "%s"\n' "$name" |
| 673 | printf 'transport = "stdio"\n' |
| 674 | printf 'command = "%s"\n' "$cmd" |
| 675 | printf 'args = %s\n' "$args_json" |
| 676 | } >> "$config" |
| 677 | fi |
| 678 | fi |
| 679 | |
| 680 | if [[ "$HAS_CODEX" == "1" ]]; then |
| 681 | |
| 682 | |
| 683 | echo "Configuring $name MCP server for Codex..." |
| 684 | mkdir -p "$HOME/.codex" |
| 685 | local config="$HOME/.codex/config.toml" |
| 686 | if ! grep -qF "[mcp_servers.$name]" "$config" 2>/dev/null; then |
| 687 | { |
| 688 | printf '\n[mcp_servers.%s]\n' "$name" |
| 689 | printf 'command = "%s"\n' "$cmd" |
| 690 | printf 'args = %s\n' "$args_json" |
| 691 | } >> "$config" |
| 692 | fi |
| 693 | fi |
| 694 | } |
| 695 | |
| 696 | |
| 697 | |
| 698 | any_agent_installed() { |
| 699 | [[ "$HAS_CLAUDE" == "1" || "$HAS_OPENCODE" == "1" \ |
| 700 | || "$HAS_CODEX" == "1" || "$HAS_VIBE" == "1" ]] |
| 701 | } |
| 702 | |
| 703 | |
| 704 | |
| 705 | if [[ "$INSTALL_MCP_CHROME" == "1" ]] && any_agent_installed; then |
| 706 | if [[ "$INSTALL_NODE" == "1" && "$INSTALL_CHROMIUM" == "1" ]]; then |
| 707 | configure_mcp chrome-devtools npx -y chrome-devtools-mcp@latest --headless=true --isolated=true |
| 708 | else |
| 709 | echo "Skipping Chrome MCP config: requires Node.js and Chromium." >&2 |
| 710 | fi |
| 711 | fi |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | if [[ "$INSTALL_MCP_PLAYWRIGHT" == "1" ]] && any_agent_installed; then |
| 720 | if [[ "$INSTALL_NODE" == "1" && "$INSTALL_CHROMIUM" == "1" ]]; then |
| 721 | configure_mcp playwright env PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 \ |
| 722 | npx -y @playwright/mcp@latest --headless --isolated \ |
| 723 | --executable-path /usr/bin/chromium |
| 724 | else |
| 725 | echo "Skipping Playwright MCP config: requires Node.js and Chromium." >&2 |
| 726 | fi |
| 727 | fi |
| 728 | |
| 729 | echo "VM setup complete." |