File size: 5,330 Bytes
8907cf8
2ae65c2
8907cf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d28748
444f882
8907cf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee3c7e7
 
2f8fc36
 
 
 
 
ee3c7e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/sh
# Auto-restart trigger: 1773447074
set -e

BOOT_START=$(date +%s)

echo "[entrypoint] OpenClaw HuggingFace Spaces Entrypoint"
echo "[entrypoint] ======================================="

# ── DNS pre-resolution (background β€” non-blocking) ───────────────────────
# Resolves WhatsApp domains via DoH for dns-fix.cjs to consume.
# Telegram connectivity is handled by API base auto-probe in sync_hf.py.
echo "[entrypoint] Starting DNS resolution in background..."
python3 /home/node/scripts/dns-resolve.py /tmp/dns-resolved.json 2>&1 &
DNS_PID=$!
echo "[entrypoint] DNS resolver PID: $DNS_PID"

# ── Node.js memory limit (only if explicitly set) ─────────────────────────
if [ -n "$NODE_MEMORY_LIMIT" ]; then
  export NODE_OPTIONS="${NODE_OPTIONS:+$NODE_OPTIONS }--max-old-space-size=$NODE_MEMORY_LIMIT"
  echo "[entrypoint] Node.js memory limit: ${NODE_MEMORY_LIMIT}MB"
fi

# Enable Node.js DNS fix (will use resolved file when ready)
export NODE_OPTIONS="${NODE_OPTIONS:+$NODE_OPTIONS }--require /home/node/scripts/dns-fix.cjs"

# Enable Telegram API proxy (redirects fetch() to working mirror if needed)
export NODE_OPTIONS="${NODE_OPTIONS:+$NODE_OPTIONS }--require /home/node/scripts/telegram-proxy.cjs"

# Enable token redirect + A2A routing + state/agents endpoints (all in one preload)
# REMOVED: A2A gateway not installed (minimal mode)
# export NODE_OPTIONS="${NODE_OPTIONS:+$NODE_OPTIONS }--require /home/node/scripts/token-redirect.cjs"

# ── Extensions symlink ──────────────────────────────────────────────────────
SYMLINK_START=$(date +%s)
if [ ! -L /home/node/.openclaw/extensions ]; then
  rm -rf /home/node/.openclaw/extensions 2>/dev/null || true
  ln -s /app/openclaw/extensions /home/node/.openclaw/extensions
  echo "[entrypoint] Created extensions symlink -> /app/openclaw/extensions"
fi
echo "[TIMER] Extensions symlink: $(($(date +%s) - SYMLINK_START))s"

# ── WhatsApp credentials check ──────────────────────────────────────────────
if [ -d /home/node/.openclaw/credentials/whatsapp ]; then
  echo "[entrypoint] Found existing WhatsApp credentials - will use for auto-connect"
fi

# ── Build artifacts check ───────────────────────────────────────────────────
cd /app/openclaw
echo "[entrypoint] Build artifacts check:"
test -f dist/entry.js && echo "  OK dist/entry.js" || echo "  INFO: dist/entry.js not found (pre-built image may use openclaw.mjs)"
test -f openclaw.mjs && echo "  OK openclaw.mjs" || echo "  INFO: openclaw.mjs not found"
test -f dist/plugin-sdk/index.js && echo "  OK dist/plugin-sdk/index.js" || echo "  INFO: dist/plugin-sdk/index.js not found"
echo "  Extensions: $(ls extensions/ 2>/dev/null | wc -l | tr -d ' ') found"
echo "  Global extensions link: $(readlink /home/node/.openclaw/extensions 2>/dev/null || echo 'NOT SET')"

# Create logs directory
mkdir -p /home/node/logs
touch /home/node/logs/app.log

ENTRYPOINT_END=$(date +%s)
echo "[TIMER] Entrypoint (before sync_hf.py): $((ENTRYPOINT_END - BOOT_START))s"

# ── Set version from build artifact ────────────────────────────────────────
if [ -f /app/openclaw/.version ]; then
  export OPENCLAW_VERSION=$(cat /app/openclaw/.version)
  echo "[entrypoint] OpenClaw version: $OPENCLAW_VERSION"
fi

# ── Start FastAPI Backend (port 7860) and A2A Proxy (port 7861) ──────────────────
echo "[entrypoint] Starting FastAPI Backend on port 7860..."

# Set default environment variables if not already set
export PORT=${PORT:-7860}
export CAIN_API_URL="http://127.0.0.1:${PORT}"

# Start FastAPI backend in background
python3 -u /home/node/app.py &
FASTAPI_PID=$!
echo "[entrypoint] FastAPI PID: $FASTAPI_PID"

# Wait for FastAPI to be ready
echo "[entrypoint] Waiting for FastAPI to start..."
sleep 3

# ── Start A2A Proxy on port 7861 ───────────────────────────────────────────────────
echo "[entrypoint] Starting A2A proxy on port 7861..."
# Port mapping: a2a-proxy (7861) -> app.py (7860)
# A2A routes are proxied from 7861 to FastAPI's /a2a/* endpoints
export LISTEN_PORT=7861
export OPENCLAW_PORT=7860

# Start a2a-proxy in background
node /home/node/scripts/a2a-proxy.cjs &
A2A_PROXY_PID=$!
echo "[entrypoint] A2A Proxy PID: $A2A_PROXY_PID"

# Health check loop - keep both processes running
echo "[entrypoint] Both services started. Monitoring health..."
while true; do
  # Check FastAPI
  if ! kill -0 $FASTAPI_PID 2>/dev/null; then
    echo "[entrypoint] ERROR: FastAPI died, restarting..."
    python3 -u /home/node/app.py &
    FASTAPI_PID=$!
  fi
  # Check A2A Proxy
  if ! kill -0 $A2A_PROXY_PID 2>/dev/null; then
    echo "[entrypoint] ERROR: A2A Proxy died, restarting..."
    node /home/node/scripts/a2a-proxy.cjs &
    A2A_PROXY_PID=$!
  fi
  sleep 5
done