@infernetprotocol/cli 0.1.22

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.
Files changed (50) hide show
  1. package/LICENSE +15 -0
  2. package/commands/chat.js +268 -0
  3. package/commands/console.js +229 -0
  4. package/commands/debug.js +222 -0
  5. package/commands/deploy.js +524 -0
  6. package/commands/doctor.js +501 -0
  7. package/commands/firewall.js +39 -0
  8. package/commands/gpu.js +130 -0
  9. package/commands/help.js +54 -0
  10. package/commands/init.js +304 -0
  11. package/commands/login.js +305 -0
  12. package/commands/logs.js +93 -0
  13. package/commands/model.js +359 -0
  14. package/commands/payments.js +92 -0
  15. package/commands/payout.js +128 -0
  16. package/commands/pubkey.js +116 -0
  17. package/commands/register.js +304 -0
  18. package/commands/remove.js +230 -0
  19. package/commands/service.js +255 -0
  20. package/commands/setup.js +869 -0
  21. package/commands/start.js +903 -0
  22. package/commands/stats.js +85 -0
  23. package/commands/status.js +116 -0
  24. package/commands/stop.js +81 -0
  25. package/commands/train.js +563 -0
  26. package/commands/tui.js +285 -0
  27. package/commands/uncensored.js +222 -0
  28. package/commands/update.js +80 -0
  29. package/commands/upgrade.js +2 -0
  30. package/examples/app.js +153 -0
  31. package/index.js +207 -0
  32. package/lib/chat-executor.js +232 -0
  33. package/lib/config.js +148 -0
  34. package/lib/daemonize.js +46 -0
  35. package/lib/firewall.js +190 -0
  36. package/lib/hf-model.js +166 -0
  37. package/lib/identity.js +18 -0
  38. package/lib/ipc.js +114 -0
  39. package/lib/model-fit.js +67 -0
  40. package/lib/model-key.js +52 -0
  41. package/lib/network.js +240 -0
  42. package/lib/nip44.js +130 -0
  43. package/lib/node-client.js +152 -0
  44. package/lib/peers.js +133 -0
  45. package/lib/prompt.js +49 -0
  46. package/lib/remote-chat.js +115 -0
  47. package/lib/route.js +44 -0
  48. package/lib/runtime-config.js +130 -0
  49. package/lib/version.js +32 -0
  50. package/package.json +57 -0
@@ -0,0 +1,85 @@
1
+ /**
2
+ * `infernet stats` — query the running daemon via IPC and print its
3
+ * in-memory snapshot (heartbeats, polls, active jobs, uptime, P2P).
4
+ */
5
+
6
+ import { isDaemonAlive, sendToDaemon } from '../lib/ipc.js';
7
+
8
+ const HELP = `infernet stats — show live daemon stats via IPC
9
+
10
+ Usage:
11
+ infernet stats [flags]
12
+
13
+ Flags:
14
+ --json Print the raw JSON snapshot
15
+ --help Show this help
16
+
17
+ Requires a running daemon (\`infernet start\`). For the persisted
18
+ per-node state from the control plane, use \`infernet status\` instead.
19
+ `;
20
+
21
+ function fmtUptime(ms) {
22
+ if (!Number.isFinite(ms) || ms < 0) return '?';
23
+ const s = Math.floor(ms / 1000);
24
+ const h = Math.floor(s / 3600);
25
+ const m = Math.floor((s % 3600) / 60);
26
+ const r = s % 60;
27
+ if (h > 0) return `${h}h${m}m${r}s`;
28
+ if (m > 0) return `${m}m${r}s`;
29
+ return `${r}s`;
30
+ }
31
+
32
+ export default async function stats(args) {
33
+ if (args.has('help') || args.has('h')) {
34
+ process.stdout.write(HELP);
35
+ return 0;
36
+ }
37
+
38
+ const alive = await isDaemonAlive();
39
+ if (!alive) {
40
+ process.stderr.write('Daemon not reachable (socket missing or unresponsive). Run `infernet start` first.\n');
41
+ return 1;
42
+ }
43
+
44
+ const res = await sendToDaemon('stats');
45
+ if (!res.ok) {
46
+ process.stderr.write(`IPC error: ${res.error}${res.cause ? ` (${res.cause})` : ''}\n`);
47
+ return 1;
48
+ }
49
+
50
+ if (args.has('json')) {
51
+ process.stdout.write(JSON.stringify(res.data, null, 2) + '\n');
52
+ return 0;
53
+ }
54
+
55
+ const d = res.data;
56
+ process.stdout.write(`Daemon pid: ${d.pid}\n`);
57
+ process.stdout.write(`Started at: ${d.startedAt}\n`);
58
+ process.stdout.write(`Uptime: ${fmtUptime(d.uptimeMs)}\n`);
59
+ process.stdout.write(`Node: ${d.node.nodeId} (${d.node.role})\n`);
60
+ process.stdout.write(`Control plane: ${d.controlPlaneUrl ?? d.supabaseUrl ?? '(not set)'}\n`);
61
+ process.stdout.write(`Intervals: heartbeat=${d.intervals.heartbeatMs}ms poll=${d.intervals.pollMs}ms\n`);
62
+
63
+ if (d.p2p?.enabled) {
64
+ process.stdout.write(`P2P: ${d.p2p.endpoint} (connections: ${d.p2p.connectionsTotal}`);
65
+ if (d.p2p.lastConnectionAt) {
66
+ process.stdout.write(`, last: ${d.p2p.lastConnectionAt}`);
67
+ }
68
+ process.stdout.write(')\n');
69
+ } else {
70
+ process.stdout.write('P2P: disabled\n');
71
+ }
72
+
73
+ const s = d.stats ?? {};
74
+ process.stdout.write(`\nHeartbeats: ok=${s.heartbeatsOk} failed=${s.heartbeatsFailed} last=${s.lastHeartbeatAt ?? '-'}\n`);
75
+ if (s.lastHeartbeatError) process.stdout.write(` last error: ${s.lastHeartbeatError}\n`);
76
+ process.stdout.write(`Polls: ok=${s.pollsOk} failed=${s.pollsFailed} last=${s.lastPollAt ?? '-'}\n`);
77
+ process.stdout.write(`Jobs: picked=${s.jobsPicked} completed=${s.jobsCompleted} failed=${s.jobsFailed} active=${s.activeJobs}\n`);
78
+ if (s.activeJobs > 0) {
79
+ process.stdout.write(` active ids: ${s.activeJobIds.join(', ')}\n`);
80
+ }
81
+ if (s.lastJobAt) process.stdout.write(` last job at: ${s.lastJobAt}\n`);
82
+ return 0;
83
+ }
84
+
85
+ export { HELP };
@@ -0,0 +1,116 @@
1
+ /**
2
+ * `infernet status` — print the current state of this node.
3
+ *
4
+ * Fetches the server-side row via signed POST to /api/v1/node/me and
5
+ * merges it with the live daemon IPC snapshot when the daemon is running.
6
+ */
7
+
8
+ import { isDaemonAlive, sendToDaemon } from '../lib/ipc.js';
9
+ import { formatEndpoint } from '../lib/network.js';
10
+
11
+ const HELP = `infernet status — show this node's state
12
+
13
+ Usage:
14
+ infernet status [flags]
15
+
16
+ Flags:
17
+ --json Print JSON combining remote row + live daemon snapshot
18
+ --help Show this help
19
+ `;
20
+
21
+ function fmtUptime(ms) {
22
+ if (!Number.isFinite(ms) || ms < 0) return '?';
23
+ const s = Math.floor(ms / 1000);
24
+ const h = Math.floor(s / 3600);
25
+ const m = Math.floor((s % 3600) / 60);
26
+ const r = s % 60;
27
+ if (h > 0) return `${h}h${m}m${r}s`;
28
+ if (m > 0) return `${m}m${r}s`;
29
+ return `${r}s`;
30
+ }
31
+
32
+ export default async function status(args, ctx) {
33
+ if (args.has('help') || args.has('h')) {
34
+ process.stdout.write(HELP);
35
+ return 0;
36
+ }
37
+
38
+ const { config, client } = ctx;
39
+ const node = config.node ?? {};
40
+ if (!node.role || !node.nodeId) {
41
+ process.stderr.write('Config is missing node.role/node.nodeId. Run `infernet init` first.\n');
42
+ return 1;
43
+ }
44
+
45
+ let row = null;
46
+ try {
47
+ const result = await client.me();
48
+ row = result?.row ?? null;
49
+ } catch (err) {
50
+ process.stderr.write(`Remote status lookup failed: ${err?.message ?? err}\n`);
51
+ // Keep going — we can still show the daemon snapshot and local config.
52
+ }
53
+
54
+ let daemon = null;
55
+ if (await isDaemonAlive()) {
56
+ const res = await sendToDaemon('status', null, { timeoutMs: 2000 });
57
+ if (res.ok) daemon = res.data;
58
+ }
59
+
60
+ if (args.has('json')) {
61
+ process.stdout.write(JSON.stringify({ row, daemon }, null, 2) + '\n');
62
+ return 0;
63
+ }
64
+
65
+ process.stdout.write(`Role: ${node.role}\n`);
66
+ process.stdout.write(`Name: ${row?.name ?? node.name ?? '(unnamed)'}\n`);
67
+ process.stdout.write(`node_id: ${row?.node_id ?? node.nodeId}\n`);
68
+ if (row?.id) process.stdout.write(`uuid: ${row.id}\n`);
69
+ process.stdout.write(
70
+ `Status: ${row?.status ?? '(unknown)'}${daemon ? ' (daemon running)' : ' (daemon not running)'}\n`
71
+ );
72
+ process.stdout.write(`Last seen: ${row?.last_seen ?? '(never)'}\n`);
73
+ if (row?.reputation !== undefined) process.stdout.write(`Reputation: ${row.reputation}\n`);
74
+ if (row?.price !== undefined) process.stdout.write(`Price: ${row.price}\n`);
75
+ if (row?.address) {
76
+ process.stdout.write(`Endpoint: ${formatEndpoint(row.address, row.port ?? 0)}\n`);
77
+ }
78
+
79
+ if (row?.specs?.gpus && Array.isArray(row.specs.gpus) && row.specs.gpus.length > 0) {
80
+ process.stdout.write(`GPUs: ${row.specs.gpus.length}\n`);
81
+ for (const g of row.specs.gpus) {
82
+ const tier = g.vram_tier ?? 'unknown';
83
+ const vendor = g.vendor ?? 'unknown';
84
+ const model = g.model ? ` (${g.model})` : '';
85
+ process.stdout.write(` - ${vendor}:${tier}${model}\n`);
86
+ }
87
+ }
88
+
89
+ if (daemon) {
90
+ process.stdout.write(`\nDaemon: pid=${daemon.pid}, uptime=${fmtUptime(daemon.uptimeMs)}\n`);
91
+ if (daemon.p2p?.enabled) {
92
+ process.stdout.write(` p2p: ${daemon.p2p.endpoint} (connections: ${daemon.p2p.connectionsTotal})\n`);
93
+ }
94
+ const s = daemon.stats ?? {};
95
+ process.stdout.write(` heartbeats: ok=${s.heartbeatsOk} failed=${s.heartbeatsFailed}\n`);
96
+ process.stdout.write(` jobs: completed=${s.jobsCompleted} active=${s.activeJobs}\n`);
97
+ }
98
+
99
+ if (node.role === 'provider') {
100
+ try {
101
+ const { rows } = await client.listPayments(100);
102
+ const sum = (list) => list.reduce((a, t) => a + (Number.parseFloat(t.amount_usd) || 0), 0);
103
+ const outbound = (rows ?? []).filter((t) => t.direction === 'outbound');
104
+ const confirmed = outbound.filter((t) => t.status === 'confirmed');
105
+ const pending = outbound.filter((t) => t.status === 'pending');
106
+ process.stdout.write(
107
+ `Earnings: $${sum(confirmed).toFixed(2)} confirmed, $${sum(pending).toFixed(2)} pending (USD)\n`
108
+ );
109
+ } catch (err) {
110
+ process.stderr.write(`earnings query failed: ${err?.message ?? err}\n`);
111
+ }
112
+ }
113
+ return 0;
114
+ }
115
+
116
+ export { HELP };
@@ -0,0 +1,81 @@
1
+ /**
2
+ * `infernet stop` — ask the running daemon to shut down.
3
+ *
4
+ * Preferred path: send a `shutdown` message over the IPC socket. The
5
+ * daemon flips its row to `status='offline'`, removes the pid/sock files,
6
+ * and exits cleanly.
7
+ *
8
+ * Fallback: read ~/.config/infernet/daemon.pid and send SIGTERM (or a
9
+ * custom --signal) directly to the process.
10
+ */
11
+
12
+ import fs from 'node:fs/promises';
13
+
14
+ import { getDaemonPidPath } from '../lib/config.js';
15
+ import { isDaemonAlive, sendToDaemon } from '../lib/ipc.js';
16
+
17
+ const HELP = `infernet stop — signal the running daemon to exit
18
+
19
+ Usage:
20
+ infernet stop [flags]
21
+
22
+ Flags:
23
+ --signal <name> Skip IPC and send this signal directly (default SIGTERM)
24
+ --force Alias for --signal SIGKILL
25
+ --help Show this help
26
+ `;
27
+
28
+ export default async function stop(args) {
29
+ if (args.has('help') || args.has('h')) {
30
+ process.stdout.write(HELP);
31
+ return 0;
32
+ }
33
+
34
+ const explicitSignal = args.get('signal') ?? (args.has('force') ? 'SIGKILL' : null);
35
+
36
+ // Prefer graceful IPC-driven shutdown when available.
37
+ if (!explicitSignal && (await isDaemonAlive())) {
38
+ const res = await sendToDaemon('shutdown', null, { timeoutMs: 5000 });
39
+ if (res.ok) {
40
+ process.stdout.write('Daemon shutting down (IPC).\n');
41
+ return 0;
42
+ }
43
+ process.stderr.write(
44
+ `IPC shutdown failed (${res.error}${res.cause ? `: ${res.cause}` : ''}); falling back to signal.\n`
45
+ );
46
+ }
47
+
48
+ // Fallback: read pidfile and signal.
49
+ const signal = explicitSignal ?? 'SIGTERM';
50
+ const pidPath = getDaemonPidPath();
51
+ let raw;
52
+ try { raw = await fs.readFile(pidPath, 'utf8'); }
53
+ catch (err) {
54
+ if (err.code === 'ENOENT') {
55
+ process.stderr.write(`No daemon PID file at ${pidPath}. Is the daemon running?\n`);
56
+ return 1;
57
+ }
58
+ throw err;
59
+ }
60
+ const pid = Number.parseInt(raw.trim(), 10);
61
+ if (!Number.isFinite(pid) || pid <= 0) {
62
+ process.stderr.write(`PID file at ${pidPath} is malformed: "${raw}"\n`);
63
+ return 1;
64
+ }
65
+
66
+ try {
67
+ process.kill(pid, signal);
68
+ process.stdout.write(`Sent ${signal} to pid ${pid}\n`);
69
+ } catch (err) {
70
+ if (err.code === 'ESRCH') {
71
+ process.stderr.write(`Process ${pid} is not running; cleaning up stale PID file.\n`);
72
+ try { await fs.unlink(pidPath); } catch { /* ignore */ }
73
+ return 1;
74
+ }
75
+ process.stderr.write(`Failed to signal ${pid}: ${err.message}\n`);
76
+ return 1;
77
+ }
78
+ return 0;
79
+ }
80
+
81
+ export { HELP };