m64_7796c3cb05e5 / start.sh
48 lines, 2.4 KBraw
1#!/usr/bin/env bash2# opencode-tmux-dsflash-openrouter-gb10 entrypoint. Runs inside the container; takes no3# arguments. opencode in a tmux session, ttyd serving that session on 0.0.0.0:76814# ([runtime.app]: m64 exposes it on a second front port), and a health server on5# $M64_PORT. 0.0.0.0 is inside the container: only the gate can reach its network.6set -eu78echo '{"phase":"loading","detail":"starting opencode in tmux"}' >> "$M64_STATUS"910# HOME is the run's /scratch: opencode's DB, caches and the workspace go with the run.11export HOME=/scratch/home12export WORK_DIR=/scratch/work13mkdir -p "$HOME" "$WORK_DIR"14# Packages opencode installs on first start, warmed at build time (at run time only15# openrouter.ai is reachable).16cp -rn /opt/opencode-capsule/. "$HOME/"1718export OPENCODE_CONFIG=/capsule/opencode.json19# opencode is a Bun binary: Bun's fetch honours HTTP_PROXY / NO_PROXY (set by m64 to the20# gate). The provider's baseURL is plain http://openrouter.ai: the gate adds the key.21export TERM=xterm-256color COLORTERM=truecolor LANG=C.UTF-822cd "$WORK_DIR"2324# The tmux server keeps the session (and opencode) alive when the browser tab closes.25# Start it now, detached, so opencode is already up when the first tab connects.26TMUX_CMD="tmux -f /capsule/tmux.conf"27$TMUX_CMD new-session -d -s opencode -c "$WORK_DIR" -x 160 -y 45 opencode2829# Every ttyd client attaches to that session (-A), or creates it again if opencode was30# quit. -W: writable. No ttyd auth and no --check-origin: the app front refuses foreign31# Origins itself, and ttyd's check would also drop clients that send no Origin (AGENT.md).32ttyd --port 7681 --interface 0.0.0.0 --writable \33 -t titleFixed=opencode -t disableLeaveAlert=true -t fontSize=14 \34 $TMUX_CMD new-session -A -s opencode -c "$WORK_DIR" opencode &35TTYD_PID=$!3637# Ready once ttyd answers (a capsule that reports a phase must report ready itself).38for _ in $(seq 1 60); do curl -sf -o /dev/null http://127.0.0.1:7681/ && break; sleep 1; done39echo '{"phase":"ready","detail":"opencode is running in tmux"}' >> "$M64_STATUS"4041# If ttyd dies, take the container down so m64 reports the error. $$ is the health42# server after the exec below; a dead ttyd lingers as a zombie, so test its state.43alive() { [ -r "/proc/$1/stat" ] && [ "$(cut -d')' -f2 "/proc/$1/stat" | cut -d' ' -f2)" != Z ]; }44( while alive "$TTYD_PID"; do sleep 5; done45 echo '{"phase":"error","detail":"ttyd exited"}' >> "$M64_STATUS"46 kill -TERM $$ ) &4748exec node /capsule/health.js