clawvault 3.0.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (291) hide show
  1. package/README.md +352 -20
  2. package/bin/clawvault.js +8 -2
  3. package/bin/command-registration.test.js +3 -1
  4. package/bin/command-runtime.js +9 -1
  5. package/bin/register-core-commands.js +23 -10
  6. package/bin/register-maintenance-commands.js +39 -3
  7. package/bin/register-query-commands.js +58 -29
  8. package/bin/register-task-commands.js +18 -1
  9. package/bin/register-task-commands.test.js +16 -0
  10. package/bin/register-vault-operations-commands.js +29 -1
  11. package/bin/register-workgraph-commands.js +1368 -0
  12. package/dashboard/lib/graph-diff.js +104 -0
  13. package/dashboard/lib/graph-diff.test.js +75 -0
  14. package/dashboard/lib/vault-parser.js +556 -0
  15. package/dashboard/lib/vault-parser.test.js +254 -0
  16. package/dashboard/public/app.js +796 -0
  17. package/dashboard/public/index.html +52 -0
  18. package/dashboard/public/styles.css +221 -0
  19. package/dashboard/server.js +374 -0
  20. package/dist/{chunk-F2JEUD4J.js → chunk-23YDQ3QU.js} +6 -8
  21. package/dist/{chunk-C7OK5WKP.js → chunk-2JQ3O2YL.js} +4 -4
  22. package/dist/{chunk-VR5NE7PZ.js → chunk-2RAZ4ZFE.js} +1 -1
  23. package/dist/chunk-2ZDO52B4.js +52 -0
  24. package/dist/{chunk-ZZA73MFY.js → chunk-33DOSHTA.js} +176 -36
  25. package/dist/chunk-33VSQP4J.js +37 -0
  26. package/dist/chunk-4BQTQMJP.js +93 -0
  27. package/dist/{chunk-GUKMRGM7.js → chunk-4OXMU5S2.js} +1 -1
  28. package/dist/{chunk-62YTUT6J.js → chunk-4PY655YM.js} +15 -3
  29. package/dist/chunk-6FH3IULF.js +352 -0
  30. package/dist/{chunk-3NSBOUT3.js → chunk-77Q5CSPJ.js} +404 -80
  31. package/dist/{chunk-4VQTUVH7.js → chunk-7YZWHM36.js} +52 -26
  32. package/dist/chunk-BSJ6RIT7.js +447 -0
  33. package/dist/chunk-BUEW6IIK.js +364 -0
  34. package/dist/{chunk-WGRQ6HDV.js → chunk-CLJTREDS.js} +74 -14
  35. package/dist/chunk-EK6S23ZB.js +469 -0
  36. package/dist/{chunk-LNJA2UGL.js → chunk-ESFLMDRB.js} +9 -86
  37. package/dist/{chunk-H34S76MB.js → chunk-ESVS6K2B.js} +6 -6
  38. package/dist/{chunk-WAZ3NLWL.js → chunk-F55HGNU4.js} +0 -47
  39. package/dist/{chunk-QK3UCXWL.js → chunk-FHFUXL6G.js} +2 -2
  40. package/dist/{chunk-YKTA5JOJ.js → chunk-GAOWA7GR.js} +212 -46
  41. package/dist/chunk-GGA32J2R.js +784 -0
  42. package/dist/chunk-GNJL4YGR.js +79 -0
  43. package/dist/chunk-MDIH26GC.js +183 -0
  44. package/dist/{chunk-LYHGEHXG.js → chunk-MFAWT5O5.js} +0 -1
  45. package/dist/chunk-MM6QGW3P.js +207 -0
  46. package/dist/{chunk-P5EPF6MB.js → chunk-MW5C6ZQA.js} +110 -13
  47. package/dist/chunk-NCKFNBHJ.js +257 -0
  48. package/dist/{chunk-QBLMXKF2.js → chunk-OIWVQYQF.js} +1 -1
  49. package/dist/{chunk-42MXU7A6.js → chunk-P62WHA27.js} +58 -47
  50. package/dist/chunk-PBACDKKP.js +66 -0
  51. package/dist/{chunk-VGLOTGAS.js → chunk-QSHD36LH.js} +2 -2
  52. package/dist/{chunk-OZ7RIXTO.js → chunk-QSRRMEYM.js} +2 -2
  53. package/dist/chunk-QVEERJSP.js +152 -0
  54. package/dist/{chunk-N2AXRYLC.js → chunk-QWQ3TIKS.js} +1 -1
  55. package/dist/{chunk-3DHXQHYG.js → chunk-R2MIW5G7.js} +1 -1
  56. package/dist/{chunk-SJSFRIYS.js → chunk-SLXOR3CC.js} +2 -2
  57. package/dist/chunk-SS4B7P7V.js +99 -0
  58. package/dist/{chunk-JY6FYXIT.js → chunk-STCQGCEQ.js} +6 -11
  59. package/dist/chunk-U4O6C46S.js +154 -0
  60. package/dist/{chunk-ITPEXLHA.js → chunk-URXDAUVH.js} +24 -5
  61. package/dist/chunk-VSL7KY3M.js +189 -0
  62. package/dist/{chunk-U55BGUAU.js → chunk-W4SPAEE7.js} +6 -6
  63. package/dist/chunk-WMGIIABP.js +15 -0
  64. package/dist/{chunk-3D6BCTP6.js → chunk-X3SPPUFG.js} +51 -39
  65. package/dist/{chunk-THRJVD4L.js → chunk-Y6VJKXGL.js} +1 -1
  66. package/dist/{chunk-ZVVFWOLW.js → chunk-ZN54U2OZ.js} +123 -10
  67. package/dist/cli/index.js +32 -25
  68. package/dist/commands/archive.js +3 -3
  69. package/dist/commands/backlog.js +3 -3
  70. package/dist/commands/blocked.js +3 -3
  71. package/dist/commands/canvas.d.ts +15 -0
  72. package/dist/commands/canvas.js +200 -0
  73. package/dist/commands/checkpoint.js +2 -2
  74. package/dist/commands/compat.js +2 -2
  75. package/dist/commands/context.js +8 -6
  76. package/dist/commands/doctor.d.ts +11 -7
  77. package/dist/commands/doctor.js +18 -16
  78. package/dist/commands/embed.js +5 -6
  79. package/dist/commands/entities.js +2 -2
  80. package/dist/commands/graph.js +4 -4
  81. package/dist/commands/inject.d.ts +1 -1
  82. package/dist/commands/inject.js +5 -6
  83. package/dist/commands/kanban.js +4 -4
  84. package/dist/commands/link.js +5 -5
  85. package/dist/commands/migrate-observations.js +4 -4
  86. package/dist/commands/observe.d.ts +0 -1
  87. package/dist/commands/observe.js +14 -13
  88. package/dist/commands/project.js +5 -5
  89. package/dist/commands/rebuild-embeddings.d.ts +21 -0
  90. package/dist/commands/rebuild-embeddings.js +91 -0
  91. package/dist/commands/rebuild.js +12 -11
  92. package/dist/commands/recover.js +3 -3
  93. package/dist/commands/reflect.js +6 -7
  94. package/dist/commands/repair-session.js +1 -1
  95. package/dist/commands/replay.js +14 -14
  96. package/dist/commands/session-recap.js +1 -1
  97. package/dist/commands/setup.d.ts +2 -90
  98. package/dist/commands/setup.js +3 -21
  99. package/dist/commands/shell-init.js +1 -1
  100. package/dist/commands/sleep.d.ts +1 -1
  101. package/dist/commands/sleep.js +20 -19
  102. package/dist/commands/status.d.ts +2 -0
  103. package/dist/commands/status.js +57 -35
  104. package/dist/commands/sync-bd.d.ts +10 -0
  105. package/dist/commands/sync-bd.js +10 -0
  106. package/dist/commands/tailscale.js +3 -3
  107. package/dist/commands/task.js +4 -4
  108. package/dist/commands/template.js +2 -2
  109. package/dist/commands/wake.d.ts +1 -1
  110. package/dist/commands/wake.js +11 -10
  111. package/dist/commands/workgraph.d.ts +124 -0
  112. package/dist/commands/workgraph.js +38 -0
  113. package/dist/index.d.ts +337 -191
  114. package/dist/index.js +387 -118
  115. package/dist/{inject-Bzi5E-By.d.cts → inject-DYUrDqQO.d.ts} +3 -3
  116. package/dist/ledger-B7g7jhqG.d.ts +44 -0
  117. package/dist/lib/auto-linker.js +2 -2
  118. package/dist/lib/canvas-layout.d.ts +100 -16
  119. package/dist/lib/canvas-layout.js +21 -78
  120. package/dist/lib/config.d.ts +27 -3
  121. package/dist/lib/config.js +4 -2
  122. package/dist/lib/entity-index.js +1 -1
  123. package/dist/lib/project-utils.js +4 -4
  124. package/dist/lib/session-repair.js +1 -1
  125. package/dist/lib/session-utils.js +1 -1
  126. package/dist/lib/tailscale.js +1 -1
  127. package/dist/lib/task-utils.js +3 -3
  128. package/dist/lib/template-engine.js +1 -1
  129. package/dist/lib/webdav.js +1 -1
  130. package/dist/onnxruntime_binding-5QEF3SUC.node +0 -0
  131. package/dist/onnxruntime_binding-BKPKNEGC.node +0 -0
  132. package/dist/onnxruntime_binding-FMOXGIUT.node +0 -0
  133. package/dist/onnxruntime_binding-OI2KMXC5.node +0 -0
  134. package/dist/onnxruntime_binding-UX44MLAZ.node +0 -0
  135. package/dist/onnxruntime_binding-Y2W7N7WY.node +0 -0
  136. package/dist/openclaw-plugin.d.ts +8 -0
  137. package/dist/openclaw-plugin.js +14 -0
  138. package/dist/registry-BR4326o0.d.ts +30 -0
  139. package/dist/store-CA-6sKCJ.d.ts +34 -0
  140. package/dist/thread-B9LhXNU0.d.ts +41 -0
  141. package/dist/transformers.node-A2ZRORSQ.js +46775 -0
  142. package/dist/{types-Y2_Um2Ls.d.cts → types-BbWJoC1c.d.ts} +1 -44
  143. package/dist/workgraph/index.d.ts +5 -0
  144. package/dist/workgraph/index.js +23 -0
  145. package/dist/workgraph/ledger.d.ts +2 -0
  146. package/dist/workgraph/ledger.js +25 -0
  147. package/dist/workgraph/registry.d.ts +2 -0
  148. package/dist/workgraph/registry.js +19 -0
  149. package/dist/workgraph/store.d.ts +2 -0
  150. package/dist/workgraph/store.js +25 -0
  151. package/dist/workgraph/thread.d.ts +2 -0
  152. package/dist/workgraph/thread.js +25 -0
  153. package/dist/workgraph/types.d.ts +54 -0
  154. package/dist/workgraph/types.js +7 -0
  155. package/hooks/clawvault/HOOK.md +34 -4
  156. package/hooks/clawvault/handler.js +760 -78
  157. package/hooks/clawvault/handler.test.js +235 -79
  158. package/hooks/clawvault/openclaw.plugin.json +72 -0
  159. package/openclaw.plugin.json +65 -38
  160. package/package.json +15 -18
  161. package/dist/chunk-3RG5ZIWI.js +0 -10
  162. package/dist/chunk-6U6MK36V.js +0 -205
  163. package/dist/chunk-7R7O6STJ.js +0 -88
  164. package/dist/chunk-CMB7UL7C.js +0 -327
  165. package/dist/chunk-DEFFDRVP.js +0 -938
  166. package/dist/chunk-E7MFQB6D.js +0 -163
  167. package/dist/chunk-GAJV4IGR.js +0 -82
  168. package/dist/chunk-GQSLDZTS.js +0 -560
  169. package/dist/chunk-K234IDRJ.js +0 -1073
  170. package/dist/chunk-MFM6K7PU.js +0 -374
  171. package/dist/chunk-MXSSG3QU.js +0 -42
  172. package/dist/chunk-PAH27GSN.js +0 -108
  173. package/dist/cli/index.cjs +0 -10033
  174. package/dist/cli/index.d.cts +0 -5
  175. package/dist/commands/archive.cjs +0 -287
  176. package/dist/commands/archive.d.cts +0 -11
  177. package/dist/commands/backlog.cjs +0 -721
  178. package/dist/commands/backlog.d.cts +0 -53
  179. package/dist/commands/blocked.cjs +0 -204
  180. package/dist/commands/blocked.d.cts +0 -26
  181. package/dist/commands/checkpoint.cjs +0 -244
  182. package/dist/commands/checkpoint.d.cts +0 -41
  183. package/dist/commands/compat.cjs +0 -369
  184. package/dist/commands/compat.d.cts +0 -28
  185. package/dist/commands/context.cjs +0 -2989
  186. package/dist/commands/context.d.cts +0 -2
  187. package/dist/commands/doctor.cjs +0 -3062
  188. package/dist/commands/doctor.d.cts +0 -21
  189. package/dist/commands/embed.cjs +0 -232
  190. package/dist/commands/embed.d.cts +0 -17
  191. package/dist/commands/entities.cjs +0 -141
  192. package/dist/commands/entities.d.cts +0 -7
  193. package/dist/commands/graph.cjs +0 -501
  194. package/dist/commands/graph.d.cts +0 -21
  195. package/dist/commands/inject.cjs +0 -1636
  196. package/dist/commands/inject.d.cts +0 -2
  197. package/dist/commands/kanban.cjs +0 -884
  198. package/dist/commands/kanban.d.cts +0 -63
  199. package/dist/commands/link.cjs +0 -965
  200. package/dist/commands/link.d.cts +0 -11
  201. package/dist/commands/migrate-observations.cjs +0 -362
  202. package/dist/commands/migrate-observations.d.cts +0 -19
  203. package/dist/commands/observe.cjs +0 -4099
  204. package/dist/commands/observe.d.cts +0 -23
  205. package/dist/commands/project.cjs +0 -1341
  206. package/dist/commands/project.d.cts +0 -85
  207. package/dist/commands/rebuild.cjs +0 -3136
  208. package/dist/commands/rebuild.d.cts +0 -11
  209. package/dist/commands/recover.cjs +0 -361
  210. package/dist/commands/recover.d.cts +0 -38
  211. package/dist/commands/reflect.cjs +0 -1008
  212. package/dist/commands/reflect.d.cts +0 -11
  213. package/dist/commands/repair-session.cjs +0 -457
  214. package/dist/commands/repair-session.d.cts +0 -38
  215. package/dist/commands/replay.cjs +0 -4103
  216. package/dist/commands/replay.d.cts +0 -16
  217. package/dist/commands/session-recap.cjs +0 -353
  218. package/dist/commands/session-recap.d.cts +0 -27
  219. package/dist/commands/setup.cjs +0 -1345
  220. package/dist/commands/setup.d.cts +0 -100
  221. package/dist/commands/shell-init.cjs +0 -75
  222. package/dist/commands/shell-init.d.cts +0 -7
  223. package/dist/commands/sleep.cjs +0 -6028
  224. package/dist/commands/sleep.d.cts +0 -36
  225. package/dist/commands/status.cjs +0 -2736
  226. package/dist/commands/status.d.cts +0 -52
  227. package/dist/commands/tailscale.cjs +0 -1532
  228. package/dist/commands/tailscale.d.cts +0 -52
  229. package/dist/commands/task.cjs +0 -1236
  230. package/dist/commands/task.d.cts +0 -97
  231. package/dist/commands/template.cjs +0 -457
  232. package/dist/commands/template.d.cts +0 -36
  233. package/dist/commands/wake.cjs +0 -2626
  234. package/dist/commands/wake.d.cts +0 -22
  235. package/dist/context-BUGaWpyL.d.cts +0 -46
  236. package/dist/index.cjs +0 -14526
  237. package/dist/index.d.cts +0 -858
  238. package/dist/inject-Bzi5E-By.d.ts +0 -137
  239. package/dist/lib/auto-linker.cjs +0 -176
  240. package/dist/lib/auto-linker.d.cts +0 -26
  241. package/dist/lib/canvas-layout.cjs +0 -136
  242. package/dist/lib/canvas-layout.d.cts +0 -31
  243. package/dist/lib/config.cjs +0 -78
  244. package/dist/lib/config.d.cts +0 -11
  245. package/dist/lib/entity-index.cjs +0 -84
  246. package/dist/lib/entity-index.d.cts +0 -26
  247. package/dist/lib/project-utils.cjs +0 -864
  248. package/dist/lib/project-utils.d.cts +0 -97
  249. package/dist/lib/session-repair.cjs +0 -239
  250. package/dist/lib/session-repair.d.cts +0 -110
  251. package/dist/lib/session-utils.cjs +0 -209
  252. package/dist/lib/session-utils.d.cts +0 -63
  253. package/dist/lib/tailscale.cjs +0 -1183
  254. package/dist/lib/tailscale.d.cts +0 -225
  255. package/dist/lib/task-utils.cjs +0 -1137
  256. package/dist/lib/task-utils.d.cts +0 -208
  257. package/dist/lib/template-engine.cjs +0 -47
  258. package/dist/lib/template-engine.d.cts +0 -11
  259. package/dist/lib/webdav.cjs +0 -568
  260. package/dist/lib/webdav.d.cts +0 -109
  261. package/dist/plugin/index.cjs +0 -1907
  262. package/dist/plugin/index.d.cts +0 -36
  263. package/dist/plugin/index.d.ts +0 -36
  264. package/dist/plugin/index.js +0 -572
  265. package/dist/plugin/inject.cjs +0 -356
  266. package/dist/plugin/inject.d.cts +0 -54
  267. package/dist/plugin/inject.d.ts +0 -54
  268. package/dist/plugin/inject.js +0 -17
  269. package/dist/plugin/observe.cjs +0 -631
  270. package/dist/plugin/observe.d.cts +0 -39
  271. package/dist/plugin/observe.d.ts +0 -39
  272. package/dist/plugin/observe.js +0 -18
  273. package/dist/plugin/templates.cjs +0 -593
  274. package/dist/plugin/templates.d.cts +0 -52
  275. package/dist/plugin/templates.d.ts +0 -52
  276. package/dist/plugin/templates.js +0 -25
  277. package/dist/plugin/types.cjs +0 -18
  278. package/dist/plugin/types.d.cts +0 -209
  279. package/dist/plugin/types.d.ts +0 -209
  280. package/dist/plugin/types.js +0 -0
  281. package/dist/plugin/vault.cjs +0 -927
  282. package/dist/plugin/vault.d.cts +0 -68
  283. package/dist/plugin/vault.d.ts +0 -68
  284. package/dist/plugin/vault.js +0 -22
  285. package/dist/types-Y2_Um2Ls.d.ts +0 -205
  286. package/templates/memory-event.md +0 -67
  287. package/templates/party.md +0 -63
  288. package/templates/primitive-registry.yaml +0 -551
  289. package/templates/run.md +0 -68
  290. package/templates/trigger.md +0 -68
  291. package/templates/workspace.md +0 -50
@@ -1,1073 +0,0 @@
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
- };