ps-claw 1.0.7 → 1.0.8

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 (3) hide show
  1. package/cli.mjs +37 -58
  2. package/package.json +1 -2
  3. package/ps-claw.mjs +0 -661
package/cli.mjs CHANGED
@@ -1,9 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  /**
4
- * PS Claw CLI — ponto de entrada global
5
- * Uso: ps-claw [comando]
6
- * Comandos: start | web | all | update | help
4
+ * PS Claw CLI — standalone, sem dependência do dist/
7
5
  */
8
6
 
9
7
  import { spawn } from "node:child_process";
@@ -33,7 +31,7 @@ ${C.cyan}${C.bold} ██████╔╝███████╗ ██
33
31
  ${C.cyan}${C.bold} ██╔═══╝ ╚════██║ ██║ ██║ ██╔══██║██║███╗██║${C.reset}
34
32
  ${C.cyan}${C.bold} ██║ ███████║ ╚██████╗███████╗██║ ██║╚███╔███╔╝${C.reset}
35
33
  ${C.cyan}${C.bold} ╚═╝ ╚══════╝ ╚═════╝╚══════╝╚═╝ ╚═╝ ╚══╝╚══╝${C.reset}
36
- ${C.dim}v1.0.0 — Lightweight AI Agent Gateway${C.reset}
34
+ ${C.dim}v1.0.8 — Lightweight AI Agent Gateway${C.reset}
37
35
  `);
38
36
  }
39
37
 
@@ -41,79 +39,60 @@ function help() {
41
39
  banner();
42
40
  console.log(` ${C.bold}Comandos:${C.reset}
43
41
 
44
- ${C.green}ps-claw start${C.reset} Inicia o agente PS Claw
45
- ${C.green}ps-claw web${C.reset} Inicia a interface web em http://localhost:3000
46
- ${C.green}ps-claw all${C.reset} Inicia o agente + interface web juntos
47
- ${C.green}ps-claw update${C.reset} Atualiza o PS Claw
48
- ${C.green}ps-claw help${C.reset} Esta mensagem
49
-
50
- ${C.bold}Início rápido:${C.reset}
51
-
52
- ${C.dim}# Via npm (global)${C.reset}
53
- npm install -g ps-claw
54
- ps-claw all
55
-
56
- ${C.dim}# Via git clone${C.reset}
57
- git clone https://github.com/Pedro21062014/ps-claw-v2.git
58
- cd ps-claw-v2 && npm install
59
- ps-claw all
42
+ ${C.green}npx ps-claw web${C.reset} Abre a interface web em http://localhost:3000
43
+ ${C.green}npx ps-claw start${C.reset} Inicia o agente PS Claw
44
+ ${C.green}npx ps-claw all${C.reset} Inicia tudo junto
45
+ ${C.green}npx ps-claw update${C.reset} Atualiza o PS Claw
46
+ ${C.green}npx ps-claw help${C.reset} Esta mensagem
60
47
 
61
48
  ${C.bold}Interface web:${C.reset} http://localhost:3000
62
49
  `);
63
50
  }
64
51
 
65
- function run(file, extraArgs = []) {
66
- if (!existsSync(file)) {
67
- console.error(`${C.red}❌ Arquivo não encontrado: ${file}${C.reset}`);
52
+ function startWeb() {
53
+ const srv = path.join(__dirname, "web-ui", "server.mjs");
54
+ if (!existsSync(srv)) {
55
+ console.error(`${C.red}❌ web-ui/server.mjs não encontrado!${C.reset}`);
68
56
  process.exit(1);
69
57
  }
70
- const proc = spawn(process.execPath, [file, ...extraArgs], { stdio: "inherit" });
58
+ console.log(`${C.green}🌐 Iniciando Interface Web...${C.reset}`);
59
+ console.log(`${C.cyan} Acesse: http://localhost:3000${C.reset}\n`);
60
+ const proc = spawn(process.execPath, [srv], { stdio: "inherit" });
71
61
  proc.on("exit", code => process.exit(code ?? 0));
72
- return proc;
73
62
  }
74
63
 
75
64
  function startAgent() {
76
- console.log(`${C.green}🦞 Iniciando PS Claw Agent...${C.reset}`);
77
- run(path.join(__dirname, "ps-claw.mjs"), args.slice(1));
78
- }
65
+ // Usa o ps-claw.mjs original do OpenClaw se existir e tiver dist/
66
+ // Caso contrário, avisa o usuário e abre a web
67
+ const distEntry = path.join(__dirname, "dist", "entry.mjs");
68
+ const distEntryJs = path.join(__dirname, "dist", "entry.js");
69
+
70
+ if (!existsSync(distEntry) && !existsSync(distEntryJs)) {
71
+ console.log(`${C.yellow}⚠️ O agente requer configuração adicional (dist/).${C.reset}`);
72
+ console.log(`${C.dim} Para usar a interface web, execute: npx ps-claw web${C.reset}\n`);
73
+ console.log(`${C.green}🌐 Iniciando Interface Web automaticamente...${C.reset}`);
74
+ console.log(`${C.cyan} Acesse: http://localhost:3000${C.reset}\n`);
75
+ startWeb();
76
+ return;
77
+ }
79
78
 
80
- function startWeb() {
81
- const srv = path.join(__dirname, "web-ui", "server.mjs");
82
- console.log(`${C.green}🌐 Interface Web http://localhost:3000${C.reset}`);
83
- run(srv);
79
+ console.log(`${C.green}🦞 Iniciando PS Claw Agent...${C.reset}`);
80
+ const proc = spawn(process.execPath, [path.join(__dirname, "ps-claw.mjs"), ...args.slice(1)], { stdio: "inherit" });
81
+ proc.on("exit", code => process.exit(code ?? 0));
84
82
  }
85
83
 
86
84
  function startAll() {
87
85
  banner();
88
- const agentFile = path.join(__dirname, "ps-claw.mjs");
89
- const webFile = path.join(__dirname, "web-ui", "server.mjs");
90
-
91
- console.log(`${C.green}🦞 Iniciando PS Claw Agent...${C.reset}`);
92
- const agent = spawn(process.execPath, [agentFile], { stdio: "inherit" });
93
-
94
- setTimeout(() => {
95
- if (existsSync(webFile)) {
96
- console.log(`\n${C.cyan}🌐 Iniciando Interface Web → http://localhost:3000${C.reset}\n`);
97
- const web = spawn(process.execPath, [webFile], { stdio: "inherit" });
98
- web.on("exit", code => process.exit(code ?? 0));
99
- }
100
- }, 1500);
101
-
102
- agent.on("exit", code => process.exit(code ?? 0));
103
- process.on("SIGINT", () => { agent.kill(); process.exit(0); });
86
+ startWeb();
104
87
  }
105
88
 
106
89
  function update() {
107
- banner();
108
- const script = path.join(__dirname, "update.sh");
109
- console.log(`${C.yellow}🔄 Verificando atualizações...${C.reset}\n`);
110
- if (!existsSync(script)) {
111
- console.log(`${C.yellow}Baixando versão mais recente...${C.reset}`);
112
- run("git", ["pull"]);
113
- return;
114
- }
115
- const proc = spawn("bash", [script], { stdio: "inherit" });
116
- proc.on("exit", code => process.exit(code ?? 0));
90
+ console.log(`${C.yellow}🔄 Atualizando PS Claw...${C.reset}\n`);
91
+ const proc = spawn("npm", ["install", "-g", "ps-claw@latest"], { stdio: "inherit", shell: true });
92
+ proc.on("exit", code => {
93
+ if (code === 0) console.log(`\n${C.green}✅ PS Claw atualizado!${C.reset}`);
94
+ process.exit(code ?? 0);
95
+ });
117
96
  }
118
97
 
119
98
  switch (cmd) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ps-claw",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "PS Claw - AI Agent Gateway with multi-provider support, web UI, and CLI. Lightweight fork of OpenClaw.",
5
5
  "keywords": [
6
6
  "ai",
@@ -41,7 +41,6 @@
41
41
  },
42
42
  "files": [
43
43
  "cli.mjs",
44
- "ps-claw.mjs",
45
44
  "web-ui/",
46
45
  "update.sh",
47
46
  "README.md",
package/ps-claw.mjs DELETED
@@ -1,661 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import { spawn } from "node:child_process";
4
- import { existsSync, readFileSync, statSync } from "node:fs";
5
- import { access } from "node:fs/promises";
6
- import module from "node:module";
7
- import os from "node:os";
8
- import path from "node:path";
9
- import { fileURLToPath } from "node:url";
10
-
11
- const MIN_NODE_MAJOR = 22;
12
- const MIN_NODE_MINOR = 19;
13
- const MIN_NODE_VERSION = `${MIN_NODE_MAJOR}.${MIN_NODE_MINOR}`;
14
-
15
- const parseNodeVersion = (rawVersion) => {
16
- const [majorRaw = "0", minorRaw = "0"] = rawVersion.split(".");
17
- return {
18
- major: Number(majorRaw),
19
- minor: Number(minorRaw),
20
- };
21
- };
22
-
23
- const isSupportedNodeVersion = (version) =>
24
- version.major > MIN_NODE_MAJOR ||
25
- (version.major === MIN_NODE_MAJOR && version.minor >= MIN_NODE_MINOR);
26
-
27
- const ensureSupportedNodeVersion = () => {
28
- if (isSupportedNodeVersion(parseNodeVersion(process.versions.node))) {
29
- return;
30
- }
31
-
32
- process.stderr.write(
33
- `ps-claw: Node.js v${MIN_NODE_VERSION}+ is required (current: v${process.versions.node}).\n` +
34
- "If you use nvm, run:\n" +
35
- ` nvm install ${MIN_NODE_MAJOR}\n` +
36
- ` nvm use ${MIN_NODE_MAJOR}\n` +
37
- ` nvm alias default ${MIN_NODE_MAJOR}\n`,
38
- );
39
- process.exit(1);
40
- };
41
-
42
- ensureSupportedNodeVersion();
43
-
44
- if (tryOutputLauncherVersion(process.argv)) {
45
- process.exit(0);
46
- }
47
-
48
- const isSourceCheckoutLauncher = () =>
49
- existsSync(new URL("./.git", import.meta.url)) ||
50
- existsSync(new URL("./src/entry.ts", import.meta.url));
51
-
52
- const isNodeCompileCacheDisabled = () => process.env.NODE_DISABLE_COMPILE_CACHE !== undefined;
53
- const isNodeCompileCacheRequested = () =>
54
- Boolean(process.env.NODE_COMPILE_CACHE) && !isNodeCompileCacheDisabled();
55
- const sanitizeCompileCachePathSegment = (value) => {
56
- const normalized = value.replace(/[^A-Za-z0-9._-]+/g, "_").replace(/^_+|_+$/g, "");
57
- return normalized.length > 0 ? normalized : "unknown";
58
- };
59
- const readPackageVersion = () => {
60
- try {
61
- const parsed = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8"));
62
- if (typeof parsed?.version === "string" && parsed.version.trim().length > 0) {
63
- return parsed.version;
64
- }
65
- } catch {
66
- // Fall through to an install-metadata-only cache key.
67
- }
68
- return "unknown";
69
- };
70
- const resolvePackagedCompileCacheDirectory = () => {
71
- const packageJsonUrl = new URL("./package.json", import.meta.url);
72
- const version = sanitizeCompileCachePathSegment(readPackageVersion());
73
- let installMarker = "no-package-json";
74
- try {
75
- const stat = statSync(packageJsonUrl);
76
- installMarker = `${Math.trunc(stat.mtimeMs)}-${stat.size}`;
77
- } catch {
78
- // Package archives should always have package.json, but keep startup best-effort.
79
- }
80
- const baseDirectory = isNodeCompileCacheRequested()
81
- ? process.env.NODE_COMPILE_CACHE
82
- : path.join(os.tmpdir(), "node-compile-cache");
83
- return path.join(
84
- baseDirectory,
85
- "ps-claw",
86
- version,
87
- sanitizeCompileCachePathSegment(installMarker),
88
- );
89
- };
90
-
91
- const respawnSignals =
92
- process.platform === "win32"
93
- ? ["SIGTERM", "SIGINT", "SIGBREAK"]
94
- : ["SIGTERM", "SIGINT", "SIGHUP", "SIGQUIT"];
95
- const respawnSignalExitGraceMs = 1_000;
96
- const respawnSignalForceKillGraceMs = 1_000;
97
- const respawnSignalHardExitGraceMs = 1_000;
98
-
99
- const runRespawnedChild = (command, args, env) => {
100
- const child = spawn(command, args, {
101
- stdio: "inherit",
102
- env,
103
- });
104
- const listeners = new Map();
105
- // This intentionally overlaps with src/entry.compile-cache.ts; keep the
106
- // respawn supervision behavior in sync until the launcher can share TS code.
107
- // Give the child a moment to honor forwarded signals, then exit the wrapper so
108
- // a child that ignores SIGTERM cannot keep the launcher alive indefinitely.
109
- let signalExitTimer = null;
110
- let signalForceKillTimer = null;
111
- let signalHardExitTimer = null;
112
- const detach = () => {
113
- for (const [signal, listener] of listeners) {
114
- process.off(signal, listener);
115
- }
116
- listeners.clear();
117
- if (signalExitTimer) {
118
- clearTimeout(signalExitTimer);
119
- signalExitTimer = null;
120
- }
121
- if (signalForceKillTimer) {
122
- clearTimeout(signalForceKillTimer);
123
- signalForceKillTimer = null;
124
- }
125
- if (signalHardExitTimer) {
126
- clearTimeout(signalHardExitTimer);
127
- signalHardExitTimer = null;
128
- }
129
- };
130
- const forceKillChild = () => {
131
- try {
132
- child.kill(process.platform === "win32" ? "SIGTERM" : "SIGKILL");
133
- } catch {
134
- // Best-effort shutdown fallback.
135
- }
136
- };
137
- const requestChildTermination = () => {
138
- try {
139
- child.kill("SIGTERM");
140
- } catch {
141
- // Best-effort shutdown fallback.
142
- }
143
- signalForceKillTimer = setTimeout(() => {
144
- forceKillChild();
145
- signalHardExitTimer = setTimeout(() => {
146
- process.exit(1);
147
- }, respawnSignalHardExitGraceMs);
148
- signalHardExitTimer.unref?.();
149
- }, respawnSignalForceKillGraceMs);
150
- signalForceKillTimer.unref?.();
151
- };
152
- const scheduleParentExit = () => {
153
- if (signalExitTimer) {
154
- return;
155
- }
156
- signalExitTimer = setTimeout(() => {
157
- requestChildTermination();
158
- }, respawnSignalExitGraceMs);
159
- signalExitTimer.unref?.();
160
- };
161
- for (const signal of respawnSignals) {
162
- const listener = () => {
163
- try {
164
- child.kill(signal);
165
- } catch {
166
- // Best-effort signal forwarding.
167
- }
168
- scheduleParentExit();
169
- };
170
- try {
171
- process.on(signal, listener);
172
- listeners.set(signal, listener);
173
- } catch {
174
- // Unsupported signal on this platform.
175
- }
176
- }
177
- child.once("exit", (code, signal) => {
178
- detach();
179
- if (signal) {
180
- process.exit(1);
181
- }
182
- process.exit(code ?? 1);
183
- });
184
- child.once("error", (error) => {
185
- detach();
186
- process.stderr.write(
187
- `[ps-claw] Failed to respawn launcher: ${
188
- error instanceof Error ? (error.stack ?? error.message) : String(error)
189
- }\n`,
190
- );
191
- process.exit(1);
192
- });
193
- return true;
194
- };
195
-
196
- const respawnWithoutCompileCacheIfNeeded = () => {
197
- if (!isSourceCheckoutLauncher()) {
198
- return false;
199
- }
200
- if (process.env.PS_CLAW_SOURCE_COMPILE_CACHE_RESPAWNED === "1") {
201
- return false;
202
- }
203
- if (!module.getCompileCacheDir?.() && !isNodeCompileCacheRequested()) {
204
- return false;
205
- }
206
- const env = {
207
- ...process.env,
208
- NODE_DISABLE_COMPILE_CACHE: "1",
209
- PS_CLAW_SOURCE_COMPILE_CACHE_RESPAWNED: "1",
210
- };
211
- delete env.NODE_COMPILE_CACHE;
212
- return runRespawnedChild(
213
- process.execPath,
214
- [...process.execArgv, fileURLToPath(import.meta.url), ...process.argv.slice(2)],
215
- env,
216
- );
217
- };
218
-
219
- const respawnWithPackagedCompileCacheIfNeeded = () => {
220
- if (isSourceCheckoutLauncher() || isNodeCompileCacheDisabled()) {
221
- return false;
222
- }
223
- if (process.env.PS_CLAW_PACKAGED_COMPILE_CACHE_RESPAWNED === "1") {
224
- return false;
225
- }
226
- const currentDirectory = module.getCompileCacheDir?.();
227
- if (!currentDirectory) {
228
- return false;
229
- }
230
- const desiredDirectory = resolvePackagedCompileCacheDirectory();
231
- if (path.resolve(currentDirectory) === path.resolve(desiredDirectory)) {
232
- return false;
233
- }
234
- const env = {
235
- ...process.env,
236
- NODE_COMPILE_CACHE: desiredDirectory,
237
- PS_CLAW_PACKAGED_COMPILE_CACHE_RESPAWNED: "1",
238
- };
239
- return runRespawnedChild(
240
- process.execPath,
241
- [...process.execArgv, fileURLToPath(import.meta.url), ...process.argv.slice(2)],
242
- env,
243
- );
244
- };
245
-
246
- const waitingForCompileCacheRespawn =
247
- respawnWithoutCompileCacheIfNeeded() || respawnWithPackagedCompileCacheIfNeeded();
248
-
249
- // https://nodejs.org/api/module.html#module-compile-cache
250
- if (
251
- !waitingForCompileCacheRespawn &&
252
- module.enableCompileCache &&
253
- !isNodeCompileCacheDisabled() &&
254
- !isSourceCheckoutLauncher()
255
- ) {
256
- try {
257
- module.enableCompileCache(resolvePackagedCompileCacheDirectory());
258
- } catch {
259
- // Ignore errors
260
- }
261
- }
262
-
263
- const getErrorMessage = (err) =>
264
- err && typeof err === "object" && "message" in err && typeof err.message === "string"
265
- ? err.message
266
- : "";
267
-
268
- const isModuleNotFoundError = (err) =>
269
- err && typeof err === "object" && "code" in err && err.code === "ERR_MODULE_NOT_FOUND";
270
-
271
- const isDirectModuleNotFoundError = (err, specifier) => {
272
- const message = getErrorMessage(err);
273
- const bunSpecifierMiss =
274
- message.includes(`Cannot find module '${specifier}'`) ||
275
- message.includes(`Cannot find module "${specifier}"`);
276
- const launcherPath = fileURLToPath(import.meta.url);
277
- const bunLauncherImporterMiss =
278
- message.includes(` from '${launcherPath}'`) || message.includes(` from "${launcherPath}"`);
279
-
280
- const expectedUrl = new URL(specifier, import.meta.url);
281
- const expectedPath = fileURLToPath(expectedUrl);
282
- const nodePathMiss =
283
- message.includes(`Cannot find module '${expectedPath}'`) ||
284
- message.includes(`Cannot find module "${expectedPath}"`);
285
-
286
- if (isModuleNotFoundError(err)) {
287
- if (err && typeof err === "object" && "url" in err && err.url === expectedUrl.href) {
288
- return true;
289
- }
290
- return nodePathMiss || (bunSpecifierMiss && bunLauncherImporterMiss);
291
- }
292
-
293
- return bunSpecifierMiss && bunLauncherImporterMiss;
294
- };
295
-
296
- const installProcessWarningFilter = async () => {
297
- // Keep bootstrap warnings consistent with the TypeScript runtime.
298
- for (const specifier of ["./dist/warning-filter.js", "./dist/warning-filter.mjs"]) {
299
- try {
300
- const mod = await import(specifier);
301
- if (typeof mod.installProcessWarningFilter === "function") {
302
- mod.installProcessWarningFilter();
303
- return;
304
- }
305
- } catch (err) {
306
- if (isDirectModuleNotFoundError(err, specifier)) {
307
- continue;
308
- }
309
- throw err;
310
- }
311
- }
312
- };
313
-
314
- const tryImport = async (specifier) => {
315
- try {
316
- await import(specifier);
317
- return true;
318
- } catch (err) {
319
- // Only swallow direct entry misses; rethrow transitive resolution failures.
320
- if (isDirectModuleNotFoundError(err, specifier)) {
321
- return false;
322
- }
323
- throw err;
324
- }
325
- };
326
-
327
- const exists = async (specifier) => {
328
- try {
329
- await access(new URL(specifier, import.meta.url));
330
- return true;
331
- } catch {
332
- return false;
333
- }
334
- };
335
-
336
- const buildMissingEntryErrorMessage = async () => {
337
- const lines = ["ps-claw: missing dist/entry.(m)js (build output)."];
338
- if (!(await exists("./src/entry.ts"))) {
339
- return lines.join("\n");
340
- }
341
-
342
- lines.push("This install looks like an unbuilt source tree or GitHub source archive.");
343
- lines.push(
344
- "Build locally with `pnpm install && pnpm build`, or install a built package instead.",
345
- );
346
- lines.push(
347
- "For pinned GitHub installs, use `npm install -g github:ps-claw/ps-claw#<ref>` instead of a raw `/archive/<ref>.tar.gz` URL.",
348
- );
349
- lines.push("For releases, use `npm install -g ps-claw@latest`.");
350
- return lines.join("\n");
351
- };
352
-
353
- const isBareRootHelpInvocation = (argv) =>
354
- argv.length === 3 && (argv[2] === "--help" || argv[2] === "-h");
355
-
356
- const resolvePrecomputedCommandHelp = (argv) => {
357
- if (argv.length !== 4 || (argv[3] !== "--help" && argv[3] !== "-h")) {
358
- return null;
359
- }
360
- if (argv[2] === "browser") {
361
- return { command: "browser", metadataKey: "browserHelpText" };
362
- }
363
- if (argv[2] === "secrets") {
364
- return { command: "secrets", metadataKey: "secretsHelpText" };
365
- }
366
- if (argv[2] === "nodes") {
367
- return { command: "nodes", metadataKey: "nodesHelpText" };
368
- }
369
- return null;
370
- };
371
-
372
- const isHelpFastPathDisabled = () =>
373
- process.env.PS_CLAW_DISABLE_CLI_STARTUP_HELP_FAST_PATH === "1";
374
-
375
- const normalizeLauncherHomeValue = (value) => {
376
- const trimmed = value?.trim();
377
- return trimmed && trimmed !== "undefined" && trimmed !== "null" ? trimmed : undefined;
378
- };
379
-
380
- const resolveLauncherOsHomeDir = () =>
381
- normalizeLauncherHomeValue(process.env.HOME) ??
382
- normalizeLauncherHomeValue(process.env.USERPROFILE) ??
383
- os.homedir();
384
-
385
- const resolveLauncherHomeDir = () => {
386
- const explicit = normalizeLauncherHomeValue(process.env.PS_CLAW_HOME);
387
- const rawHome =
388
- explicit && (explicit === "~" || explicit.startsWith("~/") || explicit.startsWith("~\\"))
389
- ? explicit.replace(/^~(?=$|[\\/])/, resolveLauncherOsHomeDir())
390
- : (explicit ?? resolveLauncherOsHomeDir());
391
- return path.resolve(rawHome);
392
- };
393
-
394
- const resolveLauncherUserPath = (input) => {
395
- if (input === "~") {
396
- return resolveLauncherHomeDir();
397
- }
398
- if (input.startsWith("~/") || input.startsWith("~\\")) {
399
- return path.join(resolveLauncherHomeDir(), input.slice(2));
400
- }
401
- return path.resolve(input);
402
- };
403
-
404
- const resolveLauncherConfigPaths = () => {
405
- const explicit = process.env.PS_CLAW_CONFIG_PATH?.trim();
406
- if (explicit) {
407
- return [resolveLauncherUserPath(explicit)];
408
- }
409
- const stateOverride = process.env.PS_CLAW_STATE_DIR?.trim();
410
- if (stateOverride) {
411
- const stateDir = resolveLauncherUserPath(stateOverride);
412
- return [path.join(stateDir, "ps-claw.json"), path.join(stateDir, "clawdbot.json")];
413
- }
414
- const homeDir = resolveLauncherHomeDir();
415
- return [
416
- path.join(homeDir, ".ps-claw", "ps-claw.json"),
417
- path.join(homeDir, ".ps-claw", "clawdbot.json"),
418
- path.join(homeDir, ".clawdbot", "ps-claw.json"),
419
- path.join(homeDir, ".clawdbot", "clawdbot.json"),
420
- ];
421
- };
422
-
423
- const shouldDeferRootHelpToRuntimeEntry = () => {
424
- if (
425
- process.env.PS_CLAW_BUNDLED_PLUGINS_DIR?.trim() ||
426
- process.env.PS_CLAW_DISABLE_BUNDLED_PLUGINS?.trim()
427
- ) {
428
- return true;
429
- }
430
- for (const configPath of resolveLauncherConfigPaths()) {
431
- try {
432
- const raw = readFileSync(configPath, "utf8");
433
- return /\bplugins\b|\$include\b/.test(raw);
434
- } catch {
435
- continue;
436
- }
437
- }
438
- return false;
439
- };
440
-
441
- const loadPrecomputedHelpText = (key) => {
442
- try {
443
- const raw = readFileSync(new URL("./dist/cli-startup-metadata.json", import.meta.url), "utf8");
444
- const parsed = JSON.parse(raw);
445
- const value = parsed?.[key];
446
- return typeof value === "string" && value.length > 0 ? value : null;
447
- } catch {
448
- return null;
449
- }
450
- };
451
-
452
- function tryOutputLauncherVersion(argv) {
453
- try {
454
- if (normalizeLauncherMetadataValue(process.env.PS_CLAW_CONTAINER)) {
455
- return false;
456
- }
457
- if (!isLauncherVersionFastPathArgv(argv)) {
458
- return false;
459
- }
460
- const version = resolveLauncherVersion();
461
- const commit = resolveLauncherCommit();
462
- process.stdout.write(commit ? `PS Claw ${version} (${commit})\n` : `PS Claw ${version}\n`);
463
- return true;
464
- } catch {
465
- return false;
466
- }
467
- }
468
-
469
- function isLauncherVersionFastPathArgv(argv) {
470
- return argv.length === 3 && (argv[2] === "--version" || argv[2] === "-V" || argv[2] === "-v");
471
- }
472
-
473
- function normalizeLauncherMetadataValue(value) {
474
- const trimmed = typeof value === "string" ? value.trim() : "";
475
- return trimmed && trimmed !== "undefined" && trimmed !== "null" ? trimmed : undefined;
476
- }
477
-
478
- function readLauncherJson(relativePath) {
479
- try {
480
- return JSON.parse(readFileSync(new URL(relativePath, import.meta.url), "utf8"));
481
- } catch {
482
- return null;
483
- }
484
- }
485
-
486
- function resolveLauncherVersion() {
487
- const packageJson = readLauncherJson("./package.json");
488
- const packageVersion = normalizeLauncherMetadataValue(packageJson?.version);
489
- if (packageVersion) {
490
- return packageVersion;
491
- }
492
- const buildInfo = readLauncherJson("./dist/build-info.json");
493
- const buildVersion = normalizeLauncherMetadataValue(buildInfo?.version);
494
- if (buildVersion) {
495
- return buildVersion;
496
- }
497
- return normalizeLauncherMetadataValue(process.env.PS_CLAW_BUNDLED_VERSION) ?? "0.0.0";
498
- }
499
-
500
- function resolveLauncherCommit() {
501
- const envCommit = formatLauncherCommit(process.env.GIT_COMMIT ?? process.env.GIT_SHA);
502
- if (envCommit) {
503
- return envCommit;
504
- }
505
- return (
506
- readLauncherGitCommit() ??
507
- formatLauncherCommit(readLauncherJson("./dist/build-info.json")?.commit) ??
508
- formatLauncherCommit(readLauncherJson("./package.json")?.gitHead) ??
509
- formatLauncherCommit(readLauncherJson("./package.json")?.githead)
510
- );
511
- }
512
-
513
- function formatLauncherCommit(value) {
514
- if (typeof value !== "string") {
515
- return null;
516
- }
517
- const match = value.trim().match(/[0-9a-fA-F]{7,40}/);
518
- return match ? match[0].slice(0, 7).toLowerCase() : null;
519
- }
520
-
521
- function readLauncherGitCommit() {
522
- try {
523
- const gitPath = fileURLToPath(new URL("./.git", import.meta.url));
524
- const headPath = resolveLauncherGitHeadPath(gitPath);
525
- if (!headPath) {
526
- return null;
527
- }
528
- const head = readFileSync(headPath, "utf8").trim();
529
- if (!head) {
530
- return null;
531
- }
532
- if (!head.startsWith("ref:")) {
533
- return formatLauncherCommit(head);
534
- }
535
- const ref = head.replace(/^ref:\s*/i, "").trim();
536
- if (!ref.startsWith("refs/") || path.isAbsolute(ref) || ref.split("/").includes("..")) {
537
- return null;
538
- }
539
- const refsBase = resolveLauncherGitRefsBase(headPath);
540
- const refPath = path.resolve(refsBase, ref);
541
- const rel = path.relative(refsBase, refPath);
542
- if (!rel || rel.startsWith("..") || path.isAbsolute(rel)) {
543
- return null;
544
- }
545
- try {
546
- return formatLauncherCommit(readFileSync(refPath, "utf8"));
547
- } catch {
548
- return readLauncherPackedRef(refsBase, ref);
549
- }
550
- } catch {
551
- return null;
552
- }
553
- }
554
-
555
- function resolveLauncherGitHeadPath(gitPath) {
556
- try {
557
- if (statSync(gitPath).isDirectory()) {
558
- return path.join(gitPath, "HEAD");
559
- }
560
- const raw = readFileSync(gitPath, "utf8").trim();
561
- if (!raw.startsWith("gitdir:")) {
562
- return null;
563
- }
564
- return path.join(
565
- path.resolve(path.dirname(gitPath), raw.slice("gitdir:".length).trim()),
566
- "HEAD",
567
- );
568
- } catch {
569
- return null;
570
- }
571
- }
572
-
573
- function resolveLauncherGitRefsBase(headPath) {
574
- const gitDir = path.dirname(headPath);
575
- try {
576
- const commonDir = readFileSync(path.join(gitDir, "commondir"), "utf8").trim();
577
- return commonDir ? path.resolve(gitDir, commonDir) : gitDir;
578
- } catch {
579
- return gitDir;
580
- }
581
- }
582
-
583
- function readLauncherPackedRef(refsBase, ref) {
584
- try {
585
- const packedRefs = readFileSync(path.join(refsBase, "packed-refs"), "utf8");
586
- for (const line of packedRefs.split("\n")) {
587
- if (!line || line.startsWith("#") || line.startsWith("^")) {
588
- continue;
589
- }
590
- const [commit, packedRef] = line.trim().split(/\s+/, 2);
591
- if (packedRef === ref) {
592
- return formatLauncherCommit(commit);
593
- }
594
- }
595
- } catch {
596
- // fall through
597
- }
598
- return null;
599
- }
600
-
601
- const tryOutputBareRootHelp = async () => {
602
- if (!isBareRootHelpInvocation(process.argv)) {
603
- return false;
604
- }
605
- if (shouldDeferRootHelpToRuntimeEntry()) {
606
- return false;
607
- }
608
- const precomputed = loadPrecomputedHelpText("rootHelpText");
609
- if (precomputed) {
610
- process.stdout.write(precomputed);
611
- return true;
612
- }
613
- for (const specifier of ["./dist/cli/program/root-help.js", "./dist/cli/program/root-help.mjs"]) {
614
- try {
615
- const mod = await import(specifier);
616
- if (typeof mod.outputRootHelp === "function") {
617
- await mod.outputRootHelp();
618
- return true;
619
- }
620
- } catch (err) {
621
- if (isDirectModuleNotFoundError(err, specifier)) {
622
- continue;
623
- }
624
- throw err;
625
- }
626
- }
627
- return false;
628
- };
629
-
630
- const tryOutputPrecomputedCommandHelp = () => {
631
- const commandHelp = resolvePrecomputedCommandHelp(process.argv);
632
- if (!commandHelp) {
633
- return false;
634
- }
635
- if (commandHelp.command === "nodes" && shouldDeferRootHelpToRuntimeEntry()) {
636
- return false;
637
- }
638
- const precomputed = loadPrecomputedHelpText(commandHelp.metadataKey);
639
- if (!precomputed) {
640
- return false;
641
- }
642
- process.stdout.write(precomputed);
643
- return true;
644
- };
645
-
646
- if (!waitingForCompileCacheRespawn) {
647
- if (!isHelpFastPathDisabled() && (await tryOutputBareRootHelp())) {
648
- // OK
649
- } else if (!isHelpFastPathDisabled() && tryOutputPrecomputedCommandHelp()) {
650
- // OK
651
- } else {
652
- await installProcessWarningFilter();
653
- if (await tryImport("./dist/entry.js")) {
654
- // OK
655
- } else if (await tryImport("./dist/entry.mjs")) {
656
- // OK
657
- } else {
658
- throw new Error(await buildMissingEntryErrorMessage());
659
- }
660
- }
661
- }