@sharedots/cli 0.1.1 → 0.1.3

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/src/up-wizard.ts DELETED
@@ -1,338 +0,0 @@
1
- import { execSync } from "node:child_process";
2
- import os from "node:os";
3
- import readline from "node:readline";
4
- import { decodeInviteCode } from "@sharedots/protocol";
5
- import { readConfig, startDaemonProcess, writeConfig } from "./daemon-runner.js";
6
-
7
- export interface SystemSpecs {
8
- cpuCores: number;
9
- cpuModel: string;
10
- totalMemoryBytes: number;
11
- totalMemoryGb: number;
12
- gpuDetected: boolean;
13
- gpuName?: string;
14
- osName: string;
15
- }
16
-
17
- export function probeSystemSpecs(): SystemSpecs {
18
- const cpus = os.cpus();
19
- const cpuCores = cpus.length;
20
- const cpuModel = cpus[0]?.model || "Generic CPU";
21
- const totalMemoryBytes = os.totalmem();
22
- const totalMemoryGb = Math.round((totalMemoryBytes / (1024 ** 3)) * 10) / 10;
23
- const osName = `${os.type()} ${os.release()} (${os.arch()})`;
24
-
25
- let gpuDetected = false;
26
- let gpuName: string | undefined;
27
-
28
- // Check for nvidia-smi
29
- try {
30
- const smi = execSync("nvidia-smi --query-gpu=name --format=csv,noheader", {
31
- encoding: "utf8",
32
- stdio: ["pipe", "pipe", "ignore"],
33
- timeout: 1500,
34
- }).trim();
35
- if (smi) {
36
- gpuDetected = true;
37
- gpuName = smi.split("\n")[0]?.trim();
38
- }
39
- } catch {
40
- // No nvidia-smi, check Windows WMIC or PowerShell if win32
41
- if (process.platform === "win32") {
42
- try {
43
- const winGpu = execSync(
44
- 'powershell -NoProfile -Command "(Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name)[0]"',
45
- { encoding: "utf8", stdio: ["pipe", "pipe", "ignore"], timeout: 1500 },
46
- ).trim();
47
- if (winGpu && !winGpu.toLowerCase().includes("basic display") && !winGpu.toLowerCase().includes("remote")) {
48
- gpuDetected = true;
49
- gpuName = winGpu;
50
- }
51
- } catch {
52
- // ignore
53
- }
54
- }
55
- }
56
-
57
- return {
58
- cpuCores,
59
- cpuModel,
60
- totalMemoryBytes,
61
- totalMemoryGb,
62
- gpuDetected,
63
- gpuName,
64
- osName,
65
- };
66
- }
67
-
68
- export async function detectCoordinator(customUrls: string[] = []): Promise<string | null> {
69
- const candidateUrls = [
70
- ...customUrls,
71
- "http://127.0.0.1:4000",
72
- "http://localhost:4000",
73
- ];
74
-
75
- // Try checking tailscale peers if tailscale is installed
76
- try {
77
- const tsOut = execSync("tailscale status --json", {
78
- encoding: "utf8",
79
- stdio: ["pipe", "pipe", "ignore"],
80
- timeout: 1000,
81
- });
82
- const parsed = JSON.parse(tsOut) as { Peer?: Record<string, { TailscaleIPs?: string[] }> };
83
- if (parsed.Peer) {
84
- for (const peer of Object.values(parsed.Peer)) {
85
- if (peer.TailscaleIPs && peer.TailscaleIPs[0]) {
86
- candidateUrls.push(`http://${peer.TailscaleIPs[0]}:4000`);
87
- }
88
- }
89
- }
90
- } catch {
91
- // Tailscale not running or not installed
92
- }
93
-
94
- for (const url of candidateUrls) {
95
- try {
96
- const res = await fetch(`${url.replace(/\/+$/, "")}/api/overview`, {
97
- signal: AbortSignal.timeout(1000),
98
- });
99
- if (res.ok) {
100
- return url.replace(/\/+$/, "");
101
- }
102
- } catch {
103
- // transient unreachable
104
- }
105
- }
106
-
107
- return null;
108
- }
109
-
110
- export interface UpWizardOptions {
111
- cluster?: boolean;
112
- contributor?: boolean;
113
- joinCode?: string;
114
- coordinator?: string;
115
- yes?: boolean;
116
- foreground?: boolean;
117
- }
118
-
119
- function promptUser(query: string): Promise<string> {
120
- const rl = readline.createInterface({
121
- input: process.stdin,
122
- output: process.stdout,
123
- });
124
- return new Promise((res) => {
125
- rl.question(query, (ans) => {
126
- rl.close();
127
- res(ans.trim());
128
- });
129
- });
130
- }
131
-
132
- export async function runUpWizard(options: UpWizardOptions = {}): Promise<void> {
133
- console.log(`\n\x1b[1m\x1b[36m⚡ SHAREDOTS ZERO-CONFIG AUTO-DISCOVERY & LAUNCH WIZARD\x1b[0m\n`);
134
-
135
- const specs = probeSystemSpecs();
136
- console.log(`\x1b[32m✔ Host Hardware Detected:\x1b[0m`);
137
- console.log(` • CPU: ${specs.cpuCores} cores (${specs.cpuModel})`);
138
- console.log(` • Memory: ${specs.totalMemoryGb} GB`);
139
- console.log(` • GPU: ${specs.gpuDetected ? (specs.gpuName ?? "Yes") : "None / Integrated"}`);
140
- console.log(` • OS: ${specs.osName}\n`);
141
-
142
- // Direct join code provided
143
- if (options.joinCode) {
144
- const decoded = decodeInviteCode(options.joinCode);
145
- if (!decoded) {
146
- console.error("\x1b[31m✖ Error: Invalid invite code provided.\x1b[0m");
147
- process.exit(1);
148
- }
149
- console.log(`Configuring worker for room ${decoded.roomId} (${decoded.coordinatorUrl})...`);
150
- writeConfig({
151
- coordinator: decoded.coordinatorUrl,
152
- roomId: decoded.roomId,
153
- token: decoded.token,
154
- joinedAt: new Date().toISOString(),
155
- });
156
- const started = startDaemonProcess({
157
- coordinator: decoded.coordinatorUrl,
158
- roomId: decoded.roomId,
159
- token: decoded.token,
160
- foreground: options.foreground,
161
- });
162
- if (started.pid) {
163
- console.log(`\x1b[32m✔ Daemon running in background (PID ${started.pid}).\x1b[0m`);
164
- console.log(`Run 'sharedots worker status' or 'sharedots tui' to monitor.\n`);
165
- }
166
- return;
167
- }
168
-
169
- // Explicit cluster flag
170
- if (options.cluster) {
171
- console.log(`Starting new local Sharedots cluster...`);
172
- console.log(`Run 'pnpm coordinator:start' or launch coordinator on port 4000.\n`);
173
- return;
174
- }
175
-
176
- // Explicit contributor mode flag
177
- if (options.contributor) {
178
- console.log(`Configuring auto-tuned contributor node...`);
179
- const existing = readConfig();
180
- const coord = options.coordinator ?? existing?.coordinator ?? "http://127.0.0.1:4000";
181
- const room = existing?.roomId ?? "public-pool";
182
- const token = existing?.token ?? "sec_public";
183
-
184
- writeConfig({ coordinator: coord, roomId: room, token, joinedAt: new Date().toISOString() });
185
- const started = startDaemonProcess({
186
- coordinator: coord,
187
- roomId: room,
188
- token,
189
- cpuLimit: 75,
190
- foreground: options.foreground,
191
- });
192
- console.log(`\x1b[32m✔ Contributor node running (PID ${started.pid}). Contributing up to 75% CPU.\x1b[0m\n`);
193
- return;
194
- }
195
-
196
- // Probe for active coordinators
197
- process.stdout.write("Scanning local network and Tailscale mesh for active coordinators... ");
198
- const detectedCoord = await detectCoordinator(options.coordinator ? [options.coordinator] : []);
199
- if (detectedCoord) {
200
- console.log(`\x1b[32mFound!\x1b[0m`);
201
- console.log(`\x1b[1m\x1b[35m● Active coordinator discovered at: ${detectedCoord}\x1b[0m\n`);
202
-
203
- let joinNow = options.yes;
204
- if (!joinNow && process.stdin.isTTY) {
205
- const ans = await promptUser(`Join active coordinator at ${detectedCoord}? [Y/n]: `);
206
- joinNow = ans === "" || ans.toLowerCase().startsWith("y");
207
- }
208
-
209
- if (joinNow) {
210
- // Fetch or probe active rooms
211
- let roomId = "mesh-default";
212
- let token = "sec_auto";
213
- try {
214
- const roomsRes = await fetch(`${detectedCoord}/api/rooms`, {
215
- signal: AbortSignal.timeout(2000),
216
- });
217
- if (roomsRes.ok) {
218
- const body = (await roomsRes.json()) as { rooms?: Array<{ roomId: string }> };
219
- if (body.rooms && body.rooms.length > 0 && body.rooms[0]) {
220
- roomId = body.rooms[0].roomId;
221
- }
222
- }
223
- } catch {
224
- // ignore
225
- }
226
-
227
- writeConfig({
228
- coordinator: detectedCoord,
229
- roomId,
230
- token,
231
- joinedAt: new Date().toISOString(),
232
- });
233
- const started = startDaemonProcess({
234
- coordinator: detectedCoord,
235
- roomId,
236
- token,
237
- foreground: options.foreground,
238
- });
239
- console.log(`\x1b[32m✔ Connected to cluster at ${detectedCoord} (room: ${roomId}).\x1b[0m`);
240
- if (started.pid) {
241
- console.log(`Worker daemon started (PID ${started.pid}). Run 'sharedots tui' to view dashboard.\n`);
242
- }
243
- return;
244
- }
245
- } else {
246
- console.log(`None detected.`);
247
- }
248
-
249
- // Interactive selection if TTY
250
- if (process.stdin.isTTY) {
251
- console.log(`How do you want to run Sharedots?`);
252
- console.log(` [1] Join existing mesh (paste invite code or enter URL)`);
253
- console.log(` [2] Start new cluster (Coordinator + local Worker)`);
254
- console.log(` [3] Contributor mode (Worker only, auto-tuned to 75% CPU)`);
255
-
256
- const choice = await promptUser(`Select option [1-3] (default 1): `);
257
-
258
- if (choice === "2") {
259
- console.log(`Starting new local Sharedots cluster...`);
260
- // Start cluster
261
- console.log(`Run 'pnpm coordinator:start' or launch coordinator on port 4000.`);
262
- return;
263
- }
264
-
265
- if (choice === "3") {
266
- console.log(`Configuring auto-tuned contributor node...`);
267
- const existing = readConfig();
268
- const coord = existing?.coordinator ?? detectedCoord ?? "http://127.0.0.1:4000";
269
- const room = existing?.roomId ?? "public-pool";
270
- const token = existing?.token ?? "sec_public";
271
-
272
- writeConfig({ coordinator: coord, roomId: room, token, joinedAt: new Date().toISOString() });
273
- const started = startDaemonProcess({
274
- coordinator: coord,
275
- roomId: room,
276
- token,
277
- cpuLimit: 75,
278
- foreground: options.foreground,
279
- });
280
- console.log(`\x1b[32m✔ Contributor node running (PID ${started.pid}). Contributing up to 75% CPU.\x1b[0m\n`);
281
- return;
282
- }
283
-
284
- // Default option 1: join
285
- const rawInput = await promptUser(`Paste Unified Invite Code ('sd_join_...') or Coordinator URL: `);
286
- if (!rawInput) {
287
- console.log("No input received. Exiting.");
288
- return;
289
- }
290
- const decoded = decodeInviteCode(rawInput);
291
- if (decoded) {
292
- writeConfig({
293
- coordinator: decoded.coordinatorUrl,
294
- roomId: decoded.roomId,
295
- token: decoded.token,
296
- joinedAt: new Date().toISOString(),
297
- });
298
- const started = startDaemonProcess({
299
- coordinator: decoded.coordinatorUrl,
300
- roomId: decoded.roomId,
301
- token: decoded.token,
302
- foreground: options.foreground,
303
- });
304
- console.log(`\x1b[32m✔ Worker successfully configured and launched (PID ${started.pid}).\x1b[0m\n`);
305
- return;
306
- }
307
-
308
- console.log(`Connecting to ${rawInput}...`);
309
- writeConfig({
310
- coordinator: rawInput,
311
- roomId: "default-room",
312
- token: "sec_default",
313
- joinedAt: new Date().toISOString(),
314
- });
315
- startDaemonProcess({
316
- coordinator: rawInput,
317
- roomId: "default-room",
318
- token: "sec_default",
319
- foreground: options.foreground,
320
- });
321
- console.log(`\x1b[32m✔ Worker started.\x1b[0m\n`);
322
- } else {
323
- // Non-interactive fallback
324
- const existing = readConfig();
325
- const coord = existing?.coordinator ?? detectedCoord ?? "http://127.0.0.1:4000";
326
- const room = existing?.roomId ?? "mesh-default";
327
- const token = existing?.token ?? "sec_auto";
328
-
329
- writeConfig({ coordinator: coord, roomId: room, token, joinedAt: new Date().toISOString() });
330
- const started = startDaemonProcess({
331
- coordinator: coord,
332
- roomId: room,
333
- token,
334
- foreground: options.foreground,
335
- });
336
- console.log(`\x1b[32m✔ Sharedots started for room ${room} on ${coord} (PID ${started.pid}).\x1b[0m\n`);
337
- }
338
- }
package/tsconfig.json DELETED
@@ -1,12 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./src",
6
- "paths": {
7
- "@sharedots/tui": ["../../apps/tui/dist/index.d.ts"]
8
- }
9
- },
10
- "include": ["src/**/*.ts"],
11
- "exclude": ["src/**/*.test.ts"]
12
- }