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