@context-vault/core 2.17.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/capture.d.ts +21 -0
- package/dist/capture.d.ts.map +1 -0
- package/dist/capture.js +269 -0
- package/dist/capture.js.map +1 -0
- package/dist/categories.d.ts +6 -0
- package/dist/categories.d.ts.map +1 -0
- package/dist/categories.js +50 -0
- package/dist/categories.js.map +1 -0
- package/dist/config.d.ts +4 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +190 -0
- package/dist/config.js.map +1 -0
- package/dist/constants.d.ts +33 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +23 -0
- package/dist/constants.js.map +1 -0
- package/dist/db.d.ts +13 -0
- package/dist/db.d.ts.map +1 -0
- package/dist/db.js +191 -0
- package/dist/db.js.map +1 -0
- package/dist/embed.d.ts +5 -0
- package/dist/embed.d.ts.map +1 -0
- package/dist/embed.js +78 -0
- package/dist/embed.js.map +1 -0
- package/dist/files.d.ts +13 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +66 -0
- package/dist/files.js.map +1 -0
- package/dist/formatters.d.ts +8 -0
- package/dist/formatters.d.ts.map +1 -0
- package/dist/formatters.js +18 -0
- package/dist/formatters.js.map +1 -0
- package/dist/frontmatter.d.ts +12 -0
- package/dist/frontmatter.d.ts.map +1 -0
- package/dist/frontmatter.js +101 -0
- package/dist/frontmatter.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +297 -0
- package/dist/index.js.map +1 -0
- package/dist/ingest-url.d.ts +20 -0
- package/dist/ingest-url.d.ts.map +1 -0
- package/dist/ingest-url.js +113 -0
- package/dist/ingest-url.js.map +1 -0
- package/dist/main.d.ts +14 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +25 -0
- package/dist/main.js.map +1 -0
- package/dist/search.d.ts +18 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +238 -0
- package/dist/search.js.map +1 -0
- package/dist/types.d.ts +176 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +66 -17
- package/src/capture.ts +308 -0
- package/src/categories.ts +54 -0
- package/src/{core/config.js → config.ts} +34 -33
- package/src/{constants.js → constants.ts} +6 -3
- package/src/db.ts +229 -0
- package/src/{index/embed.js → embed.ts} +10 -35
- package/src/files.ts +80 -0
- package/src/{capture/formatters.js → formatters.ts} +13 -11
- package/src/{core/frontmatter.js → frontmatter.ts} +27 -33
- package/src/index.ts +351 -0
- package/src/ingest-url.ts +99 -0
- package/src/main.ts +111 -0
- package/src/search.ts +285 -0
- package/src/types.ts +166 -0
- package/src/capture/file-ops.js +0 -97
- package/src/capture/import-pipeline.js +0 -46
- package/src/capture/importers.js +0 -387
- package/src/capture/index.js +0 -236
- package/src/capture/ingest-url.js +0 -252
- package/src/consolidation/index.js +0 -112
- package/src/core/categories.js +0 -72
- package/src/core/error-log.js +0 -54
- package/src/core/files.js +0 -108
- package/src/core/status.js +0 -350
- package/src/core/telemetry.js +0 -90
- package/src/index/db.js +0 -416
- package/src/index/index.js +0 -522
- package/src/index.js +0 -66
- package/src/retrieve/index.js +0 -500
- package/src/server/helpers.js +0 -44
- package/src/server/tools/clear-context.js +0 -47
- package/src/server/tools/context-status.js +0 -182
- package/src/server/tools/create-snapshot.js +0 -231
- package/src/server/tools/delete-context.js +0 -60
- package/src/server/tools/get-context.js +0 -678
- package/src/server/tools/ingest-project.js +0 -244
- package/src/server/tools/ingest-url.js +0 -88
- package/src/server/tools/list-buckets.js +0 -116
- package/src/server/tools/list-context.js +0 -163
- package/src/server/tools/save-context.js +0 -609
- package/src/server/tools/session-start.js +0 -285
- package/src/server/tools/submit-feedback.js +0 -55
- package/src/server/tools.js +0 -174
- package/src/sync/sync.js +0 -235
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
export function htmlToMarkdown(html) {
|
|
2
|
-
let md = html;
|
|
3
|
-
|
|
4
|
-
// Remove scripts, styles, nav, header, footer, aside
|
|
5
|
-
md = md.replace(/<script[\s\S]*?<\/script>/gi, "");
|
|
6
|
-
md = md.replace(/<style[\s\S]*?<\/style>/gi, "");
|
|
7
|
-
md = md.replace(/<nav[\s\S]*?<\/nav>/gi, "");
|
|
8
|
-
md = md.replace(/<header[\s\S]*?<\/header>/gi, "");
|
|
9
|
-
md = md.replace(/<footer[\s\S]*?<\/footer>/gi, "");
|
|
10
|
-
md = md.replace(/<aside[\s\S]*?<\/aside>/gi, "");
|
|
11
|
-
|
|
12
|
-
// Convert headings
|
|
13
|
-
md = md.replace(
|
|
14
|
-
/<h1[^>]*>([\s\S]*?)<\/h1>/gi,
|
|
15
|
-
(_, c) => `\n# ${stripTags(c).trim()}\n`,
|
|
16
|
-
);
|
|
17
|
-
md = md.replace(
|
|
18
|
-
/<h2[^>]*>([\s\S]*?)<\/h2>/gi,
|
|
19
|
-
(_, c) => `\n## ${stripTags(c).trim()}\n`,
|
|
20
|
-
);
|
|
21
|
-
md = md.replace(
|
|
22
|
-
/<h3[^>]*>([\s\S]*?)<\/h3>/gi,
|
|
23
|
-
(_, c) => `\n### ${stripTags(c).trim()}\n`,
|
|
24
|
-
);
|
|
25
|
-
md = md.replace(
|
|
26
|
-
/<h4[^>]*>([\s\S]*?)<\/h4>/gi,
|
|
27
|
-
(_, c) => `\n#### ${stripTags(c).trim()}\n`,
|
|
28
|
-
);
|
|
29
|
-
md = md.replace(
|
|
30
|
-
/<h5[^>]*>([\s\S]*?)<\/h5>/gi,
|
|
31
|
-
(_, c) => `\n##### ${stripTags(c).trim()}\n`,
|
|
32
|
-
);
|
|
33
|
-
md = md.replace(
|
|
34
|
-
/<h6[^>]*>([\s\S]*?)<\/h6>/gi,
|
|
35
|
-
(_, c) => `\n###### ${stripTags(c).trim()}\n`,
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
// Convert links
|
|
39
|
-
md = md.replace(
|
|
40
|
-
/<a[^>]*href="([^"]*)"[^>]*>([\s\S]*?)<\/a>/gi,
|
|
41
|
-
(_, href, text) => {
|
|
42
|
-
const cleanText = stripTags(text).trim();
|
|
43
|
-
return cleanText ? `[${cleanText}](${href})` : "";
|
|
44
|
-
},
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
// Convert code blocks
|
|
48
|
-
md = md.replace(
|
|
49
|
-
/<pre[^>]*><code[^>]*>([\s\S]*?)<\/code><\/pre>/gi,
|
|
50
|
-
(_, c) => `\n\`\`\`\n${decodeEntities(c).trim()}\n\`\`\`\n`,
|
|
51
|
-
);
|
|
52
|
-
md = md.replace(
|
|
53
|
-
/<pre[^>]*>([\s\S]*?)<\/pre>/gi,
|
|
54
|
-
(_, c) => `\n\`\`\`\n${decodeEntities(stripTags(c)).trim()}\n\`\`\`\n`,
|
|
55
|
-
);
|
|
56
|
-
|
|
57
|
-
// Convert inline code
|
|
58
|
-
md = md.replace(
|
|
59
|
-
/<code[^>]*>([\s\S]*?)<\/code>/gi,
|
|
60
|
-
(_, c) => `\`${decodeEntities(c).trim()}\``,
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
// Convert strong/em
|
|
64
|
-
md = md.replace(
|
|
65
|
-
/<(strong|b)[^>]*>([\s\S]*?)<\/\1>/gi,
|
|
66
|
-
(_, __, c) => `**${stripTags(c).trim()}**`,
|
|
67
|
-
);
|
|
68
|
-
md = md.replace(
|
|
69
|
-
/<(em|i)[^>]*>([\s\S]*?)<\/\1>/gi,
|
|
70
|
-
(_, __, c) => `*${stripTags(c).trim()}*`,
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
// Convert list items
|
|
74
|
-
md = md.replace(
|
|
75
|
-
/<li[^>]*>([\s\S]*?)<\/li>/gi,
|
|
76
|
-
(_, c) => `- ${stripTags(c).trim()}\n`,
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
// Convert paragraphs and line breaks
|
|
80
|
-
md = md.replace(/<br\s*\/?>/gi, "\n");
|
|
81
|
-
md = md.replace(
|
|
82
|
-
/<p[^>]*>([\s\S]*?)<\/p>/gi,
|
|
83
|
-
(_, c) => `\n${stripTags(c).trim()}\n`,
|
|
84
|
-
);
|
|
85
|
-
md = md.replace(/<blockquote[^>]*>([\s\S]*?)<\/blockquote>/gi, (_, c) => {
|
|
86
|
-
return (
|
|
87
|
-
"\n" +
|
|
88
|
-
stripTags(c)
|
|
89
|
-
.trim()
|
|
90
|
-
.split("\n")
|
|
91
|
-
.map((l) => `> ${l}`)
|
|
92
|
-
.join("\n") +
|
|
93
|
-
"\n"
|
|
94
|
-
);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
// Remove remaining HTML tags
|
|
98
|
-
md = stripTags(md);
|
|
99
|
-
|
|
100
|
-
// Decode HTML entities
|
|
101
|
-
md = decodeEntities(md);
|
|
102
|
-
|
|
103
|
-
// Clean up whitespace
|
|
104
|
-
md = md.replace(/\n{3,}/g, "\n\n").trim();
|
|
105
|
-
|
|
106
|
-
return md;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function stripTags(html) {
|
|
110
|
-
return html.replace(/<[^>]+>/g, "");
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function decodeEntities(text) {
|
|
114
|
-
return text
|
|
115
|
-
.replace(/&/g, "&")
|
|
116
|
-
.replace(/</g, "<")
|
|
117
|
-
.replace(/>/g, ">")
|
|
118
|
-
.replace(/"/g, '"')
|
|
119
|
-
.replace(/'/g, "'")
|
|
120
|
-
.replace(/ /g, " ")
|
|
121
|
-
.replace(/&#(\d+);/g, (_, n) => String.fromCharCode(parseInt(n, 10)))
|
|
122
|
-
.replace(/&#x([0-9a-f]+);/gi, (_, n) =>
|
|
123
|
-
String.fromCharCode(parseInt(n, 16)),
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Extract the main readable content from an HTML page.
|
|
129
|
-
* Prefers <article> or <main>, falls back to <body>.
|
|
130
|
-
*
|
|
131
|
-
* @param {string} html
|
|
132
|
-
* @param {string} url
|
|
133
|
-
* @returns {{ title: string, body: string }}
|
|
134
|
-
*/
|
|
135
|
-
export function extractHtmlContent(html, url) {
|
|
136
|
-
// Extract <title>
|
|
137
|
-
const titleMatch = html.match(/<title[^>]*>([\s\S]*?)<\/title>/i);
|
|
138
|
-
const title = titleMatch
|
|
139
|
-
? stripTags(decodeEntities(titleMatch[1])).trim()
|
|
140
|
-
: "";
|
|
141
|
-
|
|
142
|
-
// Try to extract main content area
|
|
143
|
-
let contentHtml = "";
|
|
144
|
-
|
|
145
|
-
const articleMatch = html.match(/<article[^>]*>([\s\S]*?)<\/article>/i);
|
|
146
|
-
const mainMatch = html.match(/<main[^>]*>([\s\S]*?)<\/main>/i);
|
|
147
|
-
|
|
148
|
-
if (articleMatch) {
|
|
149
|
-
contentHtml = articleMatch[1];
|
|
150
|
-
} else if (mainMatch) {
|
|
151
|
-
contentHtml = mainMatch[1];
|
|
152
|
-
} else {
|
|
153
|
-
// Fall back to <body>
|
|
154
|
-
const bodyMatch = html.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
|
|
155
|
-
contentHtml = bodyMatch ? bodyMatch[1] : html;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const body = htmlToMarkdown(contentHtml);
|
|
159
|
-
|
|
160
|
-
return { title, body };
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Fetch a URL, extract readable content, and return an EntryData object.
|
|
165
|
-
*
|
|
166
|
-
* @param {string} url
|
|
167
|
-
* @param {{ kind?: string, tags?: string[], source?: string, maxBodyLength?: number, timeoutMs?: number }} [opts]
|
|
168
|
-
* @returns {Promise<{ kind: string, title: string, body: string, tags: string[], meta: object, source: string }>}
|
|
169
|
-
*/
|
|
170
|
-
export async function ingestUrl(url, opts = {}) {
|
|
171
|
-
const {
|
|
172
|
-
kind = "reference",
|
|
173
|
-
tags = [],
|
|
174
|
-
source,
|
|
175
|
-
maxBodyLength = 50000,
|
|
176
|
-
timeoutMs = 15000,
|
|
177
|
-
} = opts;
|
|
178
|
-
|
|
179
|
-
let domain;
|
|
180
|
-
try {
|
|
181
|
-
domain = new URL(url).hostname;
|
|
182
|
-
} catch {
|
|
183
|
-
throw new Error(`Invalid URL: ${url}`);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
const controller = new AbortController();
|
|
187
|
-
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
188
|
-
|
|
189
|
-
let response;
|
|
190
|
-
try {
|
|
191
|
-
response = await fetch(url, {
|
|
192
|
-
signal: controller.signal,
|
|
193
|
-
headers: {
|
|
194
|
-
"User-Agent":
|
|
195
|
-
"ContextVault/1.0 (+https://github.com/fellanH/context-vault)",
|
|
196
|
-
Accept: "text/html,application/xhtml+xml,text/plain,*/*",
|
|
197
|
-
},
|
|
198
|
-
});
|
|
199
|
-
} catch (err) {
|
|
200
|
-
if (err.name === "AbortError") {
|
|
201
|
-
throw new Error(`Request timed out after ${timeoutMs}ms`);
|
|
202
|
-
}
|
|
203
|
-
throw new Error(`Fetch failed: ${err.message}`);
|
|
204
|
-
} finally {
|
|
205
|
-
clearTimeout(timeout);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (!response.ok) {
|
|
209
|
-
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
const contentType = response.headers.get("content-type") || "";
|
|
213
|
-
const html = await response.text();
|
|
214
|
-
|
|
215
|
-
let title, body;
|
|
216
|
-
|
|
217
|
-
if (
|
|
218
|
-
contentType.includes("text/html") ||
|
|
219
|
-
contentType.includes("application/xhtml")
|
|
220
|
-
) {
|
|
221
|
-
const extracted = extractHtmlContent(html, url);
|
|
222
|
-
title = extracted.title;
|
|
223
|
-
body = extracted.body;
|
|
224
|
-
} else {
|
|
225
|
-
// Plain text or other — use as-is
|
|
226
|
-
title = domain;
|
|
227
|
-
body = html;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// Truncate if too long
|
|
231
|
-
if (body.length > maxBodyLength) {
|
|
232
|
-
body = body.slice(0, maxBodyLength) + "\n\n[Content truncated]";
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
if (!body.trim()) {
|
|
236
|
-
throw new Error("No readable content extracted from URL");
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
return {
|
|
240
|
-
kind,
|
|
241
|
-
title: title || domain,
|
|
242
|
-
body,
|
|
243
|
-
tags: [...tags, "web-import"],
|
|
244
|
-
meta: {
|
|
245
|
-
url,
|
|
246
|
-
domain,
|
|
247
|
-
fetched_at: new Date().toISOString(),
|
|
248
|
-
content_type: contentType.split(";")[0].trim() || "text/html",
|
|
249
|
-
},
|
|
250
|
-
source: source || domain,
|
|
251
|
-
};
|
|
252
|
-
}
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Consolidation utilities — identifies tags and entries that warrant maintenance.
|
|
3
|
-
*
|
|
4
|
-
* These are pure DB queries with no LLM calls. The caller decides what to do
|
|
5
|
-
* with the results (e.g. run create_snapshot, archive entries, report to user).
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Identifies tags that have accumulated enough entries to warrant consolidation.
|
|
10
|
-
*
|
|
11
|
-
* A tag is "hot" when it has >= tagThreshold non-superseded entries AND no
|
|
12
|
-
* brief/snapshot was saved for it within the last maxSnapshotAgeDays days.
|
|
13
|
-
*
|
|
14
|
-
* @param {import('node:sqlite').DatabaseSync} db
|
|
15
|
-
* @param {{ tagThreshold?: number, maxSnapshotAgeDays?: number }} [opts]
|
|
16
|
-
* @returns {{ tag: string, entryCount: number, lastSnapshotAge: number | null }[]}
|
|
17
|
-
*/
|
|
18
|
-
export function findHotTags(
|
|
19
|
-
db,
|
|
20
|
-
{ tagThreshold = 10, maxSnapshotAgeDays = 7 } = {},
|
|
21
|
-
) {
|
|
22
|
-
const rows = db
|
|
23
|
-
.prepare(
|
|
24
|
-
`SELECT id, tags, kind FROM vault
|
|
25
|
-
WHERE superseded_by IS NULL
|
|
26
|
-
AND tags IS NOT NULL
|
|
27
|
-
AND tags != '[]'`,
|
|
28
|
-
)
|
|
29
|
-
.all();
|
|
30
|
-
|
|
31
|
-
const tagCounts = new Map();
|
|
32
|
-
|
|
33
|
-
for (const row of rows) {
|
|
34
|
-
let tags;
|
|
35
|
-
try {
|
|
36
|
-
tags = JSON.parse(row.tags);
|
|
37
|
-
} catch {
|
|
38
|
-
continue;
|
|
39
|
-
}
|
|
40
|
-
if (!Array.isArray(tags)) continue;
|
|
41
|
-
|
|
42
|
-
for (const tag of tags) {
|
|
43
|
-
if (typeof tag !== "string" || !tag) continue;
|
|
44
|
-
tagCounts.set(tag, (tagCounts.get(tag) ?? 0) + 1);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const hotTags = [];
|
|
49
|
-
|
|
50
|
-
for (const [tag, count] of tagCounts) {
|
|
51
|
-
if (count < tagThreshold) continue;
|
|
52
|
-
|
|
53
|
-
const snapshotRow = db
|
|
54
|
-
.prepare(
|
|
55
|
-
`SELECT created_at FROM vault
|
|
56
|
-
WHERE kind = 'brief'
|
|
57
|
-
AND tags LIKE ?
|
|
58
|
-
AND created_at > datetime('now', '-' || ? || ' days')
|
|
59
|
-
ORDER BY created_at DESC
|
|
60
|
-
LIMIT 1`,
|
|
61
|
-
)
|
|
62
|
-
.get(`%"${tag}"%`, String(maxSnapshotAgeDays));
|
|
63
|
-
|
|
64
|
-
if (snapshotRow) continue;
|
|
65
|
-
|
|
66
|
-
const lastSnapshotAny = db
|
|
67
|
-
.prepare(
|
|
68
|
-
`SELECT created_at FROM vault
|
|
69
|
-
WHERE kind = 'brief'
|
|
70
|
-
AND tags LIKE ?
|
|
71
|
-
ORDER BY created_at DESC
|
|
72
|
-
LIMIT 1`,
|
|
73
|
-
)
|
|
74
|
-
.get(`%"${tag}"%`);
|
|
75
|
-
|
|
76
|
-
let lastSnapshotAge = null;
|
|
77
|
-
if (lastSnapshotAny) {
|
|
78
|
-
const ms = Date.now() - new Date(lastSnapshotAny.created_at).getTime();
|
|
79
|
-
lastSnapshotAge = Math.floor(ms / (1000 * 60 * 60 * 24));
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
hotTags.push({ tag, entryCount: count, lastSnapshotAge });
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
hotTags.sort((a, b) => b.entryCount - a.entryCount);
|
|
86
|
-
|
|
87
|
-
return hotTags;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Identifies cold entries (old, never or rarely accessed) that can be archived.
|
|
92
|
-
*
|
|
93
|
-
* Returns IDs of entries that are old enough, have low hit counts, are not
|
|
94
|
-
* superseded, and are not in permanent kinds (decision, architecture, brief).
|
|
95
|
-
*
|
|
96
|
-
* @param {import('node:sqlite').DatabaseSync} db
|
|
97
|
-
* @param {{ maxAgeDays?: number, maxHitCount?: number }} [opts]
|
|
98
|
-
* @returns {string[]} Entry IDs eligible for archiving
|
|
99
|
-
*/
|
|
100
|
-
export function findColdEntries(db, { maxAgeDays = 90, maxHitCount = 0 } = {}) {
|
|
101
|
-
const rows = db
|
|
102
|
-
.prepare(
|
|
103
|
-
`SELECT id FROM vault
|
|
104
|
-
WHERE hit_count <= ?
|
|
105
|
-
AND created_at < datetime('now', '-' || ? || ' days')
|
|
106
|
-
AND superseded_by IS NULL
|
|
107
|
-
AND kind NOT IN ('decision', 'architecture', 'brief')`,
|
|
108
|
-
)
|
|
109
|
-
.all(maxHitCount, String(maxAgeDays));
|
|
110
|
-
|
|
111
|
-
return rows.map((r) => r.id);
|
|
112
|
-
}
|
package/src/core/categories.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* categories.js — Static kind→category mapping
|
|
3
|
-
*
|
|
4
|
-
* Three categories with distinct write semantics:
|
|
5
|
-
* knowledge — append-only, enduring (default)
|
|
6
|
-
* entity — upsert by identity_key, enduring
|
|
7
|
-
* event — append-only, decaying relevance
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
const KIND_CATEGORY = {
|
|
11
|
-
// Knowledge — append-only, enduring
|
|
12
|
-
insight: "knowledge",
|
|
13
|
-
decision: "knowledge",
|
|
14
|
-
pattern: "knowledge",
|
|
15
|
-
prompt: "knowledge",
|
|
16
|
-
note: "knowledge",
|
|
17
|
-
document: "knowledge",
|
|
18
|
-
reference: "knowledge",
|
|
19
|
-
// Entity — upsert, enduring
|
|
20
|
-
contact: "entity",
|
|
21
|
-
project: "entity",
|
|
22
|
-
tool: "entity",
|
|
23
|
-
source: "entity",
|
|
24
|
-
bucket: "entity",
|
|
25
|
-
// Event — append-only, decaying
|
|
26
|
-
conversation: "event",
|
|
27
|
-
message: "event",
|
|
28
|
-
session: "event",
|
|
29
|
-
task: "event",
|
|
30
|
-
log: "event",
|
|
31
|
-
feedback: "event",
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
/** Map category name → directory name on disk */
|
|
35
|
-
const CATEGORY_DIR_NAMES = {
|
|
36
|
-
knowledge: "knowledge",
|
|
37
|
-
entity: "entities",
|
|
38
|
-
event: "events",
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
/** Set of valid category directory names (for reindex discovery) */
|
|
42
|
-
export const CATEGORY_DIRS = new Set(Object.values(CATEGORY_DIR_NAMES));
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Staleness thresholds (in days) per knowledge kind.
|
|
46
|
-
* Kinds not listed here are considered enduring (no staleness threshold).
|
|
47
|
-
* Based on updated_at; falls back to created_at if updated_at is null.
|
|
48
|
-
*/
|
|
49
|
-
export const KIND_STALENESS_DAYS = {
|
|
50
|
-
pattern: 180,
|
|
51
|
-
decision: 365,
|
|
52
|
-
reference: 90,
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const DURABLE_KINDS = new Set(["decision", "architecture", "pattern"]);
|
|
56
|
-
const EPHEMERAL_KINDS = new Set(["session", "observation"]);
|
|
57
|
-
|
|
58
|
-
export function categoryFor(kind) {
|
|
59
|
-
return KIND_CATEGORY[kind] || "knowledge";
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function defaultTierFor(kind) {
|
|
63
|
-
if (DURABLE_KINDS.has(kind)) return "durable";
|
|
64
|
-
if (EPHEMERAL_KINDS.has(kind)) return "ephemeral";
|
|
65
|
-
return "working";
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/** Returns the category directory name for a given kind (e.g. "insight" → "knowledge") */
|
|
69
|
-
export function categoryDirFor(kind) {
|
|
70
|
-
const cat = categoryFor(kind);
|
|
71
|
-
return CATEGORY_DIR_NAMES[cat] || "knowledge";
|
|
72
|
-
}
|
package/src/core/error-log.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
appendFileSync,
|
|
3
|
-
existsSync,
|
|
4
|
-
mkdirSync,
|
|
5
|
-
readFileSync,
|
|
6
|
-
statSync,
|
|
7
|
-
writeFileSync,
|
|
8
|
-
} from "node:fs";
|
|
9
|
-
import { join } from "node:path";
|
|
10
|
-
|
|
11
|
-
const MAX_LOG_SIZE = 1024 * 1024; // 1MB
|
|
12
|
-
|
|
13
|
-
export function errorLogPath(dataDir) {
|
|
14
|
-
return join(dataDir, "error.log");
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Append a structured JSON entry to the startup error log.
|
|
19
|
-
* Rotates the file if it exceeds MAX_LOG_SIZE.
|
|
20
|
-
* Never throws — logging failures must not mask the original error.
|
|
21
|
-
*
|
|
22
|
-
* @param {string} dataDir
|
|
23
|
-
* @param {object} entry
|
|
24
|
-
*/
|
|
25
|
-
export function appendErrorLog(dataDir, entry) {
|
|
26
|
-
try {
|
|
27
|
-
mkdirSync(dataDir, { recursive: true });
|
|
28
|
-
const logPath = errorLogPath(dataDir);
|
|
29
|
-
if (existsSync(logPath) && statSync(logPath).size >= MAX_LOG_SIZE) {
|
|
30
|
-
writeFileSync(logPath, "");
|
|
31
|
-
}
|
|
32
|
-
appendFileSync(logPath, JSON.stringify(entry) + "\n");
|
|
33
|
-
} catch {
|
|
34
|
-
// intentionally swallowed
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Return number of log lines in the error log, or 0 if absent.
|
|
40
|
-
*
|
|
41
|
-
* @param {string} dataDir
|
|
42
|
-
* @returns {number}
|
|
43
|
-
*/
|
|
44
|
-
export function errorLogCount(dataDir) {
|
|
45
|
-
try {
|
|
46
|
-
const logPath = errorLogPath(dataDir);
|
|
47
|
-
if (!existsSync(logPath)) return 0;
|
|
48
|
-
return readFileSync(logPath, "utf-8")
|
|
49
|
-
.split("\n")
|
|
50
|
-
.filter((l) => l.trim()).length;
|
|
51
|
-
} catch {
|
|
52
|
-
return 0;
|
|
53
|
-
}
|
|
54
|
-
}
|
package/src/core/files.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* files.js — Shared file system utilities used across layers
|
|
3
|
-
*
|
|
4
|
-
* ULID generation, slugify, kind/dir mapping, directory walking.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { readdirSync } from "node:fs";
|
|
8
|
-
import { join, resolve, sep } from "node:path";
|
|
9
|
-
import { categoryDirFor } from "./categories.js";
|
|
10
|
-
|
|
11
|
-
const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
12
|
-
|
|
13
|
-
export function ulid() {
|
|
14
|
-
const now = Date.now();
|
|
15
|
-
let ts = "";
|
|
16
|
-
let t = now;
|
|
17
|
-
for (let i = 0; i < 10; i++) {
|
|
18
|
-
ts = CROCKFORD[t & 31] + ts;
|
|
19
|
-
t = Math.floor(t / 32);
|
|
20
|
-
}
|
|
21
|
-
let rand = "";
|
|
22
|
-
for (let i = 0; i < 16; i++) {
|
|
23
|
-
rand += CROCKFORD[Math.floor(Math.random() * 32)];
|
|
24
|
-
}
|
|
25
|
-
return ts + rand;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function slugify(text, maxLen = 60) {
|
|
29
|
-
let slug = text
|
|
30
|
-
.toLowerCase()
|
|
31
|
-
.replace(/[^a-z0-9]+/g, "-")
|
|
32
|
-
.replace(/^-+|-+$/g, "");
|
|
33
|
-
if (slug.length > maxLen) {
|
|
34
|
-
slug =
|
|
35
|
-
slug.slice(0, maxLen).replace(/-[^-]*$/, "") || slug.slice(0, maxLen);
|
|
36
|
-
}
|
|
37
|
-
return slug;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const PLURAL_MAP = {
|
|
41
|
-
insight: "insights",
|
|
42
|
-
decision: "decisions",
|
|
43
|
-
pattern: "patterns",
|
|
44
|
-
status: "statuses",
|
|
45
|
-
analysis: "analyses",
|
|
46
|
-
contact: "contacts",
|
|
47
|
-
project: "projects",
|
|
48
|
-
tool: "tools",
|
|
49
|
-
source: "sources",
|
|
50
|
-
conversation: "conversations",
|
|
51
|
-
message: "messages",
|
|
52
|
-
session: "sessions",
|
|
53
|
-
log: "logs",
|
|
54
|
-
feedback: "feedbacks",
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const SINGULAR_MAP = Object.fromEntries(
|
|
58
|
-
Object.entries(PLURAL_MAP).map(([k, v]) => [v, k]),
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
export function kindToDir(kind) {
|
|
62
|
-
if (PLURAL_MAP[kind]) return PLURAL_MAP[kind];
|
|
63
|
-
return kind.endsWith("s") ? kind : kind + "s";
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function dirToKind(dirName) {
|
|
67
|
-
if (SINGULAR_MAP[dirName]) return SINGULAR_MAP[dirName];
|
|
68
|
-
return dirName.replace(/s$/, "");
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** Normalize a kind input (singular or plural) to its canonical singular form. */
|
|
72
|
-
export function normalizeKind(input) {
|
|
73
|
-
if (PLURAL_MAP[input]) return input; // Already a known singular kind
|
|
74
|
-
if (SINGULAR_MAP[input]) return SINGULAR_MAP[input]; // Known plural → singular
|
|
75
|
-
return input; // Unknown — use as-is (don't strip 's')
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** Returns relative path from vault root → kind dir: "knowledge/insights", "events/sessions", etc. */
|
|
79
|
-
export function kindToPath(kind) {
|
|
80
|
-
return `${categoryDirFor(kind)}/${kindToDir(kind)}`;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export function safeJoin(base, ...parts) {
|
|
84
|
-
const resolvedBase = resolve(base);
|
|
85
|
-
const result = resolve(join(base, ...parts));
|
|
86
|
-
if (!result.startsWith(resolvedBase + sep) && result !== resolvedBase) {
|
|
87
|
-
throw new Error(
|
|
88
|
-
`Path traversal blocked: resolved path escapes base directory`,
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
return result;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function walkDir(dir) {
|
|
95
|
-
const results = [];
|
|
96
|
-
function walk(currentDir, relDir) {
|
|
97
|
-
for (const entry of readdirSync(currentDir, { withFileTypes: true })) {
|
|
98
|
-
const fullPath = join(currentDir, entry.name);
|
|
99
|
-
if (entry.isDirectory() && !entry.name.startsWith("_")) {
|
|
100
|
-
walk(fullPath, relDir ? join(relDir, entry.name) : entry.name);
|
|
101
|
-
} else if (entry.isFile() && entry.name.endsWith(".md")) {
|
|
102
|
-
results.push({ filePath: fullPath, relDir });
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
walk(dir, "");
|
|
107
|
-
return results;
|
|
108
|
-
}
|