@wipcomputer/memory-crystal 0.7.29 → 0.7.32

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 (146) hide show
  1. package/CHANGELOG.md +75 -0
  2. package/SKILL.md +1 -1
  3. package/package.json +1 -1
  4. package/scripts/migrate-lance-to-sqlite.mjs +2 -1
  5. package/_trash/RELEASE-NOTES-v0-7-23.md +0 -48
  6. package/_trash/RELEASE-NOTES-v0-7-25.md +0 -24
  7. package/_trash/RELEASE-NOTES-v0-7-26.md +0 -7
  8. package/_trash/RELEASE-NOTES-v0-7-28.md +0 -31
  9. package/_trash/RELEASE-NOTES-v0-7-29.md +0 -28
  10. package/_trash/RELEASE-NOTES-v0-7-4.md +0 -64
  11. package/_trash/RELEASE-NOTES-v0-7-5.md +0 -19
  12. package/dist/bridge.d.ts +0 -7
  13. package/dist/bridge.js +0 -14
  14. package/dist/bulk-copy.d.ts +0 -17
  15. package/dist/bulk-copy.js +0 -90
  16. package/dist/cc-hook.d.ts +0 -8
  17. package/dist/cc-hook.js +0 -368
  18. package/dist/cc-poller.d.ts +0 -1
  19. package/dist/cc-poller.js +0 -550
  20. package/dist/chunk-25LXQJ4Z.js +0 -110
  21. package/dist/chunk-2DRXIRQW.js +0 -97
  22. package/dist/chunk-2ZNH5F6E.js +0 -1281
  23. package/dist/chunk-3G3SFYYI.js +0 -288
  24. package/dist/chunk-3RG5ZIWI.js +0 -10
  25. package/dist/chunk-3S6TI23B.js +0 -97
  26. package/dist/chunk-3VFIJYS4.js +0 -818
  27. package/dist/chunk-52QE3YI3.js +0 -1169
  28. package/dist/chunk-57RP3DIN.js +0 -1205
  29. package/dist/chunk-5HSZ4W2P.js +0 -62
  30. package/dist/chunk-645IPXW3.js +0 -290
  31. package/dist/chunk-7A7ELD4C.js +0 -1205
  32. package/dist/chunk-7FYY4GZM.js +0 -1205
  33. package/dist/chunk-7IUE7ODU.js +0 -254
  34. package/dist/chunk-7RMLKZIS.js +0 -108
  35. package/dist/chunk-AA3OPP4Z.js +0 -432
  36. package/dist/chunk-AEWLSYPH.js +0 -72
  37. package/dist/chunk-ASSZDR6I.js +0 -108
  38. package/dist/chunk-AYRJVWUC.js +0 -1205
  39. package/dist/chunk-CCYI5O3D.js +0 -148
  40. package/dist/chunk-D3I3ZSE2.js +0 -411
  41. package/dist/chunk-DACSKLY6.js +0 -219
  42. package/dist/chunk-DW5B4BL7.js +0 -108
  43. package/dist/chunk-EKSACBTJ.js +0 -1070
  44. package/dist/chunk-EXEZZADG.js +0 -248
  45. package/dist/chunk-F3Y7EL7K.js +0 -83
  46. package/dist/chunk-FBQWSDPC.js +0 -1328
  47. package/dist/chunk-FHRZNOMW.js +0 -1205
  48. package/dist/chunk-IM7N24MT.js +0 -129
  49. package/dist/chunk-IPNYIXFK.js +0 -1178
  50. package/dist/chunk-J7MRSZIO.js +0 -167
  51. package/dist/chunk-JITKI2OI.js +0 -106
  52. package/dist/chunk-JWZXYVET.js +0 -1068
  53. package/dist/chunk-KCQUXVYT.js +0 -108
  54. package/dist/chunk-KOQ43OX6.js +0 -1281
  55. package/dist/chunk-KYVWO6ZM.js +0 -1069
  56. package/dist/chunk-L3VHARQH.js +0 -413
  57. package/dist/chunk-LBWDS6BE.js +0 -288
  58. package/dist/chunk-LOVAHSQV.js +0 -411
  59. package/dist/chunk-LQOYCAGG.js +0 -446
  60. package/dist/chunk-LWAIPJ2W.js +0 -146
  61. package/dist/chunk-M5DHKW7M.js +0 -127
  62. package/dist/chunk-MBKCIJHM.js +0 -1328
  63. package/dist/chunk-MK42FMEG.js +0 -147
  64. package/dist/chunk-MOBMYHKL.js +0 -1205
  65. package/dist/chunk-MPLTNMRG.js +0 -67
  66. package/dist/chunk-NIJCVN3O.js +0 -147
  67. package/dist/chunk-NZCFSZQ7.js +0 -1205
  68. package/dist/chunk-O2UITJGH.js +0 -465
  69. package/dist/chunk-OCRA44AZ.js +0 -108
  70. package/dist/chunk-P3KJR66H.js +0 -117
  71. package/dist/chunk-PEK6JH65.js +0 -432
  72. package/dist/chunk-PJ6FFKEX.js +0 -77
  73. package/dist/chunk-PLUBBZYR.js +0 -800
  74. package/dist/chunk-PNKVD2UK.js +0 -26
  75. package/dist/chunk-PSQZURHO.js +0 -229
  76. package/dist/chunk-SGL6ISBJ.js +0 -1061
  77. package/dist/chunk-SJABZZT5.js +0 -97
  78. package/dist/chunk-TD3P3K32.js +0 -1199
  79. package/dist/chunk-TMDZJJKV.js +0 -288
  80. package/dist/chunk-UNHVZB5G.js +0 -411
  81. package/dist/chunk-VAFTWSTE.js +0 -1061
  82. package/dist/chunk-VNFXFQBB.js +0 -217
  83. package/dist/chunk-X3GVFKSJ.js +0 -1205
  84. package/dist/chunk-XZ3S56RQ.js +0 -1061
  85. package/dist/chunk-Y72C7F6O.js +0 -148
  86. package/dist/chunk-YLICP577.js +0 -1205
  87. package/dist/chunk-YX6AXLVK.js +0 -159
  88. package/dist/chunk-ZCQYHTNU.js +0 -146
  89. package/dist/cli.d.ts +0 -1
  90. package/dist/cli.js +0 -1105
  91. package/dist/cloud-crystal.js +0 -6
  92. package/dist/core.d.ts +0 -232
  93. package/dist/core.js +0 -12
  94. package/dist/crypto.d.ts +0 -20
  95. package/dist/crypto.js +0 -27
  96. package/dist/crystal-capture.sh +0 -29
  97. package/dist/crystal-serve.d.ts +0 -4
  98. package/dist/crystal-serve.js +0 -252
  99. package/dist/dev-update-SZ2Z4WCQ.js +0 -6
  100. package/dist/discover.d.ts +0 -30
  101. package/dist/discover.js +0 -177
  102. package/dist/doctor.d.ts +0 -9
  103. package/dist/doctor.js +0 -334
  104. package/dist/dream-weaver.d.ts +0 -8
  105. package/dist/dream-weaver.js +0 -56
  106. package/dist/file-sync.d.ts +0 -48
  107. package/dist/file-sync.js +0 -18
  108. package/dist/installer.d.ts +0 -61
  109. package/dist/installer.js +0 -676
  110. package/dist/ldm-backup.sh +0 -116
  111. package/dist/ldm.d.ts +0 -50
  112. package/dist/ldm.js +0 -32
  113. package/dist/mcp-server.d.ts +0 -1
  114. package/dist/mcp-server.js +0 -265
  115. package/dist/migrate.d.ts +0 -1
  116. package/dist/migrate.js +0 -89
  117. package/dist/mirror-sync.d.ts +0 -1
  118. package/dist/mirror-sync.js +0 -159
  119. package/dist/oc-backfill.d.ts +0 -19
  120. package/dist/oc-backfill.js +0 -74
  121. package/dist/openclaw.d.ts +0 -5
  122. package/dist/openclaw.js +0 -423
  123. package/dist/pair.d.ts +0 -4
  124. package/dist/pair.js +0 -75
  125. package/dist/poller.d.ts +0 -1
  126. package/dist/poller.js +0 -634
  127. package/dist/role.d.ts +0 -24
  128. package/dist/role.js +0 -13
  129. package/dist/search-pipeline-4K4OJSSS.js +0 -255
  130. package/dist/search-pipeline-4PRS6LI7.js +0 -280
  131. package/dist/search-pipeline-7UJMXPLO.js +0 -280
  132. package/dist/search-pipeline-DQTRLGBH.js +0 -74
  133. package/dist/search-pipeline-HNG37REH.js +0 -282
  134. package/dist/search-pipeline-IZFPLBUB.js +0 -280
  135. package/dist/search-pipeline-MID6F26Q.js +0 -73
  136. package/dist/search-pipeline-N52JZFNN.js +0 -282
  137. package/dist/search-pipeline-OPB2PRQQ.js +0 -280
  138. package/dist/search-pipeline-VXTE5HAD.js +0 -262
  139. package/dist/search-pipeline-XHFKADRG.js +0 -73
  140. package/dist/staging.d.ts +0 -29
  141. package/dist/staging.js +0 -21
  142. package/dist/summarize.d.ts +0 -19
  143. package/dist/summarize.js +0 -10
  144. package/dist/worker-demo.js +0 -186
  145. package/dist/worker-mcp.js +0 -404
  146. package/dist/worker.js +0 -137
package/dist/pair.js DELETED
@@ -1,75 +0,0 @@
1
- import {
2
- RELAY_KEY_PATH,
3
- decodePairingString,
4
- encodePairingString,
5
- generateRelayKey,
6
- loadRelayKey
7
- } from "./chunk-KCQUXVYT.js";
8
- import "./chunk-EXEZZADG.js";
9
-
10
- // src/pair.ts
11
- import { existsSync, writeFileSync, mkdirSync, chmodSync } from "fs";
12
- import { dirname } from "path";
13
- import qrcode from "qrcode-terminal";
14
- function generateQR(text) {
15
- return new Promise((resolve) => {
16
- qrcode.generate(text, { small: true }, (code) => {
17
- resolve(code);
18
- });
19
- });
20
- }
21
- function saveKey(key) {
22
- const dir = dirname(RELAY_KEY_PATH);
23
- if (!existsSync(dir)) {
24
- mkdirSync(dir, { recursive: true });
25
- }
26
- writeFileSync(RELAY_KEY_PATH, key.toString("base64") + "\n", { mode: 384 });
27
- try {
28
- chmodSync(RELAY_KEY_PATH, 384);
29
- } catch {
30
- }
31
- }
32
- async function pairShow() {
33
- let key;
34
- try {
35
- key = loadRelayKey();
36
- console.log("Relay key found.\n");
37
- } catch {
38
- console.log("No relay key found. Generating one...");
39
- key = generateRelayKey();
40
- saveKey(key);
41
- console.log(`Key saved to ${RELAY_KEY_PATH}
42
- `);
43
- }
44
- const pairingString = encodePairingString(key);
45
- console.log("Scan this QR code from your other device:\n");
46
- const qr = await generateQR(pairingString);
47
- console.log(qr);
48
- console.log("Or copy this pairing code:\n");
49
- console.log(` ${pairingString}
50
- `);
51
- console.log("On the other device, run:");
52
- console.log(` crystal pair --code ${pairingString}
53
- `);
54
- }
55
- function pairReceive(code) {
56
- const key = decodePairingString(code);
57
- if (existsSync(RELAY_KEY_PATH)) {
58
- try {
59
- const existing = loadRelayKey();
60
- if (existing.equals(key)) {
61
- console.log("This device already has the same key. Nothing to do.");
62
- return;
63
- }
64
- console.log("Replacing existing relay key with new key from pairing code.");
65
- } catch {
66
- }
67
- }
68
- saveKey(key);
69
- console.log(`Key received and saved to ${RELAY_KEY_PATH}`);
70
- console.log("Relay encryption is now active on this device.");
71
- }
72
- export {
73
- pairReceive,
74
- pairShow
75
- };
package/dist/poller.d.ts DELETED
@@ -1 +0,0 @@
1
- #!/usr/bin/env node
package/dist/poller.js DELETED
@@ -1,634 +0,0 @@
1
- #!/usr/bin/env node
2
- import {
3
- ensureStaging,
4
- isNewAgent,
5
- markReady
6
- } from "./chunk-ZCQYHTNU.js";
7
- import {
8
- generateSessionSummary,
9
- writeSummaryFile
10
- } from "./chunk-Y72C7F6O.js";
11
- import {
12
- pushFileSync
13
- } from "./chunk-3G3SFYYI.js";
14
- import {
15
- Crystal,
16
- resolveConfig
17
- } from "./chunk-FBQWSDPC.js";
18
- import {
19
- decryptJSON,
20
- encryptJSON,
21
- loadRelayKey
22
- } from "./chunk-KCQUXVYT.js";
23
- import {
24
- ensureLdm,
25
- resolveStatePath,
26
- stateWritePath
27
- } from "./chunk-EXEZZADG.js";
28
-
29
- // src/poller.ts
30
- import { readFileSync, writeFileSync, appendFileSync, existsSync, mkdirSync } from "fs";
31
- import { join } from "path";
32
- import { createDecipheriv } from "crypto";
33
- var RELAY_URL = process.env.CRYSTAL_RELAY_URL || "";
34
- var RELAY_TOKEN = process.env.CRYSTAL_RELAY_TOKEN || "";
35
- var POLLER_STATE_PATH = resolveStatePath("relay-poller-state.json");
36
- function loadState() {
37
- try {
38
- if (existsSync(POLLER_STATE_PATH)) {
39
- const raw = JSON.parse(readFileSync(POLLER_STATE_PATH, "utf-8"));
40
- return {
41
- lastPoll: raw.lastPoll ?? null,
42
- totalIngested: raw.totalIngested ?? 0,
43
- totalChatgptIngested: raw.totalChatgptIngested ?? 0,
44
- totalAttachments: raw.totalAttachments ?? 0,
45
- lastMirrorPush: raw.lastMirrorPush ?? null,
46
- lastDeltaChunkId: raw.lastDeltaChunkId ?? 0
47
- };
48
- }
49
- } catch {
50
- }
51
- return { lastPoll: null, totalIngested: 0, totalChatgptIngested: 0, totalAttachments: 0, lastMirrorPush: null, lastDeltaChunkId: 0 };
52
- }
53
- function saveState(state) {
54
- const writePath = stateWritePath("relay-poller-state.json");
55
- writeFileSync(writePath, JSON.stringify(state, null, 2));
56
- }
57
- async function pollConversations(crystal, relayKey) {
58
- let ingested = 0;
59
- let errors = 0;
60
- const listResp = await fetch(`${RELAY_URL}/pickup/conversations`, {
61
- headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
62
- });
63
- if (!listResp.ok) {
64
- throw new Error(`Relay list failed: ${listResp.status} ${await listResp.text()}`);
65
- }
66
- const listData = await listResp.json();
67
- if (listData.count === 0) return { ingested: 0, errors: 0 };
68
- process.stderr.write(`[relay-poller] conversations: ${listData.count} blob(s) waiting
69
- `);
70
- for (const blob of listData.blobs) {
71
- try {
72
- const blobResp = await fetch(`${RELAY_URL}/pickup/conversations/${blob.id}`, {
73
- headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
74
- });
75
- if (!blobResp.ok) {
76
- process.stderr.write(`[relay-poller] failed to fetch blob ${blob.id}: ${blobResp.status}
77
- `);
78
- errors++;
79
- continue;
80
- }
81
- const encryptedText = await blobResp.text();
82
- const encrypted = JSON.parse(encryptedText);
83
- let drop;
84
- try {
85
- drop = decryptJSON(encrypted, relayKey);
86
- } catch (err) {
87
- process.stderr.write(`[relay-poller] blob ${blob.id} failed verification: ${err.message} ... DISCARDED
88
- `);
89
- await fetch(`${RELAY_URL}/confirm/conversations/${blob.id}`, {
90
- method: "DELETE",
91
- headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
92
- });
93
- errors++;
94
- continue;
95
- }
96
- if (isNewAgent(drop.agent_id)) {
97
- process.stderr.write(`[relay-poller] new agent "${drop.agent_id}" detected, routing to staging
98
- `);
99
- const staging = ensureStaging(drop.agent_id);
100
- const jsonlPath = join(staging.transcripts, `relay-${blob.id}.jsonl`);
101
- const jsonlLines = drop.messages.map((m) => JSON.stringify(m)).join("\n") + "\n";
102
- writeFileSync(jsonlPath, jsonlLines);
103
- markReady(drop.agent_id);
104
- await fetch(`${RELAY_URL}/confirm/conversations/${blob.id}`, {
105
- method: "DELETE",
106
- headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
107
- });
108
- process.stderr.write(`[relay-poller] staged ${drop.messages.length} messages for ${drop.agent_id}
109
- `);
110
- continue;
111
- }
112
- const maxSingleChunkChars = 2e3 * 4;
113
- const chunks = [];
114
- for (const msg of drop.messages) {
115
- if (msg.text.length <= maxSingleChunkChars) {
116
- chunks.push({
117
- text: msg.text,
118
- role: msg.role,
119
- source_type: "conversation",
120
- source_id: `cc:${msg.sessionId}`,
121
- agent_id: drop.agent_id,
122
- token_count: Math.ceil(msg.text.length / 4),
123
- created_at: msg.timestamp
124
- });
125
- } else {
126
- for (const ct of crystal.chunkText(msg.text)) {
127
- chunks.push({
128
- text: ct,
129
- role: msg.role,
130
- source_type: "conversation",
131
- source_id: `cc:${msg.sessionId}`,
132
- agent_id: drop.agent_id,
133
- token_count: Math.ceil(ct.length / 4),
134
- created_at: msg.timestamp
135
- });
136
- }
137
- }
138
- }
139
- const count = await crystal.ingest(chunks);
140
- ingested += count;
141
- await fetch(`${RELAY_URL}/confirm/conversations/${blob.id}`, {
142
- method: "DELETE",
143
- headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
144
- });
145
- process.stderr.write(`[relay-poller] blob ${blob.id}: ${count} chunks ingested from ${drop.agent_id}
146
- `);
147
- try {
148
- const remotePaths = ensureLdm(drop.agent_id);
149
- const jsonlPath = join(remotePaths.transcripts, `relay-${blob.id}.jsonl`);
150
- const jsonlLines = drop.messages.map((m) => JSON.stringify(m)).join("\n") + "\n";
151
- writeFileSync(jsonlPath, jsonlLines);
152
- const summaryMsgs = drop.messages.map((m) => ({
153
- role: m.role,
154
- text: m.text,
155
- timestamp: m.timestamp,
156
- sessionId: m.sessionId
157
- }));
158
- const summary = await generateSessionSummary(summaryMsgs);
159
- const sessionId = drop.messages[0]?.sessionId || "unknown";
160
- writeSummaryFile(remotePaths.sessions, summary, drop.agent_id, sessionId);
161
- appendDailyBreadcrumb(drop.agent_id, drop.messages.find((m) => m.role === "user")?.text || "");
162
- } catch (fileErr) {
163
- process.stderr.write(`[relay-poller] file tree write failed (non-fatal): ${fileErr.message}
164
- `);
165
- }
166
- } catch (err) {
167
- process.stderr.write(`[relay-poller] error processing blob ${blob.id}: ${err.message}
168
- `);
169
- errors++;
170
- }
171
- }
172
- try {
173
- await pollCommands();
174
- } catch (err) {
175
- process.stderr.write(`[relay-poller] commands poll failed (non-fatal): ${err.message}
176
- `);
177
- }
178
- return { ingested, errors };
179
- }
180
- async function pollChatgpt(crystal, relayKey) {
181
- let ingested = 0;
182
- let attachments = 0;
183
- let errors = 0;
184
- const listResp = await fetch(`${RELAY_URL}/pickup/chatgpt`, {
185
- headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
186
- });
187
- if (!listResp.ok) {
188
- process.stderr.write(`[relay-poller] chatgpt list failed: ${listResp.status}
189
- `);
190
- return { ingested: 0, attachments: 0, errors: 1 };
191
- }
192
- const listData = await listResp.json();
193
- if (listData.count > 0) {
194
- process.stderr.write(`[relay-poller] chatgpt: ${listData.count} drop(s) waiting
195
- `);
196
- }
197
- for (const blob of listData.blobs) {
198
- try {
199
- const blobResp = await fetch(`${RELAY_URL}/pickup/chatgpt/${blob.id}`, {
200
- headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
201
- });
202
- if (!blobResp.ok) {
203
- process.stderr.write(`[relay-poller] chatgpt: failed to fetch ${blob.id}: ${blobResp.status}
204
- `);
205
- errors++;
206
- continue;
207
- }
208
- const encryptedText = await blobResp.text();
209
- const encrypted = JSON.parse(encryptedText);
210
- let drop;
211
- try {
212
- drop = decryptJSON(encrypted, relayKey);
213
- } catch (err) {
214
- process.stderr.write(`[relay-poller] chatgpt: ${blob.id} failed verification: ${err.message} ... DISCARDED
215
- `);
216
- await confirmBlob("chatgpt", blob.id);
217
- errors++;
218
- continue;
219
- }
220
- switch (drop.type) {
221
- case "conversation": {
222
- const data = drop.data;
223
- const result = await ingestConversationDrop(crystal, drop, data);
224
- ingested += result;
225
- break;
226
- }
227
- case "remember": {
228
- const data = drop.data;
229
- await crystal.remember(data.text, data.category);
230
- ingested++;
231
- process.stderr.write(`[relay-poller] chatgpt: remembered "${data.text.slice(0, 60)}..." (${data.category}) from ${drop.agent_id}
232
- `);
233
- break;
234
- }
235
- case "forget": {
236
- const data = drop.data;
237
- const ok = crystal.forget(data.memory_id);
238
- process.stderr.write(`[relay-poller] chatgpt: forget memory #${data.memory_id} from ${drop.agent_id}: ${ok ? "done" : "not found"}
239
- `);
240
- break;
241
- }
242
- case "attachment": {
243
- const data = drop.data;
244
- const result = await fetchAndSaveAttachment(data, drop.agent_id, relayKey);
245
- if (result) attachments++;
246
- break;
247
- }
248
- default:
249
- process.stderr.write(`[relay-poller] chatgpt: unknown drop type "${drop.type}" ... skipping
250
- `);
251
- }
252
- await confirmBlob("chatgpt", blob.id);
253
- } catch (err) {
254
- process.stderr.write(`[relay-poller] chatgpt: error processing ${blob.id}: ${err.message}
255
- `);
256
- errors++;
257
- }
258
- }
259
- const attResp = await fetch(`${RELAY_URL}/pickup/chatgpt-attachments`, {
260
- headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
261
- });
262
- if (attResp.ok) {
263
- const attData = await attResp.json();
264
- if (attData.count > 0) {
265
- process.stderr.write(`[relay-poller] chatgpt-attachments: ${attData.count} blob(s) (cleaned up after metadata processing)
266
- `);
267
- }
268
- }
269
- return { ingested, attachments, errors };
270
- }
271
- async function ingestConversationDrop(crystal, drop, data) {
272
- const maxSingleChunkChars = 2e3 * 4;
273
- const chunks = [];
274
- const sessionTag = data.session_id ? `cloud:${data.session_id}` : `cloud:${drop.timestamp.slice(0, 10)}`;
275
- const text = data.content;
276
- if (text.length <= maxSingleChunkChars) {
277
- chunks.push({
278
- text,
279
- role: data.role,
280
- source_type: "conversation",
281
- source_id: sessionTag,
282
- agent_id: drop.agent_id,
283
- token_count: Math.ceil(text.length / 4),
284
- created_at: drop.timestamp
285
- });
286
- } else {
287
- for (const ct of crystal.chunkText(text)) {
288
- chunks.push({
289
- text: ct,
290
- role: data.role,
291
- source_type: "conversation",
292
- source_id: sessionTag,
293
- agent_id: drop.agent_id,
294
- token_count: Math.ceil(ct.length / 4),
295
- created_at: drop.timestamp
296
- });
297
- }
298
- }
299
- if (data.tool_calls?.length) {
300
- for (const tc of data.tool_calls) {
301
- const toolText = `[Tool: ${tc.tool_name}] Args: ${tc.arguments}${tc.result ? `
302
- Result: ${tc.result}` : ""}`;
303
- chunks.push({
304
- text: toolText,
305
- role: "assistant",
306
- source_type: "tool_call",
307
- source_id: sessionTag,
308
- agent_id: drop.agent_id,
309
- token_count: Math.ceil(toolText.length / 4),
310
- created_at: drop.timestamp
311
- });
312
- }
313
- }
314
- const count = await crystal.ingest(chunks);
315
- try {
316
- const remotePaths = ensureLdm(drop.agent_id);
317
- const jsonlPath = join(remotePaths.transcripts, `cloud-${drop.timestamp.replace(/[:.]/g, "-")}.jsonl`);
318
- const line = JSON.stringify({
319
- type: "conversation",
320
- agent_id: drop.agent_id,
321
- timestamp: drop.timestamp,
322
- role: data.role,
323
- content: data.content,
324
- session_id: data.session_id,
325
- turn_index: data.turn_index,
326
- model: data.model,
327
- tool_calls: data.tool_calls,
328
- raw_json: data.raw_json
329
- });
330
- appendFileSync(jsonlPath, line + "\n");
331
- appendDailyBreadcrumb(drop.agent_id, data.role === "user" ? data.content : `[${data.model || "assistant"}] ${data.content.slice(0, 80)}`);
332
- } catch (fileErr) {
333
- process.stderr.write(`[relay-poller] chatgpt file tree write failed (non-fatal): ${fileErr.message}
334
- `);
335
- }
336
- if (count > 0) {
337
- process.stderr.write(`[relay-poller] chatgpt: ${count} chunk(s) from ${drop.agent_id} (${data.role}, turn ${data.turn_index ?? "?"})
338
- `);
339
- }
340
- return count;
341
- }
342
- async function fetchAndSaveAttachment(data, agentId, relayKey) {
343
- try {
344
- const blobId = data.r2_key.split("/").pop();
345
- if (!blobId) {
346
- process.stderr.write(`[relay-poller] chatgpt: invalid attachment r2_key: ${data.r2_key}
347
- `);
348
- return false;
349
- }
350
- const blobResp = await fetch(`${RELAY_URL}/pickup/chatgpt-attachments/${blobId}`, {
351
- headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
352
- });
353
- if (!blobResp.ok) {
354
- process.stderr.write(`[relay-poller] chatgpt: failed to fetch attachment ${blobId}: ${blobResp.status}
355
- `);
356
- return false;
357
- }
358
- const encryptedText = await blobResp.text();
359
- const encryptedPayload = JSON.parse(encryptedText);
360
- const nonce = Buffer.from(encryptedPayload.nonce, "base64");
361
- const encryptedData = Buffer.from(encryptedPayload.data, "base64");
362
- const ciphertext = encryptedData.subarray(0, encryptedData.length - 16);
363
- const tag = encryptedData.subarray(encryptedData.length - 16);
364
- const decipher = createDecipheriv("aes-256-gcm", relayKey, nonce);
365
- decipher.setAuthTag(tag);
366
- const decryptedBuf = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
367
- const remotePaths = ensureLdm(agentId);
368
- const attachmentsDir = join(remotePaths.agentRoot, "memory", "attachments");
369
- if (!existsSync(attachmentsDir)) mkdirSync(attachmentsDir, { recursive: true });
370
- const safeFilename = data.filename.replace(/[^a-zA-Z0-9._-]/g, "_");
371
- const filePath = join(attachmentsDir, `${blobId}-${safeFilename}`);
372
- writeFileSync(filePath, decryptedBuf);
373
- const metaPath = filePath + ".meta.json";
374
- writeFileSync(metaPath, JSON.stringify({
375
- filename: data.filename,
376
- mime_type: data.mime_type,
377
- size_bytes: data.size_bytes,
378
- source: data.source,
379
- surface: data.surface,
380
- context: data.context,
381
- session_id: data.session_id,
382
- agent_id: agentId,
383
- saved_at: (/* @__PURE__ */ new Date()).toISOString()
384
- }, null, 2));
385
- await confirmBlob("chatgpt-attachments", blobId);
386
- process.stderr.write(`[relay-poller] chatgpt: saved attachment ${data.filename} (${data.mime_type}, ${(data.size_bytes / 1024).toFixed(1)}KB) from ${agentId}
387
- `);
388
- return true;
389
- } catch (err) {
390
- process.stderr.write(`[relay-poller] chatgpt: attachment save failed: ${err.message}
391
- `);
392
- return false;
393
- }
394
- }
395
- async function confirmBlob(channel, id) {
396
- await fetch(`${RELAY_URL}/confirm/${channel}/${id}`, {
397
- method: "DELETE",
398
- headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
399
- });
400
- }
401
- function appendDailyBreadcrumb(agentId, text) {
402
- try {
403
- const remotePaths = ensureLdm(agentId);
404
- const now = /* @__PURE__ */ new Date();
405
- const dateStr = now.toISOString().slice(0, 10);
406
- const dailyPath = join(remotePaths.daily, `${dateStr}.md`);
407
- if (!existsSync(dailyPath)) {
408
- writeFileSync(dailyPath, `# ${dateStr} - ${agentId} Daily Log (via relay)
409
-
410
- `);
411
- }
412
- if (text) {
413
- const snippet = text.slice(0, 120).replace(/\n/g, " ").trim();
414
- appendFileSync(dailyPath, `- **${now.toISOString().slice(11, 16)}** [relay] ${snippet}
415
- `);
416
- }
417
- } catch {
418
- }
419
- }
420
- async function pollOnce() {
421
- if (!RELAY_URL || !RELAY_TOKEN) {
422
- throw new Error("CRYSTAL_RELAY_URL and CRYSTAL_RELAY_TOKEN must be set");
423
- }
424
- const relayKey = loadRelayKey();
425
- const config = resolveConfig();
426
- const crystal = new Crystal(config);
427
- await crystal.init();
428
- const conv = await pollConversations(crystal, relayKey);
429
- const chatgpt = await pollChatgpt(crystal, relayKey);
430
- return {
431
- ingested: conv.ingested + chatgpt.ingested,
432
- errors: conv.errors + chatgpt.errors,
433
- chatgptIngested: chatgpt.ingested,
434
- chatgptAttachments: chatgpt.attachments
435
- };
436
- }
437
- async function pollCommands() {
438
- const relayKey = loadRelayKey();
439
- const listResp = await fetch(`${RELAY_URL}/pickup/commands`, {
440
- headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
441
- });
442
- if (!listResp.ok) return;
443
- const listData = await listResp.json();
444
- if (listData.count === 0) return;
445
- process.stderr.write(`[relay-poller] ${listData.count} command(s) waiting
446
- `);
447
- for (const blob of listData.blobs) {
448
- try {
449
- const blobResp = await fetch(`${RELAY_URL}/pickup/commands/${blob.id}`, {
450
- headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
451
- });
452
- if (!blobResp.ok) continue;
453
- const encryptedText = await blobResp.text();
454
- const encrypted = JSON.parse(encryptedText);
455
- let cmd;
456
- try {
457
- cmd = decryptJSON(encrypted, relayKey);
458
- } catch {
459
- await fetch(`${RELAY_URL}/confirm/commands/${blob.id}`, {
460
- method: "DELETE",
461
- headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
462
- });
463
- continue;
464
- }
465
- const gatewayPort = process.env.CRYSTAL_SERVE_PORT || "18790";
466
- try {
467
- const resp = await fetch(`http://127.0.0.1:${gatewayPort}/process`, {
468
- method: "POST",
469
- headers: { "Content-Type": "application/json" },
470
- body: JSON.stringify({
471
- action: cmd.action || "dream-weave",
472
- agent_id: cmd.agent_id,
473
- mode: cmd.mode
474
- })
475
- });
476
- if (resp.ok) {
477
- process.stderr.write(`[relay-poller] delivered command: ${cmd.action} for ${cmd.agent_id}
478
- `);
479
- } else {
480
- process.stderr.write(`[relay-poller] gateway rejected command: ${resp.status}
481
- `);
482
- }
483
- } catch (err) {
484
- process.stderr.write(`[relay-poller] gateway not available (non-fatal): ${err.message}
485
- `);
486
- }
487
- await fetch(`${RELAY_URL}/confirm/commands/${blob.id}`, {
488
- method: "DELETE",
489
- headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
490
- });
491
- } catch (err) {
492
- process.stderr.write(`[relay-poller] command processing error: ${err.message}
493
- `);
494
- }
495
- }
496
- }
497
- async function pushDelta(force) {
498
- if (!RELAY_URL || !RELAY_TOKEN) {
499
- throw new Error("CRYSTAL_RELAY_URL and CRYSTAL_RELAY_TOKEN must be set");
500
- }
501
- const relayKey = loadRelayKey();
502
- const config = resolveConfig();
503
- const crystal = new Crystal(config);
504
- await crystal.init();
505
- const state = loadState();
506
- const sinceId = force ? 0 : state.lastDeltaChunkId || 0;
507
- const maxId = crystal.getMaxChunkId();
508
- if (maxId <= sinceId) {
509
- process.stderr.write(`[relay-poller] no new chunks since ID ${sinceId}
510
- `);
511
- return;
512
- }
513
- const chunks = crystal.exportChunksSince(sinceId);
514
- if (chunks.length === 0) {
515
- process.stderr.write(`[relay-poller] no new chunks to push
516
- `);
517
- return;
518
- }
519
- const deltaPayload = {
520
- version: Crystal.DELTA_VERSION,
521
- sinceId,
522
- maxId,
523
- chunkCount: chunks.length,
524
- pushedAt: (/* @__PURE__ */ new Date()).toISOString(),
525
- chunks
526
- };
527
- const encrypted = encryptJSON(deltaPayload, relayKey);
528
- const resp = await fetch(`${RELAY_URL}/drop/mirror`, {
529
- method: "POST",
530
- headers: {
531
- "Authorization": `Bearer ${RELAY_TOKEN}`,
532
- "Content-Type": "application/octet-stream"
533
- },
534
- body: JSON.stringify(encrypted)
535
- });
536
- if (!resp.ok) {
537
- throw new Error(`Delta push failed: ${resp.status} ${await resp.text()}`);
538
- }
539
- state.lastDeltaChunkId = maxId;
540
- state.lastMirrorPush = (/* @__PURE__ */ new Date()).toISOString();
541
- saveState(state);
542
- const payloadSize = JSON.stringify(encrypted).length;
543
- process.stderr.write(
544
- `[relay-poller] delta pushed: ${chunks.length} chunks (ID ${sinceId + 1}..${maxId}), ${(payloadSize / 1024).toFixed(1)}KB
545
- `
546
- );
547
- }
548
- var args = process.argv.slice(2);
549
- if (args.includes("--status")) {
550
- const state = loadState();
551
- const mode = RELAY_URL && RELAY_TOKEN ? "configured" : "not configured";
552
- console.log(`Relay poller status:`);
553
- console.log(` Relay URL: ${RELAY_URL || "(not set)"}`);
554
- console.log(` Mode: ${mode}`);
555
- console.log(` Last poll: ${state.lastPoll || "never"}`);
556
- console.log(` Total ingested: ${state.totalIngested} (conversations)`);
557
- console.log(` ChatGPT ingested: ${state.totalChatgptIngested} (cloud MCP)`);
558
- console.log(` Attachments saved: ${state.totalAttachments}`);
559
- console.log(` Last delta: ${state.lastMirrorPush || "never"}`);
560
- console.log(` Delta watermark: chunk ID ${state.lastDeltaChunkId || 0}`);
561
- process.exit(0);
562
- }
563
- if (args.includes("--push-mirror") || args.includes("--push-delta")) {
564
- const full = args.includes("--full");
565
- pushDelta(full).then(() => {
566
- process.exit(0);
567
- }).catch((err) => {
568
- process.stderr.write(`[relay-poller] mirror push error: ${err.message}
569
- `);
570
- process.exit(1);
571
- });
572
- } else if (args.includes("--watch")) {
573
- const POLL_INTERVAL = 2 * 60 * 1e3;
574
- async function loop() {
575
- process.stderr.write(`[relay-poller] watching (every ${POLL_INTERVAL / 1e3}s)...
576
- `);
577
- while (true) {
578
- try {
579
- const result = await pollOnce();
580
- const state = loadState();
581
- state.lastPoll = (/* @__PURE__ */ new Date()).toISOString();
582
- state.totalIngested += result.ingested;
583
- state.totalChatgptIngested += result.chatgptIngested;
584
- state.totalAttachments += result.chatgptAttachments;
585
- saveState(state);
586
- const totalNew = result.ingested + result.chatgptAttachments;
587
- if (totalNew > 0) {
588
- process.stderr.write(`[relay-poller] poll complete: ${result.ingested} ingested, ${result.chatgptAttachments} attachments, ${result.errors} errors
589
- `);
590
- try {
591
- await pushDelta();
592
- } catch (deltaErr) {
593
- process.stderr.write(`[relay-poller] delta push failed (non-fatal): ${deltaErr.message}
594
- `);
595
- }
596
- }
597
- try {
598
- const { manifest, files } = await pushFileSync();
599
- if (files > 0) {
600
- process.stderr.write(`[relay-poller] file sync pushed: ${files} files
601
- `);
602
- }
603
- } catch (fileSyncErr) {
604
- process.stderr.write(`[relay-poller] file sync failed (non-fatal): ${fileSyncErr.message}
605
- `);
606
- }
607
- } catch (err) {
608
- process.stderr.write(`[relay-poller] poll error: ${err.message}
609
- `);
610
- }
611
- await new Promise((r) => setTimeout(r, POLL_INTERVAL));
612
- }
613
- }
614
- loop();
615
- } else {
616
- pollOnce().then((result) => {
617
- const state = loadState();
618
- state.lastPoll = (/* @__PURE__ */ new Date()).toISOString();
619
- state.totalIngested += result.ingested;
620
- state.totalChatgptIngested += result.chatgptIngested;
621
- state.totalAttachments += result.chatgptAttachments;
622
- saveState(state);
623
- const totalNew = result.ingested + result.chatgptAttachments;
624
- if (totalNew > 0) {
625
- process.stderr.write(`[relay-poller] ${result.ingested} chunks ingested, ${result.chatgptAttachments} attachments, ${result.errors} errors
626
- `);
627
- }
628
- process.exit(result.errors > 0 ? 1 : 0);
629
- }).catch((err) => {
630
- process.stderr.write(`[relay-poller] error: ${err.message}
631
- `);
632
- process.exit(1);
633
- });
634
- }