Spaces:
Running
Running
File size: 36,092 Bytes
51ec4bc 78e2c48 51ec4bc d41fe21 51ec4bc d41fe21 51ec4bc bcbf1ad ea9c8a5 f6ab881 ea9c8a5 1557f14 51ec4bc 969345a 51ec4bc 969345a 51ec4bc 969345a 51ec4bc 70d997a f6ab881 70d997a ea9c8a5 51ec4bc 78e2c48 f6ab881 09c0203 78e2c48 51ec4bc 2fcbf6a f6ab881 2fcbf6a f6ab881 2fcbf6a ea9c8a5 f6ab881 51ec4bc ea9c8a5 51ec4bc 544bf0f bcbf1ad 544bf0f bcbf1ad 544bf0f 1557f14 eec7304 1557f14 d9dfa01 70d997a d9dfa01 70d997a 1557f14 51ec4bc 18772a5 51ec4bc 18772a5 51ec4bc 18772a5 51ec4bc 78e2c48 70d997a 78e2c48 1557f14 78e2c48 1eb3ace 78e2c48 18772a5 70d997a 18772a5 51ec4bc d9dfa01 51ec4bc d9dfa01 51ec4bc 70d997a 51ec4bc 70d997a 51ec4bc d9dfa01 51ec4bc 70d997a 51ec4bc d9dfa01 51ec4bc 78e2c48 1557f14 78e2c48 51ec4bc 890b10a 6345e1c 890b10a 6345e1c 51ec4bc 6345e1c 51ec4bc 544bf0f 51ec4bc 544bf0f 51ec4bc 70d997a 51ec4bc 78e2c48 1557f14 a3d9f77 78e2c48 1557f14 78e2c48 1eb3ace 78e2c48 1eb3ace 78e2c48 6345e1c 78e2c48 51ec4bc 6345e1c 1557f14 a3d9f77 1557f14 51ec4bc 78e2c48 06ae20d 78e2c48 baa4b59 51ec4bc baa4b59 51ec4bc f6ab881 51ec4bc 70d997a baa4b59 51ec4bc f6ab881 51ec4bc d41fe21 969345a 51ec4bc f6ab881 51ec4bc d41fe21 51ec4bc f6ab881 544bf0f bcbf1ad 544bf0f bcbf1ad 544bf0f 51ec4bc 70d997a 78e2c48 51ec4bc 1557f14 51ec4bc ea9c8a5 70d997a 5f52ee7 f6ab881 51ec4bc 969345a 51ec4bc ea9c8a5 f6ab881 70d997a 5f52ee7 f6ab881 51ec4bc d41fe21 51ec4bc d41fe21 | 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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 | // Single public entrypoint for HF Spaces: local dashboard + reverse proxy to OpenClaw.
const http = require("http");
const https = require("https");
const fs = require("fs");
const net = require("net");
const PORT = 7861;
const GATEWAY_PORT = 7860;
const GATEWAY_HOST = "127.0.0.1";
const startTime = Date.now();
const LLM_MODEL = process.env.LLM_MODEL || "Not Set";
const TELEGRAM_ENABLED = !!process.env.TELEGRAM_BOT_TOKEN;
const WHATSAPP_ENABLED = /^true$/i.test(process.env.WHATSAPP_ENABLED || "");
const WHATSAPP_STATUS_FILE = "/tmp/huggingclaw-wa-status.json";
const HF_BACKUP_ENABLED = !!(process.env.HF_USERNAME && process.env.HF_TOKEN);
const SYNC_INTERVAL = process.env.SYNC_INTERVAL || "600";
const DASHBOARD_BASE = "/dashboard";
const DASHBOARD_STATUS_PATH = `${DASHBOARD_BASE}/status`;
const DASHBOARD_HEALTH_PATH = `${DASHBOARD_BASE}/health`;
const DASHBOARD_UPTIMEROBOT_PATH = `${DASHBOARD_BASE}/uptimerobot/setup`;
const DASHBOARD_APP_BASE = `${DASHBOARD_BASE}/app`;
const APP_BASE = "/app";
const SPACE_VISIBILITY_TTL_MS = 10 * 60 * 1000;
const spaceVisibilityCache = new Map();
function parseRequestUrl(url) {
try {
return new URL(url, "http://localhost");
} catch {
return new URL("http://localhost/");
}
}
function isDashboardRoute(pathname) {
return (
pathname === "/" ||
pathname === DASHBOARD_BASE ||
pathname === `${DASHBOARD_BASE}/`
);
}
function isDashboardAppRoute(pathname) {
return (
pathname === DASHBOARD_APP_BASE ||
pathname.startsWith(`${DASHBOARD_APP_BASE}/`)
);
}
function isAppRoute(pathname) {
return pathname === APP_BASE || pathname.startsWith(`${APP_BASE}/`);
}
function isLocalRoute(pathname) {
return (
pathname === "/health" ||
pathname === "/status" ||
pathname === "/uptimerobot/setup" ||
pathname === DASHBOARD_HEALTH_PATH ||
pathname === DASHBOARD_STATUS_PATH ||
pathname === DASHBOARD_UPTIMEROBOT_PATH
);
}
function mapAppProxyPath(path) {
if (path === DASHBOARD_APP_BASE) return APP_BASE;
if (path.startsWith(`${DASHBOARD_APP_BASE}/`)) {
return `${APP_BASE}${path.slice(DASHBOARD_APP_BASE.length)}`;
}
if (path === APP_BASE || path.startsWith(`${APP_BASE}/`)) {
return path;
}
return path;
}
function appendForwarded(existingValue, nextValue) {
const cleanNext = nextValue || "";
if (!existingValue) return cleanNext;
if (Array.isArray(existingValue))
return `${existingValue.join(", ")}, ${cleanNext}`;
return `${existingValue}, ${cleanNext}`;
}
function buildProxyHeaders(headers, remoteAddress) {
return {
...headers,
host: headers.host || `${GATEWAY_HOST}:${GATEWAY_PORT}`,
"x-forwarded-for": appendForwarded(
headers["x-forwarded-for"],
remoteAddress,
),
"x-forwarded-host": headers["x-forwarded-host"] || headers.host || "",
"x-forwarded-proto": headers["x-forwarded-proto"] || "https",
};
}
function readSyncStatus() {
try {
if (fs.existsSync("/tmp/sync-status.json")) {
return JSON.parse(fs.readFileSync("/tmp/sync-status.json", "utf8"));
}
} catch {}
if (HF_BACKUP_ENABLED) {
return {
status: "configured",
message: `Backup is enabled. Waiting for the next sync window (${SYNC_INTERVAL}s).`,
};
}
return { status: "unknown", message: "No sync data yet" };
}
function normalizeChannelStatus(channel, configured) {
return {
configured: configured || !!channel,
connected: !!(channel && channel.connected),
};
}
function readGuardianStatus() {
if (!WHATSAPP_ENABLED) {
return { configured: false, connected: false, pairing: false };
}
try {
if (fs.existsSync(WHATSAPP_STATUS_FILE)) {
const parsed = JSON.parse(fs.readFileSync(WHATSAPP_STATUS_FILE, "utf8"));
return {
configured: parsed.configured !== false,
connected: parsed.connected === true,
pairing: parsed.pairing === true,
};
}
} catch {}
return { configured: true, connected: false, pairing: false };
}
function decodeJwtPayload(token) {
try {
const parts = String(token || "").split(".");
if (parts.length < 2) return null;
const normalized = parts[1].replace(/-/g, "+").replace(/_/g, "/");
const padded = normalized + "=".repeat((4 - (normalized.length % 4)) % 4);
return JSON.parse(Buffer.from(padded, "base64").toString("utf8"));
} catch {
return null;
}
}
function getSpaceRef(parsedUrl) {
const signedToken = parsedUrl.searchParams.get("__sign");
if (!signedToken) return null;
const payload = decodeJwtPayload(signedToken);
const subject = payload && payload.sub;
const match =
typeof subject === "string"
? subject.match(/^\/spaces\/([^/]+)\/([^/]+)$/)
: null;
if (!match) return null;
return { owner: match[1], repo: match[2] };
}
function fetchStatusCode(url) {
return new Promise((resolve, reject) => {
const req = https.get(
url,
{
headers: {
"user-agent": "HuggingClaw/1.0",
accept: "application/json",
},
},
(res) => {
res.resume();
resolve(res.statusCode || 0);
},
);
req.on("error", reject);
req.setTimeout(5000, () => {
req.destroy(new Error("Request timed out"));
});
});
}
async function resolveSpaceIsPrivate(parsedUrl) {
const ref = getSpaceRef(parsedUrl);
if (!ref) return false;
const cacheKey = `${ref.owner}/${ref.repo}`;
const cached = spaceVisibilityCache.get(cacheKey);
if (cached && Date.now() - cached.timestamp < SPACE_VISIBILITY_TTL_MS) {
return cached.isPrivate;
}
try {
const statusCode = await fetchStatusCode(
`https://huggingface.co/api/spaces/${ref.owner}/${ref.repo}`,
);
const isPrivate = statusCode === 401 || statusCode === 403 || statusCode === 404;
spaceVisibilityCache.set(cacheKey, { isPrivate, timestamp: Date.now() });
return isPrivate;
} catch {
if (cached) return cached.isPrivate;
return false;
}
}
function renderChannelBadge(channel, configuredLabel) {
if (channel && channel.connected) {
return '<div class="status-badge status-online"><div class="pulse"></div>Active</div>';
}
if (channel && channel.configured) {
return `<div class="status-badge status-syncing">${configuredLabel}</div>`;
}
return '<div class="status-badge status-offline">Disabled</div>';
}
function renderSyncBadge(syncData) {
let badgeClass = "status-offline";
let pulseHtml = "";
if (syncData.status === "success" || syncData.status === "configured") {
badgeClass = "status-online";
pulseHtml = '<div class="pulse"></div>';
} else if (syncData.status === "syncing") {
badgeClass = "status-syncing";
pulseHtml = '<div class="pulse" style="background:#3b82f6"></div>';
}
return `<div class="status-badge ${badgeClass}">${pulseHtml}${String(syncData.status || "unknown").toUpperCase()}</div>`;
}
function renderDashboard(initialData) {
const controlUiHref = `${APP_BASE}/`;
const keepAwakeHtml = initialData.spacePrivate
? `
<div id="uptimerobot-private-note" class="helper-summary">
<strong>This Space is private.</strong> External monitors cannot reliably access private HF health URLs, so keep-awake setup is only available on public Spaces.
</div>
`
: `
<div id="uptimerobot-public-flow">
<div id="uptimerobot-summary" class="helper-summary">
One-time setup for public Spaces. Paste your UptimeRobot <strong>Main API key</strong> to create the monitor.
</div>
<button id="uptimerobot-toggle" class="helper-toggle" type="button">
Set Up Monitor
</button>
<div id="uptimerobot-shell" class="helper-shell hidden">
<div class="helper-copy">
Do <strong>not</strong> use the Read-only API key or a Monitor-specific API key.
</div>
<div class="helper-row">
<input
id="uptimerobot-key"
class="helper-input"
type="password"
placeholder="Paste your UptimeRobot Main API key"
autocomplete="off"
/>
<button id="uptimerobot-btn" class="helper-button" type="button">
Create Monitor
</button>
</div>
<div class="helper-note">
One-time setup. Your key is only used to create the monitor for this Space.
</div>
</div>
</div>
`;
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HuggingClaw Dashboard</title>
<link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;600&display=swap" rel="stylesheet">
<style>
:root {
--bg: #0f172a;
--card-bg: rgba(30, 41, 59, 0.7);
--accent: linear-gradient(135deg, #3b82f6, #8b5cf6);
--text: #f8fafc;
--text-dim: #94a3b8;
--success: #10b981;
--error: #ef4444;
--warning: #f59e0b;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Outfit', sans-serif;
background-color: var(--bg);
color: var(--text);
display: flex;
justify-content: center;
align-items: flex-start;
min-height: 100vh;
overflow-x: hidden;
overflow-y: auto;
padding: 24px 0;
background-image:
radial-gradient(at 0% 0%, rgba(59, 130, 246, 0.15) 0px, transparent 50%),
radial-gradient(at 100% 0%, rgba(139, 92, 246, 0.15) 0px, transparent 50%);
}
.dashboard {
width: 90%;
max-width: 600px;
background: var(--card-bg);
backdrop-filter: blur(12px);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 24px;
padding: 40px;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
animation: fadeIn 0.8s ease-out;
margin: 24px 0;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
header {
text-align: center;
margin-bottom: 40px;
}
h1 {
font-size: 2.5rem;
margin-bottom: 8px;
background: var(--accent);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-weight: 600;
}
.subtitle {
color: var(--text-dim);
font-size: 0.9rem;
letter-spacing: 1px;
text-transform: uppercase;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
margin-bottom: 30px;
}
.stat-card {
background: rgba(255, 255, 255, 0.03);
border: 1px solid rgba(255, 255, 255, 0.05);
padding: 20px;
border-radius: 16px;
transition: transform 0.3s ease, border-color 0.3s ease;
}
.stat-card:hover {
transform: translateY(-5px);
border-color: rgba(59, 130, 246, 0.3);
}
.stat-label {
color: var(--text-dim);
font-size: 0.75rem;
text-transform: uppercase;
margin-bottom: 8px;
display: block;
}
.stat-value {
font-size: 1.1rem;
font-weight: 600;
word-break: break-all;
}
.stat-btn {
grid-column: span 2;
background: var(--accent);
color: #fff;
padding: 16px;
border-radius: 16px;
text-align: center;
text-decoration: none;
font-weight: 600;
margin-top: 10px;
transition: transform 0.3s ease, box-shadow 0.3s ease;
box-shadow: 0 10px 20px -5px rgba(59, 130, 246, 0.4);
}
.stat-btn:hover {
transform: scale(1.02);
box-shadow: 0 15px 30px -5px rgba(59, 130, 246, 0.6);
}
.status-badge {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 4px 12px;
border-radius: 20px;
font-size: 0.8rem;
font-weight: 600;
}
.status-online { background: rgba(16, 185, 129, 0.1); color: var(--success); }
.status-offline { background: rgba(239, 68, 68, 0.1); color: var(--error); }
.status-syncing { background: rgba(59, 130, 246, 0.1); color: #3b82f6; }
.pulse {
width: 8px;
height: 8px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7);
animation: pulse 2s infinite;
}
@keyframes pulse {
0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7); }
70% { transform: scale(1); box-shadow: 0 0 0 10px rgba(16, 185, 129, 0); }
100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(16, 185, 129, 0); }
}
.footer {
text-align: center;
color: var(--text-dim);
font-size: 0.8rem;
margin-top: 20px;
}
.sync-info {
background: rgba(255, 255, 255, 0.02);
padding: 15px;
border-radius: 12px;
font-size: 0.85rem;
color: var(--text-dim);
margin-top: 10px;
}
#sync-msg { color: var(--text); display: block; margin-top: 4px; }
.card-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
}
.card-header .stat-label {
margin-bottom: 0;
}
.helper-card {
width: 100%;
margin-top: 20px;
}
.helper-copy {
color: var(--text-dim);
font-size: 0.92rem;
line-height: 1.6;
margin-top: 10px;
}
.helper-copy strong {
color: var(--text);
}
.helper-row {
display: flex;
gap: 10px;
margin-top: 16px;
flex-wrap: wrap;
}
.helper-input {
flex: 1;
min-width: 240px;
background: rgba(255, 255, 255, 0.04);
border: 1px solid rgba(255, 255, 255, 0.08);
color: var(--text);
border-radius: 12px;
padding: 14px 16px;
font: inherit;
}
.helper-input::placeholder {
color: var(--text-dim);
}
.helper-button {
background: var(--accent);
color: #fff;
border: 0;
border-radius: 12px;
padding: 14px 18px;
font: inherit;
font-weight: 600;
cursor: pointer;
min-width: 180px;
}
.helper-button:disabled {
opacity: 0.6;
cursor: wait;
}
.hidden {
display: none !important;
}
.helper-note {
margin-top: 10px;
font-size: 0.82rem;
color: var(--text-dim);
}
.helper-result {
margin-top: 14px;
padding: 12px 14px;
border-radius: 12px;
font-size: 0.9rem;
display: none;
}
.helper-result.ok {
display: block;
background: rgba(16, 185, 129, 0.1);
color: var(--success);
}
.helper-result.error {
display: block;
background: rgba(239, 68, 68, 0.1);
color: var(--error);
}
.helper-shell {
margin-top: 12px;
}
.helper-shell.hidden {
display: none;
}
.helper-summary {
margin-top: 14px;
padding: 12px 14px;
border-radius: 12px;
background: rgba(255, 255, 255, 0.03);
color: var(--text-dim);
font-size: 0.9rem;
line-height: 1.5;
}
.helper-summary strong {
color: var(--text);
}
.helper-summary.success {
background: rgba(16, 185, 129, 0.08);
}
.helper-toggle {
margin-top: 14px;
display: inline-flex;
align-items: center;
justify-content: center;
background: rgba(255, 255, 255, 0.04);
color: var(--text);
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 12px;
padding: 12px 16px;
font: inherit;
font-weight: 600;
cursor: pointer;
}
@media (max-width: 700px) {
body {
padding: 16px 0;
}
.dashboard {
width: calc(100% - 24px);
padding: 24px;
border-radius: 18px;
margin: 12px 0;
}
header {
margin-bottom: 28px;
}
h1 {
font-size: 2rem;
}
.stats-grid {
grid-template-columns: 1fr;
gap: 14px;
margin-bottom: 20px;
}
.stat-btn {
grid-column: span 1;
}
.stat-card {
padding: 16px;
}
.card-header {
align-items: flex-start;
flex-direction: column;
}
.helper-row {
flex-direction: column;
}
.helper-input,
.helper-button {
width: 100%;
min-width: 0;
}
}
</style>
</head>
<body>
<div class="dashboard">
<header>
<h1>🦞 HuggingClaw</h1>
<p class="subtitle">Space Dashboard</p>
</header>
<div class="stats-grid">
<div class="stat-card">
<span class="stat-label">Model</span>
<span class="stat-value" id="model-id">${initialData.model}</span>
</div>
<div class="stat-card">
<span class="stat-label">Uptime</span>
<span class="stat-value" id="uptime">${initialData.uptime}</span>
</div>
<div class="stat-card">
<span class="stat-label">WhatsApp</span>
<span id="wa-status">${renderChannelBadge(initialData.whatsapp, "Ready to pair")}</span>
</div>
<div class="stat-card">
<span class="stat-label">Telegram</span>
<span id="tg-status">${renderChannelBadge(initialData.telegram, "Configured")}</span>
</div>
<a href="${controlUiHref}" id="control-ui-link" class="stat-btn">Open Control UI</a>
</div>
<div class="stat-card" style="width: 100%;">
<div class="card-header">
<span class="stat-label">Workspace Sync Status</span>
<div id="sync-badge-container">${renderSyncBadge(initialData.sync)}</div>
</div>
<div class="sync-info">
Last Sync Activity: <span id="sync-time">${initialData.sync.timestamp || "Never"}</span>
<span id="sync-msg">${initialData.sync.message || "Waiting for first sync..."}</span>
</div>
</div>
<div class="stat-card helper-card">
<span class="stat-label">Keep Space Awake</span>
${keepAwakeHtml}
<div id="uptimerobot-result" class="helper-result"></div>
</div>
<div class="footer">
Live updates every 10s
</div>
</div>
<script>
function getDashboardBase() {
const pathname = window.location.pathname || '/';
if (pathname === '/' || pathname === '') return '';
if (pathname === '${DASHBOARD_BASE}' || pathname === '${DASHBOARD_BASE}/') return '${DASHBOARD_BASE}';
return pathname.endsWith('/') ? pathname.slice(0, -1) : pathname;
}
function getCurrentSearch() {
return window.location.search || '';
}
async function updateStats() {
try {
const res = await fetch(getDashboardBase() + '/status' + getCurrentSearch());
const data = await res.json();
document.getElementById('model-id').textContent = data.model;
document.getElementById('uptime').textContent = data.uptime;
function renderChannelStatus(channel, configuredLabel) {
if (channel && channel.connected) {
return '<div class="status-badge status-online"><div class="pulse"></div>Active</div>';
}
if (channel && channel.configured) {
return '<div class="status-badge status-syncing">' + configuredLabel + '</div>';
}
return '<div class="status-badge status-offline">Disabled</div>';
}
document.getElementById('wa-status').innerHTML = renderChannelStatus(data.whatsapp, 'Ready to pair');
document.getElementById('tg-status').innerHTML = renderChannelStatus(data.telegram, 'Configured');
const syncData = data.sync;
let badgeClass = 'status-offline';
let pulseHtml = '';
if (syncData.status === 'success' || syncData.status === 'configured') {
badgeClass = 'status-online';
pulseHtml = '<div class="pulse"></div>';
} else if (syncData.status === 'syncing') {
badgeClass = 'status-syncing';
pulseHtml = '<div class="pulse" style="background:#3b82f6"></div>';
}
document.getElementById('sync-badge-container').innerHTML =
'<div class="status-badge ' + badgeClass + '">' + pulseHtml + syncData.status.toUpperCase() + '</div>';
document.getElementById('sync-time').textContent = syncData.timestamp || 'Never';
document.getElementById('sync-msg').textContent = syncData.message || 'Waiting for first sync...';
} catch (e) {
console.error("Failed to fetch status", e);
}
}
const monitorStateKey = 'huggingclaw_uptimerobot_setup_v1';
const KEEP_AWAKE_PRIVATE = ${initialData.spacePrivate ? "true" : "false"};
function setMonitorUiState(isConfigured) {
const summary = document.getElementById('uptimerobot-summary');
const shell = document.getElementById('uptimerobot-shell');
const toggle = document.getElementById('uptimerobot-toggle');
if (!summary || !shell || !toggle) {
return;
}
if (isConfigured) {
summary.classList.add('success');
summary.innerHTML = '<strong>Already set up.</strong> Your UptimeRobot monitor should keep this public Space awake.';
shell.classList.add('hidden');
toggle.textContent = 'Set Up Again';
} else {
summary.classList.remove('success');
summary.innerHTML = 'One-time setup for public Spaces. Paste your UptimeRobot <strong>Main API key</strong> to create the monitor.';
toggle.textContent = 'Set Up Monitor';
}
}
function restoreMonitorUiState() {
try {
const value = window.localStorage.getItem(monitorStateKey);
setMonitorUiState(value === 'done');
} catch {
setMonitorUiState(false);
}
}
function toggleMonitorSetup() {
const shell = document.getElementById('uptimerobot-shell');
shell.classList.toggle('hidden');
}
async function setupUptimeRobot() {
const input = document.getElementById('uptimerobot-key');
const button = document.getElementById('uptimerobot-btn');
const result = document.getElementById('uptimerobot-result');
const apiKey = input.value.trim();
if (!apiKey) {
result.className = 'helper-result error';
result.textContent = 'Paste your UptimeRobot Main API key first.';
return;
}
button.disabled = true;
button.textContent = 'Creating...';
result.className = 'helper-result';
result.textContent = '';
try {
const res = await fetch(getDashboardBase() + '/uptimerobot/setup' + getCurrentSearch(), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ apiKey })
});
const data = await res.json();
if (!res.ok) {
throw new Error(data.message || 'Failed to create monitor.');
}
result.className = 'helper-result ok';
result.textContent = data.message || 'UptimeRobot monitor is ready.';
input.value = '';
try {
window.localStorage.setItem(monitorStateKey, 'done');
} catch {}
setMonitorUiState(true);
document.getElementById('uptimerobot-shell').classList.add('hidden');
} catch (error) {
result.className = 'helper-result error';
result.textContent = error.message || 'Failed to create monitor.';
} finally {
button.disabled = false;
button.textContent = 'Create Monitor';
}
}
updateStats();
setInterval(updateStats, 10000);
document.getElementById('control-ui-link').setAttribute('href', getDashboardBase() + '/app/' + getCurrentSearch());
if (!KEEP_AWAKE_PRIVATE) {
restoreMonitorUiState();
document.getElementById('uptimerobot-btn').addEventListener('click', setupUptimeRobot);
document.getElementById('uptimerobot-toggle').addEventListener('click', toggleMonitorSetup);
}
</script>
</body>
</html>
`;
}
function readRequestBody(req) {
return new Promise((resolve, reject) => {
let body = "";
req.on("data", (chunk) => {
body += chunk;
if (body.length > 1024 * 64) {
reject(new Error("Request too large"));
req.destroy();
}
});
req.on("end", () => resolve(body));
req.on("error", reject);
});
}
function postUptimeRobot(path, form) {
const body = new URLSearchParams(form).toString();
return new Promise((resolve, reject) => {
const request = https.request(
{
hostname: "api.uptimerobot.com",
port: 443,
method: "POST",
path,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": Buffer.byteLength(body),
},
},
(response) => {
let raw = "";
response.setEncoding("utf8");
response.on("data", (chunk) => {
raw += chunk;
});
response.on("end", () => {
try {
resolve(JSON.parse(raw));
} catch {
reject(new Error("Unexpected response from UptimeRobot"));
}
});
},
);
request.on("error", reject);
request.write(body);
request.end();
});
}
async function createUptimeRobotMonitor(apiKey, host) {
const cleanHost = String(host || "")
.replace(/^https?:\/\//, "")
.replace(/\/.*$/, "");
if (!cleanHost) {
throw new Error("Missing Space host.");
}
const monitorUrl = `https://${cleanHost}/health`;
const existing = await postUptimeRobot("/v2/getMonitors", {
api_key: apiKey,
format: "json",
logs: "0",
response_times: "0",
response_times_limit: "1",
});
const existingMonitor = Array.isArray(existing.monitors)
? existing.monitors.find((monitor) => monitor.url === monitorUrl)
: null;
if (existingMonitor) {
return {
created: false,
message: `Monitor already exists for ${monitorUrl}`,
};
}
const created = await postUptimeRobot("/v2/newMonitor", {
api_key: apiKey,
format: "json",
type: "1",
friendly_name: `HuggingClaw ${cleanHost}`,
url: monitorUrl,
interval: "300",
});
if (created.stat !== "ok") {
const message =
created?.error?.message ||
created?.message ||
"Failed to create UptimeRobot monitor.";
throw new Error(message);
}
return {
created: true,
message: `Monitor created for ${monitorUrl}`,
};
}
function proxyHttp(req, res, proxyPath = req.url, proxyPort = GATEWAY_PORT) {
const proxyReq = http.request(
{
hostname: GATEWAY_HOST,
port: proxyPort,
method: req.method,
path: proxyPath,
headers: buildProxyHeaders(req.headers, req.socket.remoteAddress),
},
(proxyRes) => {
res.writeHead(proxyRes.statusCode || 502, proxyRes.headers);
proxyRes.pipe(res);
},
);
proxyReq.on("error", (error) => {
res.writeHead(502, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
status: "error",
message: "Gateway unavailable",
detail: error.message,
}),
);
});
req.pipe(proxyReq);
}
function serializeUpgradeHeaders(req, remoteAddress) {
const forwardedHeaders = [];
for (let i = 0; i < req.rawHeaders.length; i += 2) {
const name = req.rawHeaders[i];
const value = req.rawHeaders[i + 1];
const lower = name.toLowerCase();
if (
lower === "x-forwarded-for" ||
lower === "x-forwarded-host" ||
lower === "x-forwarded-proto"
) {
continue;
}
forwardedHeaders.push(`${name}: ${value}`);
}
forwardedHeaders.push(
`X-Forwarded-For: ${appendForwarded(req.headers["x-forwarded-for"], remoteAddress)}`,
);
forwardedHeaders.push(
`X-Forwarded-Host: ${req.headers["x-forwarded-host"] || req.headers.host || ""}`,
);
forwardedHeaders.push(
`X-Forwarded-Proto: ${req.headers["x-forwarded-proto"] || "https"}`,
);
return forwardedHeaders;
}
function proxyUpgrade(
req,
socket,
head,
proxyPath = req.url,
proxyPort = GATEWAY_PORT,
) {
const proxySocket = net.connect(proxyPort, GATEWAY_HOST);
proxySocket.on("connect", () => {
const requestLines = [
`${req.method} ${proxyPath} HTTP/${req.httpVersion}`,
...serializeUpgradeHeaders(req, req.socket.remoteAddress),
"",
"",
];
proxySocket.write(requestLines.join("\r\n"));
if (head && head.length > 0) {
proxySocket.write(head);
}
socket.pipe(proxySocket).pipe(socket);
});
proxySocket.on("error", () => {
if (socket.writable) {
socket.write("HTTP/1.1 502 Bad Gateway\r\nConnection: close\r\n\r\n");
}
socket.destroy();
});
socket.on("error", () => {
proxySocket.destroy();
});
}
const server = http.createServer((req, res) => {
const parsedUrl = parseRequestUrl(req.url || "/");
const pathname = parsedUrl.pathname;
const uptime = Math.floor((Date.now() - startTime) / 1000);
const uptimeHuman = `${Math.floor(uptime / 3600)}h ${Math.floor((uptime % 3600) / 60)}m`;
if (pathname === "/health" || pathname === DASHBOARD_HEALTH_PATH) {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
status: "ok",
uptime,
uptimeHuman,
timestamp: new Date().toISOString(),
}),
);
return;
}
if (pathname === "/status" || pathname === DASHBOARD_STATUS_PATH) {
void (async () => {
const guardianStatus = readGuardianStatus();
res.writeHead(200, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
model: LLM_MODEL,
whatsapp: {
configured: guardianStatus.configured,
connected: guardianStatus.connected,
pairing: guardianStatus.pairing,
},
telegram: normalizeChannelStatus(null, TELEGRAM_ENABLED),
sync: readSyncStatus(),
uptime: uptimeHuman,
}),
);
})();
return;
}
if (
pathname === "/uptimerobot/setup" ||
pathname === DASHBOARD_UPTIMEROBOT_PATH
) {
if (req.method !== "POST") {
res.writeHead(405, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Method not allowed" }));
return;
}
void (async () => {
try {
const body = await readRequestBody(req);
const parsed = JSON.parse(body || "{}");
const apiKey = String(parsed.apiKey || "").trim();
if (!apiKey) {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
message: "Paste your UptimeRobot Main API key first.",
}),
);
return;
}
const result = await createUptimeRobotMonitor(apiKey, req.headers.host);
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(result));
} catch (error) {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
message:
error && error.message
? error.message
: "Failed to create UptimeRobot monitor.",
}),
);
}
})();
return;
}
if (isDashboardRoute(pathname)) {
void (async () => {
const guardianStatus = readGuardianStatus();
const initialData = {
model: LLM_MODEL,
whatsapp: {
configured: guardianStatus.configured,
connected: guardianStatus.connected,
pairing: guardianStatus.pairing,
},
telegram: normalizeChannelStatus(null, TELEGRAM_ENABLED),
sync: readSyncStatus(),
uptime: uptimeHuman,
spacePrivate: await resolveSpaceIsPrivate(parsedUrl),
};
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
res.end(renderDashboard(initialData));
})();
return;
}
if (isDashboardAppRoute(pathname) || isAppRoute(pathname)) {
const proxyPath = mapAppProxyPath(pathname) + (parsedUrl.search || "");
proxyHttp(req, res, proxyPath, GATEWAY_PORT);
return;
}
proxyHttp(req, res);
});
server.on("upgrade", (req, socket, head) => {
const pathname = parseRequestUrl(req.url || "/").pathname;
if (isLocalRoute(pathname)) {
socket.destroy();
return;
}
if (isDashboardAppRoute(pathname) || isAppRoute(pathname)) {
const parsedUrl = parseRequestUrl(req.url || "/");
const proxyPath = mapAppProxyPath(pathname) + (parsedUrl.search || "");
proxyUpgrade(req, socket, head, proxyPath, GATEWAY_PORT);
return;
}
proxyUpgrade(req, socket, head);
});
server.listen(PORT, "0.0.0.0", () => {
console.log(
`Health server listening on port ${PORT}; proxying gateway traffic to ${GATEWAY_HOST}:${GATEWAY_PORT}`,
);
});
|