@chrysb/alphaclaw 0.8.8 → 0.8.10
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/bin/alphaclaw.js +43 -174
- package/lib/public/css/tailwind.generated.css +1 -1
- package/lib/public/dist/app.bundle.js +2089 -2109
- package/lib/public/js/app.js +0 -3
- package/lib/public/js/components/gateway.js +3 -6
- package/lib/public/js/components/general/index.js +0 -2
- package/lib/public/js/components/onboarding/welcome-form-step.js +4 -29
- package/lib/public/js/components/routes/general-route.js +0 -2
- package/lib/public/js/components/routes/watchdog-route.js +0 -2
- package/lib/public/js/components/sidebar.js +7 -20
- package/lib/public/js/components/update-modal.js +1 -2
- package/lib/public/js/components/watchdog-tab/index.js +0 -2
- package/lib/public/js/components/welcome/index.js +0 -1
- package/lib/public/js/components/welcome/use-welcome.js +2 -52
- package/lib/public/js/hooks/use-app-shell-controller.js +9 -37
- package/lib/public/js/lib/api.js +0 -36
- package/lib/server/alphaclaw-version.js +128 -37
- package/lib/server/gateway.js +14 -32
- package/lib/server/init/register-server-routes.js +1 -7
- package/lib/server/openclaw-version.js +136 -76
- package/lib/server/routes/pages.js +1 -9
- package/lib/server/routes/system.js +1 -6
- package/lib/server/usage-tracker-config.js +3 -27
- package/package.json +1 -2
- package/lib/public/js/components/update-modal-helpers.js +0 -12
- package/lib/release/managed-release.js +0 -180
- package/lib/server/alphaclaw-runtime.js +0 -294
- package/lib/server/openclaw-runtime.js +0 -428
- package/lib/server/package-fingerprint.js +0 -274
- package/lib/server/pending-alphaclaw-update.js +0 -85
- package/lib/server/pending-openclaw-update.js +0 -86
|
@@ -1,428 +0,0 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const os = require("os");
|
|
3
|
-
const path = require("path");
|
|
4
|
-
|
|
5
|
-
const { kRootDir } = require("./constants");
|
|
6
|
-
const {
|
|
7
|
-
compareVersionParts,
|
|
8
|
-
normalizeOpenclawVersion,
|
|
9
|
-
} = require("./helpers");
|
|
10
|
-
const {
|
|
11
|
-
computePackageFingerprint,
|
|
12
|
-
isPackageRootSymlink,
|
|
13
|
-
packLocalPackageForInstall,
|
|
14
|
-
resolvePackageRootFromEntryPath,
|
|
15
|
-
} = require("./package-fingerprint");
|
|
16
|
-
|
|
17
|
-
const getManagedOpenclawRuntimeDir = ({ rootDir = kRootDir } = {}) =>
|
|
18
|
-
path.join(rootDir, ".openclaw-runtime");
|
|
19
|
-
|
|
20
|
-
const getBundledOpenclawPackageRoot = ({
|
|
21
|
-
fsModule = fs,
|
|
22
|
-
resolveImpl = require.resolve,
|
|
23
|
-
} = {}) =>
|
|
24
|
-
resolvePackageRootFromEntryPath({
|
|
25
|
-
fsModule,
|
|
26
|
-
entryPath: resolveImpl("openclaw"),
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
const getManagedOpenclawPackageRoot = ({ runtimeDir } = {}) =>
|
|
30
|
-
path.join(
|
|
31
|
-
runtimeDir || getManagedOpenclawRuntimeDir(),
|
|
32
|
-
"node_modules",
|
|
33
|
-
"openclaw",
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
const getManagedOpenclawBinDir = ({ runtimeDir } = {}) =>
|
|
37
|
-
path.join(
|
|
38
|
-
runtimeDir || getManagedOpenclawRuntimeDir(),
|
|
39
|
-
"node_modules",
|
|
40
|
-
".bin",
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
const getManagedOpenclawBinPath = ({ runtimeDir } = {}) =>
|
|
44
|
-
path.join(getManagedOpenclawBinDir({ runtimeDir }), "openclaw");
|
|
45
|
-
|
|
46
|
-
const getManagedOpenclawPackageJsonPath = ({ runtimeDir } = {}) =>
|
|
47
|
-
path.join(
|
|
48
|
-
getManagedOpenclawPackageRoot({ runtimeDir }),
|
|
49
|
-
"package.json",
|
|
50
|
-
);
|
|
51
|
-
|
|
52
|
-
const ensureManagedOpenclawRuntimeProject = ({
|
|
53
|
-
fsModule = fs,
|
|
54
|
-
runtimeDir,
|
|
55
|
-
} = {}) => {
|
|
56
|
-
const resolvedRuntimeDir = runtimeDir || getManagedOpenclawRuntimeDir();
|
|
57
|
-
const packageJsonPath = path.join(resolvedRuntimeDir, "package.json");
|
|
58
|
-
fsModule.mkdirSync(resolvedRuntimeDir, { recursive: true });
|
|
59
|
-
if (!fsModule.existsSync(packageJsonPath)) {
|
|
60
|
-
fsModule.writeFileSync(
|
|
61
|
-
packageJsonPath,
|
|
62
|
-
JSON.stringify(
|
|
63
|
-
{
|
|
64
|
-
name: "alphaclaw-openclaw-runtime",
|
|
65
|
-
private: true,
|
|
66
|
-
},
|
|
67
|
-
null,
|
|
68
|
-
2,
|
|
69
|
-
),
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
return {
|
|
73
|
-
runtimeDir: resolvedRuntimeDir,
|
|
74
|
-
packageJsonPath,
|
|
75
|
-
};
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
const readManagedOpenclawRuntimeVersion = ({
|
|
79
|
-
fsModule = fs,
|
|
80
|
-
runtimeDir,
|
|
81
|
-
} = {}) => {
|
|
82
|
-
try {
|
|
83
|
-
const pkg = JSON.parse(
|
|
84
|
-
fsModule.readFileSync(
|
|
85
|
-
getManagedOpenclawPackageJsonPath({ runtimeDir }),
|
|
86
|
-
"utf8",
|
|
87
|
-
),
|
|
88
|
-
);
|
|
89
|
-
return normalizeOpenclawVersion(pkg?.version || "");
|
|
90
|
-
} catch {
|
|
91
|
-
return null;
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const readBundledOpenclawVersion = ({
|
|
96
|
-
fsModule = fs,
|
|
97
|
-
resolveImpl = require.resolve,
|
|
98
|
-
} = {}) => {
|
|
99
|
-
try {
|
|
100
|
-
const packageRoot = getBundledOpenclawPackageRoot({
|
|
101
|
-
fsModule,
|
|
102
|
-
resolveImpl,
|
|
103
|
-
});
|
|
104
|
-
if (!packageRoot) return null;
|
|
105
|
-
const pkg = JSON.parse(
|
|
106
|
-
fsModule.readFileSync(path.join(packageRoot, "package.json"), "utf8"),
|
|
107
|
-
);
|
|
108
|
-
return normalizeOpenclawVersion(pkg?.version || "");
|
|
109
|
-
} catch {
|
|
110
|
-
return null;
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
const shellQuote = (value) =>
|
|
115
|
-
`'${String(value || "").replace(/'/g, `'\"'\"'`)}'`;
|
|
116
|
-
|
|
117
|
-
const applyManagedOpenclawPatch = ({
|
|
118
|
-
execSyncImpl,
|
|
119
|
-
fsModule = fs,
|
|
120
|
-
logger = console,
|
|
121
|
-
runtimeDir,
|
|
122
|
-
version,
|
|
123
|
-
alphaclawRoot = path.resolve(__dirname, "..", ".."),
|
|
124
|
-
} = {}) => {
|
|
125
|
-
const normalizedVersion = normalizeOpenclawVersion(version);
|
|
126
|
-
if (!normalizedVersion) return false;
|
|
127
|
-
const patchesDir = path.join(alphaclawRoot, "patches");
|
|
128
|
-
const patchFileName = `openclaw+${normalizedVersion}.patch`;
|
|
129
|
-
const patchFilePath = path.join(patchesDir, patchFileName);
|
|
130
|
-
if (!fsModule.existsSync(patchFilePath)) {
|
|
131
|
-
return false;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const runtimePatchDirName = ".alphaclaw-patches";
|
|
135
|
-
const runtimePatchDirPath = path.join(runtimeDir, runtimePatchDirName);
|
|
136
|
-
try {
|
|
137
|
-
if (fsModule.existsSync(runtimePatchDirPath)) {
|
|
138
|
-
fsModule.rmSync(runtimePatchDirPath, { recursive: true, force: true });
|
|
139
|
-
}
|
|
140
|
-
} catch {}
|
|
141
|
-
fsModule.symlinkSync(patchesDir, runtimePatchDirPath);
|
|
142
|
-
|
|
143
|
-
const patchPackageMain = require.resolve("patch-package/dist/index.js", {
|
|
144
|
-
paths: [alphaclawRoot],
|
|
145
|
-
});
|
|
146
|
-
logger.log(
|
|
147
|
-
`[alphaclaw] Applying bundled OpenClaw patch for ${normalizedVersion}...`,
|
|
148
|
-
);
|
|
149
|
-
execSyncImpl(
|
|
150
|
-
`${shellQuote(process.execPath)} ${shellQuote(patchPackageMain)} --patch-dir ${shellQuote(runtimePatchDirName)}`,
|
|
151
|
-
{
|
|
152
|
-
cwd: runtimeDir,
|
|
153
|
-
stdio: "inherit",
|
|
154
|
-
timeout: 120000,
|
|
155
|
-
},
|
|
156
|
-
);
|
|
157
|
-
return true;
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
const kDisableBundledPluginPostinstallEnv =
|
|
161
|
-
"OPENCLAW_DISABLE_BUNDLED_PLUGIN_POSTINSTALL";
|
|
162
|
-
const kBundledPluginPostinstallFailureMarker =
|
|
163
|
-
"[postinstall] could not install bundled plugin deps:";
|
|
164
|
-
|
|
165
|
-
const runManagedOpenclawBundledPluginPostinstall = ({
|
|
166
|
-
execSyncImpl,
|
|
167
|
-
fsModule = fs,
|
|
168
|
-
logger = console,
|
|
169
|
-
runtimeDir,
|
|
170
|
-
} = {}) => {
|
|
171
|
-
const packageRoot = getManagedOpenclawPackageRoot({ runtimeDir });
|
|
172
|
-
const postinstallScriptPath = path.join(
|
|
173
|
-
packageRoot,
|
|
174
|
-
"scripts",
|
|
175
|
-
"postinstall-bundled-plugins.mjs",
|
|
176
|
-
);
|
|
177
|
-
if (!fsModule.existsSync(postinstallScriptPath)) {
|
|
178
|
-
return false;
|
|
179
|
-
}
|
|
180
|
-
const env = { ...process.env };
|
|
181
|
-
delete env[kDisableBundledPluginPostinstallEnv];
|
|
182
|
-
const logDir = fsModule.mkdtempSync(
|
|
183
|
-
path.join(os.tmpdir(), "openclaw-bundled-postinstall-"),
|
|
184
|
-
);
|
|
185
|
-
const logPath = path.join(logDir, "postinstall.log");
|
|
186
|
-
let commandError = null;
|
|
187
|
-
let output = "";
|
|
188
|
-
let stdoutFd;
|
|
189
|
-
let stderrFd;
|
|
190
|
-
try {
|
|
191
|
-
stdoutFd = fsModule.openSync(logPath, "a");
|
|
192
|
-
stderrFd = fsModule.openSync(logPath, "a");
|
|
193
|
-
try {
|
|
194
|
-
execSyncImpl(
|
|
195
|
-
`${shellQuote(process.execPath)} ${shellQuote(postinstallScriptPath)}`,
|
|
196
|
-
{
|
|
197
|
-
cwd: packageRoot,
|
|
198
|
-
env,
|
|
199
|
-
stdio: ["ignore", stdoutFd, stderrFd],
|
|
200
|
-
timeout: 180000,
|
|
201
|
-
},
|
|
202
|
-
);
|
|
203
|
-
} catch (error) {
|
|
204
|
-
commandError = error;
|
|
205
|
-
}
|
|
206
|
-
} finally {
|
|
207
|
-
if (typeof stdoutFd === "number") {
|
|
208
|
-
try {
|
|
209
|
-
fsModule.closeSync(stdoutFd);
|
|
210
|
-
} catch {}
|
|
211
|
-
}
|
|
212
|
-
if (typeof stderrFd === "number") {
|
|
213
|
-
try {
|
|
214
|
-
fsModule.closeSync(stderrFd);
|
|
215
|
-
} catch {}
|
|
216
|
-
}
|
|
217
|
-
try {
|
|
218
|
-
output = String(fsModule.readFileSync(logPath, "utf8") || "").trim();
|
|
219
|
-
} catch {
|
|
220
|
-
output = "";
|
|
221
|
-
}
|
|
222
|
-
try {
|
|
223
|
-
fsModule.rmSync(logDir, { recursive: true, force: true });
|
|
224
|
-
} catch {}
|
|
225
|
-
}
|
|
226
|
-
if (output) {
|
|
227
|
-
logger.log(output);
|
|
228
|
-
}
|
|
229
|
-
if (output.includes(kBundledPluginPostinstallFailureMarker)) {
|
|
230
|
-
throw new Error(output);
|
|
231
|
-
}
|
|
232
|
-
if (commandError) {
|
|
233
|
-
throw commandError;
|
|
234
|
-
}
|
|
235
|
-
return true;
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
const installManagedOpenclawRuntime = ({
|
|
239
|
-
execSyncImpl,
|
|
240
|
-
fsModule = fs,
|
|
241
|
-
logger = console,
|
|
242
|
-
runtimeDir,
|
|
243
|
-
spec,
|
|
244
|
-
sourcePath,
|
|
245
|
-
alphaclawRoot,
|
|
246
|
-
} = {}) => {
|
|
247
|
-
const normalizedSourcePath = String(sourcePath || "").trim();
|
|
248
|
-
const normalizedSpec = normalizedSourcePath
|
|
249
|
-
? normalizedSourcePath
|
|
250
|
-
: String(spec || "").trim() || "openclaw@latest";
|
|
251
|
-
ensureManagedOpenclawRuntimeProject({
|
|
252
|
-
fsModule,
|
|
253
|
-
runtimeDir,
|
|
254
|
-
});
|
|
255
|
-
let packedSource = null;
|
|
256
|
-
try {
|
|
257
|
-
const installTarget = normalizedSourcePath
|
|
258
|
-
? (() => {
|
|
259
|
-
packedSource = packLocalPackageForInstall({
|
|
260
|
-
execSyncImpl,
|
|
261
|
-
fsModule,
|
|
262
|
-
packageRoot: normalizedSourcePath,
|
|
263
|
-
tempDirPrefix: "openclaw-runtime-pack-",
|
|
264
|
-
});
|
|
265
|
-
return packedSource.tarballPath;
|
|
266
|
-
})()
|
|
267
|
-
: normalizedSpec;
|
|
268
|
-
execSyncImpl(
|
|
269
|
-
`npm install ${shellQuote(installTarget)} --omit=dev --no-save --save=false --package-lock=false --prefer-online`,
|
|
270
|
-
{
|
|
271
|
-
cwd: runtimeDir,
|
|
272
|
-
env: {
|
|
273
|
-
...process.env,
|
|
274
|
-
[kDisableBundledPluginPostinstallEnv]: "1",
|
|
275
|
-
},
|
|
276
|
-
stdio: "inherit",
|
|
277
|
-
timeout: 180000,
|
|
278
|
-
},
|
|
279
|
-
);
|
|
280
|
-
} finally {
|
|
281
|
-
packedSource?.cleanup?.();
|
|
282
|
-
}
|
|
283
|
-
runManagedOpenclawBundledPluginPostinstall({
|
|
284
|
-
execSyncImpl,
|
|
285
|
-
fsModule,
|
|
286
|
-
logger,
|
|
287
|
-
runtimeDir,
|
|
288
|
-
});
|
|
289
|
-
const installedVersion = readManagedOpenclawRuntimeVersion({
|
|
290
|
-
fsModule,
|
|
291
|
-
runtimeDir,
|
|
292
|
-
});
|
|
293
|
-
applyManagedOpenclawPatch({
|
|
294
|
-
execSyncImpl,
|
|
295
|
-
fsModule,
|
|
296
|
-
logger,
|
|
297
|
-
runtimeDir,
|
|
298
|
-
version: installedVersion,
|
|
299
|
-
alphaclawRoot,
|
|
300
|
-
});
|
|
301
|
-
return {
|
|
302
|
-
spec: normalizedSpec,
|
|
303
|
-
version: installedVersion,
|
|
304
|
-
};
|
|
305
|
-
};
|
|
306
|
-
|
|
307
|
-
const syncManagedOpenclawRuntimeWithBundled = ({
|
|
308
|
-
execSyncImpl,
|
|
309
|
-
fsModule = fs,
|
|
310
|
-
logger = console,
|
|
311
|
-
runtimeDir,
|
|
312
|
-
resolveImpl,
|
|
313
|
-
alphaclawRoot,
|
|
314
|
-
} = {}) => {
|
|
315
|
-
const bundledPackageRoot = getBundledOpenclawPackageRoot({
|
|
316
|
-
fsModule,
|
|
317
|
-
resolveImpl,
|
|
318
|
-
});
|
|
319
|
-
const bundledVersion = readBundledOpenclawVersion({
|
|
320
|
-
fsModule,
|
|
321
|
-
resolveImpl,
|
|
322
|
-
});
|
|
323
|
-
if (!bundledVersion) {
|
|
324
|
-
return {
|
|
325
|
-
checked: false,
|
|
326
|
-
synced: false,
|
|
327
|
-
bundledVersion: null,
|
|
328
|
-
runtimeVersion: readManagedOpenclawRuntimeVersion({ fsModule, runtimeDir }),
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
const runtimeVersion = readManagedOpenclawRuntimeVersion({
|
|
333
|
-
fsModule,
|
|
334
|
-
runtimeDir,
|
|
335
|
-
});
|
|
336
|
-
const runtimePackageRoot = getManagedOpenclawPackageRoot({ runtimeDir });
|
|
337
|
-
const runtimePackageRootIsSymlink = isPackageRootSymlink({
|
|
338
|
-
fsModule,
|
|
339
|
-
packageRoot: runtimePackageRoot,
|
|
340
|
-
});
|
|
341
|
-
const bundledFingerprint = computePackageFingerprint({
|
|
342
|
-
fsModule,
|
|
343
|
-
packageRoot: bundledPackageRoot,
|
|
344
|
-
});
|
|
345
|
-
const runtimeFingerprint = computePackageFingerprint({
|
|
346
|
-
fsModule,
|
|
347
|
-
packageRoot: runtimePackageRoot,
|
|
348
|
-
packageJsonPath: getManagedOpenclawPackageJsonPath({ runtimeDir }),
|
|
349
|
-
});
|
|
350
|
-
if (runtimeVersion && compareVersionParts(runtimeVersion, bundledVersion) >= 0) {
|
|
351
|
-
if (
|
|
352
|
-
compareVersionParts(runtimeVersion, bundledVersion) > 0 ||
|
|
353
|
-
(!runtimePackageRootIsSymlink &&
|
|
354
|
-
(!bundledFingerprint || runtimeFingerprint === bundledFingerprint))
|
|
355
|
-
) {
|
|
356
|
-
return {
|
|
357
|
-
checked: true,
|
|
358
|
-
synced: false,
|
|
359
|
-
bundledVersion,
|
|
360
|
-
runtimeVersion,
|
|
361
|
-
};
|
|
362
|
-
}
|
|
363
|
-
logger.log(
|
|
364
|
-
runtimePackageRootIsSymlink
|
|
365
|
-
? `[alphaclaw] Managed OpenClaw runtime ${runtimeVersion} is symlinked to the bundled package; refreshing runtime...`
|
|
366
|
-
: `[alphaclaw] Managed OpenClaw runtime ${runtimeVersion} differs from bundled ${bundledVersion}; refreshing runtime...`,
|
|
367
|
-
);
|
|
368
|
-
} else {
|
|
369
|
-
logger.log(
|
|
370
|
-
runtimeVersion
|
|
371
|
-
? `[alphaclaw] Managed OpenClaw runtime ${runtimeVersion} is older than bundled ${bundledVersion}; syncing runtime...`
|
|
372
|
-
: `[alphaclaw] Managed OpenClaw runtime missing; installing bundled OpenClaw ${bundledVersion}...`,
|
|
373
|
-
);
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
const installResult = installManagedOpenclawRuntime({
|
|
377
|
-
execSyncImpl,
|
|
378
|
-
fsModule,
|
|
379
|
-
logger,
|
|
380
|
-
runtimeDir,
|
|
381
|
-
sourcePath: bundledPackageRoot,
|
|
382
|
-
alphaclawRoot,
|
|
383
|
-
});
|
|
384
|
-
return {
|
|
385
|
-
checked: true,
|
|
386
|
-
synced: true,
|
|
387
|
-
bundledVersion,
|
|
388
|
-
runtimeVersion: installResult.version || bundledVersion,
|
|
389
|
-
};
|
|
390
|
-
};
|
|
391
|
-
|
|
392
|
-
const prependManagedOpenclawBinToPath = ({
|
|
393
|
-
env = process.env,
|
|
394
|
-
fsModule = fs,
|
|
395
|
-
logger = console,
|
|
396
|
-
runtimeDir,
|
|
397
|
-
} = {}) => {
|
|
398
|
-
const resolvedRuntimeDir = runtimeDir || getManagedOpenclawRuntimeDir();
|
|
399
|
-
const binDir = getManagedOpenclawBinDir({ runtimeDir: resolvedRuntimeDir });
|
|
400
|
-
const binPath = getManagedOpenclawBinPath({ runtimeDir: resolvedRuntimeDir });
|
|
401
|
-
if (!fsModule.existsSync(binPath)) {
|
|
402
|
-
return false;
|
|
403
|
-
}
|
|
404
|
-
const currentEntries = String(env.PATH || "")
|
|
405
|
-
.split(path.delimiter)
|
|
406
|
-
.filter(Boolean);
|
|
407
|
-
const nextEntries = [binDir, ...currentEntries.filter((entry) => entry !== binDir)];
|
|
408
|
-
env.PATH = nextEntries.join(path.delimiter);
|
|
409
|
-
logger.log(`[alphaclaw] Using managed OpenClaw runtime from ${resolvedRuntimeDir}`);
|
|
410
|
-
return true;
|
|
411
|
-
};
|
|
412
|
-
|
|
413
|
-
module.exports = {
|
|
414
|
-
applyManagedOpenclawPatch,
|
|
415
|
-
ensureManagedOpenclawRuntimeProject,
|
|
416
|
-
getBundledOpenclawPackageRoot,
|
|
417
|
-
getManagedOpenclawBinDir,
|
|
418
|
-
getManagedOpenclawBinPath,
|
|
419
|
-
getManagedOpenclawPackageRoot,
|
|
420
|
-
getManagedOpenclawPackageJsonPath,
|
|
421
|
-
getManagedOpenclawRuntimeDir,
|
|
422
|
-
installManagedOpenclawRuntime,
|
|
423
|
-
prependManagedOpenclawBinToPath,
|
|
424
|
-
readBundledOpenclawVersion,
|
|
425
|
-
readManagedOpenclawRuntimeVersion,
|
|
426
|
-
runManagedOpenclawBundledPluginPostinstall,
|
|
427
|
-
syncManagedOpenclawRuntimeWithBundled,
|
|
428
|
-
};
|
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
const crypto = require("crypto");
|
|
2
|
-
const fs = require("fs");
|
|
3
|
-
const os = require("os");
|
|
4
|
-
const path = require("path");
|
|
5
|
-
|
|
6
|
-
const kIgnoredDirectoryNames = new Set([".git", "node_modules"]);
|
|
7
|
-
|
|
8
|
-
const normalizeRelativePath = (packageRoot, absolutePath) =>
|
|
9
|
-
path.relative(packageRoot, absolutePath).split(path.sep).join("/");
|
|
10
|
-
|
|
11
|
-
const addIncludedPath = ({ includeSet, value }) => {
|
|
12
|
-
const normalizedValue = String(value || "").trim();
|
|
13
|
-
if (!normalizedValue) return;
|
|
14
|
-
includeSet.add(normalizedValue.replace(/\/+$/, ""));
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const collectIncludedPaths = ({ packageJson = {} } = {}) => {
|
|
18
|
-
const includeSet = new Set(["package.json"]);
|
|
19
|
-
|
|
20
|
-
if (Array.isArray(packageJson.files)) {
|
|
21
|
-
for (const entry of packageJson.files) {
|
|
22
|
-
addIncludedPath({ includeSet, value: entry });
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (typeof packageJson.bin === "string") {
|
|
27
|
-
addIncludedPath({ includeSet, value: packageJson.bin });
|
|
28
|
-
} else if (packageJson.bin && typeof packageJson.bin === "object") {
|
|
29
|
-
for (const entry of Object.values(packageJson.bin)) {
|
|
30
|
-
addIncludedPath({ includeSet, value: entry });
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return Array.from(includeSet).sort((left, right) => left.localeCompare(right));
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const walkIncludedFiles = ({
|
|
38
|
-
fsModule = fs,
|
|
39
|
-
packageRoot,
|
|
40
|
-
absolutePath,
|
|
41
|
-
files,
|
|
42
|
-
}) => {
|
|
43
|
-
if (!fsModule.existsSync(absolutePath)) return;
|
|
44
|
-
const relativePath = normalizeRelativePath(packageRoot, absolutePath);
|
|
45
|
-
if (!relativePath || relativePath.startsWith("..")) return;
|
|
46
|
-
|
|
47
|
-
const stat = fsModule.lstatSync(absolutePath);
|
|
48
|
-
if (stat.isSymbolicLink()) {
|
|
49
|
-
files.push({
|
|
50
|
-
relativePath,
|
|
51
|
-
hash: `symlink:${fsModule.readlinkSync(absolutePath)}`,
|
|
52
|
-
});
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
if (stat.isFile()) {
|
|
56
|
-
files.push({
|
|
57
|
-
relativePath,
|
|
58
|
-
hash: crypto
|
|
59
|
-
.createHash("sha256")
|
|
60
|
-
.update(fsModule.readFileSync(absolutePath))
|
|
61
|
-
.digest("hex"),
|
|
62
|
-
});
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
if (!stat.isDirectory()) return;
|
|
66
|
-
|
|
67
|
-
const entries = fsModule
|
|
68
|
-
.readdirSync(absolutePath, { withFileTypes: true })
|
|
69
|
-
.sort((left, right) => left.name.localeCompare(right.name));
|
|
70
|
-
|
|
71
|
-
for (const entry of entries) {
|
|
72
|
-
if (entry.isDirectory() && kIgnoredDirectoryNames.has(entry.name)) continue;
|
|
73
|
-
walkIncludedFiles({
|
|
74
|
-
fsModule,
|
|
75
|
-
packageRoot,
|
|
76
|
-
absolutePath: path.join(absolutePath, entry.name),
|
|
77
|
-
files,
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
const computePackageFingerprint = ({
|
|
83
|
-
fsModule = fs,
|
|
84
|
-
packageRoot,
|
|
85
|
-
packageJsonPath = path.join(packageRoot, "package.json"),
|
|
86
|
-
} = {}) => {
|
|
87
|
-
const resolvedPackageRoot = path.resolve(String(packageRoot || ""));
|
|
88
|
-
if (!resolvedPackageRoot || !fsModule.existsSync(packageJsonPath)) return null;
|
|
89
|
-
|
|
90
|
-
let packageJson;
|
|
91
|
-
try {
|
|
92
|
-
packageJson = JSON.parse(fsModule.readFileSync(packageJsonPath, "utf8"));
|
|
93
|
-
} catch {
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const files = [];
|
|
98
|
-
for (const includePath of collectIncludedPaths({ packageJson })) {
|
|
99
|
-
walkIncludedFiles({
|
|
100
|
-
fsModule,
|
|
101
|
-
packageRoot: resolvedPackageRoot,
|
|
102
|
-
absolutePath: path.resolve(resolvedPackageRoot, includePath),
|
|
103
|
-
files,
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const hash = crypto.createHash("sha256");
|
|
108
|
-
hash.update("package-fingerprint-v1");
|
|
109
|
-
for (const entry of files.sort((left, right) => left.relativePath.localeCompare(right.relativePath))) {
|
|
110
|
-
hash.update(entry.relativePath);
|
|
111
|
-
hash.update("\0");
|
|
112
|
-
hash.update(entry.hash);
|
|
113
|
-
hash.update("\0");
|
|
114
|
-
}
|
|
115
|
-
return hash.digest("hex");
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
const isPackageRootSymlink = ({
|
|
119
|
-
fsModule = fs,
|
|
120
|
-
packageRoot,
|
|
121
|
-
} = {}) => {
|
|
122
|
-
const resolvedPackageRoot = path.resolve(String(packageRoot || ""));
|
|
123
|
-
if (!resolvedPackageRoot || !fsModule.existsSync(resolvedPackageRoot)) return false;
|
|
124
|
-
try {
|
|
125
|
-
return fsModule.lstatSync(resolvedPackageRoot).isSymbolicLink();
|
|
126
|
-
} catch {
|
|
127
|
-
return false;
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
const resolvePackageRootFromEntryPath = ({
|
|
132
|
-
fsModule = fs,
|
|
133
|
-
entryPath,
|
|
134
|
-
} = {}) => {
|
|
135
|
-
let cursor = path.dirname(path.resolve(String(entryPath || "")));
|
|
136
|
-
while (cursor && cursor !== path.dirname(cursor)) {
|
|
137
|
-
if (fsModule.existsSync(path.join(cursor, "package.json"))) {
|
|
138
|
-
return cursor;
|
|
139
|
-
}
|
|
140
|
-
cursor = path.dirname(cursor);
|
|
141
|
-
}
|
|
142
|
-
return null;
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
const resolveInstallRootFromPackageRoot = ({ packageRoot } = {}) => {
|
|
146
|
-
const resolvedPackageRoot = path.resolve(String(packageRoot || ""));
|
|
147
|
-
if (!resolvedPackageRoot) return "";
|
|
148
|
-
const nodeModulesSegment = `${path.sep}node_modules${path.sep}`;
|
|
149
|
-
const nodeModulesIndex = resolvedPackageRoot.lastIndexOf(nodeModulesSegment);
|
|
150
|
-
if (nodeModulesIndex < 0) {
|
|
151
|
-
return resolvedPackageRoot;
|
|
152
|
-
}
|
|
153
|
-
return resolvedPackageRoot.slice(0, nodeModulesIndex);
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
const seedRuntimeFromBundledInstall = ({
|
|
157
|
-
fsModule = fs,
|
|
158
|
-
packageRoot,
|
|
159
|
-
runtimeDir,
|
|
160
|
-
runtimePackageJson,
|
|
161
|
-
} = {}) => {
|
|
162
|
-
const installRoot = resolveInstallRootFromPackageRoot({ packageRoot });
|
|
163
|
-
const bundledNodeModulesPath = path.join(installRoot, "node_modules");
|
|
164
|
-
if (!installRoot || !fsModule.existsSync(bundledNodeModulesPath)) {
|
|
165
|
-
return {
|
|
166
|
-
seeded: false,
|
|
167
|
-
installRoot,
|
|
168
|
-
bundledNodeModulesPath,
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const resolvedRuntimeDir = path.resolve(String(runtimeDir || ""));
|
|
173
|
-
const runtimeParentDir = path.dirname(resolvedRuntimeDir);
|
|
174
|
-
fsModule.mkdirSync(runtimeParentDir, { recursive: true });
|
|
175
|
-
const tempRuntimeDir = fsModule.mkdtempSync(
|
|
176
|
-
path.join(runtimeParentDir, `${path.basename(resolvedRuntimeDir)}-seed-`),
|
|
177
|
-
);
|
|
178
|
-
let seeded = false;
|
|
179
|
-
try {
|
|
180
|
-
if (runtimePackageJson) {
|
|
181
|
-
fsModule.writeFileSync(
|
|
182
|
-
path.join(tempRuntimeDir, "package.json"),
|
|
183
|
-
JSON.stringify(runtimePackageJson, null, 2),
|
|
184
|
-
);
|
|
185
|
-
}
|
|
186
|
-
fsModule.cpSync(
|
|
187
|
-
bundledNodeModulesPath,
|
|
188
|
-
path.join(tempRuntimeDir, "node_modules"),
|
|
189
|
-
{
|
|
190
|
-
recursive: true,
|
|
191
|
-
dereference: true,
|
|
192
|
-
preserveTimestamps: true,
|
|
193
|
-
},
|
|
194
|
-
);
|
|
195
|
-
try {
|
|
196
|
-
fsModule.rmSync(resolvedRuntimeDir, { recursive: true, force: true });
|
|
197
|
-
} catch {}
|
|
198
|
-
fsModule.renameSync(tempRuntimeDir, resolvedRuntimeDir);
|
|
199
|
-
seeded = true;
|
|
200
|
-
return {
|
|
201
|
-
seeded: true,
|
|
202
|
-
installRoot,
|
|
203
|
-
bundledNodeModulesPath,
|
|
204
|
-
runtimeDir: resolvedRuntimeDir,
|
|
205
|
-
};
|
|
206
|
-
} finally {
|
|
207
|
-
if (!seeded) {
|
|
208
|
-
try {
|
|
209
|
-
fsModule.rmSync(tempRuntimeDir, { recursive: true, force: true });
|
|
210
|
-
} catch {}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
const packLocalPackageForInstall = ({
|
|
216
|
-
execSyncImpl,
|
|
217
|
-
fsModule = fs,
|
|
218
|
-
packageRoot,
|
|
219
|
-
tempDirPrefix = "alphaclaw-package-pack-",
|
|
220
|
-
} = {}) => {
|
|
221
|
-
const resolvedPackageRoot = path.resolve(String(packageRoot || ""));
|
|
222
|
-
const packDir = fsModule.mkdtempSync(path.join(os.tmpdir(), tempDirPrefix));
|
|
223
|
-
try {
|
|
224
|
-
const packStdout = String(
|
|
225
|
-
execSyncImpl(
|
|
226
|
-
`npm pack ${shellQuote(resolvedPackageRoot)} --quiet --ignore-scripts --pack-destination ${shellQuote(packDir)}`,
|
|
227
|
-
{
|
|
228
|
-
encoding: "utf8",
|
|
229
|
-
stdio: ["ignore", "pipe", "inherit"],
|
|
230
|
-
timeout: 180000,
|
|
231
|
-
},
|
|
232
|
-
) || "",
|
|
233
|
-
)
|
|
234
|
-
.trim()
|
|
235
|
-
.split(/\r?\n/)
|
|
236
|
-
.map((entry) => entry.trim())
|
|
237
|
-
.filter(Boolean);
|
|
238
|
-
const packFileName =
|
|
239
|
-
packStdout.at(-1) ||
|
|
240
|
-
fsModule.readdirSync(packDir).find((entry) => entry.endsWith(".tgz"));
|
|
241
|
-
if (!packFileName) {
|
|
242
|
-
throw new Error(`npm pack did not produce a tarball for ${resolvedPackageRoot}`);
|
|
243
|
-
}
|
|
244
|
-
const tarballPath = path.join(packDir, packFileName);
|
|
245
|
-
if (!fsModule.existsSync(tarballPath)) {
|
|
246
|
-
throw new Error(`Packed tarball missing at ${tarballPath}`);
|
|
247
|
-
}
|
|
248
|
-
return {
|
|
249
|
-
tarballPath,
|
|
250
|
-
cleanup: () => {
|
|
251
|
-
try {
|
|
252
|
-
fsModule.rmSync(packDir, { recursive: true, force: true });
|
|
253
|
-
} catch {}
|
|
254
|
-
},
|
|
255
|
-
};
|
|
256
|
-
} catch (error) {
|
|
257
|
-
try {
|
|
258
|
-
fsModule.rmSync(packDir, { recursive: true, force: true });
|
|
259
|
-
} catch {}
|
|
260
|
-
throw error;
|
|
261
|
-
}
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
const shellQuote = (value) =>
|
|
265
|
-
`'${String(value || "").replace(/'/g, `'\"'\"'`)}'`;
|
|
266
|
-
|
|
267
|
-
module.exports = {
|
|
268
|
-
computePackageFingerprint,
|
|
269
|
-
isPackageRootSymlink,
|
|
270
|
-
packLocalPackageForInstall,
|
|
271
|
-
resolveInstallRootFromPackageRoot,
|
|
272
|
-
resolvePackageRootFromEntryPath,
|
|
273
|
-
seedRuntimeFromBundledInstall,
|
|
274
|
-
};
|