#!/bin/bash set -e echo "=== Claude Code Real — Starting ===" echo "Model backend: ${MODEL:-z-ai/glm-5.1}" # ── Pre-configure Claude Code ── mkdir -p /home/user/.claude cat > /home/user/.claude/settings.json << 'JSON' { "permissions": { "allow": ["Bash(*)","Read(*)","Write(*)","Edit(*)","Computer(*)","WebFetch(*)","mcp__*"], "deny": [] }, "hasCompletedOnboarding": true, "hasAcknowledgedDisclaimer": true } JSON touch /home/user/.claude/.terms-accepted # ── Create .bashrc with Claude env for terminal users ── cat > /home/user/.bashrc << 'BASHRC' export ANTHROPIC_BASE_URL="http://localhost:4000" export ANTHROPIC_API_KEY="sk-ant-proxy-key-for-local-translation" export ANTHROPIC_MODEL="claude-sonnet-4-20250514" export DISABLE_PROMPT_CACHING="true" export CLAUDE_CODE_SKIP_OOBE="1" export TERM="xterm-256color" export PATH="/home/user/.npm-global/bin:/home/user/.local/bin:$PATH" echo "" echo "🤖 Claude Code Terminal — Ready!" echo " Model: ${MODEL:-z-ai/glm-5.1} via NVIDIA API" echo " Run: claude" echo " Or: claude -p 'your prompt' --dangerously-skip-permissions" echo "" cd /home/user/workspace BASHRC # ── Start Anthropic→OpenAI proxy on port 4000 ── echo "Starting proxy on :4000..." python3 /home/user/proxy.py & PROXY_PID=$! for i in $(seq 1 20); do if curl -s http://localhost:4000/health > /dev/null 2>&1; then echo "✅ Proxy ready!" break fi sleep 1 done curl -s http://localhost:4000/health | python3 -m json.tool 2>/dev/null || true echo "Claude Code: $(claude --version 2>/dev/null || echo 'checking...')" # ── Quick test (don't block startup) ── echo "Testing Claude Code (background)..." export ANTHROPIC_BASE_URL="http://localhost:4000" export ANTHROPIC_API_KEY="sk-ant-proxy-key-for-local-translation" export ANTHROPIC_MODEL="claude-sonnet-4-20250514" export DISABLE_PROMPT_CACHING="true" export CLAUDE_CODE_SKIP_OOBE="1" timeout 30 claude -p "Say hi" --dangerously-skip-permissions --output-format text 2>&1 | head -5 & # ── Start ttyd web terminal on port 7862 (password protected) ── echo "Starting web terminal on :7862..." TERM_PASSWORD="${TERMINAL_PASSWORD:-2004}" ttyd -p 7862 -c "ayoub:${TERM_PASSWORD}" \ -t fontSize=14 \ -t 'theme={"background":"#1a1b26","foreground":"#c0caf5"}' \ bash --login & TTYD_PID=$! echo " ttyd PID: $TTYD_PID" # ── Start nginx on port 7860 ── echo "Starting nginx on :7860..." nginx -c /home/user/nginx.conf -g 'daemon off;' & NGINX_PID=$! sleep 1 # Check if nginx started OK if kill -0 $NGINX_PID 2>/dev/null; then echo " ✅ nginx PID: $NGINX_PID" else echo " ⚠️ nginx failed! Falling back to Flask on :7860" NGINX_PID="" # If nginx fails, start Flask directly on 7860 export FLASK_PORT=7860 fi # ── Start Flask + Telegram bot ── FLASK_PORT=${FLASK_PORT:-7861} echo "Starting Flask+Bot on :${FLASK_PORT}..." FLASK_PORT=$FLASK_PORT python3 /home/user/app.py & APP_PID=$! echo "" echo "=== All services started ===" echo " Proxy: PID $PROXY_PID (:4000)" echo " App: PID $APP_PID (:$FLASK_PORT)" echo " Terminal: PID $TTYD_PID (:7862)" [ -n "$NGINX_PID" ] && echo " Nginx: PID $NGINX_PID (:7860)" echo "" echo " 🌐 Web Terminal: https://ayoub5550-claudecode.hf.space/terminal/" echo " 💚 Health: https://ayoub5550-claudecode.hf.space/health" echo "" # ── Keep alive: only die if the main app (bot) dies ── # Don't use wait -n, only wait for the bot process while true; do if ! kill -0 $APP_PID 2>/dev/null; then echo "❌ App process died! Restarting..." FLASK_PORT=$FLASK_PORT python3 /home/user/app.py & APP_PID=$! sleep 5 fi if ! kill -0 $PROXY_PID 2>/dev/null; then echo "❌ Proxy died! Restarting..." python3 /home/user/proxy.py & PROXY_PID=$! sleep 5 fi sleep 10 done