clawvault 1.11.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +135 -1
- package/bin/clawvault.js +51 -1252
- package/bin/command-registration.test.js +148 -0
- package/bin/command-runtime.js +42 -0
- package/bin/command-runtime.test.js +102 -0
- package/bin/help-contract.test.js +23 -0
- package/bin/register-core-commands.js +139 -0
- package/bin/register-maintenance-commands.js +137 -0
- package/bin/register-query-commands.js +225 -0
- package/bin/register-resilience-commands.js +147 -0
- package/bin/register-session-lifecycle-commands.js +204 -0
- package/bin/register-template-commands.js +72 -0
- package/bin/register-vault-operations-commands.js +295 -0
- package/bin/test-helpers/cli-command-fixtures.js +94 -0
- package/dashboard/lib/graph-diff.js +3 -1
- package/dashboard/lib/graph-diff.test.js +19 -0
- package/dashboard/lib/vault-parser.js +330 -26
- package/dashboard/lib/vault-parser.test.js +191 -11
- package/dashboard/public/app.js +22 -9
- package/dist/chunk-MXSSG3QU.js +42 -0
- package/dist/chunk-O5V7SD5C.js +398 -0
- package/dist/chunk-PAYUH64O.js +284 -0
- package/dist/{chunk-3HFB7EMU.js → chunk-QFBKWDYR.js} +12 -0
- package/dist/{chunk-UBRYOIII.js → chunk-TBVI4N53.js} +210 -21
- package/dist/chunk-TXO34J3O.js +56 -0
- package/dist/commands/compat.d.ts +28 -0
- package/dist/commands/compat.js +10 -0
- package/dist/commands/context.d.ts +2 -33
- package/dist/commands/context.js +3 -2
- package/dist/commands/doctor.js +61 -3
- package/dist/commands/entities.d.ts +1 -0
- package/dist/commands/entities.js +4 -4
- package/dist/commands/graph.d.ts +21 -0
- package/dist/commands/graph.js +10 -0
- package/dist/commands/link.d.ts +1 -0
- package/dist/commands/link.js +14 -5
- package/dist/commands/sleep.js +7 -6
- package/dist/commands/status.d.ts +6 -0
- package/dist/commands/status.js +63 -3
- package/dist/commands/wake.js +5 -4
- package/dist/context-COo8oq1k.d.ts +45 -0
- package/dist/index.d.ts +63 -2
- package/dist/index.js +53 -15
- package/dist/lib/config.d.ts +6 -1
- package/dist/lib/config.js +7 -3
- package/hooks/clawvault/HOOK.md +6 -1
- package/hooks/clawvault/handler.js +44 -3
- package/hooks/clawvault/handler.test.js +161 -0
- package/package.json +34 -2
- package/dashboard/public/graph.js +0 -376
- package/dashboard/public/style.css +0 -154
- package/dist/chunk-4KDZZW4X.js +0 -13
package/dashboard/public/app.js
CHANGED
|
@@ -215,10 +215,15 @@ function applySnapshot(payload, { shouldLazyLoad, shouldFit }) {
|
|
|
215
215
|
for (const edge of nextEdges) {
|
|
216
216
|
const sourceId = String(edge.source);
|
|
217
217
|
const targetId = String(edge.target);
|
|
218
|
-
|
|
219
|
-
|
|
218
|
+
const edgeType = String(edge.type ?? '');
|
|
219
|
+
const edgeLabel = String(edge.label ?? '');
|
|
220
|
+
const edgeKey = toEdgeKey(sourceId, targetId, edgeType, edgeLabel);
|
|
221
|
+
state.allEdgeByKey.set(edgeKey, {
|
|
222
|
+
key: edgeKey,
|
|
220
223
|
source: sourceId,
|
|
221
|
-
target: targetId
|
|
224
|
+
target: targetId,
|
|
225
|
+
type: edgeType,
|
|
226
|
+
label: edgeLabel
|
|
222
227
|
});
|
|
223
228
|
}
|
|
224
229
|
|
|
@@ -271,14 +276,18 @@ function applyPatch(payload) {
|
|
|
271
276
|
for (const edge of removedEdges) {
|
|
272
277
|
const sourceId = String(edge.source);
|
|
273
278
|
const targetId = String(edge.target);
|
|
274
|
-
|
|
279
|
+
const edgeType = String(edge.type ?? '');
|
|
280
|
+
const edgeLabel = String(edge.label ?? '');
|
|
281
|
+
state.allEdgeByKey.delete(toEdgeKey(sourceId, targetId, edgeType, edgeLabel));
|
|
275
282
|
}
|
|
276
283
|
|
|
277
284
|
for (const edge of addedEdges) {
|
|
278
285
|
const sourceId = String(edge.source);
|
|
279
286
|
const targetId = String(edge.target);
|
|
280
|
-
const
|
|
281
|
-
|
|
287
|
+
const edgeType = String(edge.type ?? '');
|
|
288
|
+
const edgeLabel = String(edge.label ?? '');
|
|
289
|
+
const key = toEdgeKey(sourceId, targetId, edgeType, edgeLabel);
|
|
290
|
+
state.allEdgeByKey.set(key, { key, source: sourceId, target: targetId, type: edgeType, label: edgeLabel });
|
|
282
291
|
}
|
|
283
292
|
|
|
284
293
|
if (payload?.stats) {
|
|
@@ -333,6 +342,8 @@ function applyFiltersAndRender({ shouldLazyLoad }) {
|
|
|
333
342
|
filteredLinks.push({
|
|
334
343
|
source: edge.source,
|
|
335
344
|
target: edge.target,
|
|
345
|
+
type: edge.type,
|
|
346
|
+
label: edge.label,
|
|
336
347
|
_key: edge.key
|
|
337
348
|
});
|
|
338
349
|
}
|
|
@@ -585,7 +596,9 @@ function linkKey(link) {
|
|
|
585
596
|
}
|
|
586
597
|
const sourceId = typeof link.source === 'object' ? link.source.id : String(link.source);
|
|
587
598
|
const targetId = typeof link.target === 'object' ? link.target.id : String(link.target);
|
|
588
|
-
|
|
599
|
+
const edgeType = String(link.type ?? '');
|
|
600
|
+
const edgeLabel = String(link.label ?? '');
|
|
601
|
+
return toEdgeKey(sourceId, targetId, edgeType, edgeLabel);
|
|
589
602
|
}
|
|
590
603
|
|
|
591
604
|
function colorForCategory(category) {
|
|
@@ -769,8 +782,8 @@ function areSetsEqual(left, right) {
|
|
|
769
782
|
return true;
|
|
770
783
|
}
|
|
771
784
|
|
|
772
|
-
function toEdgeKey(sourceId, targetId) {
|
|
773
|
-
return `${sourceId}=>${targetId}`;
|
|
785
|
+
function toEdgeKey(sourceId, targetId, edgeType = '', edgeLabel = '') {
|
|
786
|
+
return `${sourceId}=>${targetId}:${edgeType}:${edgeLabel}`;
|
|
774
787
|
}
|
|
775
788
|
|
|
776
789
|
function escapeHtml(value) {
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// src/lib/config.ts
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
function getVaultPath() {
|
|
5
|
+
const vaultPath = process.env.CLAWVAULT_PATH;
|
|
6
|
+
if (!vaultPath) {
|
|
7
|
+
throw new Error("CLAWVAULT_PATH environment variable not set");
|
|
8
|
+
}
|
|
9
|
+
return path.resolve(vaultPath);
|
|
10
|
+
}
|
|
11
|
+
function findNearestVaultPath(startPath = process.cwd()) {
|
|
12
|
+
let current = path.resolve(startPath);
|
|
13
|
+
while (true) {
|
|
14
|
+
if (fs.existsSync(path.join(current, ".clawvault.json"))) {
|
|
15
|
+
return current;
|
|
16
|
+
}
|
|
17
|
+
const parent = path.dirname(current);
|
|
18
|
+
if (parent === current) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
current = parent;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function resolveVaultPath(options = {}) {
|
|
25
|
+
if (options.explicitPath) {
|
|
26
|
+
return path.resolve(options.explicitPath);
|
|
27
|
+
}
|
|
28
|
+
if (process.env.CLAWVAULT_PATH) {
|
|
29
|
+
return path.resolve(process.env.CLAWVAULT_PATH);
|
|
30
|
+
}
|
|
31
|
+
const discovered = findNearestVaultPath(options.cwd ?? process.cwd());
|
|
32
|
+
if (discovered) {
|
|
33
|
+
return discovered;
|
|
34
|
+
}
|
|
35
|
+
throw new Error("No vault path found. Set CLAWVAULT_PATH, use --vault, or run inside a vault.");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export {
|
|
39
|
+
getVaultPath,
|
|
40
|
+
findNearestVaultPath,
|
|
41
|
+
resolveVaultPath
|
|
42
|
+
};
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
// src/lib/memory-graph.ts
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import matter from "gray-matter";
|
|
5
|
+
import { glob } from "glob";
|
|
6
|
+
var MEMORY_GRAPH_SCHEMA_VERSION = 1;
|
|
7
|
+
var GRAPH_INDEX_RELATIVE_PATH = path.join(".clawvault", "graph-index.json");
|
|
8
|
+
var WIKI_LINK_RE = /\[\[([^\]]+)\]\]/g;
|
|
9
|
+
var HASH_TAG_RE = /(^|\s)#([\w-]+)/g;
|
|
10
|
+
var FRONTMATTER_RELATION_FIELDS = [
|
|
11
|
+
"related",
|
|
12
|
+
"depends_on",
|
|
13
|
+
"dependsOn",
|
|
14
|
+
"blocked_by",
|
|
15
|
+
"blocks",
|
|
16
|
+
"owner",
|
|
17
|
+
"project",
|
|
18
|
+
"people",
|
|
19
|
+
"links"
|
|
20
|
+
];
|
|
21
|
+
function normalizeRelativePath(value) {
|
|
22
|
+
return value.split(path.sep).join("/").replace(/^\.\//, "").replace(/^\/+/, "");
|
|
23
|
+
}
|
|
24
|
+
function toNoteKey(relativePath) {
|
|
25
|
+
const normalized = normalizeRelativePath(relativePath);
|
|
26
|
+
return normalized.toLowerCase().endsWith(".md") ? normalized.slice(0, -3) : normalized;
|
|
27
|
+
}
|
|
28
|
+
function toNoteNodeId(noteKey) {
|
|
29
|
+
return `note:${noteKey}`;
|
|
30
|
+
}
|
|
31
|
+
function toTagNodeId(tag) {
|
|
32
|
+
return `tag:${tag.toLowerCase()}`;
|
|
33
|
+
}
|
|
34
|
+
function normalizeUnresolvedKey(raw) {
|
|
35
|
+
const normalized = raw.trim().toLowerCase().replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\/+/, "").replace(/\.md$/, "").replace(/[^a-z0-9/_-]+/g, "-").replace(/\/+/g, "/").replace(/-+/g, "-").replace(/^[-/]+|[-/]+$/g, "");
|
|
36
|
+
return normalized || "unknown";
|
|
37
|
+
}
|
|
38
|
+
function toUnresolvedNodeId(raw) {
|
|
39
|
+
return `unresolved:${normalizeUnresolvedKey(raw)}`;
|
|
40
|
+
}
|
|
41
|
+
function titleFromNoteKey(noteKey) {
|
|
42
|
+
const basename = noteKey.split("/").pop() ?? noteKey;
|
|
43
|
+
return basename.replace(/[-_]+/g, " ").replace(/\s+/g, " ").trim().replace(/\b\w/g, (char) => char.toUpperCase());
|
|
44
|
+
}
|
|
45
|
+
function inferNodeType(relativePath, frontmatter) {
|
|
46
|
+
const normalized = normalizeRelativePath(relativePath).toLowerCase();
|
|
47
|
+
const category = normalized.split("/")[0] ?? "note";
|
|
48
|
+
const explicitType = typeof frontmatter.type === "string" ? frontmatter.type.toLowerCase() : "";
|
|
49
|
+
if (category.includes("daily") || explicitType === "daily") return "daily";
|
|
50
|
+
if (category === "observations" || explicitType === "observation") return "observation";
|
|
51
|
+
if (category === "handoffs" || explicitType === "handoff") return "handoff";
|
|
52
|
+
if (category === "decisions" || explicitType === "decision") return "decision";
|
|
53
|
+
if (category === "lessons" || explicitType === "lesson") return "lesson";
|
|
54
|
+
if (category === "projects" || explicitType === "project") return "project";
|
|
55
|
+
if (category === "people" || explicitType === "person") return "person";
|
|
56
|
+
if (category === "commitments" || explicitType === "commitment") return "commitment";
|
|
57
|
+
return "note";
|
|
58
|
+
}
|
|
59
|
+
function ensureClawvaultDir(vaultPath) {
|
|
60
|
+
const dirPath = path.join(vaultPath, ".clawvault");
|
|
61
|
+
if (!fs.existsSync(dirPath)) {
|
|
62
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
63
|
+
}
|
|
64
|
+
return dirPath;
|
|
65
|
+
}
|
|
66
|
+
function getGraphIndexPath(vaultPath) {
|
|
67
|
+
return path.join(vaultPath, GRAPH_INDEX_RELATIVE_PATH);
|
|
68
|
+
}
|
|
69
|
+
function normalizeWikiTarget(target) {
|
|
70
|
+
let value = target.trim();
|
|
71
|
+
if (!value) return "";
|
|
72
|
+
const pipeIndex = value.indexOf("|");
|
|
73
|
+
if (pipeIndex >= 0) {
|
|
74
|
+
value = value.slice(0, pipeIndex);
|
|
75
|
+
}
|
|
76
|
+
const hashIndex = value.indexOf("#");
|
|
77
|
+
if (hashIndex >= 0) {
|
|
78
|
+
value = value.slice(0, hashIndex);
|
|
79
|
+
}
|
|
80
|
+
value = value.trim().replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\/+/, "");
|
|
81
|
+
if (value.toLowerCase().endsWith(".md")) {
|
|
82
|
+
value = value.slice(0, -3);
|
|
83
|
+
}
|
|
84
|
+
return value.trim();
|
|
85
|
+
}
|
|
86
|
+
function collectTags(frontmatter, markdownContent) {
|
|
87
|
+
const tags = /* @__PURE__ */ new Set();
|
|
88
|
+
const fmTags = frontmatter.tags;
|
|
89
|
+
if (Array.isArray(fmTags)) {
|
|
90
|
+
for (const tag of fmTags) {
|
|
91
|
+
if (typeof tag === "string" && tag.trim()) tags.add(tag.trim().toLowerCase());
|
|
92
|
+
}
|
|
93
|
+
} else if (typeof fmTags === "string") {
|
|
94
|
+
for (const token of fmTags.split(",")) {
|
|
95
|
+
const normalized = token.trim().toLowerCase();
|
|
96
|
+
if (normalized) tags.add(normalized);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const markdownMatches = markdownContent.matchAll(HASH_TAG_RE);
|
|
100
|
+
for (const match of markdownMatches) {
|
|
101
|
+
const tag = match[2]?.trim().toLowerCase();
|
|
102
|
+
if (tag) tags.add(tag);
|
|
103
|
+
}
|
|
104
|
+
return [...tags].sort((a, b) => a.localeCompare(b));
|
|
105
|
+
}
|
|
106
|
+
function extractWikiTargets(markdownContent) {
|
|
107
|
+
const targets = /* @__PURE__ */ new Set();
|
|
108
|
+
for (const match of markdownContent.matchAll(WIKI_LINK_RE)) {
|
|
109
|
+
const candidate = match[1];
|
|
110
|
+
if (!candidate) continue;
|
|
111
|
+
const normalized = normalizeWikiTarget(candidate);
|
|
112
|
+
if (normalized) targets.add(normalized);
|
|
113
|
+
}
|
|
114
|
+
return [...targets];
|
|
115
|
+
}
|
|
116
|
+
function toStringArray(value) {
|
|
117
|
+
if (typeof value === "string") {
|
|
118
|
+
return value.split(",").map((entry) => entry.trim()).filter(Boolean);
|
|
119
|
+
}
|
|
120
|
+
if (Array.isArray(value)) {
|
|
121
|
+
return value.flatMap((entry) => typeof entry === "string" ? entry.split(",") : []).map((entry) => entry.trim()).filter(Boolean);
|
|
122
|
+
}
|
|
123
|
+
return [];
|
|
124
|
+
}
|
|
125
|
+
function extractFrontmatterRelations(frontmatter) {
|
|
126
|
+
const relations = [];
|
|
127
|
+
for (const field of FRONTMATTER_RELATION_FIELDS) {
|
|
128
|
+
const raw = frontmatter[field];
|
|
129
|
+
for (const value of toStringArray(raw)) {
|
|
130
|
+
const normalized = normalizeWikiTarget(value);
|
|
131
|
+
if (normalized) relations.push({ field, target: normalized });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return relations;
|
|
135
|
+
}
|
|
136
|
+
function buildNoteRegistry(relativePaths) {
|
|
137
|
+
const byLowerPath = /* @__PURE__ */ new Map();
|
|
138
|
+
const byLowerBasename = /* @__PURE__ */ new Map();
|
|
139
|
+
for (const relativePath of relativePaths) {
|
|
140
|
+
const noteKey = toNoteKey(relativePath);
|
|
141
|
+
const lowerKey = noteKey.toLowerCase();
|
|
142
|
+
if (!byLowerPath.has(lowerKey)) {
|
|
143
|
+
byLowerPath.set(lowerKey, noteKey);
|
|
144
|
+
}
|
|
145
|
+
const base = noteKey.split("/").pop() ?? noteKey;
|
|
146
|
+
const lowerBase = base.toLowerCase();
|
|
147
|
+
const existing = byLowerBasename.get(lowerBase) ?? [];
|
|
148
|
+
existing.push(noteKey);
|
|
149
|
+
byLowerBasename.set(lowerBase, existing);
|
|
150
|
+
}
|
|
151
|
+
return { byLowerPath, byLowerBasename };
|
|
152
|
+
}
|
|
153
|
+
function resolveTargetNodeId(rawTarget, registry) {
|
|
154
|
+
const normalized = normalizeWikiTarget(rawTarget);
|
|
155
|
+
if (!normalized) {
|
|
156
|
+
return toUnresolvedNodeId(rawTarget);
|
|
157
|
+
}
|
|
158
|
+
const lowerTarget = normalized.toLowerCase();
|
|
159
|
+
const direct = registry.byLowerPath.get(lowerTarget);
|
|
160
|
+
if (direct) {
|
|
161
|
+
return toNoteNodeId(direct);
|
|
162
|
+
}
|
|
163
|
+
if (!normalized.includes("/")) {
|
|
164
|
+
const basenameMatches = registry.byLowerBasename.get(lowerTarget) ?? [];
|
|
165
|
+
if (basenameMatches.length === 1) {
|
|
166
|
+
return toNoteNodeId(basenameMatches[0]);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return toUnresolvedNodeId(normalized);
|
|
170
|
+
}
|
|
171
|
+
function createEdgeId(type, source, target, label) {
|
|
172
|
+
const suffix = label ? `:${label}` : "";
|
|
173
|
+
return `${type}:${source}->${target}${suffix}`;
|
|
174
|
+
}
|
|
175
|
+
function buildFragmentNode(id, title, type, category, pathValue, tags, missing, modifiedAt) {
|
|
176
|
+
return {
|
|
177
|
+
id,
|
|
178
|
+
title,
|
|
179
|
+
type,
|
|
180
|
+
category,
|
|
181
|
+
path: pathValue,
|
|
182
|
+
tags,
|
|
183
|
+
missing,
|
|
184
|
+
degree: 0,
|
|
185
|
+
modifiedAt
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function parseFileFragment(vaultPath, relativePath, mtimeMs, registry) {
|
|
189
|
+
const absolutePath = path.join(vaultPath, relativePath);
|
|
190
|
+
const raw = fs.readFileSync(absolutePath, "utf-8");
|
|
191
|
+
const parsed = matter(raw);
|
|
192
|
+
const frontmatter = parsed.data ?? {};
|
|
193
|
+
const noteKey = toNoteKey(relativePath);
|
|
194
|
+
const noteNodeId = toNoteNodeId(noteKey);
|
|
195
|
+
const noteType = inferNodeType(relativePath, frontmatter);
|
|
196
|
+
const tags = collectTags(frontmatter, parsed.content);
|
|
197
|
+
const modifiedAt = new Date(mtimeMs).toISOString();
|
|
198
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
199
|
+
const edges = /* @__PURE__ */ new Map();
|
|
200
|
+
nodes.set(
|
|
201
|
+
noteNodeId,
|
|
202
|
+
buildFragmentNode(
|
|
203
|
+
noteNodeId,
|
|
204
|
+
typeof frontmatter.title === "string" && frontmatter.title.trim() ? frontmatter.title.trim() : titleFromNoteKey(noteKey),
|
|
205
|
+
noteType,
|
|
206
|
+
noteType,
|
|
207
|
+
normalizeRelativePath(relativePath),
|
|
208
|
+
tags,
|
|
209
|
+
false,
|
|
210
|
+
modifiedAt
|
|
211
|
+
)
|
|
212
|
+
);
|
|
213
|
+
for (const tag of tags) {
|
|
214
|
+
const tagNodeId = toTagNodeId(tag);
|
|
215
|
+
if (!nodes.has(tagNodeId)) {
|
|
216
|
+
nodes.set(tagNodeId, buildFragmentNode(tagNodeId, `#${tag}`, "tag", "tag", null, [], false, null));
|
|
217
|
+
}
|
|
218
|
+
const edgeId = createEdgeId("tag", noteNodeId, tagNodeId);
|
|
219
|
+
edges.set(edgeId, {
|
|
220
|
+
id: edgeId,
|
|
221
|
+
source: noteNodeId,
|
|
222
|
+
target: tagNodeId,
|
|
223
|
+
type: "tag"
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
const wikiTargets = extractWikiTargets(parsed.content);
|
|
227
|
+
for (const target of wikiTargets) {
|
|
228
|
+
const targetNodeId = resolveTargetNodeId(target, registry);
|
|
229
|
+
if (targetNodeId.startsWith("unresolved:") && !nodes.has(targetNodeId)) {
|
|
230
|
+
nodes.set(
|
|
231
|
+
targetNodeId,
|
|
232
|
+
buildFragmentNode(targetNodeId, titleFromNoteKey(normalizeUnresolvedKey(target)), "unresolved", "unresolved", null, [], true, null)
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
const edgeId = createEdgeId("wiki_link", noteNodeId, targetNodeId);
|
|
236
|
+
edges.set(edgeId, {
|
|
237
|
+
id: edgeId,
|
|
238
|
+
source: noteNodeId,
|
|
239
|
+
target: targetNodeId,
|
|
240
|
+
type: "wiki_link"
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
for (const relation of extractFrontmatterRelations(frontmatter)) {
|
|
244
|
+
const targetNodeId = resolveTargetNodeId(relation.target, registry);
|
|
245
|
+
if (targetNodeId.startsWith("unresolved:") && !nodes.has(targetNodeId)) {
|
|
246
|
+
nodes.set(
|
|
247
|
+
targetNodeId,
|
|
248
|
+
buildFragmentNode(
|
|
249
|
+
targetNodeId,
|
|
250
|
+
titleFromNoteKey(normalizeUnresolvedKey(relation.target)),
|
|
251
|
+
"unresolved",
|
|
252
|
+
"unresolved",
|
|
253
|
+
null,
|
|
254
|
+
[],
|
|
255
|
+
true,
|
|
256
|
+
null
|
|
257
|
+
)
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
const edgeId = createEdgeId("frontmatter_relation", noteNodeId, targetNodeId, relation.field);
|
|
261
|
+
edges.set(edgeId, {
|
|
262
|
+
id: edgeId,
|
|
263
|
+
source: noteNodeId,
|
|
264
|
+
target: targetNodeId,
|
|
265
|
+
type: "frontmatter_relation",
|
|
266
|
+
label: relation.field
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
relativePath: normalizeRelativePath(relativePath),
|
|
271
|
+
mtimeMs,
|
|
272
|
+
nodes: [...nodes.values()],
|
|
273
|
+
edges: [...edges.values()]
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
function combineFragments(fragments, generatedAt) {
|
|
277
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
278
|
+
const edges = /* @__PURE__ */ new Map();
|
|
279
|
+
for (const fragment of Object.values(fragments)) {
|
|
280
|
+
for (const node of fragment.nodes) {
|
|
281
|
+
const existing = nodes.get(node.id);
|
|
282
|
+
if (!existing) {
|
|
283
|
+
nodes.set(node.id, { ...node, degree: 0 });
|
|
284
|
+
} else if (node.modifiedAt && (!existing.modifiedAt || node.modifiedAt > existing.modifiedAt)) {
|
|
285
|
+
nodes.set(node.id, { ...existing, ...node, degree: 0 });
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
for (const edge of fragment.edges) {
|
|
289
|
+
edges.set(edge.id, edge);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
const degreeByNode = /* @__PURE__ */ new Map();
|
|
293
|
+
for (const edge of edges.values()) {
|
|
294
|
+
degreeByNode.set(edge.source, (degreeByNode.get(edge.source) ?? 0) + 1);
|
|
295
|
+
degreeByNode.set(edge.target, (degreeByNode.get(edge.target) ?? 0) + 1);
|
|
296
|
+
}
|
|
297
|
+
for (const node of nodes.values()) {
|
|
298
|
+
node.degree = degreeByNode.get(node.id) ?? 0;
|
|
299
|
+
}
|
|
300
|
+
const nodeTypeCounts = {};
|
|
301
|
+
for (const node of nodes.values()) {
|
|
302
|
+
nodeTypeCounts[node.type] = (nodeTypeCounts[node.type] ?? 0) + 1;
|
|
303
|
+
}
|
|
304
|
+
const edgeTypeCounts = {};
|
|
305
|
+
for (const edge of edges.values()) {
|
|
306
|
+
edgeTypeCounts[edge.type] = (edgeTypeCounts[edge.type] ?? 0) + 1;
|
|
307
|
+
}
|
|
308
|
+
const sortedNodes = [...nodes.values()].sort((a, b) => a.id.localeCompare(b.id));
|
|
309
|
+
const sortedEdges = [...edges.values()].sort((a, b) => a.id.localeCompare(b.id));
|
|
310
|
+
return {
|
|
311
|
+
schemaVersion: MEMORY_GRAPH_SCHEMA_VERSION,
|
|
312
|
+
nodes: sortedNodes,
|
|
313
|
+
edges: sortedEdges,
|
|
314
|
+
stats: {
|
|
315
|
+
generatedAt,
|
|
316
|
+
nodeCount: sortedNodes.length,
|
|
317
|
+
edgeCount: sortedEdges.length,
|
|
318
|
+
nodeTypeCounts,
|
|
319
|
+
edgeTypeCounts
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
function isValidIndex(index) {
|
|
324
|
+
if (!index || typeof index !== "object") return false;
|
|
325
|
+
const typed = index;
|
|
326
|
+
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");
|
|
327
|
+
}
|
|
328
|
+
function loadMemoryGraphIndex(vaultPath) {
|
|
329
|
+
const indexPath = getGraphIndexPath(path.resolve(vaultPath));
|
|
330
|
+
if (!fs.existsSync(indexPath)) {
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
try {
|
|
334
|
+
const parsed = JSON.parse(fs.readFileSync(indexPath, "utf-8"));
|
|
335
|
+
if (!isValidIndex(parsed)) {
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
return parsed;
|
|
339
|
+
} catch {
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
async function buildOrUpdateMemoryGraphIndex(vaultPathInput, options = {}) {
|
|
344
|
+
const vaultPath = path.resolve(vaultPathInput);
|
|
345
|
+
ensureClawvaultDir(vaultPath);
|
|
346
|
+
const existing = options.forceFull ? null : loadMemoryGraphIndex(vaultPath);
|
|
347
|
+
const markdownFiles = await glob("**/*.md", {
|
|
348
|
+
cwd: vaultPath,
|
|
349
|
+
ignore: ["**/node_modules/**", "**/.git/**", "**/.obsidian/**", "**/.trash/**"]
|
|
350
|
+
});
|
|
351
|
+
const normalizedFiles = markdownFiles.map(normalizeRelativePath).sort((a, b) => a.localeCompare(b));
|
|
352
|
+
const registry = buildNoteRegistry(normalizedFiles);
|
|
353
|
+
const nextFragments = {};
|
|
354
|
+
const existingFragments = existing?.files ?? {};
|
|
355
|
+
const currentFileSet = new Set(normalizedFiles);
|
|
356
|
+
for (const relativePath of normalizedFiles) {
|
|
357
|
+
const absolutePath = path.join(vaultPath, relativePath);
|
|
358
|
+
const stat = fs.statSync(absolutePath);
|
|
359
|
+
const existingFragment = existingFragments[relativePath];
|
|
360
|
+
if (!options.forceFull && existingFragment && existingFragment.mtimeMs === stat.mtimeMs) {
|
|
361
|
+
nextFragments[relativePath] = existingFragment;
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
nextFragments[relativePath] = parseFileFragment(vaultPath, relativePath, stat.mtimeMs, registry);
|
|
365
|
+
}
|
|
366
|
+
for (const [relativePath, fragment] of Object.entries(existingFragments)) {
|
|
367
|
+
if (!currentFileSet.has(relativePath)) {
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
if (!nextFragments[relativePath]) {
|
|
371
|
+
nextFragments[relativePath] = fragment;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
const generatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
375
|
+
const graph = combineFragments(nextFragments, generatedAt);
|
|
376
|
+
const nextIndex = {
|
|
377
|
+
schemaVersion: MEMORY_GRAPH_SCHEMA_VERSION,
|
|
378
|
+
vaultPath,
|
|
379
|
+
generatedAt,
|
|
380
|
+
files: nextFragments,
|
|
381
|
+
graph
|
|
382
|
+
};
|
|
383
|
+
fs.writeFileSync(getGraphIndexPath(vaultPath), JSON.stringify(nextIndex, null, 2));
|
|
384
|
+
return nextIndex;
|
|
385
|
+
}
|
|
386
|
+
async function getMemoryGraph(vaultPath, options = {}) {
|
|
387
|
+
if (options.refresh === true) {
|
|
388
|
+
return (await buildOrUpdateMemoryGraphIndex(vaultPath, { forceFull: true })).graph;
|
|
389
|
+
}
|
|
390
|
+
return (await buildOrUpdateMemoryGraphIndex(vaultPath)).graph;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export {
|
|
394
|
+
MEMORY_GRAPH_SCHEMA_VERSION,
|
|
395
|
+
loadMemoryGraphIndex,
|
|
396
|
+
buildOrUpdateMemoryGraphIndex,
|
|
397
|
+
getMemoryGraph
|
|
398
|
+
};
|