antigravity-plugin-context-mode 1.1.0 → 1.3.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.
- package/CHANGELOG.md +41 -0
- package/README.md +53 -42
- package/hooks/pre-tool-use.js +335 -38
- package/hooks/session-start.js +7 -0
- package/hooks.json +1 -1
- package/lib/store.js +354 -67
- package/lib/tools.js +19 -1
- package/package.json +1 -1
- package/plugin.json +1 -1
- package/server.js +13 -1
package/lib/store.js
CHANGED
|
@@ -8,41 +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
|
|
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
|
-
|
|
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
|
+
);
|
|
43
56
|
|
|
44
57
|
/** Default path to context database (~/.context-mode/context.db) */
|
|
45
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
|
+
}
|
|
46
62
|
const dir = path.join(os.homedir(), ".context-mode");
|
|
47
63
|
return path.join(dir, "context.db");
|
|
48
64
|
}
|
|
@@ -103,8 +119,13 @@ export class ContextModeStore {
|
|
|
103
119
|
/** Initialize tables, FTS5 virtual tables, and indexes */
|
|
104
120
|
initSchema() {
|
|
105
121
|
if (!this.db) return;
|
|
106
|
-
|
|
107
|
-
|
|
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
|
+
}
|
|
108
129
|
|
|
109
130
|
// Standard tables
|
|
110
131
|
this.db.exec(`
|
|
@@ -471,7 +492,7 @@ export class ContextModeStore {
|
|
|
471
492
|
.all(limit);
|
|
472
493
|
events = this.db
|
|
473
494
|
.prepare(`
|
|
474
|
-
SELECT seq, tool, kind, ok, sandbox_bytes, returned_bytes, created_at
|
|
495
|
+
SELECT seq, tool, kind, key, path, ok, sandbox_bytes, returned_bytes, agent, created_at
|
|
475
496
|
FROM events
|
|
476
497
|
ORDER BY seq DESC
|
|
477
498
|
LIMIT ?
|
|
@@ -489,7 +510,7 @@ export class ContextModeStore {
|
|
|
489
510
|
.all(sinceIso, sinceTs, limit);
|
|
490
511
|
events = this.db
|
|
491
512
|
.prepare(`
|
|
492
|
-
SELECT seq, tool, kind, ok, sandbox_bytes, returned_bytes, created_at
|
|
513
|
+
SELECT seq, tool, kind, key, path, ok, sandbox_bytes, returned_bytes, agent, created_at
|
|
493
514
|
FROM events
|
|
494
515
|
WHERE created_at >= ?
|
|
495
516
|
ORDER BY seq DESC
|
|
@@ -508,7 +529,7 @@ export class ContextModeStore {
|
|
|
508
529
|
.all(sid, limit);
|
|
509
530
|
events = this.db
|
|
510
531
|
.prepare(`
|
|
511
|
-
SELECT seq, tool, kind, ok, sandbox_bytes, returned_bytes, created_at
|
|
532
|
+
SELECT seq, tool, kind, key, path, ok, sandbox_bytes, returned_bytes, agent, created_at
|
|
512
533
|
FROM events
|
|
513
534
|
WHERE session_id = ?
|
|
514
535
|
ORDER BY seq DESC
|
|
@@ -549,13 +570,15 @@ export class ContextModeStore {
|
|
|
549
570
|
|
|
550
571
|
let eventStats;
|
|
551
572
|
let sourceStats;
|
|
573
|
+
let unrecordedSources;
|
|
552
574
|
|
|
553
575
|
if (sid === "all" || sid === "lifetime") {
|
|
554
576
|
eventStats = this.db.prepare(`
|
|
555
577
|
SELECT
|
|
556
578
|
COUNT(*) AS total_calls,
|
|
557
|
-
SUM(sandbox_bytes) AS
|
|
558
|
-
SUM(returned_bytes) AS total_returned_bytes
|
|
579
|
+
SUM(MAX(sandbox_bytes, bytes, returned_bytes)) AS total_raw_bytes,
|
|
580
|
+
SUM(returned_bytes) AS total_returned_bytes,
|
|
581
|
+
SUM(MAX(0, MAX(sandbox_bytes, bytes, returned_bytes) - returned_bytes)) AS total_kept_out_bytes
|
|
559
582
|
FROM events
|
|
560
583
|
`).get();
|
|
561
584
|
|
|
@@ -566,12 +589,24 @@ export class ContextModeStore {
|
|
|
566
589
|
SUM(chunk_count) AS total_chunks
|
|
567
590
|
FROM sources
|
|
568
591
|
`).get();
|
|
592
|
+
|
|
593
|
+
unrecordedSources = this.db.prepare(`
|
|
594
|
+
SELECT COALESCE(SUM(bytes_total), 0) AS unrecorded_source_bytes
|
|
595
|
+
FROM sources s
|
|
596
|
+
WHERE s.kind NOT IN ('execution', 'batch', 'tool_output', 'exec')
|
|
597
|
+
AND NOT EXISTS (
|
|
598
|
+
SELECT 1 FROM events e
|
|
599
|
+
WHERE e.tool = 'ctx_index'
|
|
600
|
+
AND e.sandbox_bytes = s.bytes_total
|
|
601
|
+
)
|
|
602
|
+
`).get();
|
|
569
603
|
} else if (sid === "app" && sinceIso) {
|
|
570
604
|
eventStats = this.db.prepare(`
|
|
571
605
|
SELECT
|
|
572
606
|
COUNT(*) AS total_calls,
|
|
573
|
-
SUM(sandbox_bytes) AS
|
|
574
|
-
SUM(returned_bytes) AS total_returned_bytes
|
|
607
|
+
SUM(MAX(sandbox_bytes, bytes, returned_bytes)) AS total_raw_bytes,
|
|
608
|
+
SUM(returned_bytes) AS total_returned_bytes,
|
|
609
|
+
SUM(MAX(0, MAX(sandbox_bytes, bytes, returned_bytes) - returned_bytes)) AS total_kept_out_bytes
|
|
575
610
|
FROM events
|
|
576
611
|
WHERE created_at >= ?
|
|
577
612
|
`).get(sinceIso);
|
|
@@ -584,12 +619,25 @@ export class ContextModeStore {
|
|
|
584
619
|
FROM sources
|
|
585
620
|
WHERE created_at >= ? OR created_ts >= ?
|
|
586
621
|
`).get(sinceIso, sinceTs);
|
|
622
|
+
|
|
623
|
+
unrecordedSources = this.db.prepare(`
|
|
624
|
+
SELECT COALESCE(SUM(bytes_total), 0) AS unrecorded_source_bytes
|
|
625
|
+
FROM sources s
|
|
626
|
+
WHERE (created_at >= ? OR created_ts >= ?)
|
|
627
|
+
AND s.kind NOT IN ('execution', 'batch', 'tool_output', 'exec')
|
|
628
|
+
AND NOT EXISTS (
|
|
629
|
+
SELECT 1 FROM events e
|
|
630
|
+
WHERE e.tool = 'ctx_index'
|
|
631
|
+
AND e.sandbox_bytes = s.bytes_total
|
|
632
|
+
)
|
|
633
|
+
`).get(sinceIso, sinceTs);
|
|
587
634
|
} else {
|
|
588
635
|
eventStats = this.db.prepare(`
|
|
589
636
|
SELECT
|
|
590
637
|
COUNT(*) AS total_calls,
|
|
591
|
-
SUM(sandbox_bytes) AS
|
|
592
|
-
SUM(returned_bytes) AS total_returned_bytes
|
|
638
|
+
SUM(MAX(sandbox_bytes, bytes, returned_bytes)) AS total_raw_bytes,
|
|
639
|
+
SUM(returned_bytes) AS total_returned_bytes,
|
|
640
|
+
SUM(MAX(0, MAX(sandbox_bytes, bytes, returned_bytes) - returned_bytes)) AS total_kept_out_bytes
|
|
593
641
|
FROM events
|
|
594
642
|
WHERE session_id = ?
|
|
595
643
|
`).get(sid);
|
|
@@ -602,11 +650,25 @@ export class ContextModeStore {
|
|
|
602
650
|
FROM sources
|
|
603
651
|
WHERE session_id = ?
|
|
604
652
|
`).get(sid);
|
|
653
|
+
|
|
654
|
+
unrecordedSources = this.db.prepare(`
|
|
655
|
+
SELECT COALESCE(SUM(bytes_total), 0) AS unrecorded_source_bytes
|
|
656
|
+
FROM sources s
|
|
657
|
+
WHERE s.session_id = ?
|
|
658
|
+
AND s.kind NOT IN ('execution', 'batch', 'tool_output', 'exec')
|
|
659
|
+
AND NOT EXISTS (
|
|
660
|
+
SELECT 1 FROM events e
|
|
661
|
+
WHERE e.session_id = s.session_id
|
|
662
|
+
AND e.tool = 'ctx_index'
|
|
663
|
+
AND e.sandbox_bytes = s.bytes_total
|
|
664
|
+
)
|
|
665
|
+
`).get(sid);
|
|
605
666
|
}
|
|
606
667
|
|
|
607
|
-
const
|
|
668
|
+
const unrecordedBytes = unrecordedSources?.unrecorded_source_bytes || 0;
|
|
669
|
+
const totalRawBytes = (eventStats?.total_raw_bytes || 0) + unrecordedBytes;
|
|
608
670
|
const totalReturnedBytes = eventStats?.total_returned_bytes || 0;
|
|
609
|
-
const keptOutBytes =
|
|
671
|
+
const keptOutBytes = (eventStats?.total_kept_out_bytes || 0) + unrecordedBytes;
|
|
610
672
|
const savingsPercent = totalRawBytes > 0 ? Number(((keptOutBytes / totalRawBytes) * 100).toFixed(1)) : 0;
|
|
611
673
|
const tokensKeptOut = Math.round(keptOutBytes / 4);
|
|
612
674
|
|
|
@@ -707,11 +769,30 @@ export class ContextModeStore {
|
|
|
707
769
|
.run(key, String(value));
|
|
708
770
|
}
|
|
709
771
|
|
|
772
|
+
/** Delete a key from meta table */
|
|
773
|
+
deleteMeta(key) {
|
|
774
|
+
this.open();
|
|
775
|
+
this.db.prepare("DELETE FROM meta WHERE key = ?").run(key);
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
/** Clear all session-level overrides so new sessions start with lifetime defaults */
|
|
779
|
+
clearSessionOverrides() {
|
|
780
|
+
this.open();
|
|
781
|
+
this.db.prepare("DELETE FROM meta WHERE key LIKE '%:session:app' OR key LIKE '%:session:default'").run();
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
/** Check if a session or conversation has an explicit cutoff override */
|
|
785
|
+
hasCutoffOverride(sessionId) {
|
|
786
|
+
if (!sessionId || sessionId === "all" || sessionId === "lifetime") return false;
|
|
787
|
+
this.open();
|
|
788
|
+
return this.getMeta(`cutoff:session:${sessionId}`) !== null;
|
|
789
|
+
}
|
|
790
|
+
|
|
710
791
|
/** Get active cutoff in bytes (0 = none/disabled) */
|
|
711
792
|
getCutoff(sessionId = null) {
|
|
712
793
|
this.open();
|
|
713
794
|
// 1. Session/Conversation override
|
|
714
|
-
if (sessionId && sessionId !== "all") {
|
|
795
|
+
if (sessionId && sessionId !== "all" && sessionId !== "lifetime") {
|
|
715
796
|
const sessionCutoff = this.getMeta(`cutoff:session:${sessionId}`);
|
|
716
797
|
if (sessionCutoff !== null) {
|
|
717
798
|
return parseInt(sessionCutoff, 10);
|
|
@@ -747,29 +828,54 @@ export class ContextModeStore {
|
|
|
747
828
|
this.open();
|
|
748
829
|
const val = parseInt(bytes, 10);
|
|
749
830
|
const numVal = Number.isNaN(val) ? 8192 : Math.max(0, val);
|
|
750
|
-
if (sessionId && sessionId !== "all") {
|
|
831
|
+
if (sessionId && sessionId !== "all" && sessionId !== "lifetime") {
|
|
751
832
|
this.setMeta(`cutoff:session:${sessionId}`, numVal);
|
|
752
833
|
} else {
|
|
753
834
|
this.setMeta("cutoff:global", numVal);
|
|
835
|
+
// When changing cutoff in lifetime, clear session-level and default overrides
|
|
836
|
+
// so new sessions and new conversations inherit this global setting as their default.
|
|
837
|
+
this.deleteMeta("cutoff:session:app");
|
|
838
|
+
this.deleteMeta("cutoff:session:default");
|
|
754
839
|
}
|
|
755
840
|
return numVal;
|
|
756
841
|
}
|
|
757
842
|
|
|
758
|
-
/**
|
|
759
|
-
|
|
843
|
+
/** Internal helper to recompute and save the combined exempt_ext list */
|
|
844
|
+
_recomputeExemptExtensions(sessionId = null) {
|
|
845
|
+
const activeCats = this.getExemptCategories(sessionId);
|
|
846
|
+
const customs = this.getCustomExemptExtensions(sessionId);
|
|
847
|
+
const combined = new Set(customs);
|
|
848
|
+
for (const catId of activeCats) {
|
|
849
|
+
const cat = FILE_CATEGORIES[catId];
|
|
850
|
+
if (cat && Array.isArray(cat.extensions)) {
|
|
851
|
+
for (const ext of cat.extensions) {
|
|
852
|
+
combined.add(ext);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
const cleanList = Array.from(combined);
|
|
857
|
+
const serialized = JSON.stringify(cleanList);
|
|
858
|
+
if (sessionId && sessionId !== "all") {
|
|
859
|
+
this.setMeta(`exempt_ext:session:${sessionId}`, serialized);
|
|
860
|
+
} else {
|
|
861
|
+
this.setMeta("exempt_ext:global", serialized);
|
|
862
|
+
}
|
|
863
|
+
return cleanList;
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
/** Get list of active category IDs */
|
|
867
|
+
getExemptCategories(sessionId = null) {
|
|
760
868
|
this.open();
|
|
761
|
-
// 1. Session/Conversation override
|
|
762
869
|
if (sessionId && sessionId !== "all") {
|
|
763
|
-
const raw = this.getMeta(`
|
|
870
|
+
const raw = this.getMeta(`exempt_categories:session:${sessionId}`);
|
|
764
871
|
if (raw) {
|
|
765
872
|
try {
|
|
766
873
|
const parsed = JSON.parse(raw);
|
|
767
874
|
if (Array.isArray(parsed)) return parsed;
|
|
768
875
|
} catch {}
|
|
769
876
|
}
|
|
770
|
-
// If checking a conversation, fall back to the app session exemptions if set
|
|
771
877
|
if (sessionId !== "app") {
|
|
772
|
-
const appRaw = this.getMeta("
|
|
878
|
+
const appRaw = this.getMeta("exempt_categories:session:app");
|
|
773
879
|
if (appRaw) {
|
|
774
880
|
try {
|
|
775
881
|
const parsed = JSON.parse(appRaw);
|
|
@@ -778,18 +884,85 @@ export class ContextModeStore {
|
|
|
778
884
|
}
|
|
779
885
|
}
|
|
780
886
|
}
|
|
781
|
-
const globalRaw = this.getMeta("
|
|
887
|
+
const globalRaw = this.getMeta("exempt_categories:global");
|
|
782
888
|
if (globalRaw) {
|
|
783
889
|
try {
|
|
784
890
|
const parsed = JSON.parse(globalRaw);
|
|
785
891
|
if (Array.isArray(parsed)) return parsed;
|
|
786
892
|
} catch {}
|
|
787
893
|
}
|
|
788
|
-
return [...
|
|
894
|
+
return [...DEFAULT_ACTIVE_CATEGORIES];
|
|
789
895
|
}
|
|
790
896
|
|
|
791
|
-
/** Set list of
|
|
792
|
-
|
|
897
|
+
/** Set list of active category IDs */
|
|
898
|
+
setExemptCategories(categories, sessionId = null) {
|
|
899
|
+
this.open();
|
|
900
|
+
const cleanList = Array.isArray(categories)
|
|
901
|
+
? Array.from(new Set(categories.map(c => String(c).trim().toLowerCase()).filter(c => FILE_CATEGORIES[c])))
|
|
902
|
+
: [...DEFAULT_ACTIVE_CATEGORIES];
|
|
903
|
+
const serialized = JSON.stringify(cleanList);
|
|
904
|
+
if (sessionId && sessionId !== "all") {
|
|
905
|
+
this.setMeta(`exempt_categories:session:${sessionId}`, serialized);
|
|
906
|
+
} else {
|
|
907
|
+
this.setMeta("exempt_categories:global", serialized);
|
|
908
|
+
}
|
|
909
|
+
this._recomputeExemptExtensions(sessionId);
|
|
910
|
+
return cleanList;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
/** Toggle an exempt category on or off */
|
|
914
|
+
toggleExemptCategory(categoryId, enabled = null, sessionId = null) {
|
|
915
|
+
const active = this.getExemptCategories(sessionId);
|
|
916
|
+
const cat = String(categoryId || "").trim().toLowerCase();
|
|
917
|
+
if (!FILE_CATEGORIES[cat]) return active;
|
|
918
|
+
|
|
919
|
+
const isCurrentlyActive = active.includes(cat);
|
|
920
|
+
const shouldEnable = enabled !== null ? Boolean(enabled) : !isCurrentlyActive;
|
|
921
|
+
|
|
922
|
+
let updated;
|
|
923
|
+
if (shouldEnable && !isCurrentlyActive) {
|
|
924
|
+
updated = [...active, cat];
|
|
925
|
+
} else if (!shouldEnable && isCurrentlyActive) {
|
|
926
|
+
updated = active.filter(c => c !== cat);
|
|
927
|
+
} else {
|
|
928
|
+
return active;
|
|
929
|
+
}
|
|
930
|
+
return this.setExemptCategories(updated, sessionId);
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
/** Get custom file extensions (explicitly added by user) */
|
|
934
|
+
getCustomExemptExtensions(sessionId = null) {
|
|
935
|
+
this.open();
|
|
936
|
+
if (sessionId && sessionId !== "all") {
|
|
937
|
+
const raw = this.getMeta(`custom_exempt_ext:session:${sessionId}`);
|
|
938
|
+
if (raw) {
|
|
939
|
+
try {
|
|
940
|
+
const parsed = JSON.parse(raw);
|
|
941
|
+
if (Array.isArray(parsed)) return parsed;
|
|
942
|
+
} catch {}
|
|
943
|
+
}
|
|
944
|
+
if (sessionId !== "app") {
|
|
945
|
+
const appRaw = this.getMeta("custom_exempt_ext:session:app");
|
|
946
|
+
if (appRaw) {
|
|
947
|
+
try {
|
|
948
|
+
const parsed = JSON.parse(appRaw);
|
|
949
|
+
if (Array.isArray(parsed)) return parsed;
|
|
950
|
+
} catch {}
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
const globalRaw = this.getMeta("custom_exempt_ext:global");
|
|
955
|
+
if (globalRaw) {
|
|
956
|
+
try {
|
|
957
|
+
const parsed = JSON.parse(globalRaw);
|
|
958
|
+
if (Array.isArray(parsed)) return parsed;
|
|
959
|
+
} catch {}
|
|
960
|
+
}
|
|
961
|
+
return [];
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
/** Set custom file extensions */
|
|
965
|
+
setCustomExemptExtensions(extensions, sessionId = null) {
|
|
793
966
|
this.open();
|
|
794
967
|
const cleanList = Array.from(
|
|
795
968
|
new Set(
|
|
@@ -801,36 +974,150 @@ export class ContextModeStore {
|
|
|
801
974
|
);
|
|
802
975
|
const serialized = JSON.stringify(cleanList);
|
|
803
976
|
if (sessionId && sessionId !== "all") {
|
|
804
|
-
this.setMeta(`
|
|
977
|
+
this.setMeta(`custom_exempt_ext:session:${sessionId}`, serialized);
|
|
805
978
|
} else {
|
|
806
|
-
this.setMeta("
|
|
979
|
+
this.setMeta("custom_exempt_ext:global", serialized);
|
|
807
980
|
}
|
|
981
|
+
this._recomputeExemptExtensions(sessionId);
|
|
808
982
|
return cleanList;
|
|
809
983
|
}
|
|
810
984
|
|
|
811
|
-
/** Add
|
|
812
|
-
|
|
813
|
-
const list = this.
|
|
985
|
+
/** Add custom file extension */
|
|
986
|
+
addCustomExemptExtension(ext, sessionId = null) {
|
|
987
|
+
const list = this.getCustomExemptExtensions(sessionId);
|
|
814
988
|
const cleanExt = String(ext || "").trim().toLowerCase();
|
|
815
989
|
if (!cleanExt) return list;
|
|
816
990
|
const formatted = cleanExt.startsWith(".") ? cleanExt : `.${cleanExt}`;
|
|
817
991
|
if (!list.includes(formatted)) {
|
|
818
992
|
list.push(formatted);
|
|
819
|
-
this.
|
|
993
|
+
this.setCustomExemptExtensions(list, sessionId);
|
|
820
994
|
}
|
|
821
995
|
return list;
|
|
822
996
|
}
|
|
823
997
|
|
|
824
|
-
/** Remove
|
|
825
|
-
|
|
826
|
-
const list = this.
|
|
998
|
+
/** Remove custom file extension */
|
|
999
|
+
removeCustomExemptExtension(ext, sessionId = null) {
|
|
1000
|
+
const list = this.getCustomExemptExtensions(sessionId);
|
|
827
1001
|
const cleanExt = String(ext || "").trim().toLowerCase();
|
|
828
1002
|
const formatted = cleanExt.startsWith(".") ? cleanExt : `.${cleanExt}`;
|
|
829
1003
|
const filtered = list.filter((e) => e !== formatted);
|
|
830
|
-
this.
|
|
1004
|
+
this.setCustomExemptExtensions(filtered, sessionId);
|
|
831
1005
|
return filtered;
|
|
832
1006
|
}
|
|
833
1007
|
|
|
1008
|
+
/** Get list of file extensions exempt from the size cutoff */
|
|
1009
|
+
getExemptExtensions(sessionId = null) {
|
|
1010
|
+
this.open();
|
|
1011
|
+
// 1. Session/Conversation override
|
|
1012
|
+
if (sessionId && sessionId !== "all") {
|
|
1013
|
+
const raw = this.getMeta(`exempt_ext:session:${sessionId}`);
|
|
1014
|
+
if (raw) {
|
|
1015
|
+
try {
|
|
1016
|
+
const parsed = JSON.parse(raw);
|
|
1017
|
+
if (Array.isArray(parsed)) return parsed;
|
|
1018
|
+
} catch {}
|
|
1019
|
+
}
|
|
1020
|
+
// If checking a conversation, fall back to the app session exemptions if set
|
|
1021
|
+
if (sessionId !== "app") {
|
|
1022
|
+
const appRaw = this.getMeta("exempt_ext:session:app");
|
|
1023
|
+
if (appRaw) {
|
|
1024
|
+
try {
|
|
1025
|
+
const parsed = JSON.parse(appRaw);
|
|
1026
|
+
if (Array.isArray(parsed)) return parsed;
|
|
1027
|
+
} catch {}
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
const globalRaw = this.getMeta("exempt_ext:global");
|
|
1032
|
+
if (globalRaw) {
|
|
1033
|
+
try {
|
|
1034
|
+
const parsed = JSON.parse(globalRaw);
|
|
1035
|
+
if (Array.isArray(parsed)) return parsed;
|
|
1036
|
+
} catch {}
|
|
1037
|
+
}
|
|
1038
|
+
return [...DEFAULT_EXEMPT_EXTENSIONS];
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
/** Set list of file extensions exempt from the size cutoff */
|
|
1042
|
+
setExemptExtensions(extensions, sessionId = null) {
|
|
1043
|
+
this.open();
|
|
1044
|
+
if (extensions === null) {
|
|
1045
|
+
this.setExemptCategories(null, sessionId);
|
|
1046
|
+
this.setCustomExemptExtensions([], sessionId);
|
|
1047
|
+
return this.getExemptExtensions(sessionId);
|
|
1048
|
+
}
|
|
1049
|
+
const cleanList = Array.from(
|
|
1050
|
+
new Set(
|
|
1051
|
+
(Array.isArray(extensions) ? extensions : [])
|
|
1052
|
+
.map((ext) => String(ext || "").trim().toLowerCase())
|
|
1053
|
+
.filter(Boolean)
|
|
1054
|
+
.map((ext) => (ext.startsWith(".") ? ext : `.${ext}`))
|
|
1055
|
+
)
|
|
1056
|
+
);
|
|
1057
|
+
const activeCats = [];
|
|
1058
|
+
for (const [catId, catObj] of Object.entries(FILE_CATEGORIES)) {
|
|
1059
|
+
if (catObj.extensions.some((e) => cleanList.includes(e))) {
|
|
1060
|
+
activeCats.push(catId);
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
const allCatExts = new Set(activeCats.flatMap((c) => FILE_CATEGORIES[c]?.extensions || []));
|
|
1064
|
+
const customs = cleanList.filter((e) => !allCatExts.has(e));
|
|
1065
|
+
|
|
1066
|
+
this.setExemptCategories(activeCats, sessionId);
|
|
1067
|
+
this.setCustomExemptExtensions(customs, sessionId);
|
|
1068
|
+
return this.getExemptExtensions(sessionId);
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
/** Add an exempt file extension */
|
|
1072
|
+
addExemptExtension(ext, sessionId = null) {
|
|
1073
|
+
this.addCustomExemptExtension(ext, sessionId);
|
|
1074
|
+
return this.getExemptExtensions(sessionId);
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
/** Remove an exempt file extension */
|
|
1078
|
+
removeExemptExtension(ext, sessionId = null) {
|
|
1079
|
+
this.removeCustomExemptExtension(ext, sessionId);
|
|
1080
|
+
return this.getExemptExtensions(sessionId);
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
/** Check if protection is paused (true = paused, false = active) */
|
|
1084
|
+
isPaused(sessionId = null) {
|
|
1085
|
+
this.open();
|
|
1086
|
+
// 1. Session/Conversation override
|
|
1087
|
+
if (sessionId && sessionId !== "all") {
|
|
1088
|
+
const sessionPaused = this.getMeta(`paused:session:${sessionId}`);
|
|
1089
|
+
if (sessionPaused !== null) {
|
|
1090
|
+
return sessionPaused === "true" || sessionPaused === "1";
|
|
1091
|
+
}
|
|
1092
|
+
// If checking a conversation, fall back to the app session paused state if set
|
|
1093
|
+
if (sessionId !== "app") {
|
|
1094
|
+
const appPaused = this.getMeta("paused:session:app");
|
|
1095
|
+
if (appPaused !== null) {
|
|
1096
|
+
return appPaused === "true" || appPaused === "1";
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
// 2. Global paused setting
|
|
1101
|
+
const globalPaused = this.getMeta("paused:global");
|
|
1102
|
+
if (globalPaused !== null) {
|
|
1103
|
+
return globalPaused === "true" || globalPaused === "1";
|
|
1104
|
+
}
|
|
1105
|
+
return false;
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
/** Set protection paused state (true = paused, false = active) */
|
|
1109
|
+
setPaused(paused, sessionId = null) {
|
|
1110
|
+
this.open();
|
|
1111
|
+
const boolVal = Boolean(paused && paused !== "false" && paused !== "0");
|
|
1112
|
+
const strVal = boolVal ? "true" : "false";
|
|
1113
|
+
if (sessionId && sessionId !== "all") {
|
|
1114
|
+
this.setMeta(`paused:session:${sessionId}`, strVal);
|
|
1115
|
+
} else {
|
|
1116
|
+
this.setMeta("paused:global", strVal);
|
|
1117
|
+
}
|
|
1118
|
+
return boolVal;
|
|
1119
|
+
}
|
|
1120
|
+
|
|
834
1121
|
/**
|
|
835
1122
|
* Automatic LRU pruning: limits the number of indexed sources and chunks
|
|
836
1123
|
* to avoid unbounded database expansion.
|
package/lib/tools.js
CHANGED
|
@@ -233,13 +233,31 @@ export function createTools({ store, getCwd = () => process.cwd(), getSessionId
|
|
|
233
233
|
content: rawText
|
|
234
234
|
});
|
|
235
235
|
|
|
236
|
-
|
|
236
|
+
const returnObj = {
|
|
237
237
|
sourceId: indexed.sourceId,
|
|
238
238
|
label: indexed.label,
|
|
239
239
|
bytesTotal: indexed.bytesTotal,
|
|
240
240
|
chunkCount: indexed.chunkCount,
|
|
241
241
|
message: `Indexed ${formatBytes(indexed.bytesTotal)} into ${indexed.chunkCount} chunks. Query with ctx_search.`
|
|
242
242
|
};
|
|
243
|
+
|
|
244
|
+
const returnedBytes = Buffer.byteLength(JSON.stringify(returnObj), "utf8");
|
|
245
|
+
|
|
246
|
+
store.recordEvent({
|
|
247
|
+
sessionId,
|
|
248
|
+
worktree: cwd,
|
|
249
|
+
agent: "antigravity",
|
|
250
|
+
kind: "index",
|
|
251
|
+
tool: "ctx_index",
|
|
252
|
+
path: filePath || url || null,
|
|
253
|
+
ok: 1,
|
|
254
|
+
bytes: indexed.bytesTotal,
|
|
255
|
+
body: returnObj.message,
|
|
256
|
+
sandboxBytes: indexed.bytesTotal,
|
|
257
|
+
returnedBytes
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
return returnObj;
|
|
243
261
|
},
|
|
244
262
|
|
|
245
263
|
// 4. ctx_search
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "antigravity-plugin-context-mode",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Native Antigravity plugin for context-mode: SQLite FTS5 indexing, sandboxed execution, and lifecycle hook protection against context window bloat",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/plugin.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "antigravity-plugin-context-mode",
|
|
3
3
|
"displayName": "Context Mode",
|
|
4
4
|
"description": "Context window protection for Antigravity: local SQLite FTS5 search, sandboxed execution, and lifecycle hook gating against token bloat.",
|
|
5
|
-
"version": "1.
|
|
5
|
+
"version": "1.3.0",
|
|
6
6
|
"author": "Phillip Webber <github@webbsense.com>",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
package/server.js
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
2
4
|
import readline from "node:readline";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
3
6
|
import { ContextModeStore } from "./lib/store.js";
|
|
4
7
|
import { createTools } from "./lib/tools.js";
|
|
5
8
|
|
|
9
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
let pkgVersion = "1.0.0";
|
|
11
|
+
try {
|
|
12
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, "package.json"), "utf8"));
|
|
13
|
+
if (pkg.version) pkgVersion = pkg.version;
|
|
14
|
+
} catch {}
|
|
15
|
+
|
|
6
16
|
const store = new ContextModeStore();
|
|
7
17
|
let currentCwd = process.cwd();
|
|
8
18
|
let currentSessionId =
|
|
@@ -14,6 +24,8 @@ const tools = createTools({
|
|
|
14
24
|
store,
|
|
15
25
|
getCwd: () => currentCwd,
|
|
16
26
|
getSessionId: () =>
|
|
27
|
+
process.env.ANTIGRAVITY_ROOT_CONVERSATION_ID ||
|
|
28
|
+
process.env.ANTIGRAVITY_PARENT_CONVERSATION_ID ||
|
|
17
29
|
process.env.ANTIGRAVITY_CONVERSATION_ID ||
|
|
18
30
|
process.env.CONVERSATION_ID ||
|
|
19
31
|
currentSessionId ||
|
|
@@ -196,7 +208,7 @@ rl.on("line", async (line) => {
|
|
|
196
208
|
},
|
|
197
209
|
serverInfo: {
|
|
198
210
|
name: "antigravity-plugin-context-mode",
|
|
199
|
-
version:
|
|
211
|
+
version: pkgVersion
|
|
200
212
|
}
|
|
201
213
|
});
|
|
202
214
|
break;
|