@llmnative/runner 1.0.0 → 1.0.2

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 (65) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +9 -3
  3. package/bin/runner.mjs +21 -10
  4. package/bin/setup-server.mjs +160 -0
  5. package/bin/ssh-keys.mjs +15 -0
  6. package/dist/setup-app/assets/NetworkCanvasImpl-DIYuzg6x-CVEzQpWc.js +7 -0
  7. package/dist/setup-app/assets/dist-9W_fpPLL.js +11 -0
  8. package/dist/setup-app/assets/index-BAksVWhQ.js +32 -0
  9. package/dist/setup-app/assets/index-CmPc3Q2I.css +2 -0
  10. package/dist/setup-app/assets/index.es-DFOx5a43.js +1 -0
  11. package/dist/setup-app/assets/index.esm-7HxyOcz9.js +9 -0
  12. package/dist/setup-app/assets/index.esm-Bo_uP467.js +1 -0
  13. package/dist/setup-app/assets/index.esm-D6oehhSQ.js +1 -0
  14. package/dist/setup-app/assets/index.esm-D983b7TD.js +1 -0
  15. package/dist/setup-app/assets/index.esm-H_whZvB-.js +4 -0
  16. package/dist/setup-app/assets/index.esm-NAFJB7JL.js +31 -0
  17. package/dist/setup-app/assets/lucide-react-DpJIOtaQ.js +1 -0
  18. package/dist/setup-app/assets/prism-BjaHQ1lq.js +2 -0
  19. package/dist/setup-app/assets/prism-bash-D6zCJ74D.js +1 -0
  20. package/dist/setup-app/assets/prism-c-04YixN25.js +1 -0
  21. package/dist/setup-app/assets/prism-clike-DapgWyxx.js +1 -0
  22. package/dist/setup-app/assets/prism-cpp-C-lJbC-6.js +1 -0
  23. package/dist/setup-app/assets/prism-csharp-XPn2bw0d.js +1 -0
  24. package/dist/setup-app/assets/prism-css-CPgOzxXp.js +1 -0
  25. package/dist/setup-app/assets/prism-docker-DkT6yOFj.js +1 -0
  26. package/dist/setup-app/assets/prism-git-BZB8f0uj.js +1 -0
  27. package/dist/setup-app/assets/prism-go-C_8qAH5n.js +1 -0
  28. package/dist/setup-app/assets/prism-graphql-XjvHrdQl.js +1 -0
  29. package/dist/setup-app/assets/prism-ini-BESK3y0r.js +1 -0
  30. package/dist/setup-app/assets/prism-java-6wtusvIl.js +1 -0
  31. package/dist/setup-app/assets/prism-javascript-BnO-swvr.js +1 -0
  32. package/dist/setup-app/assets/prism-json-Dp_-W-Hv.js +1 -0
  33. package/dist/setup-app/assets/prism-jsx-D-D0NWtC.js +1 -0
  34. package/dist/setup-app/assets/prism-markup-Cp04rIa1.js +1 -0
  35. package/dist/setup-app/assets/prism-php-Co4fM40K.js +1 -0
  36. package/dist/setup-app/assets/prism-powershell-Cvud8pPk.js +1 -0
  37. package/dist/setup-app/assets/prism-python-CfiSVtM6.js +1 -0
  38. package/dist/setup-app/assets/prism-ruby-Cb5nlQRI.js +1 -0
  39. package/dist/setup-app/assets/prism-sql-CYhVDMEE.js +1 -0
  40. package/dist/setup-app/assets/prism-tsx-p6J2Kc_4.js +1 -0
  41. package/dist/setup-app/assets/prism-typescript-BMqONzxf.js +1 -0
  42. package/dist/setup-app/assets/prism-yaml-C0JHu8gr.js +1 -0
  43. package/dist/setup-app/assets/rolldown-runtime-XKyaHtl9.js +1 -0
  44. package/dist/setup-app/assets/style-DLioOiRN.css +1 -0
  45. package/dist/setup-app/index.html +15 -0
  46. package/dist/setup.js +1 -1
  47. package/dist/setup.js.map +1 -1
  48. package/dist/setup.mjs +21 -1
  49. package/dist/setup.mjs.map +1 -1
  50. package/dist/steps.js +1 -1
  51. package/dist/steps.js.map +1 -1
  52. package/dist/steps.mjs +2 -2
  53. package/dist/steps.mjs.map +1 -1
  54. package/dist/types/setup/index.d.ts +8 -0
  55. package/dist/types/ui/RunnersSettings.d.ts +2 -0
  56. package/dist/types/ui/texts.d.ts +6 -0
  57. package/dist/ui.js +3 -3
  58. package/dist/ui.js.map +1 -1
  59. package/dist/ui.mjs +54 -38
  60. package/dist/ui.mjs.map +1 -1
  61. package/install/compose.yaml +18 -1
  62. package/install/coolify.yaml +2 -2
  63. package/install/install.sh +51 -6
  64. package/llms.txt +1 -1
  65. package/package.json +23 -7
@@ -0,0 +1,160 @@
1
+ // The backend of `npx @llmnative/runner setup`, on 127.0.0.1 only: the runners of this computer (with their platform
2
+ // keys) in ~/.llmnative-runner/fleet.json (0600), the installation over SSH from here, the state of each runner.
3
+ // Every request carries the token of the address it opened, so that no other page of the browser can call it.
4
+ import { randomBytes, timingSafeEqual } from 'node:crypto';
5
+ import { createReadStream, existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
6
+ import { createServer } from 'node:http';
7
+ import { homedir } from 'node:os';
8
+ import { dirname, extname, join, normalize, sep } from 'node:path';
9
+ import { fileURLToPath } from 'node:url';
10
+ import { installOverSsh } from '../dist/setup.mjs';
11
+ import { RunnerClient } from '../dist/client.mjs';
12
+ import { localSshFor } from './ssh-keys.mjs';
13
+
14
+ const APP_DIR = join(dirname(fileURLToPath(import.meta.url)), '..', 'dist', 'setup-app');
15
+ const TYPES = { '.html': 'text/html; charset=utf-8', '.js': 'text/javascript', '.css': 'text/css', '.svg': 'image/svg+xml', '.png': 'image/png', '.woff2': 'font/woff2', '.json': 'application/json' };
16
+
17
+ export function fleetFile(home = homedir()) {
18
+ return join(home, '.llmnative-runner', 'fleet.json');
19
+ }
20
+
21
+ function readFleet(file) {
22
+ try {
23
+ return existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : { runners: [] };
24
+ } catch {
25
+ return { runners: [] };
26
+ }
27
+ }
28
+
29
+ function writeFleet(file, fleet) {
30
+ mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
31
+ writeFileSync(`${file}.tmp`, JSON.stringify(fleet, null, 2), { mode: 0o600 });
32
+ renameSync(`${file}.tmp`, file);
33
+ }
34
+
35
+ const view = ({ key: _key, ...entry }) => entry;
36
+ const send = (response, status, body) => {
37
+ response.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' });
38
+ response.end(JSON.stringify(body));
39
+ };
40
+ const body = (request) => new Promise((resolve, reject) => {
41
+ const chunks = [];
42
+ let size = 0;
43
+ request.on('data', (chunk) => {
44
+ size += chunk.length;
45
+ if (size > 256 * 1024) { reject(new Error('too large')); request.destroy(); } else chunks.push(chunk);
46
+ });
47
+ request.on('end', () => {
48
+ try { resolve(chunks.length ? JSON.parse(Buffer.concat(chunks).toString('utf8')) : {}); } catch { reject(new Error('invalid')); }
49
+ });
50
+ request.on('error', reject);
51
+ });
52
+
53
+ /** Starts the page; resolves with its address (token included). */
54
+ export function startSetupServer({ port = 0, file = fleetFile(), log = () => {} } = {}) {
55
+ const token = randomBytes(24).toString('base64url');
56
+ const allowed = (request) => {
57
+ const given = Buffer.from(String(request.headers['x-setup-token'] ?? ''));
58
+ const expected = Buffer.from(token);
59
+ return given.length === expected.length && timingSafeEqual(given, expected);
60
+ };
61
+ const save = (change) => {
62
+ const fleet = readFleet(file);
63
+ const next = change(fleet) ?? fleet;
64
+ writeFleet(file, next);
65
+ return next;
66
+ };
67
+ /** Connects a runner by its pairing code (or a key made before) and keeps it. */
68
+ async function connect({ name, url, code, key }, extra = {}) {
69
+ const address = String(url ?? '').replace(/\/+$/, '');
70
+ if (!/^https?:\/\//.test(address)) throw Object.assign(new Error('invalid url'), { status: 400 });
71
+ const platformKey = key ? String(key) : (await new RunnerClient({ url: address }).pair(String(code ?? ''), 'setup on this computer')).key;
72
+ // The key works: its state answers.
73
+ await new RunnerClient({ url: address, key: platformKey }).platform.status();
74
+ const entry = { id: randomBytes(4).toString('hex'), name: String(name || new URL(address).hostname).slice(0, 80), url: address, key: platformKey, ...extra };
75
+ save((fleet) => {
76
+ if (!fleet.runners.length) entry.isDefault = true;
77
+ fleet.runners.push(entry);
78
+ });
79
+ log(`runner added: ${entry.name} (${entry.url})`);
80
+ return entry;
81
+ }
82
+
83
+ const server = createServer(async (request, response) => {
84
+ const url = new URL(request.url ?? '/', 'http://localhost');
85
+ try {
86
+ if (url.pathname.startsWith('/api/')) {
87
+ if (!allowed(request)) return send(response, 401, { error: 'bad token' });
88
+ const parts = url.pathname.slice('/api/'.length).split('/').filter(Boolean).map(decodeURIComponent);
89
+ const [area, id, what] = parts;
90
+ if (area === 'runners' && !id && request.method === 'GET') return send(response, 200, { runners: readFleet(file).runners.map(view) });
91
+ if (area === 'runners' && !id && request.method === 'POST') return send(response, 201, { runner: view(await connect(await body(request))) });
92
+ const entry = readFleet(file).runners.find((runner) => runner.id === id);
93
+ if (area === 'runners' && id && !entry) return send(response, 404, { error: 'not found' });
94
+ if (area === 'runners' && what === 'status' && request.method === 'GET') {
95
+ return send(response, 200, await new RunnerClient({ url: entry.url, key: entry.key }).platform.status());
96
+ }
97
+ if (area === 'runners' && !what && request.method === 'PATCH') {
98
+ const patch = await body(request);
99
+ save((fleet) => {
100
+ for (const runner of fleet.runners) {
101
+ if (runner.id === id) {
102
+ if (typeof patch.name === 'string') runner.name = patch.name.slice(0, 80);
103
+ if (typeof patch.draining === 'boolean') runner.draining = patch.draining;
104
+ if (patch.isDefault === true) runner.isDefault = true;
105
+ if (patch.isDefault === false) delete runner.isDefault;
106
+ } else if (patch.isDefault === true) delete runner.isDefault;
107
+ }
108
+ });
109
+ return send(response, 200, { ok: true });
110
+ }
111
+ if (area === 'runners' && !what && request.method === 'DELETE') {
112
+ save((fleet) => ({ runners: fleet.runners.filter((runner) => runner.id !== id) }));
113
+ return send(response, 200, { removed: true });
114
+ }
115
+ if (area === 'install' && !id && request.method === 'POST') {
116
+ const input = await body(request);
117
+ const access = input.access ?? {};
118
+ response.writeHead(200, { 'content-type': 'application/x-ndjson', 'cache-control': 'no-store' });
119
+ const write = (event) => response.write(`${JSON.stringify(event)}\n`);
120
+ try {
121
+ // No key nor password given: the key of this computer for that host (~/.ssh/config).
122
+ const local = !access.privateKey && !access.password ? localSshFor(String(access.host ?? '')) : {};
123
+ if (!access.privateKey && !access.password && !local.privateKey) throw Object.assign(new Error('no SSH key of this computer for this host'), { code: 'ssh-key' });
124
+ const username = String(access.username || local.user || 'root');
125
+ log(`installing on ${username}@${access.host} as ${input.domain}${local.file ? ` (key ${local.file})` : ''}…`);
126
+ const installed = await installOverSsh({
127
+ host: String(access.host ?? ''),
128
+ port: Number(access.port) || 22,
129
+ username,
130
+ ...(access.privateKey ? { privateKey: String(access.privateKey) } : local.privateKey ? { privateKey: local.privateKey } : {}),
131
+ ...(access.password ? { password: String(access.password) } : {}),
132
+ }, {
133
+ domain: String(input.domain ?? ''),
134
+ onProgress: ({ line, phase }) => write({ type: 'line', line, ...(phase ? { phase } : {}) }),
135
+ });
136
+ const entry = await connect({ name: input.name, url: installed.url, code: installed.pairingCode }, { hostFingerprint: installed.hostFingerprint });
137
+ write({ type: 'done', runner: view(entry) });
138
+ } catch (error) {
139
+ write({ type: 'error', message: error.message, ...(error.code ? { code: error.code } : {}) });
140
+ }
141
+ return response.end();
142
+ }
143
+ return send(response, 404, { error: 'not found' });
144
+ }
145
+ // The page itself.
146
+ const relative = normalize(decodeURIComponent(url.pathname)).replace(/^([/\\])+/, '');
147
+ let path = join(APP_DIR, relative);
148
+ if (!path.startsWith(normalize(APP_DIR + sep)) && path !== normalize(APP_DIR)) return send(response, 404, { error: 'not found' });
149
+ if (!existsSync(path) || statSync(path).isDirectory()) path = join(APP_DIR, 'index.html');
150
+ response.writeHead(200, { 'content-type': TYPES[extname(path)] ?? 'application/octet-stream', 'x-frame-options': 'DENY', 'referrer-policy': 'no-referrer' });
151
+ createReadStream(path).pipe(response);
152
+ } catch (error) {
153
+ send(response, error.status ?? 400, { error: error.code ?? error.message });
154
+ }
155
+ });
156
+ return new Promise((resolve) => server.listen(port, '127.0.0.1', () => {
157
+ const address = `http://127.0.0.1:${server.address().port}/#token=${token}`;
158
+ resolve({ address, token, server });
159
+ }));
160
+ }
@@ -0,0 +1,15 @@
1
+ // The SSH key of this computer for a host: the IdentityFile of ~/.ssh/config for it (as ssh would use), then the
2
+ // usual default keys. Read only here, sent to nobody: the installer connects from this computer.
3
+ import { existsSync, readFileSync } from 'node:fs';
4
+ import { homedir } from 'node:os';
5
+ import { join } from 'node:path';
6
+ import { sshConfigFor } from '../dist/setup.mjs';
7
+
8
+ export function localSshFor(host) {
9
+ const home = homedir();
10
+ const configFile = join(home, '.ssh', 'config');
11
+ const config = existsSync(configFile) ? sshConfigFor(readFileSync(configFile, 'utf8'), host) : { identityFiles: [] };
12
+ const files = [...config.identityFiles.map((file) => file.replace(/^~(?=$|[\\/])/, home)), ...['id_ed25519', 'id_ecdsa', 'id_rsa'].map((name) => join(home, '.ssh', name))];
13
+ const file = files.find((candidate) => existsSync(candidate));
14
+ return { ...(config.user ? { user: config.user } : {}), ...(file ? { file, privateKey: readFileSync(file, 'utf8') } : {}) };
15
+ }