local-agent-memory 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/search.js +63 -19
- package/dist/db/project.d.ts +1 -0
- package/dist/db/project.js +49 -11
- package/package.json +1 -1
package/dist/core/search.js
CHANGED
|
@@ -27,7 +27,7 @@ async function hybridSearch(query, limit, db, filter) {
|
|
|
27
27
|
const asyncDb = new sync_1.AsyncDatabase(db);
|
|
28
28
|
const RRF_K = 60;
|
|
29
29
|
// 1. FTS5 Search
|
|
30
|
-
const ftsResults = await new Promise((resolve
|
|
30
|
+
const ftsResults = await new Promise((resolve) => {
|
|
31
31
|
// Sanitize query to prevent FTS5 syntax errors
|
|
32
32
|
const ftsQuery = sanitizeFtsQuery(query);
|
|
33
33
|
if (!ftsQuery) {
|
|
@@ -35,27 +35,71 @@ async function hybridSearch(query, limit, db, filter) {
|
|
|
35
35
|
}
|
|
36
36
|
// using bm25() for scoring in FTS5
|
|
37
37
|
db.all(`SELECT rowid as id, rank as score FROM chunks_fts WHERE chunks_fts MATCH ? ORDER BY rank LIMIT 100`, [ftsQuery], (err, rows) => {
|
|
38
|
-
if (err)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
if (err) {
|
|
39
|
+
resolve([]);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
resolve(rows || []);
|
|
43
|
+
}
|
|
42
44
|
});
|
|
43
45
|
});
|
|
44
|
-
// 2. Vector Search
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
46
|
+
// 2. Vector Search (with graceful fallback)
|
|
47
|
+
let vecResults = [];
|
|
48
|
+
try {
|
|
49
|
+
const queryEmbedding = await (0, embeddings_1.generateEmbedding)(query);
|
|
50
|
+
const vectorData = Buffer.from(new Float32Array(queryEmbedding).buffer);
|
|
51
|
+
vecResults = await new Promise((resolve) => {
|
|
52
|
+
// sqlite-vec distance function. distance is lower = better (closer)
|
|
53
|
+
// we want smallest distance first
|
|
54
|
+
db.all(`SELECT rowid as id, distance FROM chunks_vec
|
|
55
|
+
WHERE embedding MATCH ? AND k = 100
|
|
56
|
+
ORDER BY distance`, [vectorData], (err, rows) => {
|
|
57
|
+
if (!err && rows && rows.length > 0) {
|
|
58
|
+
return resolve(rows);
|
|
59
|
+
}
|
|
60
|
+
// Fallback: If chunks_vec is a regular table (no virtual distance/MATCH)
|
|
61
|
+
// or sqlite-vec is not loaded, compute cosine similarity in-memory using stored BLOBs.
|
|
62
|
+
db.all(`SELECT rowid as id, embedding FROM chunks_vec WHERE embedding IS NOT NULL`, [], (fallbackErr, fallbackRows) => {
|
|
63
|
+
if (fallbackErr || !fallbackRows || fallbackRows.length === 0) {
|
|
64
|
+
return resolve([]);
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const queryFloats = new Float32Array(queryEmbedding);
|
|
68
|
+
const scored = [];
|
|
69
|
+
for (const row of fallbackRows) {
|
|
70
|
+
if (!row.embedding)
|
|
71
|
+
continue;
|
|
72
|
+
const buf = row.embedding;
|
|
73
|
+
const storedFloats = new Float32Array(buf.buffer, buf.byteOffset, buf.byteLength / 4);
|
|
74
|
+
if (storedFloats.length !== queryFloats.length)
|
|
75
|
+
continue;
|
|
76
|
+
let dotProduct = 0;
|
|
77
|
+
for (let i = 0; i < queryFloats.length; i++) {
|
|
78
|
+
const q = queryFloats[i];
|
|
79
|
+
const s = storedFloats[i];
|
|
80
|
+
if (q !== undefined && s !== undefined) {
|
|
81
|
+
dotProduct += q * s;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// MiniLM embeddings are normalized, so cosine_distance = 1 - dotProduct
|
|
85
|
+
scored.push({
|
|
86
|
+
id: row.id,
|
|
87
|
+
distance: 1 - dotProduct,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
scored.sort((a, b) => a.distance - b.distance);
|
|
91
|
+
resolve(scored.slice(0, 100));
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
resolve([]);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
});
|
|
57
98
|
});
|
|
58
|
-
}
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
vecResults = [];
|
|
102
|
+
}
|
|
59
103
|
// 3. RRF Fusion
|
|
60
104
|
const rrfScores = new Map();
|
|
61
105
|
// Add FTS scores
|
package/dist/db/project.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export interface FileManifestRow {
|
|
|
4
4
|
mtime: number;
|
|
5
5
|
sha256: string;
|
|
6
6
|
}
|
|
7
|
+
export declare function getVecExtensionPath(): string | null;
|
|
7
8
|
export declare function getLocalDbPath(projectRoot: string): string;
|
|
8
9
|
export declare function initLocalDb(dbPath: string): sqlite3.Database;
|
|
9
10
|
export declare function upsertFileManifest(dbPath: string, file_path: string, mtime: number, sha256: string): Promise<void>;
|
package/dist/db/project.js
CHANGED
|
@@ -3,14 +3,33 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getVecExtensionPath = getVecExtensionPath;
|
|
6
7
|
exports.getLocalDbPath = getLocalDbPath;
|
|
7
8
|
exports.initLocalDb = initLocalDb;
|
|
8
9
|
exports.upsertFileManifest = upsertFileManifest;
|
|
9
10
|
exports.getFileManifest = getFileManifest;
|
|
10
11
|
exports.getAllManifests = getAllManifests;
|
|
11
12
|
exports.removeFileManifest = removeFileManifest;
|
|
13
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
14
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
12
15
|
const node_path_1 = __importDefault(require("node:path"));
|
|
13
16
|
const sqlite3_1 = __importDefault(require("sqlite3"));
|
|
17
|
+
function getVecExtensionPath() {
|
|
18
|
+
try {
|
|
19
|
+
const memDir = process.env.MEMORY_GLOBAL_DIR || node_path_1.default.join(node_os_1.default.homedir(), ".local_memory");
|
|
20
|
+
const binDir = node_path_1.default.join(memDir, "bin");
|
|
21
|
+
const extFile = process.platform === "win32"
|
|
22
|
+
? "vec0.dll"
|
|
23
|
+
: process.platform === "darwin"
|
|
24
|
+
? "vec0.dylib"
|
|
25
|
+
: "vec0.so";
|
|
26
|
+
const extPath = node_path_1.default.join(binDir, extFile);
|
|
27
|
+
return node_fs_1.default.existsSync(extPath) ? extPath : null;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
14
33
|
function getLocalDbPath(projectRoot) {
|
|
15
34
|
return node_path_1.default.join(projectRoot, ".memory", "local.sqlite");
|
|
16
35
|
}
|
|
@@ -44,22 +63,41 @@ function initLocalDb(dbPath) {
|
|
|
44
63
|
endLine INTEGER
|
|
45
64
|
)
|
|
46
65
|
`);
|
|
47
|
-
// Note: for real usage, chunks_fts and chunks_vec need FTS5 and vec0 extensions enabled.
|
|
48
|
-
// Standard sqlite3 node driver has FTS5, but vec0 must be loaded.
|
|
49
|
-
// Assuming they are available or using regular tables for E2E.
|
|
50
66
|
db.run(`
|
|
51
67
|
CREATE VIRTUAL TABLE IF NOT EXISTS chunks_fts USING fts5(
|
|
52
68
|
path, text, breadcrumbs, content='chunks', content_rowid='id'
|
|
53
69
|
)
|
|
54
70
|
`);
|
|
55
|
-
//
|
|
56
|
-
//
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
71
|
+
// Load sqlite-vec extension if downloaded, and create virtual table vec0.
|
|
72
|
+
// Falls back to regular table if extension is not present or fails to load.
|
|
73
|
+
const extPath = getVecExtensionPath();
|
|
74
|
+
if (extPath) {
|
|
75
|
+
db.loadExtension(extPath, (err) => {
|
|
76
|
+
if (!err) {
|
|
77
|
+
db.run(`
|
|
78
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS chunks_vec USING vec0(
|
|
79
|
+
embedding float[384]
|
|
80
|
+
)
|
|
81
|
+
`);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
db.run(`
|
|
85
|
+
CREATE TABLE IF NOT EXISTS chunks_vec (
|
|
86
|
+
rowid INTEGER,
|
|
87
|
+
embedding BLOB
|
|
88
|
+
)
|
|
89
|
+
`);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
db.run(`
|
|
95
|
+
CREATE TABLE IF NOT EXISTS chunks_vec (
|
|
96
|
+
rowid INTEGER,
|
|
97
|
+
embedding BLOB
|
|
98
|
+
)
|
|
99
|
+
`);
|
|
100
|
+
}
|
|
63
101
|
});
|
|
64
102
|
return db;
|
|
65
103
|
}
|