remem-mcp 0.6.7 → 0.7.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 +170 -32
- package/dist/{chunk-XK4Y53J2.js → chunk-ONQHLUGM.js} +1 -8
- package/dist/{chunk-XK4Y53J2.js.map → chunk-ONQHLUGM.js.map} +1 -1
- package/dist/{chunk-X4ECGLYH.js → chunk-OYUNKUZK.js} +111 -817
- package/dist/chunk-OYUNKUZK.js.map +1 -0
- package/dist/chunk-TGLOMFIE.js +819 -0
- package/dist/chunk-TGLOMFIE.js.map +1 -0
- package/dist/chunk-XJGDYQ34.js +22 -0
- package/dist/chunk-XJGDYQ34.js.map +1 -0
- package/dist/{chunk-FA324IBN.js → chunk-ZXI67YIV.js} +11 -18
- package/dist/chunk-ZXI67YIV.js.map +1 -0
- package/dist/{engine-LVKGZHBY.js → engine-3JUSB3RK.js} +4 -3
- package/dist/index.js +1467 -321
- package/dist/index.js.map +1 -1
- package/dist/{resolver-AVK7XKX7.js → resolver-YIFWRNMS.js} +7 -9
- package/dist/resolver-YIFWRNMS.js.map +1 -0
- package/dist/sdk.js +8 -5
- package/dist/server-SUKPVQWL.js +517 -0
- package/dist/server-SUKPVQWL.js.map +1 -0
- package/dist/storage/schema.sql +49 -1
- package/package.json +1 -1
- package/skills/remem-mcp/SKILL.md +33 -0
- package/dist/chunk-FA324IBN.js.map +0 -1
- package/dist/chunk-X4ECGLYH.js.map +0 -1
- package/dist/resolver-AVK7XKX7.js.map +0 -1
- /package/dist/{engine-LVKGZHBY.js.map → engine-3JUSB3RK.js.map} +0 -0
|
@@ -0,0 +1,819 @@
|
|
|
1
|
+
import {
|
|
2
|
+
init_engine
|
|
3
|
+
} from "./chunk-ZXI67YIV.js";
|
|
4
|
+
import {
|
|
5
|
+
LocalEmbedder,
|
|
6
|
+
SQLiteBackend,
|
|
7
|
+
init_local,
|
|
8
|
+
stripQueryProperNouns
|
|
9
|
+
} from "./chunk-OYUNKUZK.js";
|
|
10
|
+
import {
|
|
11
|
+
generateId,
|
|
12
|
+
init_ulid
|
|
13
|
+
} from "./chunk-XJGDYQ34.js";
|
|
14
|
+
import {
|
|
15
|
+
init_esm_shims
|
|
16
|
+
} from "./chunk-ONQHLUGM.js";
|
|
17
|
+
|
|
18
|
+
// src/sdk.ts
|
|
19
|
+
init_esm_shims();
|
|
20
|
+
init_local();
|
|
21
|
+
import { createHash as createHash2 } from "crypto";
|
|
22
|
+
import { homedir } from "os";
|
|
23
|
+
import { join as join2 } from "path";
|
|
24
|
+
|
|
25
|
+
// src/security/redactor.ts
|
|
26
|
+
init_esm_shims();
|
|
27
|
+
var SECRET_PATTERNS = [
|
|
28
|
+
// OpenAI API key
|
|
29
|
+
/sk-[a-zA-Z0-9]{20,}/g,
|
|
30
|
+
// Anthropic API key
|
|
31
|
+
/sk-ant-[a-zA-Z0-9-]+/g,
|
|
32
|
+
// GitHub personal access token
|
|
33
|
+
/ghp_[a-zA-Z0-9]{36}/g,
|
|
34
|
+
// GitHub OAuth token
|
|
35
|
+
/gho_[a-zA-Z0-9]{36}/g,
|
|
36
|
+
// GitHub fine-grained token
|
|
37
|
+
/github_pat_[a-zA-Z0-9_]{82}/g,
|
|
38
|
+
// Slack token
|
|
39
|
+
/xox[baprs]-[a-zA-Z0-9-]+/g,
|
|
40
|
+
// AWS access key ID
|
|
41
|
+
/AKIA[0-9A-Z]{16}/g,
|
|
42
|
+
// AWS secret access key (40 chars, base64-ish)
|
|
43
|
+
/(?<![A-Za-z0-9+/])[A-Za-z0-9+/]{40}(?![A-Za-z0-9+/])/g,
|
|
44
|
+
// PEM private key block
|
|
45
|
+
/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g,
|
|
46
|
+
// Google API key
|
|
47
|
+
/AIza[0-9A-Za-z\-_]{35}/g,
|
|
48
|
+
// Generic bearer token (require 20+ char token to avoid redacting "Bearer with JWT")
|
|
49
|
+
/Bearer\s+[a-zA-Z0-9\-._~+/]{20,}=*/g
|
|
50
|
+
];
|
|
51
|
+
var MIN_LENGTH = 40;
|
|
52
|
+
var ENTROPY_THRESHOLD = 4.5;
|
|
53
|
+
function shannonEntropy(str) {
|
|
54
|
+
const freq = /* @__PURE__ */ new Map();
|
|
55
|
+
for (const ch of str) {
|
|
56
|
+
freq.set(ch, (freq.get(ch) ?? 0) + 1);
|
|
57
|
+
}
|
|
58
|
+
let entropy = 0;
|
|
59
|
+
const len = str.length;
|
|
60
|
+
for (const count of freq.values()) {
|
|
61
|
+
const p = count / len;
|
|
62
|
+
entropy -= p * Math.log2(p);
|
|
63
|
+
}
|
|
64
|
+
return entropy;
|
|
65
|
+
}
|
|
66
|
+
function findHighEntropyStrings(text) {
|
|
67
|
+
const results = [];
|
|
68
|
+
const regex = /[a-zA-Z0-9+/=]{40,}/g;
|
|
69
|
+
let match;
|
|
70
|
+
match = regex.exec(text);
|
|
71
|
+
while (match !== null) {
|
|
72
|
+
const str = match[0];
|
|
73
|
+
if (str.length >= MIN_LENGTH) {
|
|
74
|
+
const entropy = shannonEntropy(str);
|
|
75
|
+
if (entropy >= ENTROPY_THRESHOLD) {
|
|
76
|
+
results.push(str);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
match = regex.exec(text);
|
|
80
|
+
}
|
|
81
|
+
return results;
|
|
82
|
+
}
|
|
83
|
+
function redact(text) {
|
|
84
|
+
let result = text;
|
|
85
|
+
let redacted = false;
|
|
86
|
+
for (const pattern of SECRET_PATTERNS) {
|
|
87
|
+
const before = result;
|
|
88
|
+
result = result.replace(pattern, "[REDACTED]");
|
|
89
|
+
if (result !== before) redacted = true;
|
|
90
|
+
}
|
|
91
|
+
const highEntropy = findHighEntropyStrings(result);
|
|
92
|
+
for (const str of highEntropy) {
|
|
93
|
+
result = result.replace(str, "[REDACTED]");
|
|
94
|
+
redacted = true;
|
|
95
|
+
}
|
|
96
|
+
return { text: result, redacted };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// src/sdk.ts
|
|
100
|
+
init_ulid();
|
|
101
|
+
init_engine();
|
|
102
|
+
|
|
103
|
+
// src/wiki/engine.ts
|
|
104
|
+
init_esm_shims();
|
|
105
|
+
init_ulid();
|
|
106
|
+
import { createHash } from "crypto";
|
|
107
|
+
import { existsSync, readdirSync, readFileSync, statSync } from "fs";
|
|
108
|
+
import { basename, extname, join, relative, sep } from "path";
|
|
109
|
+
function parseFrontmatter(content) {
|
|
110
|
+
if (!content.startsWith("---")) {
|
|
111
|
+
return { frontmatter: null, body: content };
|
|
112
|
+
}
|
|
113
|
+
const end = content.indexOf("\n---", 3);
|
|
114
|
+
if (end === -1) {
|
|
115
|
+
return { frontmatter: null, body: content };
|
|
116
|
+
}
|
|
117
|
+
const frontmatter = content.slice(3, end).trim();
|
|
118
|
+
const body = content.slice(end + 4).replace(/^\n/, "");
|
|
119
|
+
return { frontmatter, body };
|
|
120
|
+
}
|
|
121
|
+
function extractTags(frontmatter) {
|
|
122
|
+
if (!frontmatter) return null;
|
|
123
|
+
const match = frontmatter.match(/^tags:\s*(.+)$/m);
|
|
124
|
+
if (!match) return null;
|
|
125
|
+
return match[1].replace(/[[\]]/g, "").trim();
|
|
126
|
+
}
|
|
127
|
+
function extractTitle(frontmatter, body, fileName) {
|
|
128
|
+
if (frontmatter) {
|
|
129
|
+
const titleMatch = frontmatter.match(/^title:\s*(.+)$/m);
|
|
130
|
+
if (titleMatch) return titleMatch[1].replace(/["']/g, "").trim();
|
|
131
|
+
}
|
|
132
|
+
const h1Match = body.match(/^#\s+(.+)$/m);
|
|
133
|
+
if (h1Match) return h1Match[1].trim();
|
|
134
|
+
return basename(fileName, extname(fileName));
|
|
135
|
+
}
|
|
136
|
+
function extractWikilinks(content) {
|
|
137
|
+
const links = [];
|
|
138
|
+
const lines = content.split("\n");
|
|
139
|
+
const regex = /\[\[([^\]|]+)(?:\|([^\]]+))?\]\]/g;
|
|
140
|
+
for (let i = 0; i < lines.length; i++) {
|
|
141
|
+
let match;
|
|
142
|
+
regex.lastIndex = 0;
|
|
143
|
+
match = regex.exec(lines[i]);
|
|
144
|
+
while (match !== null) {
|
|
145
|
+
links.push({
|
|
146
|
+
target: match[1].trim(),
|
|
147
|
+
text: (match[2] ?? match[1]).trim(),
|
|
148
|
+
line: i + 1
|
|
149
|
+
});
|
|
150
|
+
match = regex.exec(lines[i]);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return links;
|
|
154
|
+
}
|
|
155
|
+
function extractMarkdownLinks(content) {
|
|
156
|
+
const links = [];
|
|
157
|
+
const lines = content.split("\n");
|
|
158
|
+
const regex = /(?<!!)\[([^\]]+)\]\(([^)]+)\)/g;
|
|
159
|
+
for (let i = 0; i < lines.length; i++) {
|
|
160
|
+
let match;
|
|
161
|
+
regex.lastIndex = 0;
|
|
162
|
+
match = regex.exec(lines[i]);
|
|
163
|
+
while (match !== null) {
|
|
164
|
+
const target = match[2].trim();
|
|
165
|
+
if (!target.startsWith("http") && !target.startsWith("mailto:")) {
|
|
166
|
+
links.push({
|
|
167
|
+
target: target.replace(/\.md$/i, ""),
|
|
168
|
+
text: match[1].trim(),
|
|
169
|
+
line: i + 1
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
match = regex.exec(lines[i]);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return links;
|
|
176
|
+
}
|
|
177
|
+
function parseMarkdownFile(filePath, repoPath) {
|
|
178
|
+
const ext = extname(filePath).toLowerCase();
|
|
179
|
+
if (ext !== ".md" && ext !== ".markdown") return null;
|
|
180
|
+
let source;
|
|
181
|
+
try {
|
|
182
|
+
source = readFileSync(filePath, "utf-8");
|
|
183
|
+
} catch {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
const relPath = relative(repoPath, filePath).split(sep).join("/");
|
|
187
|
+
const { frontmatter, body } = parseFrontmatter(source);
|
|
188
|
+
const title = extractTitle(frontmatter, body, filePath);
|
|
189
|
+
const tags = extractTags(frontmatter);
|
|
190
|
+
const contentHash = createHash("sha256").update(source).digest("hex");
|
|
191
|
+
const page = {
|
|
192
|
+
id: generateId(),
|
|
193
|
+
title,
|
|
194
|
+
content: body,
|
|
195
|
+
sourceFile: relPath,
|
|
196
|
+
section: null,
|
|
197
|
+
tags,
|
|
198
|
+
frontmatter,
|
|
199
|
+
contentHash
|
|
200
|
+
};
|
|
201
|
+
const wikilinks = extractWikilinks(body).map((l) => ({ ...l, type: "wikilink" }));
|
|
202
|
+
const mdlinks = extractMarkdownLinks(body).map((l) => ({ ...l, type: "markdown" }));
|
|
203
|
+
const links = [...wikilinks, ...mdlinks];
|
|
204
|
+
return { page, links };
|
|
205
|
+
}
|
|
206
|
+
function ingestFile(db, filePath, repoPath, teamId) {
|
|
207
|
+
const ext = extname(filePath).toLowerCase();
|
|
208
|
+
if (ext !== ".md" && ext !== ".markdown") {
|
|
209
|
+
return { file: filePath, pages: 0, links: 0, skipped: true, reason: "not markdown" };
|
|
210
|
+
}
|
|
211
|
+
const parsed = parseMarkdownFile(filePath, repoPath);
|
|
212
|
+
if (!parsed) {
|
|
213
|
+
return { file: filePath, pages: 0, links: 0, skipped: true, reason: "parse failed" };
|
|
214
|
+
}
|
|
215
|
+
const relPath = relative(repoPath, filePath).split(sep).join("/");
|
|
216
|
+
const now = Date.now();
|
|
217
|
+
const existing = db.prepare("SELECT id FROM wiki_pages WHERE source_file = ? AND team_id IS ?").get(relPath, teamId);
|
|
218
|
+
let pageId = parsed.page.id;
|
|
219
|
+
if (existing) {
|
|
220
|
+
pageId = existing.id;
|
|
221
|
+
db.prepare(
|
|
222
|
+
"UPDATE wiki_pages SET title = ?, content = ?, tags = ?, frontmatter = ?, content_hash = ?, updated_at = ? WHERE id = ?"
|
|
223
|
+
).run(
|
|
224
|
+
parsed.page.title,
|
|
225
|
+
parsed.page.content,
|
|
226
|
+
parsed.page.tags,
|
|
227
|
+
parsed.page.frontmatter,
|
|
228
|
+
parsed.page.contentHash,
|
|
229
|
+
now,
|
|
230
|
+
pageId
|
|
231
|
+
);
|
|
232
|
+
db.prepare("DELETE FROM wiki_links WHERE from_page_id = ?").run(pageId);
|
|
233
|
+
} else {
|
|
234
|
+
db.prepare(
|
|
235
|
+
`INSERT INTO wiki_pages (id, title, content, source_file, section, tags, frontmatter, content_hash, team_id, created_at, updated_at)
|
|
236
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
237
|
+
).run(
|
|
238
|
+
parsed.page.id,
|
|
239
|
+
parsed.page.title,
|
|
240
|
+
parsed.page.content,
|
|
241
|
+
parsed.page.sourceFile,
|
|
242
|
+
parsed.page.section,
|
|
243
|
+
parsed.page.tags,
|
|
244
|
+
parsed.page.frontmatter,
|
|
245
|
+
parsed.page.contentHash,
|
|
246
|
+
teamId,
|
|
247
|
+
now,
|
|
248
|
+
now
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
const linkStmt = db.prepare(
|
|
252
|
+
"INSERT INTO wiki_links (from_page_id, to_page_id, to_title, link_text, link_type, line) VALUES (?, ?, ?, ?, ?, ?)"
|
|
253
|
+
);
|
|
254
|
+
for (const link of parsed.links) {
|
|
255
|
+
const target = db.prepare("SELECT id FROM wiki_pages WHERE title = ? AND team_id IS ? LIMIT 1").get(link.target, teamId);
|
|
256
|
+
linkStmt.run(pageId, target?.id ?? null, link.target, link.text, link.type, link.line);
|
|
257
|
+
}
|
|
258
|
+
return {
|
|
259
|
+
file: relPath,
|
|
260
|
+
pages: 1,
|
|
261
|
+
links: parsed.links.length,
|
|
262
|
+
skipped: false
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
function ingestDirectory(db, dirPath, repoPath, teamId, maxFiles = 200) {
|
|
266
|
+
const results = [];
|
|
267
|
+
const files = [];
|
|
268
|
+
const walk = (dir) => {
|
|
269
|
+
if (files.length >= maxFiles) return;
|
|
270
|
+
let entries;
|
|
271
|
+
try {
|
|
272
|
+
entries = readdirSync(dir);
|
|
273
|
+
} catch {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
for (const entry of entries) {
|
|
277
|
+
if (files.length >= maxFiles) return;
|
|
278
|
+
const fullPath = join(dir, entry);
|
|
279
|
+
let stat;
|
|
280
|
+
try {
|
|
281
|
+
stat = statSync(fullPath);
|
|
282
|
+
} catch {
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
if (stat.isDirectory()) {
|
|
286
|
+
if (entry.startsWith(".") || entry === "node_modules" || entry === "vendor") continue;
|
|
287
|
+
walk(fullPath);
|
|
288
|
+
} else if (stat.isFile()) {
|
|
289
|
+
const ext = extname(fullPath).toLowerCase();
|
|
290
|
+
if (ext === ".md" || ext === ".markdown") {
|
|
291
|
+
files.push(fullPath);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
walk(dirPath);
|
|
297
|
+
for (const file of files) {
|
|
298
|
+
const result = ingestFile(db, file, repoPath, teamId);
|
|
299
|
+
results.push(result);
|
|
300
|
+
}
|
|
301
|
+
const unresolved = db.prepare("SELECT id, to_title FROM wiki_links WHERE to_page_id IS NULL").all();
|
|
302
|
+
if (unresolved.length > 0) {
|
|
303
|
+
const updateStmt = db.prepare("UPDATE wiki_links SET to_page_id = ? WHERE id = ?");
|
|
304
|
+
for (const u of unresolved) {
|
|
305
|
+
const target = db.prepare("SELECT id FROM wiki_pages WHERE title = ? AND team_id IS ? LIMIT 1").get(u.to_title, teamId);
|
|
306
|
+
if (target) {
|
|
307
|
+
updateStmt.run(target.id, u.id);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return results;
|
|
312
|
+
}
|
|
313
|
+
function searchWiki(db, query, opts = {}) {
|
|
314
|
+
const limit = opts.limit ?? 10;
|
|
315
|
+
let sql = `
|
|
316
|
+
SELECT w.id, w.title, w.source_file, snippet(wiki_fts, 1, '<b>', '</b>', '...', 20) as snippet
|
|
317
|
+
FROM wiki_fts f
|
|
318
|
+
JOIN wiki_pages w ON w.id = f.id
|
|
319
|
+
WHERE wiki_fts MATCH ?
|
|
320
|
+
`;
|
|
321
|
+
const params = [query];
|
|
322
|
+
if (opts.teamId !== void 0) {
|
|
323
|
+
sql += " AND w.team_id IS ?";
|
|
324
|
+
params.push(opts.teamId);
|
|
325
|
+
}
|
|
326
|
+
sql += " LIMIT ?";
|
|
327
|
+
params.push(limit);
|
|
328
|
+
const rows = db.prepare(sql).all(...params);
|
|
329
|
+
return rows.map((r) => ({
|
|
330
|
+
id: r.id,
|
|
331
|
+
title: r.title,
|
|
332
|
+
sourceFile: r.source_file,
|
|
333
|
+
snippet: r.snippet
|
|
334
|
+
}));
|
|
335
|
+
}
|
|
336
|
+
function getWikiPage(db, pageId) {
|
|
337
|
+
const row = db.prepare("SELECT * FROM wiki_pages WHERE id = ?").get(pageId);
|
|
338
|
+
if (!row) return null;
|
|
339
|
+
const page = {
|
|
340
|
+
id: row.id,
|
|
341
|
+
title: row.title,
|
|
342
|
+
content: row.content,
|
|
343
|
+
sourceFile: row.source_file,
|
|
344
|
+
section: row.section,
|
|
345
|
+
tags: row.tags,
|
|
346
|
+
frontmatter: row.frontmatter,
|
|
347
|
+
contentHash: row.content_hash
|
|
348
|
+
};
|
|
349
|
+
const linkRows = db.prepare("SELECT * FROM wiki_links WHERE from_page_id = ? ORDER BY line").all(pageId);
|
|
350
|
+
const links = linkRows.map((r) => ({
|
|
351
|
+
fromPageId: r.from_page_id,
|
|
352
|
+
toPageId: r.to_page_id,
|
|
353
|
+
toTitle: r.to_title,
|
|
354
|
+
linkText: r.link_text,
|
|
355
|
+
linkType: r.link_type,
|
|
356
|
+
line: r.line
|
|
357
|
+
}));
|
|
358
|
+
const backlinkRows = db.prepare("SELECT * FROM wiki_links WHERE to_page_id = ? ORDER BY line").all(pageId);
|
|
359
|
+
const backlinks = backlinkRows.map((r) => ({
|
|
360
|
+
fromPageId: r.from_page_id,
|
|
361
|
+
toPageId: r.to_page_id,
|
|
362
|
+
toTitle: r.to_title,
|
|
363
|
+
linkText: r.link_text,
|
|
364
|
+
linkType: r.link_type,
|
|
365
|
+
line: r.line
|
|
366
|
+
}));
|
|
367
|
+
return { page, links, backlinks };
|
|
368
|
+
}
|
|
369
|
+
function findOutdatedPages(db, repoPath, opts = {}) {
|
|
370
|
+
const pages = db.prepare("SELECT id, title, source_file, content_hash FROM wiki_pages WHERE team_id IS ?").all(opts.teamId ?? null);
|
|
371
|
+
const outdated = [];
|
|
372
|
+
for (const page of pages) {
|
|
373
|
+
const fullPath = join(repoPath, page.source_file);
|
|
374
|
+
if (!existsSync(fullPath)) {
|
|
375
|
+
outdated.push({
|
|
376
|
+
id: page.id,
|
|
377
|
+
title: page.title,
|
|
378
|
+
sourceFile: page.source_file,
|
|
379
|
+
reason: "file deleted"
|
|
380
|
+
});
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
try {
|
|
384
|
+
const content = readFileSync(fullPath, "utf-8");
|
|
385
|
+
const hash = createHash("sha256").update(content).digest("hex");
|
|
386
|
+
if (hash !== page.content_hash) {
|
|
387
|
+
outdated.push({
|
|
388
|
+
id: page.id,
|
|
389
|
+
title: page.title,
|
|
390
|
+
sourceFile: page.source_file,
|
|
391
|
+
reason: "content changed"
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
} catch {
|
|
395
|
+
outdated.push({
|
|
396
|
+
id: page.id,
|
|
397
|
+
title: page.title,
|
|
398
|
+
sourceFile: page.source_file,
|
|
399
|
+
reason: "read error"
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return outdated;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// src/sdk.ts
|
|
407
|
+
function classifyGlobalContent(content, type) {
|
|
408
|
+
if (type !== "learning" && type !== "decision" && type !== "task") return false;
|
|
409
|
+
const projectSignals = [
|
|
410
|
+
/\b\/[a-z]/i,
|
|
411
|
+
/\b\w+\/\w+\.\w{1,5}\b/,
|
|
412
|
+
/line\s*\d+/i,
|
|
413
|
+
/\b\d{4,}\b/,
|
|
414
|
+
/\bcommit\s+[0-9a-f]{7,}/i,
|
|
415
|
+
/\bsrc\//,
|
|
416
|
+
/\bdist\//,
|
|
417
|
+
/\btests?\//
|
|
418
|
+
];
|
|
419
|
+
for (const sig of projectSignals) {
|
|
420
|
+
if (sig.test(content)) return false;
|
|
421
|
+
}
|
|
422
|
+
const lower = content.toLowerCase();
|
|
423
|
+
const globalSignals = [
|
|
424
|
+
/\balways\s+/i,
|
|
425
|
+
/\bnever\s+/i,
|
|
426
|
+
/\brule\b/i,
|
|
427
|
+
/\bconvention\b/i,
|
|
428
|
+
/\bpattern\b/i,
|
|
429
|
+
/\bprefer\b/i,
|
|
430
|
+
/\bavoid\b/i,
|
|
431
|
+
/\bshould\b/i,
|
|
432
|
+
/\bbest\s+practice\b/i
|
|
433
|
+
];
|
|
434
|
+
let score = 0;
|
|
435
|
+
for (const sig of globalSignals) {
|
|
436
|
+
if (sig.test(lower)) score++;
|
|
437
|
+
}
|
|
438
|
+
if (content.length < 300 && score >= 1) return true;
|
|
439
|
+
if (content.length < 150 && type === "learning") return true;
|
|
440
|
+
return false;
|
|
441
|
+
}
|
|
442
|
+
var Memory = class {
|
|
443
|
+
storage;
|
|
444
|
+
embedder;
|
|
445
|
+
sessionKey;
|
|
446
|
+
globalKey;
|
|
447
|
+
redactSecrets;
|
|
448
|
+
constructor(opts) {
|
|
449
|
+
const dbPath = opts?.dbPath ?? join2(homedir(), ".local", "share", "remem-mcp", "memory.db");
|
|
450
|
+
this.storage = new SQLiteBackend(dbPath);
|
|
451
|
+
this.embedder = new LocalEmbedder();
|
|
452
|
+
this.sessionKey = opts?.sessionKey ?? process.env.REMEM_SESSION_KEY ?? createHash2("sha256").update(process.cwd()).digest("hex").slice(0, 16);
|
|
453
|
+
this.globalKey = opts?.globalSessionKey ?? process.env.REMEM_GLOBAL_SESSION_KEY ?? null;
|
|
454
|
+
this.redactSecrets = opts?.redactSecrets ?? true;
|
|
455
|
+
}
|
|
456
|
+
/** Capture a memory entry. Returns the ID, or null if duplicate. */
|
|
457
|
+
async capture(content, type, tags = [], opts) {
|
|
458
|
+
const { text: redactedContent } = this.redactSecrets ? redact(content) : { text: content };
|
|
459
|
+
let sessionKey = opts?.sessionKey ?? this.sessionKey;
|
|
460
|
+
if (opts?.autoGlobal && !opts?.sessionKey && this.globalKey && this.globalKey !== sessionKey) {
|
|
461
|
+
if (classifyGlobalContent(content, type)) {
|
|
462
|
+
sessionKey = this.globalKey;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
const contentHash = createHash2("sha256").update(redactedContent).digest("hex");
|
|
466
|
+
const existing = await this.storage.findByContentHash(contentHash, sessionKey);
|
|
467
|
+
if (existing.length > 0) return null;
|
|
468
|
+
const id = generateId();
|
|
469
|
+
const entry = {
|
|
470
|
+
id,
|
|
471
|
+
sessionKey,
|
|
472
|
+
agentId: "sdk",
|
|
473
|
+
type,
|
|
474
|
+
content: redactedContent,
|
|
475
|
+
tags,
|
|
476
|
+
createdAt: Date.now()
|
|
477
|
+
};
|
|
478
|
+
await this.storage.put(entry);
|
|
479
|
+
try {
|
|
480
|
+
const embedding = await this.embedder.embed(redactedContent);
|
|
481
|
+
await this.storage.putVector(id, embedding);
|
|
482
|
+
} catch {
|
|
483
|
+
}
|
|
484
|
+
return id;
|
|
485
|
+
}
|
|
486
|
+
/** Recall relevant memory. Searches project + global (if configured). */
|
|
487
|
+
async recall(query, opts) {
|
|
488
|
+
const limit = Math.min(opts?.limit ?? 10, 50);
|
|
489
|
+
const mode = opts?.mode ?? "hybrid";
|
|
490
|
+
const sessionKey = opts?.sessionKey ?? this.sessionKey;
|
|
491
|
+
let queryEmbedding = null;
|
|
492
|
+
if (mode === "hybrid" || mode === "vector") {
|
|
493
|
+
const stripped = stripQueryProperNouns(query);
|
|
494
|
+
queryEmbedding = await this.embedder.embed(stripped || query);
|
|
495
|
+
}
|
|
496
|
+
if (this.globalKey && !opts?.sessionKey && this.globalKey !== sessionKey) {
|
|
497
|
+
const reservedGlobal = Math.min(3, limit);
|
|
498
|
+
const projectLimit = limit - reservedGlobal;
|
|
499
|
+
const projectResults = await this.storage.search(query, queryEmbedding, {
|
|
500
|
+
sessionKey,
|
|
501
|
+
limit: projectLimit,
|
|
502
|
+
offset: 0,
|
|
503
|
+
mode
|
|
504
|
+
});
|
|
505
|
+
let globalResults = [];
|
|
506
|
+
try {
|
|
507
|
+
globalResults = await this.storage.search(query, queryEmbedding, {
|
|
508
|
+
sessionKey: this.globalKey,
|
|
509
|
+
limit: reservedGlobal,
|
|
510
|
+
offset: 0,
|
|
511
|
+
mode
|
|
512
|
+
});
|
|
513
|
+
} catch {
|
|
514
|
+
}
|
|
515
|
+
const seen = new Set(projectResults.map((r) => r.entry.id));
|
|
516
|
+
return [...projectResults, ...globalResults.filter((r) => !seen.has(r.entry.id))].slice(
|
|
517
|
+
0,
|
|
518
|
+
limit
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
return this.storage.search(query, queryEmbedding, {
|
|
522
|
+
sessionKey,
|
|
523
|
+
limit,
|
|
524
|
+
offset: 0,
|
|
525
|
+
mode
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
/** Search with filters. Searches project + global (if configured). */
|
|
529
|
+
async search(query, opts) {
|
|
530
|
+
const limit = Math.min(opts?.limit ?? 20, 100);
|
|
531
|
+
const mode = opts?.mode ?? "hybrid";
|
|
532
|
+
const sessionKey = opts?.sessionKey ?? this.sessionKey;
|
|
533
|
+
let queryEmbedding = null;
|
|
534
|
+
if (mode === "hybrid" || mode === "vector") {
|
|
535
|
+
const stripped = stripQueryProperNouns(query);
|
|
536
|
+
queryEmbedding = await this.embedder.embed(stripped || query);
|
|
537
|
+
}
|
|
538
|
+
if (this.globalKey && !opts?.sessionKey && this.globalKey !== sessionKey) {
|
|
539
|
+
const reservedGlobal = Math.min(3, limit);
|
|
540
|
+
const projectLimit = limit - reservedGlobal;
|
|
541
|
+
const projectResults = await this.storage.search(query, queryEmbedding, {
|
|
542
|
+
sessionKey,
|
|
543
|
+
limit: projectLimit,
|
|
544
|
+
offset: 0,
|
|
545
|
+
mode,
|
|
546
|
+
filters: opts?.filters
|
|
547
|
+
});
|
|
548
|
+
let globalResults = [];
|
|
549
|
+
try {
|
|
550
|
+
globalResults = await this.storage.search(query, queryEmbedding, {
|
|
551
|
+
sessionKey: this.globalKey,
|
|
552
|
+
limit: reservedGlobal,
|
|
553
|
+
offset: 0,
|
|
554
|
+
mode,
|
|
555
|
+
filters: opts?.filters
|
|
556
|
+
});
|
|
557
|
+
} catch {
|
|
558
|
+
}
|
|
559
|
+
const seen = new Set(projectResults.map((r) => r.entry.id));
|
|
560
|
+
return [...projectResults, ...globalResults.filter((r) => !seen.has(r.entry.id))].slice(
|
|
561
|
+
0,
|
|
562
|
+
limit
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
return this.storage.search(query, queryEmbedding, {
|
|
566
|
+
sessionKey,
|
|
567
|
+
limit,
|
|
568
|
+
offset: 0,
|
|
569
|
+
mode,
|
|
570
|
+
filters: opts?.filters
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
/** Compute the embedding vector for a piece of text using the configured embedder. */
|
|
574
|
+
async embed(text) {
|
|
575
|
+
return this.embedder.embed(text);
|
|
576
|
+
}
|
|
577
|
+
/** Delete a capture by ID. */
|
|
578
|
+
async forget(id) {
|
|
579
|
+
return this.storage.delete(id);
|
|
580
|
+
}
|
|
581
|
+
/** Update an existing capture's content, tags, type, or trust state. */
|
|
582
|
+
async update(id, opts) {
|
|
583
|
+
const existing = await this.storage.get(id);
|
|
584
|
+
if (!existing) return false;
|
|
585
|
+
const newContent = opts.content ?? existing.content;
|
|
586
|
+
const newTags = opts.tags ?? existing.tags;
|
|
587
|
+
const newType = opts.type ?? existing.type;
|
|
588
|
+
const newTrust = opts.verified ? "verified" : existing.trustState ?? "candidate";
|
|
589
|
+
const contentHash = createHash2("sha256").update(newContent).digest("hex");
|
|
590
|
+
const db = this.storage.getDatabase();
|
|
591
|
+
db.prepare(
|
|
592
|
+
"UPDATE captures SET content = ?, tags = ?, type = ?, trust_state = ?, content_hash = ? WHERE id = ?"
|
|
593
|
+
).run(newContent, JSON.stringify(newTags), newType, newTrust, contentHash, id);
|
|
594
|
+
const rowid = db.prepare("SELECT rowid FROM captures WHERE id = ?").get(id)?.rowid;
|
|
595
|
+
if (rowid) {
|
|
596
|
+
db.prepare(
|
|
597
|
+
"INSERT INTO captures_fts(captures_fts, rowid, content, tags, type) VALUES('delete', ?, '', '', '')"
|
|
598
|
+
).run(rowid);
|
|
599
|
+
db.prepare(
|
|
600
|
+
"INSERT INTO captures_fts (rowid, id, content, tags, type) VALUES (?, ?, ?, ?, ?)"
|
|
601
|
+
).run(rowid, id, newContent, JSON.stringify(newTags), newType);
|
|
602
|
+
}
|
|
603
|
+
if (opts.content && opts.content !== existing.content) {
|
|
604
|
+
try {
|
|
605
|
+
const embedding = await this.embedder.embed(newContent);
|
|
606
|
+
await this.storage.putVector(id, embedding);
|
|
607
|
+
} catch {
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
return true;
|
|
611
|
+
}
|
|
612
|
+
/** Find and optionally merge duplicate captures by content similarity (Jaccard). */
|
|
613
|
+
async consolidate(opts) {
|
|
614
|
+
const threshold = opts.threshold ?? 0.75;
|
|
615
|
+
const confirm = opts.confirm ?? false;
|
|
616
|
+
const sessionKey = opts.sessionKey ?? this.sessionKey;
|
|
617
|
+
const db = this.storage.getDatabase();
|
|
618
|
+
let sql = "SELECT id, content, type, tags, created_at FROM captures WHERE deleted_at IS NULL";
|
|
619
|
+
const params = [];
|
|
620
|
+
if (sessionKey !== "all") {
|
|
621
|
+
sql += " AND session_key = ?";
|
|
622
|
+
params.push(sessionKey);
|
|
623
|
+
}
|
|
624
|
+
sql += " ORDER BY created_at DESC";
|
|
625
|
+
const rows = db.prepare(sql).all(...params);
|
|
626
|
+
if (rows.length < 2) return { groups: [], merged: 0 };
|
|
627
|
+
const groups = [];
|
|
628
|
+
const seen = /* @__PURE__ */ new Set();
|
|
629
|
+
for (let i = 0; i < rows.length; i++) {
|
|
630
|
+
if (seen.has(rows[i].id)) continue;
|
|
631
|
+
const words1 = new Set(rows[i].content.toLowerCase().split(/\s+/));
|
|
632
|
+
const group = [rows[i].id];
|
|
633
|
+
for (let j = i + 1; j < rows.length; j++) {
|
|
634
|
+
if (seen.has(rows[j].id)) continue;
|
|
635
|
+
const words2 = new Set(rows[j].content.toLowerCase().split(/\s+/));
|
|
636
|
+
const intersection = [...words1].filter((w) => words2.has(w)).length;
|
|
637
|
+
const union = (/* @__PURE__ */ new Set([...words1, ...words2])).size;
|
|
638
|
+
const sim = union > 0 ? intersection / union : 0;
|
|
639
|
+
if (sim >= threshold) {
|
|
640
|
+
group.push(rows[j].id);
|
|
641
|
+
seen.add(rows[j].id);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
if (group.length > 1) {
|
|
645
|
+
seen.add(rows[i].id);
|
|
646
|
+
groups.push({
|
|
647
|
+
ids: group,
|
|
648
|
+
similarity: threshold,
|
|
649
|
+
preview: rows[i].content.slice(0, 80).replace(/\n/g, " ")
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
if (!confirm || groups.length === 0) {
|
|
654
|
+
return { groups, merged: 0 };
|
|
655
|
+
}
|
|
656
|
+
let merged = 0;
|
|
657
|
+
const softDelete = db.prepare("UPDATE captures SET deleted_at = ? WHERE id = ?");
|
|
658
|
+
const deleteVec = db.prepare("DELETE FROM captures_vec WHERE id = ?");
|
|
659
|
+
const deleteAtoms = db.prepare("DELETE FROM atoms WHERE capture_id = ?");
|
|
660
|
+
const mergeTx = db.transaction(() => {
|
|
661
|
+
for (const g of groups) {
|
|
662
|
+
const groupRows = g.ids.map((id) => rows.find((r) => r.id === id)).filter(Boolean).sort((a, b) => a.created_at - b.created_at);
|
|
663
|
+
const dups = groupRows.slice(1);
|
|
664
|
+
for (const dup of dups) {
|
|
665
|
+
softDelete.run(Date.now(), dup.id);
|
|
666
|
+
deleteVec.run(dup.id);
|
|
667
|
+
deleteAtoms.run(dup.id);
|
|
668
|
+
merged++;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
});
|
|
672
|
+
mergeTx();
|
|
673
|
+
return { groups, merged };
|
|
674
|
+
}
|
|
675
|
+
/** Create a handoff packet for the next agent session. */
|
|
676
|
+
async handoff(opts) {
|
|
677
|
+
const lines = [];
|
|
678
|
+
lines.push(`# Handoff: ${opts.task}`);
|
|
679
|
+
lines.push(`Status: ${opts.status}`);
|
|
680
|
+
lines.push(`Date: ${(/* @__PURE__ */ new Date()).toISOString()}`);
|
|
681
|
+
lines.push("");
|
|
682
|
+
lines.push("## Progress");
|
|
683
|
+
lines.push(opts.progress);
|
|
684
|
+
lines.push("");
|
|
685
|
+
if (opts.decisions && opts.decisions.length > 0) {
|
|
686
|
+
lines.push("## Decisions");
|
|
687
|
+
for (const d of opts.decisions) lines.push(`- ${d}`);
|
|
688
|
+
lines.push("");
|
|
689
|
+
}
|
|
690
|
+
if (opts.files && opts.files.length > 0) {
|
|
691
|
+
lines.push("## Files");
|
|
692
|
+
for (const f of opts.files) lines.push(`- ${f}`);
|
|
693
|
+
lines.push("");
|
|
694
|
+
}
|
|
695
|
+
if (opts.nextSteps && opts.nextSteps.length > 0) {
|
|
696
|
+
lines.push("## Next steps");
|
|
697
|
+
opts.nextSteps.forEach((s, i) => {
|
|
698
|
+
lines.push(`${i + 1}. ${s}`);
|
|
699
|
+
});
|
|
700
|
+
lines.push("");
|
|
701
|
+
}
|
|
702
|
+
const content = lines.join("\n");
|
|
703
|
+
const dedupPayload = JSON.stringify({
|
|
704
|
+
task: opts.task,
|
|
705
|
+
status: opts.status,
|
|
706
|
+
progress: opts.progress,
|
|
707
|
+
decisions: opts.decisions ?? [],
|
|
708
|
+
files: opts.files ?? [],
|
|
709
|
+
nextSteps: opts.nextSteps ?? []
|
|
710
|
+
});
|
|
711
|
+
const contentHash = createHash2("sha256").update(dedupPayload).digest("hex");
|
|
712
|
+
const existing = await this.storage.findByContentHash(contentHash, this.sessionKey);
|
|
713
|
+
if (existing.length > 0) return null;
|
|
714
|
+
const id = generateId();
|
|
715
|
+
const entry = {
|
|
716
|
+
id,
|
|
717
|
+
sessionKey: this.sessionKey,
|
|
718
|
+
agentId: "sdk",
|
|
719
|
+
type: "task",
|
|
720
|
+
content,
|
|
721
|
+
tags: ["handoff", `status:${opts.status}`],
|
|
722
|
+
createdAt: Date.now(),
|
|
723
|
+
metadata: {
|
|
724
|
+
handoff: true,
|
|
725
|
+
task: opts.task,
|
|
726
|
+
status: opts.status,
|
|
727
|
+
progress: opts.progress,
|
|
728
|
+
decisions: opts.decisions ?? [],
|
|
729
|
+
files: opts.files ?? [],
|
|
730
|
+
nextSteps: opts.nextSteps ?? []
|
|
731
|
+
},
|
|
732
|
+
contentHash
|
|
733
|
+
};
|
|
734
|
+
await this.storage.put(entry);
|
|
735
|
+
try {
|
|
736
|
+
const embedding = await this.embedder.embed(content);
|
|
737
|
+
await this.storage.putVector(id, embedding);
|
|
738
|
+
} catch {
|
|
739
|
+
}
|
|
740
|
+
return id;
|
|
741
|
+
}
|
|
742
|
+
/** Record an Architecture Decision Record (ADR). */
|
|
743
|
+
async adr(opts) {
|
|
744
|
+
const lines = [];
|
|
745
|
+
lines.push(`# ADR: ${opts.title}`);
|
|
746
|
+
lines.push(`Date: ${(/* @__PURE__ */ new Date()).toISOString()}`);
|
|
747
|
+
lines.push("");
|
|
748
|
+
lines.push("## Context");
|
|
749
|
+
lines.push(opts.context);
|
|
750
|
+
lines.push("");
|
|
751
|
+
lines.push("## Decision");
|
|
752
|
+
lines.push(opts.decision);
|
|
753
|
+
lines.push("");
|
|
754
|
+
const alternatives = Array.isArray(opts.alternatives) ? opts.alternatives : typeof opts.alternatives === "string" && opts.alternatives ? [opts.alternatives] : [];
|
|
755
|
+
if (alternatives.length > 0) {
|
|
756
|
+
lines.push("## Alternatives considered");
|
|
757
|
+
for (const alt of alternatives) lines.push(`- ${alt}`);
|
|
758
|
+
lines.push("");
|
|
759
|
+
}
|
|
760
|
+
if (opts.consequences) {
|
|
761
|
+
lines.push("## Consequences");
|
|
762
|
+
lines.push(opts.consequences);
|
|
763
|
+
lines.push("");
|
|
764
|
+
}
|
|
765
|
+
const content = lines.join("\n");
|
|
766
|
+
const dedupPayload = JSON.stringify({
|
|
767
|
+
title: opts.title,
|
|
768
|
+
context: opts.context,
|
|
769
|
+
decision: opts.decision,
|
|
770
|
+
alternatives,
|
|
771
|
+
consequences: opts.consequences ?? ""
|
|
772
|
+
});
|
|
773
|
+
const contentHash = createHash2("sha256").update(dedupPayload).digest("hex");
|
|
774
|
+
const existing = await this.storage.findByContentHash(contentHash, this.sessionKey);
|
|
775
|
+
if (existing.length > 0) return null;
|
|
776
|
+
const id = generateId();
|
|
777
|
+
const entry = {
|
|
778
|
+
id,
|
|
779
|
+
sessionKey: this.sessionKey,
|
|
780
|
+
agentId: "sdk",
|
|
781
|
+
type: "decision",
|
|
782
|
+
content,
|
|
783
|
+
tags: ["adr", ...opts.tags ?? []],
|
|
784
|
+
createdAt: Date.now(),
|
|
785
|
+
metadata: {
|
|
786
|
+
adr: true,
|
|
787
|
+
title: opts.title,
|
|
788
|
+
context: opts.context,
|
|
789
|
+
decision: opts.decision,
|
|
790
|
+
alternatives,
|
|
791
|
+
consequences: opts.consequences ?? ""
|
|
792
|
+
},
|
|
793
|
+
contentHash
|
|
794
|
+
};
|
|
795
|
+
await this.storage.put(entry);
|
|
796
|
+
try {
|
|
797
|
+
const embedding = await this.embedder.embed(content);
|
|
798
|
+
await this.storage.putVector(id, embedding);
|
|
799
|
+
} catch {
|
|
800
|
+
}
|
|
801
|
+
return id;
|
|
802
|
+
}
|
|
803
|
+
/** Close the database connection. */
|
|
804
|
+
close() {
|
|
805
|
+
this.storage.close();
|
|
806
|
+
}
|
|
807
|
+
};
|
|
808
|
+
|
|
809
|
+
export {
|
|
810
|
+
redact,
|
|
811
|
+
ingestFile,
|
|
812
|
+
ingestDirectory,
|
|
813
|
+
searchWiki,
|
|
814
|
+
getWikiPage,
|
|
815
|
+
findOutdatedPages,
|
|
816
|
+
classifyGlobalContent,
|
|
817
|
+
Memory
|
|
818
|
+
};
|
|
819
|
+
//# sourceMappingURL=chunk-TGLOMFIE.js.map
|