@wipcomputer/memory-crystal 0.7.32 → 0.7.33

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 (155) hide show
  1. package/SKILL.md +1 -1
  2. package/cloud/wrangler.toml +30 -0
  3. package/dist/bridge.d.ts +7 -0
  4. package/dist/bridge.js +14 -0
  5. package/dist/bulk-copy.d.ts +17 -0
  6. package/dist/bulk-copy.js +90 -0
  7. package/dist/cc-hook.d.ts +8 -0
  8. package/dist/cc-hook.js +368 -0
  9. package/dist/cc-poller.d.ts +1 -0
  10. package/dist/cc-poller.js +550 -0
  11. package/dist/chunk-25LXQJ4Z.js +110 -0
  12. package/dist/chunk-2DRXIRQW.js +97 -0
  13. package/dist/chunk-2GBYLMEF.js +1385 -0
  14. package/dist/chunk-2ZNH5F6E.js +1281 -0
  15. package/dist/chunk-3G3SFYYI.js +288 -0
  16. package/dist/chunk-3RG5ZIWI.js +10 -0
  17. package/dist/chunk-3S6TI23B.js +97 -0
  18. package/dist/chunk-3VFIJYS4.js +818 -0
  19. package/dist/chunk-437F27T6.js +97 -0
  20. package/dist/chunk-52QE3YI3.js +1169 -0
  21. package/dist/chunk-57RP3DIN.js +1205 -0
  22. package/dist/chunk-5HSZ4W2P.js +62 -0
  23. package/dist/chunk-5I7GMRDN.js +146 -0
  24. package/dist/chunk-645IPXW3.js +290 -0
  25. package/dist/chunk-7A7ELD4C.js +1205 -0
  26. package/dist/chunk-7FYY4GZM.js +1205 -0
  27. package/dist/chunk-7IUE7ODU.js +254 -0
  28. package/dist/chunk-7RMLKZIS.js +108 -0
  29. package/dist/chunk-AA3OPP4Z.js +432 -0
  30. package/dist/chunk-AEWLSYPH.js +72 -0
  31. package/dist/chunk-ASSZDR6I.js +108 -0
  32. package/dist/chunk-AYRJVWUC.js +1205 -0
  33. package/dist/chunk-CCYI5O3D.js +148 -0
  34. package/dist/chunk-CGIDSAJB.js +288 -0
  35. package/dist/chunk-D3I3ZSE2.js +411 -0
  36. package/dist/chunk-D3MACYZ4.js +108 -0
  37. package/dist/chunk-DACSKLY6.js +219 -0
  38. package/dist/chunk-DFQ72B7M.js +248 -0
  39. package/dist/chunk-DW5B4BL7.js +108 -0
  40. package/dist/chunk-EKSACBTJ.js +1070 -0
  41. package/dist/chunk-EXEZZADG.js +248 -0
  42. package/dist/chunk-F3Y7EL7K.js +83 -0
  43. package/dist/chunk-FBQWSDPC.js +1328 -0
  44. package/dist/chunk-FHRZNOMW.js +1205 -0
  45. package/dist/chunk-IM7N24MT.js +129 -0
  46. package/dist/chunk-IPNYIXFK.js +1178 -0
  47. package/dist/chunk-J7MRSZIO.js +167 -0
  48. package/dist/chunk-JITKI2OI.js +106 -0
  49. package/dist/chunk-JWZXYVET.js +1068 -0
  50. package/dist/chunk-KCQUXVYT.js +108 -0
  51. package/dist/chunk-KOQ43OX6.js +1281 -0
  52. package/dist/chunk-KYVWO6ZM.js +1069 -0
  53. package/dist/chunk-L3VHARQH.js +413 -0
  54. package/dist/chunk-LBWDS6BE.js +288 -0
  55. package/dist/chunk-LOVAHSQV.js +411 -0
  56. package/dist/chunk-LQOYCAGG.js +446 -0
  57. package/dist/chunk-LWAIPJ2W.js +146 -0
  58. package/dist/chunk-M5DHKW7M.js +127 -0
  59. package/dist/chunk-MBKCIJHM.js +1328 -0
  60. package/dist/chunk-MK42FMEG.js +147 -0
  61. package/dist/chunk-MOBMYHKL.js +1205 -0
  62. package/dist/chunk-MPLTNMRG.js +67 -0
  63. package/dist/chunk-NIJCVN3O.js +147 -0
  64. package/dist/chunk-NX647OM3.js +310 -0
  65. package/dist/chunk-NZCFSZQ7.js +1205 -0
  66. package/dist/chunk-O2UITJGH.js +465 -0
  67. package/dist/chunk-OCRA44AZ.js +108 -0
  68. package/dist/chunk-P3KJR66H.js +117 -0
  69. package/dist/chunk-PEK6JH65.js +432 -0
  70. package/dist/chunk-PJ6FFKEX.js +77 -0
  71. package/dist/chunk-PLUBBZYR.js +800 -0
  72. package/dist/chunk-PNKVD2UK.js +26 -0
  73. package/dist/chunk-PSQZURHO.js +229 -0
  74. package/dist/chunk-SGL6ISBJ.js +1061 -0
  75. package/dist/chunk-SJABZZT5.js +97 -0
  76. package/dist/chunk-TD3P3K32.js +1199 -0
  77. package/dist/chunk-TMDZJJKV.js +288 -0
  78. package/dist/chunk-UNHVZB5G.js +411 -0
  79. package/dist/chunk-VAFTWSTE.js +1061 -0
  80. package/dist/chunk-VNFXFQBB.js +217 -0
  81. package/dist/chunk-X3GVFKSJ.js +1205 -0
  82. package/dist/chunk-XZ3S56RQ.js +1061 -0
  83. package/dist/chunk-Y72C7F6O.js +148 -0
  84. package/dist/chunk-YLICP577.js +1205 -0
  85. package/dist/chunk-YX6AXLVK.js +159 -0
  86. package/dist/chunk-ZCQYHTNU.js +146 -0
  87. package/dist/cli.d.ts +1 -0
  88. package/dist/cli.js +1160 -0
  89. package/dist/cloud-crystal.js +6 -0
  90. package/dist/core.d.ts +252 -0
  91. package/dist/core.js +12 -0
  92. package/dist/crypto.d.ts +20 -0
  93. package/dist/crypto.js +27 -0
  94. package/dist/crystal-capture.sh +29 -0
  95. package/dist/crystal-serve.d.ts +4 -0
  96. package/dist/crystal-serve.js +252 -0
  97. package/dist/dev-update-SZ2Z4WCQ.js +6 -0
  98. package/dist/discover.d.ts +30 -0
  99. package/dist/discover.js +177 -0
  100. package/dist/doctor.d.ts +9 -0
  101. package/dist/doctor.js +342 -0
  102. package/dist/dream-weaver.d.ts +8 -0
  103. package/dist/dream-weaver.js +56 -0
  104. package/dist/file-sync.d.ts +48 -0
  105. package/dist/file-sync.js +18 -0
  106. package/dist/installer.d.ts +61 -0
  107. package/dist/installer.js +772 -0
  108. package/dist/ldm-backup.sh +116 -0
  109. package/dist/ldm.d.ts +50 -0
  110. package/dist/ldm.js +32 -0
  111. package/dist/llm-XXLYPIOF.js +16 -0
  112. package/dist/mcp-server.d.ts +1 -0
  113. package/dist/mcp-server.js +277 -0
  114. package/dist/migrate.d.ts +1 -0
  115. package/dist/migrate.js +89 -0
  116. package/dist/mirror-sync.d.ts +1 -0
  117. package/dist/mirror-sync.js +159 -0
  118. package/dist/mlx-setup-XKU67WCT.js +289 -0
  119. package/dist/oc-backfill.d.ts +19 -0
  120. package/dist/oc-backfill.js +74 -0
  121. package/dist/openclaw.d.ts +5 -0
  122. package/dist/openclaw.js +434 -0
  123. package/dist/pair.d.ts +4 -0
  124. package/dist/pair.js +75 -0
  125. package/dist/poller.d.ts +1 -0
  126. package/dist/poller.js +634 -0
  127. package/dist/role.d.ts +24 -0
  128. package/dist/role.js +13 -0
  129. package/dist/search-pipeline-4K4OJSSS.js +255 -0
  130. package/dist/search-pipeline-4PRS6LI7.js +280 -0
  131. package/dist/search-pipeline-7UJMXPLO.js +280 -0
  132. package/dist/search-pipeline-CBV25NX7.js +99 -0
  133. package/dist/search-pipeline-DQTRLGBH.js +74 -0
  134. package/dist/search-pipeline-HNG37REH.js +282 -0
  135. package/dist/search-pipeline-IZFPLBUB.js +280 -0
  136. package/dist/search-pipeline-MID6F26Q.js +73 -0
  137. package/dist/search-pipeline-N52JZFNN.js +282 -0
  138. package/dist/search-pipeline-OPB2PRQQ.js +280 -0
  139. package/dist/search-pipeline-VXTE5HAD.js +262 -0
  140. package/dist/search-pipeline-XHFKADRG.js +73 -0
  141. package/dist/staging.d.ts +29 -0
  142. package/dist/staging.js +21 -0
  143. package/dist/summarize.d.ts +19 -0
  144. package/dist/summarize.js +10 -0
  145. package/dist/worker-demo.js +186 -0
  146. package/dist/worker-mcp.js +404 -0
  147. package/dist/worker.js +137 -0
  148. package/package.json +15 -1
  149. package/.env.example +0 -20
  150. package/.publish-skill.json +0 -1
  151. package/CHANGELOG.md +0 -1372
  152. package/README-ENTERPRISE.md +0 -226
  153. package/RELAY.md +0 -199
  154. package/wrangler-demo.toml +0 -8
  155. package/wrangler-mcp.toml +0 -24
@@ -0,0 +1,219 @@
1
+ // src/ldm.ts
2
+ import { existsSync, mkdirSync, readFileSync, writeFileSync, copyFileSync, chmodSync } from "fs";
3
+ import { join, dirname } from "path";
4
+ import { execSync } from "child_process";
5
+ import { fileURLToPath } from "url";
6
+ var HOME = process.env.HOME || "";
7
+ var LDM_ROOT = join(HOME, ".ldm");
8
+ function getAgentId() {
9
+ return process.env.CRYSTAL_AGENT_ID || "cc-mini";
10
+ }
11
+ function ldmPaths(agentId) {
12
+ const id = agentId || getAgentId();
13
+ const agentRoot = join(LDM_ROOT, "agents", id);
14
+ return {
15
+ root: LDM_ROOT,
16
+ bin: join(LDM_ROOT, "bin"),
17
+ secrets: join(LDM_ROOT, "secrets"),
18
+ state: join(LDM_ROOT, "state"),
19
+ config: join(LDM_ROOT, "config.json"),
20
+ crystalDb: join(LDM_ROOT, "memory", "crystal.db"),
21
+ crystalLance: join(LDM_ROOT, "memory", "lance"),
22
+ agentRoot,
23
+ transcripts: join(agentRoot, "memory", "transcripts"),
24
+ sessions: join(agentRoot, "memory", "sessions"),
25
+ daily: join(agentRoot, "memory", "daily"),
26
+ journals: join(agentRoot, "memory", "journals"),
27
+ workspace: join(agentRoot, "memory", "workspace")
28
+ };
29
+ }
30
+ function loadConfig() {
31
+ const configPath = join(LDM_ROOT, "config.json");
32
+ try {
33
+ if (existsSync(configPath)) {
34
+ return JSON.parse(readFileSync(configPath, "utf-8"));
35
+ }
36
+ } catch {
37
+ }
38
+ return null;
39
+ }
40
+ function saveConfig(config) {
41
+ const configPath = join(LDM_ROOT, "config.json");
42
+ writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n");
43
+ }
44
+ function scaffoldLdm(agentId) {
45
+ const paths = ldmPaths(agentId);
46
+ mkdirSync(join(paths.root, "memory"), { recursive: true });
47
+ mkdirSync(paths.crystalLance, { recursive: true });
48
+ mkdirSync(paths.bin, { recursive: true });
49
+ mkdirSync(paths.secrets, { recursive: true, mode: 448 });
50
+ mkdirSync(paths.state, { recursive: true });
51
+ mkdirSync(paths.transcripts, { recursive: true });
52
+ mkdirSync(paths.sessions, { recursive: true });
53
+ mkdirSync(paths.daily, { recursive: true });
54
+ mkdirSync(paths.journals, { recursive: true });
55
+ mkdirSync(paths.workspace, { recursive: true });
56
+ const id = agentId || getAgentId();
57
+ let config = loadConfig();
58
+ if (!config) {
59
+ config = {
60
+ version: "1.0.0",
61
+ agents: [id],
62
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
63
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
64
+ };
65
+ } else {
66
+ if (!config.agents.includes(id)) {
67
+ config.agents.push(id);
68
+ }
69
+ config.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
70
+ }
71
+ saveConfig(config);
72
+ return paths;
73
+ }
74
+ function deployCaptureScript() {
75
+ const paths = ldmPaths();
76
+ mkdirSync(paths.bin, { recursive: true });
77
+ const thisDir = dirname(fileURLToPath(import.meta.url));
78
+ let scriptSrc = join(thisDir, "crystal-capture.sh");
79
+ if (!existsSync(scriptSrc)) {
80
+ scriptSrc = join(thisDir, "..", "scripts", "crystal-capture.sh");
81
+ }
82
+ const scriptDest = join(paths.bin, "crystal-capture.sh");
83
+ if (!existsSync(scriptSrc)) {
84
+ throw new Error(`crystal-capture.sh not found at ${scriptSrc}`);
85
+ }
86
+ copyFileSync(scriptSrc, scriptDest);
87
+ chmodSync(scriptDest, 493);
88
+ return scriptDest;
89
+ }
90
+ var CRON_TAG = "# crystal-capture";
91
+ var CRON_ENTRY = "* * * * * ~/.ldm/bin/crystal-capture.sh >> /tmp/ldm-dev-tools/crystal-capture.log 2>&1";
92
+ function isCrystalCaptureLine(line) {
93
+ return line === CRON_TAG || line.includes("crystal-capture.sh") && line.startsWith("*");
94
+ }
95
+ function installCron() {
96
+ mkdirSync("/tmp/ldm-dev-tools", { recursive: true });
97
+ let existing = "";
98
+ try {
99
+ existing = execSync("crontab -l 2>/dev/null", { encoding: "utf8" });
100
+ } catch {
101
+ }
102
+ const lines = existing.split("\n").filter((line) => !isCrystalCaptureLine(line));
103
+ lines.push(CRON_TAG);
104
+ lines.push(CRON_ENTRY);
105
+ const newCrontab = lines.filter((l, i, arr) => !(l === "" && i === arr.length - 1)).join("\n") + "\n";
106
+ execSync("crontab -", { input: newCrontab, encoding: "utf8" });
107
+ }
108
+ function removeCron() {
109
+ let existing = "";
110
+ try {
111
+ existing = execSync("crontab -l 2>/dev/null", { encoding: "utf8" });
112
+ } catch {
113
+ return;
114
+ }
115
+ const lines = existing.split("\n").filter((line) => !isCrystalCaptureLine(line));
116
+ const newCrontab = lines.join("\n");
117
+ execSync("crontab -", { input: newCrontab, encoding: "utf8" });
118
+ }
119
+ function deployBackupScript() {
120
+ const paths = ldmPaths();
121
+ mkdirSync(paths.bin, { recursive: true });
122
+ const thisDir = dirname(fileURLToPath(import.meta.url));
123
+ let scriptSrc = join(thisDir, "ldm-backup.sh");
124
+ if (!existsSync(scriptSrc)) {
125
+ scriptSrc = join(thisDir, "..", "scripts", "ldm-backup.sh");
126
+ }
127
+ const scriptDest = join(paths.bin, "ldm-backup.sh");
128
+ if (!existsSync(scriptSrc)) {
129
+ throw new Error(`ldm-backup.sh not found at ${scriptSrc}`);
130
+ }
131
+ copyFileSync(scriptSrc, scriptDest);
132
+ chmodSync(scriptDest, 493);
133
+ return scriptDest;
134
+ }
135
+ function installBackupLaunchAgent() {
136
+ const scriptPath = join(ldmPaths().bin, "ldm-backup.sh");
137
+ if (!existsSync(scriptPath)) {
138
+ throw new Error(`Backup script not found. Run crystal init first.`);
139
+ }
140
+ const launchAgentsDir = join(HOME, "Library", "LaunchAgents");
141
+ mkdirSync(launchAgentsDir, { recursive: true });
142
+ const plistPath = join(launchAgentsDir, "ai.openclaw.ldm-backup.plist");
143
+ const plist = `<?xml version="1.0" encoding="UTF-8"?>
144
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
145
+ <plist version="1.0">
146
+ <dict>
147
+ <key>Label</key>
148
+ <string>ai.openclaw.ldm-backup</string>
149
+ <key>ProgramArguments</key>
150
+ <array>
151
+ <string>bash</string>
152
+ <string>${scriptPath}</string>
153
+ </array>
154
+ <key>StartCalendarInterval</key>
155
+ <dict>
156
+ <key>Hour</key>
157
+ <integer>3</integer>
158
+ <key>Minute</key>
159
+ <integer>0</integer>
160
+ </dict>
161
+ <key>StandardOutPath</key>
162
+ <string>/tmp/ldm-dev-tools/ldm-backup.log</string>
163
+ <key>StandardErrorPath</key>
164
+ <string>/tmp/ldm-dev-tools/ldm-backup.log</string>
165
+ </dict>
166
+ </plist>`;
167
+ writeFileSync(plistPath, plist);
168
+ try {
169
+ execSync(`launchctl unload ${plistPath} 2>/dev/null`);
170
+ } catch {
171
+ }
172
+ execSync(`launchctl load ${plistPath}`);
173
+ return plistPath;
174
+ }
175
+ var LEGACY_OC_DIR = join(HOME, ".openclaw");
176
+ function resolveStatePath(filename) {
177
+ const paths = ldmPaths();
178
+ const ldmPath = join(paths.state, filename);
179
+ if (existsSync(ldmPath)) return ldmPath;
180
+ const legacyPath = join(LEGACY_OC_DIR, "memory", filename);
181
+ if (existsSync(legacyPath)) return legacyPath;
182
+ return ldmPath;
183
+ }
184
+ function stateWritePath(filename) {
185
+ const paths = ldmPaths();
186
+ const dir = paths.state;
187
+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
188
+ return join(dir, filename);
189
+ }
190
+ function resolveSecretPath(filename) {
191
+ const paths = ldmPaths();
192
+ const ldmPath = join(paths.secrets, filename);
193
+ if (existsSync(ldmPath)) return ldmPath;
194
+ const legacyPath = join(LEGACY_OC_DIR, "secrets", filename);
195
+ if (existsSync(legacyPath)) return legacyPath;
196
+ return ldmPath;
197
+ }
198
+ function ensureLdm(agentId) {
199
+ const paths = ldmPaths(agentId);
200
+ if (existsSync(paths.transcripts) && existsSync(paths.config)) {
201
+ return paths;
202
+ }
203
+ return scaffoldLdm(agentId);
204
+ }
205
+
206
+ export {
207
+ getAgentId,
208
+ ldmPaths,
209
+ scaffoldLdm,
210
+ deployCaptureScript,
211
+ installCron,
212
+ removeCron,
213
+ deployBackupScript,
214
+ installBackupLaunchAgent,
215
+ resolveStatePath,
216
+ stateWritePath,
217
+ resolveSecretPath,
218
+ ensureLdm
219
+ };
@@ -0,0 +1,248 @@
1
+ // src/ldm.ts
2
+ import { existsSync, mkdirSync, readFileSync, writeFileSync, copyFileSync, chmodSync, readdirSync } from "fs";
3
+ import { join, dirname } from "path";
4
+ import { execSync } from "child_process";
5
+ import { fileURLToPath } from "url";
6
+ var HOME = process.env.HOME || "";
7
+ var LDM_ROOT = join(HOME, ".ldm");
8
+ function loadAgentConfig(id) {
9
+ const cfgPath = join(LDM_ROOT, "agents", id, "config.json");
10
+ try {
11
+ if (existsSync(cfgPath)) return JSON.parse(readFileSync(cfgPath, "utf-8"));
12
+ } catch {
13
+ }
14
+ return null;
15
+ }
16
+ function saveAgentConfig(id, config) {
17
+ const dir = join(LDM_ROOT, "agents", id);
18
+ mkdirSync(dir, { recursive: true });
19
+ writeFileSync(join(dir, "config.json"), JSON.stringify(config, null, 2) + "\n");
20
+ }
21
+ function getAgentId(harnessHint) {
22
+ if (process.env.CRYSTAL_AGENT_ID) return process.env.CRYSTAL_AGENT_ID;
23
+ const agentsDir = join(LDM_ROOT, "agents");
24
+ if (existsSync(agentsDir)) {
25
+ try {
26
+ for (const d of readdirSync(agentsDir)) {
27
+ const cfg = loadAgentConfig(d);
28
+ if (!cfg || !cfg.agentId) continue;
29
+ if (!harnessHint) return cfg.agentId;
30
+ if (harnessHint === "claude-code" && cfg.harness === "claude-code-cli") return cfg.agentId;
31
+ if (harnessHint === "openclaw" && cfg.harness === "openclaw") return cfg.agentId;
32
+ }
33
+ } catch {
34
+ }
35
+ }
36
+ return harnessHint === "openclaw" ? "oc-lesa-mini" : "cc-mini";
37
+ }
38
+ function ldmPaths(agentId) {
39
+ const id = agentId || getAgentId();
40
+ const agentRoot = join(LDM_ROOT, "agents", id);
41
+ return {
42
+ root: LDM_ROOT,
43
+ bin: join(LDM_ROOT, "bin"),
44
+ secrets: join(LDM_ROOT, "secrets"),
45
+ state: join(LDM_ROOT, "state"),
46
+ config: join(LDM_ROOT, "config.json"),
47
+ crystalDb: join(LDM_ROOT, "memory", "crystal.db"),
48
+ crystalLance: join(LDM_ROOT, "memory", "lance"),
49
+ agentRoot,
50
+ transcripts: join(agentRoot, "memory", "transcripts"),
51
+ sessions: join(agentRoot, "memory", "sessions"),
52
+ daily: join(agentRoot, "memory", "daily"),
53
+ journals: join(agentRoot, "memory", "journals"),
54
+ workspace: join(agentRoot, "memory", "workspace")
55
+ };
56
+ }
57
+ function loadConfig() {
58
+ const configPath = join(LDM_ROOT, "config.json");
59
+ try {
60
+ if (existsSync(configPath)) {
61
+ return JSON.parse(readFileSync(configPath, "utf-8"));
62
+ }
63
+ } catch {
64
+ }
65
+ return null;
66
+ }
67
+ function saveConfig(config) {
68
+ const configPath = join(LDM_ROOT, "config.json");
69
+ writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n");
70
+ }
71
+ function scaffoldLdm(agentId) {
72
+ const paths = ldmPaths(agentId);
73
+ mkdirSync(join(paths.root, "memory"), { recursive: true });
74
+ mkdirSync(paths.crystalLance, { recursive: true });
75
+ mkdirSync(paths.bin, { recursive: true });
76
+ mkdirSync(paths.secrets, { recursive: true, mode: 448 });
77
+ mkdirSync(paths.state, { recursive: true });
78
+ mkdirSync(paths.transcripts, { recursive: true });
79
+ mkdirSync(paths.sessions, { recursive: true });
80
+ mkdirSync(paths.daily, { recursive: true });
81
+ mkdirSync(paths.journals, { recursive: true });
82
+ mkdirSync(paths.workspace, { recursive: true });
83
+ const id = agentId || getAgentId();
84
+ let config = loadConfig();
85
+ if (!config) {
86
+ config = {
87
+ version: "1.0.0",
88
+ agents: [id],
89
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
90
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
91
+ };
92
+ } else {
93
+ if (!config.agents.includes(id)) {
94
+ config.agents.push(id);
95
+ }
96
+ config.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
97
+ }
98
+ saveConfig(config);
99
+ return paths;
100
+ }
101
+ function deployCaptureScript() {
102
+ const paths = ldmPaths();
103
+ mkdirSync(paths.bin, { recursive: true });
104
+ const thisDir = dirname(fileURLToPath(import.meta.url));
105
+ let scriptSrc = join(thisDir, "crystal-capture.sh");
106
+ if (!existsSync(scriptSrc)) {
107
+ scriptSrc = join(thisDir, "..", "scripts", "crystal-capture.sh");
108
+ }
109
+ const scriptDest = join(paths.bin, "crystal-capture.sh");
110
+ if (!existsSync(scriptSrc)) {
111
+ throw new Error(`crystal-capture.sh not found at ${scriptSrc}`);
112
+ }
113
+ copyFileSync(scriptSrc, scriptDest);
114
+ chmodSync(scriptDest, 493);
115
+ return scriptDest;
116
+ }
117
+ var CRON_TAG = "# crystal-capture";
118
+ var CRON_ENTRY = "* * * * * ~/.ldm/bin/crystal-capture.sh >> ~/.ldm/logs/crystal-capture.log 2>&1";
119
+ function isCrystalCaptureLine(line) {
120
+ return line === CRON_TAG || line.includes("crystal-capture.sh") && line.startsWith("*");
121
+ }
122
+ function installCron() {
123
+ mkdirSync(join(HOME, ".ldm", "logs"), { recursive: true });
124
+ let existing = "";
125
+ try {
126
+ existing = execSync("crontab -l 2>/dev/null", { encoding: "utf8" });
127
+ } catch {
128
+ }
129
+ const lines = existing.split("\n").filter((line) => !isCrystalCaptureLine(line));
130
+ lines.push(CRON_TAG);
131
+ lines.push(CRON_ENTRY);
132
+ const newCrontab = lines.filter((l, i, arr) => !(l === "" && i === arr.length - 1)).join("\n") + "\n";
133
+ execSync("crontab -", { input: newCrontab, encoding: "utf8" });
134
+ }
135
+ function removeCron() {
136
+ let existing = "";
137
+ try {
138
+ existing = execSync("crontab -l 2>/dev/null", { encoding: "utf8" });
139
+ } catch {
140
+ return;
141
+ }
142
+ const lines = existing.split("\n").filter((line) => !isCrystalCaptureLine(line));
143
+ const newCrontab = lines.join("\n");
144
+ execSync("crontab -", { input: newCrontab, encoding: "utf8" });
145
+ }
146
+ function deployBackupScript() {
147
+ const paths = ldmPaths();
148
+ mkdirSync(paths.bin, { recursive: true });
149
+ const thisDir = dirname(fileURLToPath(import.meta.url));
150
+ let scriptSrc = join(thisDir, "ldm-backup.sh");
151
+ if (!existsSync(scriptSrc)) {
152
+ scriptSrc = join(thisDir, "..", "scripts", "ldm-backup.sh");
153
+ }
154
+ const scriptDest = join(paths.bin, "ldm-backup.sh");
155
+ if (!existsSync(scriptSrc)) {
156
+ throw new Error(`ldm-backup.sh not found at ${scriptSrc}`);
157
+ }
158
+ copyFileSync(scriptSrc, scriptDest);
159
+ chmodSync(scriptDest, 493);
160
+ return scriptDest;
161
+ }
162
+ function installBackupLaunchAgent() {
163
+ const scriptPath = join(ldmPaths().bin, "ldm-backup.sh");
164
+ if (!existsSync(scriptPath)) {
165
+ throw new Error(`Backup script not found. Run crystal init first.`);
166
+ }
167
+ const launchAgentsDir = join(HOME, "Library", "LaunchAgents");
168
+ mkdirSync(launchAgentsDir, { recursive: true });
169
+ const plistPath = join(launchAgentsDir, "ai.openclaw.ldm-backup.plist");
170
+ const plist = `<?xml version="1.0" encoding="UTF-8"?>
171
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
172
+ <plist version="1.0">
173
+ <dict>
174
+ <key>Label</key>
175
+ <string>ai.openclaw.ldm-backup</string>
176
+ <key>ProgramArguments</key>
177
+ <array>
178
+ <string>bash</string>
179
+ <string>${scriptPath}</string>
180
+ </array>
181
+ <key>StartCalendarInterval</key>
182
+ <dict>
183
+ <key>Hour</key>
184
+ <integer>3</integer>
185
+ <key>Minute</key>
186
+ <integer>0</integer>
187
+ </dict>
188
+ <key>StandardOutPath</key>
189
+ <string>${HOME}/.ldm/logs/ldm-backup.log</string>
190
+ <key>StandardErrorPath</key>
191
+ <string>${HOME}/.ldm/logs/ldm-backup.log</string>
192
+ </dict>
193
+ </plist>`;
194
+ writeFileSync(plistPath, plist);
195
+ try {
196
+ execSync(`launchctl unload ${plistPath} 2>/dev/null`);
197
+ } catch {
198
+ }
199
+ execSync(`launchctl load ${plistPath}`);
200
+ return plistPath;
201
+ }
202
+ var LEGACY_OC_DIR = join(HOME, ".openclaw");
203
+ function resolveStatePath(filename) {
204
+ const paths = ldmPaths();
205
+ const ldmPath = join(paths.state, filename);
206
+ if (existsSync(ldmPath)) return ldmPath;
207
+ const legacyPath = join(LEGACY_OC_DIR, "memory", filename);
208
+ if (existsSync(legacyPath)) return legacyPath;
209
+ return ldmPath;
210
+ }
211
+ function stateWritePath(filename) {
212
+ const paths = ldmPaths();
213
+ const dir = paths.state;
214
+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
215
+ return join(dir, filename);
216
+ }
217
+ function resolveSecretPath(filename) {
218
+ const paths = ldmPaths();
219
+ const ldmPath = join(paths.secrets, filename);
220
+ if (existsSync(ldmPath)) return ldmPath;
221
+ const legacyPath = join(LEGACY_OC_DIR, "secrets", filename);
222
+ if (existsSync(legacyPath)) return legacyPath;
223
+ return ldmPath;
224
+ }
225
+ function ensureLdm(agentId) {
226
+ const paths = ldmPaths(agentId);
227
+ if (existsSync(paths.transcripts) && existsSync(paths.config)) {
228
+ return paths;
229
+ }
230
+ return scaffoldLdm(agentId);
231
+ }
232
+
233
+ export {
234
+ loadAgentConfig,
235
+ saveAgentConfig,
236
+ getAgentId,
237
+ ldmPaths,
238
+ scaffoldLdm,
239
+ deployCaptureScript,
240
+ installCron,
241
+ removeCron,
242
+ deployBackupScript,
243
+ installBackupLaunchAgent,
244
+ resolveStatePath,
245
+ stateWritePath,
246
+ resolveSecretPath,
247
+ ensureLdm
248
+ };
@@ -0,0 +1,108 @@
1
+ import {
2
+ resolveSecretPath
3
+ } from "./chunk-VNFXFQBB.js";
4
+
5
+ // src/crypto.ts
6
+ import { readFileSync, existsSync } from "fs";
7
+ import { createCipheriv, createDecipheriv, createHmac, randomBytes, hkdfSync } from "crypto";
8
+ import { createHash } from "crypto";
9
+ var KEY_PATH = process.env.CRYSTAL_RELAY_KEY_PATH || resolveSecretPath("crystal-relay-key");
10
+ function loadRelayKey() {
11
+ if (!existsSync(KEY_PATH)) {
12
+ throw new Error(
13
+ `Relay key not found at ${KEY_PATH}
14
+ Generate one: openssl rand -base64 32 > ${KEY_PATH} && chmod 600 ${KEY_PATH}
15
+ Copy the same key to all trusted machines.`
16
+ );
17
+ }
18
+ const raw = readFileSync(KEY_PATH, "utf-8").trim();
19
+ const key = Buffer.from(raw, "base64");
20
+ if (key.length !== 32) {
21
+ throw new Error(`Relay key must be 32 bytes (256 bits). Got ${key.length} bytes. Regenerate with: openssl rand -base64 32`);
22
+ }
23
+ return key;
24
+ }
25
+ function deriveSigningKey(masterKey) {
26
+ return Buffer.from(hkdfSync("sha256", masterKey, "", "crystal-relay-sign", 32));
27
+ }
28
+ function encrypt(plaintext, masterKey) {
29
+ const nonce = randomBytes(12);
30
+ const cipher = createCipheriv("aes-256-gcm", masterKey, nonce);
31
+ const ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
32
+ const tag = cipher.getAuthTag();
33
+ const signingKey = deriveSigningKey(masterKey);
34
+ const hmacData = Buffer.concat([nonce, ciphertext, tag]);
35
+ const hmac = createHmac("sha256", signingKey).update(hmacData).digest("hex");
36
+ return {
37
+ v: 1,
38
+ nonce: nonce.toString("base64"),
39
+ ciphertext: ciphertext.toString("base64"),
40
+ tag: tag.toString("base64"),
41
+ hmac
42
+ };
43
+ }
44
+ function decrypt(payload, masterKey) {
45
+ if (payload.v !== 1) {
46
+ throw new Error(`Unknown payload version: ${payload.v}`);
47
+ }
48
+ const nonce = Buffer.from(payload.nonce, "base64");
49
+ const ciphertext = Buffer.from(payload.ciphertext, "base64");
50
+ const tag = Buffer.from(payload.tag, "base64");
51
+ const signingKey = deriveSigningKey(masterKey);
52
+ const hmacData = Buffer.concat([nonce, ciphertext, tag]);
53
+ const expectedHmac = createHmac("sha256", signingKey).update(hmacData).digest("hex");
54
+ if (payload.hmac !== expectedHmac) {
55
+ throw new Error("HMAC verification failed \u2014 blob rejected (tampered or wrong key)");
56
+ }
57
+ const decipher = createDecipheriv("aes-256-gcm", masterKey, nonce);
58
+ decipher.setAuthTag(tag);
59
+ return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
60
+ }
61
+ function encryptJSON(data, masterKey) {
62
+ const plaintext = Buffer.from(JSON.stringify(data), "utf-8");
63
+ return encrypt(plaintext, masterKey);
64
+ }
65
+ function decryptJSON(payload, masterKey) {
66
+ const plaintext = decrypt(payload, masterKey);
67
+ return JSON.parse(plaintext.toString("utf-8"));
68
+ }
69
+ function encryptFile(filePath, masterKey) {
70
+ const plaintext = readFileSync(filePath);
71
+ return encrypt(plaintext, masterKey);
72
+ }
73
+ var RELAY_KEY_PATH = KEY_PATH;
74
+ function generateRelayKey() {
75
+ return randomBytes(32);
76
+ }
77
+ function encodePairingString(key) {
78
+ if (key.length !== 32) throw new Error("Key must be 32 bytes");
79
+ return `mc1:${key.toString("base64")}`;
80
+ }
81
+ function decodePairingString(str) {
82
+ const trimmed = str.trim();
83
+ if (!trimmed.startsWith("mc1:")) {
84
+ throw new Error("Invalid pairing string (expected mc1: prefix)");
85
+ }
86
+ const key = Buffer.from(trimmed.slice(4), "base64");
87
+ if (key.length !== 32) {
88
+ throw new Error(`Invalid key length: expected 32 bytes, got ${key.length}`);
89
+ }
90
+ return key;
91
+ }
92
+ function hashBuffer(data) {
93
+ return createHash("sha256").update(data).digest("hex");
94
+ }
95
+
96
+ export {
97
+ loadRelayKey,
98
+ encrypt,
99
+ decrypt,
100
+ encryptJSON,
101
+ decryptJSON,
102
+ encryptFile,
103
+ RELAY_KEY_PATH,
104
+ generateRelayKey,
105
+ encodePairingString,
106
+ decodePairingString,
107
+ hashBuffer
108
+ };