pi-knowledge 0.4.2 → 0.4.3

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.
Files changed (55) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +4 -2
  3. package/dist/index.d.ts +38 -0
  4. package/dist/index.js +435 -0
  5. package/dist/src/diagnostics/health.d.ts +19 -0
  6. package/dist/src/diagnostics/health.js +110 -0
  7. package/dist/src/embedding/provider.d.ts +5 -0
  8. package/dist/src/embedding/provider.js +99 -0
  9. package/dist/src/embedding/vectors.d.ts +15 -0
  10. package/dist/src/embedding/vectors.js +111 -0
  11. package/dist/src/engine.d.ts +90 -0
  12. package/dist/src/engine.js +1370 -0
  13. package/dist/src/indexer/chunker.d.ts +53 -0
  14. package/dist/src/indexer/chunker.js +498 -0
  15. package/dist/src/indexer/chunkers/code-ast.d.ts +3 -0
  16. package/dist/src/indexer/chunkers/code-ast.js +179 -0
  17. package/dist/src/model-worker-client.d.ts +10 -0
  18. package/dist/src/model-worker-client.js +138 -0
  19. package/dist/src/model-worker.d.ts +1 -0
  20. package/dist/src/model-worker.js +61 -0
  21. package/dist/src/search/bm25.d.ts +8 -0
  22. package/dist/src/search/bm25.js +38 -0
  23. package/dist/src/search/fusion.d.ts +19 -0
  24. package/dist/src/search/fusion.js +34 -0
  25. package/dist/src/search/query.d.ts +5 -0
  26. package/dist/src/search/query.js +57 -0
  27. package/dist/src/search/ranking.d.ts +27 -0
  28. package/dist/src/search/ranking.js +180 -0
  29. package/dist/src/search/reranker.d.ts +10 -0
  30. package/dist/src/search/reranker.js +66 -0
  31. package/dist/src/search/vector.d.ts +14 -0
  32. package/dist/src/search/vector.js +58 -0
  33. package/dist/src/storage/sqlite.d.ts +99 -0
  34. package/dist/src/storage/sqlite.js +330 -0
  35. package/dist/src/watcher/file-watcher.d.ts +4 -0
  36. package/dist/src/watcher/file-watcher.js +108 -0
  37. package/extension.js +9 -0
  38. package/package.json +10 -5
  39. package/index.ts +0 -500
  40. package/src/diagnostics/health.ts +0 -145
  41. package/src/embedding/provider.ts +0 -106
  42. package/src/embedding/vectors.ts +0 -119
  43. package/src/engine.ts +0 -1592
  44. package/src/indexer/chunker.ts +0 -612
  45. package/src/indexer/chunkers/code-ast.ts +0 -209
  46. package/src/model-worker-client.ts +0 -146
  47. package/src/model-worker.ts +0 -88
  48. package/src/search/bm25.ts +0 -55
  49. package/src/search/fusion.ts +0 -48
  50. package/src/search/query.ts +0 -59
  51. package/src/search/ranking.ts +0 -201
  52. package/src/search/reranker.ts +0 -74
  53. package/src/search/vector.ts +0 -81
  54. package/src/storage/sqlite.ts +0 -489
  55. package/src/watcher/file-watcher.ts +0 -116
@@ -1,489 +0,0 @@
1
- import { randomUUID } from "node:crypto";
2
- import { existsSync, mkdirSync } from "node:fs";
3
- import { homedir } from "node:os";
4
- import { join } from "node:path";
5
- import Database from "better-sqlite3";
6
-
7
- export interface KnowledgeBase {
8
- id: string;
9
- name: string;
10
- description: string | null;
11
- source_path: string | null;
12
- source_type: "file" | "directory" | "text" | "url";
13
- source_options: string | null;
14
- created_at: number;
15
- updated_at: number;
16
- chunk_count: number;
17
- file_count: number;
18
- embedding_model: string;
19
- status: "ready" | "indexing" | "error" | "stale";
20
- }
21
-
22
- export interface IndexingJob {
23
- kb_id: string;
24
- operation: "add" | "update" | "import";
25
- status: "running" | "succeeded" | "failed" | "cancelled";
26
- phase: string;
27
- message: string;
28
- started_at: number;
29
- updated_at: number;
30
- last_progress_at: number;
31
- processed_files: number;
32
- processed_chunks: number;
33
- total_files: number | null;
34
- skipped_total: number;
35
- added_chunks: number;
36
- removed_chunks: number;
37
- unchanged_chunks: number;
38
- error_message: string | null;
39
- }
40
-
41
- export interface Chunk {
42
- id: string;
43
- kb_id: string;
44
- content_hash: string;
45
- content: string;
46
- content_tokenized: string;
47
- file_path: string;
48
- file_type: string;
49
- start_line: number;
50
- end_line: number;
51
- metadata_json: string;
52
- indexed_at: number;
53
- }
54
-
55
- export type ChunkInsert = Omit<Chunk, "id" | "kb_id" | "indexed_at">;
56
-
57
- const SCHEMA_VERSION = 3;
58
-
59
- const SCHEMA_SQL = `
60
- CREATE TABLE IF NOT EXISTS schema_version (
61
- version INTEGER NOT NULL
62
- );
63
-
64
- CREATE TABLE IF NOT EXISTS knowledge_bases (
65
- id TEXT PRIMARY KEY,
66
- name TEXT NOT NULL,
67
- description TEXT,
68
- source_path TEXT,
69
- source_type TEXT NOT NULL DEFAULT 'directory',
70
- source_options TEXT,
71
- created_at INTEGER NOT NULL,
72
- updated_at INTEGER NOT NULL,
73
- chunk_count INTEGER NOT NULL DEFAULT 0,
74
- file_count INTEGER NOT NULL DEFAULT 0,
75
- embedding_model TEXT NOT NULL DEFAULT 'multilingual-e5-small',
76
- status TEXT NOT NULL DEFAULT 'ready'
77
- );
78
-
79
- CREATE TABLE IF NOT EXISTS chunks (
80
- id TEXT PRIMARY KEY,
81
- kb_id TEXT NOT NULL,
82
- content_hash TEXT NOT NULL,
83
- content TEXT NOT NULL,
84
- content_tokenized TEXT NOT NULL,
85
- file_path TEXT NOT NULL,
86
- file_type TEXT NOT NULL DEFAULT 'text',
87
- start_line INTEGER NOT NULL DEFAULT 0,
88
- end_line INTEGER NOT NULL DEFAULT 0,
89
- metadata_json TEXT NOT NULL DEFAULT '{}',
90
- indexed_at INTEGER NOT NULL,
91
- FOREIGN KEY (kb_id) REFERENCES knowledge_bases(id) ON DELETE CASCADE
92
- );
93
-
94
- CREATE TABLE IF NOT EXISTS indexing_jobs (
95
- kb_id TEXT PRIMARY KEY,
96
- operation TEXT NOT NULL,
97
- status TEXT NOT NULL,
98
- phase TEXT NOT NULL,
99
- message TEXT NOT NULL,
100
- started_at INTEGER NOT NULL,
101
- updated_at INTEGER NOT NULL,
102
- last_progress_at INTEGER NOT NULL,
103
- processed_files INTEGER NOT NULL DEFAULT 0,
104
- processed_chunks INTEGER NOT NULL DEFAULT 0,
105
- total_files INTEGER,
106
- skipped_total INTEGER NOT NULL DEFAULT 0,
107
- added_chunks INTEGER NOT NULL DEFAULT 0,
108
- removed_chunks INTEGER NOT NULL DEFAULT 0,
109
- unchanged_chunks INTEGER NOT NULL DEFAULT 0,
110
- error_message TEXT,
111
- FOREIGN KEY (kb_id) REFERENCES knowledge_bases(id) ON DELETE CASCADE
112
- );
113
-
114
- CREATE INDEX IF NOT EXISTS idx_chunks_kb_id ON chunks(kb_id);
115
- CREATE INDEX IF NOT EXISTS idx_chunks_content_hash ON chunks(content_hash);
116
-
117
- CREATE VIRTUAL TABLE IF NOT EXISTS chunks_fts USING fts5(
118
- content_tokenized,
119
- content=chunks,
120
- content_rowid=rowid
121
- );
122
-
123
- -- Triggers to keep FTS in sync with chunks table
124
- CREATE TRIGGER IF NOT EXISTS chunks_ai AFTER INSERT ON chunks BEGIN
125
- INSERT INTO chunks_fts(rowid, content_tokenized) VALUES (new.rowid, new.content_tokenized);
126
- END;
127
-
128
- CREATE TRIGGER IF NOT EXISTS chunks_ad AFTER DELETE ON chunks BEGIN
129
- INSERT INTO chunks_fts(chunks_fts, rowid, content_tokenized) VALUES('delete', old.rowid, old.content_tokenized);
130
- END;
131
-
132
- CREATE TRIGGER IF NOT EXISTS chunks_au AFTER UPDATE ON chunks BEGIN
133
- INSERT INTO chunks_fts(chunks_fts, rowid, content_tokenized) VALUES('delete', old.rowid, old.content_tokenized);
134
- INSERT INTO chunks_fts(rowid, content_tokenized) VALUES (new.rowid, new.content_tokenized);
135
- END;
136
- `;
137
-
138
- export function getDefaultKnowledgeDir(): string {
139
- return join(homedir(), ".pi", "knowledge");
140
- }
141
-
142
- export function openDatabase(knowledgeDir?: string): Database.Database {
143
- const dir = knowledgeDir ?? getDefaultKnowledgeDir();
144
- if (!existsSync(dir)) {
145
- mkdirSync(dir, { recursive: true });
146
- }
147
- const dbPath = join(dir, "knowledge.db");
148
- const db = new Database(dbPath);
149
- db.pragma("journal_mode = WAL");
150
- db.pragma("busy_timeout = 5000");
151
- db.pragma("foreign_keys = ON");
152
-
153
- const hasVersion = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='schema_version'").get();
154
- if (!hasVersion) {
155
- db.exec(SCHEMA_SQL);
156
- db.prepare("INSERT INTO schema_version (version) VALUES (?)").run(SCHEMA_VERSION);
157
- } else {
158
- const row = db.prepare("SELECT version FROM schema_version").get() as { version: number } | undefined;
159
- const currentVersion = row?.version ?? 0;
160
- if (currentVersion < SCHEMA_VERSION) {
161
- runMigrations(db, currentVersion, SCHEMA_VERSION);
162
- db.prepare("UPDATE schema_version SET version = ?").run(SCHEMA_VERSION);
163
- }
164
- }
165
-
166
- return db;
167
- }
168
-
169
- function runMigrations(db: Database.Database, from: number, to: number): void {
170
- const migrations: Record<number, string> = {
171
- 2: `
172
- CREATE TABLE IF NOT EXISTS indexing_jobs (
173
- kb_id TEXT PRIMARY KEY,
174
- operation TEXT NOT NULL,
175
- status TEXT NOT NULL,
176
- phase TEXT NOT NULL,
177
- message TEXT NOT NULL,
178
- started_at INTEGER NOT NULL,
179
- updated_at INTEGER NOT NULL,
180
- last_progress_at INTEGER NOT NULL,
181
- processed_files INTEGER NOT NULL DEFAULT 0,
182
- processed_chunks INTEGER NOT NULL DEFAULT 0,
183
- total_files INTEGER,
184
- skipped_total INTEGER NOT NULL DEFAULT 0,
185
- added_chunks INTEGER NOT NULL DEFAULT 0,
186
- removed_chunks INTEGER NOT NULL DEFAULT 0,
187
- unchanged_chunks INTEGER NOT NULL DEFAULT 0,
188
- error_message TEXT,
189
- FOREIGN KEY (kb_id) REFERENCES knowledge_bases(id) ON DELETE CASCADE
190
- );
191
- `,
192
- };
193
- for (let v = from + 1; v <= to; v++) {
194
- if (v === 3) {
195
- const columns = db.prepare("PRAGMA table_info(knowledge_bases)").all() as Array<{ name: string }>;
196
- if (!columns.some((column) => column.name === "source_options")) {
197
- db.exec("ALTER TABLE knowledge_bases ADD COLUMN source_options TEXT");
198
- }
199
- continue;
200
- }
201
- if (migrations[v]) db.exec(migrations[v]);
202
- }
203
- }
204
-
205
- // --- CRUD: Knowledge Bases ---
206
-
207
- export function createKB(
208
- db: Database.Database,
209
- opts: {
210
- name: string;
211
- description?: string;
212
- source_path?: string;
213
- source_type: KnowledgeBase["source_type"];
214
- source_options?: string;
215
- },
216
- ): KnowledgeBase {
217
- const id = randomUUID();
218
- const now = Date.now();
219
- db.prepare(
220
- `INSERT INTO knowledge_bases (id, name, description, source_path, source_type, source_options, created_at, updated_at)
221
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
222
- ).run(
223
- id,
224
- opts.name,
225
- opts.description ?? null,
226
- opts.source_path ?? null,
227
- opts.source_type,
228
- opts.source_options ?? null,
229
- now,
230
- now,
231
- );
232
- const kb = getKB(db, id);
233
- if (!kb) throw new Error(`Failed to create knowledge base: ${id}`);
234
- return kb;
235
- }
236
-
237
- export function getKB(db: Database.Database, id: string): KnowledgeBase | undefined {
238
- return db.prepare("SELECT * FROM knowledge_bases WHERE id = ?").get(id) as KnowledgeBase | undefined;
239
- }
240
-
241
- export function getKBByName(db: Database.Database, name: string): KnowledgeBase | undefined {
242
- return db.prepare("SELECT * FROM knowledge_bases WHERE name = ?").get(name) as KnowledgeBase | undefined;
243
- }
244
-
245
- export function listKBs(db: Database.Database): KnowledgeBase[] {
246
- return db.prepare("SELECT * FROM knowledge_bases ORDER BY updated_at DESC").all() as KnowledgeBase[];
247
- }
248
-
249
- export function deleteKB(db: Database.Database, id: string): void {
250
- db.prepare("DELETE FROM indexing_jobs WHERE kb_id = ?").run(id);
251
- db.prepare("DELETE FROM chunks WHERE kb_id = ?").run(id);
252
- db.prepare("DELETE FROM knowledge_bases WHERE id = ?").run(id);
253
- }
254
-
255
- export function updateKBStatus(db: Database.Database, id: string, status: KnowledgeBase["status"]): void {
256
- db.prepare("UPDATE knowledge_bases SET status = ?, updated_at = ? WHERE id = ?").run(status, Date.now(), id);
257
- }
258
-
259
- export function updateKBCounts(db: Database.Database, id: string, chunkCount: number, fileCount: number): void {
260
- db.prepare("UPDATE knowledge_bases SET chunk_count = ?, file_count = ?, updated_at = ? WHERE id = ?").run(
261
- chunkCount,
262
- fileCount,
263
- Date.now(),
264
- id,
265
- );
266
- }
267
-
268
- // --- Indexing Jobs ---
269
-
270
- export function startIndexingJob(
271
- db: Database.Database,
272
- kbId: string,
273
- operation: IndexingJob["operation"],
274
- message: string,
275
- ): void {
276
- const now = Date.now();
277
- db.prepare(
278
- `INSERT INTO indexing_jobs (
279
- kb_id, operation, status, phase, message, started_at, updated_at, last_progress_at
280
- ) VALUES (?, ?, 'running', 'starting', ?, ?, ?, ?)
281
- ON CONFLICT(kb_id) DO UPDATE SET
282
- operation = excluded.operation,
283
- status = 'running',
284
- phase = 'starting',
285
- message = excluded.message,
286
- started_at = excluded.started_at,
287
- updated_at = excluded.updated_at,
288
- last_progress_at = excluded.last_progress_at,
289
- processed_files = 0,
290
- processed_chunks = 0,
291
- total_files = NULL,
292
- skipped_total = 0,
293
- added_chunks = 0,
294
- removed_chunks = 0,
295
- unchanged_chunks = 0,
296
- error_message = NULL`,
297
- ).run(kbId, operation, message, now, now, now);
298
- }
299
-
300
- export function updateIndexingJob(
301
- db: Database.Database,
302
- kbId: string,
303
- progress: {
304
- phase?: string;
305
- message?: string;
306
- processed_files?: number;
307
- processed_chunks?: number;
308
- total_files?: number | null;
309
- skipped_total?: number;
310
- added_chunks?: number;
311
- removed_chunks?: number;
312
- unchanged_chunks?: number;
313
- },
314
- ): void {
315
- const current = getIndexingJob(db, kbId);
316
- if (!current) return;
317
- const now = Date.now();
318
- db.prepare(
319
- `UPDATE indexing_jobs SET
320
- phase = ?,
321
- message = ?,
322
- updated_at = ?,
323
- last_progress_at = ?,
324
- processed_files = ?,
325
- processed_chunks = ?,
326
- total_files = ?,
327
- skipped_total = ?,
328
- added_chunks = ?,
329
- removed_chunks = ?,
330
- unchanged_chunks = ?
331
- WHERE kb_id = ?`,
332
- ).run(
333
- progress.phase ?? current.phase,
334
- progress.message ?? current.message,
335
- now,
336
- now,
337
- progress.processed_files ?? current.processed_files,
338
- progress.processed_chunks ?? current.processed_chunks,
339
- progress.total_files === undefined ? current.total_files : progress.total_files,
340
- progress.skipped_total ?? current.skipped_total,
341
- progress.added_chunks ?? current.added_chunks,
342
- progress.removed_chunks ?? current.removed_chunks,
343
- progress.unchanged_chunks ?? current.unchanged_chunks,
344
- kbId,
345
- );
346
- }
347
-
348
- export function finishIndexingJob(
349
- db: Database.Database,
350
- kbId: string,
351
- status: Extract<IndexingJob["status"], "succeeded" | "failed" | "cancelled">,
352
- message: string,
353
- errorMessage?: string,
354
- ): void {
355
- const now = Date.now();
356
- db.prepare(
357
- `UPDATE indexing_jobs SET
358
- status = ?,
359
- phase = ?,
360
- message = ?,
361
- updated_at = ?,
362
- last_progress_at = ?,
363
- error_message = ?
364
- WHERE kb_id = ?`,
365
- ).run(status, status, message, now, now, errorMessage ?? null, kbId);
366
- }
367
-
368
- export function getIndexingJob(db: Database.Database, kbId: string): IndexingJob | undefined {
369
- return db.prepare("SELECT * FROM indexing_jobs WHERE kb_id = ?").get(kbId) as IndexingJob | undefined;
370
- }
371
-
372
- // --- CRUD: Chunks ---
373
-
374
- export function insertChunks(db: Database.Database, kbId: string, chunks: ChunkInsert[]): void {
375
- const stmt = db.prepare(
376
- `INSERT INTO chunks (id, kb_id, content_hash, content, content_tokenized, file_path, file_type, start_line, end_line, metadata_json, indexed_at)
377
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
378
- );
379
- const now = Date.now();
380
- const insertMany = db.transaction((items: ChunkInsert[]) => {
381
- for (const c of items) {
382
- stmt.run(
383
- randomUUID(),
384
- kbId,
385
- c.content_hash,
386
- c.content,
387
- c.content_tokenized,
388
- c.file_path,
389
- c.file_type,
390
- c.start_line,
391
- c.end_line,
392
- c.metadata_json,
393
- now,
394
- );
395
- }
396
- });
397
- insertMany(chunks);
398
- }
399
-
400
- export function getChunksByKB(db: Database.Database, kbId: string): Chunk[] {
401
- return db.prepare("SELECT * FROM chunks WHERE kb_id = ? ORDER BY rowid").all(kbId) as Chunk[];
402
- }
403
-
404
- export function iterateChunksByKB(db: Database.Database, kbId: string): IterableIterator<Chunk> {
405
- return db.prepare("SELECT * FROM chunks WHERE kb_id = ? ORDER BY rowid").iterate(kbId) as IterableIterator<Chunk>;
406
- }
407
-
408
- export function getChunkIdsByKB(db: Database.Database, kbId: string): string[] {
409
- const rows = db.prepare("SELECT id FROM chunks WHERE kb_id = ? ORDER BY rowid").all(kbId) as { id: string }[];
410
- return rows.map((r) => r.id);
411
- }
412
-
413
- export function iterateChunkIdsByKB(db: Database.Database, kbId: string): IterableIterator<{ id: string }> {
414
- return db.prepare("SELECT id FROM chunks WHERE kb_id = ? ORDER BY rowid").iterate(kbId) as IterableIterator<{
415
- id: string;
416
- }>;
417
- }
418
-
419
- export function getChunkById(db: Database.Database, id: string): Chunk | undefined {
420
- return db.prepare("SELECT * FROM chunks WHERE id = ?").get(id) as Chunk | undefined;
421
- }
422
-
423
- export function getChunksByFile(
424
- db: Database.Database,
425
- kbId: string,
426
- filePath: string,
427
- startLine: number,
428
- endLine: number,
429
- ): Chunk[] {
430
- return db
431
- .prepare(
432
- `SELECT * FROM chunks
433
- WHERE kb_id = ? AND file_path = ? AND end_line >= ? AND start_line <= ?
434
- ORDER BY start_line, end_line`,
435
- )
436
- .all(kbId, filePath, startLine, endLine) as Chunk[];
437
- }
438
-
439
- export function getChunkByRowid(db: Database.Database, rowid: number): Chunk | undefined {
440
- return db.prepare("SELECT * FROM chunks WHERE rowid = ?").get(rowid) as Chunk | undefined;
441
- }
442
-
443
- export function deleteChunksByKB(db: Database.Database, kbId: string): void {
444
- db.prepare("DELETE FROM chunks WHERE kb_id = ?").run(kbId);
445
- }
446
-
447
- export function deleteChunksByIds(db: Database.Database, ids: string[]): void {
448
- if (ids.length === 0) return;
449
- const batchSize = 500;
450
- const deleteBatch = db.transaction((batch: string[]) => {
451
- const placeholders = batch.map(() => "?").join(",");
452
- db.prepare(`DELETE FROM chunks WHERE id IN (${placeholders})`).run(...batch);
453
- });
454
- for (let offset = 0; offset < ids.length; offset += batchSize) {
455
- deleteBatch(ids.slice(offset, offset + batchSize));
456
- }
457
- }
458
-
459
- export function getChunkHashesByKB(db: Database.Database, kbId: string): Map<string, string> {
460
- const rows = db.prepare("SELECT id, content_hash FROM chunks WHERE kb_id = ? ORDER BY rowid").all(kbId) as {
461
- id: string;
462
- content_hash: string;
463
- }[];
464
- return new Map(rows.map((r) => [r.content_hash, r.id]));
465
- }
466
-
467
- export function iterateChunkHashesByKB(
468
- db: Database.Database,
469
- kbId: string,
470
- ): IterableIterator<{ id: string; content_hash: string }> {
471
- return db
472
- .prepare("SELECT id, content_hash FROM chunks WHERE kb_id = ? ORDER BY rowid")
473
- .iterate(kbId) as IterableIterator<{
474
- id: string;
475
- content_hash: string;
476
- }>;
477
- }
478
-
479
- export function getChunkCount(db: Database.Database, kbId: string): number {
480
- const row = db.prepare("SELECT COUNT(*) as count FROM chunks WHERE kb_id = ?").get(kbId) as { count: number };
481
- return row.count;
482
- }
483
-
484
- export function getFileCount(db: Database.Database, kbId: string): number {
485
- const row = db.prepare("SELECT COUNT(DISTINCT file_path) as count FROM chunks WHERE kb_id = ?").get(kbId) as {
486
- count: number;
487
- };
488
- return row.count;
489
- }
@@ -1,116 +0,0 @@
1
- import { type Dirent, existsSync, type FSWatcher, readdirSync, statSync, watch } from "node:fs";
2
- import { join } from "node:path";
3
-
4
- const watchers = new Map<string, FSWatcher>();
5
- const pollers = new Map<string, ReturnType<typeof setInterval>>();
6
- const snapshots = new Map<string, Map<string, string>>();
7
- const debounceTimers = new Map<string, ReturnType<typeof setTimeout>>();
8
- const DEBOUNCE_MS = 2000;
9
- const POLL_MS = 2000;
10
-
11
- function scheduleUpdate(kbId: string, onUpdate: (kbId: string) => void): void {
12
- const existing = debounceTimers.get(kbId);
13
- if (existing) clearTimeout(existing);
14
- debounceTimers.set(
15
- kbId,
16
- setTimeout(() => {
17
- debounceTimers.delete(kbId);
18
- onUpdate(kbId);
19
- }, DEBOUNCE_MS),
20
- );
21
- }
22
-
23
- function scanSnapshot(dirPath: string): Map<string, string> {
24
- const snapshot = new Map<string, string>();
25
-
26
- function scan(dir: string): void {
27
- let entries: Dirent[];
28
- try {
29
- entries = readdirSync(dir, { withFileTypes: true });
30
- } catch {
31
- return;
32
- }
33
- for (const entry of entries) {
34
- const fullPath = join(dir, entry.name);
35
- try {
36
- const stat = statSync(fullPath);
37
- if (entry.isDirectory()) {
38
- scan(fullPath);
39
- } else if (entry.isFile()) {
40
- snapshot.set(fullPath, `${stat.mtimeMs}:${stat.size}`);
41
- }
42
- } catch {
43
- /* file disappeared or is unreadable */
44
- }
45
- }
46
- }
47
-
48
- if (existsSync(dirPath)) scan(dirPath);
49
- return snapshot;
50
- }
51
-
52
- function snapshotsDiffer(a: Map<string, string>, b: Map<string, string>): boolean {
53
- if (a.size !== b.size) return true;
54
- for (const [path, value] of a) {
55
- if (b.get(path) !== value) return true;
56
- }
57
- return false;
58
- }
59
-
60
- function startPoller(kbId: string, dirPath: string, onUpdate: (kbId: string) => void): void {
61
- snapshots.set(kbId, scanSnapshot(dirPath));
62
- pollers.set(
63
- kbId,
64
- setInterval(() => {
65
- const previous = snapshots.get(kbId) ?? new Map<string, string>();
66
- const next = scanSnapshot(dirPath);
67
- if (snapshotsDiffer(previous, next)) {
68
- snapshots.set(kbId, next);
69
- scheduleUpdate(kbId, onUpdate);
70
- }
71
- }, POLL_MS),
72
- );
73
- }
74
-
75
- export function startWatcher(kbId: string, dirPath: string, onUpdate: (kbId: string) => void): void {
76
- stopWatcher(kbId);
77
- startPoller(kbId, dirPath, onUpdate);
78
- try {
79
- const watcher = watch(dirPath, { recursive: true }, () => {
80
- snapshots.set(kbId, scanSnapshot(dirPath));
81
- scheduleUpdate(kbId, onUpdate);
82
- });
83
- watcher.on("error", () => {
84
- watchers.get(kbId)?.close();
85
- watchers.delete(kbId);
86
- });
87
- watchers.set(kbId, watcher);
88
- } catch {
89
- /* polling fallback remains active */
90
- }
91
- }
92
-
93
- export function stopWatcher(kbId: string): void {
94
- watchers.get(kbId)?.close();
95
- watchers.delete(kbId);
96
- const poller = pollers.get(kbId);
97
- if (poller) {
98
- clearInterval(poller);
99
- pollers.delete(kbId);
100
- }
101
- snapshots.delete(kbId);
102
- const t = debounceTimers.get(kbId);
103
- if (t) {
104
- clearTimeout(t);
105
- debounceTimers.delete(kbId);
106
- }
107
- }
108
-
109
- export function stopAllWatchers(): void {
110
- const ids = new Set([...watchers.keys(), ...pollers.keys()]);
111
- for (const id of ids) stopWatcher(id);
112
- }
113
-
114
- export function getActiveWatcherCount(): number {
115
- return new Set([...watchers.keys(), ...pollers.keys()]).size;
116
- }