memory-crystal 0.2.0

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 (104) hide show
  1. package/.env.example +20 -0
  2. package/CHANGELOG.md +6 -0
  3. package/LETTERS.md +22 -0
  4. package/LICENSE +21 -0
  5. package/README-ENTERPRISE.md +162 -0
  6. package/README-old.md +275 -0
  7. package/README.md +91 -0
  8. package/RELAY.md +88 -0
  9. package/TECHNICAL.md +379 -0
  10. package/ai/dev-updates/2026-02-25--cc-air--phase2-architecture-pivot.md +70 -0
  11. package/ai/dev-updates/2026-02-25--cc-air--phase2-worker-build.md +72 -0
  12. package/ai/dev-updates/2026-02-26--10-25-16--cc-mini--phase2-implementation.md +49 -0
  13. package/ai/dev-updates/2026-02-27--20-30-00--cc-mini--readme-overhaul-and-public-deploy.md +69 -0
  14. package/ai/notes/2026-02-26--cc-air--notes.md +412 -0
  15. package/ai/notes/2026-02-27--cc-mini--grok-feedback.md +44 -0
  16. package/ai/notes/2026-02-27--cc-mini--lesa-feedback.md +45 -0
  17. package/ai/notes/RESEARCH.md +1185 -0
  18. package/ai/notes/salience-research/README.md +29 -0
  19. package/ai/notes/salience-research/eurosla-salience-review.md +64 -0
  20. package/ai/notes/salience-research/full-research-summary.md +269 -0
  21. package/ai/notes/salience-research/salience-levels-diagram.png +0 -0
  22. package/ai/plan/2026-02-27--cc-mini--qr-pairing-spec.md +203 -0
  23. package/ai/plan/_archive/PLAN.md +194 -0
  24. package/ai/plan/_archive/PRD.md +1014 -0
  25. package/ai/plan/cc-plans-duplicates-from-dot-claude/2026-02-26--cc-mini--phase2-implementation-plan.md +245 -0
  26. package/ai/plan/dev-conventions-note.md +70 -0
  27. package/ai/plan/ldm-os-install-and-boot-architecture.md +285 -0
  28. package/ai/plan/memory-crystal-phase2-plan.md +192 -0
  29. package/ai/plan/memory-system-lay-of-the-land.md +214 -0
  30. package/ai/plan/phase2-ephemeral-relay.md +238 -0
  31. package/ai/plan/readme-first.md +68 -0
  32. package/ai/plan/roadmap.md +159 -0
  33. package/ai/todos/PUNCHLIST.md +44 -0
  34. package/ai/todos/README.md +31 -0
  35. package/ai/todos/inboxes/cc-air/2026-02-26--cc-air--post-relay-todos.md +85 -0
  36. package/ai/todos/inboxes/cc-mini/2026-02-26--cc-mini--phase2-status.md +100 -0
  37. package/ai/todos/inboxes/cc-mini/_archive/TODO.md +25 -0
  38. package/ai/todos/inboxes/parker/2026-02-25--cc-air--setup-checklist.md +139 -0
  39. package/ai/todos/inboxes/parker/2026-02-26--cc-mini--phase2-your-moves.md +72 -0
  40. package/dist/cc-hook.d.ts +1 -0
  41. package/dist/cc-hook.js +349 -0
  42. package/dist/chunk-3VFIJYS4.js +818 -0
  43. package/dist/chunk-52QE3YI3.js +1169 -0
  44. package/dist/chunk-AA3OPP4Z.js +432 -0
  45. package/dist/chunk-D3I3ZSE2.js +411 -0
  46. package/dist/chunk-EKSACBTJ.js +1070 -0
  47. package/dist/chunk-F3Y7EL7K.js +83 -0
  48. package/dist/chunk-JWZXYVET.js +1068 -0
  49. package/dist/chunk-KYVWO6ZM.js +1069 -0
  50. package/dist/chunk-L3VHARQH.js +413 -0
  51. package/dist/chunk-LOVAHSQV.js +411 -0
  52. package/dist/chunk-LQOYCAGG.js +446 -0
  53. package/dist/chunk-MK42FMEG.js +147 -0
  54. package/dist/chunk-NIJCVN3O.js +147 -0
  55. package/dist/chunk-O2UITJGH.js +465 -0
  56. package/dist/chunk-PEK6JH65.js +432 -0
  57. package/dist/chunk-PJ6FFKEX.js +77 -0
  58. package/dist/chunk-PLUBBZYR.js +800 -0
  59. package/dist/chunk-SGL6ISBJ.js +1061 -0
  60. package/dist/chunk-UNHVZB5G.js +411 -0
  61. package/dist/chunk-VAFTWSTE.js +1061 -0
  62. package/dist/chunk-XZ3S56RQ.js +1061 -0
  63. package/dist/chunk-Y72C7F6O.js +148 -0
  64. package/dist/cli.d.ts +1 -0
  65. package/dist/cli.js +325 -0
  66. package/dist/core.d.ts +188 -0
  67. package/dist/core.js +12 -0
  68. package/dist/crypto.d.ts +16 -0
  69. package/dist/crypto.js +18 -0
  70. package/dist/dev-update-SZ2Z4WCQ.js +6 -0
  71. package/dist/ldm.d.ts +17 -0
  72. package/dist/ldm.js +12 -0
  73. package/dist/mcp-server.d.ts +1 -0
  74. package/dist/mcp-server.js +250 -0
  75. package/dist/migrate.d.ts +1 -0
  76. package/dist/migrate.js +89 -0
  77. package/dist/mirror-sync.d.ts +1 -0
  78. package/dist/mirror-sync.js +130 -0
  79. package/dist/openclaw.d.ts +5 -0
  80. package/dist/openclaw.js +349 -0
  81. package/dist/poller.d.ts +1 -0
  82. package/dist/poller.js +272 -0
  83. package/dist/summarize.d.ts +19 -0
  84. package/dist/summarize.js +10 -0
  85. package/dist/worker.js +137 -0
  86. package/openclaw.plugin.json +11 -0
  87. package/package.json +40 -0
  88. package/scripts/migrate-lance-to-sqlite.mjs +217 -0
  89. package/skills/memory/SKILL.md +61 -0
  90. package/src/cc-hook.ts +447 -0
  91. package/src/cli.ts +356 -0
  92. package/src/core.ts +1472 -0
  93. package/src/crypto.ts +113 -0
  94. package/src/dev-update.ts +178 -0
  95. package/src/ldm.ts +117 -0
  96. package/src/mcp-server.ts +274 -0
  97. package/src/migrate.ts +104 -0
  98. package/src/mirror-sync.ts +175 -0
  99. package/src/openclaw.ts +250 -0
  100. package/src/poller.ts +345 -0
  101. package/src/summarize.ts +210 -0
  102. package/src/worker.ts +208 -0
  103. package/tsconfig.json +18 -0
  104. package/wrangler.toml +20 -0
@@ -0,0 +1,349 @@
1
+ import {
2
+ Crystal,
3
+ resolveConfig
4
+ } from "./chunk-52QE3YI3.js";
5
+
6
+ // src/dev-update.ts
7
+ import { execSync } from "child_process";
8
+ import { existsSync, mkdirSync, writeFileSync, readFileSync } from "fs";
9
+ import { join, basename } from "path";
10
+ var HOME = process.env.HOME || "";
11
+ var STAFF_DIR = join(HOME, "Documents", "wipcomputer--mac-mini-01", "staff");
12
+ var CC_REPOS = join(STAFF_DIR, "Parker", "Claude Code - Mini", "repos");
13
+ var LESA_REPOS = join(STAFF_DIR, "L\u0113sa", "repos");
14
+ var DEV_UPDATES_DIR = join(CC_REPOS, "wip-dev-updates");
15
+ var LAST_RUN_PATH = join(HOME, ".openclaw", "memory", "dev-update-last-run.json");
16
+ function loadLastRun() {
17
+ try {
18
+ if (existsSync(LAST_RUN_PATH)) {
19
+ return JSON.parse(readFileSync(LAST_RUN_PATH, "utf-8"));
20
+ }
21
+ } catch {
22
+ }
23
+ return null;
24
+ }
25
+ function saveLastRun(run) {
26
+ const dir = join(HOME, ".openclaw", "memory");
27
+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
28
+ writeFileSync(LAST_RUN_PATH, JSON.stringify(run, null, 2));
29
+ }
30
+ function git(repoPath, cmd) {
31
+ try {
32
+ return execSync(`git -C "${repoPath}" ${cmd}`, {
33
+ encoding: "utf-8",
34
+ timeout: 1e4,
35
+ stdio: ["pipe", "pipe", "pipe"]
36
+ }).trim();
37
+ } catch {
38
+ return "";
39
+ }
40
+ }
41
+ function scanRepo(repoPath, since) {
42
+ if (!existsSync(join(repoPath, ".git"))) return null;
43
+ const name = basename(repoPath);
44
+ if (name === "_third-party-repos" || name === "wip-dev-updates") return null;
45
+ const recentCommits = git(repoPath, `log --oneline --since="${since}"`);
46
+ const uncommitted = git(repoPath, "status --porcelain");
47
+ if (!recentCommits && !uncommitted) return null;
48
+ const lines = [];
49
+ lines.push(`# ${name}`);
50
+ lines.push("");
51
+ if (recentCommits) {
52
+ lines.push("## Recent Commits");
53
+ lines.push("");
54
+ lines.push("```");
55
+ lines.push(...recentCommits.split("\n").slice(0, 10));
56
+ lines.push("```");
57
+ lines.push("");
58
+ }
59
+ if (uncommitted) {
60
+ lines.push("## Uncommitted Changes");
61
+ lines.push("");
62
+ lines.push("```");
63
+ lines.push(...uncommitted.split("\n").slice(0, 20));
64
+ lines.push("```");
65
+ lines.push("");
66
+ }
67
+ if (recentCommits) {
68
+ const diffStat = git(repoPath, `diff --stat "HEAD@{${since}}" HEAD`);
69
+ if (diffStat) {
70
+ lines.push("## Files Changed");
71
+ lines.push("");
72
+ lines.push("```");
73
+ lines.push(...diffStat.split("\n").slice(-15));
74
+ lines.push("```");
75
+ lines.push("");
76
+ }
77
+ }
78
+ const branch = git(repoPath, "branch --show-current") || "unknown";
79
+ lines.push(`**Branch:** ${branch}`);
80
+ lines.push("");
81
+ return lines.join("\n");
82
+ }
83
+ function runDevUpdate(author) {
84
+ const lastRun = loadLastRun();
85
+ if (lastRun) {
86
+ const elapsed = Date.now() - new Date(lastRun.timestamp).getTime();
87
+ if (elapsed < 60 * 60 * 1e3) {
88
+ return { reposUpdated: 0, files: [] };
89
+ }
90
+ }
91
+ let since = "6 hours ago";
92
+ if (lastRun?.timestamp) {
93
+ const lastDate = new Date(lastRun.timestamp);
94
+ const hoursAgo = Math.ceil((Date.now() - lastDate.getTime()) / (1e3 * 60 * 60));
95
+ since = `${Math.max(hoursAgo, 1)} hours ago`;
96
+ }
97
+ const now = /* @__PURE__ */ new Date();
98
+ const ts = [
99
+ String(now.getMonth() + 1).padStart(2, "0"),
100
+ String(now.getDate()).padStart(2, "0"),
101
+ String(now.getFullYear())
102
+ ].join("-") + "--" + [
103
+ String(now.getHours()).padStart(2, "0"),
104
+ String(now.getMinutes()).padStart(2, "0"),
105
+ String(now.getSeconds()).padStart(2, "0")
106
+ ].join("-");
107
+ const files = [];
108
+ const repoDirs = [CC_REPOS, LESA_REPOS];
109
+ for (const parentDir of repoDirs) {
110
+ if (!existsSync(parentDir)) continue;
111
+ let entries;
112
+ try {
113
+ entries = execSync(`ls "${parentDir}"`, { encoding: "utf-8" }).trim().split("\n");
114
+ } catch {
115
+ continue;
116
+ }
117
+ for (const entry of entries) {
118
+ const repoPath = join(parentDir, entry);
119
+ const content = scanRepo(repoPath, since);
120
+ if (!content) continue;
121
+ const repoName = basename(repoPath);
122
+ const outDir = join(repoPath, "ai");
123
+ const outFile = join(outDir, `${now.toISOString().slice(0, 10)}--${now.toISOString().slice(11, 19).replace(/:/g, "-")}--${author}--dev-update-${repoName}.md`);
124
+ mkdirSync(outDir, { recursive: true });
125
+ const header = `*Auto-generated dev update by ${author} at ${now.toISOString().slice(0, 16).replace("T", " ")}*
126
+
127
+ `;
128
+ writeFileSync(outFile, content.replace(/^# .+\n/, `$&
129
+ ${header}`));
130
+ files.push(`${repoName}/ai/${basename(outFile)}`);
131
+ }
132
+ }
133
+ if (false) {
134
+ try {
135
+ execSync(
136
+ `cd "${DEV_UPDATES_DIR}" && git add -A && git commit -m "${author} auto-dev-update ${ts}: ${files.length} repo(s)" --no-verify && git push --quiet`,
137
+ { encoding: "utf-8", timeout: 3e4, stdio: "pipe" }
138
+ );
139
+ } catch {
140
+ }
141
+ }
142
+ saveLastRun({
143
+ timestamp: now.toISOString(),
144
+ author,
145
+ reposUpdated: files.length
146
+ });
147
+ return { reposUpdated: files.length, files };
148
+ }
149
+
150
+ // src/openclaw.ts
151
+ import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
152
+ import { join as join2 } from "path";
153
+ var CONFIG_DIR = join2(process.env.HOME || "", ".openclaw");
154
+ var PRIVATE_MODE_PATH = join2(CONFIG_DIR, "memory", "memory-capture-state.json");
155
+ function isPrivateMode() {
156
+ try {
157
+ if (existsSync2(PRIVATE_MODE_PATH)) {
158
+ const state = JSON.parse(readFileSync2(PRIVATE_MODE_PATH, "utf-8"));
159
+ return state.enabled === false;
160
+ }
161
+ } catch {
162
+ }
163
+ return false;
164
+ }
165
+ var openclaw_default = {
166
+ register(api) {
167
+ const crystal = new Crystal(resolveConfig());
168
+ let initialized = false;
169
+ async function ensureInit() {
170
+ if (!initialized) {
171
+ await crystal.init();
172
+ initialized = true;
173
+ }
174
+ }
175
+ api.on("agent_end", async (event, ctx) => {
176
+ if (isPrivateMode()) return;
177
+ await ensureInit();
178
+ const messages = event.messages;
179
+ if (!messages || messages.length === 0) return;
180
+ const agentId = ctx.agentId || "main";
181
+ const sessionKey = ctx.sessionKey || "unknown";
182
+ const state = crystal.getCaptureState(agentId, sessionKey);
183
+ const storedCount = state.lastMessageCount;
184
+ let startIndex = storedCount;
185
+ if (messages.length < storedCount) {
186
+ api.logger.info(`memory-crystal: compaction detected (${storedCount} \u2192 ${messages.length} messages), resetting capture position`);
187
+ startIndex = 0;
188
+ }
189
+ if (messages.length <= startIndex) return;
190
+ const newTurns = [];
191
+ for (let i = startIndex; i < messages.length; i++) {
192
+ const msg = messages[i];
193
+ if (!msg.content) continue;
194
+ const role = msg.role;
195
+ if (role !== "user" && role !== "assistant") continue;
196
+ let text = "";
197
+ if (typeof msg.content === "string") {
198
+ text = msg.content;
199
+ } else if (Array.isArray(msg.content)) {
200
+ text = msg.content.filter((b) => b.type === "text").map((b) => b.text).join("\n");
201
+ }
202
+ if (!text || text.length < 50) continue;
203
+ const maxSingleChunkChars = 2e3 * 4;
204
+ if (text.length <= maxSingleChunkChars) {
205
+ newTurns.push({
206
+ text,
207
+ role,
208
+ source_type: "conversation",
209
+ source_id: sessionKey,
210
+ agent_id: agentId,
211
+ token_count: Math.ceil(text.length / 4),
212
+ created_at: (/* @__PURE__ */ new Date()).toISOString()
213
+ });
214
+ } else {
215
+ const chunks = crystal.chunkText(text);
216
+ for (const chunkText of chunks) {
217
+ newTurns.push({
218
+ text: chunkText,
219
+ role,
220
+ source_type: "conversation",
221
+ source_id: sessionKey,
222
+ agent_id: agentId,
223
+ token_count: Math.ceil(chunkText.length / 4),
224
+ created_at: (/* @__PURE__ */ new Date()).toISOString()
225
+ });
226
+ }
227
+ }
228
+ }
229
+ const totalTokens = newTurns.reduce((sum, c) => sum + c.token_count, 0);
230
+ if (totalTokens < 500) return;
231
+ try {
232
+ const count = await crystal.ingest(newTurns);
233
+ crystal.setCaptureState(agentId, sessionKey, messages.length, state.captureCount + 1);
234
+ api.logger.info(`memory-crystal: ingested ${count} chunks from ${sessionKey} (cycle ${state.captureCount + 1})`);
235
+ } catch (err) {
236
+ api.logger.error(`memory-crystal: ingest error: ${err.message}`);
237
+ }
238
+ });
239
+ function toolResult(text, isError = false) {
240
+ return {
241
+ content: [{ type: "text", text }],
242
+ ...isError ? { isError: true } : {}
243
+ };
244
+ }
245
+ api.registerTool(
246
+ {
247
+ name: "crystal_search",
248
+ label: "Search Memory Crystal",
249
+ description: "Search memory crystal \u2014 semantic search across all conversations and stored memories.",
250
+ parameters: {
251
+ type: "object",
252
+ properties: {
253
+ query: { type: "string", description: "What to search for" },
254
+ limit: { type: "number", description: "Max results (default: 5)" },
255
+ agent_id: { type: "string", description: "Filter by agent" }
256
+ },
257
+ required: ["query"]
258
+ },
259
+ async execute(_id, params) {
260
+ try {
261
+ await ensureInit();
262
+ const results = await crystal.search(
263
+ params.query,
264
+ params.limit || 5,
265
+ params.agent_id ? { agent_id: params.agent_id } : void 0
266
+ );
267
+ if (results.length === 0) return toolResult("No results found.");
268
+ const formatted = results.map((r, i) => {
269
+ const score = (r.score * 100).toFixed(1);
270
+ const date = r.created_at?.slice(0, 10) || "unknown";
271
+ return `[${i + 1}] (${score}%, ${r.agent_id}, ${date}, ${r.role})
272
+ ${r.text}`;
273
+ }).join("\n\n---\n\n");
274
+ return toolResult(formatted);
275
+ } catch (err) {
276
+ return toolResult(`crystal_search error: ${err.message}`, true);
277
+ }
278
+ }
279
+ },
280
+ { optional: true }
281
+ );
282
+ api.registerTool(
283
+ {
284
+ name: "crystal_remember",
285
+ label: "Remember in Crystal",
286
+ description: "Store a fact, preference, or observation in memory crystal.",
287
+ parameters: {
288
+ type: "object",
289
+ properties: {
290
+ text: { type: "string", description: "The fact to remember" },
291
+ category: { type: "string", enum: ["fact", "preference", "event", "opinion", "skill"] }
292
+ },
293
+ required: ["text"]
294
+ },
295
+ async execute(_id, params) {
296
+ if (isPrivateMode()) {
297
+ return toolResult("Private mode is on. No memories are being stored. Use /private-mode off to resume.");
298
+ }
299
+ try {
300
+ await ensureInit();
301
+ const id = await crystal.remember(params.text, params.category || "fact");
302
+ return toolResult(`Remembered (id: ${id}): ${params.text}`);
303
+ } catch (err) {
304
+ return toolResult(`crystal_remember error: ${err.message}`, true);
305
+ }
306
+ }
307
+ },
308
+ { optional: true }
309
+ );
310
+ api.registerTool(
311
+ {
312
+ name: "crystal_forget",
313
+ label: "Forget Memory",
314
+ description: "Deprecate a memory by ID.",
315
+ parameters: {
316
+ type: "object",
317
+ properties: {
318
+ id: { type: "number", description: "Memory ID to deprecate" }
319
+ },
320
+ required: ["id"]
321
+ },
322
+ async execute(_id, params) {
323
+ try {
324
+ await ensureInit();
325
+ const ok = crystal.forget(params.id);
326
+ return toolResult(ok ? `Forgot memory ${params.id}` : `Memory ${params.id} not found`);
327
+ } catch (err) {
328
+ return toolResult(`crystal_forget error: ${err.message}`, true);
329
+ }
330
+ }
331
+ },
332
+ { optional: true }
333
+ );
334
+ api.on("before_compaction", (_event, _ctx) => {
335
+ try {
336
+ const result = runDevUpdate("lesa");
337
+ if (result.reposUpdated > 0) {
338
+ api.logger.info(`memory-crystal: auto-dev-update wrote ${result.reposUpdated} updates before compaction`);
339
+ }
340
+ } catch (err) {
341
+ api.logger.warn(`memory-crystal: auto-dev-update failed: ${err.message}`);
342
+ }
343
+ });
344
+ api.logger.info("memory-crystal plugin registered");
345
+ }
346
+ };
347
+ export {
348
+ openclaw_default as default
349
+ };
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
package/dist/poller.js ADDED
@@ -0,0 +1,272 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ generateSessionSummary,
4
+ writeSummaryFile
5
+ } from "./chunk-Y72C7F6O.js";
6
+ import {
7
+ Crystal,
8
+ resolveConfig
9
+ } from "./chunk-52QE3YI3.js";
10
+ import {
11
+ decryptJSON,
12
+ encrypt,
13
+ hashBuffer,
14
+ loadRelayKey
15
+ } from "./chunk-F3Y7EL7K.js";
16
+ import {
17
+ ensureLdm,
18
+ ldmPaths
19
+ } from "./chunk-PJ6FFKEX.js";
20
+
21
+ // src/poller.ts
22
+ import { readFileSync, writeFileSync, appendFileSync, existsSync, mkdirSync } from "fs";
23
+ import { join, dirname } from "path";
24
+ var HOME = process.env.HOME || "";
25
+ var RELAY_URL = process.env.CRYSTAL_RELAY_URL || "";
26
+ var RELAY_TOKEN = process.env.CRYSTAL_RELAY_TOKEN || "";
27
+ var OC_DIR = join(HOME, ".openclaw");
28
+ var POLLER_STATE_PATH = join(OC_DIR, "memory", "relay-poller-state.json");
29
+ function loadState() {
30
+ try {
31
+ if (existsSync(POLLER_STATE_PATH)) {
32
+ return JSON.parse(readFileSync(POLLER_STATE_PATH, "utf-8"));
33
+ }
34
+ } catch {
35
+ }
36
+ return { lastPoll: null, totalIngested: 0, lastMirrorPush: null };
37
+ }
38
+ function saveState(state) {
39
+ const dir = dirname(POLLER_STATE_PATH);
40
+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
41
+ writeFileSync(POLLER_STATE_PATH, JSON.stringify(state, null, 2));
42
+ }
43
+ async function pollOnce() {
44
+ if (!RELAY_URL || !RELAY_TOKEN) {
45
+ throw new Error("CRYSTAL_RELAY_URL and CRYSTAL_RELAY_TOKEN must be set");
46
+ }
47
+ const relayKey = loadRelayKey();
48
+ let ingested = 0;
49
+ let errors = 0;
50
+ const listResp = await fetch(`${RELAY_URL}/pickup/conversations`, {
51
+ headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
52
+ });
53
+ if (!listResp.ok) {
54
+ throw new Error(`Relay list failed: ${listResp.status} ${await listResp.text()}`);
55
+ }
56
+ const listData = await listResp.json();
57
+ if (listData.count === 0) {
58
+ return { ingested: 0, errors: 0 };
59
+ }
60
+ process.stderr.write(`[relay-poller] ${listData.count} blob(s) waiting
61
+ `);
62
+ const config = resolveConfig();
63
+ const crystal = new Crystal(config);
64
+ await crystal.init();
65
+ for (const blob of listData.blobs) {
66
+ try {
67
+ const blobResp = await fetch(`${RELAY_URL}/pickup/conversations/${blob.id}`, {
68
+ headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
69
+ });
70
+ if (!blobResp.ok) {
71
+ process.stderr.write(`[relay-poller] failed to fetch blob ${blob.id}: ${blobResp.status}
72
+ `);
73
+ errors++;
74
+ continue;
75
+ }
76
+ const encryptedText = await blobResp.text();
77
+ const encrypted = JSON.parse(encryptedText);
78
+ let drop;
79
+ try {
80
+ drop = decryptJSON(encrypted, relayKey);
81
+ } catch (err) {
82
+ process.stderr.write(`[relay-poller] blob ${blob.id} failed verification: ${err.message} \u2014 DISCARDED
83
+ `);
84
+ await fetch(`${RELAY_URL}/confirm/conversations/${blob.id}`, {
85
+ method: "DELETE",
86
+ headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
87
+ });
88
+ errors++;
89
+ continue;
90
+ }
91
+ const maxSingleChunkChars = 2e3 * 4;
92
+ const chunks = [];
93
+ for (const msg of drop.messages) {
94
+ if (msg.text.length <= maxSingleChunkChars) {
95
+ chunks.push({
96
+ text: msg.text,
97
+ role: msg.role,
98
+ source_type: "conversation",
99
+ source_id: `cc:${msg.sessionId}`,
100
+ agent_id: drop.agent_id,
101
+ token_count: Math.ceil(msg.text.length / 4),
102
+ created_at: msg.timestamp
103
+ });
104
+ } else {
105
+ for (const ct of crystal.chunkText(msg.text)) {
106
+ chunks.push({
107
+ text: ct,
108
+ role: msg.role,
109
+ source_type: "conversation",
110
+ source_id: `cc:${msg.sessionId}`,
111
+ agent_id: drop.agent_id,
112
+ token_count: Math.ceil(ct.length / 4),
113
+ created_at: msg.timestamp
114
+ });
115
+ }
116
+ }
117
+ }
118
+ const count = await crystal.ingest(chunks);
119
+ ingested += count;
120
+ await fetch(`${RELAY_URL}/confirm/conversations/${blob.id}`, {
121
+ method: "DELETE",
122
+ headers: { "Authorization": `Bearer ${RELAY_TOKEN}` }
123
+ });
124
+ process.stderr.write(`[relay-poller] blob ${blob.id}: ${count} chunks ingested from ${drop.agent_id}
125
+ `);
126
+ try {
127
+ const remotePaths = ensureLdm(drop.agent_id);
128
+ const jsonlPath = join(remotePaths.transcripts, `relay-${blob.id}.jsonl`);
129
+ const jsonlLines = drop.messages.map((m) => JSON.stringify(m)).join("\n") + "\n";
130
+ writeFileSync(jsonlPath, jsonlLines);
131
+ const summaryMsgs = drop.messages.map((m) => ({
132
+ role: m.role,
133
+ text: m.text,
134
+ timestamp: m.timestamp,
135
+ sessionId: m.sessionId
136
+ }));
137
+ const summary = await generateSessionSummary(summaryMsgs);
138
+ const sessionId = drop.messages[0]?.sessionId || "unknown";
139
+ writeSummaryFile(remotePaths.sessions, summary, drop.agent_id, sessionId);
140
+ const now = /* @__PURE__ */ new Date();
141
+ const dateStr = now.toISOString().slice(0, 10);
142
+ const dailyPath = join(remotePaths.daily, `${dateStr}.md`);
143
+ if (!existsSync(dailyPath)) {
144
+ writeFileSync(dailyPath, `# ${dateStr} - ${drop.agent_id} Daily Log (via relay)
145
+
146
+ `);
147
+ }
148
+ const firstUser = drop.messages.find((m) => m.role === "user");
149
+ if (firstUser) {
150
+ const snippet = firstUser.text.slice(0, 120).replace(/\n/g, " ").trim();
151
+ appendFileSync(dailyPath, `- **${now.toISOString().slice(11, 16)}** [relay] ${snippet}
152
+ `);
153
+ }
154
+ } catch (fileErr) {
155
+ process.stderr.write(`[relay-poller] file tree write failed (non-fatal): ${fileErr.message}
156
+ `);
157
+ }
158
+ } catch (err) {
159
+ process.stderr.write(`[relay-poller] error processing blob ${blob.id}: ${err.message}
160
+ `);
161
+ errors++;
162
+ }
163
+ }
164
+ return { ingested, errors };
165
+ }
166
+ async function pushMirror() {
167
+ if (!RELAY_URL || !RELAY_TOKEN) {
168
+ throw new Error("CRYSTAL_RELAY_URL and CRYSTAL_RELAY_TOKEN must be set");
169
+ }
170
+ const relayKey = loadRelayKey();
171
+ const config = resolveConfig();
172
+ const paths = ldmPaths();
173
+ const dbPath = existsSync(paths.crystalDb) ? paths.crystalDb : join(config.dataDir || join(OC_DIR, "memory-crystal"), "crystal.db");
174
+ if (!existsSync(dbPath)) {
175
+ throw new Error(`Crystal DB not found at ${dbPath}`);
176
+ }
177
+ const dbData = readFileSync(dbPath);
178
+ const dbHash = hashBuffer(dbData);
179
+ const mirrorMeta = JSON.stringify({ hash: dbHash, size: dbData.length, pushed_at: (/* @__PURE__ */ new Date()).toISOString() });
180
+ const metaEncrypted = encrypt(Buffer.from(mirrorMeta, "utf-8"), relayKey);
181
+ const dbEncrypted = encrypt(dbData, relayKey);
182
+ const payload = JSON.stringify({
183
+ meta: metaEncrypted,
184
+ db: dbEncrypted
185
+ });
186
+ const resp = await fetch(`${RELAY_URL}/drop/mirror`, {
187
+ method: "POST",
188
+ headers: {
189
+ "Authorization": `Bearer ${RELAY_TOKEN}`,
190
+ "Content-Type": "application/octet-stream"
191
+ },
192
+ body: payload
193
+ });
194
+ if (!resp.ok) {
195
+ throw new Error(`Mirror push failed: ${resp.status} ${await resp.text()}`);
196
+ }
197
+ const result = await resp.json();
198
+ process.stderr.write(`[relay-poller] mirror pushed: ${(dbData.length / 1024 / 1024).toFixed(1)}MB, hash=${dbHash.slice(0, 12)}...
199
+ `);
200
+ }
201
+ var args = process.argv.slice(2);
202
+ if (args.includes("--status")) {
203
+ const state = loadState();
204
+ const mode = RELAY_URL && RELAY_TOKEN ? "configured" : "not configured";
205
+ console.log(`Relay poller status:`);
206
+ console.log(` Relay URL: ${RELAY_URL || "(not set)"}`);
207
+ console.log(` Mode: ${mode}`);
208
+ console.log(` Last poll: ${state.lastPoll || "never"}`);
209
+ console.log(` Total ingested: ${state.totalIngested}`);
210
+ console.log(` Last mirror: ${state.lastMirrorPush || "never"}`);
211
+ process.exit(0);
212
+ }
213
+ if (args.includes("--push-mirror")) {
214
+ pushMirror().then(() => {
215
+ const state = loadState();
216
+ state.lastMirrorPush = (/* @__PURE__ */ new Date()).toISOString();
217
+ saveState(state);
218
+ process.exit(0);
219
+ }).catch((err) => {
220
+ process.stderr.write(`[relay-poller] mirror push error: ${err.message}
221
+ `);
222
+ process.exit(1);
223
+ });
224
+ } else if (args.includes("--watch")) {
225
+ const POLL_INTERVAL = 2 * 60 * 1e3;
226
+ async function loop() {
227
+ process.stderr.write(`[relay-poller] watching (every ${POLL_INTERVAL / 1e3}s)...
228
+ `);
229
+ while (true) {
230
+ try {
231
+ const { ingested, errors } = await pollOnce();
232
+ const state = loadState();
233
+ state.lastPoll = (/* @__PURE__ */ new Date()).toISOString();
234
+ state.totalIngested += ingested;
235
+ saveState(state);
236
+ if (ingested > 0) {
237
+ process.stderr.write(`[relay-poller] poll complete: ${ingested} ingested, ${errors} errors
238
+ `);
239
+ try {
240
+ await pushMirror();
241
+ state.lastMirrorPush = (/* @__PURE__ */ new Date()).toISOString();
242
+ saveState(state);
243
+ } catch (mirrorErr) {
244
+ process.stderr.write(`[relay-poller] mirror push failed (non-fatal): ${mirrorErr.message}
245
+ `);
246
+ }
247
+ }
248
+ } catch (err) {
249
+ process.stderr.write(`[relay-poller] poll error: ${err.message}
250
+ `);
251
+ }
252
+ await new Promise((r) => setTimeout(r, POLL_INTERVAL));
253
+ }
254
+ }
255
+ loop();
256
+ } else {
257
+ pollOnce().then(({ ingested, errors }) => {
258
+ const state = loadState();
259
+ state.lastPoll = (/* @__PURE__ */ new Date()).toISOString();
260
+ state.totalIngested += ingested;
261
+ saveState(state);
262
+ if (ingested > 0) {
263
+ process.stderr.write(`[relay-poller] ${ingested} chunks ingested, ${errors} errors
264
+ `);
265
+ }
266
+ process.exit(errors > 0 ? 1 : 0);
267
+ }).catch((err) => {
268
+ process.stderr.write(`[relay-poller] error: ${err.message}
269
+ `);
270
+ process.exit(1);
271
+ });
272
+ }
@@ -0,0 +1,19 @@
1
+ interface SessionSummary {
2
+ title: string;
3
+ slug: string;
4
+ summary: string;
5
+ topics: string[];
6
+ messageCount: number;
7
+ date: string;
8
+ }
9
+ interface SummaryMessage {
10
+ role: string;
11
+ text: string;
12
+ timestamp: string;
13
+ sessionId: string;
14
+ }
15
+ declare function generateSessionSummary(messages: SummaryMessage[]): Promise<SessionSummary>;
16
+ declare function formatSummaryMarkdown(summary: SessionSummary, sessionId: string): string;
17
+ declare function writeSummaryFile(sessionsDir: string, summary: SessionSummary, agentId: string, sessionId: string): string;
18
+
19
+ export { type SessionSummary, type SummaryMessage, formatSummaryMarkdown, generateSessionSummary, writeSummaryFile };
@@ -0,0 +1,10 @@
1
+ import {
2
+ formatSummaryMarkdown,
3
+ generateSessionSummary,
4
+ writeSummaryFile
5
+ } from "./chunk-Y72C7F6O.js";
6
+ export {
7
+ formatSummaryMarkdown,
8
+ generateSessionSummary,
9
+ writeSummaryFile
10
+ };