clawvault 3.1.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 (289) hide show
  1. package/README.md +422 -141
  2. package/bin/clawvault.js +10 -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 -28
  6. package/bin/register-maintenance-commands.js +39 -3
  7. package/bin/register-query-commands.js +58 -29
  8. package/bin/register-tailscale-commands.js +106 -0
  9. package/bin/register-task-commands.js +18 -1
  10. package/bin/register-task-commands.test.js +16 -0
  11. package/bin/register-vault-operations-commands.js +29 -1
  12. package/bin/register-workgraph-commands.js +1368 -0
  13. package/dashboard/lib/graph-diff.js +104 -0
  14. package/dashboard/lib/graph-diff.test.js +75 -0
  15. package/dashboard/lib/vault-parser.js +556 -0
  16. package/dashboard/lib/vault-parser.test.js +254 -0
  17. package/dashboard/public/app.js +796 -0
  18. package/dashboard/public/index.html +52 -0
  19. package/dashboard/public/styles.css +221 -0
  20. package/dashboard/server.js +374 -0
  21. package/dist/{chunk-F2JEUD4J.js → chunk-23YDQ3QU.js} +6 -8
  22. package/dist/{chunk-C7OK5WKP.js → chunk-2JQ3O2YL.js} +4 -4
  23. package/dist/{chunk-VR5NE7PZ.js → chunk-2RAZ4ZFE.js} +1 -1
  24. package/dist/chunk-2ZDO52B4.js +52 -0
  25. package/dist/{chunk-ZZA73MFY.js → chunk-33DOSHTA.js} +176 -36
  26. package/dist/chunk-33VSQP4J.js +37 -0
  27. package/dist/chunk-4BQTQMJP.js +93 -0
  28. package/dist/{chunk-GUKMRGM7.js → chunk-4OXMU5S2.js} +1 -1
  29. package/dist/{chunk-62YTUT6J.js → chunk-4PY655YM.js} +15 -3
  30. package/dist/chunk-6FH3IULF.js +352 -0
  31. package/dist/{chunk-3NSBOUT3.js → chunk-77Q5CSPJ.js} +404 -80
  32. package/dist/{chunk-4VQTUVH7.js → chunk-7YZWHM36.js} +52 -26
  33. package/dist/chunk-BSJ6RIT7.js +447 -0
  34. package/dist/chunk-BUEW6IIK.js +364 -0
  35. package/dist/{chunk-LI4O6NVK.js → chunk-CLJTREDS.js} +74 -14
  36. package/dist/chunk-EK6S23ZB.js +469 -0
  37. package/dist/{chunk-LNJA2UGL.js → chunk-ESFLMDRB.js} +9 -86
  38. package/dist/{chunk-H34S76MB.js → chunk-ESVS6K2B.js} +6 -6
  39. package/dist/{chunk-WAZ3NLWL.js → chunk-F55HGNU4.js} +0 -47
  40. package/dist/{chunk-QK3UCXWL.js → chunk-FHFUXL6G.js} +2 -2
  41. package/dist/{chunk-H62BP7RI.js → chunk-GAOWA7GR.js} +212 -46
  42. package/dist/chunk-GGA32J2R.js +784 -0
  43. package/dist/chunk-GNJL4YGR.js +79 -0
  44. package/dist/chunk-IVRIKYFE.js +520 -0
  45. package/dist/chunk-MDIH26GC.js +183 -0
  46. package/dist/{chunk-LYHGEHXG.js → chunk-MFAWT5O5.js} +0 -1
  47. package/dist/chunk-MM6QGW3P.js +207 -0
  48. package/dist/{chunk-P5EPF6MB.js → chunk-MW5C6ZQA.js} +110 -13
  49. package/dist/chunk-NCKFNBHJ.js +257 -0
  50. package/dist/{chunk-QBLMXKF2.js → chunk-OIWVQYQF.js} +1 -1
  51. package/dist/{chunk-42MXU7A6.js → chunk-P62WHA27.js} +58 -47
  52. package/dist/chunk-PBACDKKP.js +66 -0
  53. package/dist/{chunk-VGLOTGAS.js → chunk-QSHD36LH.js} +2 -2
  54. package/dist/{chunk-OZ7RIXTO.js → chunk-QSRRMEYM.js} +2 -2
  55. package/dist/chunk-QVEERJSP.js +152 -0
  56. package/dist/{chunk-N2AXRYLC.js → chunk-QWQ3TIKS.js} +1 -1
  57. package/dist/{chunk-3DHXQHYG.js → chunk-R2MIW5G7.js} +1 -1
  58. package/dist/{chunk-SJSFRIYS.js → chunk-SLXOR3CC.js} +2 -2
  59. package/dist/chunk-SS4B7P7V.js +99 -0
  60. package/dist/{chunk-JY6FYXIT.js → chunk-STCQGCEQ.js} +6 -11
  61. package/dist/chunk-TIGW564L.js +628 -0
  62. package/dist/chunk-U4O6C46S.js +154 -0
  63. package/dist/{chunk-ITPEXLHA.js → chunk-URXDAUVH.js} +24 -5
  64. package/dist/chunk-VSL7KY3M.js +189 -0
  65. package/dist/{chunk-U55BGUAU.js → chunk-W4SPAEE7.js} +6 -6
  66. package/dist/chunk-WMGIIABP.js +15 -0
  67. package/dist/{chunk-33UGEQRT.js → chunk-X3SPPUFG.js} +151 -64
  68. package/dist/chunk-Y6VJKXGL.js +373 -0
  69. package/dist/{chunk-3WRJEKN4.js → chunk-ZN54U2OZ.js} +123 -10
  70. package/dist/cli/index.js +34 -24
  71. package/dist/commands/archive.js +3 -3
  72. package/dist/commands/backlog.js +3 -3
  73. package/dist/commands/blocked.js +3 -3
  74. package/dist/commands/canvas.d.ts +15 -0
  75. package/dist/commands/canvas.js +200 -0
  76. package/dist/commands/checkpoint.js +2 -2
  77. package/dist/commands/compat.js +2 -2
  78. package/dist/commands/context.js +8 -6
  79. package/dist/commands/doctor.d.ts +11 -7
  80. package/dist/commands/doctor.js +18 -16
  81. package/dist/commands/embed.js +5 -6
  82. package/dist/commands/entities.js +2 -2
  83. package/dist/commands/graph.js +4 -4
  84. package/dist/commands/inject.d.ts +1 -1
  85. package/dist/commands/inject.js +5 -6
  86. package/dist/commands/kanban.js +4 -4
  87. package/dist/commands/link.js +5 -5
  88. package/dist/commands/migrate-observations.js +4 -4
  89. package/dist/commands/observe.d.ts +0 -1
  90. package/dist/commands/observe.js +14 -13
  91. package/dist/commands/project.js +5 -5
  92. package/dist/commands/rebuild-embeddings.d.ts +21 -0
  93. package/dist/commands/rebuild-embeddings.js +91 -0
  94. package/dist/commands/rebuild.js +12 -11
  95. package/dist/commands/recover.js +3 -3
  96. package/dist/commands/reflect.js +6 -7
  97. package/dist/commands/repair-session.js +1 -1
  98. package/dist/commands/replay.js +14 -14
  99. package/dist/commands/session-recap.js +1 -1
  100. package/dist/commands/setup.d.ts +2 -89
  101. package/dist/commands/setup.js +3 -21
  102. package/dist/commands/shell-init.js +1 -1
  103. package/dist/commands/sleep.d.ts +1 -1
  104. package/dist/commands/sleep.js +20 -19
  105. package/dist/commands/status.d.ts +2 -0
  106. package/dist/commands/status.js +57 -35
  107. package/dist/commands/sync-bd.d.ts +10 -0
  108. package/dist/commands/sync-bd.js +10 -0
  109. package/dist/commands/tailscale.d.ts +52 -0
  110. package/dist/commands/tailscale.js +26 -0
  111. package/dist/commands/task.js +4 -4
  112. package/dist/commands/template.js +2 -2
  113. package/dist/commands/wake.d.ts +1 -1
  114. package/dist/commands/wake.js +11 -10
  115. package/dist/commands/workgraph.d.ts +124 -0
  116. package/dist/commands/workgraph.js +38 -0
  117. package/dist/index.d.ts +341 -191
  118. package/dist/index.js +446 -116
  119. package/dist/{inject-Bzi5E-By.d.ts → inject-DYUrDqQO.d.ts} +3 -3
  120. package/dist/ledger-B7g7jhqG.d.ts +44 -0
  121. package/dist/lib/auto-linker.js +2 -2
  122. package/dist/lib/canvas-layout.d.ts +115 -0
  123. package/dist/lib/canvas-layout.js +35 -0
  124. package/dist/lib/config.d.ts +27 -3
  125. package/dist/lib/config.js +4 -2
  126. package/dist/lib/entity-index.js +1 -1
  127. package/dist/lib/project-utils.js +4 -4
  128. package/dist/lib/session-repair.js +1 -1
  129. package/dist/lib/session-utils.js +1 -1
  130. package/dist/lib/tailscale.d.ts +225 -0
  131. package/dist/lib/tailscale.js +50 -0
  132. package/dist/lib/task-utils.js +3 -3
  133. package/dist/lib/template-engine.js +1 -1
  134. package/dist/lib/webdav.d.ts +109 -0
  135. package/dist/lib/webdav.js +35 -0
  136. package/dist/onnxruntime_binding-5QEF3SUC.node +0 -0
  137. package/dist/onnxruntime_binding-BKPKNEGC.node +0 -0
  138. package/dist/onnxruntime_binding-FMOXGIUT.node +0 -0
  139. package/dist/onnxruntime_binding-OI2KMXC5.node +0 -0
  140. package/dist/onnxruntime_binding-UX44MLAZ.node +0 -0
  141. package/dist/onnxruntime_binding-Y2W7N7WY.node +0 -0
  142. package/dist/openclaw-plugin.d.ts +8 -0
  143. package/dist/openclaw-plugin.js +14 -0
  144. package/dist/registry-BR4326o0.d.ts +30 -0
  145. package/dist/store-CA-6sKCJ.d.ts +34 -0
  146. package/dist/thread-B9LhXNU0.d.ts +41 -0
  147. package/dist/transformers.node-A2ZRORSQ.js +46775 -0
  148. package/dist/{types-Y2_Um2Ls.d.ts → types-BbWJoC1c.d.ts} +1 -44
  149. package/dist/workgraph/index.d.ts +5 -0
  150. package/dist/workgraph/index.js +23 -0
  151. package/dist/workgraph/ledger.d.ts +2 -0
  152. package/dist/workgraph/ledger.js +25 -0
  153. package/dist/workgraph/registry.d.ts +2 -0
  154. package/dist/workgraph/registry.js +19 -0
  155. package/dist/workgraph/store.d.ts +2 -0
  156. package/dist/workgraph/store.js +25 -0
  157. package/dist/workgraph/thread.d.ts +2 -0
  158. package/dist/workgraph/thread.js +25 -0
  159. package/dist/workgraph/types.d.ts +54 -0
  160. package/dist/workgraph/types.js +7 -0
  161. package/hooks/clawvault/HOOK.md +113 -0
  162. package/hooks/clawvault/handler.js +1561 -0
  163. package/hooks/clawvault/handler.test.js +510 -0
  164. package/hooks/clawvault/openclaw.plugin.json +72 -0
  165. package/openclaw.plugin.json +65 -38
  166. package/package.json +25 -22
  167. package/dist/chunk-3RG5ZIWI.js +0 -10
  168. package/dist/chunk-3ZIH425O.js +0 -871
  169. package/dist/chunk-6U6MK36V.js +0 -205
  170. package/dist/chunk-CMB7UL7C.js +0 -327
  171. package/dist/chunk-D2H45LON.js +0 -1074
  172. package/dist/chunk-E7MFQB6D.js +0 -163
  173. package/dist/chunk-GQSLDZTS.js +0 -560
  174. package/dist/chunk-MFM6K7PU.js +0 -374
  175. package/dist/chunk-MXSSG3QU.js +0 -42
  176. package/dist/chunk-OCGVIN3L.js +0 -88
  177. package/dist/chunk-PAH27GSN.js +0 -108
  178. package/dist/chunk-YCUNCH2I.js +0 -78
  179. package/dist/cli/index.cjs +0 -8584
  180. package/dist/cli/index.d.cts +0 -5
  181. package/dist/commands/archive.cjs +0 -287
  182. package/dist/commands/archive.d.cts +0 -11
  183. package/dist/commands/backlog.cjs +0 -721
  184. package/dist/commands/backlog.d.cts +0 -53
  185. package/dist/commands/blocked.cjs +0 -204
  186. package/dist/commands/blocked.d.cts +0 -26
  187. package/dist/commands/checkpoint.cjs +0 -244
  188. package/dist/commands/checkpoint.d.cts +0 -41
  189. package/dist/commands/compat.cjs +0 -294
  190. package/dist/commands/compat.d.cts +0 -28
  191. package/dist/commands/context.cjs +0 -2990
  192. package/dist/commands/context.d.cts +0 -2
  193. package/dist/commands/doctor.cjs +0 -2986
  194. package/dist/commands/doctor.d.cts +0 -21
  195. package/dist/commands/embed.cjs +0 -232
  196. package/dist/commands/embed.d.cts +0 -17
  197. package/dist/commands/entities.cjs +0 -141
  198. package/dist/commands/entities.d.cts +0 -7
  199. package/dist/commands/graph.cjs +0 -501
  200. package/dist/commands/graph.d.cts +0 -21
  201. package/dist/commands/inject.cjs +0 -1636
  202. package/dist/commands/inject.d.cts +0 -2
  203. package/dist/commands/kanban.cjs +0 -884
  204. package/dist/commands/kanban.d.cts +0 -63
  205. package/dist/commands/link.cjs +0 -965
  206. package/dist/commands/link.d.cts +0 -11
  207. package/dist/commands/migrate-observations.cjs +0 -362
  208. package/dist/commands/migrate-observations.d.cts +0 -19
  209. package/dist/commands/observe.cjs +0 -4099
  210. package/dist/commands/observe.d.cts +0 -23
  211. package/dist/commands/project.cjs +0 -1341
  212. package/dist/commands/project.d.cts +0 -85
  213. package/dist/commands/rebuild.cjs +0 -3136
  214. package/dist/commands/rebuild.d.cts +0 -11
  215. package/dist/commands/recover.cjs +0 -361
  216. package/dist/commands/recover.d.cts +0 -38
  217. package/dist/commands/reflect.cjs +0 -1008
  218. package/dist/commands/reflect.d.cts +0 -11
  219. package/dist/commands/repair-session.cjs +0 -457
  220. package/dist/commands/repair-session.d.cts +0 -38
  221. package/dist/commands/replay.cjs +0 -4103
  222. package/dist/commands/replay.d.cts +0 -16
  223. package/dist/commands/session-recap.cjs +0 -353
  224. package/dist/commands/session-recap.d.cts +0 -27
  225. package/dist/commands/setup.cjs +0 -1278
  226. package/dist/commands/setup.d.cts +0 -99
  227. package/dist/commands/shell-init.cjs +0 -75
  228. package/dist/commands/shell-init.d.cts +0 -7
  229. package/dist/commands/sleep.cjs +0 -6029
  230. package/dist/commands/sleep.d.cts +0 -36
  231. package/dist/commands/status.cjs +0 -2737
  232. package/dist/commands/status.d.cts +0 -52
  233. package/dist/commands/task.cjs +0 -1236
  234. package/dist/commands/task.d.cts +0 -97
  235. package/dist/commands/template.cjs +0 -457
  236. package/dist/commands/template.d.cts +0 -36
  237. package/dist/commands/wake.cjs +0 -2627
  238. package/dist/commands/wake.d.cts +0 -22
  239. package/dist/context-BUGaWpyL.d.cts +0 -46
  240. package/dist/index.cjs +0 -12373
  241. package/dist/index.d.cts +0 -854
  242. package/dist/inject-Bzi5E-By.d.cts +0 -137
  243. package/dist/lib/auto-linker.cjs +0 -176
  244. package/dist/lib/auto-linker.d.cts +0 -26
  245. package/dist/lib/config.cjs +0 -78
  246. package/dist/lib/config.d.cts +0 -11
  247. package/dist/lib/entity-index.cjs +0 -84
  248. package/dist/lib/entity-index.d.cts +0 -26
  249. package/dist/lib/project-utils.cjs +0 -864
  250. package/dist/lib/project-utils.d.cts +0 -97
  251. package/dist/lib/session-repair.cjs +0 -239
  252. package/dist/lib/session-repair.d.cts +0 -110
  253. package/dist/lib/session-utils.cjs +0 -209
  254. package/dist/lib/session-utils.d.cts +0 -63
  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/plugin/index.cjs +0 -1907
  260. package/dist/plugin/index.d.cts +0 -36
  261. package/dist/plugin/index.d.ts +0 -36
  262. package/dist/plugin/index.js +0 -572
  263. package/dist/plugin/inject.cjs +0 -356
  264. package/dist/plugin/inject.d.cts +0 -54
  265. package/dist/plugin/inject.d.ts +0 -54
  266. package/dist/plugin/inject.js +0 -17
  267. package/dist/plugin/observe.cjs +0 -631
  268. package/dist/plugin/observe.d.cts +0 -39
  269. package/dist/plugin/observe.d.ts +0 -39
  270. package/dist/plugin/observe.js +0 -18
  271. package/dist/plugin/templates.cjs +0 -593
  272. package/dist/plugin/templates.d.cts +0 -52
  273. package/dist/plugin/templates.d.ts +0 -52
  274. package/dist/plugin/templates.js +0 -25
  275. package/dist/plugin/types.cjs +0 -18
  276. package/dist/plugin/types.d.cts +0 -209
  277. package/dist/plugin/types.d.ts +0 -209
  278. package/dist/plugin/types.js +0 -0
  279. package/dist/plugin/vault.cjs +0 -927
  280. package/dist/plugin/vault.d.cts +0 -68
  281. package/dist/plugin/vault.d.ts +0 -68
  282. package/dist/plugin/vault.js +0 -22
  283. package/dist/types-Y2_Um2Ls.d.cts +0 -205
  284. package/templates/memory-event.md +0 -67
  285. package/templates/party.md +0 -63
  286. package/templates/primitive-registry.yaml +0 -551
  287. package/templates/run.md +0 -68
  288. package/templates/trigger.md +0 -68
  289. package/templates/workspace.md +0 -50
@@ -1,965 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/commands/link.ts
31
- var link_exports = {};
32
- __export(link_exports, {
33
- linkCommand: () => linkCommand
34
- });
35
- module.exports = __toCommonJS(link_exports);
36
- var fs5 = __toESM(require("fs"), 1);
37
- var path5 = __toESM(require("path"), 1);
38
-
39
- // src/lib/entity-index.ts
40
- var fs = __toESM(require("fs"), 1);
41
- var path = __toESM(require("path"), 1);
42
- var import_gray_matter = __toESM(require("gray-matter"), 1);
43
- function buildEntityIndex(vaultPath) {
44
- const entries = /* @__PURE__ */ new Map();
45
- const byPath = /* @__PURE__ */ new Map();
46
- const entityFolders = ["people", "projects", "agents", "lessons", "decisions", "commitments"];
47
- for (const folder of entityFolders) {
48
- const folderPath = path.join(vaultPath, folder);
49
- if (!fs.existsSync(folderPath)) continue;
50
- const files = fs.readdirSync(folderPath).filter((f) => f.endsWith(".md"));
51
- for (const file of files) {
52
- const filePath = path.join(folderPath, file);
53
- const content = fs.readFileSync(filePath, "utf-8");
54
- const { data: frontmatter } = (0, import_gray_matter.default)(content);
55
- const relativePath = `${folder}/${file.replace(".md", "")}`;
56
- const baseName = file.replace(".md", "");
57
- const aliases = [baseName];
58
- if (frontmatter.title && frontmatter.title.toLowerCase() !== baseName.toLowerCase()) {
59
- aliases.push(frontmatter.title);
60
- }
61
- if (Array.isArray(frontmatter.aliases)) {
62
- aliases.push(...frontmatter.aliases);
63
- }
64
- for (const alias of aliases) {
65
- const key = alias.toLowerCase();
66
- if (!entries.has(key)) {
67
- entries.set(key, relativePath);
68
- }
69
- }
70
- byPath.set(relativePath, { path: relativePath, aliases });
71
- }
72
- }
73
- return { entries, byPath };
74
- }
75
- function getSortedAliases(index) {
76
- const result = [];
77
- for (const [alias, path6] of index.entries) {
78
- result.push({ alias, path: path6 });
79
- }
80
- result.sort((a, b) => b.alias.length - a.alias.length);
81
- return result;
82
- }
83
-
84
- // src/lib/auto-linker.ts
85
- function findProtectedRanges(content) {
86
- const ranges = [];
87
- const fmMatch = content.match(/^---\n[\s\S]*?\n---/);
88
- if (fmMatch) {
89
- ranges.push({ start: 0, end: fmMatch[0].length });
90
- }
91
- const codeBlockRegex = /```[\s\S]*?```|~~~[\s\S]*?~~~/g;
92
- let match;
93
- while ((match = codeBlockRegex.exec(content)) !== null) {
94
- ranges.push({ start: match.index, end: match.index + match[0].length });
95
- }
96
- const inlineCodeRegex = /`[^`]+`/g;
97
- while ((match = inlineCodeRegex.exec(content)) !== null) {
98
- ranges.push({ start: match.index, end: match.index + match[0].length });
99
- }
100
- const wikiLinkRegex = /\[\[[^\]]+\]\]/g;
101
- while ((match = wikiLinkRegex.exec(content)) !== null) {
102
- ranges.push({ start: match.index, end: match.index + match[0].length });
103
- }
104
- const urlRegex = /https?:\/\/[^\s)>\]]+/g;
105
- while ((match = urlRegex.exec(content)) !== null) {
106
- ranges.push({ start: match.index, end: match.index + match[0].length });
107
- }
108
- return ranges;
109
- }
110
- function isProtected(pos, ranges) {
111
- return ranges.some((r) => pos >= r.start && pos < r.end);
112
- }
113
- function createLineLookup(content) {
114
- const lines = content.split("\n");
115
- let charPos = 0;
116
- const lineStarts = [];
117
- for (const line of lines) {
118
- lineStarts.push(charPos);
119
- charPos += line.length + 1;
120
- }
121
- return (pos) => {
122
- for (let i = lineStarts.length - 1; i >= 0; i--) {
123
- if (pos >= lineStarts[i]) return i + 1;
124
- }
125
- return 1;
126
- };
127
- }
128
- function autoLink(content, index) {
129
- const protectedRanges = findProtectedRanges(content);
130
- const sortedAliases = getSortedAliases(index);
131
- const linkedEntities = /* @__PURE__ */ new Set();
132
- let result = content;
133
- let offset = 0;
134
- for (const { alias, path: path6 } of sortedAliases) {
135
- if (linkedEntities.has(path6)) continue;
136
- const escapedAlias = alias.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
137
- const regex = new RegExp(`\\b${escapedAlias}\\b`, "gi");
138
- let match;
139
- while ((match = regex.exec(content)) !== null) {
140
- const originalPos = match.index;
141
- const adjustedPos = originalPos + offset;
142
- if (isProtected(originalPos, protectedRanges)) continue;
143
- const beforeMatch = result.substring(0, adjustedPos);
144
- const openBrackets = (beforeMatch.match(/\[\[/g) || []).length;
145
- const closeBrackets = (beforeMatch.match(/\]\]/g) || []).length;
146
- if (openBrackets > closeBrackets) continue;
147
- const originalText = match[0];
148
- const replacement = originalText.toLowerCase() === path6.split("/").pop()?.toLowerCase() ? `[[${path6}]]` : `[[${path6}|${originalText}]]`;
149
- result = result.substring(0, adjustedPos) + replacement + result.substring(adjustedPos + originalText.length);
150
- offset += replacement.length - originalText.length;
151
- linkedEntities.add(path6);
152
- break;
153
- }
154
- }
155
- return result;
156
- }
157
- function dryRunLink(content, index) {
158
- const protectedRanges = findProtectedRanges(content);
159
- const sortedAliases = getSortedAliases(index);
160
- const linkedEntities = /* @__PURE__ */ new Set();
161
- const matches = [];
162
- const getLineNumber = createLineLookup(content);
163
- for (const { alias, path: path6 } of sortedAliases) {
164
- if (linkedEntities.has(path6)) continue;
165
- const escapedAlias = alias.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
166
- const regex = new RegExp(`\\b${escapedAlias}\\b`, "gi");
167
- let match;
168
- while ((match = regex.exec(content)) !== null) {
169
- if (isProtected(match.index, protectedRanges)) continue;
170
- matches.push({
171
- alias: match[0],
172
- path: path6,
173
- line: getLineNumber(match.index)
174
- });
175
- linkedEntities.add(path6);
176
- break;
177
- }
178
- }
179
- return matches;
180
- }
181
- function findUnlinkedMentions(content, index) {
182
- const protectedRanges = findProtectedRanges(content);
183
- const sortedAliases = getSortedAliases(index);
184
- const matches = [];
185
- const seen = /* @__PURE__ */ new Set();
186
- const getLineNumber = createLineLookup(content);
187
- for (const { alias, path: path6 } of sortedAliases) {
188
- if (seen.has(path6)) continue;
189
- const escapedAlias = alias.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
190
- const regex = new RegExp(`\\b${escapedAlias}\\b`, "gi");
191
- let match;
192
- while ((match = regex.exec(content)) !== null) {
193
- if (isProtected(match.index, protectedRanges)) continue;
194
- matches.push({
195
- alias: match[0],
196
- path: path6,
197
- line: getLineNumber(match.index)
198
- });
199
- seen.add(path6);
200
- break;
201
- }
202
- }
203
- return matches;
204
- }
205
-
206
- // src/lib/backlinks.ts
207
- var fs2 = __toESM(require("fs"), 1);
208
- var path2 = __toESM(require("path"), 1);
209
- var CLAWVAULT_DIR = ".clawvault";
210
- var BACKLINKS_FILE = "backlinks.json";
211
- var WIKI_LINK_REGEX = /\[\[([^\]]+)\]\]/g;
212
- function ensureClawvaultDir(vaultPath) {
213
- const dir = path2.join(vaultPath, CLAWVAULT_DIR);
214
- if (!fs2.existsSync(dir)) {
215
- fs2.mkdirSync(dir, { recursive: true });
216
- }
217
- return dir;
218
- }
219
- function toVaultId(vaultPath, filePath) {
220
- const relative3 = path2.relative(vaultPath, filePath).replace(/\.md$/, "");
221
- return relative3.split(path2.sep).join("/");
222
- }
223
- function normalizeLinkTarget(raw) {
224
- let target = raw.trim();
225
- if (!target) return "";
226
- if (target.startsWith("[[") && target.endsWith("]]")) {
227
- target = target.slice(2, -2);
228
- }
229
- const pipeIndex = target.indexOf("|");
230
- if (pipeIndex !== -1) {
231
- target = target.slice(0, pipeIndex);
232
- }
233
- if (target.startsWith("#")) return "";
234
- const hashIndex = target.indexOf("#");
235
- if (hashIndex !== -1) {
236
- target = target.slice(0, hashIndex);
237
- }
238
- target = target.trim();
239
- if (!target) return "";
240
- if (target.endsWith(".md")) {
241
- target = target.slice(0, -3);
242
- }
243
- if (target.startsWith("/")) {
244
- target = target.slice(1);
245
- }
246
- return target.replace(/\\/g, "/");
247
- }
248
- function listMarkdownFiles(vaultPath) {
249
- const files = [];
250
- const skipDirs = /* @__PURE__ */ new Set(["archive", "templates", "node_modules"]);
251
- function walk(dir) {
252
- const entries = fs2.readdirSync(dir, { withFileTypes: true });
253
- for (const entry of entries) {
254
- const fullPath = path2.join(dir, entry.name);
255
- if (entry.isDirectory()) {
256
- if (entry.name.startsWith(".") || skipDirs.has(entry.name)) continue;
257
- walk(fullPath);
258
- } else if (entry.isFile() && entry.name.endsWith(".md")) {
259
- files.push(fullPath);
260
- }
261
- }
262
- }
263
- walk(vaultPath);
264
- return files;
265
- }
266
- function buildKnownIds(vaultPath, files) {
267
- const ids = /* @__PURE__ */ new Set();
268
- const idsLower = /* @__PURE__ */ new Map();
269
- for (const file of files) {
270
- const id = toVaultId(vaultPath, file);
271
- ids.add(id);
272
- const lower = id.toLowerCase();
273
- if (!idsLower.has(lower)) {
274
- idsLower.set(lower, id);
275
- }
276
- }
277
- return { ids, idsLower };
278
- }
279
- function resolveTarget(target, known, entityIndex) {
280
- if (!target) return null;
281
- if (known.ids.has(target)) return target;
282
- const lower = target.toLowerCase();
283
- if (known.idsLower.has(lower)) return known.idsLower.get(lower);
284
- if (entityIndex?.entries.has(lower)) return entityIndex.entries.get(lower);
285
- return null;
286
- }
287
- function scanVaultLinks(vaultPath, options = {}) {
288
- const files = listMarkdownFiles(vaultPath);
289
- const known = buildKnownIds(vaultPath, files);
290
- const entityIndex = options.entityIndex ?? buildEntityIndex(vaultPath);
291
- const backlinks = /* @__PURE__ */ new Map();
292
- const orphans = [];
293
- let linkCount = 0;
294
- for (const file of files) {
295
- const sourceId = toVaultId(vaultPath, file);
296
- const content = fs2.readFileSync(file, "utf-8");
297
- const matches = content.match(WIKI_LINK_REGEX) || [];
298
- linkCount += matches.length;
299
- for (const match of matches) {
300
- const target = normalizeLinkTarget(match);
301
- if (!target) continue;
302
- const resolved = resolveTarget(target, known, entityIndex);
303
- if (!resolved) {
304
- orphans.push({ source: sourceId, target });
305
- continue;
306
- }
307
- if (!backlinks.has(resolved)) {
308
- backlinks.set(resolved, /* @__PURE__ */ new Set());
309
- }
310
- backlinks.get(resolved).add(sourceId);
311
- }
312
- }
313
- const backlinksMap = /* @__PURE__ */ new Map();
314
- for (const [target, sources] of backlinks) {
315
- backlinksMap.set(target, [...sources].sort());
316
- }
317
- return { backlinks: backlinksMap, orphans, linkCount };
318
- }
319
- function writeBacklinksIndex(vaultPath, backlinks) {
320
- const dir = ensureClawvaultDir(vaultPath);
321
- const output = {};
322
- const targets = [...backlinks.keys()].sort();
323
- for (const target of targets) {
324
- const sources = backlinks.get(target) || [];
325
- output[target] = [...new Set(sources)].sort();
326
- }
327
- fs2.writeFileSync(path2.join(dir, BACKLINKS_FILE), JSON.stringify(output, null, 2));
328
- }
329
- function readBacklinksIndex(vaultPath) {
330
- const filePath = path2.join(vaultPath, CLAWVAULT_DIR, BACKLINKS_FILE);
331
- if (!fs2.existsSync(filePath)) return null;
332
- try {
333
- const raw = JSON.parse(fs2.readFileSync(filePath, "utf-8"));
334
- const map = /* @__PURE__ */ new Map();
335
- for (const [target, sources] of Object.entries(raw)) {
336
- if (Array.isArray(sources)) {
337
- map.set(target, sources);
338
- }
339
- }
340
- return map;
341
- } catch {
342
- return null;
343
- }
344
- }
345
- function rebuildBacklinksIndex(vaultPath, options = {}) {
346
- const result = scanVaultLinks(vaultPath, options);
347
- writeBacklinksIndex(vaultPath, result.backlinks);
348
- return result;
349
- }
350
-
351
- // src/lib/config.ts
352
- var fs3 = __toESM(require("fs"), 1);
353
- var path3 = __toESM(require("path"), 1);
354
- function findNearestVaultPath(startPath = process.cwd()) {
355
- let current = path3.resolve(startPath);
356
- while (true) {
357
- if (fs3.existsSync(path3.join(current, ".clawvault.json"))) {
358
- return current;
359
- }
360
- const parent = path3.dirname(current);
361
- if (parent === current) {
362
- return null;
363
- }
364
- current = parent;
365
- }
366
- }
367
- function resolveVaultPath(options = {}) {
368
- if (options.explicitPath) {
369
- return path3.resolve(options.explicitPath);
370
- }
371
- if (process.env.CLAWVAULT_PATH) {
372
- return path3.resolve(process.env.CLAWVAULT_PATH);
373
- }
374
- const discovered = findNearestVaultPath(options.cwd ?? process.cwd());
375
- if (discovered) {
376
- return discovered;
377
- }
378
- throw new Error("No vault path found. Set CLAWVAULT_PATH, use --vault, or run inside a vault.");
379
- }
380
-
381
- // src/lib/memory-graph.ts
382
- var fs4 = __toESM(require("fs"), 1);
383
- var path4 = __toESM(require("path"), 1);
384
- var import_gray_matter2 = __toESM(require("gray-matter"), 1);
385
- var import_glob = require("glob");
386
- var MEMORY_GRAPH_SCHEMA_VERSION = 1;
387
- var GRAPH_INDEX_RELATIVE_PATH = path4.join(".clawvault", "graph-index.json");
388
- var WIKI_LINK_RE = /\[\[([^\]]+)\]\]/g;
389
- var HASH_TAG_RE = /(^|\s)#([\w-]+)/g;
390
- var FRONTMATTER_RELATION_FIELDS = [
391
- "related",
392
- "depends_on",
393
- "dependsOn",
394
- "blocked_by",
395
- "blocks",
396
- "owner",
397
- "project",
398
- "people",
399
- "links"
400
- ];
401
- function normalizeRelativePath(value) {
402
- return value.split(path4.sep).join("/").replace(/^\.\//, "").replace(/^\/+/, "");
403
- }
404
- function toNoteKey(relativePath) {
405
- const normalized = normalizeRelativePath(relativePath);
406
- return normalized.toLowerCase().endsWith(".md") ? normalized.slice(0, -3) : normalized;
407
- }
408
- function toNoteNodeId(noteKey) {
409
- return `note:${noteKey}`;
410
- }
411
- function toTagNodeId(tag) {
412
- return `tag:${tag.toLowerCase()}`;
413
- }
414
- function normalizeUnresolvedKey(raw) {
415
- const normalized = raw.trim().toLowerCase().replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\/+/, "").replace(/\.md$/, "").replace(/[^a-z0-9/_-]+/g, "-").replace(/\/+/g, "/").replace(/-+/g, "-").replace(/^[-/]+|[-/]+$/g, "");
416
- return normalized || "unknown";
417
- }
418
- function toUnresolvedNodeId(raw) {
419
- return `unresolved:${normalizeUnresolvedKey(raw)}`;
420
- }
421
- function titleFromNoteKey(noteKey) {
422
- const basename2 = noteKey.split("/").pop() ?? noteKey;
423
- return basename2.replace(/[-_]+/g, " ").replace(/\s+/g, " ").trim().replace(/\b\w/g, (char) => char.toUpperCase());
424
- }
425
- function inferNodeType(relativePath, frontmatter) {
426
- const normalized = normalizeRelativePath(relativePath).toLowerCase();
427
- const category = normalized.split("/")[0] ?? "note";
428
- const explicitType = typeof frontmatter.type === "string" ? frontmatter.type.toLowerCase() : "";
429
- if (category.includes("daily") || explicitType === "daily") return "daily";
430
- if (category === "observations" || explicitType === "observation") return "observation";
431
- if (category === "handoffs" || explicitType === "handoff") return "handoff";
432
- if (category === "decisions" || explicitType === "decision") return "decision";
433
- if (category === "lessons" || explicitType === "lesson") return "lesson";
434
- if (category === "projects" || explicitType === "project") return "project";
435
- if (category === "people" || explicitType === "person") return "person";
436
- if (category === "commitments" || explicitType === "commitment") return "commitment";
437
- return "note";
438
- }
439
- function ensureClawvaultDir2(vaultPath) {
440
- const dirPath = path4.join(vaultPath, ".clawvault");
441
- if (!fs4.existsSync(dirPath)) {
442
- fs4.mkdirSync(dirPath, { recursive: true });
443
- }
444
- return dirPath;
445
- }
446
- function getGraphIndexPath(vaultPath) {
447
- return path4.join(vaultPath, GRAPH_INDEX_RELATIVE_PATH);
448
- }
449
- function normalizeWikiTarget(target) {
450
- let value = target.trim();
451
- if (!value) return "";
452
- const pipeIndex = value.indexOf("|");
453
- if (pipeIndex >= 0) {
454
- value = value.slice(0, pipeIndex);
455
- }
456
- const hashIndex = value.indexOf("#");
457
- if (hashIndex >= 0) {
458
- value = value.slice(0, hashIndex);
459
- }
460
- value = value.trim().replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\/+/, "");
461
- if (value.toLowerCase().endsWith(".md")) {
462
- value = value.slice(0, -3);
463
- }
464
- return value.trim();
465
- }
466
- function collectTags(frontmatter, markdownContent) {
467
- const tags = /* @__PURE__ */ new Set();
468
- const fmTags = frontmatter.tags;
469
- if (Array.isArray(fmTags)) {
470
- for (const tag of fmTags) {
471
- if (typeof tag === "string" && tag.trim()) tags.add(tag.trim().toLowerCase());
472
- }
473
- } else if (typeof fmTags === "string") {
474
- for (const token of fmTags.split(",")) {
475
- const normalized = token.trim().toLowerCase();
476
- if (normalized) tags.add(normalized);
477
- }
478
- }
479
- const markdownMatches = markdownContent.matchAll(HASH_TAG_RE);
480
- for (const match of markdownMatches) {
481
- const tag = match[2]?.trim().toLowerCase();
482
- if (tag) tags.add(tag);
483
- }
484
- return [...tags].sort((a, b) => a.localeCompare(b));
485
- }
486
- function extractWikiTargets(markdownContent) {
487
- const targets = /* @__PURE__ */ new Set();
488
- for (const match of markdownContent.matchAll(WIKI_LINK_RE)) {
489
- const candidate = match[1];
490
- if (!candidate) continue;
491
- const normalized = normalizeWikiTarget(candidate);
492
- if (normalized) targets.add(normalized);
493
- }
494
- return [...targets];
495
- }
496
- function toStringArray(value) {
497
- if (typeof value === "string") {
498
- return value.split(",").map((entry) => entry.trim()).filter(Boolean);
499
- }
500
- if (Array.isArray(value)) {
501
- return value.flatMap((entry) => typeof entry === "string" ? entry.split(",") : []).map((entry) => entry.trim()).filter(Boolean);
502
- }
503
- return [];
504
- }
505
- function extractFrontmatterRelations(frontmatter) {
506
- const relations = [];
507
- for (const field of FRONTMATTER_RELATION_FIELDS) {
508
- const raw = frontmatter[field];
509
- for (const value of toStringArray(raw)) {
510
- const normalized = normalizeWikiTarget(value);
511
- if (normalized) relations.push({ field, target: normalized });
512
- }
513
- }
514
- return relations;
515
- }
516
- function buildNoteRegistry(relativePaths) {
517
- const byLowerPath = /* @__PURE__ */ new Map();
518
- const byLowerBasename = /* @__PURE__ */ new Map();
519
- for (const relativePath of relativePaths) {
520
- const noteKey = toNoteKey(relativePath);
521
- const lowerKey = noteKey.toLowerCase();
522
- if (!byLowerPath.has(lowerKey)) {
523
- byLowerPath.set(lowerKey, noteKey);
524
- }
525
- const base = noteKey.split("/").pop() ?? noteKey;
526
- const lowerBase = base.toLowerCase();
527
- const existing = byLowerBasename.get(lowerBase) ?? [];
528
- existing.push(noteKey);
529
- byLowerBasename.set(lowerBase, existing);
530
- }
531
- return { byLowerPath, byLowerBasename };
532
- }
533
- function resolveTargetNodeId(rawTarget, registry) {
534
- const normalized = normalizeWikiTarget(rawTarget);
535
- if (!normalized) {
536
- return toUnresolvedNodeId(rawTarget);
537
- }
538
- const lowerTarget = normalized.toLowerCase();
539
- const direct = registry.byLowerPath.get(lowerTarget);
540
- if (direct) {
541
- return toNoteNodeId(direct);
542
- }
543
- if (!normalized.includes("/")) {
544
- const basenameMatches = registry.byLowerBasename.get(lowerTarget) ?? [];
545
- if (basenameMatches.length === 1) {
546
- return toNoteNodeId(basenameMatches[0]);
547
- }
548
- }
549
- return toUnresolvedNodeId(normalized);
550
- }
551
- function createEdgeId(type, source, target, label) {
552
- const suffix = label ? `:${label}` : "";
553
- return `${type}:${source}->${target}${suffix}`;
554
- }
555
- function buildFragmentNode(id, title, type, category, pathValue, tags, missing, modifiedAt) {
556
- return {
557
- id,
558
- title,
559
- type,
560
- category,
561
- path: pathValue,
562
- tags,
563
- missing,
564
- degree: 0,
565
- modifiedAt
566
- };
567
- }
568
- function parseFileFragment(vaultPath, relativePath, mtimeMs, registry) {
569
- const absolutePath = path4.join(vaultPath, relativePath);
570
- const raw = fs4.readFileSync(absolutePath, "utf-8");
571
- const parsed = (0, import_gray_matter2.default)(raw);
572
- const frontmatter = parsed.data ?? {};
573
- const noteKey = toNoteKey(relativePath);
574
- const noteNodeId = toNoteNodeId(noteKey);
575
- const noteType = inferNodeType(relativePath, frontmatter);
576
- const tags = collectTags(frontmatter, parsed.content);
577
- const modifiedAt = new Date(mtimeMs).toISOString();
578
- const nodes = /* @__PURE__ */ new Map();
579
- const edges = /* @__PURE__ */ new Map();
580
- nodes.set(
581
- noteNodeId,
582
- buildFragmentNode(
583
- noteNodeId,
584
- typeof frontmatter.title === "string" && frontmatter.title.trim() ? frontmatter.title.trim() : titleFromNoteKey(noteKey),
585
- noteType,
586
- noteType,
587
- normalizeRelativePath(relativePath),
588
- tags,
589
- false,
590
- modifiedAt
591
- )
592
- );
593
- for (const tag of tags) {
594
- const tagNodeId = toTagNodeId(tag);
595
- if (!nodes.has(tagNodeId)) {
596
- nodes.set(tagNodeId, buildFragmentNode(tagNodeId, `#${tag}`, "tag", "tag", null, [], false, null));
597
- }
598
- const edgeId = createEdgeId("tag", noteNodeId, tagNodeId);
599
- edges.set(edgeId, {
600
- id: edgeId,
601
- source: noteNodeId,
602
- target: tagNodeId,
603
- type: "tag"
604
- });
605
- }
606
- const wikiTargets = extractWikiTargets(parsed.content);
607
- for (const target of wikiTargets) {
608
- const targetNodeId = resolveTargetNodeId(target, registry);
609
- if (targetNodeId.startsWith("unresolved:") && !nodes.has(targetNodeId)) {
610
- nodes.set(
611
- targetNodeId,
612
- buildFragmentNode(targetNodeId, titleFromNoteKey(normalizeUnresolvedKey(target)), "unresolved", "unresolved", null, [], true, null)
613
- );
614
- }
615
- const edgeId = createEdgeId("wiki_link", noteNodeId, targetNodeId);
616
- edges.set(edgeId, {
617
- id: edgeId,
618
- source: noteNodeId,
619
- target: targetNodeId,
620
- type: "wiki_link"
621
- });
622
- }
623
- for (const relation of extractFrontmatterRelations(frontmatter)) {
624
- const targetNodeId = resolveTargetNodeId(relation.target, registry);
625
- if (targetNodeId.startsWith("unresolved:") && !nodes.has(targetNodeId)) {
626
- nodes.set(
627
- targetNodeId,
628
- buildFragmentNode(
629
- targetNodeId,
630
- titleFromNoteKey(normalizeUnresolvedKey(relation.target)),
631
- "unresolved",
632
- "unresolved",
633
- null,
634
- [],
635
- true,
636
- null
637
- )
638
- );
639
- }
640
- const edgeId = createEdgeId("frontmatter_relation", noteNodeId, targetNodeId, relation.field);
641
- edges.set(edgeId, {
642
- id: edgeId,
643
- source: noteNodeId,
644
- target: targetNodeId,
645
- type: "frontmatter_relation",
646
- label: relation.field
647
- });
648
- }
649
- return {
650
- relativePath: normalizeRelativePath(relativePath),
651
- mtimeMs,
652
- nodes: [...nodes.values()],
653
- edges: [...edges.values()]
654
- };
655
- }
656
- function combineFragments(fragments, generatedAt) {
657
- const nodes = /* @__PURE__ */ new Map();
658
- const edges = /* @__PURE__ */ new Map();
659
- for (const fragment of Object.values(fragments)) {
660
- for (const node of fragment.nodes) {
661
- const existing = nodes.get(node.id);
662
- if (!existing) {
663
- nodes.set(node.id, { ...node, degree: 0 });
664
- } else if (node.modifiedAt && (!existing.modifiedAt || node.modifiedAt > existing.modifiedAt)) {
665
- nodes.set(node.id, { ...existing, ...node, degree: 0 });
666
- }
667
- }
668
- for (const edge of fragment.edges) {
669
- edges.set(edge.id, edge);
670
- }
671
- }
672
- const degreeByNode = /* @__PURE__ */ new Map();
673
- for (const edge of edges.values()) {
674
- degreeByNode.set(edge.source, (degreeByNode.get(edge.source) ?? 0) + 1);
675
- degreeByNode.set(edge.target, (degreeByNode.get(edge.target) ?? 0) + 1);
676
- }
677
- for (const node of nodes.values()) {
678
- node.degree = degreeByNode.get(node.id) ?? 0;
679
- }
680
- const nodeTypeCounts = {};
681
- for (const node of nodes.values()) {
682
- nodeTypeCounts[node.type] = (nodeTypeCounts[node.type] ?? 0) + 1;
683
- }
684
- const edgeTypeCounts = {};
685
- for (const edge of edges.values()) {
686
- edgeTypeCounts[edge.type] = (edgeTypeCounts[edge.type] ?? 0) + 1;
687
- }
688
- const sortedNodes = [...nodes.values()].sort((a, b) => a.id.localeCompare(b.id));
689
- const sortedEdges = [...edges.values()].sort((a, b) => a.id.localeCompare(b.id));
690
- return {
691
- schemaVersion: MEMORY_GRAPH_SCHEMA_VERSION,
692
- nodes: sortedNodes,
693
- edges: sortedEdges,
694
- stats: {
695
- generatedAt,
696
- nodeCount: sortedNodes.length,
697
- edgeCount: sortedEdges.length,
698
- nodeTypeCounts,
699
- edgeTypeCounts
700
- }
701
- };
702
- }
703
- function isValidIndex(index) {
704
- if (!index || typeof index !== "object") return false;
705
- const typed = index;
706
- return typed.schemaVersion === MEMORY_GRAPH_SCHEMA_VERSION && typeof typed.vaultPath === "string" && typeof typed.generatedAt === "string" && Boolean(typed.files && typeof typed.files === "object") && Boolean(typed.graph && typeof typed.graph === "object");
707
- }
708
- function loadMemoryGraphIndex(vaultPath) {
709
- const indexPath = getGraphIndexPath(path4.resolve(vaultPath));
710
- if (!fs4.existsSync(indexPath)) {
711
- return null;
712
- }
713
- try {
714
- const parsed = JSON.parse(fs4.readFileSync(indexPath, "utf-8"));
715
- if (!isValidIndex(parsed)) {
716
- return null;
717
- }
718
- return parsed;
719
- } catch {
720
- return null;
721
- }
722
- }
723
- async function buildOrUpdateMemoryGraphIndex(vaultPathInput, options = {}) {
724
- const vaultPath = path4.resolve(vaultPathInput);
725
- ensureClawvaultDir2(vaultPath);
726
- const existing = options.forceFull ? null : loadMemoryGraphIndex(vaultPath);
727
- const markdownFiles = await (0, import_glob.glob)("**/*.md", {
728
- cwd: vaultPath,
729
- ignore: ["**/node_modules/**", "**/.git/**", "**/.obsidian/**", "**/.trash/**", "**/ledger/archive/**"]
730
- });
731
- const normalizedFiles = markdownFiles.map(normalizeRelativePath).sort((a, b) => a.localeCompare(b));
732
- const registry = buildNoteRegistry(normalizedFiles);
733
- const nextFragments = {};
734
- const existingFragments = existing?.files ?? {};
735
- const currentFileSet = new Set(normalizedFiles);
736
- for (const relativePath of normalizedFiles) {
737
- const absolutePath = path4.join(vaultPath, relativePath);
738
- const stat = fs4.statSync(absolutePath);
739
- const existingFragment = existingFragments[relativePath];
740
- if (!options.forceFull && existingFragment && existingFragment.mtimeMs === stat.mtimeMs) {
741
- nextFragments[relativePath] = existingFragment;
742
- continue;
743
- }
744
- nextFragments[relativePath] = parseFileFragment(vaultPath, relativePath, stat.mtimeMs, registry);
745
- }
746
- for (const [relativePath, fragment] of Object.entries(existingFragments)) {
747
- if (!currentFileSet.has(relativePath)) {
748
- continue;
749
- }
750
- if (!nextFragments[relativePath]) {
751
- nextFragments[relativePath] = fragment;
752
- }
753
- }
754
- const generatedAt = (/* @__PURE__ */ new Date()).toISOString();
755
- const graph = combineFragments(nextFragments, generatedAt);
756
- const nextIndex = {
757
- schemaVersion: MEMORY_GRAPH_SCHEMA_VERSION,
758
- vaultPath,
759
- generatedAt,
760
- files: nextFragments,
761
- graph
762
- };
763
- fs4.writeFileSync(getGraphIndexPath(vaultPath), JSON.stringify(nextIndex, null, 2));
764
- return nextIndex;
765
- }
766
-
767
- // src/commands/link.ts
768
- async function linkCommand(file, options) {
769
- const vaultPath = resolveVaultPath({ explicitPath: options.vaultPath });
770
- const index = buildEntityIndex(vaultPath);
771
- const suggestionIndex = filterIndex(index, /* @__PURE__ */ new Set(["people", "projects", "decisions"]));
772
- const modeCount = [options.backlinks ? 1 : 0, options.orphans ? 1 : 0, options.rebuild ? 1 : 0].reduce((sum, value) => sum + value, 0);
773
- if (modeCount > 1) {
774
- console.error("Error: Use only one of --backlinks, --orphans, or --rebuild");
775
- process.exit(1);
776
- }
777
- if (options.rebuild) {
778
- const result = rebuildBacklinksIndex(vaultPath, { entityIndex: index });
779
- const orphanSuffix = result.orphans.length > 0 ? `, ${result.orphans.length} orphan(s)` : "";
780
- console.log(`\u2713 Rebuilt backlinks (${result.backlinks.size} targets, ${result.linkCount} links${orphanSuffix})`);
781
- return;
782
- }
783
- if (options.backlinks) {
784
- if (file) {
785
- console.error("Error: Use --backlinks without a file argument");
786
- process.exit(1);
787
- }
788
- const target = resolveBacklinkTarget(options.backlinks, vaultPath, index);
789
- if (!target) {
790
- console.error("Error: Invalid target for --backlinks");
791
- process.exit(1);
792
- }
793
- const backlinks = readBacklinksIndex(vaultPath) ?? rebuildBacklinksIndex(vaultPath, { entityIndex: index }).backlinks;
794
- const sources = backlinks.get(target) || [];
795
- if (sources.length === 0) {
796
- console.log(`No backlinks found for ${target}`);
797
- return;
798
- }
799
- console.log(`Backlinks \u2192 ${target}`);
800
- for (const source of sources.sort()) {
801
- console.log(` - ${source}`);
802
- }
803
- return;
804
- }
805
- if (options.orphans) {
806
- if (file || options.all) {
807
- console.error("Error: --orphans does not accept a file or --all");
808
- process.exit(1);
809
- }
810
- const result = scanVaultLinks(vaultPath, { entityIndex: index });
811
- if (result.orphans.length === 0) {
812
- console.log("\u2713 No orphan links found");
813
- return;
814
- }
815
- const orphans = result.orphans.slice().sort((a, b) => a.target.localeCompare(b.target) || a.source.localeCompare(b.source));
816
- console.log(`\u26A0 ${orphans.length} orphan link(s) found`);
817
- for (const orphan of orphans) {
818
- console.log(` - ${orphan.source} \u2192 [[${orphan.target}]]`);
819
- }
820
- return;
821
- }
822
- if (options.all) {
823
- await linkAllFiles(vaultPath, index, suggestionIndex, options.dryRun);
824
- if (!options.dryRun) {
825
- await buildOrUpdateMemoryGraphIndex(vaultPath);
826
- }
827
- return;
828
- }
829
- if (!file) {
830
- console.error("Error: Specify a file or use --all");
831
- process.exit(1);
832
- }
833
- const filePath = path5.isAbsolute(file) ? file : path5.join(process.cwd(), file);
834
- if (!fs5.existsSync(filePath)) {
835
- console.error(`Error: File not found: ${filePath}`);
836
- process.exit(1);
837
- }
838
- const linked = await linkFile(filePath, index, suggestionIndex, options.dryRun);
839
- if (!options.dryRun && linked > 0) {
840
- await buildOrUpdateMemoryGraphIndex(vaultPath);
841
- }
842
- }
843
- function filterIndex(index, categories) {
844
- const entries = /* @__PURE__ */ new Map();
845
- const byPath = /* @__PURE__ */ new Map();
846
- for (const [alias, targetPath] of index.entries) {
847
- const category = targetPath.split("/")[0];
848
- if (categories.has(category)) {
849
- entries.set(alias, targetPath);
850
- }
851
- }
852
- for (const [targetPath, entry] of index.byPath) {
853
- const category = targetPath.split("/")[0];
854
- if (categories.has(category)) {
855
- byPath.set(targetPath, entry);
856
- }
857
- }
858
- return { entries, byPath };
859
- }
860
- function resolveBacklinkTarget(input, vaultPath, index) {
861
- let target = input.trim();
862
- if (!target) return null;
863
- if (target.startsWith("[[") && target.endsWith("]]")) {
864
- target = target.slice(2, -2);
865
- }
866
- const pipeIndex = target.indexOf("|");
867
- if (pipeIndex !== -1) {
868
- target = target.slice(0, pipeIndex);
869
- }
870
- const hashIndex = target.indexOf("#");
871
- if (hashIndex !== -1) {
872
- target = target.slice(0, hashIndex);
873
- }
874
- target = target.trim();
875
- if (!target) return null;
876
- if (target.endsWith(".md")) {
877
- target = target.slice(0, -3);
878
- }
879
- if (target.startsWith("/")) {
880
- target = target.slice(1);
881
- }
882
- const candidate = path5.isAbsolute(target) ? target : path5.join(vaultPath, target);
883
- const withExtension = candidate.endsWith(".md") ? candidate : `${candidate}.md`;
884
- if (fs5.existsSync(candidate) && candidate.endsWith(".md")) {
885
- return toVaultId2(vaultPath, candidate);
886
- }
887
- if (fs5.existsSync(withExtension)) {
888
- return toVaultId2(vaultPath, withExtension);
889
- }
890
- const aliasKey = target.toLowerCase();
891
- if (index.entries.has(aliasKey)) {
892
- return index.entries.get(aliasKey);
893
- }
894
- return target.replace(/\\/g, "/");
895
- }
896
- function toVaultId2(vaultPath, filePath) {
897
- const relative3 = path5.relative(vaultPath, filePath).replace(/\.md$/, "");
898
- return relative3.split(path5.sep).join("/");
899
- }
900
- function logSuggestions(filePath, suggestions) {
901
- if (suggestions.length === 0) return;
902
- console.log(`
903
- \u{1F4A1} Suggested links in ${path5.basename(filePath)}`);
904
- for (const suggestion of suggestions) {
905
- console.log(` Line ${suggestion.line}: "${suggestion.alias}" \u2192 [[${suggestion.path}]]`);
906
- }
907
- }
908
- async function linkFile(filePath, index, suggestionIndex, dryRun) {
909
- const content = fs5.readFileSync(filePath, "utf-8");
910
- const linkedContent = autoLink(content, index);
911
- const suggestions = findUnlinkedMentions(linkedContent, suggestionIndex);
912
- if (dryRun) {
913
- const matches = dryRunLink(content, index);
914
- if (matches.length > 0) {
915
- console.log(`
916
- \u{1F4C4} ${filePath}`);
917
- for (const m of matches) {
918
- console.log(` Line ${m.line}: "${m.alias}" \u2192 [[${m.path}]]`);
919
- }
920
- }
921
- logSuggestions(filePath, suggestions);
922
- return matches.length;
923
- }
924
- if (linkedContent !== content) {
925
- fs5.writeFileSync(filePath, linkedContent);
926
- const matches = dryRunLink(content, index);
927
- console.log(`\u2713 Linked ${matches.length} entities in ${path5.basename(filePath)}`);
928
- logSuggestions(filePath, suggestions);
929
- return matches.length;
930
- }
931
- logSuggestions(filePath, suggestions);
932
- return 0;
933
- }
934
- async function linkAllFiles(vaultPath, index, suggestionIndex, dryRun) {
935
- const files = [];
936
- function walk(dir) {
937
- const entries = fs5.readdirSync(dir, { withFileTypes: true });
938
- for (const entry of entries) {
939
- const fullPath = path5.join(dir, entry.name);
940
- if (entry.isDirectory()) {
941
- if (!entry.name.startsWith(".") && entry.name !== "archive" && entry.name !== "templates") {
942
- walk(fullPath);
943
- }
944
- } else if (entry.name.endsWith(".md")) {
945
- files.push(fullPath);
946
- }
947
- }
948
- }
949
- walk(vaultPath);
950
- let totalLinks = 0;
951
- let filesModified = 0;
952
- for (const file of files) {
953
- const links = await linkFile(file, index, suggestionIndex, dryRun);
954
- if (links > 0) {
955
- totalLinks += links;
956
- filesModified++;
957
- }
958
- }
959
- console.log(`
960
- ${dryRun ? "(dry run) " : ""}${totalLinks} links in ${filesModified} files`);
961
- }
962
- // Annotate the CommonJS export names for ESM import in node:
963
- 0 && (module.exports = {
964
- linkCommand
965
- });