@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.
- package/LICENSE +15 -0
- package/commands/chat.js +268 -0
- package/commands/console.js +229 -0
- package/commands/debug.js +222 -0
- package/commands/deploy.js +524 -0
- package/commands/doctor.js +501 -0
- package/commands/firewall.js +39 -0
- package/commands/gpu.js +130 -0
- package/commands/help.js +54 -0
- package/commands/init.js +304 -0
- package/commands/login.js +305 -0
- package/commands/logs.js +93 -0
- package/commands/model.js +359 -0
- package/commands/payments.js +92 -0
- package/commands/payout.js +128 -0
- package/commands/pubkey.js +116 -0
- package/commands/register.js +304 -0
- package/commands/remove.js +230 -0
- package/commands/service.js +255 -0
- package/commands/setup.js +869 -0
- package/commands/start.js +903 -0
- package/commands/stats.js +85 -0
- package/commands/status.js +116 -0
- package/commands/stop.js +81 -0
- package/commands/train.js +563 -0
- package/commands/tui.js +285 -0
- package/commands/uncensored.js +222 -0
- package/commands/update.js +80 -0
- package/commands/upgrade.js +2 -0
- package/examples/app.js +153 -0
- package/index.js +207 -0
- package/lib/chat-executor.js +232 -0
- package/lib/config.js +148 -0
- package/lib/daemonize.js +46 -0
- package/lib/firewall.js +190 -0
- package/lib/hf-model.js +166 -0
- package/lib/identity.js +18 -0
- package/lib/ipc.js +114 -0
- package/lib/model-fit.js +67 -0
- package/lib/model-key.js +52 -0
- package/lib/network.js +240 -0
- package/lib/nip44.js +130 -0
- package/lib/node-client.js +152 -0
- package/lib/peers.js +133 -0
- package/lib/prompt.js +49 -0
- package/lib/remote-chat.js +115 -0
- package/lib/route.js +44 -0
- package/lib/runtime-config.js +130 -0
- package/lib/version.js +32 -0
- package/package.json +57 -0
package/lib/config.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config helpers for the infernet CLI.
|
|
3
|
+
*
|
|
4
|
+
* Config lives at ~/.config/infernet/config.json with mode 0600.
|
|
5
|
+
*
|
|
6
|
+
* Shape (v2 — signed-API era):
|
|
7
|
+
* {
|
|
8
|
+
* "controlPlane": { "url": "https://infernetprotocol.com" },
|
|
9
|
+
* "node": {
|
|
10
|
+
* "id": "...", // server-assigned uuid after register
|
|
11
|
+
* "nodeId": "...", // human-readable slug e.g. provider-abc123
|
|
12
|
+
* "role": "provider", // provider | aggregator | client
|
|
13
|
+
* "name": "...",
|
|
14
|
+
* "publicKey": "...", // Nostr (secp256k1 / BIP-340 x-only) pubkey hex
|
|
15
|
+
* "privateKey": "...", // Nostr privkey hex — proves ownership on every call
|
|
16
|
+
* "payoutPublicKey": "...", // optional: separate identity for payouts
|
|
17
|
+
* "address": "...|null",
|
|
18
|
+
* "port": 46337
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* Old configs with `supabase.serviceRoleKey` still load (we ignore the key and
|
|
23
|
+
* migrate `supabase.url` -> `controlPlane.url` transparently).
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import fs from 'node:fs/promises';
|
|
27
|
+
import fsSync from 'node:fs';
|
|
28
|
+
import path from 'node:path';
|
|
29
|
+
import os from 'node:os';
|
|
30
|
+
|
|
31
|
+
export function getConfigDir() {
|
|
32
|
+
const xdg = process.env.XDG_CONFIG_HOME;
|
|
33
|
+
const base = xdg && xdg.length > 0 ? xdg : path.join(os.homedir(), '.config');
|
|
34
|
+
return path.join(base, 'infernet');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function getConfigPath() {
|
|
38
|
+
return path.join(getConfigDir(), 'config.json');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function getDaemonPidPath() {
|
|
42
|
+
return path.join(getConfigDir(), 'daemon.pid');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function getDaemonSocketPath() {
|
|
46
|
+
return path.join(getConfigDir(), 'daemon.sock');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function getDaemonLogPath() {
|
|
50
|
+
// Prefer /var/log/infernet/ (standard syslog location, works with logrotate).
|
|
51
|
+
// Fall back to ~/.config/infernet/ if /var/log/infernet/ isn't writable
|
|
52
|
+
// (non-root installs, CI, etc.).
|
|
53
|
+
const dir = '/var/log/infernet';
|
|
54
|
+
try {
|
|
55
|
+
fsSync.mkdirSync(dir, { recursive: true, mode: 0o755 });
|
|
56
|
+
fsSync.accessSync(dir, fsSync.constants.W_OK);
|
|
57
|
+
return path.join(dir, 'daemon.log');
|
|
58
|
+
} catch {
|
|
59
|
+
return path.join(getConfigDir(), 'daemon.log');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns the path where the daemon is actually writing logs right now.
|
|
65
|
+
* Checks both the preferred path and the legacy fallback so that `infernet
|
|
66
|
+
* logs` works even when a daemon started before the /var/log migration is
|
|
67
|
+
* still running.
|
|
68
|
+
*/
|
|
69
|
+
export function resolveLiveDaemonLogPath() {
|
|
70
|
+
const preferred = getDaemonLogPath();
|
|
71
|
+
if (fsSync.existsSync(preferred)) return preferred;
|
|
72
|
+
const legacy = path.join(getConfigDir(), 'daemon.log');
|
|
73
|
+
if (fsSync.existsSync(legacy)) return legacy;
|
|
74
|
+
return preferred; // return preferred so the error message is forward-looking
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Normalize legacy (v1, Supabase-keyed) configs to the v2 shape. Never
|
|
79
|
+
* silently keeps a service-role key around.
|
|
80
|
+
*/
|
|
81
|
+
function migrate(raw) {
|
|
82
|
+
if (!raw || typeof raw !== 'object') return raw;
|
|
83
|
+
const out = { ...raw };
|
|
84
|
+
if (!out.controlPlane || typeof out.controlPlane !== 'object') {
|
|
85
|
+
const legacyUrl = out.supabase?.url;
|
|
86
|
+
if (legacyUrl) out.controlPlane = { url: legacyUrl };
|
|
87
|
+
}
|
|
88
|
+
if (out.supabase) {
|
|
89
|
+
// Drop the service-role key. It's no longer needed on nodes.
|
|
90
|
+
delete out.supabase;
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @returns {Promise<Object|null>}
|
|
97
|
+
*/
|
|
98
|
+
export async function loadConfig() {
|
|
99
|
+
const p = getConfigPath();
|
|
100
|
+
try {
|
|
101
|
+
const raw = await fs.readFile(p, 'utf8');
|
|
102
|
+
const parsed = JSON.parse(raw);
|
|
103
|
+
return migrate(parsed);
|
|
104
|
+
} catch (err) {
|
|
105
|
+
if (err && err.code === 'ENOENT') return null;
|
|
106
|
+
throw err;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export async function saveConfig(config) {
|
|
111
|
+
const dir = getConfigDir();
|
|
112
|
+
await fs.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
113
|
+
const p = getConfigPath();
|
|
114
|
+
const json = JSON.stringify(config, null, 2) + '\n';
|
|
115
|
+
// Write then chmod — writeFile's mode flag only applies to newly created
|
|
116
|
+
// files; chmod ensures existing files with wrong permissions get fixed.
|
|
117
|
+
await fs.writeFile(p, json, { mode: 0o600 });
|
|
118
|
+
await fs.chmod(p, 0o600);
|
|
119
|
+
return p;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Enforce 0600 on the config file without modifying its contents. */
|
|
123
|
+
export async function fixConfigPermissions() {
|
|
124
|
+
const p = getConfigPath();
|
|
125
|
+
try { await fs.chmod(p, 0o600); } catch { /* file may not exist yet */ }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export async function requireConfig() {
|
|
129
|
+
const cfg = await loadConfig();
|
|
130
|
+
if (!cfg) {
|
|
131
|
+
const err = new Error(
|
|
132
|
+
`No infernet config found at ${getConfigPath()}. Run \`infernet init\` first.`
|
|
133
|
+
);
|
|
134
|
+
err.code = 'ENOCONFIG';
|
|
135
|
+
throw err;
|
|
136
|
+
}
|
|
137
|
+
return cfg;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function getControlPlaneUrl(config) {
|
|
141
|
+
const url = config?.controlPlane?.url;
|
|
142
|
+
if (!url) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
'config.controlPlane.url is not set. Run `infernet login --url <https://...>` to point this node at a control plane.'
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return url;
|
|
148
|
+
}
|
package/lib/daemonize.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Daemonize helper — re-spawns the current CLI as a detached background
|
|
3
|
+
* process running `infernet start --foreground`, with stdout/stderr piped
|
|
4
|
+
* to the daemon log file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { spawn } from 'node:child_process';
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
|
|
11
|
+
import { getConfigDir, getDaemonLogPath } from './config.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {string[]} passthroughArgs Extra args to hand to the foreground start
|
|
15
|
+
* (e.g. --heartbeat-interval, --poll-interval).
|
|
16
|
+
* @returns {{ pid: number, logPath: string }}
|
|
17
|
+
*/
|
|
18
|
+
export function spawnDetachedDaemon(passthroughArgs = []) {
|
|
19
|
+
fs.mkdirSync(getConfigDir(), { recursive: true, mode: 0o700 });
|
|
20
|
+
|
|
21
|
+
const logPath = getDaemonLogPath();
|
|
22
|
+
const out = fs.openSync(logPath, 'a');
|
|
23
|
+
const err = fs.openSync(logPath, 'a');
|
|
24
|
+
|
|
25
|
+
// process.argv[1] is the CLI entry script (absolute path when invoked
|
|
26
|
+
// via `node path/to/cli/index.js` or via the bin symlink).
|
|
27
|
+
const scriptPath = process.argv[1];
|
|
28
|
+
if (!scriptPath || !path.isAbsolute(scriptPath)) {
|
|
29
|
+
// Fall back to resolving via import.meta-ish heuristics. This should
|
|
30
|
+
// almost never trigger in practice.
|
|
31
|
+
throw new Error('Unable to determine absolute path to the CLI script for daemonization.');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const child = spawn(
|
|
35
|
+
process.execPath,
|
|
36
|
+
[scriptPath, 'start', '--foreground', ...passthroughArgs],
|
|
37
|
+
{
|
|
38
|
+
detached: true,
|
|
39
|
+
stdio: ['ignore', out, err],
|
|
40
|
+
env: { ...process.env, INFERNET_DAEMON_FOREGROUND: '1' }
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
child.unref();
|
|
45
|
+
return { pid: child.pid, logPath };
|
|
46
|
+
}
|
package/lib/firewall.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-distro firewall hole-punch helpers for the Infernet P2P port.
|
|
3
|
+
*
|
|
4
|
+
* Two operating modes:
|
|
5
|
+
*
|
|
6
|
+
* - `printFirewallHint(port)` only prints the commands. Use for
|
|
7
|
+
* `infernet firewall` (read-only by design).
|
|
8
|
+
* - `applyFirewallRule(port)` actually runs them via sudo. Use from
|
|
9
|
+
* `infernet setup` after the operator has opted in. sudo will
|
|
10
|
+
* prompt for a password interactively in the same TTY.
|
|
11
|
+
*
|
|
12
|
+
* The Linux tools that manage these rules (ufw, firewalld, iptables,
|
|
13
|
+
* nftables) touch shared system state and require root. We never run
|
|
14
|
+
* them silently — `applyFirewallRule` is opt-in and prints what it's
|
|
15
|
+
* about to do before doing it.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { execFileSync, spawn } from 'node:child_process';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Best-effort detection of the available firewall managers. Returns an
|
|
22
|
+
* array of detected tools in order of preference: ['ufw'] | ['firewalld']
|
|
23
|
+
* | ['iptables'] | ['nftables'] | []. On macOS returns ['pf']. On Windows
|
|
24
|
+
* ['netsh']. On anything else we return [] and print a generic message.
|
|
25
|
+
*/
|
|
26
|
+
export function detectFirewall() {
|
|
27
|
+
if (process.platform === 'darwin') return ['pf'];
|
|
28
|
+
if (process.platform === 'win32') return ['netsh'];
|
|
29
|
+
if (process.platform !== 'linux') return [];
|
|
30
|
+
|
|
31
|
+
const found = [];
|
|
32
|
+
const have = (bin) => {
|
|
33
|
+
try {
|
|
34
|
+
execFileSync('sh', ['-c', `command -v ${bin}`], { stdio: 'ignore' });
|
|
35
|
+
return true;
|
|
36
|
+
} catch { return false; }
|
|
37
|
+
};
|
|
38
|
+
if (have('ufw')) found.push('ufw');
|
|
39
|
+
if (have('firewall-cmd')) found.push('firewalld');
|
|
40
|
+
if (have('nft')) found.push('nftables');
|
|
41
|
+
if (have('iptables')) found.push('iptables');
|
|
42
|
+
return found;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Return a block of text describing how to open `port` (TCP) on each
|
|
47
|
+
* firewall tool detected. Never mutates state.
|
|
48
|
+
*
|
|
49
|
+
* @param {number} port
|
|
50
|
+
* @returns {{ detected: string[], lines: string[] }}
|
|
51
|
+
*/
|
|
52
|
+
export function describeFirewallHowTo(port) {
|
|
53
|
+
const detected = detectFirewall();
|
|
54
|
+
const lines = [];
|
|
55
|
+
|
|
56
|
+
if (detected.length === 0) {
|
|
57
|
+
lines.push(`(No supported firewall manager detected on ${process.platform}.)`);
|
|
58
|
+
lines.push(`If you have one, open TCP ${port} for inbound traffic manually.`);
|
|
59
|
+
return { detected, lines };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for (const tool of detected) {
|
|
63
|
+
switch (tool) {
|
|
64
|
+
case 'ufw':
|
|
65
|
+
lines.push('[ufw]');
|
|
66
|
+
lines.push(` sudo ufw allow ${port}/tcp`);
|
|
67
|
+
lines.push(` sudo ufw reload`);
|
|
68
|
+
break;
|
|
69
|
+
case 'firewalld':
|
|
70
|
+
lines.push('[firewalld]');
|
|
71
|
+
lines.push(` sudo firewall-cmd --permanent --add-port=${port}/tcp`);
|
|
72
|
+
lines.push(` sudo firewall-cmd --reload`);
|
|
73
|
+
break;
|
|
74
|
+
case 'nftables':
|
|
75
|
+
lines.push('[nftables]');
|
|
76
|
+
lines.push(` sudo nft add rule inet filter input tcp dport ${port} accept`);
|
|
77
|
+
break;
|
|
78
|
+
case 'iptables':
|
|
79
|
+
lines.push('[iptables]');
|
|
80
|
+
lines.push(` sudo iptables -I INPUT -p tcp --dport ${port} -j ACCEPT`);
|
|
81
|
+
lines.push(` # Persist across reboots with iptables-save or your distro's equivalent.`);
|
|
82
|
+
break;
|
|
83
|
+
case 'pf':
|
|
84
|
+
lines.push('[macOS pf — add to /etc/pf.conf and reload]');
|
|
85
|
+
lines.push(` pass in proto tcp from any to any port ${port}`);
|
|
86
|
+
lines.push(` sudo pfctl -f /etc/pf.conf`);
|
|
87
|
+
break;
|
|
88
|
+
case 'netsh':
|
|
89
|
+
lines.push('[Windows — elevated PowerShell or cmd]');
|
|
90
|
+
lines.push(` netsh advfirewall firewall add rule name="Infernet P2P ${port}" dir=in action=allow protocol=TCP localport=${port}`);
|
|
91
|
+
break;
|
|
92
|
+
default:
|
|
93
|
+
lines.push(`(${tool}): manual configuration required`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return { detected, lines };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Emit a firewall how-to block to stdout. `context` is a short label
|
|
101
|
+
* (e.g. "init") that gets included in the header for clarity.
|
|
102
|
+
*/
|
|
103
|
+
export function printFirewallHint(port, context = '') {
|
|
104
|
+
const { detected, lines } = describeFirewallHowTo(port);
|
|
105
|
+
const header = context
|
|
106
|
+
? `-- firewall hint (${context}, port ${port}) --`
|
|
107
|
+
: `-- firewall hint (port ${port}) --`;
|
|
108
|
+
process.stdout.write(`${header}\n`);
|
|
109
|
+
if (detected.length > 0) {
|
|
110
|
+
process.stdout.write(`Detected: ${detected.join(', ')}\n`);
|
|
111
|
+
}
|
|
112
|
+
for (const line of lines) process.stdout.write(`${line}\n`);
|
|
113
|
+
process.stdout.write('\n');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Build the list of [bin, ...args] commands needed to open `port` on a
|
|
118
|
+
* specific tool. Each command is meant to be invoked under sudo.
|
|
119
|
+
*/
|
|
120
|
+
function buildCommandsFor(tool, port) {
|
|
121
|
+
switch (tool) {
|
|
122
|
+
case 'ufw':
|
|
123
|
+
return [
|
|
124
|
+
['ufw', 'allow', `${port}/tcp`],
|
|
125
|
+
['ufw', 'reload']
|
|
126
|
+
];
|
|
127
|
+
case 'firewalld':
|
|
128
|
+
return [
|
|
129
|
+
['firewall-cmd', '--permanent', `--add-port=${port}/tcp`],
|
|
130
|
+
['firewall-cmd', '--reload']
|
|
131
|
+
];
|
|
132
|
+
case 'nftables':
|
|
133
|
+
return [
|
|
134
|
+
['nft', 'add', 'rule', 'inet', 'filter', 'input',
|
|
135
|
+
'tcp', 'dport', String(port), 'accept']
|
|
136
|
+
];
|
|
137
|
+
case 'iptables':
|
|
138
|
+
return [
|
|
139
|
+
['iptables', '-I', 'INPUT', '-p', 'tcp', '--dport', String(port), '-j', 'ACCEPT']
|
|
140
|
+
];
|
|
141
|
+
default:
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function runWithSudo(cmd, args) {
|
|
147
|
+
return new Promise((resolve, reject) => {
|
|
148
|
+
const child = spawn('sudo', [cmd, ...args], { stdio: 'inherit' });
|
|
149
|
+
child.on('exit', (code) => {
|
|
150
|
+
if (code === 0) resolve();
|
|
151
|
+
else reject(new Error(`${cmd} exited ${code}`));
|
|
152
|
+
});
|
|
153
|
+
child.on('error', reject);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Apply the right firewall rule(s) to open `port`. Detects the
|
|
159
|
+
* available tool (ufw → firewalld → nftables → iptables) and runs
|
|
160
|
+
* each command via sudo, inheriting the TTY so password prompts work.
|
|
161
|
+
*
|
|
162
|
+
* Linux only. macOS / Windows return `{ applied: false, reason }`
|
|
163
|
+
* because pf / netsh need editing config files or admin shells, which
|
|
164
|
+
* isn't a clean "punch a hole" call.
|
|
165
|
+
*
|
|
166
|
+
* @param {number} port
|
|
167
|
+
* @param {{ tool?: string }} [opts] pin a specific tool, otherwise auto.
|
|
168
|
+
* @returns {Promise<{ applied: boolean, tool?: string, reason?: string }>}
|
|
169
|
+
*/
|
|
170
|
+
export async function applyFirewallRule(port, opts = {}) {
|
|
171
|
+
if (process.platform !== 'linux') {
|
|
172
|
+
return { applied: false, reason: `not supported on ${process.platform} — see \`infernet firewall\` for manual steps` };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const detected = detectFirewall();
|
|
176
|
+
const tool = opts.tool ?? detected[0];
|
|
177
|
+
if (!tool) {
|
|
178
|
+
return { applied: false, reason: 'no firewall manager detected' };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const cmds = buildCommandsFor(tool, port);
|
|
182
|
+
if (!cmds) {
|
|
183
|
+
return { applied: false, reason: `${tool}: no automated rule builder` };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
for (const [bin, ...args] of cmds) {
|
|
187
|
+
await runWithSudo(bin, args);
|
|
188
|
+
}
|
|
189
|
+
return { applied: true, tool };
|
|
190
|
+
}
|
package/lib/hf-model.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HuggingFace model management — download, inspect, and wire up models
|
|
3
|
+
* from HuggingFace to vLLM/SGLang backends.
|
|
4
|
+
*
|
|
5
|
+
* `infernet model pull hf:org/repo` calls downloadHfModel().
|
|
6
|
+
* The HF token is read from config.huggingface.token or HUGGINGFACE_TOKEN.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { spawn } from 'node:child_process';
|
|
10
|
+
import { loadConfig } from './config.js';
|
|
11
|
+
|
|
12
|
+
const HF_API = 'https://huggingface.co/api';
|
|
13
|
+
|
|
14
|
+
/** Read HF token from env → config → null */
|
|
15
|
+
export async function resolveHfToken() {
|
|
16
|
+
if (process.env.HUGGINGFACE_TOKEN) return process.env.HUGGINGFACE_TOKEN;
|
|
17
|
+
const cfg = await loadConfig().catch(() => null);
|
|
18
|
+
return cfg?.huggingface?.token ?? null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Fetch model metadata from the HF hub. */
|
|
22
|
+
export async function fetchHfModelInfo(repoId, token) {
|
|
23
|
+
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
|
24
|
+
const res = await fetch(`${HF_API}/models/${repoId}`, { headers });
|
|
25
|
+
if (!res.ok) {
|
|
26
|
+
const msg = await res.text().catch(() => res.status);
|
|
27
|
+
throw new Error(`HuggingFace API ${res.status}: ${String(msg).slice(0, 200)}`);
|
|
28
|
+
}
|
|
29
|
+
const data = await res.json();
|
|
30
|
+
|
|
31
|
+
const siblings = data.siblings ?? [];
|
|
32
|
+
const shards = siblings.filter(s => s.rfilename?.endsWith('.safetensors') && s.rfilename.startsWith('model-'));
|
|
33
|
+
const totalBytes = siblings.reduce((a, s) => a + (s.size ?? 0), 0);
|
|
34
|
+
const sizeGb = totalBytes > 0 ? +(totalBytes / 1024 ** 3).toFixed(1) : null;
|
|
35
|
+
|
|
36
|
+
// Fetch config.json for architecture details
|
|
37
|
+
let modelConfig = {};
|
|
38
|
+
try {
|
|
39
|
+
const cfgRes = await fetch(
|
|
40
|
+
`https://huggingface.co/${repoId}/raw/main/config.json`,
|
|
41
|
+
{ headers }
|
|
42
|
+
);
|
|
43
|
+
if (cfgRes.ok) modelConfig = await cfgRes.json();
|
|
44
|
+
} catch { /* best-effort */ }
|
|
45
|
+
|
|
46
|
+
const modelType = modelConfig.model_type ?? data.config?.model_type ?? null;
|
|
47
|
+
const torchDtype = modelConfig.torch_dtype ?? null;
|
|
48
|
+
const numExperts = modelConfig.num_experts ?? null;
|
|
49
|
+
|
|
50
|
+
// Infer a recommended backend
|
|
51
|
+
const ollamaUnsupported = [
|
|
52
|
+
'glm_moe_dsa', 'moe', 'mixtral', 'deepseek_v3', 'phi3',
|
|
53
|
+
'jamba', 'dbrx', 'xverse', 'internlm2'
|
|
54
|
+
];
|
|
55
|
+
const needsVllmOrSglang = modelType
|
|
56
|
+
? ollamaUnsupported.some(t => modelType.toLowerCase().includes(t))
|
|
57
|
+
: shards.length > 50; // large shard count → probably too big for Ollama
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
repoId,
|
|
61
|
+
modelType,
|
|
62
|
+
torchDtype,
|
|
63
|
+
shardCount: shards.length,
|
|
64
|
+
sizeGb,
|
|
65
|
+
numExperts,
|
|
66
|
+
isMoe: numExperts !== null || modelType?.includes('moe'),
|
|
67
|
+
recommendedBackend: needsVllmOrSglang ? 'vllm_or_sglang' : 'ollama_or_vllm',
|
|
68
|
+
pipeline: data.pipeline_tag ?? 'text-generation',
|
|
69
|
+
license: data.cardData?.license ?? data.tags?.find(t => t.startsWith('license:'))?.replace('license:', '') ?? null,
|
|
70
|
+
downloads: data.downloads ?? null,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Check whether huggingface-cli (Python) is available. */
|
|
75
|
+
async function hfCliAvailable() {
|
|
76
|
+
return new Promise(resolve => {
|
|
77
|
+
const p = spawn('huggingface-cli', ['--help'], { stdio: 'ignore' });
|
|
78
|
+
p.on('exit', code => resolve(code === 0));
|
|
79
|
+
p.on('error', () => resolve(false));
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Check whether Python huggingface_hub is importable. */
|
|
84
|
+
async function hfHubAvailable() {
|
|
85
|
+
return new Promise(resolve => {
|
|
86
|
+
const p = spawn('python3', ['-c', 'import huggingface_hub'], { stdio: 'ignore' });
|
|
87
|
+
p.on('exit', code => resolve(code === 0));
|
|
88
|
+
p.on('error', () => resolve(false));
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Download a model from HuggingFace using huggingface-cli.
|
|
94
|
+
* Streams CLI output directly to the terminal (progress bars etc.).
|
|
95
|
+
* Returns the local cache path.
|
|
96
|
+
*/
|
|
97
|
+
export async function downloadHfModel(repoId, token, { localDir = null } = {}) {
|
|
98
|
+
const hasCli = await hfCliAvailable();
|
|
99
|
+
const hasHub = hasCli || await hfHubAvailable();
|
|
100
|
+
|
|
101
|
+
if (!hasHub) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
'huggingface_hub not installed.\n' +
|
|
104
|
+
'Fix: pip install -U huggingface_hub\n' +
|
|
105
|
+
' (or: pip3 install -U huggingface_hub)'
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Build the download command
|
|
110
|
+
const args = ['download', repoId];
|
|
111
|
+
if (token) args.push('--token', token);
|
|
112
|
+
if (localDir) args.push('--local-dir', localDir);
|
|
113
|
+
|
|
114
|
+
return new Promise((resolve, reject) => {
|
|
115
|
+
const cmd = hasCli ? 'huggingface-cli' : 'python3';
|
|
116
|
+
const finalArgs = hasCli ? args : [
|
|
117
|
+
'-c',
|
|
118
|
+
`from huggingface_hub import snapshot_download; ` +
|
|
119
|
+
`p = snapshot_download(${JSON.stringify(repoId)}` +
|
|
120
|
+
`${token ? `, token=${JSON.stringify(token)}` : ''}` +
|
|
121
|
+
`${localDir ? `, local_dir=${JSON.stringify(localDir)}` : ''}` +
|
|
122
|
+
`); print(p)`
|
|
123
|
+
];
|
|
124
|
+
|
|
125
|
+
process.stdout.write(`\nDownloading ${repoId} from HuggingFace...\n`);
|
|
126
|
+
if (!hasCli) {
|
|
127
|
+
process.stdout.write('(tip: pip install huggingface_hub for a nicer progress bar)\n\n');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const child = spawn(cmd, finalArgs, { stdio: ['ignore', 'inherit', 'inherit'] });
|
|
131
|
+
let localPath = localDir;
|
|
132
|
+
|
|
133
|
+
child.on('exit', code => {
|
|
134
|
+
if (code === 0) resolve(localPath ?? defaultCachePath(repoId));
|
|
135
|
+
else reject(new Error(`huggingface download exited ${code}`));
|
|
136
|
+
});
|
|
137
|
+
child.on('error', reject);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Default HuggingFace cache path for a repo. */
|
|
142
|
+
function defaultCachePath(repoId) {
|
|
143
|
+
const home = process.env.HOME ?? process.env.USERPROFILE ?? '~';
|
|
144
|
+
const slug = repoId.replace('/', '--');
|
|
145
|
+
return `${home}/.cache/huggingface/hub/models--${slug}/snapshots/main`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Detect which backends are currently running.
|
|
150
|
+
* Returns { vllm: bool, sglang: bool }
|
|
151
|
+
*/
|
|
152
|
+
export async function detectRunningBackends() {
|
|
153
|
+
const check = async (url) => {
|
|
154
|
+
try {
|
|
155
|
+
const ctrl = new AbortController();
|
|
156
|
+
setTimeout(() => ctrl.abort(), 800);
|
|
157
|
+
const r = await fetch(url, { signal: ctrl.signal });
|
|
158
|
+
return r.ok;
|
|
159
|
+
} catch { return false; }
|
|
160
|
+
};
|
|
161
|
+
const [vllm, sglang] = await Promise.all([
|
|
162
|
+
check('http://localhost:8000/v1/models'),
|
|
163
|
+
check('http://localhost:30000/v1/models'),
|
|
164
|
+
]);
|
|
165
|
+
return { vllm, sglang };
|
|
166
|
+
}
|
package/lib/identity.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Nostr identity helpers for the infernet CLI — thin re-export around
|
|
3
|
+
* `@infernetprotocol/auth` so commands keep using the names they already
|
|
4
|
+
* know. Keys are real BIP-340 Schnorr / secp256k1 pairs now.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
generateKeyPair,
|
|
9
|
+
derivePublicKey,
|
|
10
|
+
keyPairIsValid,
|
|
11
|
+
isHex64
|
|
12
|
+
} from '@infernetprotocol/auth';
|
|
13
|
+
|
|
14
|
+
export function generateNostrKeyPair() {
|
|
15
|
+
return generateKeyPair();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { derivePublicKey, keyPairIsValid, isHex64 };
|
package/lib/ipc.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI ↔ daemon IPC over a Unix domain socket at
|
|
3
|
+
* `~/.config/infernet/daemon.sock`. Protocol: newline-delimited JSON.
|
|
4
|
+
*
|
|
5
|
+
* client → daemon: {"cmd": "<name>", "payload": <any>}\n
|
|
6
|
+
* daemon → client: {"ok": true, "data": <any>}\n
|
|
7
|
+
* {"ok": false, "error": "<message>"}\n
|
|
8
|
+
*
|
|
9
|
+
* The daemon writes its socket file on start and removes it on shutdown.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import net from 'node:net';
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
|
|
15
|
+
import { getDaemonSocketPath } from './config.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Is a daemon listening on the socket?
|
|
19
|
+
* Best-effort: the socket file exists AND a connect succeeds within a small
|
|
20
|
+
* timeout. Returns false on any error.
|
|
21
|
+
*/
|
|
22
|
+
export function isDaemonAlive(timeoutMs = 500) {
|
|
23
|
+
const p = getDaemonSocketPath();
|
|
24
|
+
if (!fs.existsSync(p)) return Promise.resolve(false);
|
|
25
|
+
|
|
26
|
+
return new Promise((resolve) => {
|
|
27
|
+
const sock = net.createConnection(p);
|
|
28
|
+
const timer = setTimeout(() => {
|
|
29
|
+
sock.destroy();
|
|
30
|
+
resolve(false);
|
|
31
|
+
}, timeoutMs);
|
|
32
|
+
sock.once('connect', () => {
|
|
33
|
+
clearTimeout(timer);
|
|
34
|
+
sock.end();
|
|
35
|
+
resolve(true);
|
|
36
|
+
});
|
|
37
|
+
sock.once('error', () => {
|
|
38
|
+
clearTimeout(timer);
|
|
39
|
+
resolve(false);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Send a single command to the daemon and return its response. Resolves
|
|
46
|
+
* with `{ ok, data?, error? }`. If the daemon isn't reachable, resolves
|
|
47
|
+
* with `{ ok: false, error: 'daemon-unreachable', cause: <detail> }`.
|
|
48
|
+
*
|
|
49
|
+
* @param {string} cmd
|
|
50
|
+
* @param {any} [payload]
|
|
51
|
+
* @param {{ timeoutMs?: number }} [opts]
|
|
52
|
+
*/
|
|
53
|
+
export function sendToDaemon(cmd, payload, { timeoutMs = 3000 } = {}) {
|
|
54
|
+
const p = getDaemonSocketPath();
|
|
55
|
+
return new Promise((resolve) => {
|
|
56
|
+
if (!fs.existsSync(p)) {
|
|
57
|
+
resolve({ ok: false, error: 'daemon-unreachable', cause: 'socket missing' });
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const sock = net.createConnection(p);
|
|
61
|
+
let buf = '';
|
|
62
|
+
let settled = false;
|
|
63
|
+
|
|
64
|
+
const done = (result) => {
|
|
65
|
+
if (settled) return;
|
|
66
|
+
settled = true;
|
|
67
|
+
try { sock.destroy(); } catch { /* ignore */ }
|
|
68
|
+
resolve(result);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const timer = setTimeout(() => {
|
|
72
|
+
done({ ok: false, error: 'daemon-timeout' });
|
|
73
|
+
}, timeoutMs);
|
|
74
|
+
|
|
75
|
+
sock.setEncoding('utf8');
|
|
76
|
+
|
|
77
|
+
sock.once('connect', () => {
|
|
78
|
+
try {
|
|
79
|
+
sock.write(JSON.stringify({ cmd, payload }) + '\n');
|
|
80
|
+
} catch (err) {
|
|
81
|
+
clearTimeout(timer);
|
|
82
|
+
done({ ok: false, error: 'write-failed', cause: err?.message ?? String(err) });
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
sock.on('data', (chunk) => {
|
|
87
|
+
buf += chunk;
|
|
88
|
+
const nl = buf.indexOf('\n');
|
|
89
|
+
if (nl < 0) return;
|
|
90
|
+
const line = buf.slice(0, nl);
|
|
91
|
+
clearTimeout(timer);
|
|
92
|
+
let parsed;
|
|
93
|
+
try {
|
|
94
|
+
parsed = JSON.parse(line);
|
|
95
|
+
} catch (err) {
|
|
96
|
+
done({ ok: false, error: 'bad-response', cause: err?.message ?? String(err) });
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
done(parsed);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
sock.once('error', (err) => {
|
|
103
|
+
clearTimeout(timer);
|
|
104
|
+
done({ ok: false, error: 'daemon-unreachable', cause: err?.message ?? String(err) });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
sock.once('close', () => {
|
|
108
|
+
clearTimeout(timer);
|
|
109
|
+
if (!settled) {
|
|
110
|
+
done({ ok: false, error: 'connection-closed' });
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|