antigravity-plugin-context-mode 1.0.2 → 1.2.1
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.
- package/CHANGELOG.md +46 -0
- package/README.md +54 -33
- package/hooks/pre-tool-use.js +393 -41
- package/hooks.json +1 -1
- package/lib/store.js +594 -54
- package/lib/tools.js +6 -2
- package/package.json +1 -1
- package/plugin.json +1 -1
- package/server.js +28 -3
package/lib/store.js
CHANGED
|
@@ -8,8 +8,57 @@ export const SNIPPET_MAX_BYTES = 800;
|
|
|
8
8
|
export const RRF_K = 60;
|
|
9
9
|
export const STATE_BUDGET = 2048;
|
|
10
10
|
|
|
11
|
+
export const FILE_CATEGORIES = {
|
|
12
|
+
images: {
|
|
13
|
+
id: "images",
|
|
14
|
+
name: "Images",
|
|
15
|
+
extensions: [".png", ".jpg", ".jpeg", ".gif", ".webp", ".svg", ".bmp", ".ico", ".tiff", ".psd"]
|
|
16
|
+
},
|
|
17
|
+
documents: {
|
|
18
|
+
id: "documents",
|
|
19
|
+
name: "Documents",
|
|
20
|
+
extensions: [".pdf", ".docx", ".doc", ".xlsx", ".xls", ".pptx", ".ppt", ".odt", ".rtf"]
|
|
21
|
+
},
|
|
22
|
+
code: {
|
|
23
|
+
id: "code",
|
|
24
|
+
name: "Source Code",
|
|
25
|
+
extensions: [".js", ".mjs", ".cjs", ".ts", ".tsx", ".jsx", ".py", ".rs", ".go", ".java", ".c", ".cpp", ".h", ".cs", ".html", ".css", ".json", ".yaml", ".yml", ".sql", ".sh", ".ps1"]
|
|
26
|
+
},
|
|
27
|
+
models3d: {
|
|
28
|
+
id: "models3d",
|
|
29
|
+
name: "3D Models",
|
|
30
|
+
extensions: [".blend", ".fbx", ".obj", ".gltf", ".glb", ".stl", ".dae", ".3ds", ".step", ".iges"]
|
|
31
|
+
},
|
|
32
|
+
music: {
|
|
33
|
+
id: "music",
|
|
34
|
+
name: "Music",
|
|
35
|
+
extensions: [".mp3", ".wav", ".ogg", ".flac", ".aac", ".m4a", ".wma", ".midi"]
|
|
36
|
+
},
|
|
37
|
+
video: {
|
|
38
|
+
id: "video",
|
|
39
|
+
name: "Video",
|
|
40
|
+
extensions: [".mp4", ".mov", ".avi", ".mkv", ".webm", ".flv", ".wmv", ".m4v"]
|
|
41
|
+
},
|
|
42
|
+
archives: {
|
|
43
|
+
id: "archives",
|
|
44
|
+
name: "Archives",
|
|
45
|
+
extensions: [".zip", ".tar", ".gz", ".7z", ".rar", ".wasm"]
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const DEFAULT_ACTIVE_CATEGORIES = ["images", "documents", "models3d", "music", "video", "archives"];
|
|
50
|
+
|
|
51
|
+
export const DEFAULT_EXEMPT_EXTENSIONS = Array.from(
|
|
52
|
+
new Set(
|
|
53
|
+
DEFAULT_ACTIVE_CATEGORIES.flatMap((catId) => FILE_CATEGORIES[catId]?.extensions || [])
|
|
54
|
+
)
|
|
55
|
+
);
|
|
56
|
+
|
|
11
57
|
/** Default path to context database (~/.context-mode/context.db) */
|
|
12
58
|
export function getDefaultDbPath() {
|
|
59
|
+
if (process.env.CONTEXT_MODE_DB_PATH && process.env.CONTEXT_MODE_DB_PATH.trim()) {
|
|
60
|
+
return process.env.CONTEXT_MODE_DB_PATH.trim();
|
|
61
|
+
}
|
|
13
62
|
const dir = path.join(os.homedir(), ".context-mode");
|
|
14
63
|
return path.join(dir, "context.db");
|
|
15
64
|
}
|
|
@@ -70,8 +119,13 @@ export class ContextModeStore {
|
|
|
70
119
|
/** Initialize tables, FTS5 virtual tables, and indexes */
|
|
71
120
|
initSchema() {
|
|
72
121
|
if (!this.db) return;
|
|
73
|
-
|
|
74
|
-
|
|
122
|
+
try {
|
|
123
|
+
this.db.exec("PRAGMA busy_timeout = 5000;");
|
|
124
|
+
this.db.exec("PRAGMA journal_mode = WAL;");
|
|
125
|
+
this.db.exec("PRAGMA synchronous = NORMAL;");
|
|
126
|
+
} catch {
|
|
127
|
+
// Fallback if PRAGMA configuration is restricted
|
|
128
|
+
}
|
|
75
129
|
|
|
76
130
|
// Standard tables
|
|
77
131
|
this.db.exec(`
|
|
@@ -220,6 +274,8 @@ export class ContextModeStore {
|
|
|
220
274
|
insertChunkTri.run(id, sid, ord, chunkText);
|
|
221
275
|
}
|
|
222
276
|
|
|
277
|
+
this.pruneStaleSources();
|
|
278
|
+
|
|
223
279
|
return {
|
|
224
280
|
sourceId: id,
|
|
225
281
|
bytesTotal: totalBytes,
|
|
@@ -416,33 +472,71 @@ export class ContextModeStore {
|
|
|
416
472
|
}
|
|
417
473
|
|
|
418
474
|
/** Retrieve compaction / state resume block */
|
|
419
|
-
getResumeState(sessionId, limit = 5) {
|
|
475
|
+
getResumeState(sessionId, limit = 5, options = {}) {
|
|
420
476
|
this.open();
|
|
421
477
|
const sid = sessionId || "default";
|
|
478
|
+
const sinceTs = options.sinceTs || null;
|
|
479
|
+
const sinceIso = sinceTs ? new Date(sinceTs).toISOString() : null;
|
|
422
480
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
`
|
|
426
|
-
SELECT id, label, kind, bytes_total, chunk_count, created_at
|
|
427
|
-
FROM sources
|
|
428
|
-
WHERE session_id = ?
|
|
429
|
-
ORDER BY created_ts DESC
|
|
430
|
-
LIMIT ?
|
|
431
|
-
`
|
|
432
|
-
)
|
|
433
|
-
.all(sid, limit);
|
|
481
|
+
let sources;
|
|
482
|
+
let events;
|
|
434
483
|
|
|
435
|
-
|
|
436
|
-
.
|
|
437
|
-
`
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
484
|
+
if (sid === "all" || sid === "lifetime") {
|
|
485
|
+
sources = this.db
|
|
486
|
+
.prepare(`
|
|
487
|
+
SELECT id, label, kind, bytes_total, chunk_count, created_at
|
|
488
|
+
FROM sources
|
|
489
|
+
ORDER BY created_ts DESC
|
|
490
|
+
LIMIT ?
|
|
491
|
+
`)
|
|
492
|
+
.all(limit);
|
|
493
|
+
events = this.db
|
|
494
|
+
.prepare(`
|
|
495
|
+
SELECT seq, tool, kind, key, path, ok, sandbox_bytes, returned_bytes, created_at
|
|
496
|
+
FROM events
|
|
497
|
+
ORDER BY seq DESC
|
|
498
|
+
LIMIT ?
|
|
499
|
+
`)
|
|
500
|
+
.all(limit);
|
|
501
|
+
} else if (sid === "app" && sinceIso) {
|
|
502
|
+
sources = this.db
|
|
503
|
+
.prepare(`
|
|
504
|
+
SELECT id, label, kind, bytes_total, chunk_count, created_at
|
|
505
|
+
FROM sources
|
|
506
|
+
WHERE created_at >= ? OR created_ts >= ?
|
|
507
|
+
ORDER BY created_ts DESC
|
|
508
|
+
LIMIT ?
|
|
509
|
+
`)
|
|
510
|
+
.all(sinceIso, sinceTs, limit);
|
|
511
|
+
events = this.db
|
|
512
|
+
.prepare(`
|
|
513
|
+
SELECT seq, tool, kind, key, path, ok, sandbox_bytes, returned_bytes, created_at
|
|
514
|
+
FROM events
|
|
515
|
+
WHERE created_at >= ?
|
|
516
|
+
ORDER BY seq DESC
|
|
517
|
+
LIMIT ?
|
|
518
|
+
`)
|
|
519
|
+
.all(sinceIso, limit);
|
|
520
|
+
} else {
|
|
521
|
+
sources = this.db
|
|
522
|
+
.prepare(`
|
|
523
|
+
SELECT id, label, kind, bytes_total, chunk_count, created_at
|
|
524
|
+
FROM sources
|
|
525
|
+
WHERE session_id = ?
|
|
526
|
+
ORDER BY created_ts DESC
|
|
527
|
+
LIMIT ?
|
|
528
|
+
`)
|
|
529
|
+
.all(sid, limit);
|
|
530
|
+
events = this.db
|
|
531
|
+
.prepare(`
|
|
532
|
+
SELECT seq, tool, kind, key, path, ok, sandbox_bytes, returned_bytes, created_at
|
|
533
|
+
FROM events
|
|
534
|
+
WHERE session_id = ?
|
|
535
|
+
ORDER BY seq DESC
|
|
536
|
+
LIMIT ?
|
|
537
|
+
`)
|
|
538
|
+
.all(sid, limit);
|
|
539
|
+
}
|
|
446
540
|
|
|
447
541
|
return {
|
|
448
542
|
sessionId: sid,
|
|
@@ -458,41 +552,83 @@ export class ContextModeStore {
|
|
|
458
552
|
return row || null;
|
|
459
553
|
}
|
|
460
554
|
|
|
555
|
+
/** Retrieve all chunks for a specific source */
|
|
556
|
+
getSourceChunks(sourceId) {
|
|
557
|
+
this.open();
|
|
558
|
+
if (!sourceId) return [];
|
|
559
|
+
return this.db
|
|
560
|
+
.prepare("SELECT ord, body, title FROM chunks WHERE source_id = ? ORDER BY ord ASC")
|
|
561
|
+
.all(sourceId);
|
|
562
|
+
}
|
|
563
|
+
|
|
461
564
|
/** Calculate token savings & statistics */
|
|
462
|
-
getStats(sessionId) {
|
|
565
|
+
getStats(sessionId, options = {}) {
|
|
463
566
|
this.open();
|
|
464
567
|
const sid = sessionId || "default";
|
|
568
|
+
const sinceTs = options.sinceTs || null;
|
|
569
|
+
const sinceIso = sinceTs ? new Date(sinceTs).toISOString() : null;
|
|
465
570
|
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
`
|
|
469
|
-
SELECT
|
|
470
|
-
COUNT(*) AS total_calls,
|
|
471
|
-
SUM(sandbox_bytes) AS total_sandbox_bytes,
|
|
472
|
-
SUM(returned_bytes) AS total_returned_bytes
|
|
473
|
-
FROM events
|
|
474
|
-
WHERE session_id = ?
|
|
475
|
-
`
|
|
476
|
-
)
|
|
477
|
-
.get(sid);
|
|
571
|
+
let eventStats;
|
|
572
|
+
let sourceStats;
|
|
478
573
|
|
|
479
|
-
|
|
480
|
-
.prepare(
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
574
|
+
if (sid === "all" || sid === "lifetime") {
|
|
575
|
+
eventStats = this.db.prepare(`
|
|
576
|
+
SELECT
|
|
577
|
+
COUNT(*) AS total_calls,
|
|
578
|
+
SUM(sandbox_bytes) AS total_sandbox_bytes,
|
|
579
|
+
SUM(returned_bytes) AS total_returned_bytes
|
|
580
|
+
FROM events
|
|
581
|
+
`).get();
|
|
582
|
+
|
|
583
|
+
sourceStats = this.db.prepare(`
|
|
584
|
+
SELECT
|
|
585
|
+
COUNT(*) AS total_sources,
|
|
586
|
+
SUM(bytes_total) AS total_source_bytes,
|
|
587
|
+
SUM(chunk_count) AS total_chunks
|
|
588
|
+
FROM sources
|
|
589
|
+
`).get();
|
|
590
|
+
} else if (sid === "app" && sinceIso) {
|
|
591
|
+
eventStats = this.db.prepare(`
|
|
592
|
+
SELECT
|
|
593
|
+
COUNT(*) AS total_calls,
|
|
594
|
+
SUM(sandbox_bytes) AS total_sandbox_bytes,
|
|
595
|
+
SUM(returned_bytes) AS total_returned_bytes
|
|
596
|
+
FROM events
|
|
597
|
+
WHERE created_at >= ?
|
|
598
|
+
`).get(sinceIso);
|
|
599
|
+
|
|
600
|
+
sourceStats = this.db.prepare(`
|
|
601
|
+
SELECT
|
|
602
|
+
COUNT(*) AS total_sources,
|
|
603
|
+
SUM(bytes_total) AS total_source_bytes,
|
|
604
|
+
SUM(chunk_count) AS total_chunks
|
|
605
|
+
FROM sources
|
|
606
|
+
WHERE created_at >= ? OR created_ts >= ?
|
|
607
|
+
`).get(sinceIso, sinceTs);
|
|
608
|
+
} else {
|
|
609
|
+
eventStats = this.db.prepare(`
|
|
610
|
+
SELECT
|
|
611
|
+
COUNT(*) AS total_calls,
|
|
612
|
+
SUM(sandbox_bytes) AS total_sandbox_bytes,
|
|
613
|
+
SUM(returned_bytes) AS total_returned_bytes
|
|
614
|
+
FROM events
|
|
615
|
+
WHERE session_id = ?
|
|
616
|
+
`).get(sid);
|
|
617
|
+
|
|
618
|
+
sourceStats = this.db.prepare(`
|
|
619
|
+
SELECT
|
|
620
|
+
COUNT(*) AS total_sources,
|
|
621
|
+
SUM(bytes_total) AS total_source_bytes,
|
|
622
|
+
SUM(chunk_count) AS total_chunks
|
|
623
|
+
FROM sources
|
|
624
|
+
WHERE session_id = ?
|
|
625
|
+
`).get(sid);
|
|
626
|
+
}
|
|
491
627
|
|
|
492
628
|
const totalRawBytes = (eventStats?.total_sandbox_bytes || 0) + (sourceStats?.total_source_bytes || 0);
|
|
493
629
|
const totalReturnedBytes = eventStats?.total_returned_bytes || 0;
|
|
494
630
|
const keptOutBytes = Math.max(0, totalRawBytes - totalReturnedBytes);
|
|
495
|
-
const savingsPercent = totalRawBytes > 0 ? ((keptOutBytes / totalRawBytes) * 100).toFixed(1) :
|
|
631
|
+
const savingsPercent = totalRawBytes > 0 ? Number(((keptOutBytes / totalRawBytes) * 100).toFixed(1)) : 0;
|
|
496
632
|
const tokensKeptOut = Math.round(keptOutBytes / 4);
|
|
497
633
|
|
|
498
634
|
return {
|
|
@@ -501,7 +637,7 @@ export class ContextModeStore {
|
|
|
501
637
|
totalRawBytes,
|
|
502
638
|
totalReturnedBytes,
|
|
503
639
|
keptOutBytes,
|
|
504
|
-
savingsPercent
|
|
640
|
+
savingsPercent,
|
|
505
641
|
tokensKeptOut,
|
|
506
642
|
sourcesCount: sourceStats?.total_sources || 0,
|
|
507
643
|
chunksCount: sourceStats?.total_chunks || 0
|
|
@@ -509,9 +645,9 @@ export class ContextModeStore {
|
|
|
509
645
|
}
|
|
510
646
|
|
|
511
647
|
/** Purge stored data */
|
|
512
|
-
purge({ scope = "session", sessionId, worktree }) {
|
|
648
|
+
purge({ scope = "session", sessionId, worktree, sinceTs }) {
|
|
513
649
|
this.open();
|
|
514
|
-
if (scope === "all") {
|
|
650
|
+
if (scope === "all" || scope === "lifetime") {
|
|
515
651
|
this.db.exec(`
|
|
516
652
|
DELETE FROM chunks;
|
|
517
653
|
DELETE FROM chunks_tri;
|
|
@@ -522,6 +658,18 @@ export class ContextModeStore {
|
|
|
522
658
|
return { ok: true, purged: "all" };
|
|
523
659
|
}
|
|
524
660
|
|
|
661
|
+
if (scope === "app" && sinceTs) {
|
|
662
|
+
const sinceIso = new Date(sinceTs).toISOString();
|
|
663
|
+
const sourceIds = this.db.prepare("SELECT id FROM sources WHERE created_at >= ? OR created_ts >= ?").all(sinceIso, sinceTs).map((r) => r.id);
|
|
664
|
+
for (const sId of sourceIds) {
|
|
665
|
+
this.db.prepare("DELETE FROM chunks WHERE source_id = ?").run(sId);
|
|
666
|
+
this.db.prepare("DELETE FROM chunks_tri WHERE source_id = ?").run(sId);
|
|
667
|
+
}
|
|
668
|
+
this.db.prepare("DELETE FROM sources WHERE created_at >= ? OR created_ts >= ?").run(sinceIso, sinceTs);
|
|
669
|
+
this.db.prepare("DELETE FROM events WHERE created_at >= ?").run(sinceIso);
|
|
670
|
+
return { ok: true, purged: "app", count: sourceIds.length };
|
|
671
|
+
}
|
|
672
|
+
|
|
525
673
|
if (scope === "worktree" && worktree) {
|
|
526
674
|
this.db.prepare("DELETE FROM chunks WHERE worktree = ?").run(worktree);
|
|
527
675
|
this.db.prepare("DELETE FROM sources WHERE worktree = ?").run(worktree);
|
|
@@ -562,4 +710,396 @@ export class ContextModeStore {
|
|
|
562
710
|
};
|
|
563
711
|
}
|
|
564
712
|
}
|
|
713
|
+
|
|
714
|
+
/** Read a key from meta table */
|
|
715
|
+
getMeta(key, defaultValue = null) {
|
|
716
|
+
this.open();
|
|
717
|
+
const row = this.db.prepare("SELECT value FROM meta WHERE key = ?").get(key);
|
|
718
|
+
return row ? row.value : defaultValue;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
/** Write a key-value pair to meta table */
|
|
722
|
+
setMeta(key, value) {
|
|
723
|
+
this.open();
|
|
724
|
+
this.db
|
|
725
|
+
.prepare(
|
|
726
|
+
"INSERT INTO meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value"
|
|
727
|
+
)
|
|
728
|
+
.run(key, String(value));
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/** Get active cutoff in bytes (0 = none/disabled) */
|
|
732
|
+
getCutoff(sessionId = null) {
|
|
733
|
+
this.open();
|
|
734
|
+
// 1. Session/Conversation override
|
|
735
|
+
if (sessionId && sessionId !== "all") {
|
|
736
|
+
const sessionCutoff = this.getMeta(`cutoff:session:${sessionId}`);
|
|
737
|
+
if (sessionCutoff !== null) {
|
|
738
|
+
return parseInt(sessionCutoff, 10);
|
|
739
|
+
}
|
|
740
|
+
// If checking a conversation, fall back to the app session cutoff if set
|
|
741
|
+
if (sessionId !== "app") {
|
|
742
|
+
const appCutoff = this.getMeta("cutoff:session:app");
|
|
743
|
+
if (appCutoff !== null) {
|
|
744
|
+
return parseInt(appCutoff, 10);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// 2. Environment variable if explicitly set
|
|
750
|
+
const envVal = process.env.CONTEXT_MODE_MAX_READ_BYTES;
|
|
751
|
+
if (envVal) {
|
|
752
|
+
const parsed = parseInt(envVal, 10);
|
|
753
|
+
if (!Number.isNaN(parsed)) return parsed;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// 3. Saved global setting in SQLite
|
|
757
|
+
const globalCutoff = this.getMeta("cutoff:global");
|
|
758
|
+
if (globalCutoff !== null) {
|
|
759
|
+
return parseInt(globalCutoff, 10);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
// 4. Default 8 KB
|
|
763
|
+
return 8192;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
/** Set cutoff in bytes (0 = none/disabled) */
|
|
767
|
+
setCutoff(bytes, sessionId = null) {
|
|
768
|
+
this.open();
|
|
769
|
+
const val = parseInt(bytes, 10);
|
|
770
|
+
const numVal = Number.isNaN(val) ? 8192 : Math.max(0, val);
|
|
771
|
+
if (sessionId && sessionId !== "all") {
|
|
772
|
+
this.setMeta(`cutoff:session:${sessionId}`, numVal);
|
|
773
|
+
} else {
|
|
774
|
+
this.setMeta("cutoff:global", numVal);
|
|
775
|
+
}
|
|
776
|
+
return numVal;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/** Internal helper to recompute and save the combined exempt_ext list */
|
|
780
|
+
_recomputeExemptExtensions(sessionId = null) {
|
|
781
|
+
const activeCats = this.getExemptCategories(sessionId);
|
|
782
|
+
const customs = this.getCustomExemptExtensions(sessionId);
|
|
783
|
+
const combined = new Set(customs);
|
|
784
|
+
for (const catId of activeCats) {
|
|
785
|
+
const cat = FILE_CATEGORIES[catId];
|
|
786
|
+
if (cat && Array.isArray(cat.extensions)) {
|
|
787
|
+
for (const ext of cat.extensions) {
|
|
788
|
+
combined.add(ext);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
const cleanList = Array.from(combined);
|
|
793
|
+
const serialized = JSON.stringify(cleanList);
|
|
794
|
+
if (sessionId && sessionId !== "all") {
|
|
795
|
+
this.setMeta(`exempt_ext:session:${sessionId}`, serialized);
|
|
796
|
+
} else {
|
|
797
|
+
this.setMeta("exempt_ext:global", serialized);
|
|
798
|
+
}
|
|
799
|
+
return cleanList;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
/** Get list of active category IDs */
|
|
803
|
+
getExemptCategories(sessionId = null) {
|
|
804
|
+
this.open();
|
|
805
|
+
if (sessionId && sessionId !== "all") {
|
|
806
|
+
const raw = this.getMeta(`exempt_categories:session:${sessionId}`);
|
|
807
|
+
if (raw) {
|
|
808
|
+
try {
|
|
809
|
+
const parsed = JSON.parse(raw);
|
|
810
|
+
if (Array.isArray(parsed)) return parsed;
|
|
811
|
+
} catch {}
|
|
812
|
+
}
|
|
813
|
+
if (sessionId !== "app") {
|
|
814
|
+
const appRaw = this.getMeta("exempt_categories:session:app");
|
|
815
|
+
if (appRaw) {
|
|
816
|
+
try {
|
|
817
|
+
const parsed = JSON.parse(appRaw);
|
|
818
|
+
if (Array.isArray(parsed)) return parsed;
|
|
819
|
+
} catch {}
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
const globalRaw = this.getMeta("exempt_categories:global");
|
|
824
|
+
if (globalRaw) {
|
|
825
|
+
try {
|
|
826
|
+
const parsed = JSON.parse(globalRaw);
|
|
827
|
+
if (Array.isArray(parsed)) return parsed;
|
|
828
|
+
} catch {}
|
|
829
|
+
}
|
|
830
|
+
return [...DEFAULT_ACTIVE_CATEGORIES];
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/** Set list of active category IDs */
|
|
834
|
+
setExemptCategories(categories, sessionId = null) {
|
|
835
|
+
this.open();
|
|
836
|
+
const cleanList = Array.isArray(categories)
|
|
837
|
+
? Array.from(new Set(categories.map(c => String(c).trim().toLowerCase()).filter(c => FILE_CATEGORIES[c])))
|
|
838
|
+
: [...DEFAULT_ACTIVE_CATEGORIES];
|
|
839
|
+
const serialized = JSON.stringify(cleanList);
|
|
840
|
+
if (sessionId && sessionId !== "all") {
|
|
841
|
+
this.setMeta(`exempt_categories:session:${sessionId}`, serialized);
|
|
842
|
+
} else {
|
|
843
|
+
this.setMeta("exempt_categories:global", serialized);
|
|
844
|
+
}
|
|
845
|
+
this._recomputeExemptExtensions(sessionId);
|
|
846
|
+
return cleanList;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/** Toggle an exempt category on or off */
|
|
850
|
+
toggleExemptCategory(categoryId, enabled = null, sessionId = null) {
|
|
851
|
+
const active = this.getExemptCategories(sessionId);
|
|
852
|
+
const cat = String(categoryId || "").trim().toLowerCase();
|
|
853
|
+
if (!FILE_CATEGORIES[cat]) return active;
|
|
854
|
+
|
|
855
|
+
const isCurrentlyActive = active.includes(cat);
|
|
856
|
+
const shouldEnable = enabled !== null ? Boolean(enabled) : !isCurrentlyActive;
|
|
857
|
+
|
|
858
|
+
let updated;
|
|
859
|
+
if (shouldEnable && !isCurrentlyActive) {
|
|
860
|
+
updated = [...active, cat];
|
|
861
|
+
} else if (!shouldEnable && isCurrentlyActive) {
|
|
862
|
+
updated = active.filter(c => c !== cat);
|
|
863
|
+
} else {
|
|
864
|
+
return active;
|
|
865
|
+
}
|
|
866
|
+
return this.setExemptCategories(updated, sessionId);
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
/** Get custom file extensions (explicitly added by user) */
|
|
870
|
+
getCustomExemptExtensions(sessionId = null) {
|
|
871
|
+
this.open();
|
|
872
|
+
if (sessionId && sessionId !== "all") {
|
|
873
|
+
const raw = this.getMeta(`custom_exempt_ext:session:${sessionId}`);
|
|
874
|
+
if (raw) {
|
|
875
|
+
try {
|
|
876
|
+
const parsed = JSON.parse(raw);
|
|
877
|
+
if (Array.isArray(parsed)) return parsed;
|
|
878
|
+
} catch {}
|
|
879
|
+
}
|
|
880
|
+
if (sessionId !== "app") {
|
|
881
|
+
const appRaw = this.getMeta("custom_exempt_ext:session:app");
|
|
882
|
+
if (appRaw) {
|
|
883
|
+
try {
|
|
884
|
+
const parsed = JSON.parse(appRaw);
|
|
885
|
+
if (Array.isArray(parsed)) return parsed;
|
|
886
|
+
} catch {}
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
const globalRaw = this.getMeta("custom_exempt_ext:global");
|
|
891
|
+
if (globalRaw) {
|
|
892
|
+
try {
|
|
893
|
+
const parsed = JSON.parse(globalRaw);
|
|
894
|
+
if (Array.isArray(parsed)) return parsed;
|
|
895
|
+
} catch {}
|
|
896
|
+
}
|
|
897
|
+
return [];
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
/** Set custom file extensions */
|
|
901
|
+
setCustomExemptExtensions(extensions, sessionId = null) {
|
|
902
|
+
this.open();
|
|
903
|
+
const cleanList = Array.from(
|
|
904
|
+
new Set(
|
|
905
|
+
(Array.isArray(extensions) ? extensions : [])
|
|
906
|
+
.map((ext) => String(ext || "").trim().toLowerCase())
|
|
907
|
+
.filter(Boolean)
|
|
908
|
+
.map((ext) => (ext.startsWith(".") ? ext : `.${ext}`))
|
|
909
|
+
)
|
|
910
|
+
);
|
|
911
|
+
const serialized = JSON.stringify(cleanList);
|
|
912
|
+
if (sessionId && sessionId !== "all") {
|
|
913
|
+
this.setMeta(`custom_exempt_ext:session:${sessionId}`, serialized);
|
|
914
|
+
} else {
|
|
915
|
+
this.setMeta("custom_exempt_ext:global", serialized);
|
|
916
|
+
}
|
|
917
|
+
this._recomputeExemptExtensions(sessionId);
|
|
918
|
+
return cleanList;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
/** Add custom file extension */
|
|
922
|
+
addCustomExemptExtension(ext, sessionId = null) {
|
|
923
|
+
const list = this.getCustomExemptExtensions(sessionId);
|
|
924
|
+
const cleanExt = String(ext || "").trim().toLowerCase();
|
|
925
|
+
if (!cleanExt) return list;
|
|
926
|
+
const formatted = cleanExt.startsWith(".") ? cleanExt : `.${cleanExt}`;
|
|
927
|
+
if (!list.includes(formatted)) {
|
|
928
|
+
list.push(formatted);
|
|
929
|
+
this.setCustomExemptExtensions(list, sessionId);
|
|
930
|
+
}
|
|
931
|
+
return list;
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
/** Remove custom file extension */
|
|
935
|
+
removeCustomExemptExtension(ext, sessionId = null) {
|
|
936
|
+
const list = this.getCustomExemptExtensions(sessionId);
|
|
937
|
+
const cleanExt = String(ext || "").trim().toLowerCase();
|
|
938
|
+
const formatted = cleanExt.startsWith(".") ? cleanExt : `.${cleanExt}`;
|
|
939
|
+
const filtered = list.filter((e) => e !== formatted);
|
|
940
|
+
this.setCustomExemptExtensions(filtered, sessionId);
|
|
941
|
+
return filtered;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
/** Get list of file extensions exempt from the size cutoff */
|
|
945
|
+
getExemptExtensions(sessionId = null) {
|
|
946
|
+
this.open();
|
|
947
|
+
// 1. Session/Conversation override
|
|
948
|
+
if (sessionId && sessionId !== "all") {
|
|
949
|
+
const raw = this.getMeta(`exempt_ext:session:${sessionId}`);
|
|
950
|
+
if (raw) {
|
|
951
|
+
try {
|
|
952
|
+
const parsed = JSON.parse(raw);
|
|
953
|
+
if (Array.isArray(parsed)) return parsed;
|
|
954
|
+
} catch {}
|
|
955
|
+
}
|
|
956
|
+
// If checking a conversation, fall back to the app session exemptions if set
|
|
957
|
+
if (sessionId !== "app") {
|
|
958
|
+
const appRaw = this.getMeta("exempt_ext:session:app");
|
|
959
|
+
if (appRaw) {
|
|
960
|
+
try {
|
|
961
|
+
const parsed = JSON.parse(appRaw);
|
|
962
|
+
if (Array.isArray(parsed)) return parsed;
|
|
963
|
+
} catch {}
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
const globalRaw = this.getMeta("exempt_ext:global");
|
|
968
|
+
if (globalRaw) {
|
|
969
|
+
try {
|
|
970
|
+
const parsed = JSON.parse(globalRaw);
|
|
971
|
+
if (Array.isArray(parsed)) return parsed;
|
|
972
|
+
} catch {}
|
|
973
|
+
}
|
|
974
|
+
return [...DEFAULT_EXEMPT_EXTENSIONS];
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
/** Set list of file extensions exempt from the size cutoff */
|
|
978
|
+
setExemptExtensions(extensions, sessionId = null) {
|
|
979
|
+
this.open();
|
|
980
|
+
if (extensions === null) {
|
|
981
|
+
this.setExemptCategories(null, sessionId);
|
|
982
|
+
this.setCustomExemptExtensions([], sessionId);
|
|
983
|
+
return this.getExemptExtensions(sessionId);
|
|
984
|
+
}
|
|
985
|
+
const cleanList = Array.from(
|
|
986
|
+
new Set(
|
|
987
|
+
(Array.isArray(extensions) ? extensions : [])
|
|
988
|
+
.map((ext) => String(ext || "").trim().toLowerCase())
|
|
989
|
+
.filter(Boolean)
|
|
990
|
+
.map((ext) => (ext.startsWith(".") ? ext : `.${ext}`))
|
|
991
|
+
)
|
|
992
|
+
);
|
|
993
|
+
const activeCats = [];
|
|
994
|
+
for (const [catId, catObj] of Object.entries(FILE_CATEGORIES)) {
|
|
995
|
+
if (catObj.extensions.some((e) => cleanList.includes(e))) {
|
|
996
|
+
activeCats.push(catId);
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
const allCatExts = new Set(activeCats.flatMap((c) => FILE_CATEGORIES[c]?.extensions || []));
|
|
1000
|
+
const customs = cleanList.filter((e) => !allCatExts.has(e));
|
|
1001
|
+
|
|
1002
|
+
this.setExemptCategories(activeCats, sessionId);
|
|
1003
|
+
this.setCustomExemptExtensions(customs, sessionId);
|
|
1004
|
+
return this.getExemptExtensions(sessionId);
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
/** Add an exempt file extension */
|
|
1008
|
+
addExemptExtension(ext, sessionId = null) {
|
|
1009
|
+
this.addCustomExemptExtension(ext, sessionId);
|
|
1010
|
+
return this.getExemptExtensions(sessionId);
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
/** Remove an exempt file extension */
|
|
1014
|
+
removeExemptExtension(ext, sessionId = null) {
|
|
1015
|
+
this.removeCustomExemptExtension(ext, sessionId);
|
|
1016
|
+
return this.getExemptExtensions(sessionId);
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/** Check if protection is paused (true = paused, false = active) */
|
|
1020
|
+
isPaused(sessionId = null) {
|
|
1021
|
+
this.open();
|
|
1022
|
+
// 1. Session/Conversation override
|
|
1023
|
+
if (sessionId && sessionId !== "all") {
|
|
1024
|
+
const sessionPaused = this.getMeta(`paused:session:${sessionId}`);
|
|
1025
|
+
if (sessionPaused !== null) {
|
|
1026
|
+
return sessionPaused === "true" || sessionPaused === "1";
|
|
1027
|
+
}
|
|
1028
|
+
// If checking a conversation, fall back to the app session paused state if set
|
|
1029
|
+
if (sessionId !== "app") {
|
|
1030
|
+
const appPaused = this.getMeta("paused:session:app");
|
|
1031
|
+
if (appPaused !== null) {
|
|
1032
|
+
return appPaused === "true" || appPaused === "1";
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
// 2. Global paused setting
|
|
1037
|
+
const globalPaused = this.getMeta("paused:global");
|
|
1038
|
+
if (globalPaused !== null) {
|
|
1039
|
+
return globalPaused === "true" || globalPaused === "1";
|
|
1040
|
+
}
|
|
1041
|
+
return false;
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
/** Set protection paused state (true = paused, false = active) */
|
|
1045
|
+
setPaused(paused, sessionId = null) {
|
|
1046
|
+
this.open();
|
|
1047
|
+
const boolVal = Boolean(paused && paused !== "false" && paused !== "0");
|
|
1048
|
+
const strVal = boolVal ? "true" : "false";
|
|
1049
|
+
if (sessionId && sessionId !== "all") {
|
|
1050
|
+
this.setMeta(`paused:session:${sessionId}`, strVal);
|
|
1051
|
+
} else {
|
|
1052
|
+
this.setMeta("paused:global", strVal);
|
|
1053
|
+
}
|
|
1054
|
+
return boolVal;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
/**
|
|
1058
|
+
* Automatic LRU pruning: limits the number of indexed sources and chunks
|
|
1059
|
+
* to avoid unbounded database expansion.
|
|
1060
|
+
*/
|
|
1061
|
+
pruneStaleSources({ maxSourcesPerSession = 50, maxTotalSources = 200 } = {}) {
|
|
1062
|
+
this.open();
|
|
1063
|
+
try {
|
|
1064
|
+
// 1. Prune sources exceeding per-session cap
|
|
1065
|
+
const excessSessionSources = this.db
|
|
1066
|
+
.prepare(`
|
|
1067
|
+
SELECT id FROM sources
|
|
1068
|
+
WHERE id NOT IN (
|
|
1069
|
+
SELECT id FROM sources s2
|
|
1070
|
+
WHERE s2.session_id = sources.session_id
|
|
1071
|
+
ORDER BY s2.created_ts DESC
|
|
1072
|
+
LIMIT ?
|
|
1073
|
+
)
|
|
1074
|
+
`)
|
|
1075
|
+
.all(maxSourcesPerSession);
|
|
1076
|
+
|
|
1077
|
+
if (excessSessionSources.length > 0) {
|
|
1078
|
+
const ids = excessSessionSources.map((r) => r.id);
|
|
1079
|
+
const placeholders = ids.map(() => "?").join(",");
|
|
1080
|
+
this.db.prepare(`DELETE FROM chunks WHERE source_id IN (${placeholders})`).run(...ids);
|
|
1081
|
+
this.db.prepare(`DELETE FROM chunks_tri WHERE source_id IN (${placeholders})`).run(...ids);
|
|
1082
|
+
this.db.prepare(`DELETE FROM sources WHERE id IN (${placeholders})`).run(...ids);
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
// 2. Prune global total sources if over cap
|
|
1086
|
+
const totalCountRow = this.db.prepare("SELECT count(*) as count FROM sources").get();
|
|
1087
|
+
if (totalCountRow && totalCountRow.count > maxTotalSources) {
|
|
1088
|
+
const deleteCount = totalCountRow.count - maxTotalSources;
|
|
1089
|
+
const oldest = this.db
|
|
1090
|
+
.prepare("SELECT id FROM sources ORDER BY created_ts ASC LIMIT ?")
|
|
1091
|
+
.all(deleteCount);
|
|
1092
|
+
|
|
1093
|
+
if (oldest.length > 0) {
|
|
1094
|
+
const ids = oldest.map((r) => r.id);
|
|
1095
|
+
const placeholders = ids.map(() => "?").join(",");
|
|
1096
|
+
this.db.prepare(`DELETE FROM chunks WHERE source_id IN (${placeholders})`).run(...ids);
|
|
1097
|
+
this.db.prepare(`DELETE FROM chunks_tri WHERE source_id IN (${placeholders})`).run(...ids);
|
|
1098
|
+
this.db.prepare(`DELETE FROM sources WHERE id IN (${placeholders})`).run(...ids);
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
} catch {
|
|
1102
|
+
// Best-effort auto-prune
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
565
1105
|
}
|