open-memex 0.11.0-alpha.2 → 0.11.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.
@@ -1,82 +1,82 @@
1
- /**
2
- * D96: disk cache for the cross-lingual rerank's text embeddings.
3
- *
4
- * Vectors are keyed by `(model, sha256(text))` — not by memory id — so an
5
- * edited memory re-embeds under a new key while an unchanged one stays warm
6
- * across supersede chains, scope moves, and index rebuilds. `model` is part of
7
- * the key so changing `embeddingModel` can never mix two vector spaces.
8
- *
9
- * Like the D77 counters and the D91 graph, this is derived local telemetry:
10
- * it lives in a `CREATE TABLE IF NOT EXISTS` table outside the versioned
11
- * index schema and is never dropped by `rebuildIndexSchema`. It never leaves
12
- * the machine and never appears in Markdown.
13
- *
14
- * Vectors are stored as little-endian float32 BLOBs: 1.5 KB for the 384-dim
15
- * default model, versus ~8 KB as JSON. Rows are written one per new text, so
16
- * a save costs O(1), not a rewrite of the whole cache.
17
- */
18
- import { db } from "./db.ts";
19
-
20
- function encode(values: readonly number[]): Uint8Array {
21
- const bytes = new Uint8Array(values.length * 4);
22
- const view = new DataView(bytes.buffer);
23
- for (let i = 0; i < values.length; i++) view.setFloat32(i * 4, values[i]!, true);
24
- return bytes;
25
- }
26
-
27
- function decode(bytes: Uint8Array): number[] {
28
- const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
29
- const n = Math.floor(bytes.byteLength / 4);
30
- const out = new Array<number>(n);
31
- for (let i = 0; i < n; i++) out[i] = view.getFloat32(i * 4, true);
32
- return out;
33
- }
34
-
35
- /** Load the cached vectors for `hashes`. Missing hashes are simply absent. */
36
- export function loadVectors(model: string, hashes: readonly string[]): Map<string, number[]> {
37
- const out = new Map<string, number[]>();
38
- if (hashes.length === 0) return out;
39
- const d = db();
40
- // SQLite's default variable limit is 999; chunk well below it.
41
- const CHUNK = 500;
42
- for (let i = 0; i < hashes.length; i += CHUNK) {
43
- const chunk = hashes.slice(i, i + CHUNK);
44
- const rows = d
45
- .prepare(
46
- `SELECT hash, vector FROM embedding_vectors
47
- WHERE model = ? AND hash IN (${chunk.map(() => "?").join(",")})`,
48
- )
49
- .all(model, ...chunk) as Array<{ hash: string; vector: Uint8Array | Buffer }>;
50
- for (const r of rows) {
51
- const bytes = new Uint8Array(r.vector.buffer, r.vector.byteOffset, r.vector.byteLength);
52
- out.set(r.hash, decode(bytes));
53
- }
54
- }
55
- return out;
56
- }
57
-
58
- /** Insert or replace one vector. Idempotent on `(model, hash)`. */
59
- export function saveVector(model: string, hash: string, values: readonly number[]): void {
60
- db()
61
- .prepare(
62
- `INSERT INTO embedding_vectors (model, hash, dim, vector) VALUES (?, ?, ?, ?)
63
- ON CONFLICT(model, hash) DO UPDATE SET dim = excluded.dim, vector = excluded.vector`,
64
- )
65
- .run(model, hash, values.length, encode(values));
66
- }
67
-
68
- /** Cache size for this model — `doctor` and the D96 docs quote this. */
69
- export function countVectors(model: string): number {
70
- const row = db()
71
- .prepare(`SELECT COUNT(*) AS n FROM embedding_vectors WHERE model = ?`)
72
- .get(model) as { n: number };
73
- return row.n;
74
- }
75
-
76
- /** Drop every vector for one model (a model switch or an explicit reset). */
77
- export function clearVectors(model: string): number {
78
- const d = db();
79
- const before = countVectors(model);
80
- d.prepare(`DELETE FROM embedding_vectors WHERE model = ?`).run(model);
81
- return before;
82
- }
1
+ /**
2
+ * D96: disk cache for the cross-lingual rerank's text embeddings.
3
+ *
4
+ * Vectors are keyed by `(model, sha256(text))` — not by memory id — so an
5
+ * edited memory re-embeds under a new key while an unchanged one stays warm
6
+ * across supersede chains, scope moves, and index rebuilds. `model` is part of
7
+ * the key so changing `embeddingModel` can never mix two vector spaces.
8
+ *
9
+ * Like the D77 counters and the D91 graph, this is derived local telemetry:
10
+ * it lives in a `CREATE TABLE IF NOT EXISTS` table outside the versioned
11
+ * index schema and is never dropped by `rebuildIndexSchema`. It never leaves
12
+ * the machine and never appears in Markdown.
13
+ *
14
+ * Vectors are stored as little-endian float32 BLOBs: 1.5 KB for the 384-dim
15
+ * default model, versus ~8 KB as JSON. Rows are written one per new text, so
16
+ * a save costs O(1), not a rewrite of the whole cache.
17
+ */
18
+ import { db } from "./db.ts";
19
+
20
+ function encode(values: readonly number[]): Uint8Array {
21
+ const bytes = new Uint8Array(values.length * 4);
22
+ const view = new DataView(bytes.buffer);
23
+ for (let i = 0; i < values.length; i++) view.setFloat32(i * 4, values[i]!, true);
24
+ return bytes;
25
+ }
26
+
27
+ function decode(bytes: Uint8Array): number[] {
28
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
29
+ const n = Math.floor(bytes.byteLength / 4);
30
+ const out = new Array<number>(n);
31
+ for (let i = 0; i < n; i++) out[i] = view.getFloat32(i * 4, true);
32
+ return out;
33
+ }
34
+
35
+ /** Load the cached vectors for `hashes`. Missing hashes are simply absent. */
36
+ export function loadVectors(model: string, hashes: readonly string[]): Map<string, number[]> {
37
+ const out = new Map<string, number[]>();
38
+ if (hashes.length === 0) return out;
39
+ const d = db();
40
+ // SQLite's default variable limit is 999; chunk well below it.
41
+ const CHUNK = 500;
42
+ for (let i = 0; i < hashes.length; i += CHUNK) {
43
+ const chunk = hashes.slice(i, i + CHUNK);
44
+ const rows = d
45
+ .prepare(
46
+ `SELECT hash, vector FROM embedding_vectors
47
+ WHERE model = ? AND hash IN (${chunk.map(() => "?").join(",")})`,
48
+ )
49
+ .all(model, ...chunk) as Array<{ hash: string; vector: Uint8Array | Buffer }>;
50
+ for (const r of rows) {
51
+ const bytes = new Uint8Array(r.vector.buffer, r.vector.byteOffset, r.vector.byteLength);
52
+ out.set(r.hash, decode(bytes));
53
+ }
54
+ }
55
+ return out;
56
+ }
57
+
58
+ /** Insert or replace one vector. Idempotent on `(model, hash)`. */
59
+ export function saveVector(model: string, hash: string, values: readonly number[]): void {
60
+ db()
61
+ .prepare(
62
+ `INSERT INTO embedding_vectors (model, hash, dim, vector) VALUES (?, ?, ?, ?)
63
+ ON CONFLICT(model, hash) DO UPDATE SET dim = excluded.dim, vector = excluded.vector`,
64
+ )
65
+ .run(model, hash, values.length, encode(values));
66
+ }
67
+
68
+ /** Cache size for this model — `doctor` and the D96 docs quote this. */
69
+ export function countVectors(model: string): number {
70
+ const row = db()
71
+ .prepare(`SELECT COUNT(*) AS n FROM embedding_vectors WHERE model = ?`)
72
+ .get(model) as { n: number };
73
+ return row.n;
74
+ }
75
+
76
+ /** Drop every vector for one model (a model switch or an explicit reset). */
77
+ export function clearVectors(model: string): number {
78
+ const d = db();
79
+ const before = countVectors(model);
80
+ d.prepare(`DELETE FROM embedding_vectors WHERE model = ?`).run(model);
81
+ return before;
82
+ }