@wipcomputer/memory-crystal 0.7.34-alpha.2 → 0.7.34-alpha.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.
Files changed (120) hide show
  1. package/LICENSE +1 -1
  2. package/dist/bridge.js +64 -7
  3. package/dist/bulk-copy.js +67 -16
  4. package/dist/cc-hook.js +2163 -62
  5. package/dist/cc-poller.js +1967 -70
  6. package/dist/cli.js +4538 -139
  7. package/dist/core.js +1789 -6
  8. package/dist/crypto.js +153 -14
  9. package/dist/crystal-serve.js +64 -12
  10. package/dist/doctor.js +517 -52
  11. package/dist/dream-weaver.js +1755 -7
  12. package/dist/file-sync.js +407 -9
  13. package/dist/installer.js +840 -145
  14. package/dist/ldm.js +231 -16
  15. package/dist/mcp-server.js +1882 -17
  16. package/dist/migrate.js +1707 -11
  17. package/dist/mirror-sync.js +2052 -34
  18. package/dist/openclaw.js +1895 -84
  19. package/dist/pair.js +112 -16
  20. package/dist/poller.js +2275 -80
  21. package/dist/role.js +159 -7
  22. package/dist/staging.js +235 -10
  23. package/dist/summarize.js +142 -5
  24. package/package.json +7 -4
  25. package/dist/chunk-25LXQJ4Z.js +0 -110
  26. package/dist/chunk-2DRXIRQW.js +0 -97
  27. package/dist/chunk-2GBYLMEF.js +0 -1385
  28. package/dist/chunk-2ZNH5F6E.js +0 -1281
  29. package/dist/chunk-3G3SFYYI.js +0 -288
  30. package/dist/chunk-3RG5ZIWI.js +0 -10
  31. package/dist/chunk-3S6TI23B.js +0 -97
  32. package/dist/chunk-3VFIJYS4.js +0 -818
  33. package/dist/chunk-437F27T6.js +0 -97
  34. package/dist/chunk-52QE3YI3.js +0 -1169
  35. package/dist/chunk-57RP3DIN.js +0 -1205
  36. package/dist/chunk-5HSZ4W2P.js +0 -62
  37. package/dist/chunk-5I7GMRDN.js +0 -146
  38. package/dist/chunk-645IPXW3.js +0 -290
  39. package/dist/chunk-7A7ELD4C.js +0 -1205
  40. package/dist/chunk-7FYY4GZM.js +0 -1205
  41. package/dist/chunk-7IUE7ODU.js +0 -254
  42. package/dist/chunk-7RMLKZIS.js +0 -108
  43. package/dist/chunk-AA3OPP4Z.js +0 -432
  44. package/dist/chunk-AEWLSYPH.js +0 -72
  45. package/dist/chunk-ASSZDR6I.js +0 -108
  46. package/dist/chunk-AYRJVWUC.js +0 -1205
  47. package/dist/chunk-CCYI5O3D.js +0 -148
  48. package/dist/chunk-CGIDSAJB.js +0 -288
  49. package/dist/chunk-D3I3ZSE2.js +0 -411
  50. package/dist/chunk-D3MACYZ4.js +0 -108
  51. package/dist/chunk-DACSKLY6.js +0 -219
  52. package/dist/chunk-DFQ72B7M.js +0 -248
  53. package/dist/chunk-DW5B4BL7.js +0 -108
  54. package/dist/chunk-EKSACBTJ.js +0 -1070
  55. package/dist/chunk-EXEZZADG.js +0 -248
  56. package/dist/chunk-F3Y7EL7K.js +0 -83
  57. package/dist/chunk-FBQWSDPC.js +0 -1328
  58. package/dist/chunk-FHRZNOMW.js +0 -1205
  59. package/dist/chunk-IM7N24MT.js +0 -129
  60. package/dist/chunk-IPNYIXFK.js +0 -1178
  61. package/dist/chunk-J7MRSZIO.js +0 -167
  62. package/dist/chunk-JITKI2OI.js +0 -106
  63. package/dist/chunk-JWZXYVET.js +0 -1068
  64. package/dist/chunk-KCQUXVYT.js +0 -108
  65. package/dist/chunk-KOQ43OX6.js +0 -1281
  66. package/dist/chunk-KYVWO6ZM.js +0 -1069
  67. package/dist/chunk-L3VHARQH.js +0 -413
  68. package/dist/chunk-LBWDS6BE.js +0 -288
  69. package/dist/chunk-LOVAHSQV.js +0 -411
  70. package/dist/chunk-LQOYCAGG.js +0 -446
  71. package/dist/chunk-LWAIPJ2W.js +0 -146
  72. package/dist/chunk-M5DHKW7M.js +0 -127
  73. package/dist/chunk-MBKCIJHM.js +0 -1328
  74. package/dist/chunk-MK42FMEG.js +0 -147
  75. package/dist/chunk-MOBMYHKL.js +0 -1205
  76. package/dist/chunk-MPLTNMRG.js +0 -67
  77. package/dist/chunk-NIJCVN3O.js +0 -147
  78. package/dist/chunk-NX647OM3.js +0 -310
  79. package/dist/chunk-NZCFSZQ7.js +0 -1205
  80. package/dist/chunk-O2UITJGH.js +0 -465
  81. package/dist/chunk-OCRA44AZ.js +0 -108
  82. package/dist/chunk-P3KJR66H.js +0 -117
  83. package/dist/chunk-PEK6JH65.js +0 -432
  84. package/dist/chunk-PJ6FFKEX.js +0 -77
  85. package/dist/chunk-PLUBBZYR.js +0 -800
  86. package/dist/chunk-PNKVD2UK.js +0 -26
  87. package/dist/chunk-PSQZURHO.js +0 -229
  88. package/dist/chunk-SGL6ISBJ.js +0 -1061
  89. package/dist/chunk-SJABZZT5.js +0 -97
  90. package/dist/chunk-TD3P3K32.js +0 -1199
  91. package/dist/chunk-TMDZJJKV.js +0 -288
  92. package/dist/chunk-UNHVZB5G.js +0 -411
  93. package/dist/chunk-VAFTWSTE.js +0 -1061
  94. package/dist/chunk-VNFXFQBB.js +0 -217
  95. package/dist/chunk-X3GVFKSJ.js +0 -1205
  96. package/dist/chunk-XZ3S56RQ.js +0 -1061
  97. package/dist/chunk-Y72C7F6O.js +0 -148
  98. package/dist/chunk-YLICP577.js +0 -1205
  99. package/dist/chunk-YX6AXLVK.js +0 -159
  100. package/dist/chunk-ZCQYHTNU.js +0 -146
  101. package/dist/cloud-crystal.js +0 -6
  102. package/dist/dev-update-SZ2Z4WCQ.js +0 -6
  103. package/dist/llm-XXLYPIOF.js +0 -16
  104. package/dist/mlx-setup-XKU67WCT.js +0 -289
  105. package/dist/search-pipeline-4K4OJSSS.js +0 -255
  106. package/dist/search-pipeline-4PRS6LI7.js +0 -280
  107. package/dist/search-pipeline-7UJMXPLO.js +0 -280
  108. package/dist/search-pipeline-CBV25NX7.js +0 -99
  109. package/dist/search-pipeline-DQTRLGBH.js +0 -74
  110. package/dist/search-pipeline-HNG37REH.js +0 -282
  111. package/dist/search-pipeline-IZFPLBUB.js +0 -280
  112. package/dist/search-pipeline-MID6F26Q.js +0 -73
  113. package/dist/search-pipeline-N52JZFNN.js +0 -282
  114. package/dist/search-pipeline-OPB2PRQQ.js +0 -280
  115. package/dist/search-pipeline-VXTE5HAD.js +0 -262
  116. package/dist/search-pipeline-XHFKADRG.js +0 -73
  117. package/dist/worker-demo.js +0 -186
  118. package/dist/worker-mcp.js +0 -404
  119. package/scripts/crystal-capture 2.sh +0 -29
  120. package/scripts/deploy-cloud 2.sh +0 -153
package/dist/role.js CHANGED
@@ -1,10 +1,162 @@
1
- import {
2
- demoteToNode,
3
- detectRole,
4
- loadRoleState,
5
- promoteToCore
6
- } from "./chunk-437F27T6.js";
7
- import "./chunk-DFQ72B7M.js";
1
+ // src/role.ts
2
+ import { existsSync as existsSync2, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
3
+
4
+ // src/ldm.ts
5
+ import { existsSync, mkdirSync, readFileSync, writeFileSync, copyFileSync, chmodSync, readdirSync } from "fs";
6
+ import { join, dirname } from "path";
7
+ import { execSync } from "child_process";
8
+ import { fileURLToPath } from "url";
9
+ var HOME = process.env.HOME || "";
10
+ var LDM_ROOT = join(HOME, ".ldm");
11
+ function loadAgentConfig(id) {
12
+ const cfgPath = join(LDM_ROOT, "agents", id, "config.json");
13
+ try {
14
+ if (existsSync(cfgPath)) return JSON.parse(readFileSync(cfgPath, "utf-8"));
15
+ } catch {
16
+ }
17
+ return null;
18
+ }
19
+ function getAgentId(harnessHint) {
20
+ if (process.env.CRYSTAL_AGENT_ID) return process.env.CRYSTAL_AGENT_ID;
21
+ const agentsDir = join(LDM_ROOT, "agents");
22
+ if (existsSync(agentsDir)) {
23
+ try {
24
+ for (const d of readdirSync(agentsDir)) {
25
+ const cfg = loadAgentConfig(d);
26
+ if (!cfg || !cfg.agentId) continue;
27
+ if (!harnessHint) return cfg.agentId;
28
+ if (harnessHint === "claude-code" && cfg.harness === "claude-code-cli") return cfg.agentId;
29
+ if (harnessHint === "openclaw" && cfg.harness === "openclaw") return cfg.agentId;
30
+ }
31
+ } catch {
32
+ }
33
+ }
34
+ return harnessHint === "openclaw" ? "oc-lesa-mini" : "cc-mini";
35
+ }
36
+ function ldmPaths(agentId) {
37
+ const id = agentId || getAgentId();
38
+ const agentRoot = join(LDM_ROOT, "agents", id);
39
+ return {
40
+ root: LDM_ROOT,
41
+ bin: join(LDM_ROOT, "bin"),
42
+ secrets: join(LDM_ROOT, "secrets"),
43
+ state: join(LDM_ROOT, "state"),
44
+ config: join(LDM_ROOT, "config.json"),
45
+ crystalDb: join(LDM_ROOT, "memory", "crystal.db"),
46
+ crystalLance: join(LDM_ROOT, "memory", "lance"),
47
+ agentRoot,
48
+ transcripts: join(agentRoot, "memory", "transcripts"),
49
+ sessions: join(agentRoot, "memory", "sessions"),
50
+ daily: join(agentRoot, "memory", "daily"),
51
+ journals: join(agentRoot, "memory", "journals"),
52
+ workspace: join(agentRoot, "memory", "workspace")
53
+ };
54
+ }
55
+ var LEGACY_OC_DIR = join(HOME, ".openclaw");
56
+ function resolveStatePath(filename) {
57
+ const paths = ldmPaths();
58
+ const ldmPath = join(paths.state, filename);
59
+ if (existsSync(ldmPath)) return ldmPath;
60
+ const legacyPath = join(LEGACY_OC_DIR, "memory", filename);
61
+ if (existsSync(legacyPath)) return legacyPath;
62
+ return ldmPath;
63
+ }
64
+ function stateWritePath(filename) {
65
+ const paths = ldmPaths();
66
+ const dir = paths.state;
67
+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
68
+ return join(dir, filename);
69
+ }
70
+ function resolveSecretPath(filename) {
71
+ const paths = ldmPaths();
72
+ const ldmPath = join(paths.secrets, filename);
73
+ if (existsSync(ldmPath)) return ldmPath;
74
+ const legacyPath = join(LEGACY_OC_DIR, "secrets", filename);
75
+ if (existsSync(legacyPath)) return legacyPath;
76
+ return ldmPath;
77
+ }
78
+
79
+ // src/role.ts
80
+ var STATE_FILE = "crystal-role.json";
81
+ function loadRoleState() {
82
+ try {
83
+ const path = resolveStatePath(STATE_FILE);
84
+ if (existsSync2(path)) {
85
+ return JSON.parse(readFileSync2(path, "utf-8"));
86
+ }
87
+ } catch {
88
+ }
89
+ return null;
90
+ }
91
+ function saveRoleState(state) {
92
+ const writePath = stateWritePath(STATE_FILE);
93
+ writeFileSync2(writePath, JSON.stringify(state, null, 2) + "\n");
94
+ }
95
+ function hasLocalEmbeddingProvider() {
96
+ if (process.env.OPENAI_API_KEY) return true;
97
+ if (process.env.GOOGLE_API_KEY && process.env.CRYSTAL_EMBEDDING_PROVIDER === "google") return true;
98
+ if (process.env.CRYSTAL_EMBEDDING_PROVIDER === "ollama") return true;
99
+ return false;
100
+ }
101
+ function hasRelayKey() {
102
+ const keyPath = resolveSecretPath("crystal-relay-key");
103
+ return existsSync2(keyPath);
104
+ }
105
+ function detectRole() {
106
+ const agentId = getAgentId();
107
+ const paths = ldmPaths(agentId);
108
+ const relayUrl = process.env.CRYSTAL_RELAY_URL || null;
109
+ const relayToken = !!process.env.CRYSTAL_RELAY_TOKEN;
110
+ const relayKeyExists = hasRelayKey();
111
+ const localEmbeddings = hasLocalEmbeddingProvider();
112
+ const localDb = existsSync2(paths.crystalDb);
113
+ const state = loadRoleState();
114
+ if (state && state.override) {
115
+ return {
116
+ role: state.role,
117
+ source: "state-file",
118
+ relayUrl,
119
+ relayToken,
120
+ relayKeyExists,
121
+ agentId,
122
+ hasLocalEmbeddings: localEmbeddings,
123
+ hasLocalDb: localDb
124
+ };
125
+ }
126
+ let role = "core";
127
+ if ((relayUrl || relayKeyExists) && !localEmbeddings) {
128
+ role = "node";
129
+ } else if ((relayUrl || relayKeyExists) && localEmbeddings) {
130
+ role = "core";
131
+ }
132
+ return {
133
+ role,
134
+ source: "auto-detected",
135
+ relayUrl,
136
+ relayToken,
137
+ relayKeyExists,
138
+ agentId,
139
+ hasLocalEmbeddings: localEmbeddings,
140
+ hasLocalDb: localDb
141
+ };
142
+ }
143
+ function promoteToCore() {
144
+ saveRoleState({
145
+ role: "core",
146
+ override: true,
147
+ agentId: getAgentId(),
148
+ setAt: (/* @__PURE__ */ new Date()).toISOString()
149
+ });
150
+ }
151
+ function demoteToNode(relayUrl) {
152
+ saveRoleState({
153
+ role: "node",
154
+ override: true,
155
+ relayUrl,
156
+ agentId: getAgentId(),
157
+ setAt: (/* @__PURE__ */ new Date()).toISOString()
158
+ });
159
+ }
8
160
  export {
9
161
  demoteToNode,
10
162
  detectRole,
package/dist/staging.js CHANGED
@@ -1,14 +1,239 @@
1
+ // src/staging.ts
1
2
  import {
2
- ensureStaging,
3
- hasStagedData,
4
- isNewAgent,
5
- listStagedAgents,
6
- markReady,
7
- processAllStaged,
8
- processStagedAgent,
9
- stagingPaths
10
- } from "./chunk-5I7GMRDN.js";
11
- import "./chunk-DFQ72B7M.js";
3
+ existsSync as existsSync2,
4
+ mkdirSync as mkdirSync2,
5
+ readdirSync as readdirSync2,
6
+ renameSync,
7
+ unlinkSync,
8
+ writeFileSync as writeFileSync2
9
+ } from "fs";
10
+ import { join as join2 } from "path";
11
+
12
+ // src/ldm.ts
13
+ import { existsSync, mkdirSync, readFileSync, writeFileSync, copyFileSync, chmodSync, readdirSync } from "fs";
14
+ import { join, dirname } from "path";
15
+ import { execSync } from "child_process";
16
+ import { fileURLToPath } from "url";
17
+ var HOME = process.env.HOME || "";
18
+ var LDM_ROOT = join(HOME, ".ldm");
19
+ function loadAgentConfig(id) {
20
+ const cfgPath = join(LDM_ROOT, "agents", id, "config.json");
21
+ try {
22
+ if (existsSync(cfgPath)) return JSON.parse(readFileSync(cfgPath, "utf-8"));
23
+ } catch {
24
+ }
25
+ return null;
26
+ }
27
+ function getAgentId(harnessHint) {
28
+ if (process.env.CRYSTAL_AGENT_ID) return process.env.CRYSTAL_AGENT_ID;
29
+ const agentsDir = join(LDM_ROOT, "agents");
30
+ if (existsSync(agentsDir)) {
31
+ try {
32
+ for (const d of readdirSync(agentsDir)) {
33
+ const cfg = loadAgentConfig(d);
34
+ if (!cfg || !cfg.agentId) continue;
35
+ if (!harnessHint) return cfg.agentId;
36
+ if (harnessHint === "claude-code" && cfg.harness === "claude-code-cli") return cfg.agentId;
37
+ if (harnessHint === "openclaw" && cfg.harness === "openclaw") return cfg.agentId;
38
+ }
39
+ } catch {
40
+ }
41
+ }
42
+ return harnessHint === "openclaw" ? "oc-lesa-mini" : "cc-mini";
43
+ }
44
+ function ldmPaths(agentId) {
45
+ const id = agentId || getAgentId();
46
+ const agentRoot = join(LDM_ROOT, "agents", id);
47
+ return {
48
+ root: LDM_ROOT,
49
+ bin: join(LDM_ROOT, "bin"),
50
+ secrets: join(LDM_ROOT, "secrets"),
51
+ state: join(LDM_ROOT, "state"),
52
+ config: join(LDM_ROOT, "config.json"),
53
+ crystalDb: join(LDM_ROOT, "memory", "crystal.db"),
54
+ crystalLance: join(LDM_ROOT, "memory", "lance"),
55
+ agentRoot,
56
+ transcripts: join(agentRoot, "memory", "transcripts"),
57
+ sessions: join(agentRoot, "memory", "sessions"),
58
+ daily: join(agentRoot, "memory", "daily"),
59
+ journals: join(agentRoot, "memory", "journals"),
60
+ workspace: join(agentRoot, "memory", "workspace")
61
+ };
62
+ }
63
+ function loadConfig() {
64
+ const configPath = join(LDM_ROOT, "config.json");
65
+ try {
66
+ if (existsSync(configPath)) {
67
+ return JSON.parse(readFileSync(configPath, "utf-8"));
68
+ }
69
+ } catch {
70
+ }
71
+ return null;
72
+ }
73
+ function saveConfig(config) {
74
+ const configPath = join(LDM_ROOT, "config.json");
75
+ writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n");
76
+ }
77
+ function scaffoldLdm(agentId) {
78
+ const paths = ldmPaths(agentId);
79
+ mkdirSync(join(paths.root, "memory"), { recursive: true });
80
+ mkdirSync(paths.crystalLance, { recursive: true });
81
+ mkdirSync(paths.bin, { recursive: true });
82
+ mkdirSync(paths.secrets, { recursive: true, mode: 448 });
83
+ mkdirSync(paths.state, { recursive: true });
84
+ mkdirSync(paths.transcripts, { recursive: true });
85
+ mkdirSync(paths.sessions, { recursive: true });
86
+ mkdirSync(paths.daily, { recursive: true });
87
+ mkdirSync(paths.journals, { recursive: true });
88
+ mkdirSync(paths.workspace, { recursive: true });
89
+ const id = agentId || getAgentId();
90
+ let config = loadConfig();
91
+ if (!config) {
92
+ config = {
93
+ version: "1.0.0",
94
+ agents: [id],
95
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
96
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
97
+ };
98
+ } else {
99
+ if (!config.agents.includes(id)) {
100
+ config.agents.push(id);
101
+ }
102
+ config.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
103
+ }
104
+ saveConfig(config);
105
+ return paths;
106
+ }
107
+ var LEGACY_OC_DIR = join(HOME, ".openclaw");
108
+ function ensureLdm(agentId) {
109
+ const paths = ldmPaths(agentId);
110
+ if (existsSync(paths.transcripts) && existsSync(paths.config)) {
111
+ return paths;
112
+ }
113
+ return scaffoldLdm(agentId);
114
+ }
115
+
116
+ // src/staging.ts
117
+ import { execSync as execSync2 } from "child_process";
118
+ var HOME2 = process.env.HOME || "";
119
+ var STAGING_ROOT = join2(HOME2, ".ldm", "staging");
120
+ function stagingPaths(agentId) {
121
+ const root = join2(STAGING_ROOT, agentId);
122
+ return {
123
+ root,
124
+ transcripts: join2(root, "transcripts"),
125
+ readyFile: join2(root, "READY")
126
+ };
127
+ }
128
+ function ensureStaging(agentId) {
129
+ const paths = stagingPaths(agentId);
130
+ mkdirSync2(paths.transcripts, { recursive: true });
131
+ return paths;
132
+ }
133
+ function markReady(agentId) {
134
+ const paths = stagingPaths(agentId);
135
+ writeFileSync2(paths.readyFile, JSON.stringify({
136
+ markedAt: (/* @__PURE__ */ new Date()).toISOString(),
137
+ agentId
138
+ }));
139
+ }
140
+ function isNewAgent(agentId) {
141
+ const paths = ldmPaths(agentId);
142
+ return !existsSync2(paths.agentRoot);
143
+ }
144
+ function hasStagedData(agentId) {
145
+ const paths = stagingPaths(agentId);
146
+ return existsSync2(paths.readyFile);
147
+ }
148
+ function listStagedAgents() {
149
+ if (!existsSync2(STAGING_ROOT)) return [];
150
+ const agents = [];
151
+ for (const entry of readdirSync2(STAGING_ROOT, { withFileTypes: true })) {
152
+ if (!entry.isDirectory()) continue;
153
+ if (existsSync2(join2(STAGING_ROOT, entry.name, "READY"))) {
154
+ agents.push(entry.name);
155
+ }
156
+ }
157
+ return agents;
158
+ }
159
+ async function processStagedAgent(agentId) {
160
+ const startTime = Date.now();
161
+ const staging = stagingPaths(agentId);
162
+ if (!existsSync2(staging.readyFile)) {
163
+ throw new Error(`No READY file for agent ${agentId}`);
164
+ }
165
+ const agentPaths = ensureLdm(agentId);
166
+ const stagedFiles = existsSync2(staging.transcripts) ? readdirSync2(staging.transcripts).filter((f) => f.endsWith(".jsonl")) : [];
167
+ if (stagedFiles.length === 0) {
168
+ unlinkSync(staging.readyFile);
169
+ return {
170
+ agentId,
171
+ transcriptsProcessed: 0,
172
+ backfillChunks: 0,
173
+ dreamWeaverRan: false,
174
+ durationMs: Date.now() - startTime
175
+ };
176
+ }
177
+ for (const file of stagedFiles) {
178
+ const src = join2(staging.transcripts, file);
179
+ const dest = join2(agentPaths.transcripts, file);
180
+ try {
181
+ renameSync(src, dest);
182
+ } catch {
183
+ const { copyFileSync: copyFileSync2 } = await import("fs");
184
+ copyFileSync2(src, dest);
185
+ unlinkSync(src);
186
+ }
187
+ }
188
+ let backfillChunks = 0;
189
+ let dreamWeaverRan = false;
190
+ try {
191
+ const output = execSync2(
192
+ `crystal backfill --agent ${agentId}`,
193
+ { encoding: "utf-8", timeout: 6e5 }
194
+ );
195
+ const match = output.match(/(\d+) chunks embedded/);
196
+ if (match) backfillChunks = parseInt(match[1], 10);
197
+ } catch (err) {
198
+ process.stderr.write(`[staging] backfill failed for ${agentId}: ${err.message}
199
+ `);
200
+ }
201
+ try {
202
+ execSync2(
203
+ `crystal dream-weave --agent ${agentId} --mode full`,
204
+ { encoding: "utf-8", timeout: 6e5 }
205
+ );
206
+ dreamWeaverRan = true;
207
+ } catch (err) {
208
+ process.stderr.write(`[staging] dream-weave failed for ${agentId}: ${err.message}
209
+ `);
210
+ }
211
+ try {
212
+ unlinkSync(staging.readyFile);
213
+ } catch {
214
+ }
215
+ return {
216
+ agentId,
217
+ transcriptsProcessed: stagedFiles.length,
218
+ backfillChunks,
219
+ dreamWeaverRan,
220
+ durationMs: Date.now() - startTime
221
+ };
222
+ }
223
+ async function processAllStaged() {
224
+ const agents = listStagedAgents();
225
+ const results = [];
226
+ for (const agentId of agents) {
227
+ try {
228
+ const result = await processStagedAgent(agentId);
229
+ results.push(result);
230
+ } catch (err) {
231
+ process.stderr.write(`[staging] failed to process ${agentId}: ${err.message}
232
+ `);
233
+ }
234
+ }
235
+ return results;
236
+ }
12
237
  export {
13
238
  ensureStaging,
14
239
  hasStagedData,
package/dist/summarize.js CHANGED
@@ -1,8 +1,145 @@
1
- import {
2
- formatSummaryMarkdown,
3
- generateSessionSummary,
4
- writeSummaryFile
5
- } from "./chunk-Y72C7F6O.js";
1
+ // src/summarize.ts
2
+ import { writeFileSync, existsSync, mkdirSync } from "fs";
3
+ import { join } from "path";
4
+ import https from "https";
5
+ import http from "http";
6
+ var SUMMARY_MODE = process.env.CRYSTAL_SUMMARY_MODE || "simple";
7
+ var SUMMARY_PROVIDER = process.env.CRYSTAL_SUMMARY_PROVIDER || "openai";
8
+ var SUMMARY_MODEL = process.env.CRYSTAL_SUMMARY_MODEL || "gpt-4o-mini";
9
+ function generateSimpleSummary(messages) {
10
+ const firstUser = messages.find((m) => m.role === "user");
11
+ const title = firstUser ? firstUser.text.slice(0, 80).replace(/\n/g, " ").trim() : "Untitled Session";
12
+ const slug = title.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "").slice(0, 50);
13
+ const preview = messages.slice(0, 10).map((m) => {
14
+ const roleLabel = m.role === "user" ? "User" : "Assistant";
15
+ const snippet = m.text.slice(0, 200).replace(/\n/g, " ").trim();
16
+ return `**${roleLabel}:** ${snippet}${m.text.length > 200 ? "..." : ""}`;
17
+ }).join("\n\n");
18
+ const date = messages[0]?.timestamp?.slice(0, 10) || (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
19
+ return {
20
+ title,
21
+ slug,
22
+ summary: preview,
23
+ topics: [],
24
+ messageCount: messages.length,
25
+ date
26
+ };
27
+ }
28
+ async function generateLlmSummary(messages) {
29
+ const condensed = messages.slice(0, 30).map((m) => {
30
+ const roleLabel = m.role === "user" ? "User" : "Assistant";
31
+ const text = m.text.slice(0, 500);
32
+ return `${roleLabel}: ${text}`;
33
+ }).join("\n\n");
34
+ const prompt = `Summarize this conversation. Return JSON only, no markdown fences.
35
+
36
+ Format:
37
+ {"title": "short title", "slug": "url-safe-slug", "summary": "2-4 sentences", "topics": ["topic1", "topic2"]}
38
+
39
+ Conversation:
40
+ ${condensed}`;
41
+ const apiKey = process.env.OPENAI_API_KEY;
42
+ if (!apiKey) {
43
+ return generateSimpleSummary(messages);
44
+ }
45
+ try {
46
+ const body = JSON.stringify({
47
+ model: SUMMARY_MODEL,
48
+ messages: [{ role: "user", content: prompt }],
49
+ temperature: 0.3,
50
+ max_tokens: 300
51
+ });
52
+ const result = await httpPost("https://api.openai.com/v1/chat/completions", body, {
53
+ "Authorization": `Bearer ${apiKey}`,
54
+ "Content-Type": "application/json"
55
+ });
56
+ const parsed = JSON.parse(result);
57
+ const content = parsed.choices?.[0]?.message?.content || "";
58
+ const jsonStr = content.replace(/```json?\n?/g, "").replace(/```/g, "").trim();
59
+ const data = JSON.parse(jsonStr);
60
+ const date = messages[0]?.timestamp?.slice(0, 10) || (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
61
+ return {
62
+ title: data.title || "Untitled",
63
+ slug: (data.slug || "untitled").slice(0, 50),
64
+ summary: data.summary || "",
65
+ topics: data.topics || [],
66
+ messageCount: messages.length,
67
+ date
68
+ };
69
+ } catch {
70
+ return generateSimpleSummary(messages);
71
+ }
72
+ }
73
+ function httpPost(url, body, headers) {
74
+ return new Promise((resolve, reject) => {
75
+ const parsed = new URL(url);
76
+ const client = parsed.protocol === "https:" ? https : http;
77
+ const req = client.request({
78
+ hostname: parsed.hostname,
79
+ port: parsed.port,
80
+ path: parsed.pathname + parsed.search,
81
+ method: "POST",
82
+ headers: { ...headers, "Content-Length": Buffer.byteLength(body) },
83
+ timeout: 3e4
84
+ }, (res) => {
85
+ let data = "";
86
+ res.on("data", (chunk) => {
87
+ data += chunk;
88
+ });
89
+ res.on("end", () => {
90
+ if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
91
+ resolve(data);
92
+ } else {
93
+ reject(new Error(`HTTP ${res.statusCode}: ${data.slice(0, 200)}`));
94
+ }
95
+ });
96
+ });
97
+ req.on("error", reject);
98
+ req.on("timeout", () => {
99
+ req.destroy();
100
+ reject(new Error("Request timeout"));
101
+ });
102
+ req.write(body);
103
+ req.end();
104
+ });
105
+ }
106
+ async function generateSessionSummary(messages) {
107
+ if (SUMMARY_MODE === "llm") {
108
+ return generateLlmSummary(messages);
109
+ }
110
+ return generateSimpleSummary(messages);
111
+ }
112
+ function formatSummaryMarkdown(summary, sessionId) {
113
+ const lines = [];
114
+ lines.push(`# ${summary.title}`);
115
+ lines.push("");
116
+ lines.push(`**Session:** ${sessionId} **Date:** ${summary.date} **Messages:** ${summary.messageCount}`);
117
+ lines.push("");
118
+ lines.push("## Summary");
119
+ lines.push("");
120
+ lines.push(summary.summary);
121
+ if (summary.topics.length > 0) {
122
+ lines.push("");
123
+ lines.push("## Key Topics");
124
+ lines.push("");
125
+ for (const topic of summary.topics) {
126
+ lines.push(`- ${topic}`);
127
+ }
128
+ }
129
+ lines.push("");
130
+ return lines.join("\n");
131
+ }
132
+ function writeSummaryFile(sessionsDir, summary, agentId, sessionId) {
133
+ if (!existsSync(sessionsDir)) mkdirSync(sessionsDir, { recursive: true });
134
+ const now = /* @__PURE__ */ new Date();
135
+ const dateStr = now.toISOString().slice(0, 10);
136
+ const timeStr = now.toISOString().slice(11, 19).replace(/:/g, "-");
137
+ const filename = `${dateStr}--${timeStr}--${agentId}--${summary.slug}.md`;
138
+ const filepath = join(sessionsDir, filename);
139
+ const content = formatSummaryMarkdown(summary, sessionId);
140
+ writeFileSync(filepath, content);
141
+ return filepath;
142
+ }
6
143
  export {
7
144
  formatSummaryMarkdown,
8
145
  generateSessionSummary,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wipcomputer/memory-crystal",
3
- "version": "0.7.34-alpha.2",
3
+ "version": "0.7.34-alpha.4",
4
4
  "description": "Sovereign memory system — local-first with ephemeral encrypted relay. Your memory, your machine, your rules.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -19,7 +19,10 @@
19
19
  },
20
20
  "files": [
21
21
  "dist/",
22
- "scripts/",
22
+ "scripts/crystal-capture.sh",
23
+ "scripts/deploy-cloud.sh",
24
+ "scripts/ldm-backup.sh",
25
+ "scripts/migrate-lance-to-sqlite.mjs",
23
26
  "skills/",
24
27
  "migrations/",
25
28
  "cloud/",
@@ -32,8 +35,8 @@
32
35
  ],
33
36
  "scripts": {
34
37
  "prepublishOnly": "npm run build",
35
- "build": "tsup src/core.ts src/cli.ts src/mcp-server.ts src/openclaw.ts src/migrate.ts src/cc-hook.ts src/cc-poller.ts src/crypto.ts src/pair.ts src/poller.ts src/mirror-sync.ts src/file-sync.ts src/ldm.ts src/summarize.ts src/role.ts src/doctor.ts src/bridge.ts src/discover.ts src/bulk-copy.ts src/oc-backfill.ts src/dream-weaver.ts src/crystal-serve.ts src/staging.ts src/installer.ts --format esm --dts --outDir dist && tsup src/worker.ts --format esm --outDir dist --no-dts && cp scripts/crystal-capture.sh scripts/ldm-backup.sh dist/",
36
- "build:local": "tsup src/core.ts src/cli.ts src/mcp-server.ts src/openclaw.ts src/migrate.ts src/cc-hook.ts src/cc-poller.ts src/crypto.ts src/pair.ts src/poller.ts src/mirror-sync.ts src/file-sync.ts src/ldm.ts src/summarize.ts src/role.ts src/doctor.ts src/bridge.ts src/discover.ts src/bulk-copy.ts src/oc-backfill.ts src/dream-weaver.ts src/crystal-serve.ts src/staging.ts src/installer.ts --format esm --dts --outDir dist",
38
+ "build": "tsup src/core.ts src/cli.ts src/mcp-server.ts src/openclaw.ts src/migrate.ts src/cc-hook.ts src/cc-poller.ts src/crypto.ts src/pair.ts src/poller.ts src/mirror-sync.ts src/file-sync.ts src/ldm.ts src/summarize.ts src/role.ts src/doctor.ts src/bridge.ts src/discover.ts src/bulk-copy.ts src/oc-backfill.ts src/dream-weaver.ts src/crystal-serve.ts src/staging.ts src/installer.ts --format esm --dts --outDir dist --no-splitting --clean && tsup src/worker.ts --format esm --outDir dist --no-dts --no-splitting && cp scripts/crystal-capture.sh scripts/ldm-backup.sh dist/",
39
+ "build:local": "tsup src/core.ts src/cli.ts src/mcp-server.ts src/openclaw.ts src/migrate.ts src/cc-hook.ts src/cc-poller.ts src/crypto.ts src/pair.ts src/poller.ts src/mirror-sync.ts src/file-sync.ts src/ldm.ts src/summarize.ts src/role.ts src/doctor.ts src/bridge.ts src/discover.ts src/bulk-copy.ts src/oc-backfill.ts src/dream-weaver.ts src/crystal-serve.ts src/staging.ts src/installer.ts --format esm --dts --outDir dist --no-splitting --clean",
37
40
  "build:worker": "tsup src/worker.ts --format esm --outDir dist --no-dts",
38
41
  "build:cloud": "tsup src/worker-mcp.ts src/cloud-crystal.ts --format esm --outDir dist --no-dts",
39
42
  "deploy:cloud": "bash -c 'git diff --quiet HEAD -- src/ wrangler-mcp.toml || (echo \"ERROR: uncommitted changes. commit before deploying.\" && exit 1)' && wrangler deploy --config wrangler-mcp.toml",