compound-agent 1.4.2 → 1.4.4

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,102 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Postinstall script for compound-agent.
4
+ *
5
+ * Patches the consumer's package.json to allow native addon builds
6
+ * in pnpm v10+ projects. No-op for non-pnpm or already-configured projects.
7
+ *
8
+ * IMPORTANT: This script MUST NOT import any native modules or built code.
9
+ * It runs before native addons are compiled.
10
+ *
11
+ * NOTE: The REQUIRED_BUILD_DEPS list is intentionally duplicated here
12
+ * (canonical source: src/setup/primitives.ts REQUIRED_BUILD_DEPS).
13
+ * This file cannot import TypeScript build output because it runs
14
+ * before the package is fully installed. Keep both lists in sync.
15
+ * The list intentionally excludes "esbuild" -- that is only needed
16
+ * for compound-agent's own dev build (tsup), not consumer projects.
17
+ */
18
+
19
+ import { existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
20
+ import { join } from 'node:path';
21
+
22
+ const REQUIRED_BUILD_DEPS = ['better-sqlite3', 'node-llama-cpp'];
23
+
24
+ /**
25
+ * Core postinstall logic, extracted for testability.
26
+ * @param {string} consumerRoot - The consumer project root directory.
27
+ * @returns {{ added: string[] } | null} - What was added, or null if no-op.
28
+ */
29
+ export function patchPnpmConfig(consumerRoot) {
30
+ const pkgPath = join(consumerRoot, 'package.json');
31
+
32
+ if (!existsSync(pkgPath)) return null;
33
+
34
+ let raw;
35
+ try { raw = readFileSync(pkgPath, 'utf-8'); } catch { return null; }
36
+
37
+ // Strip UTF-8 BOM if present
38
+ if (raw.charCodeAt(0) === 0xFEFF) raw = raw.slice(1);
39
+
40
+ let pkg;
41
+ try { pkg = JSON.parse(raw); } catch { return null; }
42
+
43
+ // Detect pnpm: lockfile OR packageManager field
44
+ const lockPath = join(consumerRoot, 'pnpm-lock.yaml');
45
+ const isPnpm = existsSync(lockPath) ||
46
+ (typeof pkg.packageManager === 'string' && pkg.packageManager.startsWith('pnpm'));
47
+ if (!isPnpm) return null;
48
+
49
+ // Merge onlyBuiltDependencies
50
+ if (!pkg.pnpm || typeof pkg.pnpm !== 'object') pkg.pnpm = {};
51
+ if (!Array.isArray(pkg.pnpm.onlyBuiltDependencies)) pkg.pnpm.onlyBuiltDependencies = [];
52
+
53
+ const existing = pkg.pnpm.onlyBuiltDependencies;
54
+ // Wildcard "*" means all builds are allowed — nothing to add
55
+ if (existing.includes('*')) return null;
56
+ const added = [];
57
+ for (const dep of REQUIRED_BUILD_DEPS) {
58
+ if (!existing.includes(dep)) {
59
+ existing.push(dep);
60
+ added.push(dep);
61
+ }
62
+ }
63
+
64
+ if (added.length === 0) return null; // Already configured
65
+
66
+ // Detect indentation from the original file to preserve formatting
67
+ const indentMatch = raw.match(/^(\s+)"/m);
68
+ const indent = indentMatch ? indentMatch[1] : ' ';
69
+
70
+ const content = JSON.stringify(pkg, null, indent) + '\n';
71
+
72
+ // Atomic write: write to temp file then rename to prevent corruption
73
+ const tmpPath = pkgPath + '.compound-agent-tmp';
74
+ writeFileSync(tmpPath, content, 'utf-8');
75
+ renameSync(tmpPath, pkgPath);
76
+
77
+ return { added };
78
+ }
79
+
80
+ function run() {
81
+ // INIT_CWD is set by npm/pnpm to the directory where install was initiated.
82
+ // Without it, process.cwd() points to the package dir inside node_modules,
83
+ // which is the wrong location. Exit early rather than patching the wrong file.
84
+ const consumerRoot = process.env.INIT_CWD;
85
+ if (!consumerRoot) return;
86
+
87
+ // Skip self-install (when running pnpm install inside compound-agent itself)
88
+ if (process.env.npm_package_name === 'compound-agent') return;
89
+
90
+ const result = patchPnpmConfig(consumerRoot);
91
+ if (result) {
92
+ console.error(`[compound-agent] Added pnpm.onlyBuiltDependencies: [${result.added.join(', ')}]`);
93
+ console.error('[compound-agent] Run: pnpm install (to compile native addons)');
94
+ }
95
+ }
96
+
97
+ try {
98
+ run();
99
+ } catch (e) {
100
+ // Postinstall must never fail the install, but log unexpected errors
101
+ console.error('[compound-agent] postinstall warning:', e?.message ?? e);
102
+ }