@appkit/llamacpp-cli 1.11.0 → 1.12.0

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 (105) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +356 -3
  3. package/dist/cli.js +99 -0
  4. package/dist/cli.js.map +1 -1
  5. package/dist/commands/admin/config.d.ts +10 -0
  6. package/dist/commands/admin/config.d.ts.map +1 -0
  7. package/dist/commands/admin/config.js +100 -0
  8. package/dist/commands/admin/config.js.map +1 -0
  9. package/dist/commands/admin/logs.d.ts +10 -0
  10. package/dist/commands/admin/logs.d.ts.map +1 -0
  11. package/dist/commands/admin/logs.js +114 -0
  12. package/dist/commands/admin/logs.js.map +1 -0
  13. package/dist/commands/admin/restart.d.ts +2 -0
  14. package/dist/commands/admin/restart.d.ts.map +1 -0
  15. package/dist/commands/admin/restart.js +29 -0
  16. package/dist/commands/admin/restart.js.map +1 -0
  17. package/dist/commands/admin/start.d.ts +2 -0
  18. package/dist/commands/admin/start.d.ts.map +1 -0
  19. package/dist/commands/admin/start.js +30 -0
  20. package/dist/commands/admin/start.js.map +1 -0
  21. package/dist/commands/admin/status.d.ts +2 -0
  22. package/dist/commands/admin/status.d.ts.map +1 -0
  23. package/dist/commands/admin/status.js +82 -0
  24. package/dist/commands/admin/status.js.map +1 -0
  25. package/dist/commands/admin/stop.d.ts +2 -0
  26. package/dist/commands/admin/stop.d.ts.map +1 -0
  27. package/dist/commands/admin/stop.js +21 -0
  28. package/dist/commands/admin/stop.js.map +1 -0
  29. package/dist/commands/logs.d.ts +1 -0
  30. package/dist/commands/logs.d.ts.map +1 -1
  31. package/dist/commands/logs.js +22 -0
  32. package/dist/commands/logs.js.map +1 -1
  33. package/dist/lib/admin-manager.d.ts +111 -0
  34. package/dist/lib/admin-manager.d.ts.map +1 -0
  35. package/dist/lib/admin-manager.js +413 -0
  36. package/dist/lib/admin-manager.js.map +1 -0
  37. package/dist/lib/admin-server.d.ts +148 -0
  38. package/dist/lib/admin-server.d.ts.map +1 -0
  39. package/dist/lib/admin-server.js +1161 -0
  40. package/dist/lib/admin-server.js.map +1 -0
  41. package/dist/lib/download-job-manager.d.ts +64 -0
  42. package/dist/lib/download-job-manager.d.ts.map +1 -0
  43. package/dist/lib/download-job-manager.js +164 -0
  44. package/dist/lib/download-job-manager.js.map +1 -0
  45. package/dist/tui/MultiServerMonitorApp.js +1 -1
  46. package/dist/types/admin-config.d.ts +19 -0
  47. package/dist/types/admin-config.d.ts.map +1 -0
  48. package/dist/types/admin-config.js +3 -0
  49. package/dist/types/admin-config.js.map +1 -0
  50. package/dist/utils/log-parser.d.ts +9 -0
  51. package/dist/utils/log-parser.d.ts.map +1 -1
  52. package/dist/utils/log-parser.js +11 -0
  53. package/dist/utils/log-parser.js.map +1 -1
  54. package/docs/images/web-ui-servers.png +0 -0
  55. package/package.json +1 -1
  56. package/src/cli.ts +100 -0
  57. package/src/commands/admin/config.ts +121 -0
  58. package/src/commands/admin/logs.ts +91 -0
  59. package/src/commands/admin/restart.ts +26 -0
  60. package/src/commands/admin/start.ts +27 -0
  61. package/src/commands/admin/status.ts +84 -0
  62. package/src/commands/admin/stop.ts +16 -0
  63. package/src/commands/logs.ts +24 -0
  64. package/src/lib/admin-manager.ts +435 -0
  65. package/src/lib/admin-server.ts +1243 -0
  66. package/src/lib/download-job-manager.ts +213 -0
  67. package/src/tui/MultiServerMonitorApp.ts +1 -1
  68. package/src/types/admin-config.ts +25 -0
  69. package/src/utils/log-parser.ts +13 -0
  70. package/web/README.md +429 -0
  71. package/web/eslint.config.js +23 -0
  72. package/web/index.html +13 -0
  73. package/web/llamacpp-web-dist.tar.gz +0 -0
  74. package/web/package-lock.json +4017 -0
  75. package/web/package.json +38 -0
  76. package/web/postcss.config.js +6 -0
  77. package/web/public/vite.svg +1 -0
  78. package/web/src/App.css +42 -0
  79. package/web/src/App.tsx +86 -0
  80. package/web/src/assets/react.svg +1 -0
  81. package/web/src/components/ApiKeyPrompt.tsx +71 -0
  82. package/web/src/components/CreateServerModal.tsx +372 -0
  83. package/web/src/components/DownloadProgress.tsx +123 -0
  84. package/web/src/components/Nav.tsx +89 -0
  85. package/web/src/components/RouterConfigModal.tsx +240 -0
  86. package/web/src/components/SearchModal.tsx +306 -0
  87. package/web/src/components/ServerConfigModal.tsx +291 -0
  88. package/web/src/hooks/useApi.ts +259 -0
  89. package/web/src/index.css +42 -0
  90. package/web/src/lib/api.ts +226 -0
  91. package/web/src/main.tsx +10 -0
  92. package/web/src/pages/Dashboard.tsx +103 -0
  93. package/web/src/pages/Models.tsx +258 -0
  94. package/web/src/pages/Router.tsx +270 -0
  95. package/web/src/pages/RouterLogs.tsx +201 -0
  96. package/web/src/pages/ServerLogs.tsx +553 -0
  97. package/web/src/pages/Servers.tsx +358 -0
  98. package/web/src/types/api.ts +140 -0
  99. package/web/tailwind.config.js +31 -0
  100. package/web/tsconfig.app.json +28 -0
  101. package/web/tsconfig.json +7 -0
  102. package/web/tsconfig.node.json +26 -0
  103. package/web/vite.config.ts +25 -0
  104. package/MONITORING-ACCURACY-FIX.md +0 -199
  105. package/PER-PROCESS-METRICS.md +0 -190
@@ -0,0 +1,435 @@
1
+ import * as path from 'path';
2
+ import * as fs from 'fs/promises';
3
+ import * as crypto from 'crypto';
4
+ import { AdminConfig } from '../types/admin-config';
5
+ import { execCommand, execAsync } from '../utils/process-utils';
6
+ import {
7
+ ensureDir,
8
+ writeJsonAtomic,
9
+ readJson,
10
+ fileExists,
11
+ getConfigDir,
12
+ getLogsDir,
13
+ getLaunchAgentsDir,
14
+ writeFileAtomic,
15
+ } from '../utils/file-utils';
16
+
17
+ export interface AdminServiceStatus {
18
+ isRunning: boolean;
19
+ pid: number | null;
20
+ exitCode: number | null;
21
+ lastExitReason?: string;
22
+ }
23
+
24
+ export class AdminManager {
25
+ private configDir: string;
26
+ private logsDir: string;
27
+ private configPath: string;
28
+ private launchAgentsDir: string;
29
+
30
+ constructor() {
31
+ this.configDir = getConfigDir();
32
+ this.logsDir = getLogsDir();
33
+ this.configPath = path.join(this.configDir, 'admin.json');
34
+ this.launchAgentsDir = getLaunchAgentsDir();
35
+ }
36
+
37
+ /**
38
+ * Initialize admin directories
39
+ */
40
+ async initialize(): Promise<void> {
41
+ await ensureDir(this.configDir);
42
+ await ensureDir(this.logsDir);
43
+ await ensureDir(this.launchAgentsDir);
44
+ }
45
+
46
+ /**
47
+ * Generate a secure random API key
48
+ */
49
+ generateApiKey(): string {
50
+ return crypto.randomBytes(32).toString('hex');
51
+ }
52
+
53
+ /**
54
+ * Get default admin configuration
55
+ */
56
+ getDefaultConfig(): AdminConfig {
57
+ return {
58
+ id: 'admin',
59
+ port: 9200,
60
+ host: '127.0.0.1',
61
+ apiKey: this.generateApiKey(),
62
+ label: 'com.llama.admin',
63
+ plistPath: path.join(this.launchAgentsDir, 'com.llama.admin.plist'),
64
+ stdoutPath: path.join(this.logsDir, 'admin.stdout'),
65
+ stderrPath: path.join(this.logsDir, 'admin.stderr'),
66
+ requestTimeout: 30000,
67
+ verbose: false,
68
+ status: 'stopped',
69
+ createdAt: new Date().toISOString(),
70
+ };
71
+ }
72
+
73
+ /**
74
+ * Load admin configuration
75
+ */
76
+ async loadConfig(): Promise<AdminConfig | null> {
77
+ if (!(await fileExists(this.configPath))) {
78
+ return null;
79
+ }
80
+ return await readJson<AdminConfig>(this.configPath);
81
+ }
82
+
83
+ /**
84
+ * Save admin configuration
85
+ */
86
+ async saveConfig(config: AdminConfig): Promise<void> {
87
+ await writeJsonAtomic(this.configPath, config);
88
+ }
89
+
90
+ /**
91
+ * Update admin configuration with partial changes
92
+ */
93
+ async updateConfig(updates: Partial<AdminConfig>): Promise<void> {
94
+ const existingConfig = await this.loadConfig();
95
+ if (!existingConfig) {
96
+ throw new Error('Admin configuration not found');
97
+ }
98
+ const updatedConfig = { ...existingConfig, ...updates };
99
+ await this.saveConfig(updatedConfig);
100
+ }
101
+
102
+ /**
103
+ * Delete admin configuration
104
+ */
105
+ async deleteConfig(): Promise<void> {
106
+ if (await fileExists(this.configPath)) {
107
+ await fs.unlink(this.configPath);
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Regenerate API key
113
+ */
114
+ async regenerateApiKey(): Promise<string> {
115
+ const config = await this.loadConfig();
116
+ if (!config) {
117
+ throw new Error('Admin configuration not found');
118
+ }
119
+ const newApiKey = this.generateApiKey();
120
+ await this.updateConfig({ apiKey: newApiKey });
121
+ return newApiKey;
122
+ }
123
+
124
+ /**
125
+ * Generate plist XML content for the admin service
126
+ */
127
+ generatePlist(config: AdminConfig): string {
128
+ // Find the compiled admin-server.js file
129
+ // In dev mode (tsx), __dirname is src/lib/
130
+ // In production, __dirname is dist/lib/
131
+ // Always use the compiled dist version for launchctl
132
+ let adminServerPath: string;
133
+ if (__dirname.includes('/src/')) {
134
+ // Dev mode - point to dist/lib/admin-server.js
135
+ const projectRoot = path.resolve(__dirname, '../..');
136
+ adminServerPath = path.join(projectRoot, 'dist/lib/admin-server.js');
137
+ } else {
138
+ // Production mode - already in dist/lib/
139
+ adminServerPath = path.join(__dirname, 'admin-server.js');
140
+ }
141
+
142
+ // Use the current Node.js executable path (resolves symlinks)
143
+ const nodePath = process.execPath;
144
+
145
+ const args = [
146
+ nodePath,
147
+ adminServerPath,
148
+ '--config', this.configPath,
149
+ ];
150
+
151
+ const argsXml = args.map(arg => ` <string>${arg}</string>`).join('\n');
152
+
153
+ return `<?xml version="1.0" encoding="UTF-8"?>
154
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
155
+ "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
156
+ <plist version="1.0">
157
+ <dict>
158
+ <key>Label</key>
159
+ <string>${config.label}</string>
160
+
161
+ <key>ProgramArguments</key>
162
+ <array>
163
+ ${argsXml}
164
+ </array>
165
+
166
+ <key>RunAtLoad</key>
167
+ <false/>
168
+
169
+ <key>KeepAlive</key>
170
+ <dict>
171
+ <key>Crashed</key>
172
+ <true/>
173
+ <key>SuccessfulExit</key>
174
+ <false/>
175
+ </dict>
176
+
177
+ <key>StandardOutPath</key>
178
+ <string>${config.stdoutPath}</string>
179
+
180
+ <key>StandardErrorPath</key>
181
+ <string>${config.stderrPath}</string>
182
+
183
+ <key>WorkingDirectory</key>
184
+ <string>/tmp</string>
185
+
186
+ <key>ThrottleInterval</key>
187
+ <integer>10</integer>
188
+ </dict>
189
+ </plist>
190
+ `;
191
+ }
192
+
193
+ /**
194
+ * Create and write plist file
195
+ */
196
+ async createPlist(config: AdminConfig): Promise<void> {
197
+ const plistContent = this.generatePlist(config);
198
+ await writeFileAtomic(config.plistPath, plistContent);
199
+ }
200
+
201
+ /**
202
+ * Delete plist file
203
+ */
204
+ async deletePlist(config: AdminConfig): Promise<void> {
205
+ if (await fileExists(config.plistPath)) {
206
+ await fs.unlink(config.plistPath);
207
+ }
208
+ }
209
+
210
+ /**
211
+ * Load service (register with launchctl)
212
+ */
213
+ async loadService(plistPath: string): Promise<void> {
214
+ await execCommand(`launchctl load "${plistPath}"`);
215
+ }
216
+
217
+ /**
218
+ * Unload service (unregister from launchctl)
219
+ */
220
+ async unloadService(plistPath: string): Promise<void> {
221
+ try {
222
+ await execCommand(`launchctl unload "${plistPath}"`);
223
+ } catch (error) {
224
+ // Ignore errors if service is not loaded
225
+ }
226
+ }
227
+
228
+ /**
229
+ * Start service
230
+ */
231
+ async startService(label: string): Promise<void> {
232
+ await execCommand(`launchctl start ${label}`);
233
+ }
234
+
235
+ /**
236
+ * Stop service
237
+ */
238
+ async stopService(label: string): Promise<void> {
239
+ await execCommand(`launchctl stop ${label}`);
240
+ }
241
+
242
+ /**
243
+ * Get service status from launchctl
244
+ */
245
+ async getServiceStatus(label: string): Promise<AdminServiceStatus> {
246
+ try {
247
+ const { stdout } = await execAsync(`launchctl list | grep ${label}`);
248
+ const lines = stdout.trim().split('\n');
249
+
250
+ for (const line of lines) {
251
+ const parts = line.split(/\s+/);
252
+ if (parts.length >= 3) {
253
+ const pidStr = parts[0].trim();
254
+ const exitCodeStr = parts[1].trim();
255
+ const serviceLabel = parts[2].trim();
256
+
257
+ if (serviceLabel === label) {
258
+ const pid = pidStr !== '-' ? parseInt(pidStr, 10) : null;
259
+ const exitCode = exitCodeStr !== '-' ? parseInt(exitCodeStr, 10) : null;
260
+ const isRunning = pid !== null;
261
+
262
+ return {
263
+ isRunning,
264
+ pid,
265
+ exitCode,
266
+ lastExitReason: this.interpretExitCode(exitCode),
267
+ };
268
+ }
269
+ }
270
+ }
271
+
272
+ return {
273
+ isRunning: false,
274
+ pid: null,
275
+ exitCode: null,
276
+ };
277
+ } catch (error) {
278
+ return {
279
+ isRunning: false,
280
+ pid: null,
281
+ exitCode: null,
282
+ };
283
+ }
284
+ }
285
+
286
+ /**
287
+ * Interpret exit code to human-readable reason
288
+ */
289
+ private interpretExitCode(code: number | null): string | undefined {
290
+ if (code === null || code === 0) return undefined;
291
+ if (code === -9) return 'Force killed (SIGKILL)';
292
+ if (code === -15) return 'Terminated (SIGTERM)';
293
+ return `Exit code: ${code}`;
294
+ }
295
+
296
+ /**
297
+ * Wait for service to start (with timeout)
298
+ */
299
+ async waitForServiceStart(label: string, timeoutMs = 5000): Promise<boolean> {
300
+ const startTime = Date.now();
301
+ while (Date.now() - startTime < timeoutMs) {
302
+ const status = await this.getServiceStatus(label);
303
+ if (status.isRunning) {
304
+ return true;
305
+ }
306
+ await new Promise((resolve) => setTimeout(resolve, 500));
307
+ }
308
+ return false;
309
+ }
310
+
311
+ /**
312
+ * Wait for service to stop (with timeout)
313
+ */
314
+ async waitForServiceStop(label: string, timeoutMs = 5000): Promise<boolean> {
315
+ const startTime = Date.now();
316
+ while (Date.now() - startTime < timeoutMs) {
317
+ const status = await this.getServiceStatus(label);
318
+ if (!status.isRunning) {
319
+ return true;
320
+ }
321
+ await new Promise((resolve) => setTimeout(resolve, 500));
322
+ }
323
+ return false;
324
+ }
325
+
326
+ /**
327
+ * Start admin service
328
+ */
329
+ async start(): Promise<void> {
330
+ await this.initialize();
331
+
332
+ let config = await this.loadConfig();
333
+ if (!config) {
334
+ // Create default config
335
+ config = this.getDefaultConfig();
336
+ await this.saveConfig(config);
337
+ }
338
+
339
+ // Check if already running
340
+ if (config.status === 'running') {
341
+ throw new Error('Admin service is already running');
342
+ }
343
+
344
+ // Check for throttled state (exit code 78)
345
+ const currentStatus = await this.getServiceStatus(config.label);
346
+ if (currentStatus.exitCode === 78) {
347
+ // Service is throttled - clean up and start fresh
348
+ await this.unloadService(config.plistPath);
349
+ await this.deletePlist(config);
350
+ // Give launchd a moment to clean up
351
+ await new Promise((resolve) => setTimeout(resolve, 1000));
352
+ }
353
+
354
+ // Create plist
355
+ await this.createPlist(config);
356
+
357
+ // Load and start service
358
+ try {
359
+ await this.loadService(config.plistPath);
360
+ } catch (error) {
361
+ // May already be loaded
362
+ }
363
+
364
+ await this.startService(config.label);
365
+
366
+ // Wait for startup
367
+ const started = await this.waitForServiceStart(config.label, 5000);
368
+ if (!started) {
369
+ throw new Error('Admin service failed to start');
370
+ }
371
+
372
+ // Update config
373
+ const status = await this.getServiceStatus(config.label);
374
+ await this.updateConfig({
375
+ status: 'running',
376
+ pid: status.pid || undefined,
377
+ lastStarted: new Date().toISOString(),
378
+ });
379
+ }
380
+
381
+ /**
382
+ * Stop admin service
383
+ */
384
+ async stop(): Promise<void> {
385
+ const config = await this.loadConfig();
386
+ if (!config) {
387
+ throw new Error('Admin configuration not found');
388
+ }
389
+
390
+ if (config.status !== 'running') {
391
+ throw new Error('Admin service is not running');
392
+ }
393
+
394
+ // Unload service
395
+ await this.unloadService(config.plistPath);
396
+
397
+ // Wait for shutdown
398
+ await this.waitForServiceStop(config.label, 5000);
399
+
400
+ // Update config
401
+ await this.updateConfig({
402
+ status: 'stopped',
403
+ pid: undefined,
404
+ lastStopped: new Date().toISOString(),
405
+ });
406
+ }
407
+
408
+ /**
409
+ * Restart admin service
410
+ */
411
+ async restart(): Promise<void> {
412
+ try {
413
+ await this.stop();
414
+ } catch (error) {
415
+ // May not be running
416
+ }
417
+ await this.start();
418
+ }
419
+
420
+ /**
421
+ * Get admin status
422
+ */
423
+ async getStatus(): Promise<{ config: AdminConfig; status: AdminServiceStatus } | null> {
424
+ const config = await this.loadConfig();
425
+ if (!config) {
426
+ return null;
427
+ }
428
+
429
+ const status = await this.getServiceStatus(config.label);
430
+ return { config, status };
431
+ }
432
+ }
433
+
434
+ // Export singleton instance
435
+ export const adminManager = new AdminManager();