@memextend/cursor 0.1.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 +411 -0
- package/dist/cli/capture.cjs +776 -0
- package/dist/cli/capture.d.ts +2 -0
- package/dist/cli/capture.d.ts.map +1 -0
- package/dist/cli/capture.js +222 -0
- package/dist/cli/capture.js.map +1 -0
- package/dist/cli/index.cjs +245 -0
- package/dist/cli/index.d.ts +4 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +252 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/inject.cjs +1029 -0
- package/dist/cli/inject.d.ts +2 -0
- package/dist/cli/inject.d.ts.map +1 -0
- package/dist/cli/inject.js +279 -0
- package/dist/cli/inject.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/index.d.ts +13 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +15 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/server.cjs +14865 -0
- package/dist/mcp/server.d.ts +2 -0
- package/dist/mcp/server.d.ts.map +1 -0
- package/dist/mcp/server.js +384 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/utils/index.d.ts +44 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +123 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +65 -0
- package/scripts/build-cli.js +59 -0
- package/scripts/build-mcp.js +46 -0
- package/scripts/postinstall.js +57 -0
|
@@ -0,0 +1,776 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
18
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
19
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
20
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
21
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
+
mod
|
|
24
|
+
));
|
|
25
|
+
|
|
26
|
+
// src/cli/capture.ts
|
|
27
|
+
var import_crypto = require("crypto");
|
|
28
|
+
var import_fs2 = require("fs");
|
|
29
|
+
var import_promises3 = require("fs/promises");
|
|
30
|
+
var import_path2 = require("path");
|
|
31
|
+
var import_os = require("os");
|
|
32
|
+
var import_child_process = require("child_process");
|
|
33
|
+
|
|
34
|
+
// ../../core/dist/storage/sqlite.js
|
|
35
|
+
var import_better_sqlite3 = __toESM(require("better-sqlite3"), 1);
|
|
36
|
+
var SQLiteStorage = class {
|
|
37
|
+
db;
|
|
38
|
+
constructor(dbPath) {
|
|
39
|
+
this.db = new import_better_sqlite3.default(dbPath);
|
|
40
|
+
this.db.pragma("journal_mode = WAL");
|
|
41
|
+
this.initialize();
|
|
42
|
+
}
|
|
43
|
+
initialize() {
|
|
44
|
+
this.db.exec(`
|
|
45
|
+
CREATE TABLE IF NOT EXISTS memories (
|
|
46
|
+
id TEXT PRIMARY KEY,
|
|
47
|
+
project_id TEXT,
|
|
48
|
+
content TEXT NOT NULL,
|
|
49
|
+
type TEXT NOT NULL,
|
|
50
|
+
source_tool TEXT,
|
|
51
|
+
created_at TEXT NOT NULL,
|
|
52
|
+
session_id TEXT,
|
|
53
|
+
metadata TEXT
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
CREATE TABLE IF NOT EXISTS projects (
|
|
57
|
+
id TEXT PRIMARY KEY,
|
|
58
|
+
name TEXT NOT NULL,
|
|
59
|
+
path TEXT NOT NULL,
|
|
60
|
+
created_at TEXT NOT NULL
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
CREATE TABLE IF NOT EXISTS global_profile (
|
|
64
|
+
id TEXT PRIMARY KEY,
|
|
65
|
+
key TEXT NOT NULL,
|
|
66
|
+
content TEXT NOT NULL,
|
|
67
|
+
created_at TEXT NOT NULL
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
71
|
+
content,
|
|
72
|
+
content='memories',
|
|
73
|
+
content_rowid='rowid'
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
CREATE TRIGGER IF NOT EXISTS memories_ai AFTER INSERT ON memories BEGIN
|
|
77
|
+
INSERT INTO memories_fts(rowid, content) VALUES (NEW.rowid, NEW.content);
|
|
78
|
+
END;
|
|
79
|
+
|
|
80
|
+
CREATE TRIGGER IF NOT EXISTS memories_ad AFTER DELETE ON memories BEGIN
|
|
81
|
+
INSERT INTO memories_fts(memories_fts, rowid, content) VALUES('delete', OLD.rowid, OLD.content);
|
|
82
|
+
END;
|
|
83
|
+
|
|
84
|
+
CREATE TRIGGER IF NOT EXISTS memories_au AFTER UPDATE ON memories BEGIN
|
|
85
|
+
INSERT INTO memories_fts(memories_fts, rowid, content) VALUES('delete', OLD.rowid, OLD.content);
|
|
86
|
+
INSERT INTO memories_fts(rowid, content) VALUES (NEW.rowid, NEW.content);
|
|
87
|
+
END;
|
|
88
|
+
`);
|
|
89
|
+
}
|
|
90
|
+
getTables() {
|
|
91
|
+
const rows = this.db.prepare(`
|
|
92
|
+
SELECT name FROM sqlite_master
|
|
93
|
+
WHERE type='table' OR type='virtual table'
|
|
94
|
+
`).all();
|
|
95
|
+
return rows.map((r) => r.name);
|
|
96
|
+
}
|
|
97
|
+
insertMemory(memory) {
|
|
98
|
+
const stmt = this.db.prepare(`
|
|
99
|
+
INSERT INTO memories (id, project_id, content, type, source_tool, created_at, session_id, metadata)
|
|
100
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
101
|
+
`);
|
|
102
|
+
stmt.run(memory.id, memory.projectId, memory.content, memory.type, memory.sourceTool, memory.createdAt, memory.sessionId, memory.metadata ? JSON.stringify(memory.metadata) : null);
|
|
103
|
+
}
|
|
104
|
+
getMemory(id) {
|
|
105
|
+
const row = this.db.prepare("SELECT * FROM memories WHERE id = ?").get(id);
|
|
106
|
+
if (!row)
|
|
107
|
+
return null;
|
|
108
|
+
return this.rowToMemory(row);
|
|
109
|
+
}
|
|
110
|
+
getAllMemories(projectId, limit = 100) {
|
|
111
|
+
let query = "SELECT * FROM memories";
|
|
112
|
+
const params = [];
|
|
113
|
+
if (projectId) {
|
|
114
|
+
query += " WHERE project_id = ?";
|
|
115
|
+
params.push(projectId);
|
|
116
|
+
}
|
|
117
|
+
query += " ORDER BY created_at DESC LIMIT ?";
|
|
118
|
+
params.push(limit);
|
|
119
|
+
const rows = this.db.prepare(query).all(...params);
|
|
120
|
+
return rows.map((row) => this.rowToMemory(row));
|
|
121
|
+
}
|
|
122
|
+
searchFTS(query, limit = 10) {
|
|
123
|
+
const rows = this.db.prepare(`
|
|
124
|
+
SELECT m.*, bm25(memories_fts) as score
|
|
125
|
+
FROM memories_fts fts
|
|
126
|
+
JOIN memories m ON m.rowid = fts.rowid
|
|
127
|
+
WHERE memories_fts MATCH ?
|
|
128
|
+
ORDER BY score
|
|
129
|
+
LIMIT ?
|
|
130
|
+
`).all(query, limit);
|
|
131
|
+
return rows.map((row) => ({
|
|
132
|
+
memory: this.rowToMemory(row),
|
|
133
|
+
score: Math.abs(row.score),
|
|
134
|
+
source: "fts"
|
|
135
|
+
}));
|
|
136
|
+
}
|
|
137
|
+
getRecentMemories(projectId, limit = 0, days = 0) {
|
|
138
|
+
let query = "SELECT * FROM memories";
|
|
139
|
+
const params = [];
|
|
140
|
+
const conditions = [];
|
|
141
|
+
if (days > 0) {
|
|
142
|
+
const cutoff = /* @__PURE__ */ new Date();
|
|
143
|
+
cutoff.setDate(cutoff.getDate() - days);
|
|
144
|
+
conditions.push("created_at > ?");
|
|
145
|
+
params.push(cutoff.toISOString());
|
|
146
|
+
}
|
|
147
|
+
if (projectId !== null) {
|
|
148
|
+
conditions.push("project_id = ?");
|
|
149
|
+
params.push(projectId);
|
|
150
|
+
}
|
|
151
|
+
if (conditions.length > 0) {
|
|
152
|
+
query += " WHERE " + conditions.join(" AND ");
|
|
153
|
+
}
|
|
154
|
+
query += " ORDER BY created_at DESC";
|
|
155
|
+
if (limit > 0) {
|
|
156
|
+
query += " LIMIT ?";
|
|
157
|
+
params.push(limit);
|
|
158
|
+
}
|
|
159
|
+
const rows = this.db.prepare(query).all(...params);
|
|
160
|
+
return rows.map((row) => this.rowToMemory(row));
|
|
161
|
+
}
|
|
162
|
+
deleteMemory(id) {
|
|
163
|
+
const result = this.db.prepare("DELETE FROM memories WHERE id = ?").run(id);
|
|
164
|
+
return result.changes > 0;
|
|
165
|
+
}
|
|
166
|
+
updateMemory(id, content) {
|
|
167
|
+
const result = this.db.prepare("UPDATE memories SET content = ? WHERE id = ?").run(content, id);
|
|
168
|
+
return result.changes > 0;
|
|
169
|
+
}
|
|
170
|
+
deleteAllMemories(projectId) {
|
|
171
|
+
if (projectId) {
|
|
172
|
+
const result = this.db.prepare("DELETE FROM memories WHERE project_id = ?").run(projectId);
|
|
173
|
+
return result.changes;
|
|
174
|
+
} else {
|
|
175
|
+
const result = this.db.prepare("DELETE FROM memories").run();
|
|
176
|
+
return result.changes;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
deleteMemoriesBefore(date, projectId) {
|
|
180
|
+
const timestamp = date.toISOString();
|
|
181
|
+
if (projectId) {
|
|
182
|
+
const result = this.db.prepare("DELETE FROM memories WHERE created_at < ? AND project_id = ?").run(timestamp, projectId);
|
|
183
|
+
return result.changes;
|
|
184
|
+
} else {
|
|
185
|
+
const result = this.db.prepare("DELETE FROM memories WHERE created_at < ?").run(timestamp);
|
|
186
|
+
return result.changes;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
// Project methods
|
|
190
|
+
insertProject(project) {
|
|
191
|
+
const stmt = this.db.prepare(`
|
|
192
|
+
INSERT OR REPLACE INTO projects (id, name, path, created_at)
|
|
193
|
+
VALUES (?, ?, ?, ?)
|
|
194
|
+
`);
|
|
195
|
+
stmt.run(project.id, project.name, project.path, project.createdAt);
|
|
196
|
+
}
|
|
197
|
+
getProject(id) {
|
|
198
|
+
const row = this.db.prepare("SELECT * FROM projects WHERE id = ?").get(id);
|
|
199
|
+
if (!row)
|
|
200
|
+
return null;
|
|
201
|
+
return {
|
|
202
|
+
id: row.id,
|
|
203
|
+
name: row.name,
|
|
204
|
+
path: row.path,
|
|
205
|
+
createdAt: row.created_at
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Find a project by name (case-insensitive).
|
|
210
|
+
* Returns the first matching project, preferring projects with a path set.
|
|
211
|
+
*/
|
|
212
|
+
getProjectByName(name) {
|
|
213
|
+
const rowWithPath = this.db.prepare("SELECT * FROM projects WHERE LOWER(name) = LOWER(?) AND path != '' ORDER BY created_at ASC LIMIT 1").get(name);
|
|
214
|
+
if (rowWithPath) {
|
|
215
|
+
return {
|
|
216
|
+
id: rowWithPath.id,
|
|
217
|
+
name: rowWithPath.name,
|
|
218
|
+
path: rowWithPath.path,
|
|
219
|
+
createdAt: rowWithPath.created_at
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
const row = this.db.prepare("SELECT * FROM projects WHERE LOWER(name) = LOWER(?) ORDER BY created_at ASC LIMIT 1").get(name);
|
|
223
|
+
if (!row)
|
|
224
|
+
return null;
|
|
225
|
+
return {
|
|
226
|
+
id: row.id,
|
|
227
|
+
name: row.name,
|
|
228
|
+
path: row.path,
|
|
229
|
+
createdAt: row.created_at
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Delete a project and all its memories.
|
|
234
|
+
* @returns Object with counts of deleted project and memories
|
|
235
|
+
*/
|
|
236
|
+
deleteProject(projectId) {
|
|
237
|
+
const memoriesResult = this.db.prepare("DELETE FROM memories WHERE project_id = ?").run(projectId);
|
|
238
|
+
const projectResult = this.db.prepare("DELETE FROM projects WHERE id = ?").run(projectId);
|
|
239
|
+
return {
|
|
240
|
+
projectDeleted: projectResult.changes > 0,
|
|
241
|
+
memoriesDeleted: memoriesResult.changes
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Get all projects.
|
|
246
|
+
*/
|
|
247
|
+
getAllProjects() {
|
|
248
|
+
const rows = this.db.prepare("SELECT * FROM projects ORDER BY name ASC").all();
|
|
249
|
+
return rows.map((row) => ({
|
|
250
|
+
id: row.id,
|
|
251
|
+
name: row.name,
|
|
252
|
+
path: row.path,
|
|
253
|
+
createdAt: row.created_at
|
|
254
|
+
}));
|
|
255
|
+
}
|
|
256
|
+
// Global profile methods
|
|
257
|
+
insertGlobalProfile(profile) {
|
|
258
|
+
const stmt = this.db.prepare(`
|
|
259
|
+
INSERT INTO global_profile (id, key, content, created_at)
|
|
260
|
+
VALUES (?, ?, ?, ?)
|
|
261
|
+
`);
|
|
262
|
+
stmt.run(profile.id, profile.key, profile.content, profile.createdAt);
|
|
263
|
+
}
|
|
264
|
+
getGlobalProfiles(limit = 10) {
|
|
265
|
+
const rows = this.db.prepare(`
|
|
266
|
+
SELECT * FROM global_profile ORDER BY created_at DESC LIMIT ?
|
|
267
|
+
`).all(limit);
|
|
268
|
+
return rows.map((row) => ({
|
|
269
|
+
id: row.id,
|
|
270
|
+
key: row.key,
|
|
271
|
+
content: row.content,
|
|
272
|
+
createdAt: row.created_at
|
|
273
|
+
}));
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Delete a specific global profile by ID.
|
|
277
|
+
*/
|
|
278
|
+
deleteGlobalProfile(id) {
|
|
279
|
+
const result = this.db.prepare("DELETE FROM global_profile WHERE id = ?").run(id);
|
|
280
|
+
return result.changes > 0;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Delete all global profiles.
|
|
284
|
+
* @returns Number of profiles deleted
|
|
285
|
+
*/
|
|
286
|
+
deleteAllGlobalProfiles() {
|
|
287
|
+
const result = this.db.prepare("DELETE FROM global_profile").run();
|
|
288
|
+
return result.changes;
|
|
289
|
+
}
|
|
290
|
+
getMemoryCount() {
|
|
291
|
+
const row = this.db.prepare("SELECT COUNT(*) as count FROM memories").get();
|
|
292
|
+
return row.count;
|
|
293
|
+
}
|
|
294
|
+
getMemoryCountByProject(projectId) {
|
|
295
|
+
const row = this.db.prepare("SELECT COUNT(*) as count FROM memories WHERE project_id = ?").get(projectId);
|
|
296
|
+
return row.count;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Get IDs of oldest memories for a project, exceeding the limit
|
|
300
|
+
* @returns Array of memory IDs to delete (oldest first)
|
|
301
|
+
*/
|
|
302
|
+
getOldestMemoryIds(projectId, limit) {
|
|
303
|
+
let query;
|
|
304
|
+
let params;
|
|
305
|
+
if (projectId) {
|
|
306
|
+
query = `
|
|
307
|
+
SELECT id FROM memories
|
|
308
|
+
WHERE project_id = ?
|
|
309
|
+
ORDER BY created_at ASC
|
|
310
|
+
LIMIT ?
|
|
311
|
+
`;
|
|
312
|
+
params = [projectId, limit];
|
|
313
|
+
} else {
|
|
314
|
+
query = `
|
|
315
|
+
SELECT id FROM memories
|
|
316
|
+
ORDER BY created_at ASC
|
|
317
|
+
LIMIT ?
|
|
318
|
+
`;
|
|
319
|
+
params = [limit];
|
|
320
|
+
}
|
|
321
|
+
const rows = this.db.prepare(query).all(...params);
|
|
322
|
+
return rows.map((row) => row.id);
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Prune memories to stay within limits
|
|
326
|
+
* @returns Number of memories deleted
|
|
327
|
+
*/
|
|
328
|
+
pruneMemories(options) {
|
|
329
|
+
const deletedIds = [];
|
|
330
|
+
if (options.maxPerProject && options.maxPerProject > 0 && options.projectId) {
|
|
331
|
+
const count = this.getMemoryCountByProject(options.projectId);
|
|
332
|
+
if (count > options.maxPerProject) {
|
|
333
|
+
const excess = count - options.maxPerProject;
|
|
334
|
+
const idsToDelete = this.getOldestMemoryIds(options.projectId, excess);
|
|
335
|
+
for (const id of idsToDelete) {
|
|
336
|
+
this.deleteMemory(id);
|
|
337
|
+
deletedIds.push(id);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
if (options.maxTotal && options.maxTotal > 0) {
|
|
342
|
+
const count = this.getMemoryCount();
|
|
343
|
+
if (count > options.maxTotal) {
|
|
344
|
+
const excess = count - options.maxTotal;
|
|
345
|
+
const idsToDelete = this.getOldestMemoryIds(null, excess);
|
|
346
|
+
for (const id of idsToDelete) {
|
|
347
|
+
if (!deletedIds.includes(id)) {
|
|
348
|
+
this.deleteMemory(id);
|
|
349
|
+
deletedIds.push(id);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return { deleted: deletedIds.length, deletedIds };
|
|
355
|
+
}
|
|
356
|
+
rowToMemory(row) {
|
|
357
|
+
return {
|
|
358
|
+
id: row.id,
|
|
359
|
+
projectId: row.project_id,
|
|
360
|
+
content: row.content,
|
|
361
|
+
type: row.type,
|
|
362
|
+
sourceTool: row.source_tool,
|
|
363
|
+
createdAt: row.created_at,
|
|
364
|
+
sessionId: row.session_id,
|
|
365
|
+
metadata: row.metadata ? JSON.parse(row.metadata) : null
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
close() {
|
|
369
|
+
this.db.close();
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
// ../../core/dist/storage/lancedb.js
|
|
374
|
+
var lancedb = __toESM(require("@lancedb/lancedb"), 1);
|
|
375
|
+
var LanceDBStorage = class _LanceDBStorage {
|
|
376
|
+
db;
|
|
377
|
+
table = null;
|
|
378
|
+
tableName = "memories";
|
|
379
|
+
dimensions = 384;
|
|
380
|
+
constructor(db) {
|
|
381
|
+
this.db = db;
|
|
382
|
+
}
|
|
383
|
+
static async create(dbPath) {
|
|
384
|
+
const db = await lancedb.connect(dbPath);
|
|
385
|
+
const storage = new _LanceDBStorage(db);
|
|
386
|
+
await storage.initialize();
|
|
387
|
+
return storage;
|
|
388
|
+
}
|
|
389
|
+
async initialize() {
|
|
390
|
+
const tableNames = await this.db.tableNames();
|
|
391
|
+
if (tableNames.includes(this.tableName)) {
|
|
392
|
+
this.table = await this.db.openTable(this.tableName);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
async insertVector(id, vector) {
|
|
396
|
+
if (vector.length !== this.dimensions) {
|
|
397
|
+
throw new Error(`Vector must have ${this.dimensions} dimensions, got ${vector.length}`);
|
|
398
|
+
}
|
|
399
|
+
const data = [{ id, vector }];
|
|
400
|
+
if (!this.table) {
|
|
401
|
+
this.table = await this.db.createTable(this.tableName, data);
|
|
402
|
+
} else {
|
|
403
|
+
await this.table.add(data);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
async insertVectors(items) {
|
|
407
|
+
for (const item of items) {
|
|
408
|
+
if (item.vector.length !== this.dimensions) {
|
|
409
|
+
throw new Error(`Vector must have ${this.dimensions} dimensions, got ${item.vector.length}`);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
if (!this.table) {
|
|
413
|
+
this.table = await this.db.createTable(this.tableName, items);
|
|
414
|
+
} else {
|
|
415
|
+
await this.table.add(items);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
async search(vector, limit = 10) {
|
|
419
|
+
if (!this.table) {
|
|
420
|
+
return [];
|
|
421
|
+
}
|
|
422
|
+
const effectiveLimit = limit > 0 ? limit : 100;
|
|
423
|
+
const results = await this.table.vectorSearch(vector).limit(effectiveLimit).toArray();
|
|
424
|
+
return results.map((row) => ({
|
|
425
|
+
id: row.id,
|
|
426
|
+
score: 1 - row._distance
|
|
427
|
+
// Convert distance to similarity
|
|
428
|
+
}));
|
|
429
|
+
}
|
|
430
|
+
async deleteVector(id) {
|
|
431
|
+
if (!this.table)
|
|
432
|
+
return;
|
|
433
|
+
const sanitizedId = id.replace(/'/g, "''");
|
|
434
|
+
await this.table.delete(`id = '${sanitizedId}'`);
|
|
435
|
+
}
|
|
436
|
+
async getVectorCount() {
|
|
437
|
+
if (!this.table)
|
|
438
|
+
return 0;
|
|
439
|
+
return await this.table.countRows();
|
|
440
|
+
}
|
|
441
|
+
async getVectorsByIds(ids) {
|
|
442
|
+
const result = /* @__PURE__ */ new Map();
|
|
443
|
+
if (!this.table || ids.length === 0)
|
|
444
|
+
return result;
|
|
445
|
+
const BATCH_SIZE = 100;
|
|
446
|
+
try {
|
|
447
|
+
for (let i = 0; i < ids.length; i += BATCH_SIZE) {
|
|
448
|
+
const batch = ids.slice(i, i + BATCH_SIZE);
|
|
449
|
+
const sanitizedIds = batch.map((id) => id.replace(/'/g, "''"));
|
|
450
|
+
const filter = sanitizedIds.map((id) => `id = '${id}'`).join(" OR ");
|
|
451
|
+
const rows = await this.table.query().where(filter).toArray();
|
|
452
|
+
for (const row of rows) {
|
|
453
|
+
result.set(row.id, row.vector);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
} catch {
|
|
457
|
+
}
|
|
458
|
+
return result;
|
|
459
|
+
}
|
|
460
|
+
async close() {
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
// ../../core/dist/embedding/local.js
|
|
465
|
+
var import_path = require("path");
|
|
466
|
+
var import_fs = require("fs");
|
|
467
|
+
var import_promises = require("fs/promises");
|
|
468
|
+
var import_promises2 = require("stream/promises");
|
|
469
|
+
var MODEL_URL = "https://huggingface.co/nomic-ai/nomic-embed-text-v1.5-GGUF/resolve/main/nomic-embed-text-v1.5.Q8_0.gguf";
|
|
470
|
+
var MODEL_FILENAME = "nomic-embed-text-v1.5.Q8_0.gguf";
|
|
471
|
+
var LocalEmbedding = class _LocalEmbedding {
|
|
472
|
+
// Using any to avoid ESM/CJS type issues with dynamic imports
|
|
473
|
+
model;
|
|
474
|
+
context;
|
|
475
|
+
dimensions;
|
|
476
|
+
constructor(model, context, dimensions) {
|
|
477
|
+
this.model = model;
|
|
478
|
+
this.context = context;
|
|
479
|
+
this.dimensions = dimensions;
|
|
480
|
+
}
|
|
481
|
+
static async create(modelsDir) {
|
|
482
|
+
const modelPath = (0, import_path.join)(modelsDir, MODEL_FILENAME);
|
|
483
|
+
if (!(0, import_fs.existsSync)(modelPath)) {
|
|
484
|
+
await _LocalEmbedding.downloadModel(modelsDir, modelPath);
|
|
485
|
+
}
|
|
486
|
+
const { getLlama, LlamaLogLevel } = await import("node-llama-cpp");
|
|
487
|
+
const llama = await getLlama({
|
|
488
|
+
logLevel: LlamaLogLevel.error
|
|
489
|
+
// Only show errors, not warnings
|
|
490
|
+
});
|
|
491
|
+
const model = await llama.loadModel({ modelPath });
|
|
492
|
+
const context = await model.createEmbeddingContext();
|
|
493
|
+
return new _LocalEmbedding(model, context, 384);
|
|
494
|
+
}
|
|
495
|
+
static async downloadModel(modelsDir, modelPath) {
|
|
496
|
+
await (0, import_promises.mkdir)(modelsDir, { recursive: true });
|
|
497
|
+
console.log(`Downloading embedding model to ${modelPath}...`);
|
|
498
|
+
console.log("This is a one-time download (~274MB)");
|
|
499
|
+
const response = await fetch(MODEL_URL);
|
|
500
|
+
if (!response.ok || !response.body) {
|
|
501
|
+
throw new Error(`Failed to download model: ${response.statusText}`);
|
|
502
|
+
}
|
|
503
|
+
const fileStream = (0, import_fs.createWriteStream)(modelPath);
|
|
504
|
+
await (0, import_promises2.pipeline)(response.body, fileStream);
|
|
505
|
+
console.log("Model downloaded successfully");
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Generate embedding for a document/memory
|
|
509
|
+
*/
|
|
510
|
+
async embed(text) {
|
|
511
|
+
const prefixedText = `search_document: ${text}`;
|
|
512
|
+
const embedding = await this.context.getEmbeddingFor(prefixedText);
|
|
513
|
+
const vector = Array.from(embedding.vector).slice(0, this.dimensions);
|
|
514
|
+
return this.normalize(vector);
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Generate embedding for a search query
|
|
518
|
+
*/
|
|
519
|
+
async embedQuery(text) {
|
|
520
|
+
const prefixedText = `search_query: ${text}`;
|
|
521
|
+
const embedding = await this.context.getEmbeddingFor(prefixedText);
|
|
522
|
+
const vector = Array.from(embedding.vector).slice(0, this.dimensions);
|
|
523
|
+
return this.normalize(vector);
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Batch embed multiple texts (documents)
|
|
527
|
+
*/
|
|
528
|
+
async embedBatch(texts) {
|
|
529
|
+
const results = [];
|
|
530
|
+
for (const text of texts) {
|
|
531
|
+
results.push(await this.embed(text));
|
|
532
|
+
}
|
|
533
|
+
return results;
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Get the embedding dimensions
|
|
537
|
+
*/
|
|
538
|
+
getDimensions() {
|
|
539
|
+
return this.dimensions;
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Normalize vector to unit length (for cosine similarity)
|
|
543
|
+
*/
|
|
544
|
+
normalize(vector) {
|
|
545
|
+
const norm = Math.sqrt(vector.reduce((sum, v) => sum + v * v, 0));
|
|
546
|
+
if (norm === 0)
|
|
547
|
+
return vector;
|
|
548
|
+
return vector.map((v) => v / norm);
|
|
549
|
+
}
|
|
550
|
+
async close() {
|
|
551
|
+
await this.context.dispose();
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
function isModelAvailable(modelsDir) {
|
|
555
|
+
const modelPath = (0, import_path.join)(modelsDir, MODEL_FILENAME);
|
|
556
|
+
return (0, import_fs.existsSync)(modelPath);
|
|
557
|
+
}
|
|
558
|
+
async function createEmbedFunction(modelsDir) {
|
|
559
|
+
if (isModelAvailable(modelsDir)) {
|
|
560
|
+
try {
|
|
561
|
+
const embedding = await LocalEmbedding.create(modelsDir);
|
|
562
|
+
return {
|
|
563
|
+
embed: (text) => embedding.embed(text),
|
|
564
|
+
embedQuery: (text) => embedding.embedQuery(text),
|
|
565
|
+
isReal: true,
|
|
566
|
+
close: () => embedding.close()
|
|
567
|
+
};
|
|
568
|
+
} catch (error) {
|
|
569
|
+
console.error("[memextend] Failed to load embedding model, using fallback:", error);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
const { createHash: createHash2 } = await import("crypto");
|
|
573
|
+
const hashEmbed = (text, prefix) => {
|
|
574
|
+
const hash = createHash2("sha256").update(prefix + text).digest();
|
|
575
|
+
const vector = Array.from(hash).slice(0, 32);
|
|
576
|
+
const expanded = [];
|
|
577
|
+
for (let i = 0; i < 12; i++) {
|
|
578
|
+
const roundHash = createHash2("sha256").update(text + i.toString()).digest();
|
|
579
|
+
expanded.push(...Array.from(roundHash).slice(0, 32));
|
|
580
|
+
}
|
|
581
|
+
const norm = Math.sqrt(expanded.reduce((sum, v) => sum + v * v, 0));
|
|
582
|
+
return expanded.map((v) => v / norm / 255);
|
|
583
|
+
};
|
|
584
|
+
return {
|
|
585
|
+
embed: async (text) => hashEmbed(text, "doc:"),
|
|
586
|
+
embedQuery: async (text) => hashEmbed(text, "query:"),
|
|
587
|
+
isReal: false,
|
|
588
|
+
close: async () => {
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// src/cli/capture.ts
|
|
594
|
+
var MEMEXTEND_DIR = (0, import_path2.join)((0, import_os.homedir)(), ".memextend");
|
|
595
|
+
var DB_PATH = (0, import_path2.join)(MEMEXTEND_DIR, "memextend.db");
|
|
596
|
+
var VECTORS_PATH = (0, import_path2.join)(MEMEXTEND_DIR, "vectors");
|
|
597
|
+
var MODELS_PATH = (0, import_path2.join)(MEMEXTEND_DIR, "models");
|
|
598
|
+
function parseArgs() {
|
|
599
|
+
const args = process.argv.slice(2);
|
|
600
|
+
const options = {};
|
|
601
|
+
for (let i = 0; i < args.length; i++) {
|
|
602
|
+
const arg = args[i];
|
|
603
|
+
const next = args[i + 1];
|
|
604
|
+
switch (arg) {
|
|
605
|
+
case "--content":
|
|
606
|
+
case "-c":
|
|
607
|
+
options.content = next;
|
|
608
|
+
i++;
|
|
609
|
+
break;
|
|
610
|
+
case "--file":
|
|
611
|
+
case "-f":
|
|
612
|
+
options.file = next;
|
|
613
|
+
i++;
|
|
614
|
+
break;
|
|
615
|
+
case "--stdin":
|
|
616
|
+
case "-i":
|
|
617
|
+
options.stdin = true;
|
|
618
|
+
break;
|
|
619
|
+
case "--workspace":
|
|
620
|
+
case "-w":
|
|
621
|
+
options.workspace = next;
|
|
622
|
+
i++;
|
|
623
|
+
break;
|
|
624
|
+
case "--type":
|
|
625
|
+
case "-t":
|
|
626
|
+
options.type = next;
|
|
627
|
+
i++;
|
|
628
|
+
break;
|
|
629
|
+
case "--source":
|
|
630
|
+
case "-s":
|
|
631
|
+
options.sourceTool = next;
|
|
632
|
+
i++;
|
|
633
|
+
break;
|
|
634
|
+
case "--session":
|
|
635
|
+
options.sessionId = next;
|
|
636
|
+
i++;
|
|
637
|
+
break;
|
|
638
|
+
case "--quiet":
|
|
639
|
+
case "-q":
|
|
640
|
+
options.quiet = true;
|
|
641
|
+
break;
|
|
642
|
+
case "--help":
|
|
643
|
+
case "-h":
|
|
644
|
+
printHelp();
|
|
645
|
+
process.exit(0);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
return options;
|
|
649
|
+
}
|
|
650
|
+
function printHelp() {
|
|
651
|
+
console.log(`
|
|
652
|
+
memextend-cursor-capture - Capture session content to memory
|
|
653
|
+
|
|
654
|
+
USAGE:
|
|
655
|
+
memextend-cursor-capture [OPTIONS]
|
|
656
|
+
|
|
657
|
+
OPTIONS:
|
|
658
|
+
-c, --content <text> Content to capture directly
|
|
659
|
+
-f, --file <path> Read content from file
|
|
660
|
+
-i, --stdin Read content from stdin
|
|
661
|
+
-w, --workspace <dir> Workspace directory (default: cwd)
|
|
662
|
+
-t, --type <type> Memory type: summary, tool_capture, manual (default: manual)
|
|
663
|
+
-s, --source <tool> Source tool name (for tool_capture type)
|
|
664
|
+
--session <id> Session ID for grouping captures
|
|
665
|
+
-q, --quiet Suppress output
|
|
666
|
+
-h, --help Show this help
|
|
667
|
+
|
|
668
|
+
EXAMPLES:
|
|
669
|
+
# Capture direct content
|
|
670
|
+
memextend-cursor-capture -c "Implemented Redis caching for user sessions"
|
|
671
|
+
|
|
672
|
+
# Capture from file
|
|
673
|
+
memextend-cursor-capture -f session-notes.txt
|
|
674
|
+
|
|
675
|
+
# Capture from pipe (e.g., from AI output)
|
|
676
|
+
echo "Fixed authentication bug" | memextend-cursor-capture --stdin
|
|
677
|
+
|
|
678
|
+
# Capture with workspace context
|
|
679
|
+
memextend-cursor-capture -c "Added API endpoints" -w /path/to/project
|
|
680
|
+
|
|
681
|
+
INTEGRATION:
|
|
682
|
+
Add as a Cursor task in tasks.json or bind to a keyboard shortcut.
|
|
683
|
+
`);
|
|
684
|
+
}
|
|
685
|
+
async function readStdin() {
|
|
686
|
+
const chunks = [];
|
|
687
|
+
for await (const chunk of process.stdin) {
|
|
688
|
+
chunks.push(chunk);
|
|
689
|
+
}
|
|
690
|
+
return Buffer.concat(chunks).toString("utf-8");
|
|
691
|
+
}
|
|
692
|
+
function getProjectId2(cwd) {
|
|
693
|
+
try {
|
|
694
|
+
const gitRoot = (0, import_child_process.execSync)("git rev-parse --show-toplevel", {
|
|
695
|
+
cwd,
|
|
696
|
+
encoding: "utf-8",
|
|
697
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
698
|
+
}).trim();
|
|
699
|
+
return (0, import_crypto.createHash)("sha256").update(gitRoot).digest("hex").slice(0, 16);
|
|
700
|
+
} catch {
|
|
701
|
+
return (0, import_crypto.createHash)("sha256").update(cwd).digest("hex").slice(0, 16);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
async function main() {
|
|
705
|
+
const options = parseArgs();
|
|
706
|
+
if (!(0, import_fs2.existsSync)(DB_PATH)) {
|
|
707
|
+
console.error("Error: memextend not initialized. Run `memextend init` first.");
|
|
708
|
+
process.exit(1);
|
|
709
|
+
}
|
|
710
|
+
let content;
|
|
711
|
+
if (options.content) {
|
|
712
|
+
content = options.content;
|
|
713
|
+
} else if (options.file) {
|
|
714
|
+
if (!(0, import_fs2.existsSync)(options.file)) {
|
|
715
|
+
console.error(`Error: File not found: ${options.file}`);
|
|
716
|
+
process.exit(1);
|
|
717
|
+
}
|
|
718
|
+
content = await (0, import_promises3.readFile)(options.file, "utf-8");
|
|
719
|
+
} else if (options.stdin) {
|
|
720
|
+
content = await readStdin();
|
|
721
|
+
} else {
|
|
722
|
+
console.error("Error: No content source specified. Use --content, --file, or --stdin");
|
|
723
|
+
console.error("Run with --help for usage information.");
|
|
724
|
+
process.exit(1);
|
|
725
|
+
}
|
|
726
|
+
content = content.trim();
|
|
727
|
+
if (!content || content.length < 5) {
|
|
728
|
+
console.error("Error: Content is empty or too short (minimum 5 characters)");
|
|
729
|
+
process.exit(1);
|
|
730
|
+
}
|
|
731
|
+
const workspace = options.workspace ? (0, import_path2.resolve)(options.workspace) : process.cwd();
|
|
732
|
+
const projectId = getProjectId2(workspace);
|
|
733
|
+
const sqlite = new SQLiteStorage(DB_PATH);
|
|
734
|
+
const lancedb2 = await LanceDBStorage.create(VECTORS_PATH);
|
|
735
|
+
const embedder = await createEmbedFunction(MODELS_PATH);
|
|
736
|
+
try {
|
|
737
|
+
const project = sqlite.getProject(projectId);
|
|
738
|
+
if (!project) {
|
|
739
|
+
sqlite.insertProject({
|
|
740
|
+
id: projectId,
|
|
741
|
+
name: (0, import_path2.basename)(workspace),
|
|
742
|
+
path: workspace,
|
|
743
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
const memoryId = (0, import_crypto.randomUUID)();
|
|
747
|
+
const memory = {
|
|
748
|
+
id: memoryId,
|
|
749
|
+
projectId,
|
|
750
|
+
content,
|
|
751
|
+
type: options.type ?? "manual",
|
|
752
|
+
sourceTool: options.sourceTool ?? null,
|
|
753
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
754
|
+
sessionId: options.sessionId ?? null,
|
|
755
|
+
metadata: null
|
|
756
|
+
};
|
|
757
|
+
sqlite.insertMemory(memory);
|
|
758
|
+
const vector = await embedder.embed(content);
|
|
759
|
+
await lancedb2.insertVector(memoryId, vector);
|
|
760
|
+
if (!options.quiet) {
|
|
761
|
+
console.log(`Memory captured successfully!`);
|
|
762
|
+
console.log(` ID: ${memoryId}`);
|
|
763
|
+
console.log(` Project: ${(0, import_path2.basename)(workspace)}`);
|
|
764
|
+
console.log(` Type: ${memory.type}`);
|
|
765
|
+
console.log(` Content: ${content.slice(0, 100)}${content.length > 100 ? "..." : ""}`);
|
|
766
|
+
}
|
|
767
|
+
} finally {
|
|
768
|
+
sqlite.close();
|
|
769
|
+
await lancedb2.close();
|
|
770
|
+
await embedder.close();
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
main().catch((error) => {
|
|
774
|
+
console.error("Error:", error.message);
|
|
775
|
+
process.exit(1);
|
|
776
|
+
});
|