@clagentic/console 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CREDITS.md +3 -0
- package/LICENSE +21 -0
- package/README.md +250 -0
- package/bin/cli.js +2812 -0
- package/lib/agents-favorites.js +164 -0
- package/lib/agents.js +282 -0
- package/lib/ask-user-mcp-server.js +120 -0
- package/lib/browser-mcp-server.js +484 -0
- package/lib/build-user-env.js +63 -0
- package/lib/certs/fullchain.pem +47 -0
- package/lib/certs/privkey.pem +5 -0
- package/lib/cli-sessions.js +265 -0
- package/lib/codex-defaults.js +18 -0
- package/lib/config.js +332 -0
- package/lib/crisis-safety.js +60 -0
- package/lib/daemon-projects.js +164 -0
- package/lib/daemon.js +1602 -0
- package/lib/dm.js +128 -0
- package/lib/ipc.js +125 -0
- package/lib/ipv4-only.js +39 -0
- package/lib/mcp-local.js +385 -0
- package/lib/notes.js +141 -0
- package/lib/os-users.js +529 -0
- package/lib/pages.js +1325 -0
- package/lib/project-connection.js +302 -0
- package/lib/project-external-trigger.js +236 -0
- package/lib/project-file-watch.js +120 -0
- package/lib/project-filesystem.js +528 -0
- package/lib/project-http.js +753 -0
- package/lib/project-image.js +95 -0
- package/lib/project-knowledge.js +88 -0
- package/lib/project-loop.js +1304 -0
- package/lib/project-mcp.js +374 -0
- package/lib/project-memory.js +39 -0
- package/lib/project-notifications.js +262 -0
- package/lib/project-sessions.js +1581 -0
- package/lib/project-user-mention.js +193 -0
- package/lib/project-user-message.js +628 -0
- package/lib/project.js +1303 -0
- package/lib/public/app.js +1031 -0
- package/lib/public/apple-touch-icon.png +0 -0
- package/lib/public/claude-code-avatar.png +0 -0
- package/lib/public/codex-avatar.png +0 -0
- package/lib/public/console-icon.png +0 -0
- package/lib/public/css/admin.css +736 -0
- package/lib/public/css/agent-picker.css +119 -0
- package/lib/public/css/base.css +235 -0
- package/lib/public/css/channel-layout.css +441 -0
- package/lib/public/css/command-palette.css +559 -0
- package/lib/public/css/debate.css +1609 -0
- package/lib/public/css/diff.css +139 -0
- package/lib/public/css/filebrowser.css +1976 -0
- package/lib/public/css/highlight.css +144 -0
- package/lib/public/css/home-hub.css +545 -0
- package/lib/public/css/icon-strip.css +1856 -0
- package/lib/public/css/input.css +1349 -0
- package/lib/public/css/loop.css +1248 -0
- package/lib/public/css/mention.css +317 -0
- package/lib/public/css/menus.css +857 -0
- package/lib/public/css/messages.css +1833 -0
- package/lib/public/css/mobile-nav.css +1332 -0
- package/lib/public/css/notifications-center.css +296 -0
- package/lib/public/css/overlays.css +1273 -0
- package/lib/public/css/playbook.css +264 -0
- package/lib/public/css/profile.css +397 -0
- package/lib/public/css/rewind.css +671 -0
- package/lib/public/css/scheduler-modal.css +1495 -0
- package/lib/public/css/scheduler.css +1420 -0
- package/lib/public/css/server-settings.css +909 -0
- package/lib/public/css/session-search.css +159 -0
- package/lib/public/css/sidebar.css +2012 -0
- package/lib/public/css/skills.css +789 -0
- package/lib/public/css/sticky-notes.css +854 -0
- package/lib/public/css/stt.css +155 -0
- package/lib/public/css/title-bar.css +674 -0
- package/lib/public/css/tooltip.css +67 -0
- package/lib/public/css/user-settings.css +702 -0
- package/lib/public/emoji-data.json +1 -0
- package/lib/public/favicon-banded-32.png +0 -0
- package/lib/public/favicon-banded.png +0 -0
- package/lib/public/favicon-dark.svg +32 -0
- package/lib/public/favicon.svg +32 -0
- package/lib/public/icon-192.png +0 -0
- package/lib/public/icon-512.png +0 -0
- package/lib/public/icon-banded-76.png +0 -0
- package/lib/public/icon-banded-96.png +0 -0
- package/lib/public/icon-mono.svg +25 -0
- package/lib/public/icons/email/gmail.svg +7 -0
- package/lib/public/icons/email/outlook.svg +35 -0
- package/lib/public/icons/email/yahoo.svg +1 -0
- package/lib/public/index.html +1946 -0
- package/lib/public/manifest.json +27 -0
- package/lib/public/markdown-worker.js +43 -0
- package/lib/public/marked.min.js +6 -0
- package/lib/public/modules/admin.js +954 -0
- package/lib/public/modules/agent-picker.js +429 -0
- package/lib/public/modules/app-connection.js +216 -0
- package/lib/public/modules/app-cursors.js +492 -0
- package/lib/public/modules/app-dm.js +265 -0
- package/lib/public/modules/app-favicon.js +231 -0
- package/lib/public/modules/app-header.js +242 -0
- package/lib/public/modules/app-home-hub.js +432 -0
- package/lib/public/modules/app-loop-ui.js +691 -0
- package/lib/public/modules/app-loop-wizard.js +439 -0
- package/lib/public/modules/app-messages.js +1415 -0
- package/lib/public/modules/app-misc.js +463 -0
- package/lib/public/modules/app-notifications.js +651 -0
- package/lib/public/modules/app-panels.js +1061 -0
- package/lib/public/modules/app-projects.js +875 -0
- package/lib/public/modules/app-rate-limit.js +529 -0
- package/lib/public/modules/app-rendering.js +739 -0
- package/lib/public/modules/app-skills-install.js +285 -0
- package/lib/public/modules/avatar.js +36 -0
- package/lib/public/modules/command-palette.js +491 -0
- package/lib/public/modules/console-logo.js +162 -0
- package/lib/public/modules/context-sources.js +365 -0
- package/lib/public/modules/diff.js +412 -0
- package/lib/public/modules/dom-refs.js +21 -0
- package/lib/public/modules/emoji-picker.js +222 -0
- package/lib/public/modules/events.js +21 -0
- package/lib/public/modules/filebrowser.js +1966 -0
- package/lib/public/modules/fileicons.js +172 -0
- package/lib/public/modules/icons.js +80 -0
- package/lib/public/modules/input.js +1102 -0
- package/lib/public/modules/longpress.js +74 -0
- package/lib/public/modules/markdown.js +486 -0
- package/lib/public/modules/mcp-ui.js +295 -0
- package/lib/public/modules/mention.js +1093 -0
- package/lib/public/modules/notifications.js +613 -0
- package/lib/public/modules/playbook.js +578 -0
- package/lib/public/modules/profile.js +594 -0
- package/lib/public/modules/project-settings.js +849 -0
- package/lib/public/modules/project-switcher.js +375 -0
- package/lib/public/modules/qrcode.js +87 -0
- package/lib/public/modules/rewind.js +381 -0
- package/lib/public/modules/scheduler-config.js +1611 -0
- package/lib/public/modules/scheduler-history.js +131 -0
- package/lib/public/modules/scheduler.js +1689 -0
- package/lib/public/modules/server-settings.js +842 -0
- package/lib/public/modules/session-search.js +456 -0
- package/lib/public/modules/settings-defaults.js +253 -0
- package/lib/public/modules/sidebar-mobile.js +1425 -0
- package/lib/public/modules/sidebar-projects.js +2022 -0
- package/lib/public/modules/sidebar-sessions.js +1548 -0
- package/lib/public/modules/sidebar-users.js +449 -0
- package/lib/public/modules/sidebar.js +348 -0
- package/lib/public/modules/skills.js +793 -0
- package/lib/public/modules/state.js +3 -0
- package/lib/public/modules/sticky-notes.js +1221 -0
- package/lib/public/modules/store.js +34 -0
- package/lib/public/modules/stt.js +266 -0
- package/lib/public/modules/terminal.js +987 -0
- package/lib/public/modules/theme.js +812 -0
- package/lib/public/modules/tool-palette.js +562 -0
- package/lib/public/modules/tools.js +2285 -0
- package/lib/public/modules/tooltip.js +150 -0
- package/lib/public/modules/user-settings.js +544 -0
- package/lib/public/modules/utils.js +56 -0
- package/lib/public/modules/ws-ref.js +26 -0
- package/lib/public/style.css +34 -0
- package/lib/public/sw.js +159 -0
- package/lib/push.js +149 -0
- package/lib/scheduler.js +471 -0
- package/lib/sdk-bridge.js +1792 -0
- package/lib/sdk-message-processor.js +740 -0
- package/lib/sdk-message-queue.js +42 -0
- package/lib/sdk-skill-discovery.js +131 -0
- package/lib/sdk-worker.js +677 -0
- package/lib/server-admin.js +712 -0
- package/lib/server-auth.js +737 -0
- package/lib/server-dm.js +170 -0
- package/lib/server-palette.js +104 -0
- package/lib/server-settings.js +553 -0
- package/lib/server-skills.js +280 -0
- package/lib/server.js +1390 -0
- package/lib/session-search.js +693 -0
- package/lib/sessions.js +946 -0
- package/lib/smtp.js +221 -0
- package/lib/terminal-manager.js +219 -0
- package/lib/terminal.js +39 -0
- package/lib/themes/ayu-light.json +9 -0
- package/lib/themes/catppuccin-latte.json +9 -0
- package/lib/themes/catppuccin-mocha.json +9 -0
- package/lib/themes/clagentic-dark.json +10 -0
- package/lib/themes/clagentic-light.json +10 -0
- package/lib/themes/dracula.json +9 -0
- package/lib/themes/everforest-light.json +9 -0
- package/lib/themes/everforest.json +9 -0
- package/lib/themes/github-light.json +9 -0
- package/lib/themes/gruvbox-dark.json +9 -0
- package/lib/themes/gruvbox-light.json +9 -0
- package/lib/themes/monokai.json +9 -0
- package/lib/themes/nord-light.json +9 -0
- package/lib/themes/nord.json +9 -0
- package/lib/themes/one-dark.json +9 -0
- package/lib/themes/one-light.json +9 -0
- package/lib/themes/rose-pine-dawn.json +9 -0
- package/lib/themes/rose-pine.json +9 -0
- package/lib/themes/solarized-dark.json +9 -0
- package/lib/themes/solarized-light.json +9 -0
- package/lib/themes/tokyo-night-light.json +9 -0
- package/lib/themes/tokyo-night.json +9 -0
- package/lib/updater.js +124 -0
- package/lib/user-presence.js +92 -0
- package/lib/users-auth.js +146 -0
- package/lib/users-permissions.js +118 -0
- package/lib/users-preferences.js +254 -0
- package/lib/users.js +465 -0
- package/lib/utils.js +64 -0
- package/lib/worktree.js +134 -0
- package/lib/ws-schema.js +464 -0
- package/lib/yoke/adapters/claude-worker.js +759 -0
- package/lib/yoke/adapters/claude.js +1599 -0
- package/lib/yoke/adapters/codex.js +1559 -0
- package/lib/yoke/codex-app-server.js +352 -0
- package/lib/yoke/index.js +319 -0
- package/lib/yoke/instructions.js +61 -0
- package/lib/yoke/interface.js +98 -0
- package/lib/yoke/mcp-bridge-server.js +302 -0
- package/lib/yoke/package.json +7 -0
- package/package.json +72 -0
package/bin/cli.js
ADDED
|
@@ -0,0 +1,2812 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// --- Node version check (must run before any require that may use Node 20+ features) ---
|
|
4
|
+
var _nodeMajor = parseInt(process.versions.node.split(".")[0], 10);
|
|
5
|
+
if (_nodeMajor < 20) {
|
|
6
|
+
console.error("");
|
|
7
|
+
console.error("\x1b[31m[clay] Node.js 20+ is required (current: " + process.version + ")\x1b[0m");
|
|
8
|
+
console.error("[clay] The Claude Agent SDK 0.2.40+ requires Node 20 for Symbol.dispose support.");
|
|
9
|
+
console.error("");
|
|
10
|
+
console.error(" Upgrade Node: nvm install 22 && nvm use 22");
|
|
11
|
+
console.error("");
|
|
12
|
+
process.exit(78);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
var os = require("os");
|
|
16
|
+
var fs = require("fs");
|
|
17
|
+
var path = require("path");
|
|
18
|
+
var { execSync, execFileSync, spawn } = require("child_process");
|
|
19
|
+
var qrcode = require("qrcode-terminal");
|
|
20
|
+
var net = require("net");
|
|
21
|
+
|
|
22
|
+
// Detect dev mode — dev and prod use separate daemon files so they can run simultaneously
|
|
23
|
+
var _isDev = (process.argv[1] && (path.basename(process.argv[1]) === "clay-dev" || path.basename(process.argv[1]) === "clagentic-dev")) || process.argv.includes("--dev");
|
|
24
|
+
if (_isDev) {
|
|
25
|
+
process.env.CLAGENTIC_DEV = "1";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Preserve console output in dev/debug mode so logs remain readable
|
|
29
|
+
if (_isDev || process.argv.includes("--debug")) {
|
|
30
|
+
console.clear = function() {};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
var crypto = require("crypto");
|
|
34
|
+
var { loadConfig, saveConfig, configPath, socketPath, logPath, ensureConfigDir, isDaemonAlive, isDaemonAliveAsync, generateSlug, clearStaleConfig, loadClayrc, saveClayrc, readCrashInfo, REAL_HOME } = require("../lib/config");
|
|
35
|
+
var { sendIPCCommand } = require("../lib/ipc");
|
|
36
|
+
var { generateAuthToken } = require("../lib/server");
|
|
37
|
+
var { enableMultiUser, disableMultiUser, hasAdmin, isMultiUser, getSetupCode } = require("../lib/users");
|
|
38
|
+
|
|
39
|
+
function openUrl(url) {
|
|
40
|
+
try {
|
|
41
|
+
if (process.platform === "win32") {
|
|
42
|
+
spawn("cmd", ["/c", "start", "", url], { stdio: "ignore", detached: true, windowsHide: true }).unref();
|
|
43
|
+
} else {
|
|
44
|
+
var cmd = process.platform === "darwin" ? "open" : "xdg-open";
|
|
45
|
+
spawn(cmd, [url], { stdio: "ignore", detached: true }).unref();
|
|
46
|
+
}
|
|
47
|
+
} catch (e) {}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
var args = process.argv.slice(2);
|
|
51
|
+
var port = _isDev ? 2635 : 2633;
|
|
52
|
+
var useHttps = true;
|
|
53
|
+
var forceMkcert = false;
|
|
54
|
+
var forceBuiltin = false;
|
|
55
|
+
var skipUpdate = false;
|
|
56
|
+
var debugMode = false;
|
|
57
|
+
var autoYes = false;
|
|
58
|
+
var cliPin = null;
|
|
59
|
+
var shutdownMode = false;
|
|
60
|
+
var restartMode = false;
|
|
61
|
+
var addPath = null;
|
|
62
|
+
var removePath = null;
|
|
63
|
+
var listMode = false;
|
|
64
|
+
var dangerouslySkipPermissions = false;
|
|
65
|
+
var headlessMode = false;
|
|
66
|
+
var watchMode = false;
|
|
67
|
+
var host = null;
|
|
68
|
+
var multiUserMode = false;
|
|
69
|
+
var osUsersMode = false;
|
|
70
|
+
|
|
71
|
+
for (var i = 0; i < args.length; i++) {
|
|
72
|
+
if (args[i] === "-p" || args[i] === "--port") {
|
|
73
|
+
port = parseInt(args[i + 1], 10);
|
|
74
|
+
if (isNaN(port)) {
|
|
75
|
+
console.error("Invalid port number");
|
|
76
|
+
process.exit(1);
|
|
77
|
+
}
|
|
78
|
+
i++;
|
|
79
|
+
} else if (args[i] === "--host" || args[i] === "--bind") {
|
|
80
|
+
host = args[i + 1] || null;
|
|
81
|
+
i++;
|
|
82
|
+
} else if (args[i] === "--no-https") {
|
|
83
|
+
useHttps = false;
|
|
84
|
+
} else if (args[i] === "--local-cert") {
|
|
85
|
+
forceMkcert = true;
|
|
86
|
+
} else if (args[i] === "--builtin-cert") {
|
|
87
|
+
forceBuiltin = true;
|
|
88
|
+
} else if (args[i] === "--no-update" || args[i] === "--skip-update") {
|
|
89
|
+
skipUpdate = true;
|
|
90
|
+
} else if (args[i] === "--dev") {
|
|
91
|
+
// Already handled above for CLAGENTIC_HOME, just skip
|
|
92
|
+
} else if (args[i] === "--watch" || args[i] === "-w") {
|
|
93
|
+
watchMode = true;
|
|
94
|
+
} else if (args[i] === "--debug") {
|
|
95
|
+
debugMode = true;
|
|
96
|
+
} else if (args[i] === "-y" || args[i] === "--yes") {
|
|
97
|
+
autoYes = true;
|
|
98
|
+
} else if (args[i] === "--pin") {
|
|
99
|
+
cliPin = args[i + 1] || null;
|
|
100
|
+
i++;
|
|
101
|
+
} else if (args[i] === "--shutdown") {
|
|
102
|
+
shutdownMode = true;
|
|
103
|
+
} else if (args[i] === "--restart") {
|
|
104
|
+
restartMode = true;
|
|
105
|
+
} else if (args[i] === "--add") {
|
|
106
|
+
addPath = args[i + 1] || ".";
|
|
107
|
+
i++;
|
|
108
|
+
} else if (args[i] === "--remove") {
|
|
109
|
+
removePath = args[i + 1] || null;
|
|
110
|
+
i++;
|
|
111
|
+
} else if (args[i] === "--list") {
|
|
112
|
+
listMode = true;
|
|
113
|
+
} else if (args[i] === "--headless") {
|
|
114
|
+
headlessMode = true;
|
|
115
|
+
autoYes = true;
|
|
116
|
+
} else if (args[i] === "--dangerously-skip-permissions") {
|
|
117
|
+
dangerouslySkipPermissions = true;
|
|
118
|
+
} else if (args[i] === "--multi-user") {
|
|
119
|
+
multiUserMode = true;
|
|
120
|
+
} else if (args[i] === "--os-users") {
|
|
121
|
+
osUsersMode = true;
|
|
122
|
+
} else if (args[i] === "-h" || args[i] === "--help") {
|
|
123
|
+
console.log("Usage: clagentic [-p|--port <port>] [--host <address>] [--no-https] [--no-update] [--debug] [-y|--yes] [--pin <pin>] [--shutdown] [--restart]");
|
|
124
|
+
console.log(" clagentic --add <path> Add a project to the running daemon");
|
|
125
|
+
console.log(" clagentic --remove <path> Remove a project from the running daemon");
|
|
126
|
+
console.log(" clagentic --list List registered projects");
|
|
127
|
+
console.log("");
|
|
128
|
+
console.log("Options:");
|
|
129
|
+
console.log(" -p, --port <port> Port to listen on (default: 2633)");
|
|
130
|
+
console.log(" --host <address> Address to bind to (default: 0.0.0.0)");
|
|
131
|
+
console.log(" --no-https Disable HTTPS (enabled by default)");
|
|
132
|
+
console.log(" --local-cert Use local certificate (mkcert), suppress migration notice");
|
|
133
|
+
console.log(" --builtin-cert Use builtin certificate even if mkcert is installed");
|
|
134
|
+
console.log(" --no-update Skip auto-update check on startup");
|
|
135
|
+
console.log(" --debug Enable debug panel in the web UI");
|
|
136
|
+
console.log(" -y, --yes Skip interactive prompts (accept defaults)");
|
|
137
|
+
console.log(" --pin <pin> Set 6-digit PIN (use with --yes)");
|
|
138
|
+
console.log(" --shutdown Shut down the running relay daemon");
|
|
139
|
+
console.log(" --restart Restart the running relay daemon");
|
|
140
|
+
console.log(" --add <path> Add a project directory (use '.' for current)");
|
|
141
|
+
console.log(" --remove <path> Remove a project directory");
|
|
142
|
+
console.log(" --list List all registered projects");
|
|
143
|
+
console.log(" --headless Start daemon and exit immediately (implies --yes)");
|
|
144
|
+
console.log(" --multi-user Start in multi-user mode (use with --yes for headless)");
|
|
145
|
+
console.log(" --os-users Enable OS-level user isolation (Linux, requires root + --multi-user)");
|
|
146
|
+
console.log(" --dangerously-skip-permissions");
|
|
147
|
+
console.log(" Bypass all permission prompts");
|
|
148
|
+
process.exit(0);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Dev mode implies debug + skip update
|
|
153
|
+
if (_isDev) {
|
|
154
|
+
debugMode = true;
|
|
155
|
+
skipUpdate = true;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// --- Handle --shutdown before anything else ---
|
|
159
|
+
if (shutdownMode) {
|
|
160
|
+
var shutdownConfig = loadConfig();
|
|
161
|
+
isDaemonAliveAsync(shutdownConfig).then(function (alive) {
|
|
162
|
+
if (!alive) {
|
|
163
|
+
console.error("No running daemon found.");
|
|
164
|
+
process.exit(1);
|
|
165
|
+
}
|
|
166
|
+
sendIPCCommand(socketPath(), { cmd: "shutdown" }).then(function () {
|
|
167
|
+
console.log("Server stopped.");
|
|
168
|
+
clearStaleConfig();
|
|
169
|
+
process.exit(0);
|
|
170
|
+
}).catch(function (err) {
|
|
171
|
+
console.error("Shutdown failed:", err.message);
|
|
172
|
+
process.exit(1);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// --- Handle --restart before anything else ---
|
|
179
|
+
if (restartMode) {
|
|
180
|
+
var restartConfig = loadConfig();
|
|
181
|
+
isDaemonAliveAsync(restartConfig).then(function (alive) {
|
|
182
|
+
if (!alive) {
|
|
183
|
+
console.error("No running daemon found.");
|
|
184
|
+
process.exit(1);
|
|
185
|
+
}
|
|
186
|
+
sendIPCCommand(socketPath(), { cmd: "restart" }).then(function () {
|
|
187
|
+
console.log("Server restarted.");
|
|
188
|
+
process.exit(0);
|
|
189
|
+
}).catch(function (err) {
|
|
190
|
+
console.error("Restart failed:", err.message);
|
|
191
|
+
process.exit(1);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// --- Handle --add before anything else ---
|
|
198
|
+
if (addPath !== null) {
|
|
199
|
+
var absAdd = path.resolve(addPath);
|
|
200
|
+
try {
|
|
201
|
+
var stat = fs.statSync(absAdd);
|
|
202
|
+
if (!stat.isDirectory()) {
|
|
203
|
+
console.error("Not a directory: " + absAdd);
|
|
204
|
+
process.exit(1);
|
|
205
|
+
}
|
|
206
|
+
} catch (e) {
|
|
207
|
+
console.error("Directory not found: " + absAdd);
|
|
208
|
+
process.exit(1);
|
|
209
|
+
}
|
|
210
|
+
var addConfig = loadConfig();
|
|
211
|
+
isDaemonAliveAsync(addConfig).then(function (alive) {
|
|
212
|
+
if (!alive) {
|
|
213
|
+
console.error("No running daemon. Start with: npx @clagentic/console");
|
|
214
|
+
process.exit(1);
|
|
215
|
+
}
|
|
216
|
+
sendIPCCommand(socketPath(), { cmd: "add_project", path: absAdd }).then(function (res) {
|
|
217
|
+
if (res.ok) {
|
|
218
|
+
if (res.existing) {
|
|
219
|
+
console.log("Already registered: " + res.slug);
|
|
220
|
+
} else {
|
|
221
|
+
console.log("Added: " + res.slug + " \u2192 " + absAdd);
|
|
222
|
+
}
|
|
223
|
+
process.exit(0);
|
|
224
|
+
} else {
|
|
225
|
+
console.error("Failed: " + (res.error || "unknown error"));
|
|
226
|
+
process.exit(1);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// --- Handle --remove before anything else ---
|
|
234
|
+
if (removePath !== null) {
|
|
235
|
+
var absRemove = path.resolve(removePath);
|
|
236
|
+
var removeConfig = loadConfig();
|
|
237
|
+
isDaemonAliveAsync(removeConfig).then(function (alive) {
|
|
238
|
+
if (!alive) {
|
|
239
|
+
console.error("No running daemon. Start with: npx @clagentic/console");
|
|
240
|
+
process.exit(1);
|
|
241
|
+
}
|
|
242
|
+
sendIPCCommand(socketPath(), { cmd: "remove_project", path: absRemove }).then(function (res) {
|
|
243
|
+
if (res.ok) {
|
|
244
|
+
console.log("Removed: " + path.basename(absRemove));
|
|
245
|
+
process.exit(0);
|
|
246
|
+
} else {
|
|
247
|
+
console.error("Failed: " + (res.error || "project not found"));
|
|
248
|
+
process.exit(1);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// --- Handle --list before anything else ---
|
|
256
|
+
if (listMode) {
|
|
257
|
+
var listConfig = loadConfig();
|
|
258
|
+
isDaemonAliveAsync(listConfig).then(function (alive) {
|
|
259
|
+
if (!alive) {
|
|
260
|
+
console.error("No running daemon. Start with: npx @clagentic/console");
|
|
261
|
+
process.exit(1);
|
|
262
|
+
}
|
|
263
|
+
sendIPCCommand(socketPath(), { cmd: "get_status" }).then(function (res) {
|
|
264
|
+
if (!res.ok || !res.projects || res.projects.length === 0) {
|
|
265
|
+
console.log("No projects registered.");
|
|
266
|
+
process.exit(0);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
console.log("Projects (" + res.projects.length + "):\n");
|
|
270
|
+
for (var p = 0; p < res.projects.length; p++) {
|
|
271
|
+
var proj = res.projects[p];
|
|
272
|
+
var label = " " + proj.slug;
|
|
273
|
+
if (proj.title) label += " (" + proj.title + ")";
|
|
274
|
+
label += "\n " + proj.path;
|
|
275
|
+
console.log(label);
|
|
276
|
+
}
|
|
277
|
+
console.log("");
|
|
278
|
+
process.exit(0);
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// --multi-user / --os-users are now handled in the main entry flow (setup wizard or repeat run)
|
|
285
|
+
// Flags are parsed above and applied during forkDaemon()
|
|
286
|
+
|
|
287
|
+
var cwd = process.cwd();
|
|
288
|
+
|
|
289
|
+
// --- ANSI helpers ---
|
|
290
|
+
var isBasicTerm = process.env.TERM_PROGRAM === "Apple_Terminal";
|
|
291
|
+
var a = {
|
|
292
|
+
reset: "\x1b[0m",
|
|
293
|
+
bold: "\x1b[1m",
|
|
294
|
+
dim: "\x1b[2m",
|
|
295
|
+
clay: isBasicTerm ? "\x1b[34m" : "\x1b[38;2;88;87;252m", // #5857FC Indigo — active interaction
|
|
296
|
+
green: "\x1b[32m",
|
|
297
|
+
yellow: "\x1b[33m",
|
|
298
|
+
red: "\x1b[31m",
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
function gradient(text) {
|
|
302
|
+
if (isBasicTerm) {
|
|
303
|
+
return a.yellow + text + a.reset;
|
|
304
|
+
}
|
|
305
|
+
// Terracotta (#FE7150) → Warm brown (#D09558) — Clay earthy warmth
|
|
306
|
+
var r0 = 254, g0 = 113, b0 = 80;
|
|
307
|
+
var r1 = 208, g1 = 149, b1 = 88;
|
|
308
|
+
var out = "";
|
|
309
|
+
var len = text.length;
|
|
310
|
+
for (var i = 0; i < len; i++) {
|
|
311
|
+
var t = len > 1 ? i / (len - 1) : 0;
|
|
312
|
+
var r = Math.round(r0 + (r1 - r0) * t);
|
|
313
|
+
var g = Math.round(g0 + (g1 - g0) * t);
|
|
314
|
+
var b = Math.round(b0 + (b1 - b0) * t);
|
|
315
|
+
out += "\x1b[38;2;" + r + ";" + g + ";" + b + "m" + text[i];
|
|
316
|
+
}
|
|
317
|
+
return out + a.reset;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
var sym = {
|
|
321
|
+
pointer: a.clay + "◆" + a.reset,
|
|
322
|
+
done: a.green + "◇" + a.reset,
|
|
323
|
+
bar: a.dim + "│" + a.reset,
|
|
324
|
+
end: a.dim + "└" + a.reset,
|
|
325
|
+
warn: a.yellow + "▲" + a.reset,
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
function log(s) { console.log(" " + s); }
|
|
329
|
+
|
|
330
|
+
function clearUp(n) {
|
|
331
|
+
for (var i = 0; i < n; i++) {
|
|
332
|
+
process.stdout.write("\x1b[1A\x1b[2K");
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// --- Daemon watcher ---
|
|
337
|
+
// Polls daemon socket; if connection fails, the server is down.
|
|
338
|
+
var _daemonWatcher = null;
|
|
339
|
+
|
|
340
|
+
function startDaemonWatcher() {
|
|
341
|
+
if (_daemonWatcher) return;
|
|
342
|
+
_daemonWatcher = setInterval(function () {
|
|
343
|
+
var client = net.connect(socketPath());
|
|
344
|
+
var timer = setTimeout(function () {
|
|
345
|
+
client.destroy();
|
|
346
|
+
onDaemonDied();
|
|
347
|
+
}, 1500);
|
|
348
|
+
client.on("connect", function () {
|
|
349
|
+
clearTimeout(timer);
|
|
350
|
+
client.destroy();
|
|
351
|
+
});
|
|
352
|
+
client.on("error", function () {
|
|
353
|
+
clearTimeout(timer);
|
|
354
|
+
client.destroy();
|
|
355
|
+
onDaemonDied();
|
|
356
|
+
});
|
|
357
|
+
}, 3000);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function stopDaemonWatcher() {
|
|
361
|
+
if (_daemonWatcher) {
|
|
362
|
+
clearInterval(_daemonWatcher);
|
|
363
|
+
_daemonWatcher = null;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
var _restartAttempts = 0;
|
|
368
|
+
var MAX_RESTART_ATTEMPTS = 5;
|
|
369
|
+
var _restartBackoffStart = 0;
|
|
370
|
+
|
|
371
|
+
function onDaemonDied() {
|
|
372
|
+
stopDaemonWatcher();
|
|
373
|
+
// Clean up stdin in case a prompt is active
|
|
374
|
+
try {
|
|
375
|
+
process.stdin.setRawMode(false);
|
|
376
|
+
process.stdin.pause();
|
|
377
|
+
process.stdin.removeAllListeners("data");
|
|
378
|
+
} catch (e) {}
|
|
379
|
+
|
|
380
|
+
// Check if this was a crash (crash.json exists) vs intentional shutdown
|
|
381
|
+
var crashInfo = readCrashInfo();
|
|
382
|
+
if (!crashInfo) {
|
|
383
|
+
// Intentional shutdown, no restart
|
|
384
|
+
log("");
|
|
385
|
+
log(sym.warn + " " + a.yellow + "Server has been shut down." + a.reset);
|
|
386
|
+
log(a.dim + " Run " + a.reset + "npx @clagentic/console" + a.dim + " to start again." + a.reset);
|
|
387
|
+
log("");
|
|
388
|
+
process.exit(0);
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Reset backoff counter if enough time has passed since last restart burst
|
|
393
|
+
var now = Date.now();
|
|
394
|
+
if (_restartBackoffStart && now - _restartBackoffStart > 60000) {
|
|
395
|
+
_restartAttempts = 0;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
_restartAttempts++;
|
|
399
|
+
if (_restartAttempts > MAX_RESTART_ATTEMPTS) {
|
|
400
|
+
log("");
|
|
401
|
+
log(sym.warn + " " + a.red + "Server crashed too many times (" + MAX_RESTART_ATTEMPTS + " attempts). Giving up." + a.reset);
|
|
402
|
+
if (crashInfo.reason) {
|
|
403
|
+
log(a.dim + " " + crashInfo.reason.split("\n")[0] + a.reset);
|
|
404
|
+
}
|
|
405
|
+
log(a.dim + " Check logs: " + a.reset + logPath());
|
|
406
|
+
log("");
|
|
407
|
+
process.exit(1);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
if (_restartAttempts === 1) _restartBackoffStart = now;
|
|
412
|
+
|
|
413
|
+
log("");
|
|
414
|
+
log(sym.warn + " " + a.yellow + "Server crashed. Restarting... (" + _restartAttempts + "/" + MAX_RESTART_ATTEMPTS + ")" + a.reset);
|
|
415
|
+
if (crashInfo.reason) {
|
|
416
|
+
log(a.dim + " " + crashInfo.reason.split("\n")[0] + a.reset);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// Re-fork the daemon from saved config
|
|
420
|
+
restartDaemonFromConfig();
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
async function restartDaemonFromConfig() {
|
|
424
|
+
var lastConfig = loadConfig();
|
|
425
|
+
if (!lastConfig || !lastConfig.projects) {
|
|
426
|
+
log(a.red + " No config found. Cannot restart." + a.reset);
|
|
427
|
+
process.exit(1);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
clearStaleConfig();
|
|
432
|
+
|
|
433
|
+
// Wait for port to be released
|
|
434
|
+
var targetPort = lastConfig.port || port;
|
|
435
|
+
var waited = 0;
|
|
436
|
+
while (waited < 3000) {
|
|
437
|
+
var free = await isPortFree(targetPort);
|
|
438
|
+
if (free) break;
|
|
439
|
+
await new Promise(function (resolve) { setTimeout(resolve, 300); });
|
|
440
|
+
waited += 300;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// Rebuild config (preserve everything except pid)
|
|
444
|
+
var newConfig = Object.assign({}, lastConfig, {
|
|
445
|
+
pid: null,
|
|
446
|
+
port: targetPort,
|
|
447
|
+
projects: (lastConfig.projects || []).filter(function (p) {
|
|
448
|
+
return fs.existsSync(p.path);
|
|
449
|
+
})
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
ensureConfigDir();
|
|
453
|
+
saveConfig(newConfig);
|
|
454
|
+
|
|
455
|
+
var daemonScript = path.join(__dirname, "..", "lib", "daemon.js");
|
|
456
|
+
|
|
457
|
+
// Debug mode: run in foreground with logs to stdout
|
|
458
|
+
if (debugMode) {
|
|
459
|
+
process.env.CLAGENTIC_CONFIG = configPath();
|
|
460
|
+
newConfig.pid = process.pid;
|
|
461
|
+
saveConfig(newConfig);
|
|
462
|
+
require(daemonScript);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
var logFile = logPath();
|
|
467
|
+
var logFd = fs.openSync(logFile, "a");
|
|
468
|
+
|
|
469
|
+
var child = spawn(process.execPath, [daemonScript], {
|
|
470
|
+
detached: true,
|
|
471
|
+
windowsHide: true,
|
|
472
|
+
stdio: ["ignore", logFd, logFd],
|
|
473
|
+
env: Object.assign({}, process.env, {
|
|
474
|
+
CLAGENTIC_CONFIG: configPath(),
|
|
475
|
+
}),
|
|
476
|
+
});
|
|
477
|
+
child.unref();
|
|
478
|
+
fs.closeSync(logFd);
|
|
479
|
+
|
|
480
|
+
newConfig.pid = child.pid;
|
|
481
|
+
saveConfig(newConfig);
|
|
482
|
+
|
|
483
|
+
// Wait and verify (retry up to 5 seconds)
|
|
484
|
+
var alive = false;
|
|
485
|
+
for (var rc = 0; rc < 10; rc++) {
|
|
486
|
+
await new Promise(function (resolve) { setTimeout(resolve, 500); });
|
|
487
|
+
alive = await isDaemonAliveAsync(newConfig);
|
|
488
|
+
if (alive) break;
|
|
489
|
+
}
|
|
490
|
+
if (!alive) {
|
|
491
|
+
log(a.red + " Restart failed. Check logs: " + a.reset + logFile);
|
|
492
|
+
process.exit(1);
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
var ip = getLocalIP();
|
|
496
|
+
log(sym.done + " " + a.green + "Server restarted successfully." + a.reset);
|
|
497
|
+
log("");
|
|
498
|
+
showMainMenu(newConfig, ip);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// --- Network ---
|
|
502
|
+
function getLocalIP() {
|
|
503
|
+
var interfaces = os.networkInterfaces();
|
|
504
|
+
|
|
505
|
+
// Prefer Tailscale IP
|
|
506
|
+
for (var name in interfaces) {
|
|
507
|
+
if (/^(tailscale|utun)/.test(name)) {
|
|
508
|
+
for (var j = 0; j < interfaces[name].length; j++) {
|
|
509
|
+
var addr = interfaces[name][j];
|
|
510
|
+
if (addr.family === "IPv4" && !addr.internal && addr.address.startsWith("100.")) {
|
|
511
|
+
return addr.address;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// All interfaces for Tailscale CGNAT range
|
|
518
|
+
for (var addrs of Object.values(interfaces)) {
|
|
519
|
+
for (var k = 0; k < addrs.length; k++) {
|
|
520
|
+
if (addrs[k].family === "IPv4" && !addrs[k].internal && addrs[k].address.startsWith("100.")) {
|
|
521
|
+
return addrs[k].address;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// Fall back to LAN IP
|
|
527
|
+
for (var addrs2 of Object.values(interfaces)) {
|
|
528
|
+
for (var m = 0; m < addrs2.length; m++) {
|
|
529
|
+
if (addrs2[m].family === "IPv4" && !addrs2[m].internal) {
|
|
530
|
+
return addrs2[m].address;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
return "localhost";
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// --- Certs ---
|
|
539
|
+
function isRoutableIP(addr) {
|
|
540
|
+
if (addr.startsWith("10.")) return true;
|
|
541
|
+
if (addr.startsWith("192.168.")) return true;
|
|
542
|
+
if (addr.startsWith("100.")) {
|
|
543
|
+
var second = parseInt(addr.split(".")[1], 10);
|
|
544
|
+
return second >= 64 && second <= 127; // CGNAT (Tailscale)
|
|
545
|
+
}
|
|
546
|
+
if (addr.startsWith("172.")) {
|
|
547
|
+
var second = parseInt(addr.split(".")[1], 10);
|
|
548
|
+
return second >= 16 && second <= 31;
|
|
549
|
+
}
|
|
550
|
+
return false;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function getAllIPs() {
|
|
554
|
+
var ips = [];
|
|
555
|
+
var ifaces = os.networkInterfaces();
|
|
556
|
+
for (var addrs of Object.values(ifaces)) {
|
|
557
|
+
for (var j = 0; j < addrs.length; j++) {
|
|
558
|
+
if (addrs[j].family === "IPv4" && !addrs[j].internal && isRoutableIP(addrs[j].address)) {
|
|
559
|
+
ips.push(addrs[j].address);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return ips;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
function getBuiltinCert() {
|
|
567
|
+
try {
|
|
568
|
+
var certDir = path.join(__dirname, "..", "lib", "certs");
|
|
569
|
+
var keyPath = path.join(certDir, "privkey.pem");
|
|
570
|
+
var certPath = path.join(certDir, "fullchain.pem");
|
|
571
|
+
if (!fs.existsSync(keyPath) || !fs.existsSync(certPath)) return null;
|
|
572
|
+
|
|
573
|
+
// Check expiry
|
|
574
|
+
var certText = execFileSync("openssl", [
|
|
575
|
+
"x509", "-in", certPath, "-noout", "-enddate"
|
|
576
|
+
], { encoding: "utf8" });
|
|
577
|
+
var m = certText.match(/notAfter=(.+)/);
|
|
578
|
+
if (m) {
|
|
579
|
+
var expiry = new Date(m[1]);
|
|
580
|
+
var now = new Date();
|
|
581
|
+
// Skip if expiring within 7 days
|
|
582
|
+
if (expiry.getTime() - now.getTime() < 7 * 24 * 60 * 60 * 1000) return null;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
return { key: keyPath, cert: certPath, caRoot: null, builtin: true };
|
|
586
|
+
} catch (e) {
|
|
587
|
+
return null;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function toClayStudioUrl(ip, port, protocol) {
|
|
592
|
+
var dashed = ip.replace(/\./g, "-");
|
|
593
|
+
return protocol + "://" + dashed + ".d.clay.studio:" + port;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function ensureCerts(ip) {
|
|
597
|
+
// --builtin-cert: skip mkcert entirely, go straight to builtin
|
|
598
|
+
if (forceBuiltin) {
|
|
599
|
+
var builtin = getBuiltinCert();
|
|
600
|
+
if (builtin) return builtin;
|
|
601
|
+
return null;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
var certDir = path.join(process.env.CLAGENTIC_HOME || process.env.CLAY_HOME || path.join(REAL_HOME, ".clagentic"), "certs");
|
|
605
|
+
var keyPath = path.join(certDir, "key.pem");
|
|
606
|
+
var certPath = path.join(certDir, "cert.pem");
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
var mkcertInstalled = hasMkcert();
|
|
610
|
+
|
|
611
|
+
var caRoot = null;
|
|
612
|
+
if (mkcertInstalled) {
|
|
613
|
+
try {
|
|
614
|
+
caRoot = path.join(
|
|
615
|
+
execSync("mkcert -CAROOT", { encoding: "utf8" }).trim(),
|
|
616
|
+
"rootCA.pem"
|
|
617
|
+
);
|
|
618
|
+
if (!fs.existsSync(caRoot)) caRoot = null;
|
|
619
|
+
} catch (e) {}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// Collect all IPv4 addresses (Tailscale + LAN)
|
|
623
|
+
var allIPs = getAllIPs();
|
|
624
|
+
|
|
625
|
+
if (fs.existsSync(keyPath) && fs.existsSync(certPath)) {
|
|
626
|
+
var needRegen = false;
|
|
627
|
+
var isMkcertCert = false;
|
|
628
|
+
try {
|
|
629
|
+
var certText = execFileSync("openssl", ["x509", "-in", certPath, "-text", "-noout"], { encoding: "utf8" });
|
|
630
|
+
// If cert is from an external CA (e.g. Tailscale/Let's Encrypt), never regenerate
|
|
631
|
+
if (certText.indexOf("mkcert") === -1) return { key: keyPath, cert: certPath, caRoot: caRoot };
|
|
632
|
+
isMkcertCert = true;
|
|
633
|
+
for (var i = 0; i < allIPs.length; i++) {
|
|
634
|
+
if (certText.indexOf(allIPs[i]) === -1) {
|
|
635
|
+
needRegen = true;
|
|
636
|
+
break;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
} catch (e) { needRegen = true; }
|
|
640
|
+
// mkcert cert but mkcert uninstalled: CA is gone, cert is untrusted. Skip it.
|
|
641
|
+
if (isMkcertCert && !mkcertInstalled) needRegen = true;
|
|
642
|
+
if (!needRegen) {
|
|
643
|
+
return { key: keyPath, cert: certPath, caRoot: caRoot, mkcertDetected: mkcertInstalled && !forceMkcert };
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// mkcert installed: generate local cert (legacy behavior)
|
|
648
|
+
if (mkcertInstalled) {
|
|
649
|
+
fs.mkdirSync(certDir, { recursive: true });
|
|
650
|
+
|
|
651
|
+
var domains = ["localhost", "127.0.0.1", "::1"];
|
|
652
|
+
for (var i = 0; i < allIPs.length; i++) {
|
|
653
|
+
if (domains.indexOf(allIPs[i]) === -1) domains.push(allIPs[i]);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
try {
|
|
657
|
+
var mkcertArgs = ["-key-file", keyPath, "-cert-file", certPath].concat(domains);
|
|
658
|
+
execFileSync("mkcert", mkcertArgs, { stdio: "pipe" });
|
|
659
|
+
} catch (err) {
|
|
660
|
+
// mkcert generation failed, fall through to builtin
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
if (fs.existsSync(keyPath) && fs.existsSync(certPath)) {
|
|
664
|
+
return { key: keyPath, cert: certPath, caRoot: caRoot, mkcertDetected: !forceMkcert };
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// Fallback: builtin cert (unless --local-cert forces mkcert-only)
|
|
669
|
+
if (!forceMkcert) {
|
|
670
|
+
var builtin = getBuiltinCert();
|
|
671
|
+
if (builtin) return builtin;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
return null;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// --- Logo ---
|
|
678
|
+
function printLogo() {
|
|
679
|
+
var r = a.reset;
|
|
680
|
+
var lines = [
|
|
681
|
+
"________/\\\\\\\\\\\\\\\\\\__/\\\\\\_________________/\\\\\\\\\\\\\\\\\\_____/\\\\\\________/\\\\\\",
|
|
682
|
+
" _____/\\\\\\////////__\\/\\\\\\_______________/\\\\\\\\\\\\\\\\\\\\\\\\\\__\\///\\\\\\____/\\\\\\/_",
|
|
683
|
+
" ___/\\\\\\/___________\\/\\\\\\______________/\\\\\\/////////\\\\\\___\\///\\\\\\/\\\\\\/___",
|
|
684
|
+
" __/\\\\\\_____________\\/\\\\\\_____________\\/\\\\\\_______\\/\\\\\\_____\\///\\\\\\/_____",
|
|
685
|
+
" _\\/\\\\\\_____________\\/\\\\\\_____________\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\_______\\/\\\\\\______",
|
|
686
|
+
" _\\//\\\\\\____________\\/\\\\\\_____________\\/\\\\\\/////////\\\\\\_______\\/\\\\\\______",
|
|
687
|
+
" __\\///\\\\\\__________\\/\\\\\\_____________\\/\\\\\\_______\\/\\\\\\_______\\/\\\\\\______",
|
|
688
|
+
" ____\\////\\\\\\\\\\\\\\\\\\_\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\_\\/\\\\\\_______\\/\\\\\\_______\\/\\\\\\______",
|
|
689
|
+
" _______\\/////////__\\///////////////__\\///________\\///________\\///_______",
|
|
690
|
+
];
|
|
691
|
+
console.log("");
|
|
692
|
+
if (isBasicTerm) {
|
|
693
|
+
for (var i = 0; i < lines.length; i++) {
|
|
694
|
+
console.log(a.green + lines[i] + r);
|
|
695
|
+
}
|
|
696
|
+
return;
|
|
697
|
+
}
|
|
698
|
+
// Tri-accent vertical gradient: Green (#09E5A3) → Indigo (#5857FC) → Terracotta (#FE7150)
|
|
699
|
+
var stops = [
|
|
700
|
+
[9, 229, 163],
|
|
701
|
+
[88, 87, 252],
|
|
702
|
+
[254, 113, 80],
|
|
703
|
+
];
|
|
704
|
+
for (var i = 0; i < lines.length; i++) {
|
|
705
|
+
var t = lines.length > 1 ? i / (lines.length - 1) : 0;
|
|
706
|
+
var cr, cg, cb;
|
|
707
|
+
if (t <= 0.5) {
|
|
708
|
+
var s = t * 2;
|
|
709
|
+
cr = Math.round(stops[0][0] + (stops[1][0] - stops[0][0]) * s);
|
|
710
|
+
cg = Math.round(stops[0][1] + (stops[1][1] - stops[0][1]) * s);
|
|
711
|
+
cb = Math.round(stops[0][2] + (stops[1][2] - stops[0][2]) * s);
|
|
712
|
+
} else {
|
|
713
|
+
var s = (t - 0.5) * 2;
|
|
714
|
+
cr = Math.round(stops[1][0] + (stops[2][0] - stops[1][0]) * s);
|
|
715
|
+
cg = Math.round(stops[1][1] + (stops[2][1] - stops[1][1]) * s);
|
|
716
|
+
cb = Math.round(stops[1][2] + (stops[2][2] - stops[1][2]) * s);
|
|
717
|
+
}
|
|
718
|
+
console.log("\x1b[38;2;" + cr + ";" + cg + ";" + cb + "m" + lines[i] + r);
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
// --- Interactive prompts ---
|
|
723
|
+
function promptToggle(title, desc, defaultValue, callback) {
|
|
724
|
+
var value = defaultValue || false;
|
|
725
|
+
|
|
726
|
+
function renderToggle() {
|
|
727
|
+
var yes = value
|
|
728
|
+
? a.green + a.bold + "● Yes" + a.reset
|
|
729
|
+
: a.dim + "○ Yes" + a.reset;
|
|
730
|
+
var no = !value
|
|
731
|
+
? a.green + a.bold + "● No" + a.reset
|
|
732
|
+
: a.dim + "○ No" + a.reset;
|
|
733
|
+
return yes + a.dim + " / " + a.reset + no;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
var lines = 2;
|
|
737
|
+
log(sym.pointer + " " + a.bold + title + a.reset);
|
|
738
|
+
if (desc) {
|
|
739
|
+
log(sym.bar + " " + a.dim + desc + a.reset);
|
|
740
|
+
lines = 3;
|
|
741
|
+
}
|
|
742
|
+
process.stdout.write(" " + sym.bar + " " + renderToggle());
|
|
743
|
+
|
|
744
|
+
process.stdin.setRawMode(true);
|
|
745
|
+
process.stdin.resume();
|
|
746
|
+
process.stdin.setEncoding("utf8");
|
|
747
|
+
|
|
748
|
+
process.stdin.on("data", function onToggle(ch) {
|
|
749
|
+
if (ch === "\x1b[D" || ch === "\x1b[C" || ch === "\t") {
|
|
750
|
+
value = !value;
|
|
751
|
+
process.stdout.write("\x1b[2K\r " + sym.bar + " " + renderToggle());
|
|
752
|
+
} else if (ch === "y" || ch === "Y") {
|
|
753
|
+
value = true;
|
|
754
|
+
process.stdout.write("\x1b[2K\r " + sym.bar + " " + renderToggle());
|
|
755
|
+
} else if (ch === "n" || ch === "N") {
|
|
756
|
+
value = false;
|
|
757
|
+
process.stdout.write("\x1b[2K\r " + sym.bar + " " + renderToggle());
|
|
758
|
+
} else if (ch === "\r" || ch === "\n") {
|
|
759
|
+
process.stdin.setRawMode(false);
|
|
760
|
+
process.stdin.pause();
|
|
761
|
+
process.stdin.removeListener("data", onToggle);
|
|
762
|
+
process.stdout.write("\n");
|
|
763
|
+
clearUp(lines);
|
|
764
|
+
var result = value ? a.green + "Yes" + a.reset : a.dim + "No" + a.reset;
|
|
765
|
+
log(sym.done + " " + title + " " + a.dim + "·" + a.reset + " " + result);
|
|
766
|
+
callback(value);
|
|
767
|
+
} else if (ch === "\x03") {
|
|
768
|
+
process.stdout.write("\n");
|
|
769
|
+
clearUp(lines);
|
|
770
|
+
log(sym.end + " " + a.dim + "Cancelled" + a.reset);
|
|
771
|
+
process.exit(0);
|
|
772
|
+
}
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
function promptPin(callback) {
|
|
777
|
+
log(sym.pointer + " " + a.bold + "PIN protection" + a.reset);
|
|
778
|
+
log(sym.bar + " " + a.dim + "Require a 6-digit PIN to access the web UI. Enter to skip." + a.reset);
|
|
779
|
+
process.stdout.write(" " + sym.bar + " ");
|
|
780
|
+
|
|
781
|
+
var pin = "";
|
|
782
|
+
process.stdin.setRawMode(true);
|
|
783
|
+
process.stdin.resume();
|
|
784
|
+
process.stdin.setEncoding("utf8");
|
|
785
|
+
|
|
786
|
+
process.stdin.on("data", function onPin(ch) {
|
|
787
|
+
if (ch === "\r" || ch === "\n") {
|
|
788
|
+
process.stdin.setRawMode(false);
|
|
789
|
+
process.stdin.pause();
|
|
790
|
+
process.stdin.removeListener("data", onPin);
|
|
791
|
+
process.stdout.write("\n");
|
|
792
|
+
|
|
793
|
+
if (pin !== "" && !/^\d{6}$/.test(pin)) {
|
|
794
|
+
clearUp(3);
|
|
795
|
+
log(sym.done + " PIN protection " + a.red + "Must be exactly 6 digits" + a.reset);
|
|
796
|
+
log(sym.end);
|
|
797
|
+
process.exit(1);
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
clearUp(3);
|
|
802
|
+
if (pin) {
|
|
803
|
+
log(sym.done + " PIN protection " + a.dim + "·" + a.reset + " " + a.green + "Enabled" + a.reset);
|
|
804
|
+
} else {
|
|
805
|
+
log(sym.done + " PIN protection " + a.dim + "· Skipped" + a.reset);
|
|
806
|
+
}
|
|
807
|
+
log(sym.bar);
|
|
808
|
+
callback(pin || null);
|
|
809
|
+
} else if (ch === "\x03") {
|
|
810
|
+
process.stdout.write("\n");
|
|
811
|
+
clearUp(3);
|
|
812
|
+
log(sym.end + " " + a.dim + "Cancelled" + a.reset);
|
|
813
|
+
process.exit(0);
|
|
814
|
+
} else if (ch === "\x7f" || ch === "\b") {
|
|
815
|
+
if (pin.length > 0) {
|
|
816
|
+
pin = pin.slice(0, -1);
|
|
817
|
+
process.stdout.write("\b \b");
|
|
818
|
+
}
|
|
819
|
+
} else if (/\d/.test(ch) && pin.length < 6) {
|
|
820
|
+
pin += ch;
|
|
821
|
+
process.stdout.write(a.clay + "●" + a.reset);
|
|
822
|
+
}
|
|
823
|
+
});
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* Text input prompt with placeholder and Tab directory completion.
|
|
828
|
+
* title: prompt label, placeholder: dimmed hint, callback(value)
|
|
829
|
+
* Enter with empty input returns placeholder value.
|
|
830
|
+
* Tab completes directory paths.
|
|
831
|
+
*/
|
|
832
|
+
function promptText(title, placeholder, callback, opts) {
|
|
833
|
+
var prefix = " " + sym.bar + " ";
|
|
834
|
+
var hintLine = "";
|
|
835
|
+
var lineCount = 2;
|
|
836
|
+
var escHint = (!title || (opts && opts.noEsc)) ? "" : " " + a.dim + "(esc to go back)" + a.reset;
|
|
837
|
+
log(sym.pointer + " " + a.bold + title + a.reset + escHint);
|
|
838
|
+
process.stdout.write(prefix + a.dim + placeholder + a.reset);
|
|
839
|
+
// Move cursor to start of placeholder
|
|
840
|
+
process.stdout.write("\r" + prefix);
|
|
841
|
+
|
|
842
|
+
var text = "";
|
|
843
|
+
var showingPlaceholder = true;
|
|
844
|
+
process.stdin.setRawMode(true);
|
|
845
|
+
process.stdin.resume();
|
|
846
|
+
process.stdin.setEncoding("utf8");
|
|
847
|
+
|
|
848
|
+
function redrawInput() {
|
|
849
|
+
process.stdout.write("\x1b[2K\r" + prefix + text);
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
function clearHint() {
|
|
853
|
+
if (hintLine) {
|
|
854
|
+
// Erase the hint line below
|
|
855
|
+
process.stdout.write("\n\x1b[2K\x1b[1A");
|
|
856
|
+
hintLine = "";
|
|
857
|
+
lineCount = 2;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
function showHint(msg) {
|
|
862
|
+
clearHint();
|
|
863
|
+
hintLine = msg;
|
|
864
|
+
lineCount = 3;
|
|
865
|
+
// Print hint below, then move cursor back up
|
|
866
|
+
process.stdout.write("\n" + prefix + a.dim + msg + a.reset + "\x1b[1A");
|
|
867
|
+
redrawInput();
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
function tabComplete() {
|
|
871
|
+
var current = text || "";
|
|
872
|
+
if (!current) current = "/";
|
|
873
|
+
|
|
874
|
+
// Resolve ~ to home
|
|
875
|
+
if (current.charAt(0) === "~") {
|
|
876
|
+
current = REAL_HOME + current.substring(1);
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
var resolved = path.resolve(current);
|
|
880
|
+
var dir, partial;
|
|
881
|
+
|
|
882
|
+
try {
|
|
883
|
+
var st = fs.statSync(resolved);
|
|
884
|
+
if (st.isDirectory()) {
|
|
885
|
+
// Current text is a full directory — list its children
|
|
886
|
+
dir = resolved;
|
|
887
|
+
partial = "";
|
|
888
|
+
} else {
|
|
889
|
+
dir = path.dirname(resolved);
|
|
890
|
+
partial = path.basename(resolved);
|
|
891
|
+
}
|
|
892
|
+
} catch (e) {
|
|
893
|
+
// Path doesn't exist — complete from parent
|
|
894
|
+
dir = path.dirname(resolved);
|
|
895
|
+
partial = path.basename(resolved);
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
var entries;
|
|
899
|
+
try {
|
|
900
|
+
entries = fs.readdirSync(dir);
|
|
901
|
+
} catch (e) {
|
|
902
|
+
return; // Can't read directory
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
// Filter to directories only, matching partial prefix
|
|
906
|
+
var matches = [];
|
|
907
|
+
var lowerPartial = partial.toLowerCase();
|
|
908
|
+
for (var i = 0; i < entries.length; i++) {
|
|
909
|
+
if (entries[i].charAt(0) === "." && !partial.startsWith(".")) continue;
|
|
910
|
+
if (lowerPartial && entries[i].toLowerCase().indexOf(lowerPartial) !== 0) continue;
|
|
911
|
+
try {
|
|
912
|
+
var full = path.join(dir, entries[i]);
|
|
913
|
+
if (fs.statSync(full).isDirectory()) {
|
|
914
|
+
matches.push(entries[i]);
|
|
915
|
+
}
|
|
916
|
+
} catch (e) {}
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
if (matches.length === 0) return;
|
|
920
|
+
|
|
921
|
+
if (matches.length === 1) {
|
|
922
|
+
// Single match — complete it
|
|
923
|
+
var completed = path.join(dir, matches[0]) + path.sep;
|
|
924
|
+
text = completed;
|
|
925
|
+
showingPlaceholder = false;
|
|
926
|
+
clearHint();
|
|
927
|
+
redrawInput();
|
|
928
|
+
} else {
|
|
929
|
+
// Multiple matches — find longest common prefix and show candidates
|
|
930
|
+
var common = matches[0];
|
|
931
|
+
for (var m = 1; m < matches.length; m++) {
|
|
932
|
+
var k = 0;
|
|
933
|
+
while (k < common.length && k < matches[m].length && common.charAt(k) === matches[m].charAt(k)) k++;
|
|
934
|
+
common = common.substring(0, k);
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
if (common.length > partial.length) {
|
|
938
|
+
// Extend to common prefix
|
|
939
|
+
text = path.join(dir, common);
|
|
940
|
+
showingPlaceholder = false;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
// Show candidates as hint
|
|
944
|
+
var display = matches.slice(0, 6).join(" ");
|
|
945
|
+
if (matches.length > 6) display += " " + a.dim + "+" + (matches.length - 6) + " more" + a.reset;
|
|
946
|
+
showHint(display);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
process.stdin.on("data", function onText(ch) {
|
|
951
|
+
if (ch === "\r" || ch === "\n") {
|
|
952
|
+
process.stdin.setRawMode(false);
|
|
953
|
+
process.stdin.pause();
|
|
954
|
+
process.stdin.removeListener("data", onText);
|
|
955
|
+
var result = text || placeholder;
|
|
956
|
+
clearHint();
|
|
957
|
+
process.stdout.write("\n");
|
|
958
|
+
clearUp(2);
|
|
959
|
+
log(sym.done + " " + title + " " + a.dim + "·" + a.reset + " " + result);
|
|
960
|
+
callback(result);
|
|
961
|
+
} else if (ch === "\x1b" || ch === "\x03") {
|
|
962
|
+
process.stdin.setRawMode(false);
|
|
963
|
+
process.stdin.pause();
|
|
964
|
+
process.stdin.removeListener("data", onText);
|
|
965
|
+
clearHint();
|
|
966
|
+
process.stdout.write("\n");
|
|
967
|
+
clearUp(2);
|
|
968
|
+
if (ch === "\x03") {
|
|
969
|
+
log(sym.end + " " + a.dim + "Cancelled" + a.reset);
|
|
970
|
+
process.exit(0);
|
|
971
|
+
}
|
|
972
|
+
callback(null);
|
|
973
|
+
} else if (ch === "\t") {
|
|
974
|
+
if (showingPlaceholder) {
|
|
975
|
+
// Accept placeholder first
|
|
976
|
+
text = placeholder;
|
|
977
|
+
showingPlaceholder = false;
|
|
978
|
+
redrawInput();
|
|
979
|
+
}
|
|
980
|
+
tabComplete();
|
|
981
|
+
} else if (ch === "\x7f" || ch === "\b") {
|
|
982
|
+
if (text.length > 0) {
|
|
983
|
+
text = text.slice(0, -1);
|
|
984
|
+
clearHint();
|
|
985
|
+
if (text.length === 0) {
|
|
986
|
+
// Re-show placeholder
|
|
987
|
+
showingPlaceholder = true;
|
|
988
|
+
process.stdout.write("\x1b[2K\r" + prefix + a.dim + placeholder + a.reset);
|
|
989
|
+
process.stdout.write("\r" + prefix);
|
|
990
|
+
} else {
|
|
991
|
+
redrawInput();
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
} else if (ch >= " ") {
|
|
995
|
+
if (showingPlaceholder) {
|
|
996
|
+
showingPlaceholder = false;
|
|
997
|
+
}
|
|
998
|
+
clearHint();
|
|
999
|
+
text += ch;
|
|
1000
|
+
redrawInput();
|
|
1001
|
+
}
|
|
1002
|
+
});
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
/**
|
|
1006
|
+
* Select menu: arrow keys to navigate, enter to select.
|
|
1007
|
+
* items: [{ label, value, desc? }]
|
|
1008
|
+
*/
|
|
1009
|
+
function promptSelect(title, items, callback, opts) {
|
|
1010
|
+
var idx = 0;
|
|
1011
|
+
// Build hotkeys map: { key: handler }
|
|
1012
|
+
var hotkeys = {};
|
|
1013
|
+
if (opts && opts.key && opts.onKey) {
|
|
1014
|
+
hotkeys[opts.key] = opts.onKey;
|
|
1015
|
+
}
|
|
1016
|
+
if (opts && opts.keys) {
|
|
1017
|
+
for (var ki = 0; ki < opts.keys.length; ki++) {
|
|
1018
|
+
hotkeys[opts.keys[ki].key] = opts.keys[ki].onKey;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
var hintLines = null;
|
|
1022
|
+
if (opts && opts.hint) {
|
|
1023
|
+
hintLines = Array.isArray(opts.hint) ? opts.hint : [opts.hint];
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
function render() {
|
|
1027
|
+
var out = "";
|
|
1028
|
+
for (var i = 0; i < items.length; i++) {
|
|
1029
|
+
var prefix = i === idx
|
|
1030
|
+
? a.green + a.bold + " ● " + a.reset
|
|
1031
|
+
: a.dim + " ○ " + a.reset;
|
|
1032
|
+
out += " " + sym.bar + prefix + items[i].label + "\n";
|
|
1033
|
+
}
|
|
1034
|
+
return out;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
log(sym.pointer + " " + a.bold + title + a.reset);
|
|
1038
|
+
process.stdout.write(render());
|
|
1039
|
+
|
|
1040
|
+
// Render hint lines below the menu tree
|
|
1041
|
+
var hintBoxLines = 0;
|
|
1042
|
+
if (hintLines) {
|
|
1043
|
+
log(sym.end);
|
|
1044
|
+
for (var h = 0; h < hintLines.length; h++) {
|
|
1045
|
+
log(" " + gradient(hintLines[h]));
|
|
1046
|
+
}
|
|
1047
|
+
hintBoxLines = 1 + hintLines.length; // sym.end + lines
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
var lineCount = items.length + 1 + hintBoxLines;
|
|
1051
|
+
|
|
1052
|
+
process.stdin.setRawMode(true);
|
|
1053
|
+
process.stdin.resume();
|
|
1054
|
+
process.stdin.setEncoding("utf8");
|
|
1055
|
+
|
|
1056
|
+
process.stdin.on("data", function onSelect(ch) {
|
|
1057
|
+
if (ch === "\x1b[A") { // up
|
|
1058
|
+
if (idx > 0) idx--;
|
|
1059
|
+
} else if (ch === "\x1b[B") { // down
|
|
1060
|
+
if (idx < items.length - 1) idx++;
|
|
1061
|
+
} else if (ch === "\r" || ch === "\n") {
|
|
1062
|
+
process.stdin.setRawMode(false);
|
|
1063
|
+
process.stdin.pause();
|
|
1064
|
+
process.stdin.removeListener("data", onSelect);
|
|
1065
|
+
clearUp(lineCount);
|
|
1066
|
+
log(sym.done + " " + title + " " + a.dim + "·" + a.reset + " " + items[idx].label);
|
|
1067
|
+
callback(items[idx].value);
|
|
1068
|
+
return;
|
|
1069
|
+
} else if (ch === "\x03") {
|
|
1070
|
+
process.stdout.write("\n");
|
|
1071
|
+
process.exit(0);
|
|
1072
|
+
} else if (hotkeys[ch]) {
|
|
1073
|
+
process.stdin.setRawMode(false);
|
|
1074
|
+
process.stdin.pause();
|
|
1075
|
+
process.stdin.removeListener("data", onSelect);
|
|
1076
|
+
clearUp(lineCount);
|
|
1077
|
+
hotkeys[ch]();
|
|
1078
|
+
return;
|
|
1079
|
+
} else if (ch === "\x7f" || ch === "\b") {
|
|
1080
|
+
// Backspace — trigger "back" if available
|
|
1081
|
+
for (var bi = 0; bi < items.length; bi++) {
|
|
1082
|
+
if (items[bi].value === "back") {
|
|
1083
|
+
process.stdin.setRawMode(false);
|
|
1084
|
+
process.stdin.pause();
|
|
1085
|
+
process.stdin.removeListener("data", onSelect);
|
|
1086
|
+
clearUp(lineCount);
|
|
1087
|
+
log(sym.done + " " + title + " " + a.dim + "·" + a.reset + " " + items[bi].label);
|
|
1088
|
+
callback("back");
|
|
1089
|
+
return;
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
return;
|
|
1093
|
+
} else {
|
|
1094
|
+
return;
|
|
1095
|
+
}
|
|
1096
|
+
// Redraw
|
|
1097
|
+
clearUp(items.length + hintBoxLines);
|
|
1098
|
+
process.stdout.write(render());
|
|
1099
|
+
// Re-render hint lines
|
|
1100
|
+
if (hintLines) {
|
|
1101
|
+
log(sym.end);
|
|
1102
|
+
for (var rh = 0; rh < hintLines.length; rh++) {
|
|
1103
|
+
log(" " + gradient(hintLines[rh]));
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
});
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
/**
|
|
1110
|
+
* Multi-select menu: space to toggle, enter to confirm.
|
|
1111
|
+
* items: [{ label, value, checked? }]
|
|
1112
|
+
* callback(selectedValues[])
|
|
1113
|
+
*/
|
|
1114
|
+
function promptMultiSelect(title, items, callback) {
|
|
1115
|
+
var selected = [];
|
|
1116
|
+
for (var si = 0; si < items.length; si++) {
|
|
1117
|
+
selected.push(items[si].checked !== false);
|
|
1118
|
+
}
|
|
1119
|
+
var idx = 0;
|
|
1120
|
+
|
|
1121
|
+
function render() {
|
|
1122
|
+
var out = "";
|
|
1123
|
+
for (var i = 0; i < items.length; i++) {
|
|
1124
|
+
var cursor = i === idx ? a.clay + ">" + a.reset : " ";
|
|
1125
|
+
var check = selected[i]
|
|
1126
|
+
? a.green + a.bold + "■" + a.reset
|
|
1127
|
+
: a.dim + "□" + a.reset;
|
|
1128
|
+
out += " " + sym.bar + " " + cursor + " " + check + " " + items[i].label + "\n";
|
|
1129
|
+
}
|
|
1130
|
+
out += " " + sym.bar + " " + a.dim + "space: toggle · enter: confirm" + a.reset + "\n";
|
|
1131
|
+
return out;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
log(sym.pointer + " " + a.bold + title + a.reset);
|
|
1135
|
+
process.stdout.write(render());
|
|
1136
|
+
|
|
1137
|
+
var lineCount = items.length + 2; // title + items + hint
|
|
1138
|
+
|
|
1139
|
+
process.stdin.setRawMode(true);
|
|
1140
|
+
process.stdin.resume();
|
|
1141
|
+
process.stdin.setEncoding("utf8");
|
|
1142
|
+
|
|
1143
|
+
process.stdin.on("data", function onMulti(ch) {
|
|
1144
|
+
if (ch === "\x1b[A") { // up
|
|
1145
|
+
if (idx > 0) idx--;
|
|
1146
|
+
} else if (ch === "\x1b[B") { // down
|
|
1147
|
+
if (idx < items.length - 1) idx++;
|
|
1148
|
+
} else if (ch === " ") { // toggle
|
|
1149
|
+
selected[idx] = !selected[idx];
|
|
1150
|
+
} else if (ch === "a" || ch === "A") { // toggle all
|
|
1151
|
+
var allSelected = selected.every(function (s) { return s; });
|
|
1152
|
+
for (var ai = 0; ai < selected.length; ai++) selected[ai] = !allSelected;
|
|
1153
|
+
} else if (ch === "\r" || ch === "\n") {
|
|
1154
|
+
process.stdin.setRawMode(false);
|
|
1155
|
+
process.stdin.pause();
|
|
1156
|
+
process.stdin.removeListener("data", onMulti);
|
|
1157
|
+
clearUp(lineCount);
|
|
1158
|
+
var result = [];
|
|
1159
|
+
var labels = [];
|
|
1160
|
+
for (var ri = 0; ri < items.length; ri++) {
|
|
1161
|
+
if (selected[ri]) {
|
|
1162
|
+
result.push(items[ri].value);
|
|
1163
|
+
labels.push(items[ri].label);
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
var summary = result.length === items.length
|
|
1167
|
+
? "All (" + result.length + ")"
|
|
1168
|
+
: result.length + " of " + items.length;
|
|
1169
|
+
log(sym.done + " " + title + " " + a.dim + "·" + a.reset + " " + summary);
|
|
1170
|
+
callback(result);
|
|
1171
|
+
return;
|
|
1172
|
+
} else if (ch === "\x03") {
|
|
1173
|
+
process.stdout.write("\n");
|
|
1174
|
+
process.exit(0);
|
|
1175
|
+
} else if (ch === "\x1b") {
|
|
1176
|
+
// Escape — select none
|
|
1177
|
+
process.stdin.setRawMode(false);
|
|
1178
|
+
process.stdin.pause();
|
|
1179
|
+
process.stdin.removeListener("data", onMulti);
|
|
1180
|
+
clearUp(lineCount);
|
|
1181
|
+
log(sym.done + " " + title + " " + a.dim + "· Skipped" + a.reset);
|
|
1182
|
+
callback([]);
|
|
1183
|
+
return;
|
|
1184
|
+
} else {
|
|
1185
|
+
return;
|
|
1186
|
+
}
|
|
1187
|
+
// Redraw
|
|
1188
|
+
clearUp(items.length + 1); // items + hint (not title)
|
|
1189
|
+
process.stdout.write(render());
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
// --- Port availability ---
|
|
1194
|
+
|
|
1195
|
+
function isPortFree(p) {
|
|
1196
|
+
return new Promise(function (resolve) {
|
|
1197
|
+
var srv = net.createServer();
|
|
1198
|
+
srv.once("error", function () { resolve(false); });
|
|
1199
|
+
srv.once("listening", function () { srv.close(function () { resolve(true); }); });
|
|
1200
|
+
srv.listen(p);
|
|
1201
|
+
});
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
// --- Detect tools ---
|
|
1205
|
+
function getTailscaleIP() {
|
|
1206
|
+
var interfaces = os.networkInterfaces();
|
|
1207
|
+
for (var name in interfaces) {
|
|
1208
|
+
if (/^(tailscale|utun)/.test(name)) {
|
|
1209
|
+
for (var i = 0; i < interfaces[name].length; i++) {
|
|
1210
|
+
var addr = interfaces[name][i];
|
|
1211
|
+
if (addr.family === "IPv4" && !addr.internal && addr.address.startsWith("100.")) {
|
|
1212
|
+
return addr.address;
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
for (var addrs of Object.values(interfaces)) {
|
|
1218
|
+
for (var j = 0; j < addrs.length; j++) {
|
|
1219
|
+
if (addrs[j].family === "IPv4" && !addrs[j].internal && addrs[j].address.startsWith("100.")) {
|
|
1220
|
+
return addrs[j].address;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
return null;
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
function hasTailscale() {
|
|
1228
|
+
return getTailscaleIP() !== null;
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
function hasMkcert() {
|
|
1232
|
+
try {
|
|
1233
|
+
execSync("mkcert -CAROOT", { stdio: "pipe", encoding: "utf8" });
|
|
1234
|
+
return true;
|
|
1235
|
+
} catch (e) { return false; }
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
// ==============================
|
|
1239
|
+
// Restore projects from ~/.clayrc
|
|
1240
|
+
// ==============================
|
|
1241
|
+
function promptRestoreProjects(projects, callback) {
|
|
1242
|
+
log(sym.bar);
|
|
1243
|
+
log(sym.pointer + " " + a.bold + "Previous projects found" + a.reset);
|
|
1244
|
+
log(sym.bar + " " + a.dim + "Restore projects from your last session?" + a.reset);
|
|
1245
|
+
log(sym.bar);
|
|
1246
|
+
|
|
1247
|
+
var items = projects.map(function (p) {
|
|
1248
|
+
var name = p.title || path.basename(p.path);
|
|
1249
|
+
return {
|
|
1250
|
+
label: a.bold + name + a.reset + " " + a.dim + p.path + a.reset,
|
|
1251
|
+
value: p,
|
|
1252
|
+
checked: true,
|
|
1253
|
+
};
|
|
1254
|
+
});
|
|
1255
|
+
|
|
1256
|
+
promptMultiSelect("Restore projects", items, function (selected) {
|
|
1257
|
+
// Remove unselected projects from ~/.clayrc
|
|
1258
|
+
if (selected.length < projects.length) {
|
|
1259
|
+
var selectedPaths = {};
|
|
1260
|
+
for (var si = 0; si < selected.length; si++) {
|
|
1261
|
+
selectedPaths[selected[si].path] = true;
|
|
1262
|
+
}
|
|
1263
|
+
try {
|
|
1264
|
+
var rc = loadClayrc();
|
|
1265
|
+
rc.recentProjects = (rc.recentProjects || []).filter(function (p) {
|
|
1266
|
+
return selectedPaths[p.path];
|
|
1267
|
+
});
|
|
1268
|
+
saveClayrc(rc);
|
|
1269
|
+
} catch (e) {}
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
log(sym.bar);
|
|
1273
|
+
if (selected.length > 0) {
|
|
1274
|
+
log(sym.done + " " + a.green + "Restoring " + selected.length + (selected.length === 1 ? " project" : " projects") + a.reset);
|
|
1275
|
+
} else {
|
|
1276
|
+
log(sym.done + " " + a.dim + "Starting fresh" + a.reset);
|
|
1277
|
+
}
|
|
1278
|
+
log(sym.end + " " + a.dim + "Starting relay..." + a.reset);
|
|
1279
|
+
log("");
|
|
1280
|
+
callback(selected);
|
|
1281
|
+
});
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
// ==============================
|
|
1285
|
+
// First-run setup (no daemon)
|
|
1286
|
+
// ==============================
|
|
1287
|
+
function setup(callback) {
|
|
1288
|
+
console.clear();
|
|
1289
|
+
printLogo();
|
|
1290
|
+
log("");
|
|
1291
|
+
log(sym.pointer + " " + a.bold + "Clay" + a.reset + a.dim + " · Unofficial, open-source project" + a.reset);
|
|
1292
|
+
log(sym.bar);
|
|
1293
|
+
log(sym.bar + " " + a.yellow + sym.warn + " Disclaimer" + a.reset);
|
|
1294
|
+
log(sym.bar);
|
|
1295
|
+
log(sym.bar + " " + a.dim + "This is an independent project and is not affiliated with Anthropic." + a.reset);
|
|
1296
|
+
log(sym.bar + " " + a.dim + "Claude is a trademark of Anthropic." + a.reset);
|
|
1297
|
+
log(sym.bar);
|
|
1298
|
+
log(sym.bar + " " + a.dim + "Clay is provided \"as is\" without warranty of any kind. Users are" + a.reset);
|
|
1299
|
+
log(sym.bar + " " + a.dim + "responsible for complying with the terms of service of underlying AI" + a.reset);
|
|
1300
|
+
log(sym.bar + " " + a.dim + "providers (e.g., Anthropic, OpenAI) and all applicable terms of any" + a.reset);
|
|
1301
|
+
log(sym.bar + " " + a.dim + "third-party services." + a.reset);
|
|
1302
|
+
log(sym.bar);
|
|
1303
|
+
log(sym.bar + " " + a.dim + "Features such as multi-user mode are experimental and may involve" + a.reset);
|
|
1304
|
+
log(sym.bar + " " + a.dim + "sharing access to API-based services. Before enabling such features," + a.reset);
|
|
1305
|
+
log(sym.bar + " " + a.dim + "review your provider's usage policies regarding account sharing," + a.reset);
|
|
1306
|
+
log(sym.bar + " " + a.dim + "acceptable use, and any applicable rate limits or restrictions." + a.reset);
|
|
1307
|
+
log(sym.bar);
|
|
1308
|
+
log(sym.bar + " " + a.dim + "The authors assume no liability for misuse or violations arising" + a.reset);
|
|
1309
|
+
log(sym.bar + " " + a.dim + "from the use of this software." + a.reset);
|
|
1310
|
+
log(sym.bar);
|
|
1311
|
+
log(sym.bar + " Type " + a.bold + "agree" + a.reset + " to accept and continue.");
|
|
1312
|
+
log(sym.bar);
|
|
1313
|
+
|
|
1314
|
+
promptText("", "", function (val) {
|
|
1315
|
+
if (!val || val.trim().toLowerCase() !== "agree") {
|
|
1316
|
+
log(sym.end + " " + a.dim + "Aborted." + a.reset);
|
|
1317
|
+
log("");
|
|
1318
|
+
process.exit(0);
|
|
1319
|
+
return;
|
|
1320
|
+
}
|
|
1321
|
+
log(sym.bar);
|
|
1322
|
+
|
|
1323
|
+
function askPort() {
|
|
1324
|
+
promptText("Port", String(port), function (val) {
|
|
1325
|
+
if (val === null) {
|
|
1326
|
+
log(sym.end + " " + a.dim + "Aborted." + a.reset);
|
|
1327
|
+
log("");
|
|
1328
|
+
process.exit(0);
|
|
1329
|
+
return;
|
|
1330
|
+
}
|
|
1331
|
+
var p = parseInt(val, 10);
|
|
1332
|
+
if (!p || p < 1 || p > 65535) {
|
|
1333
|
+
log(sym.warn + " " + a.red + "Invalid port number" + a.reset);
|
|
1334
|
+
askPort();
|
|
1335
|
+
return;
|
|
1336
|
+
}
|
|
1337
|
+
isPortFree(p).then(function (free) {
|
|
1338
|
+
if (!free) {
|
|
1339
|
+
log(sym.warn + " " + a.yellow + "Port " + p + " is already in use" + a.reset);
|
|
1340
|
+
askPort();
|
|
1341
|
+
return;
|
|
1342
|
+
}
|
|
1343
|
+
port = p;
|
|
1344
|
+
log(sym.bar);
|
|
1345
|
+
askMode();
|
|
1346
|
+
});
|
|
1347
|
+
});
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
function askMode() {
|
|
1351
|
+
promptSelect("How will you use Clay?", [
|
|
1352
|
+
{ label: "Just me (single user)", value: "single" },
|
|
1353
|
+
{ label: "Multiple users", value: "multi" },
|
|
1354
|
+
], function (mode) {
|
|
1355
|
+
if (mode === "single") {
|
|
1356
|
+
finishSetup(mode, false);
|
|
1357
|
+
} else {
|
|
1358
|
+
askOsUsers(mode);
|
|
1359
|
+
}
|
|
1360
|
+
});
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
function askOsUsers(mode) {
|
|
1364
|
+
// Only offer OS user isolation on Linux
|
|
1365
|
+
if (process.platform !== "linux") {
|
|
1366
|
+
finishSetup(mode, false);
|
|
1367
|
+
return;
|
|
1368
|
+
}
|
|
1369
|
+
log(sym.bar);
|
|
1370
|
+
promptSelect("Enable OS-level user isolation?", [
|
|
1371
|
+
{ label: "Yes", value: "yes" },
|
|
1372
|
+
{ label: "No", value: "no" },
|
|
1373
|
+
], function (choice) {
|
|
1374
|
+
if (choice !== "yes") {
|
|
1375
|
+
finishSetup(mode, false);
|
|
1376
|
+
return;
|
|
1377
|
+
}
|
|
1378
|
+
log(sym.bar);
|
|
1379
|
+
log(sym.bar + " " + a.yellow + sym.warn + " OS-Level User Isolation" + a.reset);
|
|
1380
|
+
log(sym.bar);
|
|
1381
|
+
log(sym.bar + " " + a.dim + "This feature maps each Clay user to a Linux OS user account." + a.reset);
|
|
1382
|
+
log(sym.bar + " " + a.dim + "The daemon must run as root and will spawn processes (SDK workers," + a.reset);
|
|
1383
|
+
log(sym.bar + " " + a.dim + "terminals, file operations) as the mapped Linux user." + a.reset);
|
|
1384
|
+
log(sym.bar);
|
|
1385
|
+
log(sym.bar + " " + a.dim + "What this means:" + a.reset);
|
|
1386
|
+
log(sym.bar + " " + a.dim + "- Each mapped user uses their own ~/.claude/ credentials" + a.reset);
|
|
1387
|
+
log(sym.bar + " " + a.dim + "- Terminals and file access follow Linux permissions" + a.reset);
|
|
1388
|
+
log(sym.bar + " " + a.dim + "- Linux user accounts are created automatically (clay-username)" + a.reset);
|
|
1389
|
+
log(sym.bar);
|
|
1390
|
+
log(sym.bar + " " + a.dim + "Recommended: Run on a dedicated Clay server or cloud instance," + a.reset);
|
|
1391
|
+
log(sym.bar + " " + a.dim + "not on a personal computer or general-purpose server." + a.reset);
|
|
1392
|
+
log(sym.bar);
|
|
1393
|
+
promptSelect("Confirm", [
|
|
1394
|
+
{ label: "Enable OS-level user isolation", value: "confirm" },
|
|
1395
|
+
{ label: "Cancel", value: "cancel" },
|
|
1396
|
+
], function (confirmChoice) {
|
|
1397
|
+
if (confirmChoice !== "confirm") {
|
|
1398
|
+
finishSetup(mode, false);
|
|
1399
|
+
return;
|
|
1400
|
+
}
|
|
1401
|
+
var isRoot = typeof process.getuid === "function" && process.getuid() === 0;
|
|
1402
|
+
if (!isRoot) {
|
|
1403
|
+
// Merge into existing config (preserve projects, TLS, etc.)
|
|
1404
|
+
var existingCfg = loadConfig() || {};
|
|
1405
|
+
existingCfg.port = port;
|
|
1406
|
+
existingCfg.host = host;
|
|
1407
|
+
existingCfg.mode = "multi";
|
|
1408
|
+
existingCfg.osUsers = true;
|
|
1409
|
+
existingCfg.setupCompleted = true;
|
|
1410
|
+
if (dangerouslySkipPermissions) existingCfg.dangerouslySkipPermissions = true;
|
|
1411
|
+
saveConfig(existingCfg);
|
|
1412
|
+
log(sym.bar);
|
|
1413
|
+
log(sym.warn + " " + a.yellow + "OS user isolation requires root." + a.reset);
|
|
1414
|
+
log(sym.bar + " Run:");
|
|
1415
|
+
log(sym.bar + " " + a.bold + "sudo npx @clagentic/console" + a.reset);
|
|
1416
|
+
log(sym.end);
|
|
1417
|
+
log("");
|
|
1418
|
+
process.exit(0);
|
|
1419
|
+
return;
|
|
1420
|
+
}
|
|
1421
|
+
finishSetup(mode, true);
|
|
1422
|
+
});
|
|
1423
|
+
});
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
function finishSetup(mode, wantOsUsers) {
|
|
1427
|
+
if (process.platform === "darwin") {
|
|
1428
|
+
log(sym.bar);
|
|
1429
|
+
promptToggle("Keep awake", "Prevent system sleep while relay is running", false, function (keepAwake) {
|
|
1430
|
+
callback(mode, keepAwake, wantOsUsers);
|
|
1431
|
+
});
|
|
1432
|
+
} else {
|
|
1433
|
+
callback(mode, false, wantOsUsers);
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
askPort();
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
// ==============================
|
|
1442
|
+
// Fork the daemon process
|
|
1443
|
+
// ==============================
|
|
1444
|
+
async function forkDaemon(mode, keepAwake, extraProjects, addCwd, wantOsUsers) {
|
|
1445
|
+
var ip = getLocalIP();
|
|
1446
|
+
var hasTls = false;
|
|
1447
|
+
var hasBuiltinCert = false;
|
|
1448
|
+
var mkcertDetected = false;
|
|
1449
|
+
|
|
1450
|
+
if (useHttps) {
|
|
1451
|
+
var certPaths = ensureCerts(ip);
|
|
1452
|
+
if (certPaths) {
|
|
1453
|
+
hasTls = true;
|
|
1454
|
+
if (certPaths.builtin) hasBuiltinCert = true;
|
|
1455
|
+
if (certPaths.mkcertDetected) mkcertDetected = true;
|
|
1456
|
+
} else {
|
|
1457
|
+
log(sym.warn + " " + a.yellow + "HTTPS unavailable" + a.reset + a.dim + " · mkcert not installed" + a.reset);
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
// Check port availability
|
|
1462
|
+
var portFree = await isPortFree(port);
|
|
1463
|
+
if (!portFree) {
|
|
1464
|
+
log(a.red + "Port " + port + " is already in use." + a.reset);
|
|
1465
|
+
log(a.dim + "Is another Clay daemon running?" + a.reset);
|
|
1466
|
+
process.exit(1);
|
|
1467
|
+
return;
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
var allProjects = [];
|
|
1471
|
+
var usedSlugs = [];
|
|
1472
|
+
|
|
1473
|
+
// Load previous config to preserve per-project settings (visibility, allowedUsers)
|
|
1474
|
+
var prevConfig = loadConfig();
|
|
1475
|
+
var prevProjectMap = {};
|
|
1476
|
+
if (prevConfig && prevConfig.projects) {
|
|
1477
|
+
for (var pi = 0; pi < prevConfig.projects.length; pi++) {
|
|
1478
|
+
prevProjectMap[prevConfig.projects[pi].path] = prevConfig.projects[pi];
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
// Only include cwd if explicitly requested
|
|
1483
|
+
if (addCwd) {
|
|
1484
|
+
var slug = generateSlug(cwd, []);
|
|
1485
|
+
var cwdEntry = { path: cwd, slug: slug, addedAt: Date.now() };
|
|
1486
|
+
// Restore title/icon from .clayrc if available
|
|
1487
|
+
var cwdRc = loadClayrc();
|
|
1488
|
+
var cwdRecent = cwdRc.recentProjects || [];
|
|
1489
|
+
for (var cr = 0; cr < cwdRecent.length; cr++) {
|
|
1490
|
+
if (cwdRecent[cr].path === cwd) {
|
|
1491
|
+
if (cwdRecent[cr].title) cwdEntry.title = cwdRecent[cr].title;
|
|
1492
|
+
if (cwdRecent[cr].icon) cwdEntry.icon = cwdRecent[cr].icon;
|
|
1493
|
+
break;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
// Restore project-level settings from previous config
|
|
1497
|
+
if (prevProjectMap[cwd]) {
|
|
1498
|
+
cwdEntry = Object.assign({}, prevProjectMap[cwd], cwdEntry);
|
|
1499
|
+
}
|
|
1500
|
+
allProjects.push(cwdEntry);
|
|
1501
|
+
usedSlugs.push(slug);
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
// Add restored projects (from ~/.clayrc)
|
|
1505
|
+
if (extraProjects && extraProjects.length > 0) {
|
|
1506
|
+
for (var ep = 0; ep < extraProjects.length; ep++) {
|
|
1507
|
+
var rp = extraProjects[ep];
|
|
1508
|
+
if (rp.path === cwd) continue; // skip if same as cwd
|
|
1509
|
+
if (!fs.existsSync(rp.path)) continue; // skip missing directories
|
|
1510
|
+
var rpSlug = generateSlug(rp.path, usedSlugs);
|
|
1511
|
+
usedSlugs.push(rpSlug);
|
|
1512
|
+
var rpEntry = { path: rp.path, slug: rpSlug, title: rp.title || undefined, icon: rp.icon || undefined, addedAt: rp.addedAt || Date.now() };
|
|
1513
|
+
// Restore project-level settings from previous config
|
|
1514
|
+
if (prevProjectMap[rp.path]) {
|
|
1515
|
+
rpEntry = Object.assign({}, prevProjectMap[rp.path], rpEntry);
|
|
1516
|
+
}
|
|
1517
|
+
allProjects.push(rpEntry);
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
var config = Object.assign({}, prevConfig || {}, {
|
|
1522
|
+
pid: null,
|
|
1523
|
+
port: port,
|
|
1524
|
+
host: host,
|
|
1525
|
+
pinHash: mode === "multi" && cliPin ? generateAuthToken(cliPin) : (prevConfig && prevConfig.pinHash) || null,
|
|
1526
|
+
tls: hasTls,
|
|
1527
|
+
builtinCert: hasBuiltinCert,
|
|
1528
|
+
mkcertDetected: mkcertDetected,
|
|
1529
|
+
debug: debugMode,
|
|
1530
|
+
headless: headlessMode,
|
|
1531
|
+
keepAwake: keepAwake,
|
|
1532
|
+
dangerouslySkipPermissions: dangerouslySkipPermissions,
|
|
1533
|
+
osUsers: wantOsUsers || osUsersMode,
|
|
1534
|
+
mode: mode || "single",
|
|
1535
|
+
setupCompleted: true,
|
|
1536
|
+
projects: allProjects,
|
|
1537
|
+
});
|
|
1538
|
+
|
|
1539
|
+
ensureConfigDir();
|
|
1540
|
+
saveConfig(config);
|
|
1541
|
+
|
|
1542
|
+
// Fork daemon
|
|
1543
|
+
var daemonScript = path.join(__dirname, "..", "lib", "daemon.js");
|
|
1544
|
+
|
|
1545
|
+
// Debug mode: run in foreground with logs to stdout
|
|
1546
|
+
if (debugMode) {
|
|
1547
|
+
process.env.CLAGENTIC_CONFIG = configPath();
|
|
1548
|
+
config.pid = process.pid;
|
|
1549
|
+
saveConfig(config);
|
|
1550
|
+
require(daemonScript);
|
|
1551
|
+
return;
|
|
1552
|
+
}
|
|
1553
|
+
|
|
1554
|
+
var logFile = logPath();
|
|
1555
|
+
var logFd = fs.openSync(logFile, "a");
|
|
1556
|
+
|
|
1557
|
+
var child = spawn(process.execPath, [daemonScript], {
|
|
1558
|
+
detached: true,
|
|
1559
|
+
windowsHide: true,
|
|
1560
|
+
stdio: ["ignore", logFd, logFd],
|
|
1561
|
+
env: Object.assign({}, process.env, {
|
|
1562
|
+
CLAGENTIC_CONFIG: configPath(),
|
|
1563
|
+
}),
|
|
1564
|
+
});
|
|
1565
|
+
child.unref();
|
|
1566
|
+
fs.closeSync(logFd);
|
|
1567
|
+
|
|
1568
|
+
// Update config with PID
|
|
1569
|
+
config.pid = child.pid;
|
|
1570
|
+
saveConfig(config);
|
|
1571
|
+
|
|
1572
|
+
// Wait for daemon to start (retry up to 5 seconds)
|
|
1573
|
+
var alive = false;
|
|
1574
|
+
for (var attempt = 0; attempt < 10; attempt++) {
|
|
1575
|
+
await new Promise(function (resolve) { setTimeout(resolve, 500); });
|
|
1576
|
+
alive = await isDaemonAliveAsync(config);
|
|
1577
|
+
if (alive) break;
|
|
1578
|
+
}
|
|
1579
|
+
if (!alive) {
|
|
1580
|
+
log(a.red + "Failed to start daemon. Check logs:" + a.reset);
|
|
1581
|
+
log(a.dim + logFile + a.reset);
|
|
1582
|
+
clearStaleConfig();
|
|
1583
|
+
process.exit(1);
|
|
1584
|
+
return;
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
// Enable/disable multi-user mode based on startup config
|
|
1588
|
+
var _pendingSetupCode = null;
|
|
1589
|
+
if (config.mode === "multi") {
|
|
1590
|
+
var muResult = enableMultiUser();
|
|
1591
|
+
if (muResult.setupCode) {
|
|
1592
|
+
_pendingSetupCode = muResult.setupCode;
|
|
1593
|
+
}
|
|
1594
|
+
} else if (isMultiUser()) {
|
|
1595
|
+
disableMultiUser();
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
// Headless mode — print status and exit immediately
|
|
1599
|
+
if (headlessMode) {
|
|
1600
|
+
var protocol = config.tls ? "https" : "http";
|
|
1601
|
+
var url = config.builtinCert
|
|
1602
|
+
? toClayStudioUrl(ip, config.port, protocol)
|
|
1603
|
+
: protocol + "://" + ip + ":" + config.port;
|
|
1604
|
+
console.log(" " + sym.done + " Daemon started (PID " + config.pid + ")");
|
|
1605
|
+
console.log(" " + sym.done + " " + url);
|
|
1606
|
+
if (config.builtinCert) console.log(" " + sym.done + " d.clay.studio provides HTTPS certificates only. Your traffic never leaves your network.");
|
|
1607
|
+
if (config.mkcertDetected) console.log(" " + sym.warn + " Clay now ships with a builtin HTTPS certificate. To use it, pass --builtin-cert or uninstall mkcert.");
|
|
1608
|
+
if (_pendingSetupCode) {
|
|
1609
|
+
console.log("");
|
|
1610
|
+
console.log(" " + sym.done + " " + a.green + "Multi-user mode enabled." + a.reset);
|
|
1611
|
+
console.log(" " + sym.bar + " Setup code: " + a.bold + _pendingSetupCode + a.reset);
|
|
1612
|
+
console.log(" " + sym.bar + " Open Clay in your browser and enter this code to create the admin account.");
|
|
1613
|
+
}
|
|
1614
|
+
console.log(" " + sym.done + " Headless mode — exiting CLI");
|
|
1615
|
+
process.exit(0);
|
|
1616
|
+
return;
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
// Show success + QR
|
|
1620
|
+
showServerStarted(config, ip, _pendingSetupCode);
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1623
|
+
// ==============================
|
|
1624
|
+
// Dev mode — foreground daemon with file watching
|
|
1625
|
+
// ==============================
|
|
1626
|
+
async function devMode(mode, keepAwake, existingPinHash, wantOsUsers) {
|
|
1627
|
+
var ip = getLocalIP();
|
|
1628
|
+
var hasTls = false;
|
|
1629
|
+
var hasBuiltinCert = false;
|
|
1630
|
+
var mkcertDetected = false;
|
|
1631
|
+
|
|
1632
|
+
if (useHttps) {
|
|
1633
|
+
var certPaths = ensureCerts(ip);
|
|
1634
|
+
if (certPaths) {
|
|
1635
|
+
hasTls = true;
|
|
1636
|
+
if (certPaths.builtin) hasBuiltinCert = true;
|
|
1637
|
+
if (certPaths.mkcertDetected) mkcertDetected = true;
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
var portFree = await isPortFree(port);
|
|
1642
|
+
if (!portFree) {
|
|
1643
|
+
console.log("\x1b[31m[dev] Port " + port + " is already in use.\x1b[0m");
|
|
1644
|
+
process.exit(1);
|
|
1645
|
+
return;
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
var slug = generateSlug(cwd, []);
|
|
1649
|
+
var cwdDevEntry = { path: cwd, slug: slug, addedAt: Date.now() };
|
|
1650
|
+
|
|
1651
|
+
// Load previous config to preserve per-project settings (visibility, allowedUsers)
|
|
1652
|
+
var prevDevConfig = loadConfig();
|
|
1653
|
+
var prevDevProjectMap = {};
|
|
1654
|
+
if (prevDevConfig && prevDevConfig.projects) {
|
|
1655
|
+
for (var pdi = 0; pdi < prevDevConfig.projects.length; pdi++) {
|
|
1656
|
+
prevDevProjectMap[prevDevConfig.projects[pdi].path] = prevDevConfig.projects[pdi];
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
// Restore previous projects
|
|
1661
|
+
var rc = loadClayrc();
|
|
1662
|
+
var restorable = (rc.recentProjects || []).filter(function (p) {
|
|
1663
|
+
return p.path !== cwd && fs.existsSync(p.path);
|
|
1664
|
+
});
|
|
1665
|
+
// Restore title/icon for cwd from .clayrc
|
|
1666
|
+
var rcAll = rc.recentProjects || [];
|
|
1667
|
+
for (var ci = 0; ci < rcAll.length; ci++) {
|
|
1668
|
+
if (rcAll[ci].path === cwd) {
|
|
1669
|
+
if (rcAll[ci].title) cwdDevEntry.title = rcAll[ci].title;
|
|
1670
|
+
if (rcAll[ci].icon) cwdDevEntry.icon = rcAll[ci].icon;
|
|
1671
|
+
break;
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
// Restore access settings for cwd from previous config
|
|
1675
|
+
if (prevDevProjectMap[cwd]) {
|
|
1676
|
+
cwdDevEntry = Object.assign({}, prevDevProjectMap[cwd], cwdDevEntry);
|
|
1677
|
+
}
|
|
1678
|
+
var allProjects = [cwdDevEntry];
|
|
1679
|
+
var usedSlugs = [slug];
|
|
1680
|
+
for (var ri = 0; ri < restorable.length; ri++) {
|
|
1681
|
+
var rp = restorable[ri];
|
|
1682
|
+
var rpSlug = generateSlug(rp.path, usedSlugs);
|
|
1683
|
+
usedSlugs.push(rpSlug);
|
|
1684
|
+
var rpDevEntry = { path: rp.path, slug: rpSlug, title: rp.title || undefined, icon: rp.icon || undefined, addedAt: rp.addedAt || Date.now() };
|
|
1685
|
+
// Restore project-level settings from previous config
|
|
1686
|
+
if (prevDevProjectMap[rp.path]) {
|
|
1687
|
+
rpDevEntry = Object.assign({}, prevDevProjectMap[rp.path], rpDevEntry);
|
|
1688
|
+
}
|
|
1689
|
+
allProjects.push(rpDevEntry);
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
var config = Object.assign({}, prevDevConfig || {}, {
|
|
1693
|
+
pid: null,
|
|
1694
|
+
port: port,
|
|
1695
|
+
host: host,
|
|
1696
|
+
pinHash: existingPinHash || null,
|
|
1697
|
+
tls: hasTls,
|
|
1698
|
+
builtinCert: hasBuiltinCert,
|
|
1699
|
+
mkcertDetected: mkcertDetected,
|
|
1700
|
+
debug: true,
|
|
1701
|
+
keepAwake: keepAwake || false,
|
|
1702
|
+
dangerouslySkipPermissions: dangerouslySkipPermissions,
|
|
1703
|
+
mode: mode || "single",
|
|
1704
|
+
setupCompleted: true,
|
|
1705
|
+
projects: allProjects,
|
|
1706
|
+
osUsers: wantOsUsers || (prevDevConfig ? (prevDevConfig.osUsers || false) : false),
|
|
1707
|
+
});
|
|
1708
|
+
|
|
1709
|
+
ensureConfigDir();
|
|
1710
|
+
saveConfig(config);
|
|
1711
|
+
|
|
1712
|
+
// Enable/disable multi-user mode based on startup config
|
|
1713
|
+
if (config.mode === "multi") {
|
|
1714
|
+
var muResult = enableMultiUser();
|
|
1715
|
+
if (muResult.setupCode) {
|
|
1716
|
+
console.log("\x1b[38;2;0;183;133m[dev]\x1b[0m Multi-user mode enabled. Setup code: " + muResult.setupCode);
|
|
1717
|
+
}
|
|
1718
|
+
} else if (isMultiUser()) {
|
|
1719
|
+
disableMultiUser();
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
var daemonScript = path.join(__dirname, "..", "lib", "daemon.js");
|
|
1723
|
+
var libDir = path.join(__dirname, "..", "lib");
|
|
1724
|
+
var child = null;
|
|
1725
|
+
var intentionalKill = false;
|
|
1726
|
+
var debounceTimer = null;
|
|
1727
|
+
|
|
1728
|
+
function spawnDaemon() {
|
|
1729
|
+
child = spawn(process.execPath, [daemonScript], {
|
|
1730
|
+
stdio: ["ignore", "inherit", "inherit"],
|
|
1731
|
+
env: Object.assign({}, process.env, {
|
|
1732
|
+
CLAGENTIC_CONFIG: configPath(),
|
|
1733
|
+
}),
|
|
1734
|
+
});
|
|
1735
|
+
|
|
1736
|
+
child.on("exit", function (code) {
|
|
1737
|
+
child = null;
|
|
1738
|
+
if (intentionalKill) {
|
|
1739
|
+
intentionalKill = false;
|
|
1740
|
+
return;
|
|
1741
|
+
}
|
|
1742
|
+
// Exit code 120 = update restart — respawn daemon with current dev code
|
|
1743
|
+
if (code === 120) {
|
|
1744
|
+
console.log("\x1b[38;2;0;183;133m[dev]\x1b[0m Update restart — respawning daemon...");
|
|
1745
|
+
console.log("");
|
|
1746
|
+
setTimeout(spawnDaemon, 500);
|
|
1747
|
+
return;
|
|
1748
|
+
}
|
|
1749
|
+
// Exit code 78 = fatal config error (e.g. Node version too old) — don't restart
|
|
1750
|
+
if (code === 78) {
|
|
1751
|
+
console.log("\x1b[31m[dev] Daemon exited with fatal error (code 78). Not restarting.\x1b[0m");
|
|
1752
|
+
process.exit(78);
|
|
1753
|
+
return;
|
|
1754
|
+
}
|
|
1755
|
+
// Unexpected exit — auto restart
|
|
1756
|
+
console.log("\x1b[33m[dev] Daemon exited (code " + code + "), restarting...\x1b[0m");
|
|
1757
|
+
setTimeout(spawnDaemon, 500);
|
|
1758
|
+
});
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1761
|
+
function restartDaemon() {
|
|
1762
|
+
intentionalKill = true;
|
|
1763
|
+
if (child) {
|
|
1764
|
+
child.kill("SIGTERM");
|
|
1765
|
+
// Give it a moment to shut down, then spawn
|
|
1766
|
+
setTimeout(spawnDaemon, 300);
|
|
1767
|
+
} else {
|
|
1768
|
+
intentionalKill = false;
|
|
1769
|
+
spawnDaemon();
|
|
1770
|
+
}
|
|
1771
|
+
}
|
|
1772
|
+
|
|
1773
|
+
console.log("\x1b[38;2;0;183;133m[dev]\x1b[0m Starting relay on port " + port + "...");
|
|
1774
|
+
if (watchMode) {
|
|
1775
|
+
console.log("\x1b[38;2;0;183;133m[dev]\x1b[0m Watching lib/ for changes (excluding lib/public/)");
|
|
1776
|
+
}
|
|
1777
|
+
console.log("");
|
|
1778
|
+
|
|
1779
|
+
spawnDaemon();
|
|
1780
|
+
|
|
1781
|
+
// Wait for daemon to be ready, then show CLI menu
|
|
1782
|
+
config.pid = child ? child.pid : null;
|
|
1783
|
+
saveConfig(config);
|
|
1784
|
+
|
|
1785
|
+
var daemonReady = false;
|
|
1786
|
+
for (var da = 0; da < 10; da++) {
|
|
1787
|
+
await new Promise(function (resolve) { setTimeout(resolve, 500); });
|
|
1788
|
+
daemonReady = await isDaemonAliveAsync(config);
|
|
1789
|
+
if (daemonReady) break;
|
|
1790
|
+
}
|
|
1791
|
+
if (daemonReady) {
|
|
1792
|
+
showServerStarted(config, ip);
|
|
1793
|
+
}
|
|
1794
|
+
|
|
1795
|
+
// Watch lib/ for server-side file changes (only with --watch)
|
|
1796
|
+
var watcher = null;
|
|
1797
|
+
if (watchMode) {
|
|
1798
|
+
watcher = fs.watch(libDir, { recursive: true }, function (eventType, filename) {
|
|
1799
|
+
if (!filename) return;
|
|
1800
|
+
// Skip client-side files — they're served from disk
|
|
1801
|
+
if (filename.startsWith("public" + path.sep) || filename.startsWith("public/")) return;
|
|
1802
|
+
// Skip non-JS files
|
|
1803
|
+
if (!filename.endsWith(".js")) return;
|
|
1804
|
+
|
|
1805
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
1806
|
+
debounceTimer = setTimeout(function () {
|
|
1807
|
+
console.log("\x1b[38;2;0;183;133m[dev]\x1b[0m File changed: lib/" + filename);
|
|
1808
|
+
console.log("\x1b[38;2;0;183;133m[dev]\x1b[0m Restarting...");
|
|
1809
|
+
console.log("");
|
|
1810
|
+
restartDaemon();
|
|
1811
|
+
}, 300);
|
|
1812
|
+
});
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
// Clean exit on Ctrl+C
|
|
1816
|
+
var shuttingDown = false;
|
|
1817
|
+
process.on("SIGINT", function () {
|
|
1818
|
+
if (shuttingDown) return;
|
|
1819
|
+
shuttingDown = true;
|
|
1820
|
+
console.log("\n\x1b[38;2;0;183;133m[dev]\x1b[0m Shutting down...");
|
|
1821
|
+
if (watcher) watcher.close();
|
|
1822
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
1823
|
+
intentionalKill = true;
|
|
1824
|
+
if (child) {
|
|
1825
|
+
child.kill("SIGTERM");
|
|
1826
|
+
child.on("exit", function () {
|
|
1827
|
+
clearStaleConfig();
|
|
1828
|
+
process.exit(0);
|
|
1829
|
+
});
|
|
1830
|
+
// Force kill after 3s
|
|
1831
|
+
setTimeout(function () { process.exit(0); }, 3000);
|
|
1832
|
+
} else {
|
|
1833
|
+
clearStaleConfig();
|
|
1834
|
+
process.exit(0);
|
|
1835
|
+
}
|
|
1836
|
+
});
|
|
1837
|
+
}
|
|
1838
|
+
|
|
1839
|
+
// ==============================
|
|
1840
|
+
// Restart daemon with TLS enabled
|
|
1841
|
+
// ==============================
|
|
1842
|
+
async function restartDaemonWithTLS(config, callback) {
|
|
1843
|
+
var ip = getLocalIP();
|
|
1844
|
+
var certPaths = ensureCerts(ip);
|
|
1845
|
+
if (!certPaths) {
|
|
1846
|
+
callback(config);
|
|
1847
|
+
return;
|
|
1848
|
+
}
|
|
1849
|
+
var hasBuiltinCert = !!(certPaths && certPaths.builtin);
|
|
1850
|
+
var mkcertDetected = !!(certPaths && certPaths.mkcertDetected);
|
|
1851
|
+
|
|
1852
|
+
// Shut down old daemon
|
|
1853
|
+
stopDaemonWatcher();
|
|
1854
|
+
try {
|
|
1855
|
+
await sendIPCCommand(socketPath(), { cmd: "shutdown" });
|
|
1856
|
+
} catch (e) {}
|
|
1857
|
+
|
|
1858
|
+
// Wait for port to be released
|
|
1859
|
+
var waited = 0;
|
|
1860
|
+
while (waited < 5000) {
|
|
1861
|
+
await new Promise(function (resolve) { setTimeout(resolve, 300); });
|
|
1862
|
+
waited += 300;
|
|
1863
|
+
var free = await isPortFree(config.port);
|
|
1864
|
+
if (free) break;
|
|
1865
|
+
}
|
|
1866
|
+
clearStaleConfig();
|
|
1867
|
+
|
|
1868
|
+
// Re-fork with TLS (preserve all existing config fields)
|
|
1869
|
+
var newConfig = Object.assign({}, config, {
|
|
1870
|
+
pid: null,
|
|
1871
|
+
tls: true,
|
|
1872
|
+
builtinCert: hasBuiltinCert,
|
|
1873
|
+
mkcertDetected: mkcertDetected,
|
|
1874
|
+
});
|
|
1875
|
+
|
|
1876
|
+
ensureConfigDir();
|
|
1877
|
+
saveConfig(newConfig);
|
|
1878
|
+
|
|
1879
|
+
var daemonScript = path.join(__dirname, "..", "lib", "daemon.js");
|
|
1880
|
+
var logFile = logPath();
|
|
1881
|
+
var logFd = fs.openSync(logFile, "a");
|
|
1882
|
+
|
|
1883
|
+
var child = spawn(process.execPath, [daemonScript], {
|
|
1884
|
+
detached: true,
|
|
1885
|
+
windowsHide: true,
|
|
1886
|
+
stdio: ["ignore", logFd, logFd],
|
|
1887
|
+
env: Object.assign({}, process.env, {
|
|
1888
|
+
CLAGENTIC_CONFIG: configPath(),
|
|
1889
|
+
}),
|
|
1890
|
+
});
|
|
1891
|
+
child.unref();
|
|
1892
|
+
fs.closeSync(logFd);
|
|
1893
|
+
|
|
1894
|
+
newConfig.pid = child.pid;
|
|
1895
|
+
saveConfig(newConfig);
|
|
1896
|
+
|
|
1897
|
+
var alive = false;
|
|
1898
|
+
for (var ra = 0; ra < 10; ra++) {
|
|
1899
|
+
await new Promise(function (resolve) { setTimeout(resolve, 500); });
|
|
1900
|
+
alive = await isDaemonAliveAsync(newConfig);
|
|
1901
|
+
if (alive) break;
|
|
1902
|
+
}
|
|
1903
|
+
if (!alive) {
|
|
1904
|
+
log(sym.warn + " " + a.yellow + "Failed to restart with HTTPS, falling back to HTTP..." + a.reset);
|
|
1905
|
+
// Re-fork without TLS so the server is at least running
|
|
1906
|
+
newConfig.tls = false;
|
|
1907
|
+
saveConfig(newConfig);
|
|
1908
|
+
var logFd2 = fs.openSync(logFile, "a");
|
|
1909
|
+
var child2 = spawn(process.execPath, [daemonScript], {
|
|
1910
|
+
detached: true,
|
|
1911
|
+
windowsHide: true,
|
|
1912
|
+
stdio: ["ignore", logFd2, logFd2],
|
|
1913
|
+
env: Object.assign({}, process.env, {
|
|
1914
|
+
CLAGENTIC_CONFIG: configPath(),
|
|
1915
|
+
}),
|
|
1916
|
+
});
|
|
1917
|
+
child2.unref();
|
|
1918
|
+
fs.closeSync(logFd2);
|
|
1919
|
+
newConfig.pid = child2.pid;
|
|
1920
|
+
saveConfig(newConfig);
|
|
1921
|
+
for (var rb = 0; rb < 10; rb++) {
|
|
1922
|
+
await new Promise(function (resolve) { setTimeout(resolve, 500); });
|
|
1923
|
+
var retryAlive = await isDaemonAliveAsync(newConfig);
|
|
1924
|
+
if (retryAlive) break;
|
|
1925
|
+
}
|
|
1926
|
+
startDaemonWatcher();
|
|
1927
|
+
callback(newConfig);
|
|
1928
|
+
return;
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
startDaemonWatcher();
|
|
1932
|
+
callback(newConfig);
|
|
1933
|
+
}
|
|
1934
|
+
|
|
1935
|
+
// ==============================
|
|
1936
|
+
// Show server started info
|
|
1937
|
+
// ==============================
|
|
1938
|
+
function showServerStarted(config, ip, setupCode) {
|
|
1939
|
+
showMainMenu(config, ip, setupCode);
|
|
1940
|
+
}
|
|
1941
|
+
|
|
1942
|
+
// ==============================
|
|
1943
|
+
// Main management menu
|
|
1944
|
+
// ==============================
|
|
1945
|
+
function showMainMenu(config, ip, setupCode) {
|
|
1946
|
+
startDaemonWatcher();
|
|
1947
|
+
var protocol = config.tls ? "https" : "http";
|
|
1948
|
+
var url = config.builtinCert
|
|
1949
|
+
? toClayStudioUrl(ip, config.port, protocol)
|
|
1950
|
+
: protocol + "://" + ip + ":" + config.port;
|
|
1951
|
+
|
|
1952
|
+
sendIPCCommand(socketPath(), { cmd: "get_status" }).then(function (status) {
|
|
1953
|
+
var projs = (status && status.projects) || [];
|
|
1954
|
+
var totalSessions = 0;
|
|
1955
|
+
var totalAwaiting = 0;
|
|
1956
|
+
for (var i = 0; i < projs.length; i++) {
|
|
1957
|
+
totalSessions += projs[i].sessions || 0;
|
|
1958
|
+
if (projs[i].isProcessing) totalAwaiting++;
|
|
1959
|
+
}
|
|
1960
|
+
|
|
1961
|
+
console.clear();
|
|
1962
|
+
printLogo();
|
|
1963
|
+
log("");
|
|
1964
|
+
|
|
1965
|
+
function afterQr() {
|
|
1966
|
+
// Status line
|
|
1967
|
+
log(" " + a.dim + "clay" + a.reset + " " + a.dim + "v" + currentVersion + a.reset + a.dim + " — " + url + a.reset);
|
|
1968
|
+
if (config.builtinCert) log(" " + a.dim + "d.clay.studio provides HTTPS certificates only. Your traffic never leaves your network." + a.reset);
|
|
1969
|
+
var parts = [];
|
|
1970
|
+
parts.push(a.bold + projs.length + a.reset + a.dim + (projs.length === 1 ? " project" : " projects"));
|
|
1971
|
+
parts.push(a.reset + a.bold + totalSessions + a.reset + a.dim + (totalSessions === 1 ? " session" : " sessions"));
|
|
1972
|
+
if (totalAwaiting > 0) {
|
|
1973
|
+
parts.push(a.reset + a.yellow + a.bold + totalAwaiting + a.reset + a.yellow + " awaiting" + a.reset + a.dim);
|
|
1974
|
+
}
|
|
1975
|
+
log(" " + a.dim + parts.join(a.reset + a.dim + " · ") + a.reset);
|
|
1976
|
+
log(" " + a.dim + "~/.clagentic → " + path.join(REAL_HOME, ".clagentic") + a.reset);
|
|
1977
|
+
log(" Press " + a.bold + "o" + a.reset + " to open in browser");
|
|
1978
|
+
log("");
|
|
1979
|
+
|
|
1980
|
+
if (config.mkcertDetected) {
|
|
1981
|
+
log(" " + sym.warn + " " + a.yellow + "Clay now ships with a builtin HTTPS certificate." + a.reset);
|
|
1982
|
+
log(" " + a.dim + "No more CA setup on each device." + a.reset);
|
|
1983
|
+
log(" " + a.dim + "To use it, pass --builtin-cert or uninstall mkcert." + a.reset);
|
|
1984
|
+
log("");
|
|
1985
|
+
}
|
|
1986
|
+
|
|
1987
|
+
showMenuItems();
|
|
1988
|
+
}
|
|
1989
|
+
|
|
1990
|
+
if (ip !== "localhost") {
|
|
1991
|
+
qrcode.generate(url, { small: !isBasicTerm }, function (code) {
|
|
1992
|
+
var lines = code.split("\n").map(function (l) { return " " + l; }).join("\n");
|
|
1993
|
+
console.log(lines);
|
|
1994
|
+
afterQr();
|
|
1995
|
+
});
|
|
1996
|
+
} else {
|
|
1997
|
+
log(a.bold + " " + url + a.reset);
|
|
1998
|
+
log("");
|
|
1999
|
+
afterQr();
|
|
2000
|
+
}
|
|
2001
|
+
|
|
2002
|
+
function showMenuItems() {
|
|
2003
|
+
var items = [
|
|
2004
|
+
{ label: "Settings", value: "settings" },
|
|
2005
|
+
{ label: "Shut down server", value: "shutdown" },
|
|
2006
|
+
{ label: "Keep server alive & exit", value: "exit" },
|
|
2007
|
+
];
|
|
2008
|
+
|
|
2009
|
+
promptSelect("What would you like to do?", items, function (choice) {
|
|
2010
|
+
switch (choice) {
|
|
2011
|
+
case "settings":
|
|
2012
|
+
showSettingsMenu(config, ip);
|
|
2013
|
+
break;
|
|
2014
|
+
|
|
2015
|
+
case "shutdown":
|
|
2016
|
+
log(sym.bar);
|
|
2017
|
+
log(sym.bar + " " + a.yellow + "This will stop the server completely." + a.reset);
|
|
2018
|
+
log(sym.bar + " " + a.dim + "All connected sessions will be disconnected." + a.reset);
|
|
2019
|
+
log(sym.bar);
|
|
2020
|
+
promptSelect("Are you sure?", [
|
|
2021
|
+
{ label: "Cancel", value: "cancel" },
|
|
2022
|
+
{ label: "Shut down", value: "confirm" },
|
|
2023
|
+
], function (confirm) {
|
|
2024
|
+
if (confirm === "confirm") {
|
|
2025
|
+
stopDaemonWatcher();
|
|
2026
|
+
sendIPCCommand(socketPath(), { cmd: "shutdown" }).then(function () {
|
|
2027
|
+
log(sym.done + " " + a.green + "Server stopped." + a.reset);
|
|
2028
|
+
log("");
|
|
2029
|
+
clearStaleConfig();
|
|
2030
|
+
process.exit(0);
|
|
2031
|
+
});
|
|
2032
|
+
} else {
|
|
2033
|
+
showMainMenu(config, ip);
|
|
2034
|
+
}
|
|
2035
|
+
});
|
|
2036
|
+
break;
|
|
2037
|
+
|
|
2038
|
+
case "exit":
|
|
2039
|
+
log("");
|
|
2040
|
+
log(" " + a.bold + "Bye!" + a.reset + " " + a.dim + "Server is still running in background." + a.reset);
|
|
2041
|
+
log(" " + a.dim + "Run " + a.reset + "npx @clagentic/console" + a.dim + " to come back here." + a.reset);
|
|
2042
|
+
log("");
|
|
2043
|
+
process.exit(0);
|
|
2044
|
+
break;
|
|
2045
|
+
}
|
|
2046
|
+
}, {
|
|
2047
|
+
hint: [
|
|
2048
|
+
"Run npx @clagentic/console in other directories to add more projects.",
|
|
2049
|
+
"★ github.com/chadbyte/clay — Press s to star the repo",
|
|
2050
|
+
],
|
|
2051
|
+
keys: [
|
|
2052
|
+
{ key: "o", onKey: function () {
|
|
2053
|
+
openUrl(url);
|
|
2054
|
+
showMainMenu(config, ip);
|
|
2055
|
+
}},
|
|
2056
|
+
{ key: "s", onKey: function () {
|
|
2057
|
+
openUrl("https://github.com/chadbyte/clay");
|
|
2058
|
+
showMainMenu(config, ip);
|
|
2059
|
+
}},
|
|
2060
|
+
],
|
|
2061
|
+
});
|
|
2062
|
+
}
|
|
2063
|
+
});
|
|
2064
|
+
}
|
|
2065
|
+
|
|
2066
|
+
// ==============================
|
|
2067
|
+
// Setup guide (2x2 toggle flow)
|
|
2068
|
+
// ==============================
|
|
2069
|
+
// ==============================
|
|
2070
|
+
// Settings sub-menu
|
|
2071
|
+
// ==============================
|
|
2072
|
+
function showSettingsMenu(config, ip) {
|
|
2073
|
+
sendIPCCommand(socketPath(), { cmd: "get_status" }).then(function (status) {
|
|
2074
|
+
var isAwake = status && status.keepAwake;
|
|
2075
|
+
var isOsUsers = status && status.osUsers;
|
|
2076
|
+
|
|
2077
|
+
console.clear();
|
|
2078
|
+
printLogo();
|
|
2079
|
+
log("");
|
|
2080
|
+
log(sym.pointer + " " + a.bold + "Settings" + a.reset);
|
|
2081
|
+
log(sym.bar);
|
|
2082
|
+
|
|
2083
|
+
// Detect current state
|
|
2084
|
+
var tlsStatus = config.tls
|
|
2085
|
+
? a.green + "Enabled" + a.reset
|
|
2086
|
+
: a.dim + "Disabled" + a.reset;
|
|
2087
|
+
var pinStatus = config.pinHash
|
|
2088
|
+
? a.green + "Enabled" + a.reset
|
|
2089
|
+
: a.dim + "Off" + a.reset;
|
|
2090
|
+
var awakeStatus = isAwake
|
|
2091
|
+
? a.green + "On" + a.reset
|
|
2092
|
+
: a.dim + "Off" + a.reset;
|
|
2093
|
+
|
|
2094
|
+
log(sym.bar + " HTTPS " + tlsStatus);
|
|
2095
|
+
var muEnabled = isMultiUser();
|
|
2096
|
+
var muStatus = muEnabled
|
|
2097
|
+
? a.green + "Enabled" + a.reset
|
|
2098
|
+
: a.dim + "Off" + a.reset;
|
|
2099
|
+
|
|
2100
|
+
var modeLabel = config.mode === "multi" ? "Multi-user" : "Single user";
|
|
2101
|
+
var modeStatus = config.mode === "multi"
|
|
2102
|
+
? a.clay + modeLabel + a.reset
|
|
2103
|
+
: a.dim + modeLabel + a.reset;
|
|
2104
|
+
log(sym.bar + " Mode " + modeStatus);
|
|
2105
|
+
log(sym.bar + " PIN " + pinStatus);
|
|
2106
|
+
log(sym.bar + " Multi-user " + muStatus);
|
|
2107
|
+
var osUsersStatus = isOsUsers
|
|
2108
|
+
? a.green + "Enabled" + a.reset
|
|
2109
|
+
: a.dim + "Off" + a.reset;
|
|
2110
|
+
if (muEnabled) {
|
|
2111
|
+
log(sym.bar + " OS users " + osUsersStatus);
|
|
2112
|
+
}
|
|
2113
|
+
if (process.platform === "darwin") {
|
|
2114
|
+
log(sym.bar + " Keep awake " + awakeStatus);
|
|
2115
|
+
}
|
|
2116
|
+
log(sym.bar);
|
|
2117
|
+
|
|
2118
|
+
// Build items
|
|
2119
|
+
var items = [];
|
|
2120
|
+
|
|
2121
|
+
if (!muEnabled) {
|
|
2122
|
+
if (config.pinHash) {
|
|
2123
|
+
items.push({ label: "Change PIN", value: "pin" });
|
|
2124
|
+
items.push({ label: "Remove PIN", value: "remove_pin" });
|
|
2125
|
+
} else {
|
|
2126
|
+
items.push({ label: "Set PIN", value: "pin" });
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
if (muEnabled) {
|
|
2130
|
+
items.push({ label: "Disable multi-user mode", value: "disable_multi_user" });
|
|
2131
|
+
if (isOsUsers) {
|
|
2132
|
+
items.push({ label: "Disable OS-level user isolation", value: "disable_os_users" });
|
|
2133
|
+
} else {
|
|
2134
|
+
items.push({ label: "Enable OS-level user isolation", value: "os_users" });
|
|
2135
|
+
}
|
|
2136
|
+
} else {
|
|
2137
|
+
items.push({ label: "Enable multi-user mode", value: "multi_user" });
|
|
2138
|
+
}
|
|
2139
|
+
if (muEnabled) {
|
|
2140
|
+
items.push({ label: "Show setup code", value: "show_setup_code" });
|
|
2141
|
+
}
|
|
2142
|
+
if (muEnabled && hasAdmin()) {
|
|
2143
|
+
items.push({ label: "Recover admin password", value: "recover_admin" });
|
|
2144
|
+
}
|
|
2145
|
+
if (process.platform === "darwin") {
|
|
2146
|
+
items.push({ label: isAwake ? "Disable keep awake" : "Enable keep awake", value: "awake" });
|
|
2147
|
+
}
|
|
2148
|
+
items.push({ label: "View logs", value: "logs" });
|
|
2149
|
+
items.push({ label: "Re-run setup wizard", value: "rerun_setup" });
|
|
2150
|
+
items.push({ label: "Back", value: "back" });
|
|
2151
|
+
|
|
2152
|
+
promptSelect("Select", items, function (choice) {
|
|
2153
|
+
switch (choice) {
|
|
2154
|
+
case "pin":
|
|
2155
|
+
log(sym.bar);
|
|
2156
|
+
promptPin(function (pin) {
|
|
2157
|
+
if (pin) {
|
|
2158
|
+
var hash = generateAuthToken(pin);
|
|
2159
|
+
sendIPCCommand(socketPath(), { cmd: "set_pin", pinHash: hash }).then(function () {
|
|
2160
|
+
config.pinHash = hash;
|
|
2161
|
+
log(sym.done + " " + a.green + "PIN updated" + a.reset);
|
|
2162
|
+
log("");
|
|
2163
|
+
showSettingsMenu(config, ip);
|
|
2164
|
+
});
|
|
2165
|
+
} else {
|
|
2166
|
+
showSettingsMenu(config, ip);
|
|
2167
|
+
}
|
|
2168
|
+
});
|
|
2169
|
+
break;
|
|
2170
|
+
|
|
2171
|
+
case "remove_pin":
|
|
2172
|
+
sendIPCCommand(socketPath(), { cmd: "set_pin", pinHash: null }).then(function () {
|
|
2173
|
+
config.pinHash = null;
|
|
2174
|
+
log(sym.done + " " + a.dim + "PIN removed" + a.reset);
|
|
2175
|
+
log("");
|
|
2176
|
+
showSettingsMenu(config, ip);
|
|
2177
|
+
});
|
|
2178
|
+
break;
|
|
2179
|
+
|
|
2180
|
+
case "multi_user":
|
|
2181
|
+
var muResult = enableMultiUser();
|
|
2182
|
+
log(sym.bar);
|
|
2183
|
+
log(sym.bar + " " + a.yellow + sym.warn + " Experimental Feature" + a.reset);
|
|
2184
|
+
log(sym.bar);
|
|
2185
|
+
log(sym.bar + " " + a.dim + "Multi-user mode is experimental and may change in future releases." + a.reset);
|
|
2186
|
+
log(sym.bar + " " + a.dim + "Sharing access to AI-powered tools may be subject to your provider's" + a.reset);
|
|
2187
|
+
log(sym.bar + " " + a.dim + "terms of service. Please review the applicable usage policies before" + a.reset);
|
|
2188
|
+
log(sym.bar + " " + a.dim + "granting access to other users." + a.reset);
|
|
2189
|
+
log(sym.bar);
|
|
2190
|
+
if (muResult.setupCode) {
|
|
2191
|
+
log(sym.bar + " " + a.green + "Multi-user mode enabled." + a.reset);
|
|
2192
|
+
log(sym.bar);
|
|
2193
|
+
log(sym.bar + " Setup code: " + a.bold + muResult.setupCode + a.reset);
|
|
2194
|
+
log(sym.bar);
|
|
2195
|
+
log(sym.bar + " " + a.dim + "Open Clay in your browser and enter this code to create the admin account." + a.reset);
|
|
2196
|
+
log(sym.bar + " " + a.dim + "The code is single-use and will be cleared once the admin is set up." + a.reset);
|
|
2197
|
+
} else {
|
|
2198
|
+
log(sym.bar + " " + a.dim + "Multi-user mode is already enabled." + a.reset);
|
|
2199
|
+
}
|
|
2200
|
+
log(sym.bar);
|
|
2201
|
+
promptSelect("Back?", [{ label: "Back", value: "back" }], function () {
|
|
2202
|
+
showSettingsMenu(config, ip);
|
|
2203
|
+
});
|
|
2204
|
+
break;
|
|
2205
|
+
|
|
2206
|
+
case "disable_multi_user":
|
|
2207
|
+
log(sym.bar);
|
|
2208
|
+
log(sym.bar + " " + a.yellow + sym.warn + " Disable multi-user mode?" + a.reset);
|
|
2209
|
+
log(sym.bar);
|
|
2210
|
+
log(sym.bar + " " + a.dim + "Sessions created by other users will no longer be visible." + a.reset);
|
|
2211
|
+
log(sym.bar + " " + a.dim + "User accounts will be preserved and restored if re-enabled." + a.reset);
|
|
2212
|
+
log(sym.bar);
|
|
2213
|
+
promptSelect("Confirm", [
|
|
2214
|
+
{ label: "Disable multi-user mode", value: "confirm" },
|
|
2215
|
+
{ label: "Cancel", value: "cancel" },
|
|
2216
|
+
], function (confirmChoice) {
|
|
2217
|
+
if (confirmChoice === "confirm") {
|
|
2218
|
+
disableMultiUser();
|
|
2219
|
+
log(sym.bar);
|
|
2220
|
+
log(sym.done + " " + a.green + "Multi-user mode disabled." + a.reset);
|
|
2221
|
+
log(sym.bar + " " + a.dim + "Restart the daemon for changes to take full effect." + a.reset);
|
|
2222
|
+
log(sym.bar);
|
|
2223
|
+
}
|
|
2224
|
+
showSettingsMenu(config, ip);
|
|
2225
|
+
});
|
|
2226
|
+
break;
|
|
2227
|
+
|
|
2228
|
+
case "os_users":
|
|
2229
|
+
if (process.platform === "win32") {
|
|
2230
|
+
log(sym.bar);
|
|
2231
|
+
log(sym.bar + " " + a.red + "OS-level user isolation is not supported on Windows." + a.reset);
|
|
2232
|
+
log(sym.bar);
|
|
2233
|
+
promptSelect("Back?", [{ label: "Back", value: "back" }], function () {
|
|
2234
|
+
showSettingsMenu(config, ip);
|
|
2235
|
+
});
|
|
2236
|
+
break;
|
|
2237
|
+
}
|
|
2238
|
+
if (process.getuid() !== 0) {
|
|
2239
|
+
log(sym.bar);
|
|
2240
|
+
log(sym.bar + " " + a.red + sym.warn + " OS user isolation requires root." + a.reset);
|
|
2241
|
+
log(sym.bar + " " + a.dim + "Shut down this server, then restart with:" + a.reset);
|
|
2242
|
+
log(sym.bar + " " + a.bold + "sudo npx @clagentic/console" + a.reset);
|
|
2243
|
+
log(sym.bar);
|
|
2244
|
+
promptSelect("Back?", [{ label: "Back", value: "back" }], function () {
|
|
2245
|
+
showSettingsMenu(config, ip);
|
|
2246
|
+
});
|
|
2247
|
+
break;
|
|
2248
|
+
}
|
|
2249
|
+
if (process.platform !== "linux") {
|
|
2250
|
+
log(sym.bar);
|
|
2251
|
+
log(sym.bar + " " + a.red + sym.warn + " OS-level user isolation requires Linux." + a.reset);
|
|
2252
|
+
log(sym.bar + " " + a.dim + "This feature depends on setfacl, getent, and uid/gid process spawning." + a.reset);
|
|
2253
|
+
log(sym.bar + " " + a.dim + "Use Docker or a Linux VM to run Clay with OS user isolation." + a.reset);
|
|
2254
|
+
log(sym.bar);
|
|
2255
|
+
showSettingsMenu(config, ip);
|
|
2256
|
+
return;
|
|
2257
|
+
}
|
|
2258
|
+
log(sym.bar);
|
|
2259
|
+
log(sym.bar + " " + a.yellow + sym.warn + " OS-Level User Isolation" + a.reset);
|
|
2260
|
+
log(sym.bar);
|
|
2261
|
+
log(sym.bar + " " + a.dim + "This feature maps each Clay user to a Linux OS user account." + a.reset);
|
|
2262
|
+
log(sym.bar + " " + a.dim + "The daemon must run as root and will spawn processes (SDK workers," + a.reset);
|
|
2263
|
+
log(sym.bar + " " + a.dim + "terminals, file operations) as the mapped Linux user." + a.reset);
|
|
2264
|
+
log(sym.bar);
|
|
2265
|
+
log(sym.bar + " " + a.dim + "What this means:" + a.reset);
|
|
2266
|
+
log(sym.bar + " " + a.dim + "- Each mapped user uses their own ~/.claude/ credentials" + a.reset);
|
|
2267
|
+
log(sym.bar + " " + a.dim + "- Terminals and file access follow Linux permissions" + a.reset);
|
|
2268
|
+
log(sym.bar + " " + a.dim + "- Linux user accounts are created automatically (clay-username)" + a.reset);
|
|
2269
|
+
log(sym.bar);
|
|
2270
|
+
log(sym.bar + " " + a.dim + "Recommended: Run on a dedicated Clay server or cloud instance," + a.reset);
|
|
2271
|
+
log(sym.bar + " " + a.dim + "not on a personal computer or general-purpose server." + a.reset);
|
|
2272
|
+
log(sym.bar);
|
|
2273
|
+
promptSelect("Select", [
|
|
2274
|
+
{ label: "Enable OS-level user isolation", value: "confirm" },
|
|
2275
|
+
{ label: "Cancel", value: "cancel" },
|
|
2276
|
+
], function (confirmChoice) {
|
|
2277
|
+
if (confirmChoice === "confirm") {
|
|
2278
|
+
sendIPCCommand(socketPath(), { cmd: "set_os_users", value: true }).then(function (res) {
|
|
2279
|
+
if (res.error === "acl_not_installed") {
|
|
2280
|
+
log(sym.bar);
|
|
2281
|
+
log(sym.bar + " " + a.red + sym.warn + " setfacl is not installed." + a.reset);
|
|
2282
|
+
log(sym.bar);
|
|
2283
|
+
log(sym.bar + " OS user isolation requires the ACL (Access Control List) package");
|
|
2284
|
+
log(sym.bar + " to manage per-user file permissions on shared projects.");
|
|
2285
|
+
log(sym.bar);
|
|
2286
|
+
log(sym.bar + " " + a.bold + "Install it:" + a.reset);
|
|
2287
|
+
log(sym.bar + " " + a.cyan + res.installCmd + a.reset);
|
|
2288
|
+
log(sym.bar);
|
|
2289
|
+
log(sym.bar + " " + a.dim + "Then try enabling OS user isolation again." + a.reset);
|
|
2290
|
+
log(sym.bar);
|
|
2291
|
+
showSettingsMenu(config, ip);
|
|
2292
|
+
return;
|
|
2293
|
+
} else if (res.error) {
|
|
2294
|
+
log(sym.bar);
|
|
2295
|
+
log(sym.bar + " " + a.red + sym.warn + " Failed to enable OS users: " + res.error + a.reset);
|
|
2296
|
+
log(sym.bar);
|
|
2297
|
+
showSettingsMenu(config, ip);
|
|
2298
|
+
return;
|
|
2299
|
+
} else if (!res.ok) {
|
|
2300
|
+
log(sym.bar);
|
|
2301
|
+
log(sym.bar + " " + a.red + sym.warn + " Unexpected response from daemon." + a.reset);
|
|
2302
|
+
log(sym.bar + " " + a.dim + JSON.stringify(res) + a.reset);
|
|
2303
|
+
log(sym.bar);
|
|
2304
|
+
showSettingsMenu(config, ip);
|
|
2305
|
+
return;
|
|
2306
|
+
}
|
|
2307
|
+
// Daemon saved the flag. Now provision from CLI with live progress.
|
|
2308
|
+
config.osUsers = true;
|
|
2309
|
+
log(sym.bar);
|
|
2310
|
+
log(sym.done + " " + a.green + "OS-level user isolation enabled." + a.reset);
|
|
2311
|
+
log(sym.bar);
|
|
2312
|
+
|
|
2313
|
+
// Provision Linux accounts from CLI (we have root + terminal)
|
|
2314
|
+
var osUsersLib = require("../lib/os-users");
|
|
2315
|
+
var usersLib = require("../lib/users");
|
|
2316
|
+
|
|
2317
|
+
try { osUsersLib.ensureProjectsDir(); } catch (e) {
|
|
2318
|
+
log(sym.bar + " " + a.yellow + sym.warn + " Failed to create projects dir: " + e.message + a.reset);
|
|
2319
|
+
}
|
|
2320
|
+
|
|
2321
|
+
var allUsers = usersLib.getAllUsers();
|
|
2322
|
+
if (allUsers.length === 0) {
|
|
2323
|
+
log(sym.bar + " " + a.dim + "No users to provision yet. Accounts will be created when users register." + a.reset);
|
|
2324
|
+
} else {
|
|
2325
|
+
log(sym.bar + " " + a.dim + "Provisioning " + allUsers.length + " user(s)..." + a.reset);
|
|
2326
|
+
for (var ui = 0; ui < allUsers.length; ui++) {
|
|
2327
|
+
var usr = allUsers[ui];
|
|
2328
|
+
if (usr.linuxUser && osUsersLib.linuxUserExists(usr.linuxUser)) {
|
|
2329
|
+
log(sym.bar + " " + a.dim + sym.done + " " + usr.username + " -> " + usr.linuxUser + " (exists)" + a.reset);
|
|
2330
|
+
continue;
|
|
2331
|
+
}
|
|
2332
|
+
log(sym.bar + " " + a.dim + "Creating Linux account for " + usr.username + "..." + a.reset);
|
|
2333
|
+
var provision = osUsersLib.provisionLinuxUser(usr.username);
|
|
2334
|
+
if (provision.ok) {
|
|
2335
|
+
usersLib.updateLinuxUser(usr.id, provision.linuxUser);
|
|
2336
|
+
log(sym.bar + " " + a.green + sym.done + " " + usr.username + " -> " + provision.linuxUser + a.reset);
|
|
2337
|
+
} else {
|
|
2338
|
+
log(sym.bar + " " + a.red + sym.warn + " " + usr.username + ": " + (provision.error || "unknown error") + a.reset);
|
|
2339
|
+
}
|
|
2340
|
+
}
|
|
2341
|
+
}
|
|
2342
|
+
|
|
2343
|
+
// Set up ACLs for existing projects
|
|
2344
|
+
var cfg = loadConfig() || {};
|
|
2345
|
+
var cfgProjects = cfg.projects || [];
|
|
2346
|
+
if (cfgProjects.length > 0) {
|
|
2347
|
+
log(sym.bar);
|
|
2348
|
+
log(sym.bar + " " + a.dim + "Setting ACLs for " + cfgProjects.length + " project(s)..." + a.reset);
|
|
2349
|
+
for (var pi = 0; pi < cfgProjects.length; pi++) {
|
|
2350
|
+
var proj = cfgProjects[pi];
|
|
2351
|
+
if (osUsersLib.isHomeDirectory(proj.path)) {
|
|
2352
|
+
log(sym.bar + " " + a.dim + "~ " + (proj.slug || proj.path) + " (home dir, skipped)" + a.reset);
|
|
2353
|
+
continue;
|
|
2354
|
+
}
|
|
2355
|
+
try {
|
|
2356
|
+
if (proj.visibility === "public") {
|
|
2357
|
+
osUsersLib.grantAllUsersAccess(proj.path, usersLib);
|
|
2358
|
+
}
|
|
2359
|
+
if (proj.ownerId) {
|
|
2360
|
+
var ownerUser = usersLib.findUserById(proj.ownerId);
|
|
2361
|
+
if (ownerUser && ownerUser.linuxUser) {
|
|
2362
|
+
osUsersLib.grantProjectAccess(proj.path, ownerUser.linuxUser);
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2365
|
+
log(sym.bar + " " + a.dim + sym.done + " " + (proj.slug || proj.path) + a.reset);
|
|
2366
|
+
} catch (aclErr) {
|
|
2367
|
+
log(sym.bar + " " + a.yellow + sym.warn + " " + (proj.slug || proj.path) + ": " + aclErr.message + a.reset);
|
|
2368
|
+
}
|
|
2369
|
+
}
|
|
2370
|
+
}
|
|
2371
|
+
|
|
2372
|
+
log(sym.bar);
|
|
2373
|
+
log(sym.bar + " " + a.dim + "Restart the daemon for full effect." + a.reset);
|
|
2374
|
+
log(sym.bar);
|
|
2375
|
+
showSettingsMenu(config, ip);
|
|
2376
|
+
}).catch(function (err) {
|
|
2377
|
+
log(sym.bar);
|
|
2378
|
+
log(sym.bar + " " + a.red + sym.warn + " IPC error: " + (err.message || err) + a.reset);
|
|
2379
|
+
log(sym.bar);
|
|
2380
|
+
showSettingsMenu(config, ip);
|
|
2381
|
+
});
|
|
2382
|
+
} else {
|
|
2383
|
+
showSettingsMenu(config, ip);
|
|
2384
|
+
}
|
|
2385
|
+
});
|
|
2386
|
+
break;
|
|
2387
|
+
|
|
2388
|
+
case "disable_os_users":
|
|
2389
|
+
log(sym.bar);
|
|
2390
|
+
log(sym.bar + " " + a.yellow + sym.warn + " Disable OS-level user isolation?" + a.reset);
|
|
2391
|
+
log(sym.bar);
|
|
2392
|
+
log(sym.bar + " " + a.dim + "Processes will no longer be spawned as mapped Linux users." + a.reset);
|
|
2393
|
+
log(sym.bar + " " + a.dim + "User mappings will be preserved and restored if re-enabled." + a.reset);
|
|
2394
|
+
log(sym.bar);
|
|
2395
|
+
promptSelect("Confirm", [
|
|
2396
|
+
{ label: "Disable OS-level user isolation", value: "confirm" },
|
|
2397
|
+
{ label: "Cancel", value: "cancel" },
|
|
2398
|
+
], function (confirmChoice) {
|
|
2399
|
+
if (confirmChoice === "confirm") {
|
|
2400
|
+
sendIPCCommand(socketPath(), { cmd: "set_os_users", value: false }).then(function (res) {
|
|
2401
|
+
if (res.ok) {
|
|
2402
|
+
config.osUsers = false;
|
|
2403
|
+
log(sym.bar);
|
|
2404
|
+
log(sym.done + " " + a.green + "OS-level user isolation disabled." + a.reset);
|
|
2405
|
+
log(sym.bar + " " + a.dim + "Restart the daemon for changes to take full effect." + a.reset);
|
|
2406
|
+
log(sym.bar);
|
|
2407
|
+
}
|
|
2408
|
+
showSettingsMenu(config, ip);
|
|
2409
|
+
});
|
|
2410
|
+
} else {
|
|
2411
|
+
showSettingsMenu(config, ip);
|
|
2412
|
+
}
|
|
2413
|
+
});
|
|
2414
|
+
break;
|
|
2415
|
+
|
|
2416
|
+
case "rerun_setup":
|
|
2417
|
+
log(sym.bar);
|
|
2418
|
+
log(sym.bar + " " + a.yellow + sym.warn + " Re-run setup wizard?" + a.reset);
|
|
2419
|
+
log(sym.bar);
|
|
2420
|
+
log(sym.bar + " " + a.dim + "This will shut down the running daemon, reset your setup" + a.reset);
|
|
2421
|
+
log(sym.bar + " " + a.dim + "preferences (mode, port), and walk you through the wizard again." + a.reset);
|
|
2422
|
+
log(sym.bar + " " + a.dim + "Your projects and user accounts will be preserved." + a.reset);
|
|
2423
|
+
log(sym.bar);
|
|
2424
|
+
promptSelect("Confirm", [
|
|
2425
|
+
{ label: "Re-run setup wizard", value: "confirm" },
|
|
2426
|
+
{ label: "Cancel", value: "cancel" },
|
|
2427
|
+
], function (confirmChoice) {
|
|
2428
|
+
if (confirmChoice === "confirm") {
|
|
2429
|
+
// Save old PID before clearing, so we can force-kill if needed
|
|
2430
|
+
var cfg = loadConfig() || {};
|
|
2431
|
+
var oldPid = cfg.pid;
|
|
2432
|
+
// Clear setupCompleted so setup() runs fresh
|
|
2433
|
+
delete cfg.setupCompleted;
|
|
2434
|
+
delete cfg.mode;
|
|
2435
|
+
cfg.pid = null;
|
|
2436
|
+
saveConfig(cfg);
|
|
2437
|
+
|
|
2438
|
+
// Helper: wait for port to be free, force-kill if needed
|
|
2439
|
+
function waitForPortFree(cb) {
|
|
2440
|
+
var attempts = 0;
|
|
2441
|
+
var maxAttempts = 12; // 6 seconds total
|
|
2442
|
+
function check() {
|
|
2443
|
+
isPortFree(port).then(function (free) {
|
|
2444
|
+
if (free) return cb();
|
|
2445
|
+
attempts++;
|
|
2446
|
+
if (attempts >= maxAttempts) {
|
|
2447
|
+
// Port still busy, force-kill old daemon
|
|
2448
|
+
if (oldPid) {
|
|
2449
|
+
try { process.kill(oldPid, "SIGKILL"); } catch (e) {}
|
|
2450
|
+
}
|
|
2451
|
+
// Wait a bit more after SIGKILL
|
|
2452
|
+
setTimeout(function () {
|
|
2453
|
+
isPortFree(port).then(function (free2) {
|
|
2454
|
+
if (!free2) {
|
|
2455
|
+
log(sym.warn + " " + a.yellow + "Port " + port + " still in use. Kill the process manually:" + a.reset);
|
|
2456
|
+
log(sym.bar + " " + a.bold + "lsof -ti:" + port + " | xargs kill -9" + a.reset);
|
|
2457
|
+
}
|
|
2458
|
+
cb();
|
|
2459
|
+
});
|
|
2460
|
+
}, 1000);
|
|
2461
|
+
return;
|
|
2462
|
+
}
|
|
2463
|
+
setTimeout(check, 500);
|
|
2464
|
+
});
|
|
2465
|
+
}
|
|
2466
|
+
check();
|
|
2467
|
+
}
|
|
2468
|
+
|
|
2469
|
+
// Helper: run setup wizard after daemon is dead
|
|
2470
|
+
function proceedWithSetup() {
|
|
2471
|
+
clearStaleConfig();
|
|
2472
|
+
setup(function (mode, keepAwake, wantOsUsers) {
|
|
2473
|
+
var rc = loadClayrc();
|
|
2474
|
+
var restorable = (rc.recentProjects || []).filter(function (p) {
|
|
2475
|
+
return p.path !== cwd && fs.existsSync(p.path);
|
|
2476
|
+
});
|
|
2477
|
+
if (restorable.length > 0) {
|
|
2478
|
+
promptRestoreProjects(restorable, function (selected) {
|
|
2479
|
+
forkDaemon(mode, keepAwake, selected, false, wantOsUsers);
|
|
2480
|
+
});
|
|
2481
|
+
} else {
|
|
2482
|
+
log(sym.bar);
|
|
2483
|
+
log(sym.end + " " + a.dim + "Starting relay..." + a.reset);
|
|
2484
|
+
log("");
|
|
2485
|
+
forkDaemon(mode, keepAwake, undefined, true, wantOsUsers);
|
|
2486
|
+
}
|
|
2487
|
+
});
|
|
2488
|
+
}
|
|
2489
|
+
|
|
2490
|
+
// Shut down the daemon, then wait for port to be free
|
|
2491
|
+
sendIPCCommand(socketPath(), { cmd: "shutdown" }).then(function () {
|
|
2492
|
+
waitForPortFree(proceedWithSetup);
|
|
2493
|
+
}).catch(function () {
|
|
2494
|
+
// IPC failed, daemon may be unresponsive. Try SIGTERM, then wait.
|
|
2495
|
+
if (oldPid) {
|
|
2496
|
+
try { process.kill(oldPid, "SIGTERM"); } catch (e) {}
|
|
2497
|
+
}
|
|
2498
|
+
waitForPortFree(proceedWithSetup);
|
|
2499
|
+
});
|
|
2500
|
+
} else {
|
|
2501
|
+
showSettingsMenu(config, ip);
|
|
2502
|
+
}
|
|
2503
|
+
});
|
|
2504
|
+
break;
|
|
2505
|
+
|
|
2506
|
+
case "show_setup_code":
|
|
2507
|
+
// getSetupCode() auto-generates if multi-user is on and no code exists
|
|
2508
|
+
var currentCode = getSetupCode();
|
|
2509
|
+
log(sym.bar);
|
|
2510
|
+
if (currentCode) {
|
|
2511
|
+
log(sym.bar + " " + a.yellow + sym.warn + " Setup code: " + a.bold + currentCode + a.reset);
|
|
2512
|
+
if (hasAdmin()) {
|
|
2513
|
+
log(sym.bar + " " + a.dim + "Admin account exists. This code is for adding the next admin." + a.reset);
|
|
2514
|
+
} else {
|
|
2515
|
+
log(sym.bar + " " + a.dim + "Enter this code in the browser to create the admin account." + a.reset);
|
|
2516
|
+
}
|
|
2517
|
+
} else {
|
|
2518
|
+
log(sym.bar + " " + a.dim + "Multi-user mode is not enabled." + a.reset);
|
|
2519
|
+
}
|
|
2520
|
+
log(sym.bar);
|
|
2521
|
+
promptSelect("Back?", [{ label: "Back", value: "back" }], function () {
|
|
2522
|
+
showSettingsMenu(config, ip);
|
|
2523
|
+
});
|
|
2524
|
+
break;
|
|
2525
|
+
|
|
2526
|
+
case "logs":
|
|
2527
|
+
console.clear();
|
|
2528
|
+
log(a.bold + "Daemon logs" + a.reset + " " + a.dim + "(" + logPath() + ")" + a.reset);
|
|
2529
|
+
log("");
|
|
2530
|
+
try {
|
|
2531
|
+
var logContent = fs.readFileSync(logPath(), "utf8");
|
|
2532
|
+
var logLines = logContent.split("\n").slice(-30);
|
|
2533
|
+
for (var li = 0; li < logLines.length; li++) {
|
|
2534
|
+
log(a.dim + logLines[li] + a.reset);
|
|
2535
|
+
}
|
|
2536
|
+
} catch (e) {
|
|
2537
|
+
log(a.dim + "(empty)" + a.reset);
|
|
2538
|
+
}
|
|
2539
|
+
log("");
|
|
2540
|
+
promptSelect("Back?", [{ label: "Back", value: "back" }], function () {
|
|
2541
|
+
showSettingsMenu(config, ip);
|
|
2542
|
+
});
|
|
2543
|
+
break;
|
|
2544
|
+
|
|
2545
|
+
case "awake":
|
|
2546
|
+
sendIPCCommand(socketPath(), { cmd: "set_keep_awake", value: !isAwake }).then(function (res) {
|
|
2547
|
+
if (res.ok) {
|
|
2548
|
+
config.keepAwake = !isAwake;
|
|
2549
|
+
}
|
|
2550
|
+
showSettingsMenu(config, ip);
|
|
2551
|
+
});
|
|
2552
|
+
break;
|
|
2553
|
+
|
|
2554
|
+
case "recover_admin": {
|
|
2555
|
+
var recoveryUrlPath = crypto.randomBytes(16).toString("hex");
|
|
2556
|
+
var recoveryPassword = crypto.randomBytes(8).toString("base64url");
|
|
2557
|
+
sendIPCCommand(socketPath(), { cmd: "enable_recovery", urlPath: recoveryUrlPath, password: recoveryPassword }).then(function (res) {
|
|
2558
|
+
if (!res.ok) {
|
|
2559
|
+
log(sym.bar + " " + a.red + "Failed to enable recovery mode." + a.reset);
|
|
2560
|
+
log(sym.bar);
|
|
2561
|
+
showSettingsMenu(config, ip);
|
|
2562
|
+
return;
|
|
2563
|
+
}
|
|
2564
|
+
var protocol = config.tls ? "https" : "http";
|
|
2565
|
+
var recoveryUrl = config.builtinCert
|
|
2566
|
+
? toClayStudioUrl(ip, config.port, protocol) + "/recover/" + recoveryUrlPath
|
|
2567
|
+
: protocol + "://" + ip + ":" + config.port + "/recover/" + recoveryUrlPath;
|
|
2568
|
+
log(sym.bar);
|
|
2569
|
+
log(sym.bar + " " + a.yellow + sym.warn + " Admin Password Recovery" + a.reset);
|
|
2570
|
+
log(sym.bar);
|
|
2571
|
+
log(sym.bar + " " + a.dim + "Recovery URL:" + a.reset);
|
|
2572
|
+
log(sym.bar + " " + a.bold + recoveryUrl + a.reset);
|
|
2573
|
+
log(sym.bar);
|
|
2574
|
+
log(sym.bar + " " + a.dim + "Recovery password:" + a.reset);
|
|
2575
|
+
log(sym.bar + " " + a.bold + recoveryPassword + a.reset);
|
|
2576
|
+
log(sym.bar);
|
|
2577
|
+
log(sym.bar + " " + a.dim + "Open the URL in a browser and enter the password above." + a.reset);
|
|
2578
|
+
log(sym.bar + " " + a.dim + "This link is single-use and will expire when the PIN is reset." + a.reset);
|
|
2579
|
+
log(sym.bar);
|
|
2580
|
+
promptSelect("Done?", [
|
|
2581
|
+
{ label: "Disable recovery link", value: "disable" },
|
|
2582
|
+
{ label: "Back (keep link active)", value: "back" },
|
|
2583
|
+
], function (rc) {
|
|
2584
|
+
if (rc === "disable") {
|
|
2585
|
+
sendIPCCommand(socketPath(), { cmd: "disable_recovery" }).then(function () {
|
|
2586
|
+
log(sym.done + " " + a.dim + "Recovery link disabled." + a.reset);
|
|
2587
|
+
log("");
|
|
2588
|
+
showSettingsMenu(config, ip);
|
|
2589
|
+
});
|
|
2590
|
+
} else {
|
|
2591
|
+
showSettingsMenu(config, ip);
|
|
2592
|
+
}
|
|
2593
|
+
});
|
|
2594
|
+
});
|
|
2595
|
+
break;
|
|
2596
|
+
}
|
|
2597
|
+
|
|
2598
|
+
case "back":
|
|
2599
|
+
showMainMenu(config, ip);
|
|
2600
|
+
break;
|
|
2601
|
+
}
|
|
2602
|
+
});
|
|
2603
|
+
});
|
|
2604
|
+
}
|
|
2605
|
+
|
|
2606
|
+
// ==============================
|
|
2607
|
+
// Main entry: daemon alive?
|
|
2608
|
+
// ==============================
|
|
2609
|
+
var { checkAndUpdate } = require("../lib/updater");
|
|
2610
|
+
var currentVersion = require("../package.json").version;
|
|
2611
|
+
|
|
2612
|
+
(async function () {
|
|
2613
|
+
var updated = await checkAndUpdate(currentVersion, skipUpdate);
|
|
2614
|
+
if (updated) return;
|
|
2615
|
+
|
|
2616
|
+
// Dev mode — foreground daemon with file watching
|
|
2617
|
+
if (_isDev) {
|
|
2618
|
+
var devConfig = loadConfig();
|
|
2619
|
+
var devAlive = devConfig ? await isDaemonAliveAsync(devConfig) : false;
|
|
2620
|
+
if (devAlive) {
|
|
2621
|
+
console.log("\x1b[38;2;0;183;133m[dev]\x1b[0m Shutting down existing daemon...");
|
|
2622
|
+
await sendIPCCommand(socketPath(), { cmd: "shutdown" });
|
|
2623
|
+
clearStaleConfig();
|
|
2624
|
+
await new Promise(function (resolve) { setTimeout(resolve, 500); });
|
|
2625
|
+
}
|
|
2626
|
+
// No running daemon — clear config so setup runs fresh
|
|
2627
|
+
if (!devAlive && devConfig) {
|
|
2628
|
+
if (devConfig.pid) clearStaleConfig();
|
|
2629
|
+
devConfig = null;
|
|
2630
|
+
}
|
|
2631
|
+
// No config — go through setup (disclaimer, port, mode, etc.)
|
|
2632
|
+
if (!devConfig) {
|
|
2633
|
+
setup(function (mode, keepAwake, wantOsUsers) {
|
|
2634
|
+
devMode(mode, keepAwake, null, wantOsUsers);
|
|
2635
|
+
});
|
|
2636
|
+
} else {
|
|
2637
|
+
// Reuse existing config (repeat run)
|
|
2638
|
+
await devMode(devConfig.mode || "single", devConfig.keepAwake || false, devConfig.pinHash || null, devConfig.osUsers || false);
|
|
2639
|
+
}
|
|
2640
|
+
return;
|
|
2641
|
+
}
|
|
2642
|
+
|
|
2643
|
+
var config = loadConfig();
|
|
2644
|
+
var alive = config ? await isDaemonAliveAsync(config) : false;
|
|
2645
|
+
|
|
2646
|
+
if (!alive && config && config.pid) {
|
|
2647
|
+
// Stale config
|
|
2648
|
+
clearStaleConfig();
|
|
2649
|
+
config = null;
|
|
2650
|
+
}
|
|
2651
|
+
|
|
2652
|
+
if (alive) {
|
|
2653
|
+
// Headless mode — daemon already running, just report and exit
|
|
2654
|
+
if (headlessMode) {
|
|
2655
|
+
var protocol = config.tls ? "https" : "http";
|
|
2656
|
+
var ip = getLocalIP();
|
|
2657
|
+
var url = config.builtinCert
|
|
2658
|
+
? toClayStudioUrl(ip, config.port, protocol)
|
|
2659
|
+
: protocol + "://" + ip + ":" + config.port;
|
|
2660
|
+
console.log(" " + sym.done + " Daemon already running (PID " + config.pid + ")");
|
|
2661
|
+
console.log(" " + sym.done + " " + url);
|
|
2662
|
+
if (config.builtinCert) console.log(" " + sym.done + " d.clay.studio provides HTTPS certificates only. Your traffic never leaves your network.");
|
|
2663
|
+
process.exit(0);
|
|
2664
|
+
return;
|
|
2665
|
+
}
|
|
2666
|
+
|
|
2667
|
+
// Daemon is running — auto-add cwd if needed, then show menu
|
|
2668
|
+
var ip = getLocalIP();
|
|
2669
|
+
|
|
2670
|
+
var status = await sendIPCCommand(socketPath(), { cmd: "get_status" });
|
|
2671
|
+
if (!status.ok) {
|
|
2672
|
+
log(a.red + "Daemon not responding" + a.reset);
|
|
2673
|
+
clearStaleConfig();
|
|
2674
|
+
process.exit(1);
|
|
2675
|
+
return;
|
|
2676
|
+
}
|
|
2677
|
+
|
|
2678
|
+
// Check if cwd needs to be added
|
|
2679
|
+
var projs = status.projects || [];
|
|
2680
|
+
var cwdRegistered = false;
|
|
2681
|
+
for (var j = 0; j < projs.length; j++) {
|
|
2682
|
+
if (projs[j].path === cwd) {
|
|
2683
|
+
cwdRegistered = true;
|
|
2684
|
+
break;
|
|
2685
|
+
}
|
|
2686
|
+
}
|
|
2687
|
+
|
|
2688
|
+
if (!cwdRegistered) {
|
|
2689
|
+
var slug = path.basename(cwd).toLowerCase().replace(/[^a-z0-9_-]/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "") || "project";
|
|
2690
|
+
console.clear();
|
|
2691
|
+
printLogo();
|
|
2692
|
+
log("");
|
|
2693
|
+
log(sym.pointer + " " + a.bold + "Add this project?" + a.reset);
|
|
2694
|
+
log(sym.bar);
|
|
2695
|
+
log(sym.bar + " " + a.dim + cwd + a.reset);
|
|
2696
|
+
log(sym.bar);
|
|
2697
|
+
promptSelect("Add " + a.green + slug + a.reset + " to relay?", [
|
|
2698
|
+
{ label: "Yes", value: "yes" },
|
|
2699
|
+
{ label: "No", value: "no" },
|
|
2700
|
+
], function (answer) {
|
|
2701
|
+
if (answer === "yes") {
|
|
2702
|
+
sendIPCCommand(socketPath(), { cmd: "add_project", path: cwd }).then(function (res) {
|
|
2703
|
+
if (res.ok) {
|
|
2704
|
+
config = loadConfig() || config;
|
|
2705
|
+
log(sym.done + " " + a.green + "Added: " + (res.slug || slug) + a.reset);
|
|
2706
|
+
}
|
|
2707
|
+
log("");
|
|
2708
|
+
showMainMenu(config || { pid: status.pid, port: status.port, tls: status.tls }, ip);
|
|
2709
|
+
});
|
|
2710
|
+
} else {
|
|
2711
|
+
showMainMenu(config || { pid: status.pid, port: status.port, tls: status.tls }, ip);
|
|
2712
|
+
}
|
|
2713
|
+
});
|
|
2714
|
+
} else {
|
|
2715
|
+
showMainMenu(config || { pid: status.pid, port: status.port, tls: status.tls }, ip);
|
|
2716
|
+
}
|
|
2717
|
+
} else {
|
|
2718
|
+
// No daemon running — check for saved config (repeat run)
|
|
2719
|
+
var savedConfig = loadConfig();
|
|
2720
|
+
var isRepeatRun = savedConfig && savedConfig.setupCompleted;
|
|
2721
|
+
|
|
2722
|
+
// --multi-user / --os-users CLI flags set config directly for headless/scripted usage
|
|
2723
|
+
if (multiUserMode) {
|
|
2724
|
+
if (!savedConfig) savedConfig = {};
|
|
2725
|
+
savedConfig.mode = "multi";
|
|
2726
|
+
savedConfig.setupCompleted = true;
|
|
2727
|
+
}
|
|
2728
|
+
if (osUsersMode) {
|
|
2729
|
+
if (!savedConfig) savedConfig = {};
|
|
2730
|
+
savedConfig.osUsers = true;
|
|
2731
|
+
savedConfig.mode = "multi";
|
|
2732
|
+
savedConfig.setupCompleted = true;
|
|
2733
|
+
}
|
|
2734
|
+
isRepeatRun = savedConfig && savedConfig.setupCompleted;
|
|
2735
|
+
|
|
2736
|
+
if (isRepeatRun || autoYes) {
|
|
2737
|
+
// Repeat run or --yes: skip wizard, reuse saved config
|
|
2738
|
+
var savedMode = (savedConfig && savedConfig.mode) || "single";
|
|
2739
|
+
var savedKeepAwake = (savedConfig && savedConfig.keepAwake) || false;
|
|
2740
|
+
var savedOsUsers = (savedConfig && savedConfig.osUsers) || false;
|
|
2741
|
+
|
|
2742
|
+
// os-users requires root
|
|
2743
|
+
if (savedOsUsers && typeof process.getuid === "function" && process.getuid() !== 0) {
|
|
2744
|
+
console.error(a.red + "OS user isolation requires root." + a.reset);
|
|
2745
|
+
console.error("Run: " + a.bold + "sudo npx @clagentic/console" + a.reset);
|
|
2746
|
+
process.exit(1);
|
|
2747
|
+
return;
|
|
2748
|
+
}
|
|
2749
|
+
|
|
2750
|
+
// os-users requires setfacl (ACL package)
|
|
2751
|
+
if (savedOsUsers && process.platform === "linux") {
|
|
2752
|
+
var { checkAclSupport } = require("../lib/os-users");
|
|
2753
|
+
var aclCheck = checkAclSupport();
|
|
2754
|
+
if (!aclCheck.available) {
|
|
2755
|
+
console.error(a.red + "OS user isolation requires the 'acl' package (setfacl)." + a.reset);
|
|
2756
|
+
console.error("");
|
|
2757
|
+
console.error("Install it: " + a.bold + aclCheck.installCmd + a.reset);
|
|
2758
|
+
console.error("");
|
|
2759
|
+
console.error("Then restart Clay.");
|
|
2760
|
+
process.exit(1);
|
|
2761
|
+
return;
|
|
2762
|
+
}
|
|
2763
|
+
}
|
|
2764
|
+
|
|
2765
|
+
if (savedConfig && savedConfig.port) port = savedConfig.port;
|
|
2766
|
+
if (savedConfig && savedConfig.host) host = savedConfig.host;
|
|
2767
|
+
if (savedConfig && savedConfig.dangerouslySkipPermissions) dangerouslySkipPermissions = true;
|
|
2768
|
+
|
|
2769
|
+
if (autoYes) {
|
|
2770
|
+
console.log(" " + sym.done + " Auto-accepted disclaimer");
|
|
2771
|
+
console.log(" " + sym.done + " Mode: " + savedMode);
|
|
2772
|
+
if (dangerouslySkipPermissions) {
|
|
2773
|
+
console.log(" " + sym.warn + " " + a.yellow + "Skip permissions mode enabled" + a.reset);
|
|
2774
|
+
}
|
|
2775
|
+
}
|
|
2776
|
+
|
|
2777
|
+
var autoRc = loadClayrc();
|
|
2778
|
+
var autoRestorable = (autoRc.recentProjects || []).filter(function (p) {
|
|
2779
|
+
return p.path !== cwd && fs.existsSync(p.path);
|
|
2780
|
+
});
|
|
2781
|
+
if (autoRestorable.length > 0 && autoYes) {
|
|
2782
|
+
console.log(" " + sym.done + " Restoring " + autoRestorable.length + " previous project(s)");
|
|
2783
|
+
}
|
|
2784
|
+
// Add cwd if it has history in .clayrc, or if there are no other projects to restore
|
|
2785
|
+
var cwdInRc = (autoRc.recentProjects || []).some(function (p) {
|
|
2786
|
+
return p.path === cwd;
|
|
2787
|
+
});
|
|
2788
|
+
var addCwd = cwdInRc || autoRestorable.length === 0;
|
|
2789
|
+
await forkDaemon(savedMode, savedKeepAwake, autoRestorable.length > 0 ? autoRestorable : undefined, addCwd, savedOsUsers);
|
|
2790
|
+
} else {
|
|
2791
|
+
// First run: interactive wizard
|
|
2792
|
+
setup(function (mode, keepAwake, wantOsUsers) {
|
|
2793
|
+
// Check ~/.clayrc for previous projects to restore
|
|
2794
|
+
var rc = loadClayrc();
|
|
2795
|
+
var restorable = (rc.recentProjects || []).filter(function (p) {
|
|
2796
|
+
return p.path !== cwd && fs.existsSync(p.path);
|
|
2797
|
+
});
|
|
2798
|
+
|
|
2799
|
+
if (restorable.length > 0) {
|
|
2800
|
+
promptRestoreProjects(restorable, function (selected) {
|
|
2801
|
+
forkDaemon(mode, keepAwake, selected, false, wantOsUsers);
|
|
2802
|
+
});
|
|
2803
|
+
} else {
|
|
2804
|
+
log(sym.bar);
|
|
2805
|
+
log(sym.end + " " + a.dim + "Starting relay..." + a.reset);
|
|
2806
|
+
log("");
|
|
2807
|
+
forkDaemon(mode, keepAwake, undefined, true, wantOsUsers);
|
|
2808
|
+
}
|
|
2809
|
+
});
|
|
2810
|
+
}
|
|
2811
|
+
}
|
|
2812
|
+
})();
|