File size: 3,930 Bytes
cef2fa1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/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