apsolut-cortex 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 +88 -0
- package/dist/cli.js +1364 -0
- package/dist/hooks/post-tool-use.js +5577 -0
- package/dist/hooks/session-end.js +38093 -0
- package/dist/hooks/session-start.js +972 -0
- package/dist/hooks/stop.js +1000 -0
- package/dist/mcp/server.js +48099 -0
- package/package.json +43 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,1364 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
9
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
10
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
11
|
+
for (let key of __getOwnPropNames(mod))
|
|
12
|
+
if (!__hasOwnProp.call(to, key))
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: () => mod[key],
|
|
15
|
+
enumerable: true
|
|
16
|
+
});
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
20
|
+
var __export = (target, all) => {
|
|
21
|
+
for (var name in all)
|
|
22
|
+
__defProp(target, name, {
|
|
23
|
+
get: all[name],
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
set: (newValue) => all[name] = () => newValue
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
30
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
31
|
+
|
|
32
|
+
// node_modules/better-sqlite3/lib/util.js
|
|
33
|
+
var require_util = __commonJS((exports) => {
|
|
34
|
+
exports.getBooleanOption = (options, key) => {
|
|
35
|
+
let value = false;
|
|
36
|
+
if (key in options && typeof (value = options[key]) !== "boolean") {
|
|
37
|
+
throw new TypeError(`Expected the "${key}" option to be a boolean`);
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
};
|
|
41
|
+
exports.cppdb = Symbol();
|
|
42
|
+
exports.inspect = Symbol.for("nodejs.util.inspect.custom");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// node_modules/better-sqlite3/lib/sqlite-error.js
|
|
46
|
+
var require_sqlite_error = __commonJS((exports, module) => {
|
|
47
|
+
var descriptor = { value: "SqliteError", writable: true, enumerable: false, configurable: true };
|
|
48
|
+
function SqliteError(message, code) {
|
|
49
|
+
if (new.target !== SqliteError) {
|
|
50
|
+
return new SqliteError(message, code);
|
|
51
|
+
}
|
|
52
|
+
if (typeof code !== "string") {
|
|
53
|
+
throw new TypeError("Expected second argument to be a string");
|
|
54
|
+
}
|
|
55
|
+
Error.call(this, message);
|
|
56
|
+
descriptor.value = "" + message;
|
|
57
|
+
Object.defineProperty(this, "message", descriptor);
|
|
58
|
+
Error.captureStackTrace(this, SqliteError);
|
|
59
|
+
this.code = code;
|
|
60
|
+
}
|
|
61
|
+
Object.setPrototypeOf(SqliteError, Error);
|
|
62
|
+
Object.setPrototypeOf(SqliteError.prototype, Error.prototype);
|
|
63
|
+
Object.defineProperty(SqliteError.prototype, "name", descriptor);
|
|
64
|
+
module.exports = SqliteError;
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// node_modules/file-uri-to-path/index.js
|
|
68
|
+
var require_file_uri_to_path = __commonJS((exports, module) => {
|
|
69
|
+
var sep = __require("path").sep || "/";
|
|
70
|
+
module.exports = fileUriToPath;
|
|
71
|
+
function fileUriToPath(uri) {
|
|
72
|
+
if (typeof uri != "string" || uri.length <= 7 || uri.substring(0, 7) != "file://") {
|
|
73
|
+
throw new TypeError("must pass in a file:// URI to convert to a file path");
|
|
74
|
+
}
|
|
75
|
+
var rest = decodeURI(uri.substring(7));
|
|
76
|
+
var firstSlash = rest.indexOf("/");
|
|
77
|
+
var host = rest.substring(0, firstSlash);
|
|
78
|
+
var path = rest.substring(firstSlash + 1);
|
|
79
|
+
if (host == "localhost")
|
|
80
|
+
host = "";
|
|
81
|
+
if (host) {
|
|
82
|
+
host = sep + sep + host;
|
|
83
|
+
}
|
|
84
|
+
path = path.replace(/^(.+)\|/, "$1:");
|
|
85
|
+
if (sep == "\\") {
|
|
86
|
+
path = path.replace(/\//g, "\\");
|
|
87
|
+
}
|
|
88
|
+
if (/^.+\:/.test(path)) {} else {
|
|
89
|
+
path = sep + path;
|
|
90
|
+
}
|
|
91
|
+
return host + path;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// node_modules/bindings/bindings.js
|
|
96
|
+
var require_bindings = __commonJS((exports, module) => {
|
|
97
|
+
var __filename = "D:\\sites\\git\\apsolut-cortex\\node_modules\\bindings\\bindings.js";
|
|
98
|
+
var fs = __require("fs");
|
|
99
|
+
var path = __require("path");
|
|
100
|
+
var fileURLToPath = require_file_uri_to_path();
|
|
101
|
+
var join = path.join;
|
|
102
|
+
var dirname = path.dirname;
|
|
103
|
+
var exists = fs.accessSync && function(path2) {
|
|
104
|
+
try {
|
|
105
|
+
fs.accessSync(path2);
|
|
106
|
+
} catch (e) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
return true;
|
|
110
|
+
} || fs.existsSync || path.existsSync;
|
|
111
|
+
var defaults = {
|
|
112
|
+
arrow: process.env.NODE_BINDINGS_ARROW || " → ",
|
|
113
|
+
compiled: process.env.NODE_BINDINGS_COMPILED_DIR || "compiled",
|
|
114
|
+
platform: process.platform,
|
|
115
|
+
arch: process.arch,
|
|
116
|
+
nodePreGyp: "node-v" + process.versions.modules + "-" + process.platform + "-" + process.arch,
|
|
117
|
+
version: process.versions.node,
|
|
118
|
+
bindings: "bindings.node",
|
|
119
|
+
try: [
|
|
120
|
+
["module_root", "build", "bindings"],
|
|
121
|
+
["module_root", "build", "Debug", "bindings"],
|
|
122
|
+
["module_root", "build", "Release", "bindings"],
|
|
123
|
+
["module_root", "out", "Debug", "bindings"],
|
|
124
|
+
["module_root", "Debug", "bindings"],
|
|
125
|
+
["module_root", "out", "Release", "bindings"],
|
|
126
|
+
["module_root", "Release", "bindings"],
|
|
127
|
+
["module_root", "build", "default", "bindings"],
|
|
128
|
+
["module_root", "compiled", "version", "platform", "arch", "bindings"],
|
|
129
|
+
["module_root", "addon-build", "release", "install-root", "bindings"],
|
|
130
|
+
["module_root", "addon-build", "debug", "install-root", "bindings"],
|
|
131
|
+
["module_root", "addon-build", "default", "install-root", "bindings"],
|
|
132
|
+
["module_root", "lib", "binding", "nodePreGyp", "bindings"]
|
|
133
|
+
]
|
|
134
|
+
};
|
|
135
|
+
function bindings(opts) {
|
|
136
|
+
if (typeof opts == "string") {
|
|
137
|
+
opts = { bindings: opts };
|
|
138
|
+
} else if (!opts) {
|
|
139
|
+
opts = {};
|
|
140
|
+
}
|
|
141
|
+
Object.keys(defaults).map(function(i2) {
|
|
142
|
+
if (!(i2 in opts))
|
|
143
|
+
opts[i2] = defaults[i2];
|
|
144
|
+
});
|
|
145
|
+
if (!opts.module_root) {
|
|
146
|
+
opts.module_root = exports.getRoot(exports.getFileName());
|
|
147
|
+
}
|
|
148
|
+
if (path.extname(opts.bindings) != ".node") {
|
|
149
|
+
opts.bindings += ".node";
|
|
150
|
+
}
|
|
151
|
+
var requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : __require;
|
|
152
|
+
var tries = [], i = 0, l = opts.try.length, n, b, err;
|
|
153
|
+
for (;i < l; i++) {
|
|
154
|
+
n = join.apply(null, opts.try[i].map(function(p) {
|
|
155
|
+
return opts[p] || p;
|
|
156
|
+
}));
|
|
157
|
+
tries.push(n);
|
|
158
|
+
try {
|
|
159
|
+
b = opts.path ? requireFunc.resolve(n) : requireFunc(n);
|
|
160
|
+
if (!opts.path) {
|
|
161
|
+
b.path = n;
|
|
162
|
+
}
|
|
163
|
+
return b;
|
|
164
|
+
} catch (e) {
|
|
165
|
+
if (e.code !== "MODULE_NOT_FOUND" && e.code !== "QUALIFIED_PATH_RESOLUTION_FAILED" && !/not find/i.test(e.message)) {
|
|
166
|
+
throw e;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
err = new Error(`Could not locate the bindings file. Tried:
|
|
171
|
+
` + tries.map(function(a) {
|
|
172
|
+
return opts.arrow + a;
|
|
173
|
+
}).join(`
|
|
174
|
+
`));
|
|
175
|
+
err.tries = tries;
|
|
176
|
+
throw err;
|
|
177
|
+
}
|
|
178
|
+
module.exports = exports = bindings;
|
|
179
|
+
exports.getFileName = function getFileName(calling_file) {
|
|
180
|
+
var { prepareStackTrace: origPST, stackTraceLimit: origSTL } = Error, dummy = {}, fileName;
|
|
181
|
+
Error.stackTraceLimit = 10;
|
|
182
|
+
Error.prepareStackTrace = function(e, st) {
|
|
183
|
+
for (var i = 0, l = st.length;i < l; i++) {
|
|
184
|
+
fileName = st[i].getFileName();
|
|
185
|
+
if (fileName !== __filename) {
|
|
186
|
+
if (calling_file) {
|
|
187
|
+
if (fileName !== calling_file) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
} else {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
Error.captureStackTrace(dummy);
|
|
197
|
+
dummy.stack;
|
|
198
|
+
Error.prepareStackTrace = origPST;
|
|
199
|
+
Error.stackTraceLimit = origSTL;
|
|
200
|
+
var fileSchema = "file://";
|
|
201
|
+
if (fileName.indexOf(fileSchema) === 0) {
|
|
202
|
+
fileName = fileURLToPath(fileName);
|
|
203
|
+
}
|
|
204
|
+
return fileName;
|
|
205
|
+
};
|
|
206
|
+
exports.getRoot = function getRoot(file) {
|
|
207
|
+
var dir = dirname(file), prev;
|
|
208
|
+
while (true) {
|
|
209
|
+
if (dir === ".") {
|
|
210
|
+
dir = process.cwd();
|
|
211
|
+
}
|
|
212
|
+
if (exists(join(dir, "package.json")) || exists(join(dir, "node_modules"))) {
|
|
213
|
+
return dir;
|
|
214
|
+
}
|
|
215
|
+
if (prev === dir) {
|
|
216
|
+
throw new Error('Could not find module root given file: "' + file + '". Do you have a `package.json` file? ');
|
|
217
|
+
}
|
|
218
|
+
prev = dir;
|
|
219
|
+
dir = join(dir, "..");
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// node_modules/better-sqlite3/lib/methods/wrappers.js
|
|
225
|
+
var require_wrappers = __commonJS((exports) => {
|
|
226
|
+
var { cppdb } = require_util();
|
|
227
|
+
exports.prepare = function prepare(sql) {
|
|
228
|
+
return this[cppdb].prepare(sql, this, false);
|
|
229
|
+
};
|
|
230
|
+
exports.exec = function exec(sql) {
|
|
231
|
+
this[cppdb].exec(sql);
|
|
232
|
+
return this;
|
|
233
|
+
};
|
|
234
|
+
exports.close = function close() {
|
|
235
|
+
this[cppdb].close();
|
|
236
|
+
return this;
|
|
237
|
+
};
|
|
238
|
+
exports.loadExtension = function loadExtension(...args) {
|
|
239
|
+
this[cppdb].loadExtension(...args);
|
|
240
|
+
return this;
|
|
241
|
+
};
|
|
242
|
+
exports.defaultSafeIntegers = function defaultSafeIntegers(...args) {
|
|
243
|
+
this[cppdb].defaultSafeIntegers(...args);
|
|
244
|
+
return this;
|
|
245
|
+
};
|
|
246
|
+
exports.unsafeMode = function unsafeMode(...args) {
|
|
247
|
+
this[cppdb].unsafeMode(...args);
|
|
248
|
+
return this;
|
|
249
|
+
};
|
|
250
|
+
exports.getters = {
|
|
251
|
+
name: {
|
|
252
|
+
get: function name() {
|
|
253
|
+
return this[cppdb].name;
|
|
254
|
+
},
|
|
255
|
+
enumerable: true
|
|
256
|
+
},
|
|
257
|
+
open: {
|
|
258
|
+
get: function open() {
|
|
259
|
+
return this[cppdb].open;
|
|
260
|
+
},
|
|
261
|
+
enumerable: true
|
|
262
|
+
},
|
|
263
|
+
inTransaction: {
|
|
264
|
+
get: function inTransaction() {
|
|
265
|
+
return this[cppdb].inTransaction;
|
|
266
|
+
},
|
|
267
|
+
enumerable: true
|
|
268
|
+
},
|
|
269
|
+
readonly: {
|
|
270
|
+
get: function readonly() {
|
|
271
|
+
return this[cppdb].readonly;
|
|
272
|
+
},
|
|
273
|
+
enumerable: true
|
|
274
|
+
},
|
|
275
|
+
memory: {
|
|
276
|
+
get: function memory() {
|
|
277
|
+
return this[cppdb].memory;
|
|
278
|
+
},
|
|
279
|
+
enumerable: true
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// node_modules/better-sqlite3/lib/methods/transaction.js
|
|
285
|
+
var require_transaction = __commonJS((exports, module) => {
|
|
286
|
+
var { cppdb } = require_util();
|
|
287
|
+
var controllers = new WeakMap;
|
|
288
|
+
module.exports = function transaction(fn) {
|
|
289
|
+
if (typeof fn !== "function")
|
|
290
|
+
throw new TypeError("Expected first argument to be a function");
|
|
291
|
+
const db = this[cppdb];
|
|
292
|
+
const controller = getController(db, this);
|
|
293
|
+
const { apply } = Function.prototype;
|
|
294
|
+
const properties = {
|
|
295
|
+
default: { value: wrapTransaction(apply, fn, db, controller.default) },
|
|
296
|
+
deferred: { value: wrapTransaction(apply, fn, db, controller.deferred) },
|
|
297
|
+
immediate: { value: wrapTransaction(apply, fn, db, controller.immediate) },
|
|
298
|
+
exclusive: { value: wrapTransaction(apply, fn, db, controller.exclusive) },
|
|
299
|
+
database: { value: this, enumerable: true }
|
|
300
|
+
};
|
|
301
|
+
Object.defineProperties(properties.default.value, properties);
|
|
302
|
+
Object.defineProperties(properties.deferred.value, properties);
|
|
303
|
+
Object.defineProperties(properties.immediate.value, properties);
|
|
304
|
+
Object.defineProperties(properties.exclusive.value, properties);
|
|
305
|
+
return properties.default.value;
|
|
306
|
+
};
|
|
307
|
+
var getController = (db, self) => {
|
|
308
|
+
let controller = controllers.get(db);
|
|
309
|
+
if (!controller) {
|
|
310
|
+
const shared = {
|
|
311
|
+
commit: db.prepare("COMMIT", self, false),
|
|
312
|
+
rollback: db.prepare("ROLLBACK", self, false),
|
|
313
|
+
savepoint: db.prepare("SAVEPOINT `\t_bs3.\t`", self, false),
|
|
314
|
+
release: db.prepare("RELEASE `\t_bs3.\t`", self, false),
|
|
315
|
+
rollbackTo: db.prepare("ROLLBACK TO `\t_bs3.\t`", self, false)
|
|
316
|
+
};
|
|
317
|
+
controllers.set(db, controller = {
|
|
318
|
+
default: Object.assign({ begin: db.prepare("BEGIN", self, false) }, shared),
|
|
319
|
+
deferred: Object.assign({ begin: db.prepare("BEGIN DEFERRED", self, false) }, shared),
|
|
320
|
+
immediate: Object.assign({ begin: db.prepare("BEGIN IMMEDIATE", self, false) }, shared),
|
|
321
|
+
exclusive: Object.assign({ begin: db.prepare("BEGIN EXCLUSIVE", self, false) }, shared)
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
return controller;
|
|
325
|
+
};
|
|
326
|
+
var wrapTransaction = (apply, fn, db, { begin, commit, rollback, savepoint, release, rollbackTo }) => function sqliteTransaction() {
|
|
327
|
+
let before, after, undo;
|
|
328
|
+
if (db.inTransaction) {
|
|
329
|
+
before = savepoint;
|
|
330
|
+
after = release;
|
|
331
|
+
undo = rollbackTo;
|
|
332
|
+
} else {
|
|
333
|
+
before = begin;
|
|
334
|
+
after = commit;
|
|
335
|
+
undo = rollback;
|
|
336
|
+
}
|
|
337
|
+
before.run();
|
|
338
|
+
try {
|
|
339
|
+
const result = apply.call(fn, this, arguments);
|
|
340
|
+
if (result && typeof result.then === "function") {
|
|
341
|
+
throw new TypeError("Transaction function cannot return a promise");
|
|
342
|
+
}
|
|
343
|
+
after.run();
|
|
344
|
+
return result;
|
|
345
|
+
} catch (ex) {
|
|
346
|
+
if (db.inTransaction) {
|
|
347
|
+
undo.run();
|
|
348
|
+
if (undo !== rollback)
|
|
349
|
+
after.run();
|
|
350
|
+
}
|
|
351
|
+
throw ex;
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
// node_modules/better-sqlite3/lib/methods/pragma.js
|
|
357
|
+
var require_pragma = __commonJS((exports, module) => {
|
|
358
|
+
var { getBooleanOption, cppdb } = require_util();
|
|
359
|
+
module.exports = function pragma(source, options) {
|
|
360
|
+
if (options == null)
|
|
361
|
+
options = {};
|
|
362
|
+
if (typeof source !== "string")
|
|
363
|
+
throw new TypeError("Expected first argument to be a string");
|
|
364
|
+
if (typeof options !== "object")
|
|
365
|
+
throw new TypeError("Expected second argument to be an options object");
|
|
366
|
+
const simple = getBooleanOption(options, "simple");
|
|
367
|
+
const stmt = this[cppdb].prepare(`PRAGMA ${source}`, this, true);
|
|
368
|
+
return simple ? stmt.pluck().get() : stmt.all();
|
|
369
|
+
};
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
// node_modules/better-sqlite3/lib/methods/backup.js
|
|
373
|
+
var require_backup = __commonJS((exports, module) => {
|
|
374
|
+
var fs = __require("fs");
|
|
375
|
+
var path = __require("path");
|
|
376
|
+
var { promisify } = __require("util");
|
|
377
|
+
var { cppdb } = require_util();
|
|
378
|
+
var fsAccess = promisify(fs.access);
|
|
379
|
+
module.exports = async function backup(filename, options) {
|
|
380
|
+
if (options == null)
|
|
381
|
+
options = {};
|
|
382
|
+
if (typeof filename !== "string")
|
|
383
|
+
throw new TypeError("Expected first argument to be a string");
|
|
384
|
+
if (typeof options !== "object")
|
|
385
|
+
throw new TypeError("Expected second argument to be an options object");
|
|
386
|
+
filename = filename.trim();
|
|
387
|
+
const attachedName = "attached" in options ? options.attached : "main";
|
|
388
|
+
const handler = "progress" in options ? options.progress : null;
|
|
389
|
+
if (!filename)
|
|
390
|
+
throw new TypeError("Backup filename cannot be an empty string");
|
|
391
|
+
if (filename === ":memory:")
|
|
392
|
+
throw new TypeError('Invalid backup filename ":memory:"');
|
|
393
|
+
if (typeof attachedName !== "string")
|
|
394
|
+
throw new TypeError('Expected the "attached" option to be a string');
|
|
395
|
+
if (!attachedName)
|
|
396
|
+
throw new TypeError('The "attached" option cannot be an empty string');
|
|
397
|
+
if (handler != null && typeof handler !== "function")
|
|
398
|
+
throw new TypeError('Expected the "progress" option to be a function');
|
|
399
|
+
await fsAccess(path.dirname(filename)).catch(() => {
|
|
400
|
+
throw new TypeError("Cannot save backup because the directory does not exist");
|
|
401
|
+
});
|
|
402
|
+
const isNewFile = await fsAccess(filename).then(() => false, () => true);
|
|
403
|
+
return runBackup(this[cppdb].backup(this, attachedName, filename, isNewFile), handler || null);
|
|
404
|
+
};
|
|
405
|
+
var runBackup = (backup, handler) => {
|
|
406
|
+
let rate = 0;
|
|
407
|
+
let useDefault = true;
|
|
408
|
+
return new Promise((resolve, reject) => {
|
|
409
|
+
setImmediate(function step() {
|
|
410
|
+
try {
|
|
411
|
+
const progress = backup.transfer(rate);
|
|
412
|
+
if (!progress.remainingPages) {
|
|
413
|
+
backup.close();
|
|
414
|
+
resolve(progress);
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
if (useDefault) {
|
|
418
|
+
useDefault = false;
|
|
419
|
+
rate = 100;
|
|
420
|
+
}
|
|
421
|
+
if (handler) {
|
|
422
|
+
const ret = handler(progress);
|
|
423
|
+
if (ret !== undefined) {
|
|
424
|
+
if (typeof ret === "number" && ret === ret)
|
|
425
|
+
rate = Math.max(0, Math.min(2147483647, Math.round(ret)));
|
|
426
|
+
else
|
|
427
|
+
throw new TypeError("Expected progress callback to return a number or undefined");
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
setImmediate(step);
|
|
431
|
+
} catch (err) {
|
|
432
|
+
backup.close();
|
|
433
|
+
reject(err);
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
};
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
// node_modules/better-sqlite3/lib/methods/serialize.js
|
|
441
|
+
var require_serialize = __commonJS((exports, module) => {
|
|
442
|
+
var { cppdb } = require_util();
|
|
443
|
+
module.exports = function serialize(options) {
|
|
444
|
+
if (options == null)
|
|
445
|
+
options = {};
|
|
446
|
+
if (typeof options !== "object")
|
|
447
|
+
throw new TypeError("Expected first argument to be an options object");
|
|
448
|
+
const attachedName = "attached" in options ? options.attached : "main";
|
|
449
|
+
if (typeof attachedName !== "string")
|
|
450
|
+
throw new TypeError('Expected the "attached" option to be a string');
|
|
451
|
+
if (!attachedName)
|
|
452
|
+
throw new TypeError('The "attached" option cannot be an empty string');
|
|
453
|
+
return this[cppdb].serialize(attachedName);
|
|
454
|
+
};
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
// node_modules/better-sqlite3/lib/methods/function.js
|
|
458
|
+
var require_function = __commonJS((exports, module) => {
|
|
459
|
+
var { getBooleanOption, cppdb } = require_util();
|
|
460
|
+
module.exports = function defineFunction(name, options, fn) {
|
|
461
|
+
if (options == null)
|
|
462
|
+
options = {};
|
|
463
|
+
if (typeof options === "function") {
|
|
464
|
+
fn = options;
|
|
465
|
+
options = {};
|
|
466
|
+
}
|
|
467
|
+
if (typeof name !== "string")
|
|
468
|
+
throw new TypeError("Expected first argument to be a string");
|
|
469
|
+
if (typeof fn !== "function")
|
|
470
|
+
throw new TypeError("Expected last argument to be a function");
|
|
471
|
+
if (typeof options !== "object")
|
|
472
|
+
throw new TypeError("Expected second argument to be an options object");
|
|
473
|
+
if (!name)
|
|
474
|
+
throw new TypeError("User-defined function name cannot be an empty string");
|
|
475
|
+
const safeIntegers = "safeIntegers" in options ? +getBooleanOption(options, "safeIntegers") : 2;
|
|
476
|
+
const deterministic = getBooleanOption(options, "deterministic");
|
|
477
|
+
const directOnly = getBooleanOption(options, "directOnly");
|
|
478
|
+
const varargs = getBooleanOption(options, "varargs");
|
|
479
|
+
let argCount = -1;
|
|
480
|
+
if (!varargs) {
|
|
481
|
+
argCount = fn.length;
|
|
482
|
+
if (!Number.isInteger(argCount) || argCount < 0)
|
|
483
|
+
throw new TypeError("Expected function.length to be a positive integer");
|
|
484
|
+
if (argCount > 100)
|
|
485
|
+
throw new RangeError("User-defined functions cannot have more than 100 arguments");
|
|
486
|
+
}
|
|
487
|
+
this[cppdb].function(fn, name, argCount, safeIntegers, deterministic, directOnly);
|
|
488
|
+
return this;
|
|
489
|
+
};
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
// node_modules/better-sqlite3/lib/methods/aggregate.js
|
|
493
|
+
var require_aggregate = __commonJS((exports, module) => {
|
|
494
|
+
var { getBooleanOption, cppdb } = require_util();
|
|
495
|
+
module.exports = function defineAggregate(name, options) {
|
|
496
|
+
if (typeof name !== "string")
|
|
497
|
+
throw new TypeError("Expected first argument to be a string");
|
|
498
|
+
if (typeof options !== "object" || options === null)
|
|
499
|
+
throw new TypeError("Expected second argument to be an options object");
|
|
500
|
+
if (!name)
|
|
501
|
+
throw new TypeError("User-defined function name cannot be an empty string");
|
|
502
|
+
const start = "start" in options ? options.start : null;
|
|
503
|
+
const step = getFunctionOption(options, "step", true);
|
|
504
|
+
const inverse = getFunctionOption(options, "inverse", false);
|
|
505
|
+
const result = getFunctionOption(options, "result", false);
|
|
506
|
+
const safeIntegers = "safeIntegers" in options ? +getBooleanOption(options, "safeIntegers") : 2;
|
|
507
|
+
const deterministic = getBooleanOption(options, "deterministic");
|
|
508
|
+
const directOnly = getBooleanOption(options, "directOnly");
|
|
509
|
+
const varargs = getBooleanOption(options, "varargs");
|
|
510
|
+
let argCount = -1;
|
|
511
|
+
if (!varargs) {
|
|
512
|
+
argCount = Math.max(getLength(step), inverse ? getLength(inverse) : 0);
|
|
513
|
+
if (argCount > 0)
|
|
514
|
+
argCount -= 1;
|
|
515
|
+
if (argCount > 100)
|
|
516
|
+
throw new RangeError("User-defined functions cannot have more than 100 arguments");
|
|
517
|
+
}
|
|
518
|
+
this[cppdb].aggregate(start, step, inverse, result, name, argCount, safeIntegers, deterministic, directOnly);
|
|
519
|
+
return this;
|
|
520
|
+
};
|
|
521
|
+
var getFunctionOption = (options, key, required) => {
|
|
522
|
+
const value = key in options ? options[key] : null;
|
|
523
|
+
if (typeof value === "function")
|
|
524
|
+
return value;
|
|
525
|
+
if (value != null)
|
|
526
|
+
throw new TypeError(`Expected the "${key}" option to be a function`);
|
|
527
|
+
if (required)
|
|
528
|
+
throw new TypeError(`Missing required option "${key}"`);
|
|
529
|
+
return null;
|
|
530
|
+
};
|
|
531
|
+
var getLength = ({ length }) => {
|
|
532
|
+
if (Number.isInteger(length) && length >= 0)
|
|
533
|
+
return length;
|
|
534
|
+
throw new TypeError("Expected function.length to be a positive integer");
|
|
535
|
+
};
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
// node_modules/better-sqlite3/lib/methods/table.js
|
|
539
|
+
var require_table = __commonJS((exports, module) => {
|
|
540
|
+
var { cppdb } = require_util();
|
|
541
|
+
module.exports = function defineTable(name, factory) {
|
|
542
|
+
if (typeof name !== "string")
|
|
543
|
+
throw new TypeError("Expected first argument to be a string");
|
|
544
|
+
if (!name)
|
|
545
|
+
throw new TypeError("Virtual table module name cannot be an empty string");
|
|
546
|
+
let eponymous = false;
|
|
547
|
+
if (typeof factory === "object" && factory !== null) {
|
|
548
|
+
eponymous = true;
|
|
549
|
+
factory = defer(parseTableDefinition(factory, "used", name));
|
|
550
|
+
} else {
|
|
551
|
+
if (typeof factory !== "function")
|
|
552
|
+
throw new TypeError("Expected second argument to be a function or a table definition object");
|
|
553
|
+
factory = wrapFactory(factory);
|
|
554
|
+
}
|
|
555
|
+
this[cppdb].table(factory, name, eponymous);
|
|
556
|
+
return this;
|
|
557
|
+
};
|
|
558
|
+
function wrapFactory(factory) {
|
|
559
|
+
return function virtualTableFactory(moduleName, databaseName, tableName, ...args) {
|
|
560
|
+
const thisObject = {
|
|
561
|
+
module: moduleName,
|
|
562
|
+
database: databaseName,
|
|
563
|
+
table: tableName
|
|
564
|
+
};
|
|
565
|
+
const def = apply.call(factory, thisObject, args);
|
|
566
|
+
if (typeof def !== "object" || def === null) {
|
|
567
|
+
throw new TypeError(`Virtual table module "${moduleName}" did not return a table definition object`);
|
|
568
|
+
}
|
|
569
|
+
return parseTableDefinition(def, "returned", moduleName);
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
function parseTableDefinition(def, verb, moduleName) {
|
|
573
|
+
if (!hasOwnProperty.call(def, "rows")) {
|
|
574
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition without a "rows" property`);
|
|
575
|
+
}
|
|
576
|
+
if (!hasOwnProperty.call(def, "columns")) {
|
|
577
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition without a "columns" property`);
|
|
578
|
+
}
|
|
579
|
+
const rows = def.rows;
|
|
580
|
+
if (typeof rows !== "function" || Object.getPrototypeOf(rows) !== GeneratorFunctionPrototype) {
|
|
581
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "rows" property (should be a generator function)`);
|
|
582
|
+
}
|
|
583
|
+
let columns = def.columns;
|
|
584
|
+
if (!Array.isArray(columns) || !(columns = [...columns]).every((x) => typeof x === "string")) {
|
|
585
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "columns" property (should be an array of strings)`);
|
|
586
|
+
}
|
|
587
|
+
if (columns.length !== new Set(columns).size) {
|
|
588
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with duplicate column names`);
|
|
589
|
+
}
|
|
590
|
+
if (!columns.length) {
|
|
591
|
+
throw new RangeError(`Virtual table module "${moduleName}" ${verb} a table definition with zero columns`);
|
|
592
|
+
}
|
|
593
|
+
let parameters;
|
|
594
|
+
if (hasOwnProperty.call(def, "parameters")) {
|
|
595
|
+
parameters = def.parameters;
|
|
596
|
+
if (!Array.isArray(parameters) || !(parameters = [...parameters]).every((x) => typeof x === "string")) {
|
|
597
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "parameters" property (should be an array of strings)`);
|
|
598
|
+
}
|
|
599
|
+
} else {
|
|
600
|
+
parameters = inferParameters(rows);
|
|
601
|
+
}
|
|
602
|
+
if (parameters.length !== new Set(parameters).size) {
|
|
603
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with duplicate parameter names`);
|
|
604
|
+
}
|
|
605
|
+
if (parameters.length > 32) {
|
|
606
|
+
throw new RangeError(`Virtual table module "${moduleName}" ${verb} a table definition with more than the maximum number of 32 parameters`);
|
|
607
|
+
}
|
|
608
|
+
for (const parameter of parameters) {
|
|
609
|
+
if (columns.includes(parameter)) {
|
|
610
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with column "${parameter}" which was ambiguously defined as both a column and parameter`);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
let safeIntegers = 2;
|
|
614
|
+
if (hasOwnProperty.call(def, "safeIntegers")) {
|
|
615
|
+
const bool = def.safeIntegers;
|
|
616
|
+
if (typeof bool !== "boolean") {
|
|
617
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "safeIntegers" property (should be a boolean)`);
|
|
618
|
+
}
|
|
619
|
+
safeIntegers = +bool;
|
|
620
|
+
}
|
|
621
|
+
let directOnly = false;
|
|
622
|
+
if (hasOwnProperty.call(def, "directOnly")) {
|
|
623
|
+
directOnly = def.directOnly;
|
|
624
|
+
if (typeof directOnly !== "boolean") {
|
|
625
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "directOnly" property (should be a boolean)`);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
const columnDefinitions = [
|
|
629
|
+
...parameters.map(identifier).map((str) => `${str} HIDDEN`),
|
|
630
|
+
...columns.map(identifier)
|
|
631
|
+
];
|
|
632
|
+
return [
|
|
633
|
+
`CREATE TABLE x(${columnDefinitions.join(", ")});`,
|
|
634
|
+
wrapGenerator(rows, new Map(columns.map((x, i) => [x, parameters.length + i])), moduleName),
|
|
635
|
+
parameters,
|
|
636
|
+
safeIntegers,
|
|
637
|
+
directOnly
|
|
638
|
+
];
|
|
639
|
+
}
|
|
640
|
+
function wrapGenerator(generator, columnMap, moduleName) {
|
|
641
|
+
return function* virtualTable(...args) {
|
|
642
|
+
const output = args.map((x) => Buffer.isBuffer(x) ? Buffer.from(x) : x);
|
|
643
|
+
for (let i = 0;i < columnMap.size; ++i) {
|
|
644
|
+
output.push(null);
|
|
645
|
+
}
|
|
646
|
+
for (const row of generator(...args)) {
|
|
647
|
+
if (Array.isArray(row)) {
|
|
648
|
+
extractRowArray(row, output, columnMap.size, moduleName);
|
|
649
|
+
yield output;
|
|
650
|
+
} else if (typeof row === "object" && row !== null) {
|
|
651
|
+
extractRowObject(row, output, columnMap, moduleName);
|
|
652
|
+
yield output;
|
|
653
|
+
} else {
|
|
654
|
+
throw new TypeError(`Virtual table module "${moduleName}" yielded something that isn't a valid row object`);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
function extractRowArray(row, output, columnCount, moduleName) {
|
|
660
|
+
if (row.length !== columnCount) {
|
|
661
|
+
throw new TypeError(`Virtual table module "${moduleName}" yielded a row with an incorrect number of columns`);
|
|
662
|
+
}
|
|
663
|
+
const offset = output.length - columnCount;
|
|
664
|
+
for (let i = 0;i < columnCount; ++i) {
|
|
665
|
+
output[i + offset] = row[i];
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
function extractRowObject(row, output, columnMap, moduleName) {
|
|
669
|
+
let count = 0;
|
|
670
|
+
for (const key of Object.keys(row)) {
|
|
671
|
+
const index = columnMap.get(key);
|
|
672
|
+
if (index === undefined) {
|
|
673
|
+
throw new TypeError(`Virtual table module "${moduleName}" yielded a row with an undeclared column "${key}"`);
|
|
674
|
+
}
|
|
675
|
+
output[index] = row[key];
|
|
676
|
+
count += 1;
|
|
677
|
+
}
|
|
678
|
+
if (count !== columnMap.size) {
|
|
679
|
+
throw new TypeError(`Virtual table module "${moduleName}" yielded a row with missing columns`);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
function inferParameters({ length }) {
|
|
683
|
+
if (!Number.isInteger(length) || length < 0) {
|
|
684
|
+
throw new TypeError("Expected function.length to be a positive integer");
|
|
685
|
+
}
|
|
686
|
+
const params = [];
|
|
687
|
+
for (let i = 0;i < length; ++i) {
|
|
688
|
+
params.push(`$${i + 1}`);
|
|
689
|
+
}
|
|
690
|
+
return params;
|
|
691
|
+
}
|
|
692
|
+
var { hasOwnProperty } = Object.prototype;
|
|
693
|
+
var { apply } = Function.prototype;
|
|
694
|
+
var GeneratorFunctionPrototype = Object.getPrototypeOf(function* () {});
|
|
695
|
+
var identifier = (str) => `"${str.replace(/"/g, '""')}"`;
|
|
696
|
+
var defer = (x) => () => x;
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
// node_modules/better-sqlite3/lib/methods/inspect.js
|
|
700
|
+
var require_inspect = __commonJS((exports, module) => {
|
|
701
|
+
var DatabaseInspection = function Database() {};
|
|
702
|
+
module.exports = function inspect(depth, opts) {
|
|
703
|
+
return Object.assign(new DatabaseInspection, this);
|
|
704
|
+
};
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
// node_modules/better-sqlite3/lib/database.js
|
|
708
|
+
var require_database = __commonJS((exports, module) => {
|
|
709
|
+
var fs = __require("fs");
|
|
710
|
+
var path = __require("path");
|
|
711
|
+
var util = require_util();
|
|
712
|
+
var SqliteError = require_sqlite_error();
|
|
713
|
+
var DEFAULT_ADDON;
|
|
714
|
+
function Database(filenameGiven, options) {
|
|
715
|
+
if (new.target == null) {
|
|
716
|
+
return new Database(filenameGiven, options);
|
|
717
|
+
}
|
|
718
|
+
let buffer;
|
|
719
|
+
if (Buffer.isBuffer(filenameGiven)) {
|
|
720
|
+
buffer = filenameGiven;
|
|
721
|
+
filenameGiven = ":memory:";
|
|
722
|
+
}
|
|
723
|
+
if (filenameGiven == null)
|
|
724
|
+
filenameGiven = "";
|
|
725
|
+
if (options == null)
|
|
726
|
+
options = {};
|
|
727
|
+
if (typeof filenameGiven !== "string")
|
|
728
|
+
throw new TypeError("Expected first argument to be a string");
|
|
729
|
+
if (typeof options !== "object")
|
|
730
|
+
throw new TypeError("Expected second argument to be an options object");
|
|
731
|
+
if ("readOnly" in options)
|
|
732
|
+
throw new TypeError('Misspelled option "readOnly" should be "readonly"');
|
|
733
|
+
if ("memory" in options)
|
|
734
|
+
throw new TypeError('Option "memory" was removed in v7.0.0 (use ":memory:" filename instead)');
|
|
735
|
+
const filename = filenameGiven.trim();
|
|
736
|
+
const anonymous = filename === "" || filename === ":memory:";
|
|
737
|
+
const readonly = util.getBooleanOption(options, "readonly");
|
|
738
|
+
const fileMustExist = util.getBooleanOption(options, "fileMustExist");
|
|
739
|
+
const timeout = "timeout" in options ? options.timeout : 5000;
|
|
740
|
+
const verbose = "verbose" in options ? options.verbose : null;
|
|
741
|
+
const nativeBinding = "nativeBinding" in options ? options.nativeBinding : null;
|
|
742
|
+
if (readonly && anonymous && !buffer)
|
|
743
|
+
throw new TypeError("In-memory/temporary databases cannot be readonly");
|
|
744
|
+
if (!Number.isInteger(timeout) || timeout < 0)
|
|
745
|
+
throw new TypeError('Expected the "timeout" option to be a positive integer');
|
|
746
|
+
if (timeout > 2147483647)
|
|
747
|
+
throw new RangeError('Option "timeout" cannot be greater than 2147483647');
|
|
748
|
+
if (verbose != null && typeof verbose !== "function")
|
|
749
|
+
throw new TypeError('Expected the "verbose" option to be a function');
|
|
750
|
+
if (nativeBinding != null && typeof nativeBinding !== "string" && typeof nativeBinding !== "object")
|
|
751
|
+
throw new TypeError('Expected the "nativeBinding" option to be a string or addon object');
|
|
752
|
+
let addon;
|
|
753
|
+
if (nativeBinding == null) {
|
|
754
|
+
addon = DEFAULT_ADDON || (DEFAULT_ADDON = require_bindings()("better_sqlite3.node"));
|
|
755
|
+
} else if (typeof nativeBinding === "string") {
|
|
756
|
+
const requireFunc = typeof __non_webpack_require__ === "function" ? __non_webpack_require__ : __require;
|
|
757
|
+
addon = requireFunc(path.resolve(nativeBinding).replace(/(\.node)?$/, ".node"));
|
|
758
|
+
} else {
|
|
759
|
+
addon = nativeBinding;
|
|
760
|
+
}
|
|
761
|
+
if (!addon.isInitialized) {
|
|
762
|
+
addon.setErrorConstructor(SqliteError);
|
|
763
|
+
addon.isInitialized = true;
|
|
764
|
+
}
|
|
765
|
+
if (!anonymous && !fs.existsSync(path.dirname(filename))) {
|
|
766
|
+
throw new TypeError("Cannot open database because the directory does not exist");
|
|
767
|
+
}
|
|
768
|
+
Object.defineProperties(this, {
|
|
769
|
+
[util.cppdb]: { value: new addon.Database(filename, filenameGiven, anonymous, readonly, fileMustExist, timeout, verbose || null, buffer || null) },
|
|
770
|
+
...wrappers.getters
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
var wrappers = require_wrappers();
|
|
774
|
+
Database.prototype.prepare = wrappers.prepare;
|
|
775
|
+
Database.prototype.transaction = require_transaction();
|
|
776
|
+
Database.prototype.pragma = require_pragma();
|
|
777
|
+
Database.prototype.backup = require_backup();
|
|
778
|
+
Database.prototype.serialize = require_serialize();
|
|
779
|
+
Database.prototype.function = require_function();
|
|
780
|
+
Database.prototype.aggregate = require_aggregate();
|
|
781
|
+
Database.prototype.table = require_table();
|
|
782
|
+
Database.prototype.loadExtension = wrappers.loadExtension;
|
|
783
|
+
Database.prototype.exec = wrappers.exec;
|
|
784
|
+
Database.prototype.close = wrappers.close;
|
|
785
|
+
Database.prototype.defaultSafeIntegers = wrappers.defaultSafeIntegers;
|
|
786
|
+
Database.prototype.unsafeMode = wrappers.unsafeMode;
|
|
787
|
+
Database.prototype[util.inspect] = require_inspect();
|
|
788
|
+
module.exports = Database;
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
// node_modules/better-sqlite3/lib/index.js
|
|
792
|
+
var require_lib = __commonJS((exports, module) => {
|
|
793
|
+
module.exports = require_database();
|
|
794
|
+
module.exports.SqliteError = require_sqlite_error();
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
// src/db.ts
|
|
798
|
+
var exports_db = {};
|
|
799
|
+
__export(exports_db, {
|
|
800
|
+
upsertSession: () => upsertSession,
|
|
801
|
+
upsertProject: () => upsertProject,
|
|
802
|
+
updateWeight: () => updateWeight,
|
|
803
|
+
searchVector: () => searchVector,
|
|
804
|
+
searchBM25: () => searchBM25,
|
|
805
|
+
mergeRRF: () => mergeRRF,
|
|
806
|
+
markObservationsPromoted: () => markObservationsPromoted,
|
|
807
|
+
insertObservation: () => insertObservation,
|
|
808
|
+
insertMemory: () => insertMemory,
|
|
809
|
+
getSessionObservations: () => getSessionObservations,
|
|
810
|
+
getRecentSummaries: () => getRecentSummaries,
|
|
811
|
+
getDb: () => getDb,
|
|
812
|
+
decayAndPrune: () => decayAndPrune,
|
|
813
|
+
cosineSimilarity: () => cosineSimilarity,
|
|
814
|
+
applyMMR: () => applyMMR,
|
|
815
|
+
REGISTRY_PATH: () => REGISTRY_PATH,
|
|
816
|
+
MODELS_DIR: () => MODELS_DIR,
|
|
817
|
+
DB_PATH: () => DB_PATH,
|
|
818
|
+
CORTEX_DIR: () => CORTEX_DIR
|
|
819
|
+
});
|
|
820
|
+
import { existsSync, mkdirSync } from "fs";
|
|
821
|
+
import { homedir } from "os";
|
|
822
|
+
import { join } from "path";
|
|
823
|
+
function getDb() {
|
|
824
|
+
if (_db)
|
|
825
|
+
return _db;
|
|
826
|
+
if (!existsSync(CORTEX_DIR))
|
|
827
|
+
mkdirSync(CORTEX_DIR, { recursive: true });
|
|
828
|
+
if (!existsSync(MODELS_DIR))
|
|
829
|
+
mkdirSync(MODELS_DIR, { recursive: true });
|
|
830
|
+
_db = new import_better_sqlite3.default(DB_PATH);
|
|
831
|
+
_db.pragma("journal_mode = WAL");
|
|
832
|
+
_db.pragma("synchronous = NORMAL");
|
|
833
|
+
_db.pragma("foreign_keys = ON");
|
|
834
|
+
_db.pragma("cache_size = -32000");
|
|
835
|
+
_db.exec(`
|
|
836
|
+
CREATE TABLE IF NOT EXISTS projects (
|
|
837
|
+
id TEXT PRIMARY KEY,
|
|
838
|
+
name TEXT NOT NULL,
|
|
839
|
+
path TEXT,
|
|
840
|
+
created_at INTEGER NOT NULL,
|
|
841
|
+
last_session INTEGER
|
|
842
|
+
);
|
|
843
|
+
|
|
844
|
+
CREATE TABLE IF NOT EXISTS sessions (
|
|
845
|
+
id TEXT PRIMARY KEY,
|
|
846
|
+
project_id TEXT NOT NULL REFERENCES projects(id),
|
|
847
|
+
started_at INTEGER NOT NULL,
|
|
848
|
+
ended_at INTEGER,
|
|
849
|
+
summary TEXT,
|
|
850
|
+
memories_injected INTEGER NOT NULL DEFAULT 0,
|
|
851
|
+
memories_stored INTEGER NOT NULL DEFAULT 0,
|
|
852
|
+
tool_failures INTEGER NOT NULL DEFAULT 0
|
|
853
|
+
);
|
|
854
|
+
|
|
855
|
+
CREATE INDEX IF NOT EXISTS idx_sessions_project
|
|
856
|
+
ON sessions(project_id, started_at DESC);
|
|
857
|
+
|
|
858
|
+
CREATE TABLE IF NOT EXISTS observations (
|
|
859
|
+
id TEXT PRIMARY KEY,
|
|
860
|
+
session_id TEXT NOT NULL REFERENCES sessions(id),
|
|
861
|
+
project_id TEXT NOT NULL,
|
|
862
|
+
tool_name TEXT,
|
|
863
|
+
content TEXT NOT NULL,
|
|
864
|
+
category TEXT,
|
|
865
|
+
created_at INTEGER NOT NULL,
|
|
866
|
+
promoted INTEGER NOT NULL DEFAULT 0
|
|
867
|
+
);
|
|
868
|
+
|
|
869
|
+
CREATE INDEX IF NOT EXISTS idx_obs_session ON observations(session_id);
|
|
870
|
+
CREATE INDEX IF NOT EXISTS idx_obs_project ON observations(project_id, created_at DESC);
|
|
871
|
+
|
|
872
|
+
CREATE TABLE IF NOT EXISTS memories (
|
|
873
|
+
id TEXT PRIMARY KEY,
|
|
874
|
+
project_id TEXT NOT NULL,
|
|
875
|
+
tier TEXT NOT NULL DEFAULT 'semantic',
|
|
876
|
+
category TEXT NOT NULL DEFAULT 'insight',
|
|
877
|
+
trust TEXT NOT NULL DEFAULT 'observed',
|
|
878
|
+
content TEXT NOT NULL,
|
|
879
|
+
context TEXT,
|
|
880
|
+
source TEXT NOT NULL DEFAULT 'manual',
|
|
881
|
+
embedding BLOB,
|
|
882
|
+
weight REAL NOT NULL DEFAULT 1.0,
|
|
883
|
+
used_count INTEGER NOT NULL DEFAULT 0,
|
|
884
|
+
last_used INTEGER,
|
|
885
|
+
created_at INTEGER NOT NULL,
|
|
886
|
+
session_id TEXT REFERENCES sessions(id),
|
|
887
|
+
flagged INTEGER NOT NULL DEFAULT 0,
|
|
888
|
+
flag_reason TEXT
|
|
889
|
+
);
|
|
890
|
+
|
|
891
|
+
CREATE INDEX IF NOT EXISTS idx_mem_project ON memories(project_id);
|
|
892
|
+
CREATE INDEX IF NOT EXISTS idx_mem_weight ON memories(project_id, weight DESC);
|
|
893
|
+
CREATE INDEX IF NOT EXISTS idx_mem_tier ON memories(project_id, tier);
|
|
894
|
+
CREATE INDEX IF NOT EXISTS idx_mem_trust ON memories(project_id, trust);
|
|
895
|
+
CREATE INDEX IF NOT EXISTS idx_mem_flagged ON memories(project_id, flagged)
|
|
896
|
+
WHERE flagged = 1;
|
|
897
|
+
|
|
898
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
|
|
899
|
+
content, context,
|
|
900
|
+
content='memories',
|
|
901
|
+
content_rowid='rowid',
|
|
902
|
+
tokenize='porter ascii'
|
|
903
|
+
);
|
|
904
|
+
|
|
905
|
+
CREATE TRIGGER IF NOT EXISTS memories_ai AFTER INSERT ON memories BEGIN
|
|
906
|
+
INSERT INTO memories_fts(rowid, content, context)
|
|
907
|
+
VALUES (new.rowid, new.content, COALESCE(new.context, ''));
|
|
908
|
+
END;
|
|
909
|
+
|
|
910
|
+
CREATE TRIGGER IF NOT EXISTS memories_au AFTER UPDATE ON memories BEGIN
|
|
911
|
+
INSERT INTO memories_fts(memories_fts, rowid, content, context)
|
|
912
|
+
VALUES ('delete', old.rowid, old.content, COALESCE(old.context, ''));
|
|
913
|
+
INSERT INTO memories_fts(rowid, content, context)
|
|
914
|
+
VALUES (new.rowid, new.content, COALESCE(new.context, ''));
|
|
915
|
+
END;
|
|
916
|
+
|
|
917
|
+
CREATE TRIGGER IF NOT EXISTS memories_ad AFTER DELETE ON memories BEGIN
|
|
918
|
+
INSERT INTO memories_fts(memories_fts, rowid, content, context)
|
|
919
|
+
VALUES ('delete', old.rowid, old.content, COALESCE(old.context, ''));
|
|
920
|
+
END;
|
|
921
|
+
`);
|
|
922
|
+
return _db;
|
|
923
|
+
}
|
|
924
|
+
function upsertProject(db, project) {
|
|
925
|
+
const existing = db.prepare("SELECT id FROM projects WHERE id = ?").get(project.id);
|
|
926
|
+
if (existing) {
|
|
927
|
+
db.prepare("UPDATE projects SET last_session = ? WHERE id = ?").run(Date.now(), project.id);
|
|
928
|
+
} else {
|
|
929
|
+
db.prepare("INSERT INTO projects (id, name, path, created_at) VALUES (?, ?, ?, ?)").run(project.id, project.name, project.path ?? null, Date.now());
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
function upsertSession(db, s) {
|
|
933
|
+
const existing = db.prepare("SELECT id FROM sessions WHERE id = ?").get(s.id);
|
|
934
|
+
if (existing) {
|
|
935
|
+
const sets = [];
|
|
936
|
+
const vals = [];
|
|
937
|
+
if (s.ended_at !== undefined) {
|
|
938
|
+
sets.push("ended_at = ?");
|
|
939
|
+
vals.push(s.ended_at);
|
|
940
|
+
}
|
|
941
|
+
if (s.summary !== undefined) {
|
|
942
|
+
sets.push("summary = ?");
|
|
943
|
+
vals.push(s.summary);
|
|
944
|
+
}
|
|
945
|
+
if (s.memories_stored !== undefined) {
|
|
946
|
+
sets.push("memories_stored = ?");
|
|
947
|
+
vals.push(s.memories_stored);
|
|
948
|
+
}
|
|
949
|
+
if (s.tool_failures !== undefined) {
|
|
950
|
+
sets.push("tool_failures = ?");
|
|
951
|
+
vals.push(s.tool_failures);
|
|
952
|
+
}
|
|
953
|
+
if (sets.length) {
|
|
954
|
+
vals.push(s.id);
|
|
955
|
+
db.prepare(`UPDATE sessions SET ${sets.join(", ")} WHERE id = ?`).run(...vals);
|
|
956
|
+
}
|
|
957
|
+
} else {
|
|
958
|
+
db.prepare("INSERT INTO sessions (id, project_id, started_at) VALUES (?, ?, ?)").run(s.id, s.project_id, Date.now());
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
function getRecentSummaries(db, projectId, limit = 3) {
|
|
962
|
+
const rows = db.prepare(`
|
|
963
|
+
SELECT summary FROM sessions
|
|
964
|
+
WHERE project_id = ? AND summary IS NOT NULL AND summary != ''
|
|
965
|
+
ORDER BY started_at DESC LIMIT ?
|
|
966
|
+
`).all(projectId, limit);
|
|
967
|
+
return rows.map((r) => r.summary);
|
|
968
|
+
}
|
|
969
|
+
function insertObservation(db, obs) {
|
|
970
|
+
db.prepare(`
|
|
971
|
+
INSERT INTO observations (id, session_id, project_id, tool_name, content, category, created_at)
|
|
972
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
973
|
+
`).run(crypto.randomUUID(), obs.session_id, obs.project_id, obs.tool_name ?? null, obs.content, obs.category ?? null, Date.now());
|
|
974
|
+
}
|
|
975
|
+
function getSessionObservations(db, sessionId) {
|
|
976
|
+
return db.prepare("SELECT tool_name, content, category FROM observations WHERE session_id = ? AND promoted = 0 ORDER BY created_at ASC").all(sessionId);
|
|
977
|
+
}
|
|
978
|
+
function markObservationsPromoted(db, sessionId) {
|
|
979
|
+
db.prepare("UPDATE observations SET promoted = 1 WHERE session_id = ?").run(sessionId);
|
|
980
|
+
}
|
|
981
|
+
function insertMemory(db, m) {
|
|
982
|
+
const id = crypto.randomUUID();
|
|
983
|
+
db.prepare(`
|
|
984
|
+
INSERT INTO memories
|
|
985
|
+
(id, project_id, tier, category, trust, content, context,
|
|
986
|
+
source, embedding, weight, used_count, created_at, session_id)
|
|
987
|
+
VALUES
|
|
988
|
+
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?)
|
|
989
|
+
`).run(id, m.project_id, m.tier, m.category, m.trust, m.content, m.context ?? null, m.source, m.embedding ?? null, m.weight, Date.now(), m.session_id ?? null);
|
|
990
|
+
return id;
|
|
991
|
+
}
|
|
992
|
+
function searchBM25(db, projectId, query, limit) {
|
|
993
|
+
return db.prepare(`
|
|
994
|
+
SELECT m.* FROM memories_fts
|
|
995
|
+
JOIN memories m ON m.rowid = memories_fts.rowid
|
|
996
|
+
WHERE memories_fts MATCH ? AND m.project_id = ?
|
|
997
|
+
ORDER BY bm25(memories_fts) LIMIT ?
|
|
998
|
+
`).all(query, projectId, limit);
|
|
999
|
+
}
|
|
1000
|
+
function cosineSimilarity(a, b) {
|
|
1001
|
+
let dot = 0, na = 0, nb = 0;
|
|
1002
|
+
for (let i = 0;i < a.length; i++) {
|
|
1003
|
+
dot += a[i] * b[i];
|
|
1004
|
+
na += a[i] * a[i];
|
|
1005
|
+
nb += b[i] * b[i];
|
|
1006
|
+
}
|
|
1007
|
+
const d = Math.sqrt(na) * Math.sqrt(nb);
|
|
1008
|
+
return d === 0 ? 0 : dot / d;
|
|
1009
|
+
}
|
|
1010
|
+
function searchVector(db, projectId, queryEmb, limit) {
|
|
1011
|
+
const candidates = db.prepare("SELECT * FROM memories WHERE project_id = ? AND embedding IS NOT NULL").all(projectId);
|
|
1012
|
+
return candidates.map((m) => ({
|
|
1013
|
+
...m,
|
|
1014
|
+
similarity: cosineSimilarity(queryEmb, new Float32Array(m.embedding.buffer))
|
|
1015
|
+
})).sort((a, b) => b.similarity - a.similarity).slice(0, limit);
|
|
1016
|
+
}
|
|
1017
|
+
function mergeRRF(list1, list2, limit, allItems) {
|
|
1018
|
+
const k = 60;
|
|
1019
|
+
const scores = new Map;
|
|
1020
|
+
list1.forEach((r, i) => scores.set(r.id, (scores.get(r.id) ?? 0) + 1 / (i + k)));
|
|
1021
|
+
list2.forEach((r, i) => scores.set(r.id, (scores.get(r.id) ?? 0) + 1 / (i + k)));
|
|
1022
|
+
return [...scores.entries()].sort(([, a], [, b]) => b - a).slice(0, limit).map(([id]) => allItems.get(id)).filter(Boolean);
|
|
1023
|
+
}
|
|
1024
|
+
function applyMMR(candidates, queryEmb, limit, lambda = 0.7) {
|
|
1025
|
+
if (!queryEmb || candidates.length <= limit)
|
|
1026
|
+
return candidates.slice(0, limit);
|
|
1027
|
+
const selected = [];
|
|
1028
|
+
const remaining = [...candidates];
|
|
1029
|
+
while (selected.length < limit && remaining.length > 0) {
|
|
1030
|
+
let bestIdx = 0;
|
|
1031
|
+
let bestScore = -Infinity;
|
|
1032
|
+
for (let i = 0;i < remaining.length; i++) {
|
|
1033
|
+
const cand = remaining[i];
|
|
1034
|
+
const candEmb = cand.embedding ? new Float32Array(cand.embedding.buffer) : null;
|
|
1035
|
+
if (!candEmb) {
|
|
1036
|
+
bestIdx = i;
|
|
1037
|
+
break;
|
|
1038
|
+
}
|
|
1039
|
+
const relevance = cand.similarity ?? cosineSimilarity(queryEmb, candEmb);
|
|
1040
|
+
const maxSim = selected.reduce((max, s) => {
|
|
1041
|
+
if (!s.embedding)
|
|
1042
|
+
return max;
|
|
1043
|
+
const sim = cosineSimilarity(candEmb, new Float32Array(s.embedding.buffer));
|
|
1044
|
+
return Math.max(max, sim);
|
|
1045
|
+
}, 0);
|
|
1046
|
+
const score = lambda * relevance - (1 - lambda) * maxSim;
|
|
1047
|
+
if (score > bestScore) {
|
|
1048
|
+
bestScore = score;
|
|
1049
|
+
bestIdx = i;
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
selected.push(remaining[bestIdx]);
|
|
1053
|
+
remaining.splice(bestIdx, 1);
|
|
1054
|
+
}
|
|
1055
|
+
return selected;
|
|
1056
|
+
}
|
|
1057
|
+
function updateWeight(db, id, score) {
|
|
1058
|
+
const mem = db.prepare("SELECT weight, used_count FROM memories WHERE id = ?").get(id);
|
|
1059
|
+
if (!mem)
|
|
1060
|
+
return;
|
|
1061
|
+
const alpha = 0.3;
|
|
1062
|
+
const newWeight = alpha * (score / 3) + (1 - alpha) * mem.weight;
|
|
1063
|
+
const newTrust = newWeight > 1.4 || mem.used_count + 1 >= 3 ? "validated" : undefined;
|
|
1064
|
+
if (newTrust) {
|
|
1065
|
+
db.prepare("UPDATE memories SET weight = ?, used_count = used_count + 1, last_used = ?, trust = CASE WHEN trust = 'observed' THEN ? ELSE trust END WHERE id = ?").run(newWeight, Date.now(), newTrust, id);
|
|
1066
|
+
} else {
|
|
1067
|
+
db.prepare("UPDATE memories SET weight = ?, used_count = used_count + 1, last_used = ? WHERE id = ?").run(newWeight, Date.now(), id);
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
function decayAndPrune(db, projectId) {
|
|
1071
|
+
const cutoff = Date.now() - 7 * 24 * 60 * 60 * 1000;
|
|
1072
|
+
const decayed = db.prepare(`
|
|
1073
|
+
UPDATE memories
|
|
1074
|
+
SET weight = weight * CASE
|
|
1075
|
+
WHEN trust IN ('proven', 'canonical') THEN 1.0
|
|
1076
|
+
WHEN trust = 'validated' THEN 0.98
|
|
1077
|
+
ELSE 0.95
|
|
1078
|
+
END
|
|
1079
|
+
WHERE project_id = ?
|
|
1080
|
+
AND trust NOT IN ('canonical')
|
|
1081
|
+
AND (last_used IS NULL OR last_used < ?)
|
|
1082
|
+
`).run(projectId, cutoff).changes;
|
|
1083
|
+
const pruned = db.prepare(`
|
|
1084
|
+
DELETE FROM memories
|
|
1085
|
+
WHERE project_id = ? AND weight < 0.1 AND used_count > 3
|
|
1086
|
+
AND trust NOT IN ('proven', 'canonical')
|
|
1087
|
+
`).run(projectId).changes;
|
|
1088
|
+
return { decayed, pruned };
|
|
1089
|
+
}
|
|
1090
|
+
var import_better_sqlite3, CORTEX_DIR, DB_PATH, REGISTRY_PATH, MODELS_DIR, _db = null;
|
|
1091
|
+
var init_db = __esm(() => {
|
|
1092
|
+
import_better_sqlite3 = __toESM(require_lib(), 1);
|
|
1093
|
+
CORTEX_DIR = join(homedir(), ".apsolut");
|
|
1094
|
+
DB_PATH = join(CORTEX_DIR, "memory.db");
|
|
1095
|
+
REGISTRY_PATH = join(CORTEX_DIR, "registry.json");
|
|
1096
|
+
MODELS_DIR = join(CORTEX_DIR, "models");
|
|
1097
|
+
});
|
|
1098
|
+
|
|
1099
|
+
// src/cli.ts
|
|
1100
|
+
import {
|
|
1101
|
+
existsSync as existsSync3,
|
|
1102
|
+
mkdirSync as mkdirSync2,
|
|
1103
|
+
readFileSync as readFileSync2,
|
|
1104
|
+
writeFileSync as writeFileSync2
|
|
1105
|
+
} from "fs";
|
|
1106
|
+
import { join as join2, resolve, dirname } from "path";
|
|
1107
|
+
import { homedir as homedir2 } from "os";
|
|
1108
|
+
import { fileURLToPath } from "url";
|
|
1109
|
+
|
|
1110
|
+
// src/registry.ts
|
|
1111
|
+
init_db();
|
|
1112
|
+
import { existsSync as existsSync2, readFileSync, writeFileSync } from "fs";
|
|
1113
|
+
function readRegistry() {
|
|
1114
|
+
if (!existsSync2(REGISTRY_PATH))
|
|
1115
|
+
return { projects: {} };
|
|
1116
|
+
try {
|
|
1117
|
+
return JSON.parse(readFileSync(REGISTRY_PATH, "utf-8"));
|
|
1118
|
+
} catch {
|
|
1119
|
+
return { projects: {} };
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
function writeRegistry(reg) {
|
|
1123
|
+
writeFileSync(REGISTRY_PATH, JSON.stringify(reg, null, 2));
|
|
1124
|
+
}
|
|
1125
|
+
function registerProject(id, name, path) {
|
|
1126
|
+
const reg = readRegistry();
|
|
1127
|
+
reg.projects[id] = { name, path, registered_at: Date.now() };
|
|
1128
|
+
writeRegistry(reg);
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
// src/cli.ts
|
|
1132
|
+
var __filename2 = fileURLToPath(import.meta.url);
|
|
1133
|
+
var __dirname2 = dirname(__filename2);
|
|
1134
|
+
var PACKAGE_ROOT = resolve(__dirname2, "..");
|
|
1135
|
+
var IS_DIST = __dirname2.endsWith("dist") || __dirname2.includes(`${process.sep}dist${process.sep}`);
|
|
1136
|
+
var PROJECT_ROOT = process.cwd();
|
|
1137
|
+
var CLAUDE_SETTINGS = join2(homedir2(), ".claude", "settings.json");
|
|
1138
|
+
var MCP_JSON = join2(PROJECT_ROOT, ".mcp.json");
|
|
1139
|
+
var PROJECT_APSOLUT = join2(PROJECT_ROOT, ".apsolut");
|
|
1140
|
+
var PROJECT_CONFIG = join2(PROJECT_APSOLUT, "project.json");
|
|
1141
|
+
var cmd = process.argv[2];
|
|
1142
|
+
switch (cmd) {
|
|
1143
|
+
case "init":
|
|
1144
|
+
await init();
|
|
1145
|
+
break;
|
|
1146
|
+
case "status":
|
|
1147
|
+
await status();
|
|
1148
|
+
break;
|
|
1149
|
+
case "uninstall":
|
|
1150
|
+
uninstall();
|
|
1151
|
+
break;
|
|
1152
|
+
case "hook:session-start":
|
|
1153
|
+
await runHook("session-start");
|
|
1154
|
+
break;
|
|
1155
|
+
case "hook:post-tool-use":
|
|
1156
|
+
await runHook("post-tool-use");
|
|
1157
|
+
break;
|
|
1158
|
+
case "hook:stop":
|
|
1159
|
+
await runHook("stop");
|
|
1160
|
+
break;
|
|
1161
|
+
case "hook:session-end":
|
|
1162
|
+
await runHook("session-end");
|
|
1163
|
+
break;
|
|
1164
|
+
default:
|
|
1165
|
+
console.log(`
|
|
1166
|
+
apsolut-cortex v0.1.0
|
|
1167
|
+
|
|
1168
|
+
Commands:
|
|
1169
|
+
init Set up memory for this Claude Code project
|
|
1170
|
+
status Show memory stats for this project
|
|
1171
|
+
uninstall Remove hooks and MCP config (keeps DB)
|
|
1172
|
+
|
|
1173
|
+
DB: ~/.apsolut/memory.db
|
|
1174
|
+
Models: ~/.apsolut/models/
|
|
1175
|
+
`);
|
|
1176
|
+
}
|
|
1177
|
+
async function runHook(name) {
|
|
1178
|
+
const hookPath = IS_DIST ? join2(__dirname2, "hooks", `${name}.js`) : join2(__dirname2, "hooks", `${name}.ts`);
|
|
1179
|
+
if (!existsSync3(hookPath)) {
|
|
1180
|
+
process.stderr.write(`[apsolut-cortex] hook not found: ${hookPath}
|
|
1181
|
+
`);
|
|
1182
|
+
process.exit(0);
|
|
1183
|
+
}
|
|
1184
|
+
await import(hookPath);
|
|
1185
|
+
}
|
|
1186
|
+
async function init() {
|
|
1187
|
+
console.log(`
|
|
1188
|
+
apsolut-cortex init
|
|
1189
|
+
`);
|
|
1190
|
+
if (!existsSync3(PROJECT_APSOLUT)) {
|
|
1191
|
+
mkdirSync2(PROJECT_APSOLUT, { recursive: true });
|
|
1192
|
+
}
|
|
1193
|
+
let projectId;
|
|
1194
|
+
let projectName;
|
|
1195
|
+
if (existsSync3(PROJECT_CONFIG)) {
|
|
1196
|
+
const existing = JSON.parse(readFileSync2(PROJECT_CONFIG, "utf-8"));
|
|
1197
|
+
projectId = existing.id;
|
|
1198
|
+
projectName = existing.name;
|
|
1199
|
+
console.log(`✓ Project already initialised: ${projectName}`);
|
|
1200
|
+
} else {
|
|
1201
|
+
projectId = crypto.randomUUID();
|
|
1202
|
+
projectName = PROJECT_ROOT.split(/[\\/]/).filter(Boolean).pop() ?? "project";
|
|
1203
|
+
writeFileSync2(PROJECT_CONFIG, JSON.stringify({
|
|
1204
|
+
id: projectId,
|
|
1205
|
+
name: projectName,
|
|
1206
|
+
created_at: new Date().toISOString()
|
|
1207
|
+
}, null, 2));
|
|
1208
|
+
console.log(`✓ Created .apsolut/project.json`);
|
|
1209
|
+
console.log(` ID: ${projectId}`);
|
|
1210
|
+
console.log(` Name: ${projectName}`);
|
|
1211
|
+
}
|
|
1212
|
+
registerProject(projectId, projectName, PROJECT_ROOT);
|
|
1213
|
+
console.log(`✓ Registered in ~/.apsolut/registry.json`);
|
|
1214
|
+
const mcpServerPath = IS_DIST ? join2(__dirname2, "mcp", "server.js") : join2(__dirname2, "mcp", "server.ts");
|
|
1215
|
+
const mcpCommand = IS_DIST ? "node" : "bun";
|
|
1216
|
+
const mcpArgs = [mcpServerPath];
|
|
1217
|
+
let mcp = {};
|
|
1218
|
+
if (existsSync3(MCP_JSON)) {
|
|
1219
|
+
try {
|
|
1220
|
+
mcp = JSON.parse(readFileSync2(MCP_JSON, "utf-8"));
|
|
1221
|
+
} catch {}
|
|
1222
|
+
}
|
|
1223
|
+
const servers = mcp.mcpServers ?? {};
|
|
1224
|
+
servers["apsolut-cortex"] = {
|
|
1225
|
+
command: mcpCommand,
|
|
1226
|
+
args: mcpArgs,
|
|
1227
|
+
env: { APSOLUT_PROJECT_PATH: PROJECT_ROOT }
|
|
1228
|
+
};
|
|
1229
|
+
mcp.mcpServers = servers;
|
|
1230
|
+
writeFileSync2(MCP_JSON, JSON.stringify(mcp, null, 2));
|
|
1231
|
+
console.log(`✓ Written .mcp.json`);
|
|
1232
|
+
const hookCmd = IS_DIST ? "apsolut-cortex" : `bun run ${join2(__dirname2, "cli.ts")}`;
|
|
1233
|
+
const hookEntries = {
|
|
1234
|
+
SessionStart: [{ command: `${hookCmd} hook:session-start` }],
|
|
1235
|
+
PostToolUse: [{ command: `${hookCmd} hook:post-tool-use` }],
|
|
1236
|
+
Stop: [{ command: `${hookCmd} hook:stop` }],
|
|
1237
|
+
SessionEnd: [{ command: `${hookCmd} hook:session-end` }]
|
|
1238
|
+
};
|
|
1239
|
+
let settings = {};
|
|
1240
|
+
const settingsDir = dirname(CLAUDE_SETTINGS);
|
|
1241
|
+
if (!existsSync3(settingsDir))
|
|
1242
|
+
mkdirSync2(settingsDir, { recursive: true });
|
|
1243
|
+
if (existsSync3(CLAUDE_SETTINGS)) {
|
|
1244
|
+
try {
|
|
1245
|
+
settings = JSON.parse(readFileSync2(CLAUDE_SETTINGS, "utf-8"));
|
|
1246
|
+
} catch {}
|
|
1247
|
+
}
|
|
1248
|
+
const existingHooks = settings.hooks ?? {};
|
|
1249
|
+
let added = 0;
|
|
1250
|
+
for (const [event, entries] of Object.entries(hookEntries)) {
|
|
1251
|
+
const current = existingHooks[event] ?? [];
|
|
1252
|
+
const alreadyRegistered = current.some((e) => typeof e === "object" && e.command?.includes("apsolut-cortex"));
|
|
1253
|
+
if (!alreadyRegistered) {
|
|
1254
|
+
existingHooks[event] = [...current, ...entries];
|
|
1255
|
+
added++;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
settings.hooks = existingHooks;
|
|
1259
|
+
writeFileSync2(CLAUDE_SETTINGS, JSON.stringify(settings, null, 2));
|
|
1260
|
+
console.log(added > 0 ? `✓ Registered ${added} hooks in ~/.claude/settings.json` : `✓ Hooks already registered`);
|
|
1261
|
+
const gitignore = join2(PROJECT_ROOT, ".gitignore");
|
|
1262
|
+
if (existsSync3(gitignore)) {
|
|
1263
|
+
const content = readFileSync2(gitignore, "utf-8");
|
|
1264
|
+
if (!content.includes(".apsolut/")) {
|
|
1265
|
+
writeFileSync2(gitignore, content + `
|
|
1266
|
+
# apsolut-cortex
|
|
1267
|
+
.apsolut/
|
|
1268
|
+
`);
|
|
1269
|
+
console.log(`✓ Added .apsolut/ to .gitignore`);
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
const BANNER = `
|
|
1273
|
+
┌─────────────────────────────────────────────────────────┐
|
|
1274
|
+
│ │
|
|
1275
|
+
│ █████╗ ██████╗ ███████╗ ██████╗ ██╗ ██╗ ██╗████████╗ │
|
|
1276
|
+
│ ██╔══██╗██╔══██╗██╔════╝██╔═══██╗██║ ██║ ██║╚══██╔══╝ │
|
|
1277
|
+
│ ███████║██████╔╝███████╗██║ ██║██║ ██║ ██║ ██║ │
|
|
1278
|
+
│ ██╔══██║██╔═══╝ ╚════██║██║ ██║██║ ██║ ██║ ██║ │
|
|
1279
|
+
│ ██║ ██║██║ ███████║╚██████╔╝███████╗╚██████╔╝ ██║ │
|
|
1280
|
+
│ ╚═╝ ╚═╝╚═╝ ╚══════╝ ╚═════╝ ╚══════╝ ╚═════╝ ╚═╝ │
|
|
1281
|
+
│ │
|
|
1282
|
+
│ c o r t e x · v 0 . 1 . 0 │
|
|
1283
|
+
│ │
|
|
1284
|
+
└─────────────────────────────────────────────────────────┘
|
|
1285
|
+
|
|
1286
|
+
✓ project ${projectName}
|
|
1287
|
+
✓ id ${projectId.slice(0, 18)}...
|
|
1288
|
+
✓ memory ~/.apsolut/memory.db
|
|
1289
|
+
✓ models ~/.apsolut/models/
|
|
1290
|
+
|
|
1291
|
+
──────────────────────────────────────────────────────────
|
|
1292
|
+
|
|
1293
|
+
Restart Claude Code, then say "remember <topic>" to search.
|
|
1294
|
+
compression: ANTHROPIC_API_KEY → ollama fallback → loud error
|
|
1295
|
+
|
|
1296
|
+
`;
|
|
1297
|
+
console.log(BANNER);
|
|
1298
|
+
}
|
|
1299
|
+
async function status() {
|
|
1300
|
+
const { getDb: getDb2 } = await Promise.resolve().then(() => (init_db(), exports_db));
|
|
1301
|
+
if (!existsSync3(PROJECT_CONFIG)) {
|
|
1302
|
+
console.log("No project found. Run: apsolut-cortex init");
|
|
1303
|
+
process.exit(1);
|
|
1304
|
+
}
|
|
1305
|
+
const project = JSON.parse(readFileSync2(PROJECT_CONFIG, "utf-8"));
|
|
1306
|
+
const db = getDb2();
|
|
1307
|
+
const total = db.prepare("SELECT COUNT(*) as n FROM memories WHERE project_id = ?").get(project.id).n;
|
|
1308
|
+
const sessions = db.prepare("SELECT COUNT(*) as n FROM sessions WHERE project_id = ?").get(project.id).n;
|
|
1309
|
+
const byTrust = db.prepare(`
|
|
1310
|
+
SELECT trust, COUNT(*) as n FROM memories
|
|
1311
|
+
WHERE project_id = ? GROUP BY trust
|
|
1312
|
+
`).all(project.id);
|
|
1313
|
+
const recent = db.prepare(`
|
|
1314
|
+
SELECT summary, started_at FROM sessions
|
|
1315
|
+
WHERE project_id = ? AND summary IS NOT NULL
|
|
1316
|
+
ORDER BY started_at DESC LIMIT 3
|
|
1317
|
+
`).all(project.id);
|
|
1318
|
+
console.log(`
|
|
1319
|
+
apsolut-cortex — ${project.name}
|
|
1320
|
+
`);
|
|
1321
|
+
console.log(`Memories : ${total} across ${sessions} sessions`);
|
|
1322
|
+
byTrust.forEach((r) => console.log(` ${r.trust}: ${r.n}`));
|
|
1323
|
+
if (recent.length) {
|
|
1324
|
+
console.log(`
|
|
1325
|
+
Recent sessions:`);
|
|
1326
|
+
recent.forEach((r) => {
|
|
1327
|
+
const age = Math.round((Date.now() - r.started_at) / 86400000);
|
|
1328
|
+
console.log(` ${age}d ago: ${r.summary}`);
|
|
1329
|
+
});
|
|
1330
|
+
}
|
|
1331
|
+
console.log(`
|
|
1332
|
+
DB: ~/.apsolut/memory.db
|
|
1333
|
+
`);
|
|
1334
|
+
}
|
|
1335
|
+
function uninstall() {
|
|
1336
|
+
if (existsSync3(MCP_JSON)) {
|
|
1337
|
+
try {
|
|
1338
|
+
const mcp = JSON.parse(readFileSync2(MCP_JSON, "utf-8"));
|
|
1339
|
+
if (mcp.mcpServers?.["apsolut-cortex"]) {
|
|
1340
|
+
delete mcp.mcpServers["apsolut-cortex"];
|
|
1341
|
+
writeFileSync2(MCP_JSON, JSON.stringify(mcp, null, 2));
|
|
1342
|
+
console.log("✓ Removed from .mcp.json");
|
|
1343
|
+
}
|
|
1344
|
+
} catch {}
|
|
1345
|
+
}
|
|
1346
|
+
if (existsSync3(CLAUDE_SETTINGS)) {
|
|
1347
|
+
try {
|
|
1348
|
+
const settings = JSON.parse(readFileSync2(CLAUDE_SETTINGS, "utf-8"));
|
|
1349
|
+
const hooks = settings.hooks;
|
|
1350
|
+
if (hooks) {
|
|
1351
|
+
for (const event of ["SessionStart", "PostToolUse", "Stop", "SessionEnd"]) {
|
|
1352
|
+
if (hooks[event]) {
|
|
1353
|
+
hooks[event] = hooks[event].filter((e) => !(typeof e === "object" && e.command?.includes("apsolut-cortex")));
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
writeFileSync2(CLAUDE_SETTINGS, JSON.stringify(settings, null, 2));
|
|
1357
|
+
console.log("✓ Removed hooks from ~/.claude/settings.json");
|
|
1358
|
+
}
|
|
1359
|
+
} catch {}
|
|
1360
|
+
}
|
|
1361
|
+
console.log(`
|
|
1362
|
+
Uninstalled. DB at ~/.apsolut/memory.db kept.
|
|
1363
|
+
`);
|
|
1364
|
+
}
|