@kybernesis/brain-storage-vec 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.
@@ -0,0 +1,29 @@
1
+ /**
2
+ * brain-storage-vec — sqlite-vec implementation of the VectorStore primitive.
3
+ *
4
+ * Port of cortex-appydave's `cortex-provider-sqlite-vec` adapted to the
5
+ * brain-core `TenantContext` pattern. The schema matches KAD's vectors.db
6
+ * (CANONICAL.md §1.3): a single `chunks` vec0 virtual table plus a
7
+ * `chunk_meta` table for the searchable fields.
8
+ *
9
+ * Embedding dimension is fixed at 1536 (text-embedding-3-small), per
10
+ * CANONICAL.md §2 "Embedding lock". Content is capped at 8192 chars upstream.
11
+ *
12
+ * Consumers must install `better-sqlite3` and `sqlite-vec` as peer deps.
13
+ */
14
+ import Database from 'better-sqlite3';
15
+ import type { TenantContext } from '@kybernesis/brain-contracts';
16
+ export declare const EMBEDDING_DIM = 1536;
17
+ /**
18
+ * Open (or return cached) the vectors DB for this tenant. Loads the
19
+ * sqlite-vec extension and ensures the schema. Throws on extension load
20
+ * failure — callers that want graceful no-op behaviour should catch.
21
+ */
22
+ export declare function getVectorDb(t: TenantContext): Database.Database;
23
+ export declare function closeVectorDb(slug: string): void;
24
+ export declare function closeAllVectorDbs(): void;
25
+ /** Convert a number[] embedding into the Float32 buffer sqlite-vec expects. */
26
+ export declare function toFloat32Buffer(vector: number[]): Buffer;
27
+ /** Returns count of indexed chunks. */
28
+ export declare function vectorCount(t: TenantContext): number;
29
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AAItC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAEjE,eAAO,MAAM,aAAa,OAAO,CAAC;AAuBlC;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,aAAa,GAAG,QAAQ,CAAC,QAAQ,CAmB/D;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAOhD;AAED,wBAAgB,iBAAiB,IAAI,IAAI,CAMxC;AAED,+EAA+E;AAC/E,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAExD;AAED,uCAAuC;AACvC,wBAAgB,WAAW,CAAC,CAAC,EAAE,aAAa,GAAG,MAAM,CAIpD"}
package/dist/index.js ADDED
@@ -0,0 +1,91 @@
1
+ /**
2
+ * brain-storage-vec — sqlite-vec implementation of the VectorStore primitive.
3
+ *
4
+ * Port of cortex-appydave's `cortex-provider-sqlite-vec` adapted to the
5
+ * brain-core `TenantContext` pattern. The schema matches KAD's vectors.db
6
+ * (CANONICAL.md §1.3): a single `chunks` vec0 virtual table plus a
7
+ * `chunk_meta` table for the searchable fields.
8
+ *
9
+ * Embedding dimension is fixed at 1536 (text-embedding-3-small), per
10
+ * CANONICAL.md §2 "Embedding lock". Content is capped at 8192 chars upstream.
11
+ *
12
+ * Consumers must install `better-sqlite3` and `sqlite-vec` as peer deps.
13
+ */
14
+ import Database from 'better-sqlite3';
15
+ import { mkdirSync } from 'node:fs';
16
+ import { dirname } from 'node:path';
17
+ import * as sqliteVec from 'sqlite-vec';
18
+ export const EMBEDDING_DIM = 1536;
19
+ const pool = new Map();
20
+ const schemaInit = new Set();
21
+ function schemaDDL() {
22
+ return `
23
+ CREATE VIRTUAL TABLE IF NOT EXISTS chunks USING vec0(
24
+ embedding float[${EMBEDDING_DIM}]
25
+ );
26
+
27
+ CREATE TABLE IF NOT EXISTS chunk_meta (
28
+ rowid INTEGER PRIMARY KEY,
29
+ ts TEXT NOT NULL,
30
+ source TEXT,
31
+ content TEXT NOT NULL,
32
+ origin_id TEXT
33
+ );
34
+
35
+ CREATE INDEX IF NOT EXISTS chunk_meta_origin ON chunk_meta(origin_id);
36
+ `;
37
+ }
38
+ /**
39
+ * Open (or return cached) the vectors DB for this tenant. Loads the
40
+ * sqlite-vec extension and ensures the schema. Throws on extension load
41
+ * failure — callers that want graceful no-op behaviour should catch.
42
+ */
43
+ export function getVectorDb(t) {
44
+ const cached = pool.get(t.slug);
45
+ if (cached)
46
+ return cached;
47
+ const p = t.paths.vectorsDbPath;
48
+ mkdirSync(dirname(p), { recursive: true });
49
+ const db = new Database(p);
50
+ sqliteVec.load(db);
51
+ db.pragma('journal_mode = WAL');
52
+ db.pragma('synchronous = NORMAL');
53
+ if (!schemaInit.has(t.slug)) {
54
+ db.exec(schemaDDL());
55
+ schemaInit.add(t.slug);
56
+ }
57
+ pool.set(t.slug, db);
58
+ return db;
59
+ }
60
+ export function closeVectorDb(slug) {
61
+ const db = pool.get(slug);
62
+ if (db) {
63
+ try {
64
+ db.close();
65
+ }
66
+ catch { /* ignore */ }
67
+ pool.delete(slug);
68
+ schemaInit.delete(slug);
69
+ }
70
+ }
71
+ export function closeAllVectorDbs() {
72
+ for (const [, db] of pool) {
73
+ try {
74
+ db.close();
75
+ }
76
+ catch { /* ignore */ }
77
+ }
78
+ pool.clear();
79
+ schemaInit.clear();
80
+ }
81
+ /** Convert a number[] embedding into the Float32 buffer sqlite-vec expects. */
82
+ export function toFloat32Buffer(vector) {
83
+ return Buffer.from(new Float32Array(vector).buffer);
84
+ }
85
+ /** Returns count of indexed chunks. */
86
+ export function vectorCount(t) {
87
+ const db = getVectorDb(t);
88
+ const row = db.prepare('SELECT COUNT(*) AS c FROM chunk_meta').get();
89
+ return row.c;
90
+ }
91
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,OAAO,EAA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AAC,CAAC;IAC1B,IAAI,EAAE,EAAE,CAAC;QACP,IAAI,CAAC;YAAC,EAAE,CAAC,KAAK,EAAE,CAAC;QAAC,CAAC;QAAC,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC;QAC1C,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAClB,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;AACH,CAAC;AAED,MAAM,UAAU,iBAAiB;IAC/B,KAAK,MAAM,CAAC,EAAE,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC;QAC1B,IAAI,CAAC;YAAC,EAAE,CAAC,KAAK,EAAE,CAAC;QAAC,CAAC;QAAC,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC;IAC5C,CAAC;IACD,IAAI,CAAC,KAAK,EAAE,CAAC;IACb,UAAU,CAAC,KAAK,EAAE,CAAC;AACrB,CAAC;AAED,+EAA+E;AAC/E,MAAM,UAAU,eAAe,CAAC,MAAgB;IAC9C,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,YAAY,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;AACtD,CAAC;AAED,uCAAuC;AACvC,MAAM,UAAU,WAAW,CAAC,CAAgB;IAC1C,MAAM,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;IAC1B,MAAM,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,sCAAsC,CAAC,CAAC,GAAG,EAAmB,CAAC;IACtF,OAAO,GAAG,CAAC,CAAC,CAAC;AACf,CAAC"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@kybernesis/brain-storage-vec",
3
+ "version": "0.1.0",
4
+ "description": "sqlite-vec VectorStore implementation for brain-core (1536-dim, cap 8192)",
5
+ "license": "MIT",
6
+ "author": "David Cruwys (AppyDave)",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/KybernesisAI/cortex.git",
10
+ "directory": "packages/brain-storage-vec"
11
+ },
12
+ "homepage": "https://github.com/KybernesisAI/cortex/tree/main/packages/brain-storage-vec#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/KybernesisAI/cortex/issues"
15
+ },
16
+ "type": "module",
17
+ "main": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "README.md"
28
+ ],
29
+ "dependencies": {
30
+ "@kybernesis/brain-contracts": "0.1.0"
31
+ },
32
+ "peerDependencies": {
33
+ "better-sqlite3": ">=9.0.0",
34
+ "sqlite-vec": ">=0.1.0"
35
+ },
36
+ "devDependencies": {
37
+ "@types/better-sqlite3": "^7.6.0",
38
+ "better-sqlite3": "^12.0.0",
39
+ "sqlite-vec": "^0.1.9",
40
+ "@kybernesis/brain-testkit": "0.1.0"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public"
44
+ },
45
+ "scripts": {
46
+ "build": "tsc -b",
47
+ "clean": "tsc -b --clean",
48
+ "typecheck": "tsc -b"
49
+ }
50
+ }