@wipcomputer/memory-crystal 0.7.30 → 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 (211) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/SKILL.md +1 -1
  3. package/package.json +1 -1
  4. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/.env.example +0 -20
  5. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/.publish-skill.json +0 -1
  6. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/CHANGELOG.md +0 -1297
  7. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/CLA.md +0 -19
  8. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/LICENSE +0 -52
  9. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/README-ENTERPRISE.md +0 -226
  10. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/README.md +0 -151
  11. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/RELAY.md +0 -199
  12. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/SKILL.md +0 -462
  13. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/TECHNICAL.md +0 -656
  14. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/_trash/RELEASE-NOTES-v0-7-23.md +0 -48
  15. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/_trash/RELEASE-NOTES-v0-7-25.md +0 -24
  16. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/_trash/RELEASE-NOTES-v0-7-26.md +0 -7
  17. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/_trash/RELEASE-NOTES-v0-7-28.md +0 -31
  18. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/_trash/RELEASE-NOTES-v0-7-29.md +0 -28
  19. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/_trash/RELEASE-NOTES-v0-7-4.md +0 -64
  20. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/_trash/RELEASE-NOTES-v0-7-5.md +0 -19
  21. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/cloud/README.md +0 -116
  22. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/cloud/docs/gpt-system-instructions.md +0 -69
  23. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/cloud/migrations/0001_init.sql +0 -52
  24. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/migrations/0001_init.sql +0 -51
  25. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/migrations/0002_cloud_storage.sql +0 -49
  26. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/openclaw.plugin.json +0 -11
  27. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/package-lock.json +0 -4169
  28. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/package.json +0 -61
  29. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/scripts/crystal-capture.sh +0 -29
  30. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/scripts/deploy-cloud.sh +0 -153
  31. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/scripts/ldm-backup.sh +0 -116
  32. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/scripts/migrate-lance-to-sqlite.mjs +0 -218
  33. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/skills/memory/SKILL.md +0 -438
  34. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/wrangler-demo.toml +0 -8
  35. package/.worktrees/memory-crystal-private--cc-mini-fix-home-fallback/wrangler-mcp.toml +0 -24
  36. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/.env.example +0 -20
  37. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/.publish-skill.json +0 -1
  38. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/CHANGELOG.md +0 -1297
  39. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/CLA.md +0 -19
  40. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/LICENSE +0 -52
  41. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/README-ENTERPRISE.md +0 -226
  42. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/README.md +0 -151
  43. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/RELAY.md +0 -199
  44. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/RELEASE-NOTES-v0.7.30.md +0 -29
  45. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/SKILL.md +0 -462
  46. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/TECHNICAL.md +0 -656
  47. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/_trash/RELEASE-NOTES-v0-7-23.md +0 -48
  48. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/_trash/RELEASE-NOTES-v0-7-25.md +0 -24
  49. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/_trash/RELEASE-NOTES-v0-7-26.md +0 -7
  50. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/_trash/RELEASE-NOTES-v0-7-28.md +0 -31
  51. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/_trash/RELEASE-NOTES-v0-7-29.md +0 -28
  52. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/_trash/RELEASE-NOTES-v0-7-4.md +0 -64
  53. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/_trash/RELEASE-NOTES-v0-7-5.md +0 -19
  54. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/cloud/README.md +0 -116
  55. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/cloud/docs/gpt-system-instructions.md +0 -69
  56. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/cloud/migrations/0001_init.sql +0 -52
  57. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/migrations/0001_init.sql +0 -51
  58. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/migrations/0002_cloud_storage.sql +0 -49
  59. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/openclaw.plugin.json +0 -11
  60. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/package-lock.json +0 -4169
  61. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/package.json +0 -61
  62. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/scripts/crystal-capture.sh +0 -29
  63. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/scripts/deploy-cloud.sh +0 -153
  64. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/scripts/ldm-backup.sh +0 -116
  65. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/scripts/migrate-lance-to-sqlite.mjs +0 -218
  66. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/skills/memory/SKILL.md +0 -438
  67. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/wrangler-demo.toml +0 -8
  68. package/.worktrees/memory-crystal-private--cc-mini-release-notes-v0.7.30/wrangler-mcp.toml +0 -24
  69. package/_trash/RELEASE-NOTES-v0-7-23.md +0 -48
  70. package/_trash/RELEASE-NOTES-v0-7-25.md +0 -24
  71. package/_trash/RELEASE-NOTES-v0-7-26.md +0 -7
  72. package/_trash/RELEASE-NOTES-v0-7-28.md +0 -31
  73. package/_trash/RELEASE-NOTES-v0-7-29.md +0 -28
  74. package/_trash/RELEASE-NOTES-v0-7-4.md +0 -64
  75. package/_trash/RELEASE-NOTES-v0-7-5.md +0 -19
  76. package/_trash/RELEASE-NOTES-v0.7.30.md +0 -29
  77. package/dist/bridge.d.ts +0 -7
  78. package/dist/bridge.js +0 -14
  79. package/dist/bulk-copy.d.ts +0 -17
  80. package/dist/bulk-copy.js +0 -90
  81. package/dist/cc-hook.d.ts +0 -8
  82. package/dist/cc-hook.js +0 -368
  83. package/dist/cc-poller.d.ts +0 -1
  84. package/dist/cc-poller.js +0 -550
  85. package/dist/chunk-25LXQJ4Z.js +0 -110
  86. package/dist/chunk-2DRXIRQW.js +0 -97
  87. package/dist/chunk-2ZNH5F6E.js +0 -1281
  88. package/dist/chunk-3G3SFYYI.js +0 -288
  89. package/dist/chunk-3RG5ZIWI.js +0 -10
  90. package/dist/chunk-3S6TI23B.js +0 -97
  91. package/dist/chunk-3VFIJYS4.js +0 -818
  92. package/dist/chunk-52QE3YI3.js +0 -1169
  93. package/dist/chunk-57RP3DIN.js +0 -1205
  94. package/dist/chunk-5HSZ4W2P.js +0 -62
  95. package/dist/chunk-645IPXW3.js +0 -290
  96. package/dist/chunk-7A7ELD4C.js +0 -1205
  97. package/dist/chunk-7FYY4GZM.js +0 -1205
  98. package/dist/chunk-7IUE7ODU.js +0 -254
  99. package/dist/chunk-7RMLKZIS.js +0 -108
  100. package/dist/chunk-AA3OPP4Z.js +0 -432
  101. package/dist/chunk-AEWLSYPH.js +0 -72
  102. package/dist/chunk-ASSZDR6I.js +0 -108
  103. package/dist/chunk-AYRJVWUC.js +0 -1205
  104. package/dist/chunk-CCYI5O3D.js +0 -148
  105. package/dist/chunk-D3I3ZSE2.js +0 -411
  106. package/dist/chunk-DACSKLY6.js +0 -219
  107. package/dist/chunk-DW5B4BL7.js +0 -108
  108. package/dist/chunk-EKSACBTJ.js +0 -1070
  109. package/dist/chunk-EXEZZADG.js +0 -248
  110. package/dist/chunk-F3Y7EL7K.js +0 -83
  111. package/dist/chunk-FBQWSDPC.js +0 -1328
  112. package/dist/chunk-FHRZNOMW.js +0 -1205
  113. package/dist/chunk-IM7N24MT.js +0 -129
  114. package/dist/chunk-IPNYIXFK.js +0 -1178
  115. package/dist/chunk-J7MRSZIO.js +0 -167
  116. package/dist/chunk-JITKI2OI.js +0 -106
  117. package/dist/chunk-JWZXYVET.js +0 -1068
  118. package/dist/chunk-KCQUXVYT.js +0 -108
  119. package/dist/chunk-KOQ43OX6.js +0 -1281
  120. package/dist/chunk-KYVWO6ZM.js +0 -1069
  121. package/dist/chunk-L3VHARQH.js +0 -413
  122. package/dist/chunk-LBWDS6BE.js +0 -288
  123. package/dist/chunk-LOVAHSQV.js +0 -411
  124. package/dist/chunk-LQOYCAGG.js +0 -446
  125. package/dist/chunk-LWAIPJ2W.js +0 -146
  126. package/dist/chunk-M5DHKW7M.js +0 -127
  127. package/dist/chunk-MBKCIJHM.js +0 -1328
  128. package/dist/chunk-MK42FMEG.js +0 -147
  129. package/dist/chunk-MOBMYHKL.js +0 -1205
  130. package/dist/chunk-MPLTNMRG.js +0 -67
  131. package/dist/chunk-NIJCVN3O.js +0 -147
  132. package/dist/chunk-NZCFSZQ7.js +0 -1205
  133. package/dist/chunk-O2UITJGH.js +0 -465
  134. package/dist/chunk-OCRA44AZ.js +0 -108
  135. package/dist/chunk-P3KJR66H.js +0 -117
  136. package/dist/chunk-PEK6JH65.js +0 -432
  137. package/dist/chunk-PJ6FFKEX.js +0 -77
  138. package/dist/chunk-PLUBBZYR.js +0 -800
  139. package/dist/chunk-PNKVD2UK.js +0 -26
  140. package/dist/chunk-PSQZURHO.js +0 -229
  141. package/dist/chunk-SGL6ISBJ.js +0 -1061
  142. package/dist/chunk-SJABZZT5.js +0 -97
  143. package/dist/chunk-TD3P3K32.js +0 -1199
  144. package/dist/chunk-TMDZJJKV.js +0 -288
  145. package/dist/chunk-UNHVZB5G.js +0 -411
  146. package/dist/chunk-VAFTWSTE.js +0 -1061
  147. package/dist/chunk-VNFXFQBB.js +0 -217
  148. package/dist/chunk-X3GVFKSJ.js +0 -1205
  149. package/dist/chunk-XZ3S56RQ.js +0 -1061
  150. package/dist/chunk-Y72C7F6O.js +0 -148
  151. package/dist/chunk-YLICP577.js +0 -1205
  152. package/dist/chunk-YX6AXLVK.js +0 -159
  153. package/dist/chunk-ZCQYHTNU.js +0 -146
  154. package/dist/cli.d.ts +0 -1
  155. package/dist/cli.js +0 -1105
  156. package/dist/cloud-crystal.js +0 -6
  157. package/dist/core.d.ts +0 -232
  158. package/dist/core.js +0 -12
  159. package/dist/crypto.d.ts +0 -20
  160. package/dist/crypto.js +0 -27
  161. package/dist/crystal-capture.sh +0 -29
  162. package/dist/crystal-serve.d.ts +0 -4
  163. package/dist/crystal-serve.js +0 -252
  164. package/dist/dev-update-SZ2Z4WCQ.js +0 -6
  165. package/dist/discover.d.ts +0 -30
  166. package/dist/discover.js +0 -177
  167. package/dist/doctor.d.ts +0 -9
  168. package/dist/doctor.js +0 -334
  169. package/dist/dream-weaver.d.ts +0 -8
  170. package/dist/dream-weaver.js +0 -56
  171. package/dist/file-sync.d.ts +0 -48
  172. package/dist/file-sync.js +0 -18
  173. package/dist/installer.d.ts +0 -61
  174. package/dist/installer.js +0 -676
  175. package/dist/ldm-backup.sh +0 -116
  176. package/dist/ldm.d.ts +0 -50
  177. package/dist/ldm.js +0 -32
  178. package/dist/mcp-server.d.ts +0 -1
  179. package/dist/mcp-server.js +0 -265
  180. package/dist/migrate.d.ts +0 -1
  181. package/dist/migrate.js +0 -89
  182. package/dist/mirror-sync.d.ts +0 -1
  183. package/dist/mirror-sync.js +0 -159
  184. package/dist/oc-backfill.d.ts +0 -19
  185. package/dist/oc-backfill.js +0 -74
  186. package/dist/openclaw.d.ts +0 -5
  187. package/dist/openclaw.js +0 -423
  188. package/dist/pair.d.ts +0 -4
  189. package/dist/pair.js +0 -75
  190. package/dist/poller.d.ts +0 -1
  191. package/dist/poller.js +0 -634
  192. package/dist/role.d.ts +0 -24
  193. package/dist/role.js +0 -13
  194. package/dist/search-pipeline-4K4OJSSS.js +0 -255
  195. package/dist/search-pipeline-4PRS6LI7.js +0 -280
  196. package/dist/search-pipeline-7UJMXPLO.js +0 -280
  197. package/dist/search-pipeline-DQTRLGBH.js +0 -74
  198. package/dist/search-pipeline-HNG37REH.js +0 -282
  199. package/dist/search-pipeline-IZFPLBUB.js +0 -280
  200. package/dist/search-pipeline-MID6F26Q.js +0 -73
  201. package/dist/search-pipeline-N52JZFNN.js +0 -282
  202. package/dist/search-pipeline-OPB2PRQQ.js +0 -280
  203. package/dist/search-pipeline-VXTE5HAD.js +0 -262
  204. package/dist/search-pipeline-XHFKADRG.js +0 -73
  205. package/dist/staging.d.ts +0 -29
  206. package/dist/staging.js +0 -21
  207. package/dist/summarize.d.ts +0 -19
  208. package/dist/summarize.js +0 -10
  209. package/dist/worker-demo.js +0 -186
  210. package/dist/worker-mcp.js +0 -404
  211. package/dist/worker.js +0 -137
@@ -1,1199 +0,0 @@
1
- // src/core.ts
2
- import * as lancedb from "@lancedb/lancedb";
3
- import Database from "better-sqlite3";
4
- import * as sqliteVec from "sqlite-vec";
5
- import { readFileSync, existsSync, mkdirSync, readdirSync, statSync } from "fs";
6
- import { execSync } from "child_process";
7
- import { join, relative, extname, basename } from "path";
8
- import { createHash } from "crypto";
9
- import http from "http";
10
- import https from "https";
11
- async function embedOpenAI(texts, apiKey, model) {
12
- return new Promise((resolve, reject) => {
13
- const body = JSON.stringify({ input: texts, model });
14
- const req = https.request({
15
- hostname: "api.openai.com",
16
- path: "/v1/embeddings",
17
- method: "POST",
18
- headers: {
19
- "Content-Type": "application/json",
20
- "Authorization": `Bearer ${apiKey}`,
21
- "Content-Length": Buffer.byteLength(body)
22
- },
23
- timeout: 3e4
24
- }, (res) => {
25
- let data = "";
26
- res.on("data", (chunk) => data += chunk);
27
- res.on("end", () => {
28
- if (res.statusCode !== 200) {
29
- reject(new Error(`OpenAI API error ${res.statusCode}: ${data.slice(0, 200)}`));
30
- return;
31
- }
32
- const parsed = JSON.parse(data);
33
- resolve(parsed.data.map((d) => d.embedding));
34
- });
35
- });
36
- req.on("error", reject);
37
- req.on("timeout", () => {
38
- req.destroy();
39
- reject(new Error("OpenAI timeout"));
40
- });
41
- req.write(body);
42
- req.end();
43
- });
44
- }
45
- async function embedOllama(texts, host, model) {
46
- const results = [];
47
- for (const text of texts) {
48
- const result = await new Promise((resolve, reject) => {
49
- const url = new URL("/api/embeddings", host);
50
- const body = JSON.stringify({ model, prompt: text });
51
- const req = http.request({
52
- hostname: url.hostname,
53
- port: url.port,
54
- path: url.pathname,
55
- method: "POST",
56
- headers: {
57
- "Content-Type": "application/json",
58
- "Content-Length": Buffer.byteLength(body)
59
- },
60
- timeout: 15e3
61
- }, (res) => {
62
- let data = "";
63
- res.on("data", (chunk) => data += chunk);
64
- res.on("end", () => {
65
- if (res.statusCode !== 200) {
66
- reject(new Error(`Ollama error ${res.statusCode}: ${data.slice(0, 200)}`));
67
- return;
68
- }
69
- resolve(JSON.parse(data).embedding);
70
- });
71
- });
72
- req.on("error", reject);
73
- req.on("timeout", () => {
74
- req.destroy();
75
- reject(new Error("Ollama timeout"));
76
- });
77
- req.write(body);
78
- req.end();
79
- });
80
- results.push(result);
81
- }
82
- return results;
83
- }
84
- async function embedGoogle(texts, apiKey, model) {
85
- return new Promise((resolve, reject) => {
86
- const body = JSON.stringify({
87
- requests: texts.map((text) => ({ model: `models/${model}`, content: { parts: [{ text }] } }))
88
- });
89
- const req = https.request({
90
- hostname: "generativelanguage.googleapis.com",
91
- path: `/v1beta/models/${model}:batchEmbedContents?key=${apiKey}`,
92
- method: "POST",
93
- headers: {
94
- "Content-Type": "application/json",
95
- "Content-Length": Buffer.byteLength(body)
96
- },
97
- timeout: 3e4
98
- }, (res) => {
99
- let data = "";
100
- res.on("data", (chunk) => data += chunk);
101
- res.on("end", () => {
102
- if (res.statusCode !== 200) {
103
- reject(new Error(`Google API error ${res.statusCode}: ${data.slice(0, 200)}`));
104
- return;
105
- }
106
- const parsed = JSON.parse(data);
107
- resolve(parsed.embeddings.map((e) => e.values));
108
- });
109
- });
110
- req.on("error", reject);
111
- req.on("timeout", () => {
112
- req.destroy();
113
- reject(new Error("Google timeout"));
114
- });
115
- req.write(body);
116
- req.end();
117
- });
118
- }
119
- var Crystal = class _Crystal {
120
- config;
121
- lanceDb = null;
122
- sqliteDb = null;
123
- chunksTable = null;
124
- vecDimensions = null;
125
- constructor(config) {
126
- this.config = config;
127
- if (!existsSync(config.dataDir)) {
128
- mkdirSync(config.dataDir, { recursive: true });
129
- }
130
- }
131
- // ── Initialization ──
132
- async init() {
133
- const lanceDir = join(this.config.dataDir, "lance");
134
- const sqlitePath = join(this.config.dataDir, "crystal.db");
135
- if (!existsSync(lanceDir)) mkdirSync(lanceDir, { recursive: true });
136
- this.lanceDb = await lancedb.connect(lanceDir);
137
- this.sqliteDb = new Database(sqlitePath);
138
- this.sqliteDb.pragma("journal_mode = WAL");
139
- sqliteVec.load(this.sqliteDb);
140
- this.initSqliteTables();
141
- this.initChunksTables();
142
- await this.initLanceTables();
143
- }
144
- initSqliteTables() {
145
- const db = this.sqliteDb;
146
- db.exec(`
147
- CREATE TABLE IF NOT EXISTS sources (
148
- id INTEGER PRIMARY KEY AUTOINCREMENT,
149
- type TEXT NOT NULL,
150
- uri TEXT NOT NULL,
151
- title TEXT,
152
- agent_id TEXT NOT NULL,
153
- metadata TEXT DEFAULT '{}',
154
- ingested_at TEXT NOT NULL,
155
- chunk_count INTEGER DEFAULT 0
156
- );
157
-
158
- CREATE TABLE IF NOT EXISTS capture_state (
159
- agent_id TEXT NOT NULL,
160
- source_id TEXT NOT NULL,
161
- last_message_count INTEGER DEFAULT 0,
162
- capture_count INTEGER DEFAULT 0,
163
- last_capture_at TEXT,
164
- PRIMARY KEY (agent_id, source_id)
165
- );
166
-
167
- CREATE TABLE IF NOT EXISTS memories (
168
- id INTEGER PRIMARY KEY AUTOINCREMENT,
169
- text TEXT NOT NULL,
170
- category TEXT NOT NULL DEFAULT 'fact',
171
- confidence REAL NOT NULL DEFAULT 1.0,
172
- source_ids TEXT DEFAULT '[]',
173
- status TEXT NOT NULL DEFAULT 'active',
174
- created_at TEXT NOT NULL,
175
- updated_at TEXT NOT NULL
176
- );
177
-
178
- CREATE TABLE IF NOT EXISTS entities (
179
- id INTEGER PRIMARY KEY AUTOINCREMENT,
180
- name TEXT NOT NULL UNIQUE,
181
- type TEXT NOT NULL DEFAULT 'concept',
182
- description TEXT,
183
- properties TEXT DEFAULT '{}',
184
- created_at TEXT NOT NULL,
185
- updated_at TEXT NOT NULL
186
- );
187
-
188
- CREATE TABLE IF NOT EXISTS relationships (
189
- id INTEGER PRIMARY KEY AUTOINCREMENT,
190
- source_id INTEGER NOT NULL REFERENCES entities(id),
191
- target_id INTEGER NOT NULL REFERENCES entities(id),
192
- type TEXT NOT NULL,
193
- description TEXT,
194
- weight REAL DEFAULT 1.0,
195
- valid_from TEXT NOT NULL,
196
- valid_until TEXT,
197
- created_at TEXT NOT NULL
198
- );
199
-
200
- CREATE INDEX IF NOT EXISTS idx_sources_agent ON sources(agent_id);
201
- CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status);
202
- CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
203
- CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_id);
204
- CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_id);
205
-
206
- -- Source file indexing (optional feature)
207
- CREATE TABLE IF NOT EXISTS source_collections (
208
- id INTEGER PRIMARY KEY AUTOINCREMENT,
209
- name TEXT NOT NULL UNIQUE,
210
- root_path TEXT NOT NULL,
211
- glob_patterns TEXT NOT NULL DEFAULT '["**/*"]',
212
- ignore_patterns TEXT NOT NULL DEFAULT '[]',
213
- file_count INTEGER DEFAULT 0,
214
- chunk_count INTEGER DEFAULT 0,
215
- last_sync_at TEXT,
216
- created_at TEXT NOT NULL
217
- );
218
-
219
- CREATE TABLE IF NOT EXISTS source_files (
220
- id INTEGER PRIMARY KEY AUTOINCREMENT,
221
- collection_id INTEGER NOT NULL REFERENCES source_collections(id) ON DELETE CASCADE,
222
- file_path TEXT NOT NULL,
223
- file_hash TEXT NOT NULL,
224
- file_size INTEGER NOT NULL,
225
- chunk_count INTEGER DEFAULT 0,
226
- last_indexed_at TEXT NOT NULL
227
- );
228
-
229
- CREATE UNIQUE INDEX IF NOT EXISTS idx_source_files_path ON source_files(collection_id, file_path);
230
- CREATE INDEX IF NOT EXISTS idx_source_files_collection ON source_files(collection_id);
231
- `);
232
- }
233
- initChunksTables() {
234
- const db = this.sqliteDb;
235
- db.exec(`
236
- CREATE TABLE IF NOT EXISTS chunks (
237
- id INTEGER PRIMARY KEY AUTOINCREMENT,
238
- text TEXT NOT NULL,
239
- text_hash TEXT NOT NULL,
240
- role TEXT,
241
- source_type TEXT,
242
- source_id TEXT,
243
- agent_id TEXT,
244
- token_count INTEGER,
245
- created_at TEXT NOT NULL
246
- );
247
-
248
- CREATE INDEX IF NOT EXISTS idx_chunks_agent ON chunks(agent_id);
249
- CREATE INDEX IF NOT EXISTS idx_chunks_source ON chunks(source_type);
250
- CREATE INDEX IF NOT EXISTS idx_chunks_hash ON chunks(text_hash);
251
- CREATE INDEX IF NOT EXISTS idx_chunks_created ON chunks(created_at);
252
-
253
- -- FTS5 full-text search table
254
- CREATE VIRTUAL TABLE IF NOT EXISTS chunks_fts USING fts5(
255
- text,
256
- tokenize='porter unicode61'
257
- );
258
-
259
- -- Sync trigger: populate FTS on chunk insert
260
- CREATE TRIGGER IF NOT EXISTS chunks_fts_insert AFTER INSERT ON chunks
261
- BEGIN
262
- INSERT INTO chunks_fts(rowid, text) VALUES (NEW.id, NEW.text);
263
- END;
264
- `);
265
- const vecTable = db.prepare(
266
- `SELECT name FROM sqlite_master WHERE type='table' AND name='chunks_vec'`
267
- ).get();
268
- if (vecTable) {
269
- try {
270
- const row = db.prepare("SELECT embedding FROM chunks_vec LIMIT 1").get();
271
- if (row?.embedding) {
272
- this.vecDimensions = row.embedding.length / 4;
273
- }
274
- } catch {
275
- }
276
- }
277
- }
278
- ensureVecTable(dimensions) {
279
- const db = this.sqliteDb;
280
- const existing = db.prepare(
281
- `SELECT name FROM sqlite_master WHERE type='table' AND name='chunks_vec'`
282
- ).get();
283
- if (!existing) {
284
- db.exec(`
285
- CREATE VIRTUAL TABLE chunks_vec USING vec0(
286
- chunk_id INTEGER PRIMARY KEY,
287
- embedding float[${dimensions}] distance_metric=cosine
288
- );
289
- `);
290
- }
291
- this.vecDimensions = dimensions;
292
- }
293
- async initLanceTables() {
294
- const db = this.lanceDb;
295
- const tableNames = await db.tableNames();
296
- if (tableNames.includes("chunks")) {
297
- this.chunksTable = await db.openTable("chunks");
298
- }
299
- }
300
- // ── Embedding ──
301
- async embed(texts) {
302
- if (texts.length === 0) return [];
303
- const cfg = this.config;
304
- switch (cfg.embeddingProvider) {
305
- case "openai": {
306
- if (!cfg.openaiApiKey) throw new Error("OpenAI API key required");
307
- const model = cfg.openaiModel || "text-embedding-3-small";
308
- const maxCharsPerBatch = 8e5;
309
- const results = [];
310
- let batch = [];
311
- let batchChars = 0;
312
- for (const text of texts) {
313
- if (batchChars + text.length > maxCharsPerBatch && batch.length > 0) {
314
- results.push(...await embedOpenAI(batch, cfg.openaiApiKey, model));
315
- batch = [];
316
- batchChars = 0;
317
- }
318
- batch.push(text);
319
- batchChars += text.length;
320
- }
321
- if (batch.length > 0) {
322
- results.push(...await embedOpenAI(batch, cfg.openaiApiKey, model));
323
- }
324
- return results;
325
- }
326
- case "ollama":
327
- return embedOllama(texts, cfg.ollamaHost || "http://localhost:11434", cfg.ollamaModel || "nomic-embed-text");
328
- case "google":
329
- if (!cfg.googleApiKey) throw new Error("Google API key required");
330
- return embedGoogle(texts, cfg.googleApiKey, cfg.googleModel || "text-embedding-004");
331
- default:
332
- throw new Error(`Unknown embedding provider: ${cfg.embeddingProvider}`);
333
- }
334
- }
335
- // ── Chunking ──
336
- chunkText(text, targetTokens = 400, overlapTokens = 80) {
337
- const targetChars = targetTokens * 4;
338
- const overlapChars = overlapTokens * 4;
339
- const chunks = [];
340
- let start = 0;
341
- while (start < text.length) {
342
- let end = Math.min(start + targetChars, text.length);
343
- if (end < text.length) {
344
- const minBreak = start + Math.floor(targetChars * 0.5);
345
- const paraBreak = text.lastIndexOf("\n\n", end);
346
- if (paraBreak > minBreak) {
347
- end = paraBreak;
348
- } else {
349
- const sentBreak = text.lastIndexOf(". ", end);
350
- if (sentBreak > minBreak) {
351
- end = sentBreak + 1;
352
- }
353
- }
354
- }
355
- const chunk = text.slice(start, end).trim();
356
- if (chunk.length > 0) chunks.push(chunk);
357
- if (end >= text.length) break;
358
- start = end - overlapChars;
359
- if (start <= (chunks.length > 0 ? end - targetChars : 0)) {
360
- start = end;
361
- }
362
- }
363
- return chunks;
364
- }
365
- // ── Ingest ──
366
- async ingest(chunks) {
367
- if (chunks.length === 0) return 0;
368
- const db = this.sqliteDb;
369
- const newChunks = chunks.filter((c) => {
370
- const hash = createHash("sha256").update(c.text).digest("hex");
371
- return !db.prepare("SELECT 1 FROM chunks WHERE text_hash = ?").get(hash);
372
- });
373
- if (newChunks.length === 0) return 0;
374
- const texts = newChunks.map((c) => c.text);
375
- const embeddings = await this.embed(texts);
376
- if (!this.vecDimensions && embeddings.length > 0) {
377
- this.ensureVecTable(embeddings[0].length);
378
- }
379
- const insertChunk = db.prepare(`
380
- INSERT INTO chunks (text, text_hash, role, source_type, source_id, agent_id, token_count, created_at)
381
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)
382
- `);
383
- const insertVec = db.prepare(`
384
- INSERT INTO chunks_vec (chunk_id, embedding) VALUES (?, ?)
385
- `);
386
- const transaction = db.transaction(() => {
387
- for (let i = 0; i < newChunks.length; i++) {
388
- const c = newChunks[i];
389
- const hash = createHash("sha256").update(c.text).digest("hex");
390
- const result = insertChunk.run(
391
- c.text,
392
- hash,
393
- c.role,
394
- c.source_type,
395
- c.source_id,
396
- c.agent_id,
397
- c.token_count,
398
- c.created_at || (/* @__PURE__ */ new Date()).toISOString()
399
- );
400
- const chunkId = typeof result.lastInsertRowid === "bigint" ? result.lastInsertRowid : BigInt(result.lastInsertRowid);
401
- insertVec.run(chunkId, new Float32Array(embeddings[i]));
402
- }
403
- });
404
- transaction();
405
- const records = newChunks.map((chunk, i) => ({
406
- text: chunk.text,
407
- vector: embeddings[i],
408
- role: chunk.role,
409
- source_type: chunk.source_type,
410
- source_id: chunk.source_id,
411
- agent_id: chunk.agent_id,
412
- token_count: chunk.token_count,
413
- created_at: chunk.created_at || (/* @__PURE__ */ new Date()).toISOString()
414
- }));
415
- try {
416
- if (!this.chunksTable) {
417
- this.chunksTable = await this.lanceDb.createTable("chunks", records);
418
- } else {
419
- await this.chunksTable.add(records);
420
- }
421
- } catch (err) {
422
- console.warn("LanceDB dual-write failed (non-fatal):", err.message);
423
- }
424
- return newChunks.length;
425
- }
426
- // ── Recency helpers ──
427
- recencyWeight(ageDays) {
428
- return Math.max(0.3, Math.exp(-ageDays * 0.1));
429
- }
430
- /** Parse relative time strings ("24h", "7d", "30d") or ISO dates into ISO date strings. */
431
- parseSince(since) {
432
- const match = since.match(/^(\d+)(h|d)$/);
433
- if (match) {
434
- const [, num, unit] = match;
435
- const ms = unit === "h" ? parseInt(num) * 36e5 : parseInt(num) * 864e5;
436
- return new Date(Date.now() - ms).toISOString();
437
- }
438
- const parsed = new Date(since);
439
- if (!isNaN(parsed.getTime())) return parsed.toISOString();
440
- return void 0;
441
- }
442
- freshnessLabel(ageDays) {
443
- if (ageDays < 3) return "fresh";
444
- if (ageDays < 7) return "recent";
445
- if (ageDays < 14) return "aging";
446
- return "stale";
447
- }
448
- // ── Search (Hybrid: BM25 + Vector + RRF fusion + Recency) ──
449
- async search(query, limit = 5, filter) {
450
- const db = this.sqliteDb;
451
- const sqliteChunks = db.prepare("SELECT COUNT(*) as count FROM chunks").get()?.count || 0;
452
- let lanceChunks = 0;
453
- if (this.chunksTable) {
454
- try {
455
- lanceChunks = await this.chunksTable.countRows();
456
- } catch {
457
- }
458
- }
459
- if (sqliteChunks === 0 || lanceChunks > 0 && sqliteChunks < lanceChunks * 0.5) {
460
- return this.searchLanceFallback(query, limit, filter);
461
- }
462
- const sinceDate = filter?.since ? this.parseSince(filter.since) : void 0;
463
- const [embedding] = await this.embed([query]);
464
- const fetchLimit = Math.max(limit * 5, 50);
465
- const vecResults = this.searchVec(embedding, fetchLimit, { ...filter, sinceDate });
466
- const ftsResults = this.searchFTS(query, fetchLimit, { ...filter, sinceDate });
467
- const fused = this.reciprocalRankFusion([ftsResults, vecResults], [2, 1]);
468
- const now = Date.now();
469
- const scored = fused.map((r) => {
470
- const ageDays = r.created_at ? (now - new Date(r.created_at).getTime()) / 864e5 : 0;
471
- const recency = r.created_at ? this.recencyWeight(ageDays) : 1;
472
- const rescaled = Math.min(r.score * recency * 8, 1);
473
- return {
474
- ...r,
475
- score: rescaled,
476
- freshness: r.created_at ? this.freshnessLabel(ageDays) : void 0
477
- };
478
- });
479
- return scored.sort((a, b) => b.score - a.score).slice(0, limit);
480
- }
481
- /** Vector search via sqlite-vec. Two-step pattern: MATCH first, then JOIN. */
482
- searchVec(embedding, limit, filter) {
483
- const db = this.sqliteDb;
484
- if (!this.vecDimensions) return [];
485
- const vecRows = db.prepare(`
486
- SELECT chunk_id, distance
487
- FROM chunks_vec
488
- WHERE embedding MATCH ? AND k = ?
489
- `).all(new Float32Array(embedding), limit);
490
- if (vecRows.length === 0) return [];
491
- const ids = vecRows.map((r) => r.chunk_id);
492
- const distMap = new Map(vecRows.map((r) => [r.chunk_id, r.distance]));
493
- const placeholders = ids.map(() => "?").join(",");
494
- let sql = `SELECT id, text, role, source_type, source_id, agent_id, created_at FROM chunks WHERE id IN (${placeholders})`;
495
- const params = [...ids];
496
- if (filter?.agent_id) {
497
- sql += " AND agent_id = ?";
498
- params.push(filter.agent_id);
499
- }
500
- if (filter?.source_type) {
501
- sql += " AND source_type = ?";
502
- params.push(filter.source_type);
503
- }
504
- if (filter?.sinceDate) {
505
- sql += " AND created_at >= ?";
506
- params.push(filter.sinceDate);
507
- }
508
- const rows = db.prepare(sql).all(...params);
509
- return rows.map((row) => ({
510
- text: row.text,
511
- role: row.role,
512
- score: 1 - (distMap.get(row.id) || 1),
513
- // cosine similarity from distance
514
- source_type: row.source_type,
515
- source_id: row.source_id,
516
- agent_id: row.agent_id,
517
- created_at: row.created_at
518
- }));
519
- }
520
- /** Full-text search via FTS5 with BM25 scoring. */
521
- searchFTS(query, limit, filter) {
522
- const db = this.sqliteDb;
523
- const ftsQuery = this.buildFTS5Query(query);
524
- if (!ftsQuery) return [];
525
- let sql = `
526
- SELECT c.id, c.text, c.role, c.source_type, c.source_id, c.agent_id, c.created_at,
527
- bm25(chunks_fts) as bm25_score
528
- FROM chunks_fts f
529
- JOIN chunks c ON c.id = f.rowid
530
- WHERE chunks_fts MATCH ?
531
- `;
532
- const params = [ftsQuery];
533
- if (filter?.agent_id) {
534
- sql += " AND c.agent_id = ?";
535
- params.push(filter.agent_id);
536
- }
537
- if (filter?.source_type) {
538
- sql += " AND c.source_type = ?";
539
- params.push(filter.source_type);
540
- }
541
- if (filter?.sinceDate) {
542
- sql += " AND c.created_at >= ?";
543
- params.push(filter.sinceDate);
544
- }
545
- sql += " ORDER BY bm25_score LIMIT ?";
546
- params.push(limit);
547
- const rows = db.prepare(sql).all(...params);
548
- return rows.map((row) => ({
549
- text: row.text,
550
- role: row.role,
551
- // BM25 scores are negative (lower = better). Normalize to [0..1).
552
- // |x| / (1 + |x|) maps: strong(-10)->0.91, medium(-2)->0.67, weak(-0.5)->0.33
553
- score: Math.abs(row.bm25_score) / (1 + Math.abs(row.bm25_score)),
554
- source_type: row.source_type,
555
- source_id: row.source_id,
556
- agent_id: row.agent_id,
557
- created_at: row.created_at
558
- }));
559
- }
560
- /** Build a safe FTS5 query from user input. */
561
- buildFTS5Query(query) {
562
- const terms = query.split(/\s+/).map((t) => t.replace(/[^\p{L}\p{N}']/gu, "").toLowerCase()).filter((t) => t.length > 0);
563
- if (terms.length === 0) return null;
564
- if (terms.length === 1) return `"${terms[0]}"*`;
565
- return terms.map((t) => `"${t}"*`).join(" AND ");
566
- }
567
- /**
568
- * Reciprocal Rank Fusion. Ported from QMD (MIT License, Tobi Lutke, 2024-2026).
569
- * Fuses multiple ranked result lists into one using RRF scoring.
570
- * Uses text content as dedup key (instead of QMD's file path).
571
- */
572
- reciprocalRankFusion(resultLists, weights = [], k = 60) {
573
- const scores = /* @__PURE__ */ new Map();
574
- for (let listIdx = 0; listIdx < resultLists.length; listIdx++) {
575
- const list = resultLists[listIdx];
576
- if (!list) continue;
577
- const weight = weights[listIdx] ?? 1;
578
- for (let rank = 0; rank < list.length; rank++) {
579
- const result = list[rank];
580
- if (!result) continue;
581
- const rrfContribution = weight / (k + rank + 1);
582
- const dedup = result.text.slice(0, 200);
583
- const existing = scores.get(dedup);
584
- if (existing) {
585
- existing.rrfScore += rrfContribution;
586
- existing.topRank = Math.min(existing.topRank, rank);
587
- } else {
588
- scores.set(dedup, {
589
- result,
590
- rrfScore: rrfContribution,
591
- topRank: rank
592
- });
593
- }
594
- }
595
- }
596
- for (const entry of scores.values()) {
597
- if (entry.topRank === 0) {
598
- entry.rrfScore += 0.05;
599
- } else if (entry.topRank <= 2) {
600
- entry.rrfScore += 0.02;
601
- }
602
- }
603
- return Array.from(scores.values()).sort((a, b) => b.rrfScore - a.rrfScore).map((e) => ({ ...e.result, score: e.rrfScore }));
604
- }
605
- /** LanceDB fallback for search (used when sqlite-vec tables are empty, pre-migration). */
606
- async searchLanceFallback(query, limit, filter) {
607
- if (!this.chunksTable) return [];
608
- const [embedding] = await this.embed([query]);
609
- const fetchLimit = Math.max(limit * 3, 30);
610
- let queryBuilder = this.chunksTable.vectorSearch(embedding).distanceType("cosine").limit(fetchLimit);
611
- if (filter?.agent_id) {
612
- queryBuilder = queryBuilder.where(`agent_id = '${filter.agent_id}'`);
613
- }
614
- if (filter?.source_type) {
615
- queryBuilder = queryBuilder.where(`source_type = '${filter.source_type}'`);
616
- }
617
- const results = await queryBuilder.toArray();
618
- const now = Date.now();
619
- return results.map((row) => {
620
- const cosine = row._distance != null ? 1 - row._distance : 0;
621
- const createdAt = row.created_at || "";
622
- const ageDays = createdAt ? (now - new Date(createdAt).getTime()) / 864e5 : 0;
623
- const weight = createdAt ? this.recencyWeight(ageDays) : 1;
624
- return {
625
- text: row.text,
626
- role: row.role,
627
- score: cosine * weight,
628
- source_type: row.source_type,
629
- source_id: row.source_id,
630
- agent_id: row.agent_id,
631
- created_at: createdAt,
632
- freshness: createdAt ? this.freshnessLabel(ageDays) : void 0
633
- };
634
- }).sort((a, b) => b.score - a.score).slice(0, limit);
635
- }
636
- // ── Remember (explicit fact storage) ──
637
- async remember(text, category = "fact") {
638
- const db = this.sqliteDb;
639
- const now = (/* @__PURE__ */ new Date()).toISOString();
640
- const stmt = db.prepare(`
641
- INSERT INTO memories (text, category, confidence, source_ids, status, created_at, updated_at)
642
- VALUES (?, ?, 1.0, '[]', 'active', ?, ?)
643
- `);
644
- const result = stmt.run(text, category, now, now);
645
- await this.ingest([{
646
- text,
647
- role: "system",
648
- source_type: "manual",
649
- source_id: `memory:${result.lastInsertRowid}`,
650
- agent_id: "system",
651
- token_count: Math.ceil(text.length / 4),
652
- created_at: now
653
- }]);
654
- return result.lastInsertRowid;
655
- }
656
- // ── Forget (deprecate a memory) ──
657
- forget(memoryId) {
658
- const db = this.sqliteDb;
659
- const now = (/* @__PURE__ */ new Date()).toISOString();
660
- const result = db.prepare(`
661
- UPDATE memories SET status = 'deprecated', updated_at = ? WHERE id = ? AND status = 'active'
662
- `).run(now, memoryId);
663
- return result.changes > 0;
664
- }
665
- // ── Status ──
666
- async status() {
667
- const db = this.sqliteDb;
668
- const sqliteChunks = db.prepare("SELECT COUNT(*) as count FROM chunks").get()?.count || 0;
669
- let lanceChunks = 0;
670
- if (this.chunksTable) {
671
- try {
672
- lanceChunks = await this.chunksTable.countRows();
673
- } catch {
674
- }
675
- }
676
- const chunks = Math.max(sqliteChunks, lanceChunks);
677
- const oldest = db.prepare("SELECT MIN(created_at) as ts FROM chunks").get()?.ts || null;
678
- const newest = db.prepare("SELECT MAX(created_at) as ts FROM chunks").get()?.ts || null;
679
- const memories = db.prepare("SELECT COUNT(*) as count FROM memories WHERE status = ?").get("active")?.count || 0;
680
- const sources = db.prepare("SELECT COUNT(*) as count FROM sources").get()?.count || 0;
681
- const chunkAgentRows = db.prepare("SELECT DISTINCT agent_id FROM chunks WHERE agent_id IS NOT NULL").all();
682
- const sourceAgentRows = db.prepare("SELECT DISTINCT agent_id FROM sources").all();
683
- const captureAgentRows = db.prepare("SELECT DISTINCT agent_id FROM capture_state").all();
684
- const agents = [.../* @__PURE__ */ new Set([
685
- ...chunkAgentRows.map((r) => r.agent_id),
686
- ...sourceAgentRows.map((r) => r.agent_id),
687
- ...captureAgentRows.map((r) => r.agent_id)
688
- ])];
689
- const captureInfo = db.prepare(
690
- "SELECT COUNT(*) as count, MAX(last_capture_at) as latest FROM capture_state"
691
- ).get();
692
- return {
693
- chunks,
694
- memories,
695
- sources,
696
- agents,
697
- oldestChunk: oldest,
698
- newestChunk: newest,
699
- embeddingProvider: this.config.embeddingProvider,
700
- dataDir: this.config.dataDir,
701
- capturedSessions: captureInfo?.count || 0,
702
- latestCapture: captureInfo?.latest || null
703
- };
704
- }
705
- // ── Capture State (for incremental ingestion) ──
706
- getCaptureState(agentId, sourceId) {
707
- const db = this.sqliteDb;
708
- const row = db.prepare("SELECT last_message_count, capture_count FROM capture_state WHERE agent_id = ? AND source_id = ?").get(agentId, sourceId);
709
- if (!row) return { lastMessageCount: 0, captureCount: 0 };
710
- return {
711
- lastMessageCount: row.last_message_count,
712
- captureCount: row.capture_count
713
- };
714
- }
715
- setCaptureState(agentId, sourceId, messageCount, captureCount) {
716
- const db = this.sqliteDb;
717
- db.prepare(`
718
- INSERT OR REPLACE INTO capture_state (agent_id, source_id, last_message_count, capture_count, last_capture_at)
719
- VALUES (?, ?, ?, ?, ?)
720
- `).run(agentId, sourceId, messageCount, captureCount, (/* @__PURE__ */ new Date()).toISOString());
721
- }
722
- // ── Source File Indexing (optional feature) ──
723
- //
724
- // Add directories as "collections", sync to index/re-index changed files.
725
- // All source chunks get source_type='file' so they're searchable alongside
726
- // conversations and memories. Nothing here is required... you can use MC
727
- // without ever touching sources.
728
- // Default patterns for files worth indexing
729
- static DEFAULT_INCLUDE = [
730
- "**/*.ts",
731
- "**/*.js",
732
- "**/*.tsx",
733
- "**/*.jsx",
734
- "**/*.py",
735
- "**/*.rs",
736
- "**/*.go",
737
- "**/*.java",
738
- "**/*.md",
739
- "**/*.txt",
740
- "**/*.json",
741
- "**/*.yaml",
742
- "**/*.yml",
743
- "**/*.toml",
744
- "**/*.sh",
745
- "**/*.bash",
746
- "**/*.zsh",
747
- "**/*.css",
748
- "**/*.html",
749
- "**/*.svg",
750
- "**/*.sql",
751
- "**/*.graphql",
752
- "**/*.c",
753
- "**/*.cpp",
754
- "**/*.h",
755
- "**/*.hpp",
756
- "**/*.swift",
757
- "**/*.kt",
758
- "**/*.rb",
759
- "**/*.env.example",
760
- "**/*.gitignore",
761
- "**/Makefile",
762
- "**/Dockerfile",
763
- "**/Cargo.toml",
764
- "**/package.json",
765
- "**/tsconfig.json"
766
- ];
767
- static DEFAULT_IGNORE = [
768
- "**/node_modules/**",
769
- "**/.git/**",
770
- "**/dist/**",
771
- "**/build/**",
772
- "**/.next/**",
773
- "**/.cache/**",
774
- "**/coverage/**",
775
- "**/__pycache__/**",
776
- "**/target/**",
777
- "**/vendor/**",
778
- "**/.venv/**",
779
- "**/*.lock",
780
- "**/package-lock.json",
781
- "**/yarn.lock",
782
- "**/bun.lockb",
783
- "**/*.min.js",
784
- "**/*.min.css",
785
- "**/*.map",
786
- "**/*.png",
787
- "**/*.jpg",
788
- "**/*.jpeg",
789
- "**/*.gif",
790
- "**/*.ico",
791
- "**/*.webp",
792
- "**/*.woff",
793
- "**/*.woff2",
794
- "**/*.ttf",
795
- "**/*.eot",
796
- "**/*.mp3",
797
- "**/*.mp4",
798
- "**/*.wav",
799
- "**/*.ogg",
800
- "**/*.webm",
801
- "**/*.zip",
802
- "**/*.tar",
803
- "**/*.gz",
804
- "**/*.br",
805
- "**/*.sqlite",
806
- "**/*.db",
807
- "**/*.lance/**",
808
- "**/*.jsonl",
809
- "**/secrets/**",
810
- "**/.env"
811
- ];
812
- /** Add a directory as a source collection for indexing. */
813
- async sourcesAdd(rootPath, name, options) {
814
- const db = this.sqliteDb;
815
- const now = (/* @__PURE__ */ new Date()).toISOString();
816
- const includePatterns = JSON.stringify(options?.include || _Crystal.DEFAULT_INCLUDE);
817
- const ignorePatterns = JSON.stringify(options?.ignore || _Crystal.DEFAULT_IGNORE);
818
- const existing = db.prepare("SELECT * FROM source_collections WHERE name = ?").get(name);
819
- if (existing) {
820
- throw new Error(`Collection "${name}" already exists. Use sourcesSync() to update it.`);
821
- }
822
- db.prepare(`
823
- INSERT INTO source_collections (name, root_path, glob_patterns, ignore_patterns, created_at)
824
- VALUES (?, ?, ?, ?, ?)
825
- `).run(name, rootPath, includePatterns, ignorePatterns, now);
826
- const row = db.prepare("SELECT * FROM source_collections WHERE name = ?").get(name);
827
- return row;
828
- }
829
- /** Remove a source collection and its file records. Chunks remain in LanceDB. */
830
- sourcesRemove(name) {
831
- const db = this.sqliteDb;
832
- const col = db.prepare("SELECT id FROM source_collections WHERE name = ?").get(name);
833
- if (!col) return false;
834
- db.prepare("DELETE FROM source_files WHERE collection_id = ?").run(col.id);
835
- db.prepare("DELETE FROM source_collections WHERE id = ?").run(col.id);
836
- return true;
837
- }
838
- /** Sync a collection: scan files, detect changes, re-index what changed. */
839
- async sourcesSync(name, options) {
840
- const db = this.sqliteDb;
841
- const startTime = Date.now();
842
- const batchSize = options?.batchSize || 20;
843
- const col = db.prepare("SELECT * FROM source_collections WHERE name = ?").get(name);
844
- if (!col) throw new Error(`Collection "${name}" not found. Add it first with sourcesAdd().`);
845
- const includePatterns = JSON.parse(col.glob_patterns);
846
- const ignorePatterns = JSON.parse(col.ignore_patterns);
847
- const files = this.scanDirectory(col.root_path, includePatterns, ignorePatterns);
848
- const existingFiles = /* @__PURE__ */ new Map();
849
- const rows = db.prepare("SELECT id, file_path, file_hash FROM source_files WHERE collection_id = ?").all(col.id);
850
- for (const row of rows) {
851
- existingFiles.set(row.file_path, { id: row.id, file_hash: row.file_hash });
852
- }
853
- let added = 0;
854
- let updated = 0;
855
- let removed = 0;
856
- let chunksAdded = 0;
857
- const now = (/* @__PURE__ */ new Date()).toISOString();
858
- const toIndex = [];
859
- for (const absPath of files) {
860
- const relPath = relative(col.root_path, absPath);
861
- let content;
862
- try {
863
- content = readFileSync(absPath, "utf-8");
864
- } catch {
865
- continue;
866
- }
867
- const stat = statSync(absPath);
868
- if (stat.size > 500 * 1024) continue;
869
- const hash = createHash("sha256").update(content).digest("hex");
870
- const existing = existingFiles.get(relPath);
871
- if (existing) {
872
- existingFiles.delete(relPath);
873
- if (existing.file_hash === hash) continue;
874
- toIndex.push({ relPath, absPath, hash, size: stat.size, isUpdate: true });
875
- } else {
876
- toIndex.push({ relPath, absPath, hash, size: stat.size, isUpdate: false });
877
- }
878
- }
879
- if (options?.dryRun) {
880
- const newFiles = toIndex.filter((f) => !f.isUpdate).length;
881
- const updatedFiles = toIndex.filter((f) => f.isUpdate).length;
882
- return {
883
- collection: name,
884
- added: newFiles,
885
- updated: updatedFiles,
886
- removed: existingFiles.size,
887
- chunks_added: 0,
888
- duration_ms: Date.now() - startTime
889
- };
890
- }
891
- for (let i = 0; i < toIndex.length; i += batchSize) {
892
- const batch = toIndex.slice(i, i + batchSize);
893
- const allChunks = [];
894
- for (const file of batch) {
895
- const content = readFileSync(file.absPath, "utf-8");
896
- const ext = extname(file.absPath);
897
- const fileName = basename(file.absPath);
898
- const header = `File: ${file.relPath}
899
-
900
- `;
901
- const textChunks = this.chunkText(header + content, 400, 80);
902
- const fileChunks = textChunks.map((text) => ({
903
- text,
904
- role: "system",
905
- source_type: "file",
906
- source_id: `file:${name}:${file.relPath}`,
907
- agent_id: "system",
908
- token_count: Math.ceil(text.length / 4),
909
- created_at: now
910
- }));
911
- allChunks.push(...fileChunks);
912
- if (file.isUpdate) {
913
- db.prepare(`
914
- UPDATE source_files SET file_hash = ?, file_size = ?, chunk_count = ?, last_indexed_at = ?
915
- WHERE collection_id = ? AND file_path = ?
916
- `).run(file.hash, file.size, fileChunks.length, now, col.id, file.relPath);
917
- updated++;
918
- } else {
919
- db.prepare(`
920
- INSERT INTO source_files (collection_id, file_path, file_hash, file_size, chunk_count, last_indexed_at)
921
- VALUES (?, ?, ?, ?, ?, ?)
922
- `).run(col.id, file.relPath, file.hash, file.size, fileChunks.length, now);
923
- added++;
924
- }
925
- }
926
- if (allChunks.length > 0) {
927
- const ingested = await this.ingest(allChunks);
928
- chunksAdded += ingested;
929
- }
930
- }
931
- for (const [relPath, { id }] of existingFiles) {
932
- db.prepare("DELETE FROM source_files WHERE id = ?").run(id);
933
- removed++;
934
- }
935
- const fileCount = db.prepare("SELECT COUNT(*) as count FROM source_files WHERE collection_id = ?").get(col.id).count;
936
- const chunkCount = db.prepare("SELECT SUM(chunk_count) as total FROM source_files WHERE collection_id = ?").get(col.id).total || 0;
937
- db.prepare("UPDATE source_collections SET file_count = ?, chunk_count = ?, last_sync_at = ? WHERE id = ?").run(fileCount, chunkCount, now, col.id);
938
- return {
939
- collection: name,
940
- added,
941
- updated,
942
- removed,
943
- chunks_added: chunksAdded,
944
- duration_ms: Date.now() - startTime
945
- };
946
- }
947
- /** Get status of all source collections. */
948
- sourcesStatus() {
949
- const db = this.sqliteDb;
950
- const collections = db.prepare("SELECT name, root_path, file_count, chunk_count, last_sync_at FROM source_collections").all();
951
- const totalFiles = collections.reduce((sum, c) => sum + c.file_count, 0);
952
- const totalChunks = collections.reduce((sum, c) => sum + c.chunk_count, 0);
953
- return {
954
- collections: collections.map((c) => ({
955
- name: c.name,
956
- root_path: c.root_path,
957
- file_count: c.file_count,
958
- chunk_count: c.chunk_count,
959
- last_sync_at: c.last_sync_at
960
- })),
961
- total_files: totalFiles,
962
- total_chunks: totalChunks
963
- };
964
- }
965
- /** Scan a directory recursively, matching include/ignore patterns. */
966
- scanDirectory(rootPath, includePatterns, ignorePatterns) {
967
- const results = [];
968
- const allowedExtensions = /* @__PURE__ */ new Set();
969
- const allowedExactNames = /* @__PURE__ */ new Set();
970
- for (const pattern of includePatterns) {
971
- const extMatch = pattern.match(/\*\*\/\*(\.\w+)$/);
972
- if (extMatch) {
973
- allowedExtensions.add(extMatch[1]);
974
- }
975
- const nameMatch = pattern.match(/\*\*\/([^*]+)$/);
976
- if (nameMatch && !nameMatch[1].startsWith("*.")) {
977
- allowedExactNames.add(nameMatch[1]);
978
- }
979
- }
980
- const ignoreDirs = /* @__PURE__ */ new Set();
981
- for (const pattern of ignorePatterns) {
982
- const dirMatch = pattern.match(/\*\*\/([^/*]+)\/\*\*$/);
983
- if (dirMatch) {
984
- ignoreDirs.add(dirMatch[1]);
985
- }
986
- }
987
- const ignoreFiles = /* @__PURE__ */ new Set();
988
- for (const pattern of ignorePatterns) {
989
- const fileMatch = pattern.match(/\*\*\/\*(\.\w+)$/);
990
- if (fileMatch) {
991
- ignoreFiles.add(fileMatch[1]);
992
- }
993
- const exactMatch = pattern.match(/\*\*\/([^*]+)$/);
994
- if (exactMatch && !exactMatch[1].includes("/")) {
995
- ignoreFiles.add(exactMatch[1]);
996
- }
997
- }
998
- const walk = (dir) => {
999
- let entries;
1000
- try {
1001
- entries = readdirSync(dir);
1002
- } catch {
1003
- return;
1004
- }
1005
- for (const entry of entries) {
1006
- const fullPath = join(dir, entry);
1007
- let stat;
1008
- try {
1009
- stat = statSync(fullPath);
1010
- } catch {
1011
- continue;
1012
- }
1013
- if (stat.isDirectory()) {
1014
- if (ignoreDirs.has(entry)) continue;
1015
- if (entry.startsWith(".")) continue;
1016
- walk(fullPath);
1017
- } else if (stat.isFile()) {
1018
- const ext = extname(entry);
1019
- if (ignoreFiles.has(ext)) continue;
1020
- if (ignoreFiles.has(entry)) continue;
1021
- if (allowedExtensions.has(ext) || allowedExactNames.has(entry)) {
1022
- results.push(fullPath);
1023
- }
1024
- }
1025
- }
1026
- };
1027
- walk(rootPath);
1028
- return results;
1029
- }
1030
- // ── Cleanup ──
1031
- close() {
1032
- this.sqliteDb?.close();
1033
- }
1034
- };
1035
- function resolveConfig(overrides) {
1036
- const HOME = process.env.HOME || "";
1037
- const ldmMemory = join(HOME, ".ldm", "memory");
1038
- let dataDir = overrides?.dataDir || process.env.CRYSTAL_DATA_DIR;
1039
- if (!dataDir) {
1040
- if (existsSync(join(ldmMemory, "crystal.db"))) {
1041
- dataDir = ldmMemory;
1042
- } else {
1043
- const legacyDir = join(HOME, ".openclaw", "memory-crystal");
1044
- if (existsSync(join(legacyDir, "crystal.db"))) {
1045
- dataDir = legacyDir;
1046
- } else {
1047
- dataDir = ldmMemory;
1048
- }
1049
- }
1050
- }
1051
- loadEnvFile(join(dataDir, ".env"));
1052
- const openaiApiKey = overrides?.openaiApiKey || process.env.OPENAI_API_KEY || opRead("OpenAI API", "api key");
1053
- const googleApiKey = overrides?.googleApiKey || process.env.GOOGLE_API_KEY || opRead("Google AI", "api key");
1054
- const remoteToken = overrides?.remoteToken || process.env.CRYSTAL_REMOTE_TOKEN || opRead("Memory Crystal Remote", "token");
1055
- return {
1056
- dataDir,
1057
- embeddingProvider: overrides?.embeddingProvider || process.env.CRYSTAL_EMBEDDING_PROVIDER || "openai",
1058
- openaiApiKey,
1059
- openaiModel: overrides?.openaiModel || process.env.CRYSTAL_OPENAI_MODEL || "text-embedding-3-small",
1060
- ollamaHost: overrides?.ollamaHost || process.env.CRYSTAL_OLLAMA_HOST || "http://localhost:11434",
1061
- ollamaModel: overrides?.ollamaModel || process.env.CRYSTAL_OLLAMA_MODEL || "nomic-embed-text",
1062
- googleApiKey,
1063
- googleModel: overrides?.googleModel || process.env.CRYSTAL_GOOGLE_MODEL || "text-embedding-004",
1064
- remoteUrl: overrides?.remoteUrl || process.env.CRYSTAL_REMOTE_URL,
1065
- remoteToken
1066
- };
1067
- }
1068
- function loadEnvFile(path) {
1069
- if (!existsSync(path)) return;
1070
- const content = readFileSync(path, "utf8");
1071
- for (const line of content.split("\n")) {
1072
- const trimmed = line.trim();
1073
- if (!trimmed || trimmed.startsWith("#")) continue;
1074
- const eqIdx = trimmed.indexOf("=");
1075
- if (eqIdx === -1) continue;
1076
- const key = trimmed.slice(0, eqIdx).trim();
1077
- let value = trimmed.slice(eqIdx + 1).trim();
1078
- if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
1079
- value = value.slice(1, -1);
1080
- }
1081
- if (key && !process.env[key]) {
1082
- process.env[key] = value;
1083
- }
1084
- }
1085
- }
1086
- function opRead(item, field) {
1087
- try {
1088
- const HOME = process.env.HOME || "";
1089
- let saTokenPath = join(HOME, ".ldm", "secrets", "op-sa-token");
1090
- if (!existsSync(saTokenPath)) {
1091
- saTokenPath = join(HOME, ".openclaw", "secrets", "op-sa-token");
1092
- }
1093
- if (!existsSync(saTokenPath)) return void 0;
1094
- const saToken = readFileSync(saTokenPath, "utf8").trim();
1095
- return execSync(`op read "op://Agent Secrets/${item}/${field}" 2>/dev/null`, {
1096
- encoding: "utf8",
1097
- env: { ...process.env, OP_SERVICE_ACCOUNT_TOKEN: saToken },
1098
- timeout: 1e4
1099
- }).trim() || void 0;
1100
- } catch {
1101
- return void 0;
1102
- }
1103
- }
1104
- var RemoteCrystal = class {
1105
- url;
1106
- token;
1107
- constructor(url, token) {
1108
- this.url = url.replace(/\/$/, "");
1109
- this.token = token;
1110
- }
1111
- async init() {
1112
- const resp = await fetch(`${this.url}/health`);
1113
- if (!resp.ok) {
1114
- throw new Error(`Remote crystal unreachable: ${resp.status}`);
1115
- }
1116
- }
1117
- async request(path, body) {
1118
- const resp = await fetch(`${this.url}${path}`, {
1119
- method: body ? "POST" : "GET",
1120
- headers: {
1121
- "Authorization": `Bearer ${this.token}`,
1122
- "Content-Type": "application/json"
1123
- },
1124
- ...body ? { body: JSON.stringify(body) } : {}
1125
- });
1126
- if (!resp.ok) {
1127
- const err = await resp.text();
1128
- throw new Error(`Remote crystal error ${resp.status}: ${err}`);
1129
- }
1130
- return resp.json();
1131
- }
1132
- async search(query, limit = 5, filter) {
1133
- const data = await this.request("/search", { query, limit, agent_id: filter?.agent_id });
1134
- return data.results || [];
1135
- }
1136
- async ingest(chunks) {
1137
- const data = await this.request("/ingest", { chunks });
1138
- return data.ingested || 0;
1139
- }
1140
- async remember(text, category = "fact") {
1141
- const data = await this.request("/remember", { text, category });
1142
- return data.id;
1143
- }
1144
- forget(memoryId) {
1145
- return this.request("/forget", { id: memoryId }).then((d) => d.ok);
1146
- }
1147
- async status() {
1148
- const data = await this.request("/status");
1149
- return {
1150
- chunks: data.chunks || 0,
1151
- memories: data.memories || 0,
1152
- sources: 0,
1153
- agents: data.agents || [],
1154
- oldestChunk: data.oldestChunk,
1155
- newestChunk: data.newestChunk,
1156
- embeddingProvider: "remote",
1157
- dataDir: this.url,
1158
- capturedSessions: data.capturedSessions || 0,
1159
- latestCapture: data.newestChunk
1160
- };
1161
- }
1162
- // Expose chunkText from a local Crystal instance for cc-hook to use
1163
- chunkText(text) {
1164
- const targetChars = 400 * 4;
1165
- const overlapChars = 80 * 4;
1166
- if (text.length <= targetChars) return [text];
1167
- const chunks = [];
1168
- let start = 0;
1169
- while (start < text.length) {
1170
- let end = start + targetChars;
1171
- if (end >= text.length) {
1172
- chunks.push(text.slice(start));
1173
- break;
1174
- }
1175
- const paraBreak = text.lastIndexOf("\n\n", end);
1176
- if (paraBreak > start + targetChars * 0.5) end = paraBreak;
1177
- else {
1178
- const sentBreak = text.lastIndexOf(". ", end);
1179
- if (sentBreak > start + targetChars * 0.5) end = sentBreak + 1;
1180
- }
1181
- chunks.push(text.slice(start, end));
1182
- start = end - overlapChars;
1183
- }
1184
- return chunks;
1185
- }
1186
- };
1187
- function createCrystal(config) {
1188
- if (config.remoteUrl && config.remoteToken) {
1189
- return new RemoteCrystal(config.remoteUrl, config.remoteToken);
1190
- }
1191
- return new Crystal(config);
1192
- }
1193
-
1194
- export {
1195
- Crystal,
1196
- resolveConfig,
1197
- RemoteCrystal,
1198
- createCrystal
1199
- };