-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrag-db.ts
More file actions
191 lines (168 loc) · 7.92 KB
/
Copy pathrag-db.ts
File metadata and controls
191 lines (168 loc) · 7.92 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
import * as fs from "node:fs";
import * as path from "node:path";
import { app } from "electron";
import Database from "better-sqlite3";
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);
}
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(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);
return row;
}
export function getCollectionByPath(folderPath: string): CollectionRow | undefined {
return getDb().prepare(`SELECT * FROM collections WHERE folder_path = ?`).get(folderPath) as CollectionRow | undefined;
}
export function getCollection(id: string): CollectionRow | undefined {
return getDb().prepare(`SELECT * FROM collections WHERE id = ?`).get(id) as CollectionRow | undefined;
}
export function listCollections(): CollectionRow[] {
return getDb().prepare(`SELECT * FROM collections ORDER BY updated_at DESC`).all() as CollectionRow[];
}
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 {
return getDb().prepare(`SELECT * FROM documents WHERE collection_id = ? AND path = ?`).get(collectionId, filePath) as DocumentRow | undefined;
}
export function listDocuments(collectionId: string): DocumentRow[] {
return getDb().prepare(`SELECT * FROM documents WHERE collection_id = ?`).all(collectionId) as DocumentRow[];
}
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, 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: chunk.text, token_count: chunk.tokenCount, heading: 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 })[] {
return 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 })[];
}
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;
}