claude-memory-layer 1.0.8 → 1.0.9
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/.claude/settings.local.json +7 -1
- package/.claude-memory/test.sqlite +0 -0
- package/.history/package_20260202114053.json +49 -0
- package/HANDOFF.md +92 -0
- package/dist/cli/index.js +1150 -71
- package/dist/cli/index.js.map +4 -4
- package/dist/core/index.js +1033 -47
- package/dist/core/index.js.map +4 -4
- package/dist/hooks/post-tool-use.js +5589 -0
- package/dist/hooks/post-tool-use.js.map +7 -0
- package/dist/hooks/session-end.js +1117 -64
- package/dist/hooks/session-end.js.map +4 -4
- package/dist/hooks/session-start.js +1112 -63
- package/dist/hooks/session-start.js.map +4 -4
- package/dist/hooks/stop.js +1117 -64
- package/dist/hooks/stop.js.map +4 -4
- package/dist/hooks/user-prompt-submit.js +1151 -67
- package/dist/hooks/user-prompt-submit.js.map +4 -4
- package/dist/server/api/index.js +1145 -70
- package/dist/server/api/index.js.map +4 -4
- package/dist/server/index.js +1145 -70
- package/dist/server/index.js.map +4 -4
- package/dist/services/memory-service.js +1122 -65
- package/dist/services/memory-service.js.map +4 -4
- package/dist/ui/app.js +304 -0
- package/dist/ui/index.html +195 -1188
- package/dist/ui/style.css +595 -0
- package/package.json +3 -1
- package/scripts/build.ts +2 -0
- package/src/core/event-store.ts +18 -0
- package/src/core/index.ts +3 -0
- package/src/core/retriever.ts +4 -1
- package/src/core/sqlite-event-store.ts +849 -0
- package/src/core/sqlite-wrapper.ts +108 -0
- package/src/core/sync-worker.ts +228 -0
- package/src/core/vector-worker.ts +44 -14
- package/src/hooks/user-prompt-submit.ts +53 -4
- package/src/server/api/stats.ts +37 -7
- package/src/services/memory-service.ts +168 -39
- package/src/ui/app.js +304 -0
- package/src/ui/index.html +195 -1188
- package/src/ui/style.css +595 -0
- package/test_access.js +49 -0
|
@@ -701,26 +701,975 @@ var EventStore = class {
|
|
|
701
701
|
tags: row.tags ? JSON.parse(row.tags) : void 0
|
|
702
702
|
}));
|
|
703
703
|
}
|
|
704
|
+
/**
|
|
705
|
+
* Increment access count for events (stub for compatibility)
|
|
706
|
+
*/
|
|
707
|
+
async incrementAccessCount(eventIds) {
|
|
708
|
+
return Promise.resolve();
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Get most accessed memories (stub for compatibility)
|
|
712
|
+
*/
|
|
713
|
+
async getMostAccessed(limit = 10) {
|
|
714
|
+
return [];
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* Close database connection
|
|
718
|
+
*/
|
|
719
|
+
async close() {
|
|
720
|
+
await dbClose(this.db);
|
|
721
|
+
}
|
|
722
|
+
/**
|
|
723
|
+
* Convert database row to MemoryEvent
|
|
724
|
+
*/
|
|
725
|
+
rowToEvent(row) {
|
|
726
|
+
return {
|
|
727
|
+
id: row.id,
|
|
728
|
+
eventType: row.event_type,
|
|
729
|
+
sessionId: row.session_id,
|
|
730
|
+
timestamp: toDate(row.timestamp),
|
|
731
|
+
content: row.content,
|
|
732
|
+
canonicalKey: row.canonical_key,
|
|
733
|
+
dedupeKey: row.dedupe_key,
|
|
734
|
+
metadata: row.metadata ? JSON.parse(row.metadata) : void 0
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
};
|
|
738
|
+
|
|
739
|
+
// src/core/sqlite-event-store.ts
|
|
740
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
741
|
+
|
|
742
|
+
// src/core/sqlite-wrapper.ts
|
|
743
|
+
import Database from "better-sqlite3";
|
|
744
|
+
function createSQLiteDatabase(path2, options) {
|
|
745
|
+
const db = new Database(path2, {
|
|
746
|
+
readonly: options?.readonly ?? false
|
|
747
|
+
});
|
|
748
|
+
if (!options?.readonly && (options?.walMode ?? true)) {
|
|
749
|
+
db.pragma("journal_mode = WAL");
|
|
750
|
+
db.pragma("synchronous = NORMAL");
|
|
751
|
+
db.pragma("busy_timeout = 5000");
|
|
752
|
+
}
|
|
753
|
+
return db;
|
|
754
|
+
}
|
|
755
|
+
function sqliteRun(db, sql, params = []) {
|
|
756
|
+
const stmt = db.prepare(sql);
|
|
757
|
+
return stmt.run(...params);
|
|
758
|
+
}
|
|
759
|
+
function sqliteAll(db, sql, params = []) {
|
|
760
|
+
const stmt = db.prepare(sql);
|
|
761
|
+
return stmt.all(...params);
|
|
762
|
+
}
|
|
763
|
+
function sqliteGet(db, sql, params = []) {
|
|
764
|
+
const stmt = db.prepare(sql);
|
|
765
|
+
return stmt.get(...params);
|
|
766
|
+
}
|
|
767
|
+
function sqliteExec(db, sql) {
|
|
768
|
+
db.exec(sql);
|
|
769
|
+
}
|
|
770
|
+
function sqliteClose(db) {
|
|
771
|
+
db.close();
|
|
772
|
+
}
|
|
773
|
+
function toDateFromSQLite(value) {
|
|
774
|
+
if (value instanceof Date)
|
|
775
|
+
return value;
|
|
776
|
+
if (typeof value === "string")
|
|
777
|
+
return new Date(value);
|
|
778
|
+
if (typeof value === "number")
|
|
779
|
+
return new Date(value);
|
|
780
|
+
return new Date(String(value));
|
|
781
|
+
}
|
|
782
|
+
function toSQLiteTimestamp(date) {
|
|
783
|
+
return date.toISOString();
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
// src/core/sqlite-event-store.ts
|
|
787
|
+
var SQLiteEventStore = class {
|
|
788
|
+
constructor(dbPath, options) {
|
|
789
|
+
this.dbPath = dbPath;
|
|
790
|
+
this.readOnly = options?.readonly ?? false;
|
|
791
|
+
this.db = createSQLiteDatabase(dbPath, {
|
|
792
|
+
readonly: this.readOnly,
|
|
793
|
+
walMode: !this.readOnly
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
db;
|
|
797
|
+
initialized = false;
|
|
798
|
+
readOnly;
|
|
799
|
+
/**
|
|
800
|
+
* Initialize database schema
|
|
801
|
+
*/
|
|
802
|
+
async initialize() {
|
|
803
|
+
if (this.initialized)
|
|
804
|
+
return;
|
|
805
|
+
if (this.readOnly) {
|
|
806
|
+
this.initialized = true;
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
sqliteExec(this.db, `
|
|
810
|
+
-- L0 EventStore: Single Source of Truth (immutable, append-only)
|
|
811
|
+
CREATE TABLE IF NOT EXISTS events (
|
|
812
|
+
id TEXT PRIMARY KEY,
|
|
813
|
+
event_type TEXT NOT NULL,
|
|
814
|
+
session_id TEXT NOT NULL,
|
|
815
|
+
timestamp TEXT NOT NULL,
|
|
816
|
+
content TEXT NOT NULL,
|
|
817
|
+
canonical_key TEXT NOT NULL,
|
|
818
|
+
dedupe_key TEXT UNIQUE,
|
|
819
|
+
metadata TEXT,
|
|
820
|
+
access_count INTEGER DEFAULT 0,
|
|
821
|
+
last_accessed_at TEXT
|
|
822
|
+
);
|
|
823
|
+
|
|
824
|
+
-- Dedup table for idempotency
|
|
825
|
+
CREATE TABLE IF NOT EXISTS event_dedup (
|
|
826
|
+
dedupe_key TEXT PRIMARY KEY,
|
|
827
|
+
event_id TEXT NOT NULL,
|
|
828
|
+
created_at TEXT DEFAULT (datetime('now'))
|
|
829
|
+
);
|
|
830
|
+
|
|
831
|
+
-- Session metadata
|
|
832
|
+
CREATE TABLE IF NOT EXISTS sessions (
|
|
833
|
+
id TEXT PRIMARY KEY,
|
|
834
|
+
started_at TEXT NOT NULL,
|
|
835
|
+
ended_at TEXT,
|
|
836
|
+
project_path TEXT,
|
|
837
|
+
summary TEXT,
|
|
838
|
+
tags TEXT
|
|
839
|
+
);
|
|
840
|
+
|
|
841
|
+
-- Insights (derived data, rebuildable)
|
|
842
|
+
CREATE TABLE IF NOT EXISTS insights (
|
|
843
|
+
id TEXT PRIMARY KEY,
|
|
844
|
+
insight_type TEXT NOT NULL,
|
|
845
|
+
content TEXT NOT NULL,
|
|
846
|
+
canonical_key TEXT NOT NULL,
|
|
847
|
+
confidence REAL,
|
|
848
|
+
source_events TEXT,
|
|
849
|
+
created_at TEXT,
|
|
850
|
+
last_updated TEXT
|
|
851
|
+
);
|
|
852
|
+
|
|
853
|
+
-- Embedding Outbox (Single-Writer Pattern)
|
|
854
|
+
CREATE TABLE IF NOT EXISTS embedding_outbox (
|
|
855
|
+
id TEXT PRIMARY KEY,
|
|
856
|
+
event_id TEXT NOT NULL,
|
|
857
|
+
content TEXT NOT NULL,
|
|
858
|
+
status TEXT DEFAULT 'pending',
|
|
859
|
+
retry_count INTEGER DEFAULT 0,
|
|
860
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
861
|
+
processed_at TEXT,
|
|
862
|
+
error_message TEXT
|
|
863
|
+
);
|
|
864
|
+
|
|
865
|
+
-- Projection offset tracking
|
|
866
|
+
CREATE TABLE IF NOT EXISTS projection_offsets (
|
|
867
|
+
projection_name TEXT PRIMARY KEY,
|
|
868
|
+
last_event_id TEXT,
|
|
869
|
+
last_timestamp TEXT,
|
|
870
|
+
updated_at TEXT DEFAULT (datetime('now'))
|
|
871
|
+
);
|
|
872
|
+
|
|
873
|
+
-- Memory level tracking
|
|
874
|
+
CREATE TABLE IF NOT EXISTS memory_levels (
|
|
875
|
+
event_id TEXT PRIMARY KEY,
|
|
876
|
+
level TEXT NOT NULL DEFAULT 'L0',
|
|
877
|
+
promoted_at TEXT DEFAULT (datetime('now'))
|
|
878
|
+
);
|
|
879
|
+
|
|
880
|
+
-- Entries (immutable memory units)
|
|
881
|
+
CREATE TABLE IF NOT EXISTS entries (
|
|
882
|
+
entry_id TEXT PRIMARY KEY,
|
|
883
|
+
created_ts TEXT NOT NULL,
|
|
884
|
+
entry_type TEXT NOT NULL,
|
|
885
|
+
title TEXT NOT NULL,
|
|
886
|
+
content_json TEXT NOT NULL,
|
|
887
|
+
stage TEXT NOT NULL DEFAULT 'raw',
|
|
888
|
+
status TEXT DEFAULT 'active',
|
|
889
|
+
superseded_by TEXT,
|
|
890
|
+
build_id TEXT,
|
|
891
|
+
evidence_json TEXT,
|
|
892
|
+
canonical_key TEXT,
|
|
893
|
+
created_at TEXT DEFAULT (datetime('now'))
|
|
894
|
+
);
|
|
895
|
+
|
|
896
|
+
-- Entities (task/condition/artifact)
|
|
897
|
+
CREATE TABLE IF NOT EXISTS entities (
|
|
898
|
+
entity_id TEXT PRIMARY KEY,
|
|
899
|
+
entity_type TEXT NOT NULL,
|
|
900
|
+
canonical_key TEXT NOT NULL,
|
|
901
|
+
title TEXT NOT NULL,
|
|
902
|
+
stage TEXT NOT NULL DEFAULT 'raw',
|
|
903
|
+
status TEXT NOT NULL DEFAULT 'active',
|
|
904
|
+
current_json TEXT NOT NULL,
|
|
905
|
+
title_norm TEXT,
|
|
906
|
+
search_text TEXT,
|
|
907
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
908
|
+
updated_at TEXT DEFAULT (datetime('now'))
|
|
909
|
+
);
|
|
910
|
+
|
|
911
|
+
-- Entity aliases for canonical key lookup
|
|
912
|
+
CREATE TABLE IF NOT EXISTS entity_aliases (
|
|
913
|
+
entity_type TEXT NOT NULL,
|
|
914
|
+
canonical_key TEXT NOT NULL,
|
|
915
|
+
entity_id TEXT NOT NULL,
|
|
916
|
+
is_primary INTEGER DEFAULT 0,
|
|
917
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
918
|
+
PRIMARY KEY(entity_type, canonical_key)
|
|
919
|
+
);
|
|
920
|
+
|
|
921
|
+
-- Edges (relationships between entries/entities)
|
|
922
|
+
CREATE TABLE IF NOT EXISTS edges (
|
|
923
|
+
edge_id TEXT PRIMARY KEY,
|
|
924
|
+
src_type TEXT NOT NULL,
|
|
925
|
+
src_id TEXT NOT NULL,
|
|
926
|
+
rel_type TEXT NOT NULL,
|
|
927
|
+
dst_type TEXT NOT NULL,
|
|
928
|
+
dst_id TEXT NOT NULL,
|
|
929
|
+
meta_json TEXT,
|
|
930
|
+
created_at TEXT DEFAULT (datetime('now'))
|
|
931
|
+
);
|
|
932
|
+
|
|
933
|
+
-- Vector Outbox V2 Table
|
|
934
|
+
CREATE TABLE IF NOT EXISTS vector_outbox (
|
|
935
|
+
job_id TEXT PRIMARY KEY,
|
|
936
|
+
item_kind TEXT NOT NULL,
|
|
937
|
+
item_id TEXT NOT NULL,
|
|
938
|
+
embedding_version TEXT NOT NULL,
|
|
939
|
+
status TEXT NOT NULL DEFAULT 'pending',
|
|
940
|
+
retry_count INTEGER DEFAULT 0,
|
|
941
|
+
error TEXT,
|
|
942
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
943
|
+
updated_at TEXT DEFAULT (datetime('now')),
|
|
944
|
+
UNIQUE(item_kind, item_id, embedding_version)
|
|
945
|
+
);
|
|
946
|
+
|
|
947
|
+
-- Build Runs
|
|
948
|
+
CREATE TABLE IF NOT EXISTS build_runs (
|
|
949
|
+
build_id TEXT PRIMARY KEY,
|
|
950
|
+
started_at TEXT NOT NULL,
|
|
951
|
+
finished_at TEXT,
|
|
952
|
+
extractor_model TEXT NOT NULL,
|
|
953
|
+
extractor_prompt_hash TEXT NOT NULL,
|
|
954
|
+
embedder_model TEXT NOT NULL,
|
|
955
|
+
embedding_version TEXT NOT NULL,
|
|
956
|
+
idris_version TEXT NOT NULL,
|
|
957
|
+
schema_version TEXT NOT NULL,
|
|
958
|
+
status TEXT NOT NULL DEFAULT 'running',
|
|
959
|
+
error TEXT
|
|
960
|
+
);
|
|
961
|
+
|
|
962
|
+
-- Pipeline Metrics
|
|
963
|
+
CREATE TABLE IF NOT EXISTS pipeline_metrics (
|
|
964
|
+
id TEXT PRIMARY KEY,
|
|
965
|
+
ts TEXT NOT NULL,
|
|
966
|
+
stage TEXT NOT NULL,
|
|
967
|
+
latency_ms REAL NOT NULL,
|
|
968
|
+
success INTEGER NOT NULL,
|
|
969
|
+
error TEXT,
|
|
970
|
+
session_id TEXT
|
|
971
|
+
);
|
|
972
|
+
|
|
973
|
+
-- Working Set table (active memory window)
|
|
974
|
+
CREATE TABLE IF NOT EXISTS working_set (
|
|
975
|
+
id TEXT PRIMARY KEY,
|
|
976
|
+
event_id TEXT NOT NULL,
|
|
977
|
+
added_at TEXT DEFAULT (datetime('now')),
|
|
978
|
+
relevance_score REAL DEFAULT 1.0,
|
|
979
|
+
topics TEXT,
|
|
980
|
+
expires_at TEXT
|
|
981
|
+
);
|
|
982
|
+
|
|
983
|
+
-- Consolidated Memories table (long-term integrated memories)
|
|
984
|
+
CREATE TABLE IF NOT EXISTS consolidated_memories (
|
|
985
|
+
memory_id TEXT PRIMARY KEY,
|
|
986
|
+
summary TEXT NOT NULL,
|
|
987
|
+
topics TEXT,
|
|
988
|
+
source_events TEXT,
|
|
989
|
+
confidence REAL DEFAULT 0.5,
|
|
990
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
991
|
+
accessed_at TEXT,
|
|
992
|
+
access_count INTEGER DEFAULT 0
|
|
993
|
+
);
|
|
994
|
+
|
|
995
|
+
-- Continuity Log table (tracks context transitions)
|
|
996
|
+
CREATE TABLE IF NOT EXISTS continuity_log (
|
|
997
|
+
log_id TEXT PRIMARY KEY,
|
|
998
|
+
from_context_id TEXT,
|
|
999
|
+
to_context_id TEXT,
|
|
1000
|
+
continuity_score REAL,
|
|
1001
|
+
transition_type TEXT,
|
|
1002
|
+
created_at TEXT DEFAULT (datetime('now'))
|
|
1003
|
+
);
|
|
1004
|
+
|
|
1005
|
+
-- Endless Mode Config table
|
|
1006
|
+
CREATE TABLE IF NOT EXISTS endless_config (
|
|
1007
|
+
key TEXT PRIMARY KEY,
|
|
1008
|
+
value TEXT,
|
|
1009
|
+
updated_at TEXT DEFAULT (datetime('now'))
|
|
1010
|
+
);
|
|
1011
|
+
|
|
1012
|
+
-- Sync position tracking (for SQLite -> DuckDB sync)
|
|
1013
|
+
CREATE TABLE IF NOT EXISTS sync_positions (
|
|
1014
|
+
target_name TEXT PRIMARY KEY,
|
|
1015
|
+
last_event_id TEXT,
|
|
1016
|
+
last_timestamp TEXT,
|
|
1017
|
+
updated_at TEXT DEFAULT (datetime('now'))
|
|
1018
|
+
);
|
|
1019
|
+
|
|
1020
|
+
-- Create indexes
|
|
1021
|
+
CREATE INDEX IF NOT EXISTS idx_events_session ON events(session_id);
|
|
1022
|
+
CREATE INDEX IF NOT EXISTS idx_events_timestamp ON events(timestamp);
|
|
1023
|
+
CREATE INDEX IF NOT EXISTS idx_entries_type ON entries(entry_type);
|
|
1024
|
+
CREATE INDEX IF NOT EXISTS idx_entries_stage ON entries(stage);
|
|
1025
|
+
CREATE INDEX IF NOT EXISTS idx_entries_canonical ON entries(canonical_key);
|
|
1026
|
+
CREATE INDEX IF NOT EXISTS idx_entities_type_key ON entities(entity_type, canonical_key);
|
|
1027
|
+
CREATE INDEX IF NOT EXISTS idx_entities_status ON entities(status);
|
|
1028
|
+
CREATE INDEX IF NOT EXISTS idx_edges_src ON edges(src_id, rel_type);
|
|
1029
|
+
CREATE INDEX IF NOT EXISTS idx_edges_dst ON edges(dst_id, rel_type);
|
|
1030
|
+
CREATE INDEX IF NOT EXISTS idx_edges_rel ON edges(rel_type);
|
|
1031
|
+
CREATE INDEX IF NOT EXISTS idx_outbox_status ON vector_outbox(status);
|
|
1032
|
+
CREATE INDEX IF NOT EXISTS idx_working_set_expires ON working_set(expires_at);
|
|
1033
|
+
CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score);
|
|
1034
|
+
CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence);
|
|
1035
|
+
CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at);
|
|
1036
|
+
CREATE INDEX IF NOT EXISTS idx_embedding_outbox_status ON embedding_outbox(status);
|
|
1037
|
+
`);
|
|
1038
|
+
const tableInfo = sqliteAll(this.db, "PRAGMA table_info(events)", []);
|
|
1039
|
+
const columnNames = tableInfo.map((col) => col.name);
|
|
1040
|
+
if (!columnNames.includes("access_count")) {
|
|
1041
|
+
try {
|
|
1042
|
+
sqliteExec(this.db, `
|
|
1043
|
+
ALTER TABLE events ADD COLUMN access_count INTEGER DEFAULT 0;
|
|
1044
|
+
`);
|
|
1045
|
+
} catch (err) {
|
|
1046
|
+
console.error("Error adding access_count column:", err);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
if (!columnNames.includes("last_accessed_at")) {
|
|
1050
|
+
try {
|
|
1051
|
+
sqliteExec(this.db, `
|
|
1052
|
+
ALTER TABLE events ADD COLUMN last_accessed_at TEXT;
|
|
1053
|
+
`);
|
|
1054
|
+
} catch (err) {
|
|
1055
|
+
console.error("Error adding last_accessed_at column:", err);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
try {
|
|
1059
|
+
sqliteExec(this.db, `
|
|
1060
|
+
CREATE INDEX IF NOT EXISTS idx_events_access_count ON events(access_count DESC);
|
|
1061
|
+
`);
|
|
1062
|
+
} catch (err) {
|
|
1063
|
+
}
|
|
1064
|
+
try {
|
|
1065
|
+
sqliteExec(this.db, `
|
|
1066
|
+
CREATE INDEX IF NOT EXISTS idx_events_last_accessed ON events(last_accessed_at DESC);
|
|
1067
|
+
`);
|
|
1068
|
+
} catch (err) {
|
|
1069
|
+
}
|
|
1070
|
+
this.initialized = true;
|
|
1071
|
+
}
|
|
1072
|
+
/**
|
|
1073
|
+
* Append event to store (Append-only, Idempotent)
|
|
1074
|
+
*/
|
|
1075
|
+
async append(input) {
|
|
1076
|
+
await this.initialize();
|
|
1077
|
+
const canonicalKey = makeCanonicalKey(input.content);
|
|
1078
|
+
const dedupeKey = makeDedupeKey(input.content, input.sessionId);
|
|
1079
|
+
const existing = sqliteGet(
|
|
1080
|
+
this.db,
|
|
1081
|
+
`SELECT event_id FROM event_dedup WHERE dedupe_key = ?`,
|
|
1082
|
+
[dedupeKey]
|
|
1083
|
+
);
|
|
1084
|
+
if (existing) {
|
|
1085
|
+
return {
|
|
1086
|
+
success: true,
|
|
1087
|
+
eventId: existing.event_id,
|
|
1088
|
+
isDuplicate: true
|
|
1089
|
+
};
|
|
1090
|
+
}
|
|
1091
|
+
const id = randomUUID2();
|
|
1092
|
+
const timestamp = toSQLiteTimestamp(input.timestamp);
|
|
1093
|
+
try {
|
|
1094
|
+
const insertEvent = this.db.prepare(`
|
|
1095
|
+
INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata)
|
|
1096
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
1097
|
+
`);
|
|
1098
|
+
const insertDedup = this.db.prepare(`
|
|
1099
|
+
INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)
|
|
1100
|
+
`);
|
|
1101
|
+
const insertLevel = this.db.prepare(`
|
|
1102
|
+
INSERT INTO memory_levels (event_id, level) VALUES (?, 'L0')
|
|
1103
|
+
`);
|
|
1104
|
+
const transaction = this.db.transaction(() => {
|
|
1105
|
+
insertEvent.run(
|
|
1106
|
+
id,
|
|
1107
|
+
input.eventType,
|
|
1108
|
+
input.sessionId,
|
|
1109
|
+
timestamp,
|
|
1110
|
+
input.content,
|
|
1111
|
+
canonicalKey,
|
|
1112
|
+
dedupeKey,
|
|
1113
|
+
JSON.stringify(input.metadata || {})
|
|
1114
|
+
);
|
|
1115
|
+
insertDedup.run(dedupeKey, id);
|
|
1116
|
+
insertLevel.run(id);
|
|
1117
|
+
});
|
|
1118
|
+
transaction();
|
|
1119
|
+
return { success: true, eventId: id, isDuplicate: false };
|
|
1120
|
+
} catch (error) {
|
|
1121
|
+
return {
|
|
1122
|
+
success: false,
|
|
1123
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1124
|
+
};
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
/**
|
|
1128
|
+
* Get events by session ID
|
|
1129
|
+
*/
|
|
1130
|
+
async getSessionEvents(sessionId) {
|
|
1131
|
+
await this.initialize();
|
|
1132
|
+
const rows = sqliteAll(
|
|
1133
|
+
this.db,
|
|
1134
|
+
`SELECT * FROM events WHERE session_id = ? ORDER BY timestamp ASC`,
|
|
1135
|
+
[sessionId]
|
|
1136
|
+
);
|
|
1137
|
+
return rows.map(this.rowToEvent);
|
|
1138
|
+
}
|
|
1139
|
+
/**
|
|
1140
|
+
* Get recent events
|
|
1141
|
+
*/
|
|
1142
|
+
async getRecentEvents(limit = 100) {
|
|
1143
|
+
await this.initialize();
|
|
1144
|
+
const rows = sqliteAll(
|
|
1145
|
+
this.db,
|
|
1146
|
+
`SELECT * FROM events ORDER BY timestamp DESC LIMIT ?`,
|
|
1147
|
+
[limit]
|
|
1148
|
+
);
|
|
1149
|
+
return rows.map(this.rowToEvent);
|
|
1150
|
+
}
|
|
1151
|
+
/**
|
|
1152
|
+
* Get event by ID
|
|
1153
|
+
*/
|
|
1154
|
+
async getEvent(id) {
|
|
1155
|
+
await this.initialize();
|
|
1156
|
+
const row = sqliteGet(
|
|
1157
|
+
this.db,
|
|
1158
|
+
`SELECT * FROM events WHERE id = ?`,
|
|
1159
|
+
[id]
|
|
1160
|
+
);
|
|
1161
|
+
if (!row)
|
|
1162
|
+
return null;
|
|
1163
|
+
return this.rowToEvent(row);
|
|
1164
|
+
}
|
|
1165
|
+
/**
|
|
1166
|
+
* Get events since a timestamp (for sync)
|
|
1167
|
+
*/
|
|
1168
|
+
async getEventsSince(timestamp, limit = 1e3) {
|
|
1169
|
+
await this.initialize();
|
|
1170
|
+
const rows = sqliteAll(
|
|
1171
|
+
this.db,
|
|
1172
|
+
`SELECT * FROM events WHERE timestamp > ? ORDER BY timestamp ASC LIMIT ?`,
|
|
1173
|
+
[timestamp, limit]
|
|
1174
|
+
);
|
|
1175
|
+
return rows.map(this.rowToEvent);
|
|
1176
|
+
}
|
|
1177
|
+
/**
|
|
1178
|
+
* Create or update session
|
|
1179
|
+
*/
|
|
1180
|
+
async upsertSession(session) {
|
|
1181
|
+
await this.initialize();
|
|
1182
|
+
const existing = sqliteGet(
|
|
1183
|
+
this.db,
|
|
1184
|
+
`SELECT id FROM sessions WHERE id = ?`,
|
|
1185
|
+
[session.id]
|
|
1186
|
+
);
|
|
1187
|
+
if (!existing) {
|
|
1188
|
+
sqliteRun(
|
|
1189
|
+
this.db,
|
|
1190
|
+
`INSERT INTO sessions (id, started_at, project_path, tags)
|
|
1191
|
+
VALUES (?, ?, ?, ?)`,
|
|
1192
|
+
[
|
|
1193
|
+
session.id,
|
|
1194
|
+
toSQLiteTimestamp(session.startedAt || /* @__PURE__ */ new Date()),
|
|
1195
|
+
session.projectPath || null,
|
|
1196
|
+
JSON.stringify(session.tags || [])
|
|
1197
|
+
]
|
|
1198
|
+
);
|
|
1199
|
+
} else {
|
|
1200
|
+
const updates = [];
|
|
1201
|
+
const values = [];
|
|
1202
|
+
if (session.endedAt) {
|
|
1203
|
+
updates.push("ended_at = ?");
|
|
1204
|
+
values.push(toSQLiteTimestamp(session.endedAt));
|
|
1205
|
+
}
|
|
1206
|
+
if (session.summary) {
|
|
1207
|
+
updates.push("summary = ?");
|
|
1208
|
+
values.push(session.summary);
|
|
1209
|
+
}
|
|
1210
|
+
if (session.tags) {
|
|
1211
|
+
updates.push("tags = ?");
|
|
1212
|
+
values.push(JSON.stringify(session.tags));
|
|
1213
|
+
}
|
|
1214
|
+
if (updates.length > 0) {
|
|
1215
|
+
values.push(session.id);
|
|
1216
|
+
sqliteRun(
|
|
1217
|
+
this.db,
|
|
1218
|
+
`UPDATE sessions SET ${updates.join(", ")} WHERE id = ?`,
|
|
1219
|
+
values
|
|
1220
|
+
);
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
/**
|
|
1225
|
+
* Get session by ID
|
|
1226
|
+
*/
|
|
1227
|
+
async getSession(id) {
|
|
1228
|
+
await this.initialize();
|
|
1229
|
+
const row = sqliteGet(
|
|
1230
|
+
this.db,
|
|
1231
|
+
`SELECT * FROM sessions WHERE id = ?`,
|
|
1232
|
+
[id]
|
|
1233
|
+
);
|
|
1234
|
+
if (!row)
|
|
1235
|
+
return null;
|
|
1236
|
+
return {
|
|
1237
|
+
id: row.id,
|
|
1238
|
+
startedAt: toDateFromSQLite(row.started_at),
|
|
1239
|
+
endedAt: row.ended_at ? toDateFromSQLite(row.ended_at) : void 0,
|
|
1240
|
+
projectPath: row.project_path,
|
|
1241
|
+
summary: row.summary,
|
|
1242
|
+
tags: row.tags ? JSON.parse(row.tags) : void 0
|
|
1243
|
+
};
|
|
1244
|
+
}
|
|
1245
|
+
/**
|
|
1246
|
+
* Get all sessions
|
|
1247
|
+
*/
|
|
1248
|
+
async getAllSessions() {
|
|
1249
|
+
await this.initialize();
|
|
1250
|
+
const rows = sqliteAll(
|
|
1251
|
+
this.db,
|
|
1252
|
+
`SELECT * FROM sessions ORDER BY started_at DESC`
|
|
1253
|
+
);
|
|
1254
|
+
return rows.map((row) => ({
|
|
1255
|
+
id: row.id,
|
|
1256
|
+
startedAt: toDateFromSQLite(row.started_at),
|
|
1257
|
+
endedAt: row.ended_at ? toDateFromSQLite(row.ended_at) : void 0,
|
|
1258
|
+
projectPath: row.project_path,
|
|
1259
|
+
summary: row.summary,
|
|
1260
|
+
tags: row.tags ? JSON.parse(row.tags) : void 0
|
|
1261
|
+
}));
|
|
1262
|
+
}
|
|
1263
|
+
/**
|
|
1264
|
+
* Add to embedding outbox
|
|
1265
|
+
*/
|
|
1266
|
+
async enqueueForEmbedding(eventId, content) {
|
|
1267
|
+
await this.initialize();
|
|
1268
|
+
const id = randomUUID2();
|
|
1269
|
+
sqliteRun(
|
|
1270
|
+
this.db,
|
|
1271
|
+
`INSERT INTO embedding_outbox (id, event_id, content, status, retry_count)
|
|
1272
|
+
VALUES (?, ?, ?, 'pending', 0)`,
|
|
1273
|
+
[id, eventId, content]
|
|
1274
|
+
);
|
|
1275
|
+
return id;
|
|
1276
|
+
}
|
|
1277
|
+
/**
|
|
1278
|
+
* Get pending outbox items
|
|
1279
|
+
*/
|
|
1280
|
+
async getPendingOutboxItems(limit = 32) {
|
|
1281
|
+
await this.initialize();
|
|
1282
|
+
const pending = sqliteAll(
|
|
1283
|
+
this.db,
|
|
1284
|
+
`SELECT * FROM embedding_outbox
|
|
1285
|
+
WHERE status = 'pending'
|
|
1286
|
+
ORDER BY created_at
|
|
1287
|
+
LIMIT ?`,
|
|
1288
|
+
[limit]
|
|
1289
|
+
);
|
|
1290
|
+
if (pending.length === 0)
|
|
1291
|
+
return [];
|
|
1292
|
+
const ids = pending.map((r) => r.id);
|
|
1293
|
+
const placeholders = ids.map(() => "?").join(",");
|
|
1294
|
+
sqliteRun(
|
|
1295
|
+
this.db,
|
|
1296
|
+
`UPDATE embedding_outbox SET status = 'processing' WHERE id IN (${placeholders})`,
|
|
1297
|
+
ids
|
|
1298
|
+
);
|
|
1299
|
+
return pending.map((row) => ({
|
|
1300
|
+
id: row.id,
|
|
1301
|
+
eventId: row.event_id,
|
|
1302
|
+
content: row.content,
|
|
1303
|
+
status: "processing",
|
|
1304
|
+
retryCount: row.retry_count,
|
|
1305
|
+
createdAt: toDateFromSQLite(row.created_at),
|
|
1306
|
+
errorMessage: row.error_message
|
|
1307
|
+
}));
|
|
1308
|
+
}
|
|
1309
|
+
/**
|
|
1310
|
+
* Mark outbox items as done
|
|
1311
|
+
*/
|
|
1312
|
+
async completeOutboxItems(ids) {
|
|
1313
|
+
if (ids.length === 0)
|
|
1314
|
+
return;
|
|
1315
|
+
const placeholders = ids.map(() => "?").join(",");
|
|
1316
|
+
sqliteRun(
|
|
1317
|
+
this.db,
|
|
1318
|
+
`DELETE FROM embedding_outbox WHERE id IN (${placeholders})`,
|
|
1319
|
+
ids
|
|
1320
|
+
);
|
|
1321
|
+
}
|
|
1322
|
+
/**
|
|
1323
|
+
* Mark outbox items as failed
|
|
1324
|
+
*/
|
|
1325
|
+
async failOutboxItems(ids, error) {
|
|
1326
|
+
if (ids.length === 0)
|
|
1327
|
+
return;
|
|
1328
|
+
const placeholders = ids.map(() => "?").join(",");
|
|
1329
|
+
sqliteRun(
|
|
1330
|
+
this.db,
|
|
1331
|
+
`UPDATE embedding_outbox
|
|
1332
|
+
SET status = CASE WHEN retry_count >= 3 THEN 'failed' ELSE 'pending' END,
|
|
1333
|
+
retry_count = retry_count + 1,
|
|
1334
|
+
error_message = ?
|
|
1335
|
+
WHERE id IN (${placeholders})`,
|
|
1336
|
+
[error, ...ids]
|
|
1337
|
+
);
|
|
1338
|
+
}
|
|
1339
|
+
/**
|
|
1340
|
+
* Update memory level
|
|
1341
|
+
*/
|
|
1342
|
+
async updateMemoryLevel(eventId, level) {
|
|
1343
|
+
await this.initialize();
|
|
1344
|
+
sqliteRun(
|
|
1345
|
+
this.db,
|
|
1346
|
+
`UPDATE memory_levels SET level = ?, promoted_at = datetime('now') WHERE event_id = ?`,
|
|
1347
|
+
[level, eventId]
|
|
1348
|
+
);
|
|
1349
|
+
}
|
|
1350
|
+
/**
|
|
1351
|
+
* Get memory level statistics
|
|
1352
|
+
*/
|
|
1353
|
+
async getLevelStats() {
|
|
1354
|
+
await this.initialize();
|
|
1355
|
+
const rows = sqliteAll(
|
|
1356
|
+
this.db,
|
|
1357
|
+
`SELECT level, COUNT(*) as count FROM memory_levels GROUP BY level`
|
|
1358
|
+
);
|
|
1359
|
+
return rows;
|
|
1360
|
+
}
|
|
1361
|
+
/**
|
|
1362
|
+
* Get events by memory level
|
|
1363
|
+
*/
|
|
1364
|
+
async getEventsByLevel(level, options) {
|
|
1365
|
+
await this.initialize();
|
|
1366
|
+
const limit = options?.limit || 50;
|
|
1367
|
+
const offset = options?.offset || 0;
|
|
1368
|
+
const rows = sqliteAll(
|
|
1369
|
+
this.db,
|
|
1370
|
+
`SELECT e.* FROM events e
|
|
1371
|
+
INNER JOIN memory_levels ml ON e.id = ml.event_id
|
|
1372
|
+
WHERE ml.level = ?
|
|
1373
|
+
ORDER BY e.timestamp DESC
|
|
1374
|
+
LIMIT ? OFFSET ?`,
|
|
1375
|
+
[level, limit, offset]
|
|
1376
|
+
);
|
|
1377
|
+
return rows.map((row) => this.rowToEvent(row));
|
|
1378
|
+
}
|
|
1379
|
+
/**
|
|
1380
|
+
* Get memory level for a specific event
|
|
1381
|
+
*/
|
|
1382
|
+
async getEventLevel(eventId) {
|
|
1383
|
+
await this.initialize();
|
|
1384
|
+
const row = sqliteGet(
|
|
1385
|
+
this.db,
|
|
1386
|
+
`SELECT level FROM memory_levels WHERE event_id = ?`,
|
|
1387
|
+
[eventId]
|
|
1388
|
+
);
|
|
1389
|
+
return row ? row.level : null;
|
|
1390
|
+
}
|
|
1391
|
+
/**
|
|
1392
|
+
* Get sync position for a target
|
|
1393
|
+
*/
|
|
1394
|
+
async getSyncPosition(targetName) {
|
|
1395
|
+
await this.initialize();
|
|
1396
|
+
const row = sqliteGet(
|
|
1397
|
+
this.db,
|
|
1398
|
+
`SELECT last_event_id, last_timestamp FROM sync_positions WHERE target_name = ?`,
|
|
1399
|
+
[targetName]
|
|
1400
|
+
);
|
|
1401
|
+
return {
|
|
1402
|
+
lastEventId: row?.last_event_id ?? null,
|
|
1403
|
+
lastTimestamp: row?.last_timestamp ?? null
|
|
1404
|
+
};
|
|
1405
|
+
}
|
|
1406
|
+
/**
|
|
1407
|
+
* Update sync position for a target
|
|
1408
|
+
*/
|
|
1409
|
+
async updateSyncPosition(targetName, lastEventId, lastTimestamp) {
|
|
1410
|
+
await this.initialize();
|
|
1411
|
+
sqliteRun(
|
|
1412
|
+
this.db,
|
|
1413
|
+
`INSERT OR REPLACE INTO sync_positions (target_name, last_event_id, last_timestamp, updated_at)
|
|
1414
|
+
VALUES (?, ?, ?, datetime('now'))`,
|
|
1415
|
+
[targetName, lastEventId, lastTimestamp]
|
|
1416
|
+
);
|
|
1417
|
+
}
|
|
1418
|
+
/**
|
|
1419
|
+
* Get config value for endless mode
|
|
1420
|
+
*/
|
|
1421
|
+
async getEndlessConfig(key) {
|
|
1422
|
+
await this.initialize();
|
|
1423
|
+
const row = sqliteGet(
|
|
1424
|
+
this.db,
|
|
1425
|
+
`SELECT value FROM endless_config WHERE key = ?`,
|
|
1426
|
+
[key]
|
|
1427
|
+
);
|
|
1428
|
+
if (!row)
|
|
1429
|
+
return null;
|
|
1430
|
+
return JSON.parse(row.value);
|
|
1431
|
+
}
|
|
1432
|
+
/**
|
|
1433
|
+
* Set config value for endless mode
|
|
1434
|
+
*/
|
|
1435
|
+
async setEndlessConfig(key, value) {
|
|
1436
|
+
await this.initialize();
|
|
1437
|
+
sqliteRun(
|
|
1438
|
+
this.db,
|
|
1439
|
+
`INSERT OR REPLACE INTO endless_config (key, value, updated_at)
|
|
1440
|
+
VALUES (?, ?, datetime('now'))`,
|
|
1441
|
+
[key, JSON.stringify(value)]
|
|
1442
|
+
);
|
|
1443
|
+
}
|
|
1444
|
+
/**
|
|
1445
|
+
* Increment access count for events
|
|
1446
|
+
*/
|
|
1447
|
+
async incrementAccessCount(eventIds) {
|
|
1448
|
+
if (eventIds.length === 0 || this.readOnly)
|
|
1449
|
+
return;
|
|
1450
|
+
await this.initialize();
|
|
1451
|
+
const placeholders = eventIds.map(() => "?").join(",");
|
|
1452
|
+
const currentTime = toSQLiteTimestamp(/* @__PURE__ */ new Date());
|
|
1453
|
+
sqliteRun(
|
|
1454
|
+
this.db,
|
|
1455
|
+
`UPDATE events
|
|
1456
|
+
SET access_count = access_count + 1,
|
|
1457
|
+
last_accessed_at = ?
|
|
1458
|
+
WHERE id IN (${placeholders})`,
|
|
1459
|
+
[currentTime, ...eventIds]
|
|
1460
|
+
);
|
|
1461
|
+
}
|
|
1462
|
+
/**
|
|
1463
|
+
* Get most accessed memories
|
|
1464
|
+
*/
|
|
1465
|
+
async getMostAccessed(limit = 10) {
|
|
1466
|
+
await this.initialize();
|
|
1467
|
+
const rows = sqliteAll(
|
|
1468
|
+
this.db,
|
|
1469
|
+
`SELECT * FROM events
|
|
1470
|
+
WHERE access_count > 0
|
|
1471
|
+
ORDER BY access_count DESC, last_accessed_at DESC
|
|
1472
|
+
LIMIT ?`,
|
|
1473
|
+
[limit]
|
|
1474
|
+
);
|
|
1475
|
+
return rows.map((row) => this.rowToEvent(row));
|
|
1476
|
+
}
|
|
1477
|
+
/**
|
|
1478
|
+
* Get database instance for direct access
|
|
1479
|
+
*/
|
|
1480
|
+
getDatabase() {
|
|
1481
|
+
return this.db;
|
|
1482
|
+
}
|
|
704
1483
|
/**
|
|
705
1484
|
* Close database connection
|
|
706
1485
|
*/
|
|
707
1486
|
async close() {
|
|
708
|
-
|
|
1487
|
+
sqliteClose(this.db);
|
|
709
1488
|
}
|
|
710
1489
|
/**
|
|
711
1490
|
* Convert database row to MemoryEvent
|
|
712
1491
|
*/
|
|
713
1492
|
rowToEvent(row) {
|
|
714
|
-
|
|
1493
|
+
const event = {
|
|
715
1494
|
id: row.id,
|
|
716
1495
|
eventType: row.event_type,
|
|
717
1496
|
sessionId: row.session_id,
|
|
718
|
-
timestamp:
|
|
1497
|
+
timestamp: toDateFromSQLite(row.timestamp),
|
|
719
1498
|
content: row.content,
|
|
720
1499
|
canonicalKey: row.canonical_key,
|
|
721
1500
|
dedupeKey: row.dedupe_key,
|
|
722
1501
|
metadata: row.metadata ? JSON.parse(row.metadata) : void 0
|
|
723
1502
|
};
|
|
1503
|
+
if (row.access_count !== void 0) {
|
|
1504
|
+
event.access_count = row.access_count;
|
|
1505
|
+
}
|
|
1506
|
+
if (row.last_accessed_at !== void 0) {
|
|
1507
|
+
event.last_accessed_at = row.last_accessed_at;
|
|
1508
|
+
}
|
|
1509
|
+
return event;
|
|
1510
|
+
}
|
|
1511
|
+
};
|
|
1512
|
+
|
|
1513
|
+
// src/core/sync-worker.ts
|
|
1514
|
+
var DEFAULT_CONFIG = {
|
|
1515
|
+
intervalMs: 3e4,
|
|
1516
|
+
batchSize: 500,
|
|
1517
|
+
maxRetries: 3,
|
|
1518
|
+
retryDelayMs: 5e3
|
|
1519
|
+
};
|
|
1520
|
+
var SyncWorker = class {
|
|
1521
|
+
constructor(sqliteStore, duckdbStore, config) {
|
|
1522
|
+
this.sqliteStore = sqliteStore;
|
|
1523
|
+
this.duckdbStore = duckdbStore;
|
|
1524
|
+
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
1525
|
+
}
|
|
1526
|
+
config;
|
|
1527
|
+
intervalHandle = null;
|
|
1528
|
+
running = false;
|
|
1529
|
+
stats = {
|
|
1530
|
+
lastSyncAt: null,
|
|
1531
|
+
eventsSynced: 0,
|
|
1532
|
+
sessionsSynced: 0,
|
|
1533
|
+
errors: 0,
|
|
1534
|
+
status: "idle"
|
|
1535
|
+
};
|
|
1536
|
+
/**
|
|
1537
|
+
* Start the sync worker
|
|
1538
|
+
*/
|
|
1539
|
+
start() {
|
|
1540
|
+
if (this.running)
|
|
1541
|
+
return;
|
|
1542
|
+
this.running = true;
|
|
1543
|
+
this.stats.status = "idle";
|
|
1544
|
+
this.syncNow().catch((err) => {
|
|
1545
|
+
console.error("[SyncWorker] Initial sync failed:", err);
|
|
1546
|
+
});
|
|
1547
|
+
this.intervalHandle = setInterval(() => {
|
|
1548
|
+
this.syncNow().catch((err) => {
|
|
1549
|
+
console.error("[SyncWorker] Periodic sync failed:", err);
|
|
1550
|
+
});
|
|
1551
|
+
}, this.config.intervalMs);
|
|
1552
|
+
}
|
|
1553
|
+
/**
|
|
1554
|
+
* Stop the sync worker
|
|
1555
|
+
*/
|
|
1556
|
+
stop() {
|
|
1557
|
+
this.running = false;
|
|
1558
|
+
this.stats.status = "stopped";
|
|
1559
|
+
if (this.intervalHandle) {
|
|
1560
|
+
clearInterval(this.intervalHandle);
|
|
1561
|
+
this.intervalHandle = null;
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
/**
|
|
1565
|
+
* Trigger immediate sync
|
|
1566
|
+
*/
|
|
1567
|
+
async syncNow() {
|
|
1568
|
+
if (this.stats.status === "syncing") {
|
|
1569
|
+
return;
|
|
1570
|
+
}
|
|
1571
|
+
this.stats.status = "syncing";
|
|
1572
|
+
try {
|
|
1573
|
+
await this.syncEvents();
|
|
1574
|
+
await this.syncSessions();
|
|
1575
|
+
this.stats.lastSyncAt = /* @__PURE__ */ new Date();
|
|
1576
|
+
this.stats.status = "idle";
|
|
1577
|
+
} catch (error) {
|
|
1578
|
+
this.stats.errors++;
|
|
1579
|
+
this.stats.status = "error";
|
|
1580
|
+
throw error;
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
/**
|
|
1584
|
+
* Sync events from SQLite to DuckDB
|
|
1585
|
+
*/
|
|
1586
|
+
async syncEvents() {
|
|
1587
|
+
const targetName = "duckdb_analytics";
|
|
1588
|
+
const position = await this.sqliteStore.getSyncPosition(targetName);
|
|
1589
|
+
const lastTimestamp = position.lastTimestamp || "1970-01-01T00:00:00.000Z";
|
|
1590
|
+
let hasMore = true;
|
|
1591
|
+
let totalSynced = 0;
|
|
1592
|
+
while (hasMore) {
|
|
1593
|
+
const events = await this.sqliteStore.getEventsSince(lastTimestamp, this.config.batchSize);
|
|
1594
|
+
if (events.length === 0) {
|
|
1595
|
+
hasMore = false;
|
|
1596
|
+
break;
|
|
1597
|
+
}
|
|
1598
|
+
await this.retryWithBackoff(async () => {
|
|
1599
|
+
for (const event of events) {
|
|
1600
|
+
await this.insertEventToDuckDB(event);
|
|
1601
|
+
}
|
|
1602
|
+
});
|
|
1603
|
+
totalSynced += events.length;
|
|
1604
|
+
const lastEvent = events[events.length - 1];
|
|
1605
|
+
await this.sqliteStore.updateSyncPosition(
|
|
1606
|
+
targetName,
|
|
1607
|
+
lastEvent.id,
|
|
1608
|
+
lastEvent.timestamp.toISOString()
|
|
1609
|
+
);
|
|
1610
|
+
hasMore = events.length === this.config.batchSize;
|
|
1611
|
+
}
|
|
1612
|
+
this.stats.eventsSynced += totalSynced;
|
|
1613
|
+
}
|
|
1614
|
+
/**
|
|
1615
|
+
* Sync sessions from SQLite to DuckDB
|
|
1616
|
+
*/
|
|
1617
|
+
async syncSessions() {
|
|
1618
|
+
const sessions = await this.sqliteStore.getAllSessions();
|
|
1619
|
+
for (const session of sessions) {
|
|
1620
|
+
await this.retryWithBackoff(async () => {
|
|
1621
|
+
await this.duckdbStore.upsertSession(session);
|
|
1622
|
+
});
|
|
1623
|
+
}
|
|
1624
|
+
this.stats.sessionsSynced = sessions.length;
|
|
1625
|
+
}
|
|
1626
|
+
/**
|
|
1627
|
+
* Insert a single event into DuckDB
|
|
1628
|
+
*/
|
|
1629
|
+
async insertEventToDuckDB(event) {
|
|
1630
|
+
await this.duckdbStore.append({
|
|
1631
|
+
eventType: event.eventType,
|
|
1632
|
+
sessionId: event.sessionId,
|
|
1633
|
+
timestamp: event.timestamp,
|
|
1634
|
+
content: event.content,
|
|
1635
|
+
metadata: event.metadata
|
|
1636
|
+
});
|
|
1637
|
+
}
|
|
1638
|
+
/**
|
|
1639
|
+
* Retry operation with exponential backoff
|
|
1640
|
+
*/
|
|
1641
|
+
async retryWithBackoff(fn) {
|
|
1642
|
+
let lastError = null;
|
|
1643
|
+
for (let attempt = 0; attempt < this.config.maxRetries; attempt++) {
|
|
1644
|
+
try {
|
|
1645
|
+
return await fn();
|
|
1646
|
+
} catch (error) {
|
|
1647
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
1648
|
+
if (attempt < this.config.maxRetries - 1) {
|
|
1649
|
+
const delay = this.config.retryDelayMs * Math.pow(2, attempt);
|
|
1650
|
+
await this.sleep(delay);
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
throw lastError;
|
|
1655
|
+
}
|
|
1656
|
+
/**
|
|
1657
|
+
* Sleep utility
|
|
1658
|
+
*/
|
|
1659
|
+
sleep(ms) {
|
|
1660
|
+
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
1661
|
+
}
|
|
1662
|
+
/**
|
|
1663
|
+
* Get sync statistics
|
|
1664
|
+
*/
|
|
1665
|
+
getStats() {
|
|
1666
|
+
return { ...this.stats };
|
|
1667
|
+
}
|
|
1668
|
+
/**
|
|
1669
|
+
* Check if worker is running
|
|
1670
|
+
*/
|
|
1671
|
+
isRunning() {
|
|
1672
|
+
return this.running;
|
|
724
1673
|
}
|
|
725
1674
|
};
|
|
726
1675
|
|
|
@@ -952,7 +1901,7 @@ function getDefaultEmbedder() {
|
|
|
952
1901
|
}
|
|
953
1902
|
|
|
954
1903
|
// src/core/vector-outbox.ts
|
|
955
|
-
var
|
|
1904
|
+
var DEFAULT_CONFIG2 = {
|
|
956
1905
|
embeddingVersion: "v1",
|
|
957
1906
|
maxRetries: 3,
|
|
958
1907
|
stuckThresholdMs: 5 * 60 * 1e3,
|
|
@@ -961,7 +1910,7 @@ var DEFAULT_CONFIG = {
|
|
|
961
1910
|
};
|
|
962
1911
|
|
|
963
1912
|
// src/core/vector-worker.ts
|
|
964
|
-
var
|
|
1913
|
+
var DEFAULT_CONFIG3 = {
|
|
965
1914
|
batchSize: 32,
|
|
966
1915
|
pollIntervalMs: 1e3,
|
|
967
1916
|
maxRetries: 3
|
|
@@ -972,12 +1921,13 @@ var VectorWorker = class {
|
|
|
972
1921
|
embedder;
|
|
973
1922
|
config;
|
|
974
1923
|
running = false;
|
|
1924
|
+
stopping = false;
|
|
975
1925
|
pollTimeout = null;
|
|
976
1926
|
constructor(eventStore, vectorStore, embedder, config = {}) {
|
|
977
1927
|
this.eventStore = eventStore;
|
|
978
1928
|
this.vectorStore = vectorStore;
|
|
979
1929
|
this.embedder = embedder;
|
|
980
|
-
this.config = { ...
|
|
1930
|
+
this.config = { ...DEFAULT_CONFIG3, ...config };
|
|
981
1931
|
}
|
|
982
1932
|
/**
|
|
983
1933
|
* Start the worker polling loop
|
|
@@ -986,6 +1936,7 @@ var VectorWorker = class {
|
|
|
986
1936
|
if (this.running)
|
|
987
1937
|
return;
|
|
988
1938
|
this.running = true;
|
|
1939
|
+
this.stopping = false;
|
|
989
1940
|
this.poll();
|
|
990
1941
|
}
|
|
991
1942
|
/**
|
|
@@ -993,6 +1944,7 @@ var VectorWorker = class {
|
|
|
993
1944
|
*/
|
|
994
1945
|
stop() {
|
|
995
1946
|
this.running = false;
|
|
1947
|
+
this.stopping = true;
|
|
996
1948
|
if (this.pollTimeout) {
|
|
997
1949
|
clearTimeout(this.pollTimeout);
|
|
998
1950
|
this.pollTimeout = null;
|
|
@@ -1042,9 +1994,15 @@ var VectorWorker = class {
|
|
|
1042
1994
|
}
|
|
1043
1995
|
return successful.length;
|
|
1044
1996
|
} catch (error) {
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1997
|
+
if (!this.stopping) {
|
|
1998
|
+
try {
|
|
1999
|
+
const allIds = items.map((i) => i.id);
|
|
2000
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
2001
|
+
await this.eventStore.failOutboxItems(allIds, errorMessage);
|
|
2002
|
+
} catch (failError) {
|
|
2003
|
+
console.warn("Could not mark outbox items as failed (database may be closed)");
|
|
2004
|
+
}
|
|
2005
|
+
}
|
|
1048
2006
|
throw error;
|
|
1049
2007
|
}
|
|
1050
2008
|
}
|
|
@@ -1052,14 +2010,18 @@ var VectorWorker = class {
|
|
|
1052
2010
|
* Poll for new items
|
|
1053
2011
|
*/
|
|
1054
2012
|
async poll() {
|
|
1055
|
-
if (!this.running)
|
|
2013
|
+
if (!this.running || this.stopping)
|
|
1056
2014
|
return;
|
|
1057
2015
|
try {
|
|
1058
2016
|
await this.processBatch();
|
|
1059
2017
|
} catch (error) {
|
|
1060
|
-
|
|
2018
|
+
if (!this.stopping) {
|
|
2019
|
+
console.error("Vector worker error:", error);
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
if (this.running && !this.stopping) {
|
|
2023
|
+
this.pollTimeout = setTimeout(() => this.poll(), this.config.pollIntervalMs);
|
|
1061
2024
|
}
|
|
1062
|
-
this.pollTimeout = setTimeout(() => this.poll(), this.config.pollIntervalMs);
|
|
1063
2025
|
}
|
|
1064
2026
|
/**
|
|
1065
2027
|
* Process all pending items (blocking)
|
|
@@ -1086,7 +2048,7 @@ function createVectorWorker(eventStore, vectorStore, embedder, config) {
|
|
|
1086
2048
|
}
|
|
1087
2049
|
|
|
1088
2050
|
// src/core/matcher.ts
|
|
1089
|
-
var
|
|
2051
|
+
var DEFAULT_CONFIG4 = {
|
|
1090
2052
|
weights: {
|
|
1091
2053
|
semanticSimilarity: 0.4,
|
|
1092
2054
|
ftsScore: 0.25,
|
|
@@ -1101,9 +2063,9 @@ var Matcher = class {
|
|
|
1101
2063
|
config;
|
|
1102
2064
|
constructor(config = {}) {
|
|
1103
2065
|
this.config = {
|
|
1104
|
-
...
|
|
2066
|
+
...DEFAULT_CONFIG4,
|
|
1105
2067
|
...config,
|
|
1106
|
-
weights: { ...
|
|
2068
|
+
weights: { ...DEFAULT_CONFIG4.weights, ...config.weights }
|
|
1107
2069
|
};
|
|
1108
2070
|
}
|
|
1109
2071
|
/**
|
|
@@ -1811,7 +2773,7 @@ function createSharedEventStore(dbPath) {
|
|
|
1811
2773
|
}
|
|
1812
2774
|
|
|
1813
2775
|
// src/core/shared-store.ts
|
|
1814
|
-
import { randomUUID as
|
|
2776
|
+
import { randomUUID as randomUUID3 } from "crypto";
|
|
1815
2777
|
var SharedStore = class {
|
|
1816
2778
|
constructor(sharedEventStore) {
|
|
1817
2779
|
this.sharedEventStore = sharedEventStore;
|
|
@@ -1823,7 +2785,7 @@ var SharedStore = class {
|
|
|
1823
2785
|
* Promote a verified troubleshooting entry to shared storage
|
|
1824
2786
|
*/
|
|
1825
2787
|
async promoteEntry(input) {
|
|
1826
|
-
const entryId =
|
|
2788
|
+
const entryId = randomUUID3();
|
|
1827
2789
|
await dbRun(
|
|
1828
2790
|
this.db,
|
|
1829
2791
|
`INSERT INTO shared_troubleshooting (
|
|
@@ -2188,7 +3150,7 @@ function createSharedVectorStore(dbPath) {
|
|
|
2188
3150
|
}
|
|
2189
3151
|
|
|
2190
3152
|
// src/core/shared-promoter.ts
|
|
2191
|
-
import { randomUUID as
|
|
3153
|
+
import { randomUUID as randomUUID4 } from "crypto";
|
|
2192
3154
|
var SharedPromoter = class {
|
|
2193
3155
|
constructor(sharedStore, sharedVectorStore, embedder, config) {
|
|
2194
3156
|
this.sharedStore = sharedStore;
|
|
@@ -2256,7 +3218,7 @@ var SharedPromoter = class {
|
|
|
2256
3218
|
const embeddingContent = this.createEmbeddingContent(input);
|
|
2257
3219
|
const embedding = await this.embedder.embed(embeddingContent);
|
|
2258
3220
|
await this.sharedVectorStore.upsert({
|
|
2259
|
-
id:
|
|
3221
|
+
id: randomUUID4(),
|
|
2260
3222
|
entryId,
|
|
2261
3223
|
entryType: "troubleshooting",
|
|
2262
3224
|
content: embeddingContent,
|
|
@@ -2396,7 +3358,7 @@ function createToolObservationEmbedding(toolName, metadata, success) {
|
|
|
2396
3358
|
}
|
|
2397
3359
|
|
|
2398
3360
|
// src/core/working-set-store.ts
|
|
2399
|
-
import { randomUUID as
|
|
3361
|
+
import { randomUUID as randomUUID5 } from "crypto";
|
|
2400
3362
|
var WorkingSetStore = class {
|
|
2401
3363
|
constructor(eventStore, config) {
|
|
2402
3364
|
this.eventStore = eventStore;
|
|
@@ -2417,7 +3379,7 @@ var WorkingSetStore = class {
|
|
|
2417
3379
|
`INSERT OR REPLACE INTO working_set (id, event_id, added_at, relevance_score, topics, expires_at)
|
|
2418
3380
|
VALUES (?, ?, CURRENT_TIMESTAMP, ?, ?, ?)`,
|
|
2419
3381
|
[
|
|
2420
|
-
|
|
3382
|
+
randomUUID5(),
|
|
2421
3383
|
eventId,
|
|
2422
3384
|
relevanceScore,
|
|
2423
3385
|
JSON.stringify(topics || []),
|
|
@@ -2601,7 +3563,7 @@ function createWorkingSetStore(eventStore, config) {
|
|
|
2601
3563
|
}
|
|
2602
3564
|
|
|
2603
3565
|
// src/core/consolidated-store.ts
|
|
2604
|
-
import { randomUUID as
|
|
3566
|
+
import { randomUUID as randomUUID6 } from "crypto";
|
|
2605
3567
|
var ConsolidatedStore = class {
|
|
2606
3568
|
constructor(eventStore) {
|
|
2607
3569
|
this.eventStore = eventStore;
|
|
@@ -2613,7 +3575,7 @@ var ConsolidatedStore = class {
|
|
|
2613
3575
|
* Create a new consolidated memory
|
|
2614
3576
|
*/
|
|
2615
3577
|
async create(input) {
|
|
2616
|
-
const memoryId =
|
|
3578
|
+
const memoryId = randomUUID6();
|
|
2617
3579
|
await dbRun(
|
|
2618
3580
|
this.db,
|
|
2619
3581
|
`INSERT INTO consolidated_memories
|
|
@@ -3140,7 +4102,7 @@ function createConsolidationWorker(workingSetStore, consolidatedStore, config) {
|
|
|
3140
4102
|
}
|
|
3141
4103
|
|
|
3142
4104
|
// src/core/continuity-manager.ts
|
|
3143
|
-
import { randomUUID as
|
|
4105
|
+
import { randomUUID as randomUUID7 } from "crypto";
|
|
3144
4106
|
var ContinuityManager = class {
|
|
3145
4107
|
constructor(eventStore, config) {
|
|
3146
4108
|
this.eventStore = eventStore;
|
|
@@ -3294,7 +4256,7 @@ var ContinuityManager = class {
|
|
|
3294
4256
|
`INSERT INTO continuity_log
|
|
3295
4257
|
(log_id, from_context_id, to_context_id, continuity_score, transition_type, created_at)
|
|
3296
4258
|
VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
|
|
3297
|
-
[
|
|
4259
|
+
[randomUUID7(), previous.id, current.id, score, type]
|
|
3298
4260
|
);
|
|
3299
4261
|
}
|
|
3300
4262
|
/**
|
|
@@ -3403,7 +4365,7 @@ function createContinuityManager(eventStore, config) {
|
|
|
3403
4365
|
}
|
|
3404
4366
|
|
|
3405
4367
|
// src/core/graduation-worker.ts
|
|
3406
|
-
var
|
|
4368
|
+
var DEFAULT_CONFIG5 = {
|
|
3407
4369
|
evaluationIntervalMs: 3e5,
|
|
3408
4370
|
// 5 minutes
|
|
3409
4371
|
batchSize: 50,
|
|
@@ -3411,7 +4373,7 @@ var DEFAULT_CONFIG4 = {
|
|
|
3411
4373
|
// 1 hour cooldown between evaluations
|
|
3412
4374
|
};
|
|
3413
4375
|
var GraduationWorker = class {
|
|
3414
|
-
constructor(eventStore, graduation, config =
|
|
4376
|
+
constructor(eventStore, graduation, config = DEFAULT_CONFIG5) {
|
|
3415
4377
|
this.eventStore = eventStore;
|
|
3416
4378
|
this.graduation = graduation;
|
|
3417
4379
|
this.config = config;
|
|
@@ -3519,7 +4481,7 @@ function createGraduationWorker(eventStore, graduation, config) {
|
|
|
3519
4481
|
return new GraduationWorker(
|
|
3520
4482
|
eventStore,
|
|
3521
4483
|
graduation,
|
|
3522
|
-
{ ...
|
|
4484
|
+
{ ...DEFAULT_CONFIG5, ...config }
|
|
3523
4485
|
);
|
|
3524
4486
|
}
|
|
3525
4487
|
|
|
@@ -3579,7 +4541,11 @@ function registerSession(sessionId, projectPath) {
|
|
|
3579
4541
|
saveSessionRegistry(registry);
|
|
3580
4542
|
}
|
|
3581
4543
|
var MemoryService = class {
|
|
3582
|
-
|
|
4544
|
+
// Primary store: SQLite (WAL mode) - for hooks, always available
|
|
4545
|
+
sqliteStore;
|
|
4546
|
+
// Analytics store: DuckDB - for server reads (optional, synced from SQLite)
|
|
4547
|
+
analyticsStore;
|
|
4548
|
+
syncWorker = null;
|
|
3583
4549
|
vectorStore;
|
|
3584
4550
|
embedder;
|
|
3585
4551
|
matcher;
|
|
@@ -3610,17 +4576,39 @@ var MemoryService = class {
|
|
|
3610
4576
|
}
|
|
3611
4577
|
this.projectHash = config.projectHash || null;
|
|
3612
4578
|
this.sharedStoreConfig = config.sharedStoreConfig ?? { enabled: true };
|
|
3613
|
-
this.
|
|
4579
|
+
this.sqliteStore = new SQLiteEventStore(
|
|
4580
|
+
path.join(storagePath, "events.sqlite"),
|
|
4581
|
+
{ readonly: this.readOnly }
|
|
4582
|
+
);
|
|
4583
|
+
const analyticsEnabled = config.analyticsEnabled ?? this.readOnly;
|
|
4584
|
+
if (!analyticsEnabled) {
|
|
4585
|
+
this.analyticsStore = null;
|
|
4586
|
+
} else if (this.readOnly) {
|
|
4587
|
+
try {
|
|
4588
|
+
this.analyticsStore = new EventStore(
|
|
4589
|
+
path.join(storagePath, "analytics.duckdb"),
|
|
4590
|
+
{ readOnly: true }
|
|
4591
|
+
);
|
|
4592
|
+
} catch {
|
|
4593
|
+
this.analyticsStore = null;
|
|
4594
|
+
}
|
|
4595
|
+
} else {
|
|
4596
|
+
this.analyticsStore = new EventStore(
|
|
4597
|
+
path.join(storagePath, "analytics.duckdb"),
|
|
4598
|
+
{ readOnly: false }
|
|
4599
|
+
);
|
|
4600
|
+
}
|
|
3614
4601
|
this.vectorStore = new VectorStore(path.join(storagePath, "vectors"));
|
|
3615
4602
|
this.embedder = config.embeddingModel ? new Embedder(config.embeddingModel) : getDefaultEmbedder();
|
|
3616
4603
|
this.matcher = getDefaultMatcher();
|
|
3617
4604
|
this.retriever = createRetriever(
|
|
3618
|
-
this.
|
|
4605
|
+
this.sqliteStore,
|
|
4606
|
+
// Interface compatible
|
|
3619
4607
|
this.vectorStore,
|
|
3620
4608
|
this.embedder,
|
|
3621
4609
|
this.matcher
|
|
3622
4610
|
);
|
|
3623
|
-
this.graduation = createGraduationPipeline(this.
|
|
4611
|
+
this.graduation = createGraduationPipeline(this.sqliteStore);
|
|
3624
4612
|
}
|
|
3625
4613
|
/**
|
|
3626
4614
|
* Initialize all components
|
|
@@ -3628,23 +4616,38 @@ var MemoryService = class {
|
|
|
3628
4616
|
async initialize() {
|
|
3629
4617
|
if (this.initialized)
|
|
3630
4618
|
return;
|
|
3631
|
-
await this.
|
|
4619
|
+
await this.sqliteStore.initialize();
|
|
4620
|
+
if (this.analyticsStore) {
|
|
4621
|
+
try {
|
|
4622
|
+
await this.analyticsStore.initialize();
|
|
4623
|
+
} catch (error) {
|
|
4624
|
+
console.warn("[MemoryService] Analytics store (DuckDB) initialization failed, using SQLite for reads:", error);
|
|
4625
|
+
}
|
|
4626
|
+
}
|
|
3632
4627
|
await this.vectorStore.initialize();
|
|
3633
4628
|
await this.embedder.initialize();
|
|
3634
4629
|
if (!this.readOnly) {
|
|
3635
4630
|
this.vectorWorker = createVectorWorker(
|
|
3636
|
-
this.
|
|
4631
|
+
this.sqliteStore,
|
|
3637
4632
|
this.vectorStore,
|
|
3638
4633
|
this.embedder
|
|
3639
4634
|
);
|
|
3640
4635
|
this.vectorWorker.start();
|
|
3641
4636
|
this.retriever.setGraduationPipeline(this.graduation);
|
|
3642
4637
|
this.graduationWorker = createGraduationWorker(
|
|
3643
|
-
this.
|
|
4638
|
+
this.sqliteStore,
|
|
3644
4639
|
this.graduation
|
|
3645
4640
|
);
|
|
3646
4641
|
this.graduationWorker.start();
|
|
3647
|
-
|
|
4642
|
+
if (this.analyticsStore) {
|
|
4643
|
+
this.syncWorker = new SyncWorker(
|
|
4644
|
+
this.sqliteStore,
|
|
4645
|
+
this.analyticsStore,
|
|
4646
|
+
{ intervalMs: 3e4, batchSize: 500 }
|
|
4647
|
+
);
|
|
4648
|
+
this.syncWorker.start();
|
|
4649
|
+
}
|
|
4650
|
+
const savedMode = await this.sqliteStore.getEndlessConfig("mode");
|
|
3648
4651
|
if (savedMode === "endless") {
|
|
3649
4652
|
this.endlessMode = "endless";
|
|
3650
4653
|
await this.initializeEndlessMode();
|
|
@@ -3685,7 +4688,7 @@ var MemoryService = class {
|
|
|
3685
4688
|
*/
|
|
3686
4689
|
async startSession(sessionId, projectPath) {
|
|
3687
4690
|
await this.initialize();
|
|
3688
|
-
await this.
|
|
4691
|
+
await this.sqliteStore.upsertSession({
|
|
3689
4692
|
id: sessionId,
|
|
3690
4693
|
startedAt: /* @__PURE__ */ new Date(),
|
|
3691
4694
|
projectPath
|
|
@@ -3696,7 +4699,7 @@ var MemoryService = class {
|
|
|
3696
4699
|
*/
|
|
3697
4700
|
async endSession(sessionId, summary) {
|
|
3698
4701
|
await this.initialize();
|
|
3699
|
-
await this.
|
|
4702
|
+
await this.sqliteStore.upsertSession({
|
|
3700
4703
|
id: sessionId,
|
|
3701
4704
|
endedAt: /* @__PURE__ */ new Date(),
|
|
3702
4705
|
summary
|
|
@@ -3707,7 +4710,7 @@ var MemoryService = class {
|
|
|
3707
4710
|
*/
|
|
3708
4711
|
async storeUserPrompt(sessionId, content, metadata) {
|
|
3709
4712
|
await this.initialize();
|
|
3710
|
-
const result = await this.
|
|
4713
|
+
const result = await this.sqliteStore.append({
|
|
3711
4714
|
eventType: "user_prompt",
|
|
3712
4715
|
sessionId,
|
|
3713
4716
|
timestamp: /* @__PURE__ */ new Date(),
|
|
@@ -3715,7 +4718,7 @@ var MemoryService = class {
|
|
|
3715
4718
|
metadata
|
|
3716
4719
|
});
|
|
3717
4720
|
if (result.success && !result.isDuplicate) {
|
|
3718
|
-
await this.
|
|
4721
|
+
await this.sqliteStore.enqueueForEmbedding(result.eventId, content);
|
|
3719
4722
|
}
|
|
3720
4723
|
return result;
|
|
3721
4724
|
}
|
|
@@ -3724,7 +4727,7 @@ var MemoryService = class {
|
|
|
3724
4727
|
*/
|
|
3725
4728
|
async storeAgentResponse(sessionId, content, metadata) {
|
|
3726
4729
|
await this.initialize();
|
|
3727
|
-
const result = await this.
|
|
4730
|
+
const result = await this.sqliteStore.append({
|
|
3728
4731
|
eventType: "agent_response",
|
|
3729
4732
|
sessionId,
|
|
3730
4733
|
timestamp: /* @__PURE__ */ new Date(),
|
|
@@ -3732,7 +4735,7 @@ var MemoryService = class {
|
|
|
3732
4735
|
metadata
|
|
3733
4736
|
});
|
|
3734
4737
|
if (result.success && !result.isDuplicate) {
|
|
3735
|
-
await this.
|
|
4738
|
+
await this.sqliteStore.enqueueForEmbedding(result.eventId, content);
|
|
3736
4739
|
}
|
|
3737
4740
|
return result;
|
|
3738
4741
|
}
|
|
@@ -3741,14 +4744,14 @@ var MemoryService = class {
|
|
|
3741
4744
|
*/
|
|
3742
4745
|
async storeSessionSummary(sessionId, summary) {
|
|
3743
4746
|
await this.initialize();
|
|
3744
|
-
const result = await this.
|
|
4747
|
+
const result = await this.sqliteStore.append({
|
|
3745
4748
|
eventType: "session_summary",
|
|
3746
4749
|
sessionId,
|
|
3747
4750
|
timestamp: /* @__PURE__ */ new Date(),
|
|
3748
4751
|
content: summary
|
|
3749
4752
|
});
|
|
3750
4753
|
if (result.success && !result.isDuplicate) {
|
|
3751
|
-
await this.
|
|
4754
|
+
await this.sqliteStore.enqueueForEmbedding(result.eventId, summary);
|
|
3752
4755
|
}
|
|
3753
4756
|
return result;
|
|
3754
4757
|
}
|
|
@@ -3758,7 +4761,7 @@ var MemoryService = class {
|
|
|
3758
4761
|
async storeToolObservation(sessionId, payload) {
|
|
3759
4762
|
await this.initialize();
|
|
3760
4763
|
const content = JSON.stringify(payload);
|
|
3761
|
-
const result = await this.
|
|
4764
|
+
const result = await this.sqliteStore.append({
|
|
3762
4765
|
eventType: "tool_observation",
|
|
3763
4766
|
sessionId,
|
|
3764
4767
|
timestamp: /* @__PURE__ */ new Date(),
|
|
@@ -3774,7 +4777,7 @@ var MemoryService = class {
|
|
|
3774
4777
|
payload.metadata || {},
|
|
3775
4778
|
payload.success
|
|
3776
4779
|
);
|
|
3777
|
-
await this.
|
|
4780
|
+
await this.sqliteStore.enqueueForEmbedding(result.eventId, embeddingContent);
|
|
3778
4781
|
}
|
|
3779
4782
|
return result;
|
|
3780
4783
|
}
|
|
@@ -3800,21 +4803,21 @@ var MemoryService = class {
|
|
|
3800
4803
|
*/
|
|
3801
4804
|
async getSessionHistory(sessionId) {
|
|
3802
4805
|
await this.initialize();
|
|
3803
|
-
return this.
|
|
4806
|
+
return this.sqliteStore.getSessionEvents(sessionId);
|
|
3804
4807
|
}
|
|
3805
4808
|
/**
|
|
3806
4809
|
* Get recent events
|
|
3807
4810
|
*/
|
|
3808
4811
|
async getRecentEvents(limit = 100) {
|
|
3809
4812
|
await this.initialize();
|
|
3810
|
-
return this.
|
|
4813
|
+
return this.sqliteStore.getRecentEvents(limit);
|
|
3811
4814
|
}
|
|
3812
4815
|
/**
|
|
3813
4816
|
* Get memory statistics
|
|
3814
4817
|
*/
|
|
3815
4818
|
async getStats() {
|
|
3816
4819
|
await this.initialize();
|
|
3817
|
-
const recentEvents = await this.
|
|
4820
|
+
const recentEvents = await this.sqliteStore.getRecentEvents(1e4);
|
|
3818
4821
|
const vectorCount = await this.vectorStore.count();
|
|
3819
4822
|
const levelStats = await this.graduation.getStats();
|
|
3820
4823
|
return {
|
|
@@ -3837,14 +4840,14 @@ var MemoryService = class {
|
|
|
3837
4840
|
*/
|
|
3838
4841
|
async getEventsByLevel(level, options) {
|
|
3839
4842
|
await this.initialize();
|
|
3840
|
-
return this.
|
|
4843
|
+
return this.sqliteStore.getEventsByLevel(level, options);
|
|
3841
4844
|
}
|
|
3842
4845
|
/**
|
|
3843
4846
|
* Get memory level for a specific event
|
|
3844
4847
|
*/
|
|
3845
4848
|
async getEventLevel(eventId) {
|
|
3846
4849
|
await this.initialize();
|
|
3847
|
-
return this.
|
|
4850
|
+
return this.sqliteStore.getEventLevel(eventId);
|
|
3848
4851
|
}
|
|
3849
4852
|
/**
|
|
3850
4853
|
* Format retrieval results as context for Claude
|
|
@@ -3938,21 +4941,21 @@ var MemoryService = class {
|
|
|
3938
4941
|
*/
|
|
3939
4942
|
async initializeEndlessMode() {
|
|
3940
4943
|
const config = await this.getEndlessConfig();
|
|
3941
|
-
this.workingSetStore = createWorkingSetStore(this.
|
|
3942
|
-
this.consolidatedStore = createConsolidatedStore(this.
|
|
4944
|
+
this.workingSetStore = createWorkingSetStore(this.sqliteStore, config);
|
|
4945
|
+
this.consolidatedStore = createConsolidatedStore(this.sqliteStore);
|
|
3943
4946
|
this.consolidationWorker = createConsolidationWorker(
|
|
3944
4947
|
this.workingSetStore,
|
|
3945
4948
|
this.consolidatedStore,
|
|
3946
4949
|
config
|
|
3947
4950
|
);
|
|
3948
|
-
this.continuityManager = createContinuityManager(this.
|
|
4951
|
+
this.continuityManager = createContinuityManager(this.sqliteStore, config);
|
|
3949
4952
|
this.consolidationWorker.start();
|
|
3950
4953
|
}
|
|
3951
4954
|
/**
|
|
3952
4955
|
* Get Endless Mode configuration
|
|
3953
4956
|
*/
|
|
3954
4957
|
async getEndlessConfig() {
|
|
3955
|
-
const savedConfig = await this.
|
|
4958
|
+
const savedConfig = await this.sqliteStore.getEndlessConfig("config");
|
|
3956
4959
|
return savedConfig || this.getDefaultEndlessConfig();
|
|
3957
4960
|
}
|
|
3958
4961
|
/**
|
|
@@ -3961,7 +4964,7 @@ var MemoryService = class {
|
|
|
3961
4964
|
async setEndlessConfig(config) {
|
|
3962
4965
|
const current = await this.getEndlessConfig();
|
|
3963
4966
|
const merged = { ...current, ...config };
|
|
3964
|
-
await this.
|
|
4967
|
+
await this.sqliteStore.setEndlessConfig("config", merged);
|
|
3965
4968
|
}
|
|
3966
4969
|
/**
|
|
3967
4970
|
* Set memory mode (session or endless)
|
|
@@ -3971,7 +4974,7 @@ var MemoryService = class {
|
|
|
3971
4974
|
if (mode === this.endlessMode)
|
|
3972
4975
|
return;
|
|
3973
4976
|
this.endlessMode = mode;
|
|
3974
|
-
await this.
|
|
4977
|
+
await this.sqliteStore.setEndlessConfig("mode", mode);
|
|
3975
4978
|
if (mode === "endless") {
|
|
3976
4979
|
await this.initializeEndlessMode();
|
|
3977
4980
|
} else {
|
|
@@ -4029,12 +5032,49 @@ var MemoryService = class {
|
|
|
4029
5032
|
return this.consolidatedStore.getAll({ limit });
|
|
4030
5033
|
}
|
|
4031
5034
|
/**
|
|
4032
|
-
*
|
|
5035
|
+
* Increment access count for memories that were used in prompts
|
|
5036
|
+
*/
|
|
5037
|
+
async incrementMemoryAccess(eventIds) {
|
|
5038
|
+
if (eventIds.length === 0)
|
|
5039
|
+
return;
|
|
5040
|
+
if (this.sqliteStore) {
|
|
5041
|
+
await this.sqliteStore.incrementAccessCount(eventIds);
|
|
5042
|
+
} else if (this.eventStore) {
|
|
5043
|
+
await this.eventStore.incrementAccessCount(eventIds);
|
|
5044
|
+
}
|
|
5045
|
+
}
|
|
5046
|
+
/**
|
|
5047
|
+
* Get most accessed memories from events
|
|
4033
5048
|
*/
|
|
4034
5049
|
async getMostAccessedMemories(limit = 10) {
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
5050
|
+
console.log("[getMostAccessedMemories] sqliteStore available:", !!this.sqliteStore);
|
|
5051
|
+
if (this.sqliteStore) {
|
|
5052
|
+
const events = await this.sqliteStore.getMostAccessed(limit);
|
|
5053
|
+
console.log("[getMostAccessedMemories] Got events from SQLite:", events.length);
|
|
5054
|
+
return events.map((event) => ({
|
|
5055
|
+
memoryId: event.id,
|
|
5056
|
+
summary: event.content.substring(0, 200) + (event.content.length > 200 ? "..." : ""),
|
|
5057
|
+
topics: [],
|
|
5058
|
+
// Could extract topics from content if needed
|
|
5059
|
+
accessCount: event.access_count || 0,
|
|
5060
|
+
lastAccessed: event.last_accessed_at || null,
|
|
5061
|
+
confidence: 1,
|
|
5062
|
+
createdAt: event.timestamp
|
|
5063
|
+
}));
|
|
5064
|
+
}
|
|
5065
|
+
if (this.consolidatedStore) {
|
|
5066
|
+
const consolidated = await this.consolidatedStore.getMostAccessed(limit);
|
|
5067
|
+
return consolidated.map((m) => ({
|
|
5068
|
+
memoryId: m.memoryId,
|
|
5069
|
+
summary: m.summary,
|
|
5070
|
+
topics: m.topics,
|
|
5071
|
+
accessCount: m.accessCount,
|
|
5072
|
+
lastAccessed: m.accessedAt,
|
|
5073
|
+
confidence: m.confidence,
|
|
5074
|
+
createdAt: m.createdAt
|
|
5075
|
+
}));
|
|
5076
|
+
}
|
|
5077
|
+
return [];
|
|
4038
5078
|
}
|
|
4039
5079
|
/**
|
|
4040
5080
|
* Mark a consolidated memory as accessed
|
|
@@ -4159,10 +5199,16 @@ var MemoryService = class {
|
|
|
4159
5199
|
if (this.vectorWorker) {
|
|
4160
5200
|
this.vectorWorker.stop();
|
|
4161
5201
|
}
|
|
5202
|
+
if (this.syncWorker) {
|
|
5203
|
+
this.syncWorker.stop();
|
|
5204
|
+
}
|
|
4162
5205
|
if (this.sharedEventStore) {
|
|
4163
5206
|
await this.sharedEventStore.close();
|
|
4164
5207
|
}
|
|
4165
|
-
await this.
|
|
5208
|
+
await this.sqliteStore.close();
|
|
5209
|
+
if (this.analyticsStore) {
|
|
5210
|
+
await this.analyticsStore.close();
|
|
5211
|
+
}
|
|
4166
5212
|
}
|
|
4167
5213
|
/**
|
|
4168
5214
|
* Expand ~ to home directory
|
|
@@ -4182,7 +5228,10 @@ function getMemoryServiceForProject(projectPath, sharedStoreConfig) {
|
|
|
4182
5228
|
serviceCache.set(hash, new MemoryService({
|
|
4183
5229
|
storagePath,
|
|
4184
5230
|
projectHash: hash,
|
|
4185
|
-
|
|
5231
|
+
// Override shared store config - hooks don't need DuckDB
|
|
5232
|
+
sharedStoreConfig: sharedStoreConfig ?? { enabled: false },
|
|
5233
|
+
analyticsEnabled: false
|
|
5234
|
+
// Hooks don't need DuckDB
|
|
4186
5235
|
}));
|
|
4187
5236
|
}
|
|
4188
5237
|
return serviceCache.get(hash);
|