#!/usr/bin/env bash # Boot order: generate configs -> status page (7860, public) -> router (4000, internal) # -> hermes gateway (Telegram long-polling, outbound only) set -euo pipefail export PATH="/opt/venv/bin:/home/agent/.local/bin:/home/agent/.hermes/bin:$PATH" cd "$(dirname "$0")" echo "==> [1/5] Generating LiteLLM router config from env..." python3 gen_router_config.py echo "==> [2/5] Generating Hermes config from env..." python3 gen_hermes_config.py echo " --- generated ~/.hermes/config.yaml (env refs only, no real secrets) ---" sed -e 's/^/ | /' "${HERMES_HOME_OVERRIDE:-$HOME/.hermes}/config.yaml" || true echo " ---------------------------------------------------------------" echo "==> [3/5] Starting LiteLLM router on 127.0.0.1:4000 ..." # A stale DATABASE_URL (from other projects) makes LiteLLM attempt a Prisma DB # connect at boot and DIE. We run config-only, so drop it unless opted in. if [ "${LITELLM_USE_DB:-0}" != "1" ] && [ -n "${DATABASE_URL:-}" ]; then echo " (ignoring env DATABASE_URL — router runs without a database)" unset DATABASE_URL fi litellm --config "${LITELLM_CONFIG_OUT:-litellm.generated.yaml}" \ --host 127.0.0.1 --port 4000 > /tmp/litellm.log 2>&1 & echo " waiting for router health..." READY=0 for _ in $(seq 1 90); do if curl -sf http://127.0.0.1:4000/health/liveliness > /dev/null 2>&1; then READY=1; break fi sleep 2 done if [ "$READY" != "1" ]; then echo "!! router did not become healthy — last log lines:" >&2 tail -n 50 /tmp/litellm.log >&2 || true fi if [ -z "${TELEGRAM_BOT_TOKEN:-}" ]; then echo "!! WARNING: TELEGRAM_BOT_TOKEN not set (Space Settings -> Secrets)" >&2 fi # The gateway gets a supervisor: if it ever crashes (bad config, bad relay # URL, transient network...) we RESTART it instead of letting HF brick the # whole Space ("Your space is in error"). Health page + /logs.txt stay up # and keep telling us exactly what happened. echo "==> [4/5] Starting Hermes gateway supervisor (Telegram long-polling) ..." echo "=== hermes gateway log (auto-restarts on crash) ===" > /tmp/hermes.log ( set +e while true; do hermes gateway >> /tmp/hermes.log 2>&1 ec=$? echo "!! gateway exited (code $ec) — restart in 15s; see /logs.txt" \ | tee -a /tmp/hermes.log sleep 15 done ) & echo "==> [5/5] Starting status/health page on 0.0.0.0:7860 (foreground) ..." exec python3 health_server.py