-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrag-db.ts
More file actions
315 lines (283 loc) · 14 KB
/
Copy pathrag-db.ts
File metadata and controls
315 lines (283 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import * as fs from "node:fs";
import * as path from "node:path";
import { app } from "electron";
import Database from "better-sqlite3";
import * as caseEncryption from "./case-encryption";
import { CaseDataLockedError } from "./case-encryption";
export interface CollectionRow {
id: string;
name: string;
folder_path: string;
embedding_model: string;
created_at: number;
updated_at: number;
}
export interface DocumentRow {
id: string;
collection_id: string;
path: string;
name: string;
content_hash: string;
size: number;
mtime_ms: number;
page_count: number | null;
indexed_at: number;
}
export interface ChunkInput {
text: string;
tokenCount: number;
heading: string | null;
page: number | null;
startLine: number;
endLine: number;
embedding: number[];
}
export interface ChunkRow {
id: string;
document_id: string;
collection_id: string;
ordinal: number;
text: string;
token_count: number;
heading: string | null;
page: number | null;
start_line: number;
end_line: number;
embedding: Buffer;
}
function filePath(): string {
return path.join(app.getPath("userData"), "rag.db");
}
let db: Database.Database | null = null;
// Module-level singleton, opened lazily so tests (and any code running
// before app.getPath is available) don't pay for it until first use.
export function getDb(): Database.Database {
if (db) return db;
fs.mkdirSync(path.dirname(filePath()), { recursive: true });
db = new Database(filePath());
db.pragma("journal_mode = WAL");
db.exec(`
CREATE TABLE IF NOT EXISTS collections (
id TEXT PRIMARY KEY, name TEXT NOT NULL, folder_path TEXT NOT NULL UNIQUE,
embedding_model TEXT NOT NULL, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS documents (
id TEXT PRIMARY KEY, collection_id TEXT NOT NULL REFERENCES collections(id) ON DELETE CASCADE,
path TEXT NOT NULL, name TEXT NOT NULL, content_hash TEXT NOT NULL,
size INTEGER NOT NULL, mtime_ms INTEGER NOT NULL, page_count INTEGER, indexed_at INTEGER NOT NULL,
UNIQUE(collection_id, path)
);
CREATE TABLE IF NOT EXISTS chunks (
id TEXT PRIMARY KEY, document_id TEXT NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
collection_id TEXT NOT NULL REFERENCES collections(id) ON DELETE CASCADE,
ordinal INTEGER NOT NULL, text TEXT NOT NULL, token_count INTEGER NOT NULL,
heading TEXT, page INTEGER, start_line INTEGER NOT NULL, end_line INTEGER NOT NULL,
embedding BLOB NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_chunks_collection ON chunks(collection_id);
CREATE INDEX IF NOT EXISTS idx_documents_collection ON documents(collection_id);
`);
return db;
}
// Exposed for tests only — the electron mock points app.getPath("userData")
// at a real temp directory shared for the whole test process, so without
// this, rows from one test would leak into the next via the same rag.db file.
export function clearAllForTests(): void {
getDb().exec(`DELETE FROM chunks; DELETE FROM documents; DELETE FROM collections;`);
}
export function encodeEmbedding(vector: number[]): Buffer {
return Buffer.from(Float32Array.from(vector).buffer);
}
export function decodeEmbedding(buffer: Buffer): Float32Array {
return new Float32Array(buffer.buffer, buffer.byteOffset, buffer.length / Float32Array.BYTES_PER_ELEMENT);
}
// --- At-rest encryption for extracted document content ---------------------
//
// RAG folders are arbitrary local directories a user points the app at —
// just as capable of containing clinical documents as a Patient Case is,
// and unlike Patient Cases/chat sessions this store had no encryption at
// all until now. Reuses case-encryption.ts's existing passphrase-derived
// key and AES-256-GCM primitives — the same "Enable encryption" toggle in
// Settings → Audit & Privacy covers this store too, not a second one to
// configure separately.
//
// What's encrypted: `collections.name`, `documents.name`, `chunks.text`,
// `chunks.heading` — the actual retrievable/displayable content.
//
// What's deliberately NOT encrypted, and why:
// - `documents.path` / `collections.folder_path`: both are SQL
// equality-lookup/uniqueness keys (`getDocument`, `getCollectionByPath`,
// the `UNIQUE` constraints themselves). AES-GCM's random IV makes the
// same plaintext produce different ciphertext every time, which breaks
// exact-match lookups entirely without a separate deterministic/
// blind-index scheme this file doesn't implement. A filename or folder
// name is a real but smaller information leak than the full extracted
// text of every document in it — this tradeoff is deliberate and
// documented, not an oversight.
// - `chunks.embedding`: not human-readable, and similarity search
// (`cosineSimilarity` in rag.ts) needs to compute against it directly —
// encrypting it would mean decrypting every row on every query just to
// search, with no confidentiality benefit since the vector itself
// doesn't reveal document content the way its source text does.
// - Everything else (hashes, sizes, timestamps, ordinals, line/page
// numbers): structural metadata, not content.
//
// Same locked-state guarantee as patient-cases-store.ts/sessions-store.ts:
// reading or writing an encrypted field while enabled-but-locked throws
// CaseDataLockedError rather than silently returning garbage or an empty
// result.
function readField(raw: string): string {
if (!caseEncryption.isEnabled()) return raw;
if (!caseEncryption.isUnlocked()) throw new CaseDataLockedError();
const payload = JSON.parse(raw) as caseEncryption.EncryptedPayload;
return caseEncryption.decrypt(payload, caseEncryption.getSessionKey()!);
}
function writeField(plain: string): string {
if (!caseEncryption.isEnabled()) return plain;
if (!caseEncryption.isUnlocked()) throw new CaseDataLockedError();
return JSON.stringify(caseEncryption.encrypt(plain, caseEncryption.getSessionKey()!));
}
function decryptCollection(row: CollectionRow): CollectionRow {
return { ...row, name: readField(row.name) };
}
function decryptDocument(row: DocumentRow): DocumentRow {
return { ...row, name: readField(row.name) };
}
function decryptChunk<T extends { text: string; heading: string | null }>(row: T): T {
return { ...row, text: readField(row.text), heading: row.heading === null ? null : readField(row.heading) };
}
export interface RagContentForMigration {
collections: { id: string; name: string }[];
documents: { id: string; name: string }[];
chunks: { id: string; text: string; heading: string | null }[];
}
/** Reads every collection/document/chunk's encrypted-or-plaintext content
* fields under whichever mode is active right now, decrypted to plaintext
* — mirrors patient-cases-store.ts's getAllCasesForMigration(): used by the
* encryption setup/disable/rotate-passphrase flows (encryption-handlers.ts)
* to move this content between plaintext and encrypted storage. Never
* touches embeddings, paths, or any other column, so no re-embedding is
* ever needed just to change encryption state. */
export function getAllContentForMigration(): RagContentForMigration {
const db = getDb();
const collections = (db.prepare(`SELECT id, name FROM collections`).all() as { id: string; name: string }[]).map((r) => ({
id: r.id,
name: readField(r.name),
}));
const documents = (db.prepare(`SELECT id, name FROM documents`).all() as { id: string; name: string }[]).map((r) => ({
id: r.id,
name: readField(r.name),
}));
const chunks = (db.prepare(`SELECT id, text, heading FROM chunks`).all() as { id: string; text: string; heading: string | null }[]).map(
(r) => ({ id: r.id, text: readField(r.text), heading: r.heading === null ? null : readField(r.heading) })
);
return { collections, documents, chunks };
}
/** Writes the given (plaintext) content back under whichever mode is active
* right now — paired with getAllContentForMigration() so a caller can read
* under the old key/mode, change the key/mode, then write back under the
* new one. */
export function overwriteAllContent(data: RagContentForMigration): void {
const db = getDb();
const tx = db.transaction(() => {
for (const c of data.collections) {
db.prepare(`UPDATE collections SET name = ? WHERE id = ?`).run(writeField(c.name), c.id);
}
for (const d of data.documents) {
db.prepare(`UPDATE documents SET name = ? WHERE id = ?`).run(writeField(d.name), d.id);
}
for (const c of data.chunks) {
db.prepare(`UPDATE chunks SET text = ?, heading = ? WHERE id = ?`).run(
writeField(c.text),
c.heading === null ? null : writeField(c.heading),
c.id
);
}
});
tx();
}
export function upsertCollection(input: { id: string; name: string; folderPath: string; embeddingModel: string }): CollectionRow {
const now = Date.now();
const existing = getCollectionByPath(input.folderPath);
if (existing) {
getDb().prepare(`UPDATE collections SET name = ?, updated_at = ? WHERE id = ?`).run(writeField(input.name), now, existing.id);
return { ...existing, name: input.name, updated_at: now };
}
const row: CollectionRow = { id: input.id, name: input.name, folder_path: input.folderPath, embedding_model: input.embeddingModel, created_at: now, updated_at: now };
getDb()
.prepare(`INSERT INTO collections (id, name, folder_path, embedding_model, created_at, updated_at) VALUES (@id, @name, @folder_path, @embedding_model, @created_at, @updated_at)`)
.run({ ...row, name: writeField(row.name) });
return row;
}
export function getCollectionByPath(folderPath: string): CollectionRow | undefined {
const row = getDb().prepare(`SELECT * FROM collections WHERE folder_path = ?`).get(folderPath) as CollectionRow | undefined;
return row ? decryptCollection(row) : undefined;
}
export function getCollection(id: string): CollectionRow | undefined {
const row = getDb().prepare(`SELECT * FROM collections WHERE id = ?`).get(id) as CollectionRow | undefined;
return row ? decryptCollection(row) : undefined;
}
export function listCollections(): CollectionRow[] {
const rows = getDb().prepare(`SELECT * FROM collections ORDER BY updated_at DESC`).all() as CollectionRow[];
return rows.map(decryptCollection);
}
export function deleteCollection(id: string): void {
getDb().prepare(`DELETE FROM collections WHERE id = ?`).run(id);
}
export function touchCollection(id: string): void {
getDb().prepare(`UPDATE collections SET updated_at = ? WHERE id = ?`).run(Date.now(), id);
}
export function getDocument(collectionId: string, filePath: string): DocumentRow | undefined {
const row = getDb().prepare(`SELECT * FROM documents WHERE collection_id = ? AND path = ?`).get(collectionId, filePath) as DocumentRow | undefined;
return row ? decryptDocument(row) : undefined;
}
export function listDocuments(collectionId: string): DocumentRow[] {
const rows = getDb().prepare(`SELECT * FROM documents WHERE collection_id = ?`).all(collectionId) as DocumentRow[];
return rows.map(decryptDocument);
}
export function upsertDocument(input: {
id: string; collectionId: string; path: string; name: string; contentHash: string; size: number; mtimeMs: number; pageCount: number | null;
}): void {
const now = Date.now();
getDb().prepare(`
INSERT INTO documents (id, collection_id, path, name, content_hash, size, mtime_ms, page_count, indexed_at)
VALUES (@id, @collectionId, @path, @name, @contentHash, @size, @mtimeMs, @pageCount, @now)
ON CONFLICT(collection_id, path) DO UPDATE SET
content_hash = excluded.content_hash, size = excluded.size, mtime_ms = excluded.mtime_ms,
page_count = excluded.page_count, indexed_at = excluded.indexed_at
`).run({ ...input, name: writeField(input.name), now });
}
export function deleteDocument(id: string): void {
getDb().prepare(`DELETE FROM documents WHERE id = ?`).run(id);
}
export function replaceChunks(documentId: string, collectionId: string, chunks: ChunkInput[]): void {
const db = getDb();
const del = db.prepare(`DELETE FROM chunks WHERE document_id = ?`);
const insert = db.prepare(`
INSERT INTO chunks (id, document_id, collection_id, ordinal, text, token_count, heading, page, start_line, end_line, embedding)
VALUES (@id, @document_id, @collection_id, @ordinal, @text, @token_count, @heading, @page, @start_line, @end_line, @embedding)
`);
const tx = db.transaction((rows: ChunkInput[]) => {
del.run(documentId);
rows.forEach((chunk, ordinal) => {
insert.run({
id: `${documentId}:${ordinal}`, document_id: documentId, collection_id: collectionId, ordinal,
text: writeField(chunk.text), token_count: chunk.tokenCount, heading: chunk.heading === null ? null : writeField(chunk.heading),
page: chunk.page, start_line: chunk.startLine, end_line: chunk.endLine, embedding: encodeEmbedding(chunk.embedding),
});
});
});
tx(chunks);
}
export function chunksForCollection(collectionId: string): (ChunkRow & { doc_path: string; doc_name: string })[] {
const rows = getDb().prepare(`
SELECT chunks.*, documents.path AS doc_path, documents.name AS doc_name
FROM chunks JOIN documents ON documents.id = chunks.document_id
WHERE chunks.collection_id = ?
`).all(collectionId) as (ChunkRow & { doc_path: string; doc_name: string })[];
return rows.map((row) => ({ ...decryptChunk(row), doc_name: readField(row.doc_name) }));
}
export function countChunks(collectionId: string): number {
const row = getDb().prepare(`SELECT COUNT(*) AS n FROM chunks WHERE collection_id = ?`).get(collectionId) as { n: number };
return row.n;
}