@rethinkingstudio/clawpilot 1.1.10 → 1.1.11

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.
@@ -0,0 +1,464 @@
1
+ import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "fs";
2
+ import { execSync } from "child_process";
3
+ import { homedir } from "os";
4
+ import { join } from "path";
5
+
6
+ export type ServicePlatform = "macos" | "linux" | "unsupported";
7
+
8
+ export interface ServiceStatus {
9
+ platform: ServicePlatform;
10
+ installed: boolean;
11
+ running: boolean;
12
+ serviceName: string;
13
+ manager: string;
14
+ servicePath?: string;
15
+ logPath: string;
16
+ startHint?: string;
17
+ }
18
+
19
+ const MAC_LABEL = "com.rethinkingstudio.clawpilot";
20
+ const MAC_LABEL_OLD = "com.rethinkingstudio.clawai";
21
+ const MAC_PLIST_DIR = join(homedir(), "Library", "LaunchAgents");
22
+ const MAC_PLIST_PATH = join(MAC_PLIST_DIR, `${MAC_LABEL}.plist`);
23
+ const MAC_PLIST_PATH_OLD = join(MAC_PLIST_DIR, `${MAC_LABEL_OLD}.plist`);
24
+
25
+ const LINUX_SERVICE_NAME = "clawpilot.service";
26
+ const LINUX_SYSTEMD_USER_DIR = join(homedir(), ".config", "systemd", "user");
27
+ const LINUX_SERVICE_PATH = join(LINUX_SYSTEMD_USER_DIR, LINUX_SERVICE_NAME);
28
+
29
+ const LOG_DIR = join(homedir(), ".clawai");
30
+ const LOG_PATH = join(LOG_DIR, "clawpilot.log");
31
+ const ERROR_LOG_PATH = join(LOG_DIR, "clawpilot-error.log");
32
+ const LINUX_NOHUP_PID_PATH = join(LOG_DIR, "clawpilot.pid");
33
+ const LINUX_NOHUP_START_SCRIPT_PATH = join(LOG_DIR, "clawpilot-start.sh");
34
+
35
+ function detectPlatform(): ServicePlatform {
36
+ if (process.platform === "darwin") return "macos";
37
+ if (process.platform === "linux") return "linux";
38
+ return "unsupported";
39
+ }
40
+
41
+ function shellEscape(arg: string): string {
42
+ return `'${arg.replace(/'/g, `'\\''`)}'`;
43
+ }
44
+
45
+ function run(command: string, stdio: "pipe" | "inherit" = "pipe"): void {
46
+ execSync(command, { stdio });
47
+ }
48
+
49
+ function commandExists(command: string): boolean {
50
+ try {
51
+ run(`command -v ${command}`, "pipe");
52
+ return true;
53
+ } catch {
54
+ return false;
55
+ }
56
+ }
57
+
58
+ function getProgramArgs(): string[] {
59
+ const nodeBin = process.execPath;
60
+ const scriptPath = process.argv[1];
61
+ return nodeBin === scriptPath ? [scriptPath, "run"] : [nodeBin, scriptPath, "run"];
62
+ }
63
+
64
+ function ensureLogDir(): void {
65
+ mkdirSync(LOG_DIR, { recursive: true });
66
+ }
67
+
68
+ function canUseSystemdUser(): boolean {
69
+ if (!commandExists("systemctl")) return false;
70
+ try {
71
+ run("systemctl --user show-environment", "pipe");
72
+ return true;
73
+ } catch {
74
+ return false;
75
+ }
76
+ }
77
+
78
+ function isPidRunning(pid: number): boolean {
79
+ try {
80
+ process.kill(pid, 0);
81
+ return true;
82
+ } catch {
83
+ return false;
84
+ }
85
+ }
86
+
87
+ function readNohupPid(): number | null {
88
+ if (!existsSync(LINUX_NOHUP_PID_PATH)) return null;
89
+ try {
90
+ const raw = readFileSync(LINUX_NOHUP_PID_PATH, "utf-8").trim();
91
+ const pid = Number(raw);
92
+ return Number.isInteger(pid) && pid > 0 ? pid : null;
93
+ } catch {
94
+ return null;
95
+ }
96
+ }
97
+
98
+ function removeNohupPidFile(): void {
99
+ if (existsSync(LINUX_NOHUP_PID_PATH)) {
100
+ unlinkSync(LINUX_NOHUP_PID_PATH);
101
+ }
102
+ }
103
+
104
+ function getNohupStartCommand(): string {
105
+ return `bash ${shellEscape(LINUX_NOHUP_START_SCRIPT_PATH)}`;
106
+ }
107
+
108
+ function writeLinuxNohupStartScript(): void {
109
+ const args = getProgramArgs().map(shellEscape).join(" ");
110
+ const script = `#!/usr/bin/env bash
111
+ set -euo pipefail
112
+
113
+ mkdir -p ${shellEscape(LOG_DIR)}
114
+ if [ -f ${shellEscape(LINUX_NOHUP_PID_PATH)} ]; then
115
+ pid="$(cat ${shellEscape(LINUX_NOHUP_PID_PATH)} 2>/dev/null || true)"
116
+ if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
117
+ echo "clawpilot is already running (pid=$pid)"
118
+ exit 0
119
+ fi
120
+ fi
121
+
122
+ nohup ${args} >> ${shellEscape(LOG_PATH)} 2>> ${shellEscape(ERROR_LOG_PATH)} < /dev/null &
123
+ echo $! > ${shellEscape(LINUX_NOHUP_PID_PATH)}
124
+ echo "clawpilot started in nohup mode (pid=$(cat ${shellEscape(LINUX_NOHUP_PID_PATH)}))"
125
+ `;
126
+ writeFileSync(LINUX_NOHUP_START_SCRIPT_PATH, script, { encoding: "utf-8", mode: 0o755 });
127
+ }
128
+
129
+ function installMacService(): boolean {
130
+ const argsXml = getProgramArgs().map((arg) => ` <string>${arg}</string>`).join("\n");
131
+ const plistContent = `<?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>${MAC_LABEL}</string>
138
+ <key>ProgramArguments</key>
139
+ <array>
140
+ ${argsXml}
141
+ </array>
142
+ <key>RunAtLoad</key>
143
+ <true/>
144
+ <key>KeepAlive</key>
145
+ <true/>
146
+ <key>StandardOutPath</key>
147
+ <string>${LOG_PATH}</string>
148
+ <key>StandardErrorPath</key>
149
+ <string>${ERROR_LOG_PATH}</string>
150
+ </dict>
151
+ </plist>`;
152
+
153
+ mkdirSync(MAC_PLIST_DIR, { recursive: true });
154
+ ensureLogDir();
155
+
156
+ try {
157
+ run(`launchctl unload -w "${MAC_PLIST_PATH}"`);
158
+ } catch {
159
+ // Ignore if not loaded.
160
+ }
161
+
162
+ writeFileSync(MAC_PLIST_PATH, plistContent, "utf-8");
163
+
164
+ try {
165
+ run(`launchctl load -w "${MAC_PLIST_PATH}"`, "inherit");
166
+ return true;
167
+ } catch {
168
+ return false;
169
+ }
170
+ }
171
+
172
+ function installLinuxServiceSystemd(): boolean {
173
+ mkdirSync(LINUX_SYSTEMD_USER_DIR, { recursive: true });
174
+ ensureLogDir();
175
+
176
+ const args = getProgramArgs().map(shellEscape).join(" ");
177
+ const serviceContent = `[Unit]
178
+ Description=ClawPilot relay client
179
+ After=network-online.target
180
+ Wants=network-online.target
181
+
182
+ [Service]
183
+ Type=simple
184
+ ExecStart=${args}
185
+ Restart=always
186
+ RestartSec=5
187
+ WorkingDirectory=${shellEscape(process.cwd())}
188
+ StandardOutput=append:${LOG_PATH}
189
+ StandardError=append:${ERROR_LOG_PATH}
190
+
191
+ [Install]
192
+ WantedBy=default.target
193
+ `;
194
+
195
+ writeFileSync(LINUX_SERVICE_PATH, serviceContent, "utf-8");
196
+ run("systemctl --user daemon-reload", "inherit");
197
+ run(`systemctl --user enable --now ${LINUX_SERVICE_NAME}`, "inherit");
198
+ return true;
199
+ }
200
+
201
+ function installLinuxServiceNohup(): boolean {
202
+ ensureLogDir();
203
+ writeLinuxNohupStartScript();
204
+ run(`sh -lc ${shellEscape(getNohupStartCommand())}`, "inherit");
205
+ const pid = readNohupPid();
206
+ return pid != null && isPidRunning(pid);
207
+ }
208
+
209
+ function installLinuxService(): boolean {
210
+ if (canUseSystemdUser()) {
211
+ try {
212
+ return installLinuxServiceSystemd();
213
+ } catch {
214
+ // Fall back to nohup below.
215
+ }
216
+ }
217
+
218
+ try {
219
+ return installLinuxServiceNohup();
220
+ } catch {
221
+ return false;
222
+ }
223
+ }
224
+
225
+ function uninstallMacArtifacts(): boolean {
226
+ let changed = false;
227
+ try {
228
+ run(`launchctl unload -w "${MAC_PLIST_PATH}"`);
229
+ changed = true;
230
+ } catch {
231
+ // ignore
232
+ }
233
+ if (existsSync(MAC_PLIST_PATH)) {
234
+ unlinkSync(MAC_PLIST_PATH);
235
+ changed = true;
236
+ }
237
+ try {
238
+ run(`launchctl unload -w "${MAC_PLIST_PATH_OLD}"`);
239
+ changed = true;
240
+ } catch {
241
+ // ignore
242
+ }
243
+ if (existsSync(MAC_PLIST_PATH_OLD)) {
244
+ unlinkSync(MAC_PLIST_PATH_OLD);
245
+ changed = true;
246
+ }
247
+ return changed;
248
+ }
249
+
250
+ function uninstallLinuxArtifacts(removeFile: boolean): boolean {
251
+ let changed = false;
252
+
253
+ if (canUseSystemdUser()) {
254
+ try {
255
+ run(`systemctl --user stop ${LINUX_SERVICE_NAME}`);
256
+ changed = true;
257
+ } catch {
258
+ // ignore
259
+ }
260
+ try {
261
+ run(`systemctl --user disable ${LINUX_SERVICE_NAME}`);
262
+ changed = true;
263
+ } catch {
264
+ // ignore
265
+ }
266
+ try {
267
+ run("systemctl --user daemon-reload");
268
+ } catch {
269
+ // ignore
270
+ }
271
+ }
272
+
273
+ const nohupPid = readNohupPid();
274
+ if (nohupPid != null) {
275
+ try {
276
+ process.kill(nohupPid, "SIGTERM");
277
+ changed = true;
278
+ } catch {
279
+ // ignore
280
+ }
281
+ removeNohupPidFile();
282
+ changed = true;
283
+ }
284
+
285
+ if (removeFile && existsSync(LINUX_SERVICE_PATH)) {
286
+ unlinkSync(LINUX_SERVICE_PATH);
287
+ changed = true;
288
+ }
289
+ if (removeFile && existsSync(LINUX_NOHUP_START_SCRIPT_PATH)) {
290
+ unlinkSync(LINUX_NOHUP_START_SCRIPT_PATH);
291
+ changed = true;
292
+ }
293
+
294
+ return changed;
295
+ }
296
+
297
+ function restartMacService(): boolean {
298
+ return installMacService();
299
+ }
300
+
301
+ function restartLinuxService(): boolean {
302
+ if (canUseSystemdUser() && existsSync(LINUX_SERVICE_PATH)) {
303
+ try {
304
+ run("systemctl --user daemon-reload", "inherit");
305
+ run(`systemctl --user restart ${LINUX_SERVICE_NAME}`, "inherit");
306
+ return true;
307
+ } catch {
308
+ // Fall back to nohup restart below.
309
+ }
310
+ }
311
+
312
+ uninstallLinuxArtifacts(false);
313
+ try {
314
+ return installLinuxServiceNohup();
315
+ } catch {
316
+ return false;
317
+ }
318
+ }
319
+
320
+ export function getServicePlatform(): ServicePlatform {
321
+ return detectPlatform();
322
+ }
323
+
324
+ export function installService(): boolean {
325
+ switch (detectPlatform()) {
326
+ case "macos":
327
+ return installMacService();
328
+ case "linux":
329
+ return installLinuxService();
330
+ default:
331
+ return false;
332
+ }
333
+ }
334
+
335
+ export function restartService(): boolean {
336
+ switch (detectPlatform()) {
337
+ case "macos":
338
+ return restartMacService();
339
+ case "linux":
340
+ return restartLinuxService();
341
+ default:
342
+ return false;
343
+ }
344
+ }
345
+
346
+ export function stopService(): boolean {
347
+ switch (detectPlatform()) {
348
+ case "macos":
349
+ return uninstallMacArtifacts();
350
+ case "linux":
351
+ return uninstallLinuxArtifacts(false);
352
+ default:
353
+ return false;
354
+ }
355
+ }
356
+
357
+ export function uninstallService(): boolean {
358
+ switch (detectPlatform()) {
359
+ case "macos":
360
+ return uninstallMacArtifacts();
361
+ case "linux":
362
+ return uninstallLinuxArtifacts(true);
363
+ default:
364
+ return false;
365
+ }
366
+ }
367
+
368
+ export function getServiceStatus(): ServiceStatus {
369
+ const platform = detectPlatform();
370
+
371
+ if (platform === "macos") {
372
+ let running = false;
373
+ try {
374
+ run(`launchctl list ${MAC_LABEL}`);
375
+ running = true;
376
+ } catch {
377
+ running = false;
378
+ }
379
+ return {
380
+ platform,
381
+ installed: existsSync(MAC_PLIST_PATH),
382
+ running,
383
+ serviceName: MAC_LABEL,
384
+ manager: "launchd",
385
+ servicePath: MAC_PLIST_PATH,
386
+ logPath: LOG_PATH,
387
+ startHint: `launchctl start ${MAC_LABEL}`,
388
+ };
389
+ }
390
+
391
+ if (platform === "linux") {
392
+ const pid = readNohupPid();
393
+ const hasNohupArtifacts = pid != null || existsSync(LINUX_NOHUP_START_SCRIPT_PATH);
394
+ const running = pid != null && isPidRunning(pid);
395
+ if (!running && pid != null) {
396
+ removeNohupPidFile();
397
+ }
398
+
399
+ if (running || (hasNohupArtifacts && !canUseSystemdUser())) {
400
+ return {
401
+ platform,
402
+ installed: hasNohupArtifacts,
403
+ running,
404
+ serviceName: "clawpilot (nohup)",
405
+ manager: "nohup",
406
+ servicePath: existsSync(LINUX_NOHUP_START_SCRIPT_PATH) ? LINUX_NOHUP_START_SCRIPT_PATH : undefined,
407
+ logPath: LOG_PATH,
408
+ startHint: getNohupStartCommand(),
409
+ };
410
+ }
411
+
412
+ const hasSystemdServiceFile = existsSync(LINUX_SERVICE_PATH);
413
+ if (hasSystemdServiceFile) {
414
+ let systemdRunning = false;
415
+ if (canUseSystemdUser()) {
416
+ try {
417
+ run(`systemctl --user is-active --quiet ${LINUX_SERVICE_NAME}`);
418
+ systemdRunning = true;
419
+ } catch {
420
+ systemdRunning = false;
421
+ }
422
+ }
423
+ return {
424
+ platform,
425
+ installed: true,
426
+ running: systemdRunning,
427
+ serviceName: LINUX_SERVICE_NAME,
428
+ manager: "systemd",
429
+ servicePath: LINUX_SERVICE_PATH,
430
+ logPath: LOG_PATH,
431
+ startHint: `systemctl --user start ${LINUX_SERVICE_NAME}`,
432
+ };
433
+ }
434
+
435
+ return {
436
+ platform,
437
+ installed: hasNohupArtifacts,
438
+ running,
439
+ serviceName: "clawpilot (nohup)",
440
+ manager: "nohup",
441
+ servicePath: existsSync(LINUX_NOHUP_START_SCRIPT_PATH) ? LINUX_NOHUP_START_SCRIPT_PATH : undefined,
442
+ logPath: LOG_PATH,
443
+ startHint: getNohupStartCommand(),
444
+ };
445
+ }
446
+
447
+ return {
448
+ platform,
449
+ installed: false,
450
+ running: false,
451
+ serviceName: "",
452
+ manager: "unsupported",
453
+ logPath: LOG_PATH,
454
+ };
455
+ }
456
+
457
+ export const servicePaths = {
458
+ logPath: LOG_PATH,
459
+ errorLogPath: ERROR_LOG_PATH,
460
+ macPlistPath: MAC_PLIST_PATH,
461
+ linuxServicePath: LINUX_SERVICE_PATH,
462
+ linuxNohupPidPath: LINUX_NOHUP_PID_PATH,
463
+ linuxNohupStartScriptPath: LINUX_NOHUP_START_SCRIPT_PATH,
464
+ };