clawvault 2.6.1 → 3.0.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 (232) hide show
  1. package/bin/command-registration.test.js +1 -3
  2. package/bin/register-core-commands.js +10 -23
  3. package/bin/register-maintenance-commands.js +3 -20
  4. package/bin/register-query-commands.js +23 -0
  5. package/bin/register-task-commands.js +1 -18
  6. package/bin/register-task-commands.test.js +0 -16
  7. package/bin/register-vault-operations-commands.js +1 -29
  8. package/dist/{chunk-QVMXF7FY.js → chunk-3D6BCTP6.js} +39 -1
  9. package/dist/{chunk-R2MIW5G7.js → chunk-3DHXQHYG.js} +1 -1
  10. package/dist/{chunk-Q2J5YTUF.js → chunk-3NSBOUT3.js} +73 -36
  11. package/dist/chunk-3RG5ZIWI.js +10 -0
  12. package/dist/{chunk-AZYOKJYC.js → chunk-62YTUT6J.js} +2 -2
  13. package/dist/chunk-6U6MK36V.js +205 -0
  14. package/dist/{chunk-4QYGFWRM.js → chunk-7R7O6STJ.js} +4 -4
  15. package/dist/{chunk-VXEOHTSL.js → chunk-C7OK5WKP.js} +4 -4
  16. package/dist/chunk-CMB7UL7C.js +327 -0
  17. package/dist/chunk-DEFFDRVP.js +938 -0
  18. package/dist/{chunk-HIHOUSXS.js → chunk-E7MFQB6D.js} +59 -18
  19. package/dist/{chunk-ME37YNW3.js → chunk-F2JEUD4J.js} +6 -4
  20. package/dist/chunk-GAJV4IGR.js +82 -0
  21. package/dist/chunk-GQSLDZTS.js +560 -0
  22. package/dist/{chunk-4OXMU5S2.js → chunk-GUKMRGM7.js} +1 -1
  23. package/dist/{chunk-T76H47ZS.js → chunk-H34S76MB.js} +6 -6
  24. package/dist/{chunk-R6SXNSFD.js → chunk-JY6FYXIT.js} +10 -5
  25. package/dist/chunk-K234IDRJ.js +1073 -0
  26. package/dist/{chunk-IEVLHNLU.js → chunk-LNJA2UGL.js} +86 -9
  27. package/dist/{chunk-MFAWT5O5.js → chunk-LYHGEHXG.js} +1 -0
  28. package/dist/chunk-MFM6K7PU.js +374 -0
  29. package/dist/{chunk-QWQ3TIKS.js → chunk-N2AXRYLC.js} +1 -1
  30. package/dist/chunk-PAH27GSN.js +108 -0
  31. package/dist/{chunk-OIWVQYQF.js → chunk-QBLMXKF2.js} +1 -1
  32. package/dist/{chunk-FHFUXL6G.js → chunk-QK3UCXWL.js} +2 -2
  33. package/dist/{chunk-3BTHWPMB.js → chunk-SJSFRIYS.js} +1 -1
  34. package/dist/{chunk-4VRIMU4O.js → chunk-U55BGUAU.js} +2 -2
  35. package/dist/{chunk-PBEE567J.js → chunk-VGLOTGAS.js} +1 -1
  36. package/dist/{chunk-F55HGNU4.js → chunk-WAZ3NLWL.js} +47 -0
  37. package/dist/{chunk-KL4NAOMO.js → chunk-WGRQ6HDV.js} +1 -1
  38. package/dist/{chunk-UEOUADMO.js → chunk-YKTA5JOJ.js} +13 -10
  39. package/dist/{chunk-XAVB4GB4.js → chunk-ZVVFWOLW.js} +4 -4
  40. package/dist/cli/index.cjs +10033 -0
  41. package/dist/cli/index.d.cts +5 -0
  42. package/dist/cli/index.js +20 -18
  43. package/dist/commands/archive.cjs +287 -0
  44. package/dist/commands/archive.d.cts +11 -0
  45. package/dist/commands/archive.js +1 -0
  46. package/dist/commands/backlog.cjs +721 -0
  47. package/dist/commands/backlog.d.cts +53 -0
  48. package/dist/commands/backlog.js +3 -2
  49. package/dist/commands/blocked.cjs +204 -0
  50. package/dist/commands/blocked.d.cts +26 -0
  51. package/dist/commands/blocked.js +3 -2
  52. package/dist/commands/checkpoint.cjs +244 -0
  53. package/dist/commands/checkpoint.d.cts +41 -0
  54. package/dist/commands/checkpoint.js +2 -1
  55. package/dist/commands/compat.cjs +369 -0
  56. package/dist/commands/compat.d.cts +28 -0
  57. package/dist/commands/compat.js +2 -1
  58. package/dist/commands/context.cjs +2989 -0
  59. package/dist/commands/context.d.cts +2 -0
  60. package/dist/commands/context.js +5 -4
  61. package/dist/commands/doctor.cjs +3062 -0
  62. package/dist/commands/doctor.d.cts +21 -0
  63. package/dist/commands/doctor.d.ts +6 -1
  64. package/dist/commands/doctor.js +13 -11
  65. package/dist/commands/embed.cjs +232 -0
  66. package/dist/commands/embed.d.cts +17 -0
  67. package/dist/commands/embed.js +5 -2
  68. package/dist/commands/entities.cjs +141 -0
  69. package/dist/commands/entities.d.cts +7 -0
  70. package/dist/commands/entities.js +1 -0
  71. package/dist/commands/graph.cjs +501 -0
  72. package/dist/commands/graph.d.cts +21 -0
  73. package/dist/commands/graph.js +1 -0
  74. package/dist/commands/inject.cjs +1636 -0
  75. package/dist/commands/inject.d.cts +2 -0
  76. package/dist/commands/inject.d.ts +1 -1
  77. package/dist/commands/inject.js +4 -2
  78. package/dist/commands/kanban.cjs +884 -0
  79. package/dist/commands/kanban.d.cts +63 -0
  80. package/dist/commands/kanban.js +4 -3
  81. package/dist/commands/link.cjs +965 -0
  82. package/dist/commands/link.d.cts +11 -0
  83. package/dist/commands/link.js +1 -0
  84. package/dist/commands/migrate-observations.cjs +362 -0
  85. package/dist/commands/migrate-observations.d.cts +19 -0
  86. package/dist/commands/migrate-observations.js +3 -2
  87. package/dist/commands/observe.cjs +4099 -0
  88. package/dist/commands/observe.d.cts +23 -0
  89. package/dist/commands/observe.d.ts +1 -0
  90. package/dist/commands/observe.js +11 -9
  91. package/dist/commands/project.cjs +1341 -0
  92. package/dist/commands/project.d.cts +85 -0
  93. package/dist/commands/project.js +5 -4
  94. package/dist/commands/rebuild.cjs +3136 -0
  95. package/dist/commands/rebuild.d.cts +11 -0
  96. package/dist/commands/rebuild.js +10 -8
  97. package/dist/commands/recover.cjs +361 -0
  98. package/dist/commands/recover.d.cts +38 -0
  99. package/dist/commands/recover.js +3 -2
  100. package/dist/commands/reflect.cjs +1008 -0
  101. package/dist/commands/reflect.d.cts +11 -0
  102. package/dist/commands/reflect.js +6 -4
  103. package/dist/commands/repair-session.cjs +457 -0
  104. package/dist/commands/repair-session.d.cts +38 -0
  105. package/dist/commands/repair-session.js +1 -0
  106. package/dist/commands/replay.cjs +4103 -0
  107. package/dist/commands/replay.d.cts +16 -0
  108. package/dist/commands/replay.js +12 -10
  109. package/dist/commands/session-recap.cjs +353 -0
  110. package/dist/commands/session-recap.d.cts +27 -0
  111. package/dist/commands/session-recap.js +1 -0
  112. package/dist/commands/setup.cjs +1345 -0
  113. package/dist/commands/setup.d.cts +100 -0
  114. package/dist/commands/setup.d.ts +90 -2
  115. package/dist/commands/setup.js +21 -2
  116. package/dist/commands/shell-init.cjs +75 -0
  117. package/dist/commands/shell-init.d.cts +7 -0
  118. package/dist/commands/shell-init.js +2 -0
  119. package/dist/commands/sleep.cjs +6028 -0
  120. package/dist/commands/sleep.d.cts +36 -0
  121. package/dist/commands/sleep.d.ts +1 -1
  122. package/dist/commands/sleep.js +17 -15
  123. package/dist/commands/status.cjs +2736 -0
  124. package/dist/commands/status.d.cts +52 -0
  125. package/dist/commands/status.js +12 -10
  126. package/dist/commands/tailscale.cjs +1532 -0
  127. package/dist/commands/tailscale.d.cts +52 -0
  128. package/dist/commands/tailscale.js +1 -0
  129. package/dist/commands/task.cjs +1236 -0
  130. package/dist/commands/task.d.cts +97 -0
  131. package/dist/commands/task.js +4 -3
  132. package/dist/commands/template.cjs +457 -0
  133. package/dist/commands/template.d.cts +36 -0
  134. package/dist/commands/template.js +2 -1
  135. package/dist/commands/wake.cjs +2626 -0
  136. package/dist/commands/wake.d.cts +22 -0
  137. package/dist/commands/wake.d.ts +1 -1
  138. package/dist/commands/wake.js +10 -9
  139. package/dist/context-BUGaWpyL.d.cts +46 -0
  140. package/dist/index.cjs +14526 -0
  141. package/dist/index.d.cts +858 -0
  142. package/dist/index.d.ts +192 -7
  143. package/dist/index.js +101 -75
  144. package/dist/{inject-x65KXWPk.d.ts → inject-Bzi5E-By.d.cts} +1 -1
  145. package/dist/inject-Bzi5E-By.d.ts +137 -0
  146. package/dist/lib/auto-linker.cjs +176 -0
  147. package/dist/lib/auto-linker.d.cts +26 -0
  148. package/dist/lib/auto-linker.js +1 -0
  149. package/dist/lib/canvas-layout.cjs +136 -0
  150. package/dist/lib/canvas-layout.d.cts +31 -0
  151. package/dist/lib/canvas-layout.d.ts +16 -100
  152. package/dist/lib/canvas-layout.js +78 -20
  153. package/dist/lib/config.cjs +78 -0
  154. package/dist/lib/config.d.cts +11 -0
  155. package/dist/lib/config.js +1 -0
  156. package/dist/lib/entity-index.cjs +84 -0
  157. package/dist/lib/entity-index.d.cts +26 -0
  158. package/dist/lib/entity-index.js +1 -0
  159. package/dist/lib/project-utils.cjs +864 -0
  160. package/dist/lib/project-utils.d.cts +97 -0
  161. package/dist/lib/project-utils.js +4 -3
  162. package/dist/lib/session-repair.cjs +239 -0
  163. package/dist/lib/session-repair.d.cts +110 -0
  164. package/dist/lib/session-repair.js +1 -0
  165. package/dist/lib/session-utils.cjs +209 -0
  166. package/dist/lib/session-utils.d.cts +63 -0
  167. package/dist/lib/session-utils.js +1 -0
  168. package/dist/lib/tailscale.cjs +1183 -0
  169. package/dist/lib/tailscale.d.cts +225 -0
  170. package/dist/lib/tailscale.js +1 -0
  171. package/dist/lib/task-utils.cjs +1137 -0
  172. package/dist/lib/task-utils.d.cts +208 -0
  173. package/dist/lib/task-utils.js +3 -2
  174. package/dist/lib/template-engine.cjs +47 -0
  175. package/dist/lib/template-engine.d.cts +11 -0
  176. package/dist/lib/template-engine.js +1 -0
  177. package/dist/lib/webdav.cjs +568 -0
  178. package/dist/lib/webdav.d.cts +109 -0
  179. package/dist/lib/webdav.js +1 -0
  180. package/dist/plugin/index.cjs +1907 -0
  181. package/dist/plugin/index.d.cts +36 -0
  182. package/dist/plugin/index.d.ts +36 -0
  183. package/dist/plugin/index.js +572 -0
  184. package/dist/plugin/inject.cjs +356 -0
  185. package/dist/plugin/inject.d.cts +54 -0
  186. package/dist/plugin/inject.d.ts +54 -0
  187. package/dist/plugin/inject.js +17 -0
  188. package/dist/plugin/observe.cjs +631 -0
  189. package/dist/plugin/observe.d.cts +39 -0
  190. package/dist/plugin/observe.d.ts +39 -0
  191. package/dist/plugin/observe.js +18 -0
  192. package/dist/plugin/templates.cjs +593 -0
  193. package/dist/plugin/templates.d.cts +52 -0
  194. package/dist/plugin/templates.d.ts +52 -0
  195. package/dist/plugin/templates.js +25 -0
  196. package/dist/plugin/types.cjs +18 -0
  197. package/dist/plugin/types.d.cts +209 -0
  198. package/dist/plugin/types.d.ts +209 -0
  199. package/dist/plugin/types.js +0 -0
  200. package/dist/plugin/vault.cjs +927 -0
  201. package/dist/plugin/vault.d.cts +68 -0
  202. package/dist/plugin/vault.d.ts +68 -0
  203. package/dist/plugin/vault.js +22 -0
  204. package/dist/{types-C74wgGL1.d.ts → types-Y2_Um2Ls.d.cts} +44 -1
  205. package/dist/types-Y2_Um2Ls.d.ts +205 -0
  206. package/hooks/clawvault/handler.js +70 -7
  207. package/hooks/clawvault/handler.test.js +91 -0
  208. package/openclaw.plugin.json +56 -0
  209. package/package.json +17 -7
  210. package/templates/memory-event.md +67 -0
  211. package/templates/party.md +63 -0
  212. package/templates/primitive-registry.yaml +551 -0
  213. package/templates/run.md +68 -0
  214. package/templates/trigger.md +68 -0
  215. package/templates/workspace.md +50 -0
  216. package/dashboard/lib/graph-diff.js +0 -104
  217. package/dashboard/lib/graph-diff.test.js +0 -75
  218. package/dashboard/lib/vault-parser.js +0 -556
  219. package/dashboard/lib/vault-parser.test.js +0 -254
  220. package/dashboard/public/app.js +0 -796
  221. package/dashboard/public/index.html +0 -52
  222. package/dashboard/public/styles.css +0 -221
  223. package/dashboard/server.js +0 -374
  224. package/dist/chunk-MAKNAHAW.js +0 -375
  225. package/dist/chunk-MDIH26GC.js +0 -183
  226. package/dist/chunk-MGDEINGP.js +0 -99
  227. package/dist/chunk-RVYA52PY.js +0 -363
  228. package/dist/chunk-TLGBDTYT.js +0 -33
  229. package/dist/commands/canvas.d.ts +0 -15
  230. package/dist/commands/canvas.js +0 -199
  231. package/dist/commands/sync-bd.d.ts +0 -10
  232. package/dist/commands/sync-bd.js +0 -9
@@ -0,0 +1,1073 @@
1
+ import {
2
+ DATE_HEADING_RE,
3
+ parseObservationMarkdown
4
+ } from "./chunk-QK3UCXWL.js";
5
+ import {
6
+ listObservationFiles
7
+ } from "./chunk-Z2XBWN7A.js";
8
+
9
+ // src/lib/reweave.ts
10
+ import * as fs from "fs";
11
+ var SUPERSEDED_MARKER_RE = /\[superseded\|by=([^\]|]+)\|detected=([^\]]+)\]/;
12
+ function isSuperseded(line) {
13
+ return SUPERSEDED_MARKER_RE.test(line);
14
+ }
15
+ function getSupersessionInfo(line) {
16
+ const m = line.match(SUPERSEDED_MARKER_RE);
17
+ if (!m) return null;
18
+ return { supersededBy: m[1], detectedAt: m[2] };
19
+ }
20
+ function makeSupersededMarker(supersedingDate, detectedAt) {
21
+ return ` [superseded|by=${supersedingDate}|detected=${detectedAt}]`;
22
+ }
23
+ function extractEntities(content) {
24
+ const normalized = content.toLowerCase().replace(/['']/g, "'");
25
+ const quoted = [];
26
+ for (const m of normalized.matchAll(/[""]([^""]+)[""]/g)) {
27
+ quoted.push(m[1].trim());
28
+ }
29
+ for (const m of normalized.matchAll(/"([^"]+)"/g)) {
30
+ quoted.push(m[1].trim());
31
+ }
32
+ const patterns = [
33
+ /(\w[\w\s]{1,30}?)\s+(?:is|are|was|were|changed to|switched to|moved to|updated to|now uses?|now lives?|now works?)\s+/gi,
34
+ /(?:uses?|prefers?|likes?|lives? (?:in|at)|works? (?:at|for)|drives?|owns?)\s+([\w\s]{2,30})/gi,
35
+ /(\w[\w\s]{1,20}?)'s\s+(\w[\w\s]{1,20})/gi
36
+ ];
37
+ const phrases = [...quoted];
38
+ for (const pat of patterns) {
39
+ for (const m of content.matchAll(pat)) {
40
+ if (m[1]) phrases.push(m[1].trim().toLowerCase());
41
+ if (m[2]) phrases.push(m[2].trim().toLowerCase());
42
+ }
43
+ }
44
+ const stopwords = /* @__PURE__ */ new Set([
45
+ "the",
46
+ "a",
47
+ "an",
48
+ "is",
49
+ "are",
50
+ "was",
51
+ "were",
52
+ "be",
53
+ "been",
54
+ "being",
55
+ "have",
56
+ "has",
57
+ "had",
58
+ "do",
59
+ "does",
60
+ "did",
61
+ "will",
62
+ "would",
63
+ "could",
64
+ "should",
65
+ "may",
66
+ "might",
67
+ "shall",
68
+ "can",
69
+ "need",
70
+ "dare",
71
+ "ought",
72
+ "used",
73
+ "to",
74
+ "of",
75
+ "in",
76
+ "for",
77
+ "on",
78
+ "with",
79
+ "at",
80
+ "by",
81
+ "from",
82
+ "as",
83
+ "into",
84
+ "through",
85
+ "during",
86
+ "before",
87
+ "after",
88
+ "above",
89
+ "below",
90
+ "between",
91
+ "out",
92
+ "off",
93
+ "over",
94
+ "under",
95
+ "again",
96
+ "further",
97
+ "then",
98
+ "once",
99
+ "and",
100
+ "but",
101
+ "or",
102
+ "nor",
103
+ "not",
104
+ "so",
105
+ "yet",
106
+ "both",
107
+ "either",
108
+ "neither",
109
+ "each",
110
+ "every",
111
+ "all",
112
+ "any",
113
+ "few",
114
+ "more",
115
+ "most",
116
+ "other",
117
+ "some",
118
+ "such",
119
+ "no",
120
+ "only",
121
+ "own",
122
+ "same",
123
+ "than",
124
+ "too",
125
+ "very",
126
+ "just",
127
+ "because",
128
+ "that",
129
+ "this",
130
+ "these",
131
+ "those",
132
+ "it",
133
+ "its",
134
+ "he",
135
+ "she",
136
+ "they",
137
+ "we",
138
+ "you",
139
+ "i",
140
+ "me",
141
+ "my",
142
+ "his",
143
+ "her",
144
+ "our",
145
+ "your",
146
+ "their",
147
+ "pedro",
148
+ "clawdious"
149
+ ]);
150
+ const words = normalized.replace(/[^a-z0-9\s'-]/g, " ").split(/\s+/).filter((w) => w.length > 2 && !stopwords.has(w));
151
+ return [.../* @__PURE__ */ new Set([...phrases, ...words])].filter(Boolean);
152
+ }
153
+ function entitySimilarity(a, b) {
154
+ if (a.length === 0 || b.length === 0) return 0;
155
+ const setA = new Set(a);
156
+ const setB = new Set(b);
157
+ let overlap = 0;
158
+ for (const item of setA) {
159
+ if (setB.has(item)) overlap++;
160
+ }
161
+ const union = (/* @__PURE__ */ new Set([...a, ...b])).size;
162
+ return union > 0 ? overlap / union : 0;
163
+ }
164
+ function isKnowledgeUpdate(older, newer, threshold = 0.3) {
165
+ const updateableTypes = /* @__PURE__ */ new Set(["fact", "preference", "decision", "commitment", "project", "relationship"]);
166
+ if (!updateableTypes.has(older.type) && !updateableTypes.has(newer.type)) {
167
+ return { isUpdate: false, reason: "non-updateable types" };
168
+ }
169
+ const olderEntities = extractEntities(older.content);
170
+ const newerEntities = extractEntities(newer.content);
171
+ const similarity = entitySimilarity(olderEntities, newerEntities);
172
+ if (similarity < threshold) {
173
+ return { isUpdate: false, reason: `low entity similarity: ${similarity.toFixed(2)}` };
174
+ }
175
+ const normalizeContent = (s) => s.toLowerCase().replace(/\s+/g, " ").trim();
176
+ if (normalizeContent(older.content) === normalizeContent(newer.content)) {
177
+ return { isUpdate: false, reason: "identical content" };
178
+ }
179
+ return {
180
+ isUpdate: true,
181
+ reason: `entity overlap ${similarity.toFixed(2)}: entities=[${olderEntities.slice(0, 3).join(", ")}]`
182
+ };
183
+ }
184
+ function loadObservations(vaultPath, since) {
185
+ const files = listObservationFiles(vaultPath, {
186
+ fromDate: since
187
+ });
188
+ return files.map((f) => {
189
+ const content = fs.readFileSync(f.path, "utf-8");
190
+ const records = parseObservationMarkdown(content);
191
+ return { file: f, records };
192
+ });
193
+ }
194
+ function reweave(options) {
195
+ const { vaultPath, since, dryRun = false, similarityThreshold = 0.3 } = options;
196
+ const allObsFiles = loadObservations(vaultPath);
197
+ const newObsFiles = since ? allObsFiles.filter((f) => f.file.date >= since) : allObsFiles;
198
+ const allRecordsWithFile = [];
199
+ for (const { file, records } of allObsFiles) {
200
+ for (let i = 0; i < records.length; i++) {
201
+ allRecordsWithFile.push({ record: records[i], file, lineIndex: i });
202
+ }
203
+ }
204
+ allRecordsWithFile.sort((a, b) => a.record.date.localeCompare(b.record.date));
205
+ const supersessions = [];
206
+ const detectedAt = (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
207
+ for (const { file: newFile, records: newRecords } of newObsFiles) {
208
+ for (const newRec of newRecords) {
209
+ if (isSuperseded(newRec.rawLine)) continue;
210
+ for (const { record: oldRec, file: oldFile } of allRecordsWithFile) {
211
+ if (oldRec.date >= newRec.date && oldFile.path === newFile.path) continue;
212
+ if (oldRec.date > newRec.date) continue;
213
+ if (isSuperseded(oldRec.rawLine)) continue;
214
+ const { isUpdate, reason } = isKnowledgeUpdate(oldRec, newRec, similarityThreshold);
215
+ if (isUpdate) {
216
+ supersessions.push({
217
+ oldObservation: oldRec,
218
+ newObservation: newRec,
219
+ oldFile: oldFile.path,
220
+ newFile: newFile.path,
221
+ reason,
222
+ detectedAt
223
+ });
224
+ }
225
+ }
226
+ }
227
+ }
228
+ if (!dryRun && supersessions.length > 0) {
229
+ applySupersessions(supersessions, detectedAt);
230
+ }
231
+ return {
232
+ filesScanned: allObsFiles.length,
233
+ observationsChecked: allRecordsWithFile.length,
234
+ supersessions,
235
+ dryRun
236
+ };
237
+ }
238
+ function applySupersessions(supersessions, detectedAt) {
239
+ const byFile = /* @__PURE__ */ new Map();
240
+ for (const s of supersessions) {
241
+ const existing = byFile.get(s.oldFile) ?? [];
242
+ existing.push(s);
243
+ byFile.set(s.oldFile, existing);
244
+ }
245
+ for (const [filePath, records] of byFile) {
246
+ let content = fs.readFileSync(filePath, "utf-8");
247
+ for (const s of records) {
248
+ const oldLine = s.oldObservation.rawLine;
249
+ if (content.includes(oldLine) && !isSuperseded(oldLine)) {
250
+ const marker = makeSupersededMarker(s.newObservation.date, detectedAt);
251
+ content = content.replace(oldLine, oldLine + marker);
252
+ }
253
+ }
254
+ fs.writeFileSync(filePath, content, "utf-8");
255
+ }
256
+ }
257
+ function filterSuperseded(lines) {
258
+ return lines.filter((line) => !isSuperseded(line));
259
+ }
260
+ function stripSupersededObservations(markdown) {
261
+ const lines = markdown.split("\n");
262
+ const result = [];
263
+ for (const line of lines) {
264
+ if (DATE_HEADING_RE.test(line) || line.trim() === "") {
265
+ result.push(line);
266
+ continue;
267
+ }
268
+ if (isSuperseded(line)) continue;
269
+ result.push(line);
270
+ }
271
+ return result.join("\n").replace(/\n{3,}/g, "\n\n").trim();
272
+ }
273
+
274
+ // src/lib/search.ts
275
+ import { execFileSync, spawnSync } from "child_process";
276
+ import * as fs2 from "fs";
277
+ import * as path from "path";
278
+ var QMD_INSTALL_URL = "https://github.com/tobi/qmd";
279
+ var QMD_INSTALL_COMMAND = "bun install -g github:tobi/qmd";
280
+ var QMD_NOT_INSTALLED_MESSAGE = `ClawVault requires qmd. Install: ${QMD_INSTALL_COMMAND}`;
281
+ var QMD_INDEX_ENV_VAR = "CLAWVAULT_QMD_INDEX";
282
+ var QmdUnavailableError = class extends Error {
283
+ constructor(message = QMD_NOT_INSTALLED_MESSAGE) {
284
+ super(message);
285
+ this.name = "QmdUnavailableError";
286
+ }
287
+ };
288
+ function ensureJsonArgs(args) {
289
+ return args.includes("--json") ? args : [...args, "--json"];
290
+ }
291
+ function resolveQmdIndexName(indexName) {
292
+ const explicit = indexName?.trim();
293
+ if (explicit) {
294
+ return explicit;
295
+ }
296
+ const fromEnv = process.env[QMD_INDEX_ENV_VAR]?.trim();
297
+ return fromEnv || void 0;
298
+ }
299
+ function withQmdIndexArgs(args, indexName) {
300
+ if (args.includes("--index")) {
301
+ return [...args];
302
+ }
303
+ const resolvedIndexName = resolveQmdIndexName(indexName);
304
+ if (!resolvedIndexName) {
305
+ return [...args];
306
+ }
307
+ return ["--index", resolvedIndexName, ...args];
308
+ }
309
+ function tryParseJson(raw) {
310
+ try {
311
+ return JSON.parse(raw);
312
+ } catch {
313
+ return null;
314
+ }
315
+ }
316
+ function extractJsonPayload(raw) {
317
+ const start = raw.search(/[\[{]/);
318
+ if (start === -1) return null;
319
+ const end = Math.max(raw.lastIndexOf("]"), raw.lastIndexOf("}"));
320
+ if (end <= start) return null;
321
+ return raw.slice(start, end + 1);
322
+ }
323
+ function stripQmdNoise(raw) {
324
+ return raw.split("\n").filter((line) => {
325
+ const t = line.trim();
326
+ if (!t) return true;
327
+ if (t.startsWith("[node-llama-cpp]")) return false;
328
+ if (t.startsWith("Expanding query")) return false;
329
+ if (t.startsWith("Searching ") && t.endsWith("queries...")) return false;
330
+ if (/^[├└─│]/.test(t)) return false;
331
+ return true;
332
+ }).join("\n");
333
+ }
334
+ function parseQmdOutput(raw) {
335
+ const trimmed = stripQmdNoise(raw).trim();
336
+ if (!trimmed) return [];
337
+ const direct = tryParseJson(trimmed);
338
+ const extracted = direct ? null : extractJsonPayload(trimmed);
339
+ const parsed = direct ?? (extracted ? tryParseJson(extracted) : null);
340
+ if (!parsed) {
341
+ throw new Error("qmd returned non-JSON output. Ensure qmd supports --json.");
342
+ }
343
+ if (Array.isArray(parsed)) {
344
+ return parsed;
345
+ }
346
+ if (parsed && typeof parsed === "object") {
347
+ const candidate = parsed.results ?? parsed.items ?? parsed.data;
348
+ if (Array.isArray(candidate)) {
349
+ return candidate;
350
+ }
351
+ }
352
+ throw new Error("qmd returned an unexpected JSON shape.");
353
+ }
354
+ function ensureQmdAvailable() {
355
+ if (!hasQmd()) {
356
+ throw new QmdUnavailableError();
357
+ }
358
+ }
359
+ function execQmd(args, indexName) {
360
+ ensureQmdAvailable();
361
+ const finalArgs = withQmdIndexArgs(ensureJsonArgs(args), indexName);
362
+ try {
363
+ const result = execFileSync("qmd", finalArgs, {
364
+ encoding: "utf-8",
365
+ stdio: ["ignore", "pipe", "pipe"],
366
+ maxBuffer: 10 * 1024 * 1024
367
+ // 10MB
368
+ });
369
+ return parseQmdOutput(result);
370
+ } catch (err) {
371
+ if (err?.code === "ENOENT") {
372
+ throw new QmdUnavailableError();
373
+ }
374
+ const output = [err?.stdout, err?.stderr].filter(Boolean).join("\n");
375
+ if (output) {
376
+ try {
377
+ return parseQmdOutput(output);
378
+ } catch {
379
+ }
380
+ }
381
+ const message = err?.message ? `qmd failed: ${err.message}` : "qmd failed";
382
+ throw new Error(message);
383
+ }
384
+ }
385
+ function hasQmd() {
386
+ const result = spawnSync("qmd", ["--version"], { stdio: "ignore" });
387
+ return !result.error;
388
+ }
389
+ function qmdUpdate(collection, indexName) {
390
+ ensureQmdAvailable();
391
+ const args = ["update"];
392
+ if (collection) {
393
+ args.push("-c", collection);
394
+ }
395
+ execFileSync("qmd", withQmdIndexArgs(args, indexName), { stdio: "inherit" });
396
+ }
397
+ function qmdEmbed(collection, indexName) {
398
+ ensureQmdAvailable();
399
+ const args = ["embed"];
400
+ if (collection) {
401
+ args.push("-c", collection);
402
+ }
403
+ execFileSync("qmd", withQmdIndexArgs(args, indexName), { stdio: "inherit" });
404
+ }
405
+ function sentenceChunk(text, maxChars = 600, overlapSentences = 1) {
406
+ const sentences = text.split(/(?<=[.!?])\s+|\n{2,}/).map((s) => s.trim()).filter(Boolean);
407
+ if (sentences.length === 0) return text.trim() ? [text] : [];
408
+ const chunks = [];
409
+ let i = 0;
410
+ while (i < sentences.length) {
411
+ const chunkSents = [];
412
+ let chunkLen = 0;
413
+ let j = i;
414
+ while (j < sentences.length && chunkLen + sentences[j].length < maxChars) {
415
+ chunkSents.push(sentences[j]);
416
+ chunkLen += sentences[j].length + 1;
417
+ j++;
418
+ }
419
+ if (chunkSents.length === 0) {
420
+ chunkSents.push(sentences[j].slice(0, maxChars));
421
+ j++;
422
+ }
423
+ chunks.push(chunkSents.join(" "));
424
+ i = Math.max(j - overlapSentences, i + 1);
425
+ }
426
+ return chunks;
427
+ }
428
+ var STOPWORDS = /* @__PURE__ */ new Set([
429
+ "what",
430
+ "when",
431
+ "where",
432
+ "which",
433
+ "that",
434
+ "this",
435
+ "have",
436
+ "from",
437
+ "with",
438
+ "they",
439
+ "been",
440
+ "were",
441
+ "will",
442
+ "about",
443
+ "would",
444
+ "could",
445
+ "should",
446
+ "their",
447
+ "there",
448
+ "does",
449
+ "your",
450
+ "more",
451
+ "some",
452
+ "than",
453
+ "into",
454
+ "also",
455
+ "just",
456
+ "very",
457
+ "much",
458
+ "most",
459
+ "many",
460
+ "only",
461
+ "other",
462
+ "each",
463
+ "every",
464
+ "after",
465
+ "before",
466
+ "did",
467
+ "the",
468
+ "and",
469
+ "for",
470
+ "are",
471
+ "was",
472
+ "not",
473
+ "but",
474
+ "can",
475
+ "had",
476
+ "has",
477
+ "how",
478
+ "who",
479
+ "why",
480
+ "its",
481
+ "you",
482
+ "my",
483
+ "me",
484
+ "is",
485
+ "it",
486
+ "do",
487
+ "so",
488
+ "if",
489
+ "or",
490
+ "an",
491
+ "on",
492
+ "at",
493
+ "by",
494
+ "no",
495
+ "up",
496
+ "to",
497
+ "in",
498
+ "of",
499
+ "am",
500
+ "be"
501
+ ]);
502
+ function tokenize(text) {
503
+ return text.toLowerCase().split(/\s+/).map((w) => w.replace(/^[?.,!"'\-():;[\]{}*]+|[?.,!"'\-():;[\]{}*]+$/g, "")).filter((w) => w.length > 1);
504
+ }
505
+ function queryTerms(query) {
506
+ return tokenize(query).filter((w) => !STOPWORDS.has(w));
507
+ }
508
+ function bm25RankChunks(chunks, terms, max = 5) {
509
+ if (chunks.length === 0) return [];
510
+ const termSet = new Set(terms);
511
+ const scored = chunks.map((text, idx) => {
512
+ const words = new Set(tokenize(text));
513
+ let overlap = 0;
514
+ for (const t of termSet) if (words.has(t)) overlap++;
515
+ return { text, score: overlap, idx };
516
+ });
517
+ scored.sort((a, b) => b.score - a.score);
518
+ const seen = /* @__PURE__ */ new Set();
519
+ const result = [];
520
+ seen.add(0);
521
+ result.push({ text: chunks[0], score: scored.find((s) => s.idx === 0)?.score ?? 0 });
522
+ for (const s of scored) {
523
+ if (result.length >= max) break;
524
+ if (!seen.has(s.idx) && s.score > 0) {
525
+ seen.add(s.idx);
526
+ result.push({ text: s.text, score: s.score });
527
+ }
528
+ }
529
+ return result;
530
+ }
531
+ var MONTH_NAMES = {
532
+ january: 1,
533
+ february: 2,
534
+ march: 3,
535
+ april: 4,
536
+ may: 5,
537
+ june: 6,
538
+ july: 7,
539
+ august: 8,
540
+ september: 9,
541
+ october: 10,
542
+ november: 11,
543
+ december: 12,
544
+ jan: 1,
545
+ feb: 2,
546
+ mar: 3,
547
+ apr: 4,
548
+ jun: 6,
549
+ jul: 7,
550
+ aug: 8,
551
+ sep: 9,
552
+ sept: 9,
553
+ oct: 10,
554
+ nov: 11,
555
+ dec: 12
556
+ };
557
+ var MONTH_RE_PART = Object.keys(MONTH_NAMES).join("|");
558
+ var DATE_ISO_RE = /\b(\d{4})[/-](\d{1,2})[/-](\d{1,2})\b/g;
559
+ var DATE_US_RE = /\b(\d{1,2})\/(\d{1,2})\/(\d{4})\b/g;
560
+ var DATE_MONTH_DAY_YEAR_RE = new RegExp(
561
+ `\\b(${MONTH_RE_PART})\\s+(\\d{1,2})(?:st|nd|rd|th)?,?\\s*(\\d{4})\\b`,
562
+ "gi"
563
+ );
564
+ var DATE_DAY_MONTH_YEAR_RE = new RegExp(
565
+ `\\b(\\d{1,2})(?:st|nd|rd|th)?\\s+(${MONTH_RE_PART}),?\\s*(\\d{4})\\b`,
566
+ "gi"
567
+ );
568
+ var DATE_MONTH_DAY_RE = new RegExp(
569
+ `\\b(${MONTH_RE_PART})\\s+(\\d{1,2})(?:st|nd|rd|th)?\\b`,
570
+ "gi"
571
+ );
572
+ var RELATIVE_AGO_RE = /\b(\d+)\s+(days?|weeks?|months?|years?)\s+ago\b/gi;
573
+ var RELATIVE_IN_RE = /\bin\s+(\d+)\s+(days?|weeks?|months?|years?)\b/gi;
574
+ var DURATION_RE = /(?:for|took|spent|lasted|about|approximately|around)\s+(\d+)\s+(days?|weeks?|months?|years?|hours?|minutes?)/gi;
575
+ function tryParseISODate(y, m, d) {
576
+ const dt = new Date(Date.UTC(y, m - 1, d));
577
+ if (dt.getUTCFullYear() === y && dt.getUTCMonth() === m - 1 && dt.getUTCDate() === d) return dt;
578
+ return null;
579
+ }
580
+ function unitToDays(n, unit) {
581
+ const u = unit.toLowerCase().replace(/s$/, "");
582
+ switch (u) {
583
+ case "day":
584
+ return n;
585
+ case "week":
586
+ return n * 7;
587
+ case "month":
588
+ return n * 30;
589
+ case "year":
590
+ return n * 365;
591
+ default:
592
+ return null;
593
+ }
594
+ }
595
+ function contextSnippet(text, start, end, maxLen = 150) {
596
+ const s = Math.max(0, start - Math.floor(maxLen / 2));
597
+ const e = Math.min(text.length, end + Math.floor(maxLen / 2));
598
+ return text.slice(s, e).replace(/\n/g, " ").trim();
599
+ }
600
+ function isoStr(d) {
601
+ const yy = d.getUTCFullYear();
602
+ const mm = String(d.getUTCMonth() + 1).padStart(2, "0");
603
+ const dd = String(d.getUTCDate()).padStart(2, "0");
604
+ return `${yy}-${mm}-${dd}`;
605
+ }
606
+ function extractDates(text, sessionDateStr) {
607
+ const results = [];
608
+ const sessionDate = sessionDateStr ? new Date(sessionDateStr) : null;
609
+ const seen = /* @__PURE__ */ new Set();
610
+ function push(date, ctx, docId = "") {
611
+ const key = `${date}|${ctx.slice(0, 60)}`;
612
+ if (seen.has(key)) return;
613
+ seen.add(key);
614
+ results.push({ date, context: ctx, documentId: docId });
615
+ }
616
+ for (const m of text.matchAll(DATE_ISO_RE)) {
617
+ const dt = tryParseISODate(+m[1], +m[2], +m[3]);
618
+ if (dt) push(isoStr(dt), contextSnippet(text, m.index, m.index + m[0].length));
619
+ }
620
+ for (const m of text.matchAll(DATE_US_RE)) {
621
+ const dt = tryParseISODate(+m[3], +m[1], +m[2]);
622
+ if (dt) push(isoStr(dt), contextSnippet(text, m.index, m.index + m[0].length));
623
+ }
624
+ for (const m of text.matchAll(DATE_MONTH_DAY_YEAR_RE)) {
625
+ const mon = MONTH_NAMES[m[1].toLowerCase()];
626
+ if (mon) {
627
+ const dt = tryParseISODate(+m[3], mon, +m[2]);
628
+ if (dt) push(isoStr(dt), contextSnippet(text, m.index, m.index + m[0].length));
629
+ }
630
+ }
631
+ for (const m of text.matchAll(DATE_DAY_MONTH_YEAR_RE)) {
632
+ const mon = MONTH_NAMES[m[2].toLowerCase()];
633
+ if (mon) {
634
+ const dt = tryParseISODate(+m[3], mon, +m[1]);
635
+ if (dt) push(isoStr(dt), contextSnippet(text, m.index, m.index + m[0].length));
636
+ }
637
+ }
638
+ if (sessionDate) {
639
+ for (const m of text.matchAll(DATE_MONTH_DAY_RE)) {
640
+ const mon = MONTH_NAMES[m[1].toLowerCase()];
641
+ if (mon) {
642
+ const dt = tryParseISODate(sessionDate.getFullYear(), mon, +m[2]);
643
+ if (dt) push(isoStr(dt), contextSnippet(text, m.index, m.index + m[0].length));
644
+ }
645
+ }
646
+ }
647
+ if (sessionDate) {
648
+ for (const m of text.matchAll(RELATIVE_AGO_RE)) {
649
+ const days = unitToDays(+m[1], m[2]);
650
+ if (days !== null) {
651
+ const dt = new Date(sessionDate.getTime() - days * 864e5);
652
+ push(isoStr(dt), contextSnippet(text, m.index, m.index + m[0].length));
653
+ }
654
+ }
655
+ for (const m of text.matchAll(RELATIVE_IN_RE)) {
656
+ const days = unitToDays(+m[1], m[2]);
657
+ if (days !== null) {
658
+ const dt = new Date(sessionDate.getTime() + days * 864e5);
659
+ push(isoStr(dt), contextSnippet(text, m.index, m.index + m[0].length));
660
+ }
661
+ }
662
+ }
663
+ for (const m of text.matchAll(DURATION_RE)) {
664
+ push(`duration:${m[1]} ${m[2]}`, contextSnippet(text, m.index, m.index + m[0].length));
665
+ }
666
+ return results;
667
+ }
668
+ var PREF_PATTERNS = [
669
+ // "I use/prefer/like/love/enjoy X"
670
+ /\bi\s+(?:use|prefer|like|love|enjoy|favor|chose|switched to|started using|always use|usually use)\s+(.{3,60}?)(?:[.,;!?\n]|$)/gi,
671
+ // "my favorite X is Y"
672
+ /\bmy\s+(?:favorite|preferred|go-to|usual)\s+\w+\s+(?:is|are|was)\s+(.{3,60}?)(?:[.,;!?\n]|$)/gi,
673
+ // "I'm a big fan of X"
674
+ /\bi(?:'m| am)\s+(?:a )?(?:big |huge )?fan of\s+(.{3,60}?)(?:[.,;!?\n]|$)/gi,
675
+ // "I switched from X to Y"
676
+ /\bi\s+switched\s+from\s+(.{3,40}?)\s+to\s+(.{3,40}?)(?:[.,;!?\n]|$)/gi
677
+ ];
678
+ function extractPreferences(text, documentId = "") {
679
+ const results = [];
680
+ const seen = /* @__PURE__ */ new Set();
681
+ for (const pattern of PREF_PATTERNS) {
682
+ for (const m of text.matchAll(pattern)) {
683
+ const value = (m[1] || "").trim();
684
+ if (!value || value.length < 3) continue;
685
+ const key = value.toLowerCase();
686
+ if (seen.has(key)) continue;
687
+ seen.add(key);
688
+ const ctx = contextSnippet(text, m.index, m.index + m[0].length, 200);
689
+ let category = "general";
690
+ if (/tool|software|app|editor|ide|framework|library|language/i.test(ctx)) category = "tool";
691
+ else if (/hobby|sport|exercise|game|play/i.test(ctx)) category = "hobby";
692
+ else if (/brand|product|model|device|hardware/i.test(ctx)) category = "brand";
693
+ else if (/food|drink|restaurant|cuisine|recipe/i.test(ctx)) category = "food";
694
+ else if (/music|movie|show|book|podcast|artist|band/i.test(ctx)) category = "entertainment";
695
+ results.push({ category, value, documentId, context: ctx });
696
+ }
697
+ }
698
+ return results;
699
+ }
700
+ var PREFERENCE_Q_RE = /(?:can you (?:recommend|suggest)|any (?:tips|advice|suggestions|recommendations)|what .*(?:recommend|suggest)|what should i|where should i|which .* should i|please (?:recommend|suggest)|based on .* (?:interest|preference|taste)|personalized|tailored to (?:my|me))/i;
701
+ var TEMPORAL_Q_RE = /(?:how many (?:days|weeks|months|years|hours|minutes) (?:passed|did|have|ago|between|since|in total|took)|how long (?:did|was|were|have|has|does)|how long ago|what (?:is the )?order|in order|which .* (?:first|last|earlier|later|before|after|most recent|oldest|newest)|chronological|(?:earlier|later|sooner|newer|older) than)/i;
702
+ var AGGREGATION_Q_RE = /(?:how many|how much|total|all the|count|list all|every|what are all|name all)/i;
703
+ function classifyQuestion(q) {
704
+ if (PREFERENCE_Q_RE.test(q)) return "preference";
705
+ if (TEMPORAL_Q_RE.test(q)) return "temporal";
706
+ if (!TEMPORAL_Q_RE.test(q) && AGGREGATION_Q_RE.test(q)) return "aggregation";
707
+ return "default";
708
+ }
709
+ var SearchEngine = class {
710
+ documents = /* @__PURE__ */ new Map();
711
+ collection = "clawvault";
712
+ vaultPath = "";
713
+ collectionRoot = "";
714
+ qmdIndexName;
715
+ /** v2.7 — Per-document date index built at ingest time */
716
+ dateIndex = /* @__PURE__ */ new Map();
717
+ /** v2.7 — Per-document preference index built at ingest time */
718
+ preferenceIndex = /* @__PURE__ */ new Map();
719
+ /** v2.7 — Per-document chunk cache for BM25 pre-filtering */
720
+ chunkCache = /* @__PURE__ */ new Map();
721
+ /**
722
+ * Set the collection name (usually vault name)
723
+ */
724
+ setCollection(name) {
725
+ this.collection = name;
726
+ }
727
+ /**
728
+ * Set the vault path for file resolution
729
+ */
730
+ setVaultPath(vaultPath) {
731
+ this.vaultPath = vaultPath;
732
+ }
733
+ /**
734
+ * Set the collection root for qmd:// URI resolution
735
+ */
736
+ setCollectionRoot(root) {
737
+ this.collectionRoot = path.resolve(root);
738
+ }
739
+ /**
740
+ * Set qmd index name (defaults to qmd global default when omitted)
741
+ */
742
+ setIndexName(indexName) {
743
+ this.qmdIndexName = indexName;
744
+ }
745
+ /**
746
+ * Add or update a document in the local cache.
747
+ * v2.7: also extracts dates, preferences, and chunks at ingest time.
748
+ * Note: qmd indexing happens via qmd update command
749
+ */
750
+ addDocument(doc) {
751
+ this.documents.set(doc.id, doc);
752
+ if (doc.content) {
753
+ const sessionDate = doc.modified ? isoStr(doc.modified) : void 0;
754
+ const dates = extractDates(doc.content, sessionDate);
755
+ for (const d of dates) d.documentId = doc.id;
756
+ if (dates.length > 0) this.dateIndex.set(doc.id, dates);
757
+ const prefs = extractPreferences(doc.content, doc.id);
758
+ if (prefs.length > 0) this.preferenceIndex.set(doc.id, prefs);
759
+ const chunks = sentenceChunk(doc.content, 600, 1);
760
+ if (chunks.length > 0) this.chunkCache.set(doc.id, chunks);
761
+ }
762
+ }
763
+ /**
764
+ * Remove a document from the local cache
765
+ */
766
+ removeDocument(id) {
767
+ this.documents.delete(id);
768
+ this.dateIndex.delete(id);
769
+ this.preferenceIndex.delete(id);
770
+ this.chunkCache.delete(id);
771
+ }
772
+ /**
773
+ * No-op for qmd - indexing is managed externally
774
+ */
775
+ rebuildIDF() {
776
+ }
777
+ /**
778
+ * BM25 search via qmd
779
+ */
780
+ search(query, options = {}) {
781
+ return this.runQmdQuery("search", query, options);
782
+ }
783
+ /**
784
+ * Vector/semantic search via qmd vsearch
785
+ */
786
+ vsearch(query, options = {}) {
787
+ return this.runQmdQuery("vsearch", query, options);
788
+ }
789
+ /**
790
+ * Combined search with query expansion (qmd query command)
791
+ */
792
+ query(query, options = {}) {
793
+ return this.runQmdQuery("query", query, options);
794
+ }
795
+ runQmdQuery(command, query, options) {
796
+ const {
797
+ limit = 10,
798
+ minScore = 0,
799
+ category,
800
+ tags,
801
+ fullContent = false,
802
+ temporalBoost = false,
803
+ relevanceThreshold,
804
+ thresholdMaxResults = 40
805
+ } = options;
806
+ if (!query.trim()) return [];
807
+ const fetchLimit = relevanceThreshold !== void 0 ? thresholdMaxResults * 2 : limit * 2;
808
+ const args = [
809
+ command,
810
+ query,
811
+ "-n",
812
+ String(fetchLimit),
813
+ "--json"
814
+ ];
815
+ if (this.collection) {
816
+ args.push("-c", this.collection);
817
+ }
818
+ const qmdResults = execQmd(args, this.qmdIndexName);
819
+ const effectiveLimit = relevanceThreshold !== void 0 ? thresholdMaxResults : limit;
820
+ const results = this.convertResults(qmdResults, {
821
+ limit: effectiveLimit,
822
+ minScore: relevanceThreshold !== void 0 ? relevanceThreshold : minScore,
823
+ category,
824
+ tags,
825
+ fullContent,
826
+ temporalBoost
827
+ });
828
+ return results;
829
+ }
830
+ // -------------------------------------------------------------------------
831
+ // v2.7 — New public APIs
832
+ // -------------------------------------------------------------------------
833
+ /**
834
+ * v2.7 — Chunk-level BM25 pre-filtered search. Ranks chunks within each
835
+ * document by keyword relevance before semantic ranking, so relevant
836
+ * content deep in long documents isn't missed.
837
+ *
838
+ * Returns results with snippets from the best-matching chunks.
839
+ */
840
+ chunkPrefilterSearch(query, options = {}) {
841
+ const terms = queryTerms(query);
842
+ const results = this.runQmdQuery("query", query, options);
843
+ for (const r of results) {
844
+ const chunks = this.chunkCache.get(r.document.id);
845
+ if (chunks && chunks.length > 0 && terms.length > 0) {
846
+ const ranked = bm25RankChunks(chunks, terms, 3);
847
+ if (ranked.length > 0 && ranked[0].score > 0) {
848
+ r.snippet = ranked.map((c) => c.text).join("\n...\n").slice(0, 600);
849
+ }
850
+ }
851
+ }
852
+ return results;
853
+ }
854
+ /**
855
+ * v2.7 — Exhaustive threshold-based search for aggregation queries.
856
+ * Keeps pulling results until relevance drops below threshold.
857
+ */
858
+ exhaustiveSearch(query, threshold = 0.01, maxResults = 40) {
859
+ return this.runQmdQuery("query", query, {
860
+ relevanceThreshold: threshold,
861
+ thresholdMaxResults: maxResults,
862
+ fullContent: false
863
+ });
864
+ }
865
+ /**
866
+ * v2.7 — Get all extracted dates, optionally filtered by document ids.
867
+ */
868
+ getDates(documentIds) {
869
+ const all = [];
870
+ const iter = documentIds ? documentIds.map((id) => [id, this.dateIndex.get(id)]).filter(([, v]) => v) : this.dateIndex.entries();
871
+ for (const [, dates] of iter) {
872
+ if (dates) all.push(...dates);
873
+ }
874
+ return all;
875
+ }
876
+ /**
877
+ * v2.7 — Get all extracted preferences, optionally filtered by document ids.
878
+ */
879
+ getPreferences(documentIds) {
880
+ const all = [];
881
+ const iter = documentIds ? documentIds.map((id) => [id, this.preferenceIndex.get(id)]).filter(([, v]) => v) : this.preferenceIndex.entries();
882
+ for (const [, prefs] of iter) {
883
+ if (prefs) all.push(...prefs);
884
+ }
885
+ return all;
886
+ }
887
+ /**
888
+ * v2.7 — Search with automatic strategy selection based on question type.
889
+ * Classifies the query and routes to the appropriate pipeline.
890
+ */
891
+ smartQuery(query, options = {}) {
892
+ const qtype = classifyQuestion(query);
893
+ switch (qtype) {
894
+ case "aggregation":
895
+ return this.exhaustiveSearch(query, 0.01, options.thresholdMaxResults ?? 40);
896
+ case "preference":
897
+ case "temporal":
898
+ default:
899
+ return this.chunkPrefilterSearch(query, { ...options, limit: options.limit ?? 10 });
900
+ }
901
+ }
902
+ /**
903
+ * Convert qmd results to ClawVault SearchResult format
904
+ */
905
+ convertResults(qmdResults, options) {
906
+ const { limit = 10, minScore = 0, category, tags, fullContent = false, temporalBoost = false } = options;
907
+ const results = [];
908
+ const maxScore = qmdResults[0]?.score || 1;
909
+ for (const qr of qmdResults) {
910
+ const filePath = this.qmdUriToPath(qr.file);
911
+ const relativePath = this.vaultPath ? path.relative(this.vaultPath, filePath) : filePath;
912
+ const normalizedRelativePath = relativePath.replace(/\\/g, "/");
913
+ if (normalizedRelativePath.startsWith("ledger/archive/") || normalizedRelativePath.includes("/ledger/archive/")) {
914
+ continue;
915
+ }
916
+ const docId = normalizedRelativePath.replace(/\.md$/, "");
917
+ let doc = this.documents.get(docId) ?? this.documents.get(docId.split("/").join(path.sep));
918
+ const modifiedAt = this.resolveModifiedAt(doc, filePath);
919
+ const parts = normalizedRelativePath.split("/");
920
+ const docCategory = parts.length > 1 ? parts[0] : "root";
921
+ if (category && docCategory !== category) continue;
922
+ if (tags && tags.length > 0 && doc) {
923
+ const docTags = new Set(doc.tags);
924
+ if (!tags.some((t) => docTags.has(t))) continue;
925
+ }
926
+ const normalizedScore = maxScore > 0 ? qr.score / maxScore : 0;
927
+ const finalScore = temporalBoost ? normalizedScore * this.getRecencyFactor(modifiedAt) : normalizedScore;
928
+ if (finalScore < minScore) continue;
929
+ if (!doc) {
930
+ doc = {
931
+ id: docId,
932
+ path: filePath,
933
+ category: docCategory,
934
+ title: qr.title || path.basename(relativePath, ".md"),
935
+ content: "",
936
+ // Content loaded separately if needed
937
+ frontmatter: {},
938
+ links: [],
939
+ tags: [],
940
+ modified: modifiedAt
941
+ };
942
+ }
943
+ results.push({
944
+ document: fullContent ? doc : { ...doc, content: "" },
945
+ score: finalScore,
946
+ snippet: this.stripSupersededFromSnippet(this.cleanSnippet(qr.snippet)),
947
+ matchedTerms: []
948
+ // qmd doesn't provide this
949
+ });
950
+ }
951
+ return results.sort((a, b) => b.score - a.score).slice(0, limit);
952
+ }
953
+ resolveModifiedAt(doc, filePath) {
954
+ if (doc) return doc.modified;
955
+ try {
956
+ return fs2.statSync(filePath).mtime;
957
+ } catch {
958
+ return /* @__PURE__ */ new Date(0);
959
+ }
960
+ }
961
+ getRecencyFactor(modifiedAt) {
962
+ const ageMs = Math.max(0, Date.now() - modifiedAt.getTime());
963
+ const ageDays = ageMs / (24 * 60 * 60 * 1e3);
964
+ if (ageDays < 1) return 1;
965
+ if (ageDays <= 7) return 0.9;
966
+ return 0.7;
967
+ }
968
+ /**
969
+ * Convert qmd:// URI to file path
970
+ */
971
+ qmdUriToPath(uri) {
972
+ if (uri.startsWith("qmd://")) {
973
+ const withoutScheme = uri.slice(6);
974
+ const slashIndex = withoutScheme.indexOf("/");
975
+ if (slashIndex > -1) {
976
+ const relativePath = withoutScheme.slice(slashIndex + 1);
977
+ const root = this.collectionRoot || this.vaultPath;
978
+ if (root) {
979
+ return path.join(root, relativePath);
980
+ }
981
+ return relativePath;
982
+ }
983
+ }
984
+ return uri;
985
+ }
986
+ /**
987
+ * v2.8 — Filter superseded observation lines from snippet text.
988
+ * Ensures search results prefer the latest version of knowledge.
989
+ */
990
+ stripSupersededFromSnippet(snippet) {
991
+ if (!snippet) return snippet;
992
+ return snippet.split("\n").filter((line) => !isSuperseded(line)).join("\n");
993
+ }
994
+ /**
995
+ * Clean up qmd snippet format
996
+ */
997
+ cleanSnippet(snippet) {
998
+ if (!snippet) return "";
999
+ return snippet.replace(/@@ [-+]?\d+,?\d* @@ \([^)]+\)/g, "").trim().split("\n").slice(0, 3).join("\n").slice(0, 300);
1000
+ }
1001
+ /**
1002
+ * Get all cached documents
1003
+ */
1004
+ getAllDocuments() {
1005
+ return [...this.documents.values()];
1006
+ }
1007
+ /**
1008
+ * Get document count
1009
+ */
1010
+ get size() {
1011
+ return this.documents.size;
1012
+ }
1013
+ /**
1014
+ * Clear the local document cache and all v2.7 indices
1015
+ */
1016
+ clear() {
1017
+ this.documents.clear();
1018
+ this.dateIndex.clear();
1019
+ this.preferenceIndex.clear();
1020
+ this.chunkCache.clear();
1021
+ }
1022
+ /**
1023
+ * Export documents for persistence
1024
+ */
1025
+ export() {
1026
+ return {
1027
+ documents: [...this.documents.values()]
1028
+ };
1029
+ }
1030
+ /**
1031
+ * Import from persisted data
1032
+ */
1033
+ import(data) {
1034
+ this.clear();
1035
+ for (const doc of data.documents) {
1036
+ this.addDocument(doc);
1037
+ }
1038
+ }
1039
+ };
1040
+ function extractWikiLinks(content) {
1041
+ const matches = content.match(/\[\[([^\]]+)\]\]/g) || [];
1042
+ return matches.map((m) => m.slice(2, -2).toLowerCase());
1043
+ }
1044
+ function extractTags(content) {
1045
+ const matches = content.match(/#[\w-]+/g) || [];
1046
+ return [...new Set(matches.map((m) => m.slice(1).toLowerCase()))];
1047
+ }
1048
+
1049
+ export {
1050
+ isSuperseded,
1051
+ getSupersessionInfo,
1052
+ extractEntities,
1053
+ entitySimilarity,
1054
+ isKnowledgeUpdate,
1055
+ reweave,
1056
+ filterSuperseded,
1057
+ stripSupersededObservations,
1058
+ QMD_INSTALL_URL,
1059
+ QMD_INSTALL_COMMAND,
1060
+ QmdUnavailableError,
1061
+ withQmdIndexArgs,
1062
+ hasQmd,
1063
+ qmdUpdate,
1064
+ qmdEmbed,
1065
+ sentenceChunk,
1066
+ bm25RankChunks,
1067
+ extractDates,
1068
+ extractPreferences,
1069
+ classifyQuestion,
1070
+ SearchEngine,
1071
+ extractWikiLinks,
1072
+ extractTags
1073
+ };