-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathserver.js
More file actions
1578 lines (1390 loc) · 58.2 KB
/
Copy pathserver.js
File metadata and controls
1578 lines (1390 loc) · 58.2 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import childProcess from "node:child_process";
import crypto from "node:crypto";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import express from "express";
import httpProxy from "http-proxy";
import * as tar from "tar";
// Migrate deprecated CLAWDBOT_* env vars → OPENCLAW_* so existing Railway deployments
// keep working. Users should update their Railway Variables to use the new names.
for (const suffix of ["PUBLIC_PORT", "STATE_DIR", "WORKSPACE_DIR", "GATEWAY_TOKEN", "CONFIG_PATH"]) {
const oldKey = `CLAWDBOT_${suffix}`;
const newKey = `OPENCLAW_${suffix}`;
if (process.env[oldKey] && !process.env[newKey]) {
process.env[newKey] = process.env[oldKey];
// Best-effort compatibility shim for old Railway templates.
// Intentionally no warning: Railway templates can still set legacy keys and warnings are noisy.
}
// Avoid forwarding legacy variables into OpenClaw subprocesses.
// OpenClaw logs a warning when deprecated CLAWDBOT_* variables are present.
delete process.env[oldKey];
}
// Railway injects PORT at runtime and routes traffic to that port.
// Do not force a different public port in the container image, or the service may
// boot but the Railway domain will be routed to a different port.
//
// OPENCLAW_PUBLIC_PORT is kept as an escape hatch for non-Railway deployments.
const PORT = Number.parseInt(process.env.PORT ?? process.env.OPENCLAW_PUBLIC_PORT ?? "3000", 10);
// State/workspace
// OpenClaw defaults to ~/.openclaw.
const STATE_DIR =
process.env.OPENCLAW_STATE_DIR?.trim() ||
path.join(os.homedir(), ".openclaw");
const WORKSPACE_DIR =
process.env.OPENCLAW_WORKSPACE_DIR?.trim() ||
path.join(STATE_DIR, "workspace");
// Protect /setup with a user-provided password.
const SETUP_PASSWORD = process.env.SETUP_PASSWORD?.trim();
// Gateway admin token (protects OpenClaw gateway + Control UI).
// Must be stable across restarts. If not provided via env, persist it in the state dir.
function resolveGatewayToken() {
const envTok = process.env.OPENCLAW_GATEWAY_TOKEN?.trim();
if (envTok) return envTok;
const tokenPath = path.join(STATE_DIR, "gateway.token");
try {
const existing = fs.readFileSync(tokenPath, "utf8").trim();
if (existing) return existing;
} catch {
// ignore
}
const generated = crypto.randomBytes(32).toString("hex");
try {
fs.mkdirSync(STATE_DIR, { recursive: true });
fs.writeFileSync(tokenPath, generated, { encoding: "utf8", mode: 0o600 });
} catch {
// best-effort
}
return generated;
}
const OPENCLAW_GATEWAY_TOKEN = resolveGatewayToken();
process.env.OPENCLAW_GATEWAY_TOKEN = OPENCLAW_GATEWAY_TOKEN;
// Where the gateway will listen internally (we proxy to it).
const INTERNAL_GATEWAY_PORT = Number.parseInt(process.env.INTERNAL_GATEWAY_PORT ?? "18789", 10);
const INTERNAL_GATEWAY_HOST = process.env.INTERNAL_GATEWAY_HOST ?? "127.0.0.1";
const GATEWAY_TARGET = `http://${INTERNAL_GATEWAY_HOST}:${INTERNAL_GATEWAY_PORT}`;
// Always run the built-from-source CLI entry directly to avoid PATH/global-install mismatches.
const OPENCLAW_ENTRY = process.env.OPENCLAW_ENTRY?.trim() || "/openclaw/dist/entry.js";
const OPENCLAW_NODE = process.env.OPENCLAW_NODE?.trim() || "node";
function clawArgs(args) {
return [OPENCLAW_ENTRY, ...args];
}
function resolveConfigCandidates() {
const explicit = process.env.OPENCLAW_CONFIG_PATH?.trim();
if (explicit) return [explicit];
return [path.join(STATE_DIR, "openclaw.json")];
}
function configPath() {
const candidates = resolveConfigCandidates();
for (const candidate of candidates) {
try {
if (fs.existsSync(candidate)) return candidate;
} catch {
// ignore
}
}
// Default to canonical even if it doesn't exist yet.
return candidates[0] || path.join(STATE_DIR, "openclaw.json");
}
function isConfigured() {
try {
return resolveConfigCandidates().some((candidate) => fs.existsSync(candidate));
} catch {
return false;
}
}
// One-time migration: rename legacy config files to openclaw.json so existing
// deployments that still have the old filename on their volume keep working.
(function migrateLegacyConfigFile() {
// If the operator explicitly chose a config path, do not rename files in STATE_DIR.
if (process.env.OPENCLAW_CONFIG_PATH?.trim()) return;
const canonical = path.join(STATE_DIR, "openclaw.json");
if (fs.existsSync(canonical)) return;
for (const legacy of ["clawdbot.json", "moltbot.json"]) {
const legacyPath = path.join(STATE_DIR, legacy);
try {
if (fs.existsSync(legacyPath)) {
fs.renameSync(legacyPath, canonical);
console.log(`[migration] Renamed ${legacy} → openclaw.json`);
return;
}
} catch (err) {
console.warn(`[migration] Failed to rename ${legacy}: ${err}`);
}
}
})();
let gatewayProc = null;
let gatewayStarting = null;
// Debug breadcrumbs for common Railway failures (502 / "Application failed to respond").
let lastGatewayError = null;
let lastGatewayExit = null;
let lastDoctorOutput = null;
let lastDoctorAt = null;
async function tryFreePort(port, { signal = 'SIGTERM', waitMs = 500 } = {}) {
// On container restart/restart cycles, a zombie openclaw process can keep
// port 18789 bound. The supervisor's own tracked handle is null (we're fresh)
// so childProcess.kill can't reach it. Shell out to lsof/fuser (installed in
// the Dockerfile runtime stage) to find the PID and send it a signal so the
// next spawn doesn't fail with EADDRINUSE.
const { execFile } = childProcess;
const tryCmd = (cmd, args) => new Promise((resolve) => {
execFile(cmd, args, { timeout: 3000 }, (err, stdout) => {
resolve({ err, stdout: (stdout || '').toString().trim() });
});
});
// Prefer lsof: returns one PID per line with -t.
let pids = [];
const lsof = await tryCmd('lsof', ['-t', `-i:${port}`, '-sTCP:LISTEN']);
if (!lsof.err && lsof.stdout) {
pids = lsof.stdout.split(/\s+/).filter(Boolean);
}
// Fall back to fuser if lsof is missing.
if (pids.length === 0) {
const fuser = await tryCmd('fuser', [`${port}/tcp`]);
if (!fuser.err && fuser.stdout) {
pids = fuser.stdout.split(/\s+/).filter(Boolean);
}
}
if (pids.length === 0) return { killed: [], reason: 'no listener' };
const killed = [];
for (const raw of pids) {
const pid = Number.parseInt(raw, 10);
if (!Number.isFinite(pid) || pid <= 1) continue;
try {
process.kill(pid, signal);
killed.push(pid);
} catch {
// already gone
}
}
if (killed.length > 0 && waitMs > 0) {
await new Promise((r) => setTimeout(r, waitMs));
}
return { killed, reason: killed.length ? `killed with ${signal}` : 'no pids killed' };
}
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
async function waitForGatewayReady(opts = {}) {
const timeoutMs = opts.timeoutMs ?? 20_000;
const start = Date.now();
while (Date.now() - start < timeoutMs) {
try {
// Try the default Control UI base path, then fall back to root.
const paths = ["/openclaw", "/"];
for (const p of paths) {
try {
const res = await fetch(`${GATEWAY_TARGET}${p}`, { method: "GET" });
// Any HTTP response means the port is open.
if (res) return true;
} catch {
// try next
}
}
} catch {
// not ready
}
await sleep(250);
}
return false;
}
async function startGateway() {
if (gatewayProc) return;
if (!isConfigured()) throw new Error("Gateway cannot start: not configured");
fs.mkdirSync(STATE_DIR, { recursive: true });
fs.mkdirSync(WORKSPACE_DIR, { recursive: true });
const args = [
"gateway",
"run",
"--bind",
"loopback",
"--port",
String(INTERNAL_GATEWAY_PORT),
"--auth",
"token",
"--token",
OPENCLAW_GATEWAY_TOKEN,
];
// Defend against zombie openclaw processes from a previous container life
// that never got reaped — their listener still holds INTERNAL_GATEWAY_PORT
// and the new spawn would fail with EADDRINUSE. lsof is installed in the
// runtime image for exactly this purpose.
try {
const free = await tryFreePort(INTERNAL_GATEWAY_PORT, { signal: 'SIGTERM' });
if (free.killed.length > 0) {
console.warn(`[gateway] freed port ${INTERNAL_GATEWAY_PORT} before spawn (pids=${free.killed.join(',')})`);
// If SIGTERM didn't stick, escalate.
const still = await tryFreePort(INTERNAL_GATEWAY_PORT, { signal: 'SIGKILL' });
if (still.killed.length > 0) {
console.warn(`[gateway] escalated to SIGKILL on port ${INTERNAL_GATEWAY_PORT} (pids=${still.killed.join(',')})`);
}
}
} catch (err) {
console.warn(`[gateway] tryFreePort failed: ${String(err)}`);
}
gatewayProc = childProcess.spawn(OPENCLAW_NODE, clawArgs(args), {
stdio: "inherit",
env: {
...process.env,
OPENCLAW_STATE_DIR: STATE_DIR,
OPENCLAW_WORKSPACE_DIR: WORKSPACE_DIR,
},
});
gatewayProc.on("error", (err) => {
const msg = `[gateway] spawn error: ${String(err)}`;
console.error(msg);
lastGatewayError = msg;
gatewayProc = null;
});
gatewayProc.on("exit", (code, signal) => {
const msg = `[gateway] exited code=${code} signal=${signal}`;
console.error(msg);
lastGatewayExit = { code, signal, at: new Date().toISOString() };
gatewayProc = null;
});
}
async function runDoctorBestEffort() {
// Avoid spamming `openclaw doctor` in a crash loop.
const now = Date.now();
if (lastDoctorAt && now - lastDoctorAt < 5 * 60 * 1000) return;
lastDoctorAt = now;
try {
const r = await runCmd(OPENCLAW_NODE, clawArgs(["doctor"]));
const out = redactSecrets(r.output || "");
lastDoctorOutput = out.length > 50_000 ? out.slice(0, 50_000) + "\n... (truncated)\n" : out;
} catch (err) {
lastDoctorOutput = `doctor failed: ${String(err)}`;
}
}
async function ensureGatewayRunning() {
if (!isConfigured()) return { ok: false, reason: "not configured" };
if (gatewayProc) return { ok: true };
if (!gatewayStarting) {
gatewayStarting = (async () => {
try {
lastGatewayError = null;
await startGateway();
const ready = await waitForGatewayReady({ timeoutMs: 20_000 });
if (!ready) {
throw new Error("Gateway did not become ready in time");
}
} catch (err) {
const msg = `[gateway] start failure: ${String(err)}`;
lastGatewayError = msg;
// Collect extra diagnostics to help users file issues.
await runDoctorBestEffort();
throw err;
}
})().finally(() => {
gatewayStarting = null;
});
}
await gatewayStarting;
return { ok: true };
}
async function restartGateway() {
if (gatewayProc) {
try {
gatewayProc.kill("SIGTERM");
} catch {
// ignore
}
// Give it a moment to exit and release the port.
await sleep(750);
gatewayProc = null;
}
// Always sweep port 18789 before restart — a stuck child could have escaped
// SIGTERM, and a previous container life may have left a zombie.
try {
const free = await tryFreePort(INTERNAL_GATEWAY_PORT, { signal: 'SIGKILL' });
if (free.killed.length > 0) {
console.warn(`[gateway] restart killed lingering pids on ${INTERNAL_GATEWAY_PORT}: ${free.killed.join(',')}`);
}
} catch {
// best-effort
}
return ensureGatewayRunning();
}
function requireSetupAuth(req, res, next) {
if (!SETUP_PASSWORD) {
return res
.status(500)
.type("text/plain")
.send("SETUP_PASSWORD is not set. Set it in Railway Variables before using /setup.");
}
const header = req.headers.authorization || "";
const [scheme, encoded] = header.split(" ");
if (scheme !== "Basic" || !encoded) {
res.set("WWW-Authenticate", 'Basic realm="OpenClaw Setup"');
return res.status(401).send("Auth required");
}
const decoded = Buffer.from(encoded, "base64").toString("utf8");
const idx = decoded.indexOf(":");
const password = idx >= 0 ? decoded.slice(idx + 1) : "";
if (password !== SETUP_PASSWORD) {
res.set("WWW-Authenticate", 'Basic realm="OpenClaw Setup"');
return res.status(401).send("Invalid password");
}
return next();
}
const app = express();
app.disable("x-powered-by");
app.use(express.json({ limit: "1mb" }));
// Minimal health endpoint for Railway.
app.get("/setup/healthz", (_req, res) => res.json({ ok: true }));
async function probeGateway() {
// Don't assume HTTP — the gateway primarily speaks WebSocket.
// A simple TCP connect check is enough for "is it up".
const net = await import("node:net");
return await new Promise((resolve) => {
const sock = net.createConnection({
host: INTERNAL_GATEWAY_HOST,
port: INTERNAL_GATEWAY_PORT,
timeout: 750,
});
const done = (ok) => {
try { sock.destroy(); } catch {}
resolve(ok);
};
sock.on("connect", () => done(true));
sock.on("timeout", () => done(false));
sock.on("error", () => done(false));
});
}
// Public health endpoint (no auth) so Railway can probe without /setup.
// Keep this free of secrets.
app.get("/healthz", async (_req, res) => {
let gatewayReachable = false;
const configured = isConfigured();
if (configured) {
try {
gatewayReachable = await probeGateway();
} catch {
gatewayReachable = false;
}
}
// Fail the healthcheck when the gateway is supposed to be running but the
// internal port is dead. Railway's healthcheck (restartPolicyType=on_failure)
// will restart the container, which clears zombie listeners.
const ok = !configured || gatewayReachable;
const body = {
ok,
wrapper: {
configured,
stateDir: STATE_DIR,
workspaceDir: WORKSPACE_DIR,
},
gateway: {
target: GATEWAY_TARGET,
reachable: gatewayReachable,
lastError: lastGatewayError,
lastExit: lastGatewayExit,
lastDoctorAt,
},
};
res.status(ok ? 200 : 503).json(body);
});
app.get("/setup/app.js", requireSetupAuth, (_req, res) => {
// Serve JS for /setup (kept external to avoid inline encoding/template issues)
res.type("application/javascript");
res.send(fs.readFileSync(path.join(process.cwd(), "src", "setup-app.js"), "utf8"));
});
app.get("/setup", requireSetupAuth, (_req, res) => {
// No inline <script>: serve JS from /setup/app.js to avoid any encoding/template-literal issues.
res.type("html").send(`<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>OpenClaw Setup</title>
<style>
body { font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial; margin: 2rem; max-width: 900px; }
.card { border: 1px solid #ddd; border-radius: 12px; padding: 1.25rem; margin: 1rem 0; }
label { display:block; margin-top: 0.75rem; font-weight: 600; }
input, select { width: 100%; padding: 0.6rem; margin-top: 0.25rem; }
button { padding: 0.8rem 1.2rem; border-radius: 10px; border: 0; background: #111; color: #fff; font-weight: 700; cursor: pointer; }
code { background: #f6f6f6; padding: 0.1rem 0.3rem; border-radius: 6px; }
.muted { color: #555; }
</style>
</head>
<body>
<h1>OpenClaw Setup</h1>
<p class="muted">This wizard configures OpenClaw by running the same onboarding command it uses in the terminal, but from the browser.</p>
<div class="card">
<h2>Status</h2>
<div id="status">Loading...</div>
<div id="statusDetails" class="muted" style="margin-top:0.5rem"></div>
<div style="margin-top: 0.75rem">
<a href="/openclaw" target="_blank">Open OpenClaw UI</a>
|
<a href="/setup/export" target="_blank">Download backup (.tar.gz)</a>
</div>
<div style="margin-top: 0.75rem">
<div class="muted" style="margin-bottom:0.25rem"><strong>Import backup</strong> (advanced): restores into <code>/data</code> and restarts the gateway.</div>
<input id="importFile" type="file" accept=".tar.gz,application/gzip" />
<button id="importRun" style="background:#7c2d12; margin-top:0.5rem">Import</button>
<pre id="importOut" style="white-space:pre-wrap"></pre>
</div>
</div>
<div class="card">
<h2>Debug console</h2>
<p class="muted">Run a small allowlist of safe commands (no shell). Useful for debugging and recovery.</p>
<div style="display:flex; gap:0.5rem; align-items:center">
<select id="consoleCmd" style="flex: 1">
<option value="gateway.restart">gateway.restart (wrapper-managed)</option>
<option value="gateway.stop">gateway.stop (wrapper-managed)</option>
<option value="gateway.start">gateway.start (wrapper-managed)</option>
<option value="openclaw.status">openclaw status</option>
<option value="openclaw.health">openclaw health</option>
<option value="openclaw.doctor">openclaw doctor</option>
<option value="openclaw.logs.tail">openclaw logs --tail N</option>
<option value="openclaw.config.get">openclaw config get <path></option>
<option value="openclaw.version">openclaw --version</option>
<option value="openclaw.devices.list">openclaw devices list</option>
<option value="openclaw.devices.approve">openclaw devices approve <requestId></option>
<option value="openclaw.plugins.list">openclaw plugins list</option>
<option value="openclaw.plugins.enable">openclaw plugins enable <name></option>
</select>
<input id="consoleArg" placeholder="Optional arg (e.g. 200, gateway.port)" style="flex: 1" />
<button id="consoleRun" style="background:#0f172a">Run</button>
</div>
<pre id="consoleOut" style="white-space:pre-wrap"></pre>
</div>
<div class="card">
<h2>Config editor (advanced)</h2>
<p class="muted">Edits the full config file on disk (JSON5). Saving creates a timestamped <code>.bak-*</code> backup and restarts the gateway.</p>
<div class="muted" id="configPath"></div>
<textarea id="configText" style="width:100%; height: 260px; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;"></textarea>
<div style="margin-top:0.5rem">
<button id="configReload" style="background:#1f2937">Reload</button>
<button id="configSave" style="background:#111; margin-left:0.5rem">Save</button>
</div>
<pre id="configOut" style="white-space:pre-wrap"></pre>
</div>
<div class="card">
<h2>1) Model/auth provider</h2>
<p class="muted">Matches the groups shown in the terminal onboarding.</p>
<label>Provider group</label>
<select id="authGroup">
<option>Loading providers…</option>
</select>
<label>Auth method</label>
<select id="authChoice">
<option>Loading methods…</option>
</select>
<label>Key / Token (if required)</label>
<input id="authSecret" type="password" placeholder="Paste API key / token if applicable" />
<label>Wizard flow</label>
<select id="flow">
<option value="quickstart">quickstart</option>
<option value="advanced">advanced</option>
<option value="manual">manual</option>
</select>
</div>
<div class="card">
<h2>2) Optional: Channels</h2>
<p class="muted">You can also add channels later inside OpenClaw, but this helps you get messaging working immediately.</p>
<label>Telegram bot token (optional)</label>
<input id="telegramToken" type="password" placeholder="123456:ABC..." />
<div class="muted" style="margin-top: 0.25rem">
Get it from BotFather: open Telegram, message <code>@BotFather</code>, run <code>/newbot</code>, then copy the token.
</div>
<label>Discord bot token (optional)</label>
<input id="discordToken" type="password" placeholder="Bot token" />
<div class="muted" style="margin-top: 0.25rem">
Get it from the Discord Developer Portal: create an application, add a Bot, then copy the Bot Token.<br/>
<strong>Important:</strong> Enable <strong>MESSAGE CONTENT INTENT</strong> in Bot → Privileged Gateway Intents, or the bot will crash on startup.
</div>
<label>Slack bot token (optional)</label>
<input id="slackBotToken" type="password" placeholder="xoxb-..." />
<label>Slack app token (optional)</label>
<input id="slackAppToken" type="password" placeholder="xapp-..." />
</div>
<div class="card">
<h2>2b) Advanced: Custom OpenAI-compatible provider (optional)</h2>
<p class="muted">Use this to configure an OpenAI-compatible API that requires a custom base URL (e.g. Ollama, vLLM, LM Studio, hosted proxies). You usually set the API key as a Railway variable and reference it here.</p>
<label>Provider id (e.g. ollama, deepseek, myproxy)</label>
<input id="customProviderId" placeholder="ollama" />
<label>Base URL (must include /v1, e.g. http://host:11434/v1)</label>
<input id="customProviderBaseUrl" placeholder="http://127.0.0.1:11434/v1" />
<label>API (openai-completions or openai-responses)</label>
<select id="customProviderApi">
<option value="openai-completions">openai-completions</option>
<option value="openai-responses">openai-responses</option>
</select>
<label>API key env var name (optional, e.g. OLLAMA_API_KEY). Leave blank for no key.</label>
<input id="customProviderApiKeyEnv" placeholder="OLLAMA_API_KEY" />
<label>Optional model id to register (e.g. llama3.1:8b)</label>
<input id="customProviderModelId" placeholder="" />
</div>
<div class="card">
<h2>3) Run onboarding</h2>
<button id="run">Run setup</button>
<button id="pairingApprove" style="background:#1f2937; margin-left:0.5rem">Approve pairing</button>
<button id="reset" style="background:#444; margin-left:0.5rem">Reset setup</button>
<pre id="log" style="white-space:pre-wrap"></pre>
<p class="muted">Reset deletes the OpenClaw config file so you can rerun onboarding. Pairing approval lets you grant DM access when dmPolicy=pairing.</p>
<details style="margin-top: 0.75rem">
<summary><strong>Pairing helper</strong> (for “disconnected (1008): pairing required”)</summary>
<p class="muted">This lists pending device requests and lets you approve them without SSH.</p>
<button id="devicesRefresh" style="background:#0f172a">Refresh pending devices</button>
<div id="devicesList" class="muted" style="margin-top:0.5rem"></div>
</details>
</div>
<script src="/setup/app.js"></script>
</body>
</html>`);
});
const AUTH_GROUPS = [
{ value: "openai", label: "OpenAI", hint: "Codex OAuth + API key", options: [
{ value: "codex-cli", label: "OpenAI Codex OAuth (Codex CLI)" },
{ value: "openai-codex", label: "OpenAI Codex (ChatGPT OAuth)" },
{ value: "openai-api-key", label: "OpenAI API key" }
]},
{ value: "anthropic", label: "Anthropic", hint: "Claude Code CLI + API key", options: [
{ value: "claude-cli", label: "Anthropic token (Claude Code CLI)" },
{ value: "token", label: "Anthropic token (paste setup-token)" },
{ value: "apiKey", label: "Anthropic API key" }
]},
{ value: "google", label: "Google", hint: "Gemini API key + OAuth", options: [
{ value: "gemini-api-key", label: "Google Gemini API key" },
{ value: "google-antigravity", label: "Google Antigravity OAuth" },
{ value: "google-gemini-cli", label: "Google Gemini CLI OAuth" }
]},
{ value: "openrouter", label: "OpenRouter", hint: "API key", options: [
{ value: "openrouter-api-key", label: "OpenRouter API key" }
]},
{ value: "ai-gateway", label: "Vercel AI Gateway", hint: "API key", options: [
{ value: "ai-gateway-api-key", label: "Vercel AI Gateway API key" }
]},
{ value: "moonshot", label: "Moonshot AI", hint: "Kimi K2 + Kimi Code", options: [
{ value: "moonshot-api-key", label: "Moonshot AI API key" },
{ value: "kimi-code-api-key", label: "Kimi Code API key" }
]},
{ value: "zai", label: "Z.AI (GLM 4.7)", hint: "API key", options: [
{ value: "zai-api-key", label: "Z.AI (GLM 4.7) API key" }
]},
{ value: "minimax", label: "MiniMax", hint: "M2.1 (recommended)", options: [
{ value: "minimax-api", label: "MiniMax M2.1" },
{ value: "minimax-api-lightning", label: "MiniMax M2.1 Lightning" }
]},
{ value: "qwen", label: "Qwen", hint: "OAuth", options: [
{ value: "qwen-portal", label: "Qwen OAuth" }
]},
{ value: "copilot", label: "Copilot", hint: "GitHub + local proxy", options: [
{ value: "github-copilot", label: "GitHub Copilot (GitHub device login)" },
{ value: "copilot-proxy", label: "Copilot Proxy (local)" }
]},
{ value: "synthetic", label: "Synthetic", hint: "Anthropic-compatible (multi-model)", options: [
{ value: "synthetic-api-key", label: "Synthetic API key" }
]},
{ value: "opencode-zen", label: "OpenCode Zen", hint: "API key", options: [
{ value: "opencode-zen", label: "OpenCode Zen (multi-model proxy)" }
]}
];
app.get("/setup/api/status", requireSetupAuth, async (_req, res) => {
const version = await runCmd(OPENCLAW_NODE, clawArgs(["--version"]));
const channelsHelp = await runCmd(OPENCLAW_NODE, clawArgs(["channels", "add", "--help"]));
res.json({
configured: isConfigured(),
gatewayTarget: GATEWAY_TARGET,
openclawVersion: version.output.trim(),
channelsAddHelp: channelsHelp.output,
authGroups: AUTH_GROUPS,
});
});
app.get("/setup/api/auth-groups", requireSetupAuth, (_req, res) => {
res.json({ ok: true, authGroups: AUTH_GROUPS });
});
function buildOnboardArgs(payload) {
const args = [
"onboard",
"--non-interactive",
"--accept-risk",
"--json",
"--no-install-daemon",
"--skip-health",
"--workspace",
WORKSPACE_DIR,
// The wrapper owns public networking; keep the gateway internal.
"--gateway-bind",
"loopback",
"--gateway-port",
String(INTERNAL_GATEWAY_PORT),
"--gateway-auth",
"token",
"--gateway-token",
OPENCLAW_GATEWAY_TOKEN,
"--flow",
payload.flow || "quickstart",
];
if (payload.authChoice) {
args.push("--auth-choice", payload.authChoice);
// Map secret to correct flag for common choices.
const secret = (payload.authSecret || "").trim();
const map = {
"openai-api-key": "--openai-api-key",
"apiKey": "--anthropic-api-key",
"openrouter-api-key": "--openrouter-api-key",
"ai-gateway-api-key": "--ai-gateway-api-key",
"moonshot-api-key": "--moonshot-api-key",
"kimi-code-api-key": "--kimi-code-api-key",
"gemini-api-key": "--gemini-api-key",
"zai-api-key": "--zai-api-key",
"minimax-api": "--minimax-api-key",
"minimax-api-lightning": "--minimax-api-key",
"synthetic-api-key": "--synthetic-api-key",
"opencode-zen": "--opencode-zen-api-key",
};
const flag = map[payload.authChoice];
// If the user picked an API-key auth choice but didn't provide a secret, fail fast.
// Otherwise OpenClaw may fall back to its default auth choice, which looks like the
// wizard "reverted" their selection.
if (flag && !secret) {
throw new Error(`Missing auth secret for authChoice=${payload.authChoice}`);
}
if (flag) {
args.push(flag, secret);
}
if (payload.authChoice === "token") {
// This is the Anthropic setup-token flow.
if (!secret) throw new Error("Missing auth secret for authChoice=token");
args.push("--token-provider", "anthropic", "--token", secret);
}
}
return args;
}
function runCmd(cmd, args, opts = {}) {
return new Promise((resolve) => {
const timeoutMs = Number.isFinite(opts.timeoutMs) ? opts.timeoutMs : 120_000;
const proc = childProcess.spawn(cmd, args, {
...opts,
env: {
...process.env,
OPENCLAW_STATE_DIR: STATE_DIR,
OPENCLAW_WORKSPACE_DIR: WORKSPACE_DIR,
},
});
let out = "";
proc.stdout?.on("data", (d) => (out += d.toString("utf8")));
proc.stderr?.on("data", (d) => (out += d.toString("utf8")));
let killTimer;
const timer = setTimeout(() => {
try { proc.kill("SIGTERM"); } catch {}
killTimer = setTimeout(() => {
try { proc.kill("SIGKILL"); } catch {}
}, 2_000);
out += `\n[timeout] Command exceeded ${timeoutMs}ms and was terminated.\n`;
resolve({ code: 124, output: out });
}, timeoutMs);
proc.on("error", (err) => {
clearTimeout(timer);
if (killTimer) clearTimeout(killTimer);
out += `\n[spawn error] ${String(err)}\n`;
resolve({ code: 127, output: out });
});
proc.on("close", (code) => {
clearTimeout(timer);
if (killTimer) clearTimeout(killTimer);
resolve({ code: code ?? 0, output: out });
});
});
}
app.post("/setup/api/run", requireSetupAuth, async (req, res) => {
try {
const respondJson = (status, body) => {
if (res.writableEnded || res.headersSent) return;
res.status(status).json(body);
};
if (isConfigured()) {
await ensureGatewayRunning();
return respondJson(200, {
ok: true,
output: "Already configured.\nUse Reset setup if you want to rerun onboarding.\n",
});
}
fs.mkdirSync(STATE_DIR, { recursive: true });
fs.mkdirSync(WORKSPACE_DIR, { recursive: true });
const payload = req.body || {};
let onboardArgs;
try {
onboardArgs = buildOnboardArgs(payload);
} catch (err) {
return respondJson(400, { ok: false, output: `Setup input error: ${String(err)}` });
}
const prefix = "[setup] running openclaw onboard...\n";
const onboard = await runCmd(OPENCLAW_NODE, clawArgs(onboardArgs));
let extra = "";
const ok = onboard.code === 0 && isConfigured();
// Optional setup (only after successful onboarding).
if (ok) {
// Ensure gateway token is written into config so the browser UI can authenticate reliably.
// (We also enforce loopback bind since the wrapper proxies externally.)
// IMPORTANT: Set both gateway.auth.token (server-side) and gateway.remote.token (client-side)
// to the same value so the Control UI can connect without "token mismatch" errors.
await runCmd(OPENCLAW_NODE, clawArgs(["config", "set", "gateway.auth.mode", "token"]));
await runCmd(OPENCLAW_NODE, clawArgs(["config", "set", "gateway.auth.token", OPENCLAW_GATEWAY_TOKEN]));
await runCmd(OPENCLAW_NODE, clawArgs(["config", "set", "gateway.remote.token", OPENCLAW_GATEWAY_TOKEN]));
await runCmd(OPENCLAW_NODE, clawArgs(["config", "set", "gateway.bind", "loopback"]));
await runCmd(OPENCLAW_NODE, clawArgs(["config", "set", "gateway.port", String(INTERNAL_GATEWAY_PORT)]));
// Railway runs behind a reverse proxy. Trust loopback as a proxy hop so local client detection
// remains correct when X-Forwarded-* headers are present.
await runCmd(
OPENCLAW_NODE,
clawArgs(["config", "set", "--json", "gateway.trustedProxies", JSON.stringify(["127.0.0.1"]) ]),
);
// Optional: configure a custom OpenAI-compatible provider (base URL) for advanced users.
if (payload.customProviderId?.trim() && payload.customProviderBaseUrl?.trim()) {
const providerId = payload.customProviderId.trim();
const baseUrl = payload.customProviderBaseUrl.trim();
const api = (payload.customProviderApi || "openai-completions").trim();
const apiKeyEnv = (payload.customProviderApiKeyEnv || "").trim();
const modelId = (payload.customProviderModelId || "").trim();
if (!/^[A-Za-z0-9_-]+$/.test(providerId)) {
extra += `\n[custom provider] skipped: invalid provider id (use letters/numbers/_/-)`;
} else if (!/^https?:\/\//.test(baseUrl)) {
extra += `\n[custom provider] skipped: baseUrl must start with http(s)://`;
} else if (api !== "openai-completions" && api !== "openai-responses") {
extra += `\n[custom provider] skipped: api must be openai-completions or openai-responses`;
} else if (apiKeyEnv && !/^[A-Za-z_][A-Za-z0-9_]*$/.test(apiKeyEnv)) {
extra += `\n[custom provider] skipped: invalid api key env var name`;
} else {
const providerCfg = {
baseUrl,
api,
apiKey: apiKeyEnv ? "${" + apiKeyEnv + "}" : undefined,
models: modelId ? [{ id: modelId, name: modelId }] : undefined,
};
// Ensure we merge in this provider rather than replacing other providers.
await runCmd(OPENCLAW_NODE, clawArgs(["config", "set", "models.mode", "merge"]));
const set = await runCmd(
OPENCLAW_NODE,
clawArgs(["config", "set", "--json", `models.providers.${providerId}`, JSON.stringify(providerCfg)]),
);
extra += `\n[custom provider] exit=${set.code} (output ${set.output.length} chars)\n${set.output || "(no output)"}`;
}
}
const channelsHelp = await runCmd(OPENCLAW_NODE, clawArgs(["channels", "add", "--help"]));
const helpText = channelsHelp.output || "";
const supports = (name) => helpText.includes(name);
if (payload.telegramToken?.trim()) {
if (!supports("telegram")) {
extra += "\n[telegram] skipped (this openclaw build does not list telegram in `channels add --help`)\n";
} else {
// Avoid `channels add` here (it has proven flaky across builds); write config directly.
const token = payload.telegramToken.trim();
const cfgObj = {
enabled: true,
dmPolicy: "pairing",
botToken: token,
groupPolicy: "allowlist",
streamMode: "partial",
};
const set = await runCmd(
OPENCLAW_NODE,
clawArgs(["config", "set", "--json", "channels.telegram", JSON.stringify(cfgObj)]),
);
const get = await runCmd(OPENCLAW_NODE, clawArgs(["config", "get", "channels.telegram"]));
// Best-effort: enable the telegram plugin explicitly (some builds require this even when configured).
const plug = await runCmd(OPENCLAW_NODE, clawArgs(["plugins", "enable", "telegram"]));
extra += `\n[telegram config] exit=${set.code} (output ${set.output.length} chars)\n${set.output || "(no output)"}`;
extra += `\n[telegram verify] exit=${get.code} (output ${get.output.length} chars)\n${get.output || "(no output)"}`;
extra += `\n[telegram plugin enable] exit=${plug.code} (output ${plug.output.length} chars)\n${plug.output || "(no output)"}`;
}
}
if (payload.discordToken?.trim()) {
if (!supports("discord")) {
extra += "\n[discord] skipped (this openclaw build does not list discord in `channels add --help`)\n";
} else {
const token = payload.discordToken.trim();
const cfgObj = {
enabled: true,
token,
groupPolicy: "allowlist",
dm: {
policy: "pairing",
},
};
const set = await runCmd(
OPENCLAW_NODE,
clawArgs(["config", "set", "--json", "channels.discord", JSON.stringify(cfgObj)]),
);
const get = await runCmd(OPENCLAW_NODE, clawArgs(["config", "get", "channels.discord"]));
extra += `\n[discord config] exit=${set.code} (output ${set.output.length} chars)\n${set.output || "(no output)"}`;
extra += `\n[discord verify] exit=${get.code} (output ${get.output.length} chars)\n${get.output || "(no output)"}`;
}
}
if (payload.slackBotToken?.trim() || payload.slackAppToken?.trim()) {
if (!supports("slack")) {
extra += "\n[slack] skipped (this openclaw build does not list slack in `channels add --help`)\n";
} else {
const cfgObj = {
enabled: true,
botToken: payload.slackBotToken?.trim() || undefined,
appToken: payload.slackAppToken?.trim() || undefined,
};
const set = await runCmd(
OPENCLAW_NODE,
clawArgs(["config", "set", "--json", "channels.slack", JSON.stringify(cfgObj)]),
);
const get = await runCmd(OPENCLAW_NODE, clawArgs(["config", "get", "channels.slack"]));
extra += `\n[slack config] exit=${set.code} (output ${set.output.length} chars)\n${set.output || "(no output)"}`;
extra += `\n[slack verify] exit=${get.code} (output ${get.output.length} chars)\n${get.output || "(no output)"}`;
}
}
// Apply changes immediately.
await restartGateway();
// Ensure OpenClaw applies any "configured but not enabled" channel/plugin changes.
// This makes Telegram/Discord pairing issues much less "silent".
const fix = await runCmd(OPENCLAW_NODE, clawArgs(["doctor", "--fix"]));
extra += `\n[doctor --fix] exit=${fix.code} (output ${fix.output.length} chars)\n${fix.output || "(no output)"}`;
// Doctor may require a restart depending on changes.
await restartGateway();
}
return respondJson(ok ? 200 : 500, {
ok,
output: `${prefix}${onboard.output}${extra}`,
});
} catch (err) {
console.error("[/setup/api/run] error:", err);
return respondJson(500, { ok: false, output: `Internal error: ${String(err)}` });
}
});
app.get("/setup/api/debug", requireSetupAuth, async (_req, res) => {
const v = await runCmd(OPENCLAW_NODE, clawArgs(["--version"]));
const help = await runCmd(OPENCLAW_NODE, clawArgs(["channels", "add", "--help"]));
// Channel config checks (redact secrets before returning to client)
const tg = await runCmd(OPENCLAW_NODE, clawArgs(["config", "get", "channels.telegram"]));
const dc = await runCmd(OPENCLAW_NODE, clawArgs(["config", "get", "channels.discord"]));
const tgOut = redactSecrets(tg.output || "");
const dcOut = redactSecrets(dc.output || "");
res.json({
wrapper: {
node: process.version,
port: PORT,
publicPortEnv: process.env.PORT || null,
stateDir: STATE_DIR,
workspaceDir: WORKSPACE_DIR,
configured: isConfigured(),
configPathResolved: configPath(),
configPathCandidates: typeof resolveConfigCandidates === "function" ? resolveConfigCandidates() : null,
internalGatewayHost: INTERNAL_GATEWAY_HOST,
internalGatewayPort: INTERNAL_GATEWAY_PORT,
gatewayTarget: GATEWAY_TARGET,
gatewayRunning: Boolean(gatewayProc),
gatewayTokenFromEnv: Boolean(process.env.OPENCLAW_GATEWAY_TOKEN?.trim()),
gatewayTokenPersisted: fs.existsSync(path.join(STATE_DIR, "gateway.token")),
lastGatewayError,
lastGatewayExit,
lastDoctorAt,
lastDoctorOutput,