@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,167 @@
1
+ // src/cloud-crystal.ts
2
+ async function embed(text, apiKey) {
3
+ const resp = await fetch("https://api.openai.com/v1/embeddings", {
4
+ method: "POST",
5
+ headers: {
6
+ "Authorization": `Bearer ${apiKey}`,
7
+ "Content-Type": "application/json"
8
+ },
9
+ body: JSON.stringify({
10
+ model: "text-embedding-3-small",
11
+ input: text,
12
+ dimensions: 1024
13
+ })
14
+ });
15
+ if (!resp.ok) {
16
+ const err = await resp.text();
17
+ throw new Error(`Embedding failed: ${resp.status} ${err}`);
18
+ }
19
+ const data = await resp.json();
20
+ return data.data[0].embedding;
21
+ }
22
+ var CloudCrystal = class {
23
+ constructor(db, vectorize, apiKey) {
24
+ this.db = db;
25
+ this.vectorize = vectorize;
26
+ this.apiKey = apiKey;
27
+ }
28
+ // Hybrid search: BM25 (FTS5) + vector (Vectorize) + RRF fusion + recency
29
+ async search(userId, query, limit = 5, agentId) {
30
+ const agentFilter = agentId ? "AND c.agent_id = ?" : "";
31
+ const bm25Params = [userId, query];
32
+ if (agentId) bm25Params.push(agentId);
33
+ const bm25Results = await this.db.prepare(`
34
+ SELECT c.id, c.text, c.role, c.agent_id, c.created_at, c.source_type,
35
+ rank AS bm25_score
36
+ FROM chunks_fts f
37
+ JOIN chunks c ON c.id = f.rowid
38
+ WHERE f.text MATCH ? AND c.user_id = ? ${agentFilter}
39
+ ORDER BY rank
40
+ LIMIT ?
41
+ `).bind(query, userId, ...agentId ? [agentId] : [], limit * 3).all();
42
+ const queryVec = await embed(query, this.apiKey);
43
+ const vecResults = await this.vectorize.query(queryVec, {
44
+ topK: limit * 3,
45
+ filter: { user_id: userId, ...agentId ? { agent_id: agentId } : {} },
46
+ returnMetadata: "all"
47
+ });
48
+ const k = 60;
49
+ const scores = /* @__PURE__ */ new Map();
50
+ const bm25Rows = bm25Results.results || [];
51
+ for (let i = 0; i < bm25Rows.length; i++) {
52
+ const id = bm25Rows[i].id;
53
+ const existing = scores.get(id) || { score: 0, data: bm25Rows[i] };
54
+ existing.score += 1 / (k + i + 1);
55
+ scores.set(id, existing);
56
+ }
57
+ for (let i = 0; i < vecResults.matches.length; i++) {
58
+ const match = vecResults.matches[i];
59
+ const id = parseInt(match.id, 10);
60
+ const existing = scores.get(id) || { score: 0 };
61
+ existing.score += 1 / (k + i + 1);
62
+ scores.set(id, existing);
63
+ }
64
+ const now = Date.now();
65
+ for (const [id, entry] of scores) {
66
+ if (entry.data?.created_at) {
67
+ const age = now - new Date(entry.data.created_at).getTime();
68
+ const dayAge = age / (1e3 * 60 * 60 * 24);
69
+ const recencyBoost = Math.max(0, 1 - dayAge / 90) * 0.15;
70
+ entry.score += recencyBoost;
71
+ }
72
+ }
73
+ const ranked = [...scores.entries()].sort((a, b) => b[1].score - a[1].score).slice(0, limit);
74
+ const needFetch = ranked.filter(([_, v]) => !v.data).map(([id]) => id);
75
+ if (needFetch.length > 0) {
76
+ const placeholders = needFetch.map(() => "?").join(",");
77
+ const rows = await this.db.prepare(
78
+ `SELECT id, text, role, agent_id, created_at, source_type FROM chunks WHERE id IN (${placeholders})`
79
+ ).bind(...needFetch).all();
80
+ const rowMap = new Map(rows.results.map((r) => [r.id, r]));
81
+ for (const [id, entry] of scores) {
82
+ if (!entry.data && rowMap.has(id)) {
83
+ entry.data = rowMap.get(id);
84
+ }
85
+ }
86
+ }
87
+ return ranked.map(([id, entry]) => ({
88
+ id,
89
+ text: entry.data?.text || "",
90
+ score: entry.score,
91
+ role: entry.data?.role || "unknown",
92
+ agent_id: entry.data?.agent_id || "unknown",
93
+ created_at: entry.data?.created_at || "",
94
+ source_type: entry.data?.source_type || "unknown"
95
+ }));
96
+ }
97
+ // Ingest chunks (conversation turns)
98
+ async ingest(userId, chunks) {
99
+ let ingested = 0;
100
+ for (const chunk of chunks) {
101
+ const result = await this.db.prepare(`
102
+ INSERT INTO chunks (user_id, text, role, agent_id, source_type, token_count)
103
+ VALUES (?, ?, ?, ?, ?, ?)
104
+ `).bind(
105
+ userId,
106
+ chunk.text,
107
+ chunk.role,
108
+ chunk.agent_id,
109
+ chunk.source_type || "chatgpt",
110
+ Math.ceil(chunk.text.length / 4)
111
+ // rough token estimate
112
+ ).run();
113
+ const chunkId = result.meta.last_row_id;
114
+ const vec = await embed(chunk.text, this.apiKey);
115
+ await this.vectorize.upsert([{
116
+ id: String(chunkId),
117
+ values: vec,
118
+ metadata: {
119
+ user_id: userId,
120
+ agent_id: chunk.agent_id,
121
+ role: chunk.role,
122
+ source_type: chunk.source_type || "chatgpt"
123
+ }
124
+ }]);
125
+ ingested++;
126
+ }
127
+ return ingested;
128
+ }
129
+ // Remember a fact/preference/event
130
+ async remember(userId, text, category = "fact") {
131
+ const result = await this.db.prepare(`
132
+ INSERT INTO memories (user_id, text, category) VALUES (?, ?, ?)
133
+ `).bind(userId, text, category).run();
134
+ return result.meta.last_row_id;
135
+ }
136
+ // Forget (deprecate) a memory
137
+ async forget(userId, memoryId) {
138
+ const result = await this.db.prepare(`
139
+ UPDATE memories SET status = 'deprecated', updated_at = datetime('now')
140
+ WHERE id = ? AND user_id = ? AND status = 'active'
141
+ `).bind(memoryId, userId).run();
142
+ return (result.meta.changes || 0) > 0;
143
+ }
144
+ // Status
145
+ async status(userId) {
146
+ const chunkCount = await this.db.prepare(
147
+ "SELECT COUNT(*) as count FROM chunks WHERE user_id = ?"
148
+ ).bind(userId).first();
149
+ const memoryCount = await this.db.prepare(
150
+ "SELECT COUNT(*) as count FROM memories WHERE user_id = ? AND status = ?"
151
+ ).bind(userId, "active").first();
152
+ const agents = await this.db.prepare(
153
+ "SELECT DISTINCT agent_id FROM chunks WHERE user_id = ?"
154
+ ).bind(userId).all();
155
+ return {
156
+ chunks: chunkCount?.count || 0,
157
+ memories: memoryCount?.count || 0,
158
+ agents: (agents.results || []).map((r) => r.agent_id),
159
+ tier: "convenience"
160
+ // Tier 2 users only
161
+ };
162
+ }
163
+ };
164
+
165
+ export {
166
+ CloudCrystal
167
+ };
@@ -0,0 +1,106 @@
1
+ // src/crypto.ts
2
+ import { readFileSync, existsSync } from "fs";
3
+ import { createCipheriv, createDecipheriv, createHmac, randomBytes, hkdfSync } from "crypto";
4
+ import { join } from "path";
5
+ import { createHash } from "crypto";
6
+ var HOME = process.env.HOME || "";
7
+ var KEY_PATH = process.env.CRYSTAL_RELAY_KEY_PATH || join(HOME, ".openclaw", "secrets", "crystal-relay-key");
8
+ function loadRelayKey() {
9
+ if (!existsSync(KEY_PATH)) {
10
+ throw new Error(
11
+ `Relay key not found at ${KEY_PATH}
12
+ Generate one: openssl rand -base64 32 > ${KEY_PATH} && chmod 600 ${KEY_PATH}
13
+ Copy the same key to all trusted machines.`
14
+ );
15
+ }
16
+ const raw = readFileSync(KEY_PATH, "utf-8").trim();
17
+ const key = Buffer.from(raw, "base64");
18
+ if (key.length !== 32) {
19
+ throw new Error(`Relay key must be 32 bytes (256 bits). Got ${key.length} bytes. Regenerate with: openssl rand -base64 32`);
20
+ }
21
+ return key;
22
+ }
23
+ function deriveSigningKey(masterKey) {
24
+ return Buffer.from(hkdfSync("sha256", masterKey, "", "crystal-relay-sign", 32));
25
+ }
26
+ function encrypt(plaintext, masterKey) {
27
+ const nonce = randomBytes(12);
28
+ const cipher = createCipheriv("aes-256-gcm", masterKey, nonce);
29
+ const ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
30
+ const tag = cipher.getAuthTag();
31
+ const signingKey = deriveSigningKey(masterKey);
32
+ const hmacData = Buffer.concat([nonce, ciphertext, tag]);
33
+ const hmac = createHmac("sha256", signingKey).update(hmacData).digest("hex");
34
+ return {
35
+ v: 1,
36
+ nonce: nonce.toString("base64"),
37
+ ciphertext: ciphertext.toString("base64"),
38
+ tag: tag.toString("base64"),
39
+ hmac
40
+ };
41
+ }
42
+ function decrypt(payload, masterKey) {
43
+ if (payload.v !== 1) {
44
+ throw new Error(`Unknown payload version: ${payload.v}`);
45
+ }
46
+ const nonce = Buffer.from(payload.nonce, "base64");
47
+ const ciphertext = Buffer.from(payload.ciphertext, "base64");
48
+ const tag = Buffer.from(payload.tag, "base64");
49
+ const signingKey = deriveSigningKey(masterKey);
50
+ const hmacData = Buffer.concat([nonce, ciphertext, tag]);
51
+ const expectedHmac = createHmac("sha256", signingKey).update(hmacData).digest("hex");
52
+ if (payload.hmac !== expectedHmac) {
53
+ throw new Error("HMAC verification failed \u2014 blob rejected (tampered or wrong key)");
54
+ }
55
+ const decipher = createDecipheriv("aes-256-gcm", masterKey, nonce);
56
+ decipher.setAuthTag(tag);
57
+ return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
58
+ }
59
+ function encryptJSON(data, masterKey) {
60
+ const plaintext = Buffer.from(JSON.stringify(data), "utf-8");
61
+ return encrypt(plaintext, masterKey);
62
+ }
63
+ function decryptJSON(payload, masterKey) {
64
+ const plaintext = decrypt(payload, masterKey);
65
+ return JSON.parse(plaintext.toString("utf-8"));
66
+ }
67
+ function encryptFile(filePath, masterKey) {
68
+ const plaintext = readFileSync(filePath);
69
+ return encrypt(plaintext, masterKey);
70
+ }
71
+ var RELAY_KEY_PATH = KEY_PATH;
72
+ function generateRelayKey() {
73
+ return randomBytes(32);
74
+ }
75
+ function encodePairingString(key) {
76
+ if (key.length !== 32) throw new Error("Key must be 32 bytes");
77
+ return `mc1:${key.toString("base64")}`;
78
+ }
79
+ function decodePairingString(str) {
80
+ const trimmed = str.trim();
81
+ if (!trimmed.startsWith("mc1:")) {
82
+ throw new Error("Invalid pairing string (expected mc1: prefix)");
83
+ }
84
+ const key = Buffer.from(trimmed.slice(4), "base64");
85
+ if (key.length !== 32) {
86
+ throw new Error(`Invalid key length: expected 32 bytes, got ${key.length}`);
87
+ }
88
+ return key;
89
+ }
90
+ function hashBuffer(data) {
91
+ return createHash("sha256").update(data).digest("hex");
92
+ }
93
+
94
+ export {
95
+ loadRelayKey,
96
+ encrypt,
97
+ decrypt,
98
+ encryptJSON,
99
+ decryptJSON,
100
+ encryptFile,
101
+ RELAY_KEY_PATH,
102
+ generateRelayKey,
103
+ encodePairingString,
104
+ decodePairingString,
105
+ hashBuffer
106
+ };