File size: 6,229 Bytes
f072685
 
 
 
 
0e8d8be
 
 
 
f072685
 
324aa0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f072685
 
324aa0a
f072685
 
de215dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
324aa0a
de215dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd91ff6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de215dc
 
cd91ff6
de215dc
 
324aa0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b35ec3
 
 
 
 
 
 
324aa0a
 
 
 
 
9b35ec3
324aa0a
 
88c5bc9
cd91ff6
f072685
 
 
 
 
 
 
 
21b888e
f072685
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env bash
set -euo pipefail

export PROXY_PORT="${PROXY_PORT:-7860}"
export OPENCLAW_PORT="${OPENCLAW_PORT:-8080}"
export PORT="${OPENCLAW_PORT}"
export HOST="0.0.0.0"
export OPENCLAW_HOST="0.0.0.0"
export OPENCLAW_PORT
export SYNC_INTERVAL="${SYNC_INTERVAL:-300}"
export PLAYWRIGHT_BROWSERS_PATH="${PLAYWRIGHT_BROWSERS_PATH:-/ms-playwright}"
export TELEGRAM_API_ROOT="${TELEGRAM_API_ROOT:-https://tg-relay.markdevil11.workers.dev}"
export OPENCLAW_TELEGRAM_API_ROOT="${OPENCLAW_TELEGRAM_API_ROOT:-${TELEGRAM_API_ROOT}}"

configure_dns() {
  if [ -w /etc/resolv.conf ]; then
    cat > /etc/resolv.conf <<'EOF'
nameserver 1.1.1.1
nameserver 1.0.0.1
nameserver 8.8.8.8
nameserver 8.8.4.4
options timeout:2 attempts:3 rotate
EOF
    echo "DNS configured with Cloudflare and Google resolvers."
  else
    echo "WARN: /etc/resolv.conf is not writable; keeping existing DNS."
  fi
}

mkdir -p /root/workspace
configure_dns

/app/sync-root-data.sh restore
mkdir -p /root/.openclaw

generate_openclaw_config() {
  local clean_base
  clean_base="$(printf '%s' "${OPENAI_API_BASE:-}" | sed 's|/chat/completions||g' | sed 's|/v1/|/v1|g' | sed 's|/v1$|/v1|g')"
  local allow_id
  allow_id="${TELEGRAM_ALLOW_ID:-0}"

  cat > /root/.openclaw/openclaw.json <<JSONEOF
{
  "models": {
    "providers": {
      "kilo_gateway": {
        "baseUrl": "https://api.kilo.ai/api/gateway",
        "apiKey": "anonymous",
        "api": "openai-completions",
        "models": [
          { "id": "kilo-auto/free", "name": "Kilo Auto Free", "contextWindow": 128000, "maxTokens": 16000 }
        ]
      },
      "nvidia": {
        "baseUrl": "${clean_base}",
        "apiKey": "${OPENAI_API_KEY:-}",
        "api": "openai-completions",
        "models": [
          { "id": "${MODEL:-gpt-5.5}", "name": "${MODEL:-gpt-5.5}", "contextWindow": 128000 },
          { "id": "${VISION_MODEL:-${MODEL:-gpt-5.5}}", "name": "Nvidia Vision", "contextWindow": 128000, "input": ["text", "image"] }
        ]
      }
    }
  },
  "agents": {
    "defaults": {
      "model": {
        "primary": "nvidia/${MODEL:-gpt-5.5}",
        "fallbacks": ["kilo_gateway/kilo-auto/free"]
      },
      "imageModel": {
        "primary": "nvidia/${VISION_MODEL:-${MODEL:-gpt-5.5}}"
      }
    }
  },
  "commands": { "restart": true },
  "browser": {
    "enabled": true,
    "headless": true,
    "noSandbox": true,
    "defaultProfile": "openclaw",
    "ssrfPolicy": { "dangerouslyAllowPrivateNetwork": true },
    "profiles": {
      "openclaw": {
        "cdpPort": 18800,
        "color": "0088FF"
      }
    }
  },
  "channels": {
    "telegram": {
      "enabled": true,
      "botToken": "${TELEGRAM_BOT_TOKEN:-}",
      "dmPolicy": "allowlist",
      "allowFrom": [${allow_id}],
      "apiRoot": "${TELEGRAM_API_ROOT}",
      "webhookUrl": "https://elysiadev11-openclaw.hf.space/tg-webhook",
      "webhookSecret": "${OPENCLAW_GATEWAY_PASSWORD:-}",
      "webhookPath": "/tg-webhook",
      "webhookHost": "0.0.0.0",
      "webhookPort": 8787,
      "streaming": {
        "mode": "partial"
      }
    }
  },
  "gateway": {
    "mode": "local",
    "bind": "lan",
    "port": ${OPENCLAW_PORT},
    "trustedProxies": ["0.0.0.0/0"],
    "auth": { "mode": "token", "token": "${OPENCLAW_GATEWAY_PASSWORD:-}" },
    "http": {
      "endpoints": {
        "chatCompletions": { "enabled": true }
      }
    },
    "controlUi": {
      "enabled": true,
      "allowInsecureAuth": true,
      "dangerouslyDisableDeviceAuth": true,
      "dangerouslyAllowHostHeaderOriginFallback": true
    }
  }
}
JSONEOF
}

config_is_complete() {
  python3 - <<'PY'
import json
from pathlib import Path

path = Path('/root/.openclaw/openclaw.json')
if not path.exists():
    raise SystemExit(1)

try:
    data = json.loads(path.read_text())
except Exception:
    raise SystemExit(1)

required_paths = [
    ('models', 'providers'),
    ('models', 'providers', 'kilo_gateway'),
    ('models', 'providers', 'nvidia'),
    ('agents', 'defaults'),
    ('browser',),
    ('gateway',),
    ('gateway', 'controlUi'),
    ('channels', 'telegram'),
]

for parts in required_paths:
    cur = data
    for part in parts:
        if not isinstance(cur, dict) or part not in cur:
            raise SystemExit(1)
        cur = cur[part]

raise SystemExit(0)
PY
}

if ! config_is_complete; then
  echo "Generating full /root/.openclaw/openclaw.json before OpenClaw startup..."
  generate_openclaw_config
else
  echo "Complete /root/.openclaw/openclaw.json found; keeping it."
fi

python3 - <<'PY'
import json
import os
from pathlib import Path

path = Path('/root/.openclaw/openclaw.json')
api_root = os.environ.get('TELEGRAM_API_ROOT', 'https://tg-relay.markdevil11.workers.dev')
if not path.exists():
    raise SystemExit(0)

try:
    data = json.loads(path.read_text())
except Exception as exc:
    print(f'WARN: cannot update Telegram apiRoot in openclaw.json: {exc}')
    raise SystemExit(0)

channels = data.setdefault('channels', {})
telegram = channels.setdefault('telegram', {})
old = telegram.get('apiRoot')
browser = data.get('browser')
if isinstance(browser, dict):
    browser.pop('requirePairing', None)
agents = data.get('agents')
defaults = agents.get('defaults') if isinstance(agents, dict) else None
if isinstance(defaults, dict):
    defaults.pop('llm', None)
if old != api_root:
    telegram['apiRoot'] = api_root
    print(f'Updated Telegram apiRoot: {old!r} -> {api_root!r}')
else:
    print(f'Telegram apiRoot already set to {api_root}')
path.write_text(json.dumps(data, indent=2) + '\n')
PY

/app/sync-root-data.sh reconcile

/app/sync-root-data.sh loop &

run_openclaw() {
  while true; do
    echo "Starting OpenClaw on 127.0.0.1:${OPENCLAW_PORT}..."
    if [ -n "${OPENCLAW_CMD:-}" ]; then
      sh -lc "$OPENCLAW_CMD"
    else
      openclaw gateway --port "${OPENCLAW_PORT}" --allow-unconfigured
    fi
    echo "OpenClaw exited; restarting in 2 seconds..."
    sleep 2
  done
}

run_nginx() {
  while true; do
    envsubst '${PROXY_PORT} ${OPENCLAW_PORT}' < /app/nginx.conf.template > /tmp/nginx.conf
    nginx -c /tmp/nginx.conf -g 'daemon off;'
    echo "Nginx exited; restarting in 2 seconds..."
    sleep 2
  done
}

run_openclaw &
run_nginx &

wait -n
exit 1