claude-memory-layer 1.0.10 → 1.0.11
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/dist/cli/index.js +1266 -181
- package/dist/cli/index.js.map +4 -4
- package/dist/core/index.js +367 -7
- package/dist/core/index.js.map +2 -2
- package/dist/hooks/post-tool-use.js +598 -40
- package/dist/hooks/post-tool-use.js.map +4 -4
- package/dist/hooks/session-end.js +486 -49
- package/dist/hooks/session-end.js.map +3 -3
- package/dist/hooks/session-start.js +474 -22
- package/dist/hooks/session-start.js.map +3 -3
- package/dist/hooks/stop.js +586 -70
- package/dist/hooks/stop.js.map +4 -4
- package/dist/hooks/user-prompt-submit.js +537 -27
- package/dist/hooks/user-prompt-submit.js.map +4 -4
- package/dist/server/api/index.js +938 -39
- package/dist/server/api/index.js.map +4 -4
- package/dist/server/index.js +947 -48
- package/dist/server/index.js.map +4 -4
- package/dist/services/memory-service.js +475 -22
- package/dist/services/memory-service.js.map +3 -3
- package/dist/ui/app.js +1380 -55
- package/dist/ui/index.html +311 -148
- package/dist/ui/style.css +892 -0
- package/docs/OPERATIONS.md +18 -0
- package/package.json +8 -2
- package/scripts/fix-sync-gap.js +32 -0
- package/scripts/heartbeat-memory-orchestrator.sh +28 -0
- package/scripts/report-sync-gap.js +26 -0
- package/scripts/review-queue-auto-resolve.js +21 -0
- package/scripts/sync-gap-auto-heal.sh +17 -0
- package/specs/20260207-dashboard-upgrade/context.md +38 -0
- package/specs/20260207-dashboard-upgrade/spec.md +96 -0
- package/src/cli/index.ts +110 -58
- package/src/core/sqlite-event-store.ts +444 -6
- package/src/core/sqlite-wrapper.ts +8 -0
- package/src/core/turn-state.ts +159 -0
- package/src/core/types.ts +23 -8
- package/src/core/vector-store.ts +21 -3
- package/src/hooks/post-tool-use.ts +68 -23
- package/src/hooks/session-end.ts +8 -3
- package/src/hooks/stop.ts +96 -25
- package/src/hooks/user-prompt-submit.ts +44 -5
- package/src/server/api/chat.ts +244 -0
- package/src/server/api/citations.ts +3 -3
- package/src/server/api/events.ts +30 -5
- package/src/server/api/index.ts +7 -1
- package/src/server/api/projects.ts +74 -0
- package/src/server/api/search.ts +3 -3
- package/src/server/api/sessions.ts +3 -3
- package/src/server/api/stats.ts +43 -7
- package/src/server/api/turns.ts +143 -0
- package/src/server/api/utils.ts +46 -0
- package/src/services/memory-service.ts +137 -5
- package/src/services/session-history-importer.ts +215 -51
- package/src/ui/app.js +1380 -55
- package/src/ui/index.html +311 -148
- package/src/ui/style.css +892 -0
- package/.claude/settings.local.json +0 -27
- package/.claude-memory/test.sqlite +0 -0
- package/.history/package_20260201112328.json +0 -45
- package/.history/package_20260201113602.json +0 -45
- package/.history/package_20260201113713.json +0 -45
- package/.history/package_20260201114110.json +0 -45
- package/.history/package_20260201114632.json +0 -46
- package/.history/package_20260201133143.json +0 -45
- package/.history/package_20260201134319.json +0 -45
- package/.history/package_20260201134326.json +0 -45
- package/.history/package_20260201134334.json +0 -45
- package/.history/package_20260201134912.json +0 -45
- package/.history/package_20260201142928.json +0 -46
- package/.history/package_20260201192048.json +0 -47
- package/.history/package_20260202114053.json +0 -49
- package/.history/package_20260202121115.json +0 -49
- package/test_access.js +0 -49
package/dist/hooks/stop.js
CHANGED
|
@@ -6,10 +6,14 @@ const require = createRequire(import.meta.url);
|
|
|
6
6
|
const __filename = fileURLToPath(import.meta.url);
|
|
7
7
|
const __dirname = dirname(__filename);
|
|
8
8
|
|
|
9
|
+
// src/hooks/stop.ts
|
|
10
|
+
import * as fs4 from "fs";
|
|
11
|
+
import * as readline from "readline";
|
|
12
|
+
|
|
9
13
|
// src/services/memory-service.ts
|
|
10
14
|
import * as path from "path";
|
|
11
15
|
import * as os from "os";
|
|
12
|
-
import * as
|
|
16
|
+
import * as fs2 from "fs";
|
|
13
17
|
import * as crypto2 from "crypto";
|
|
14
18
|
|
|
15
19
|
// src/core/event-store.ts
|
|
@@ -67,11 +71,11 @@ function toDate(value) {
|
|
|
67
71
|
return new Date(value);
|
|
68
72
|
return new Date(String(value));
|
|
69
73
|
}
|
|
70
|
-
function createDatabase(
|
|
74
|
+
function createDatabase(path3, options) {
|
|
71
75
|
if (options?.readOnly) {
|
|
72
|
-
return new duckdb.Database(
|
|
76
|
+
return new duckdb.Database(path3, { access_mode: "READ_ONLY" });
|
|
73
77
|
}
|
|
74
|
-
return new duckdb.Database(
|
|
78
|
+
return new duckdb.Database(path3);
|
|
75
79
|
}
|
|
76
80
|
function dbRun(db, sql, params = []) {
|
|
77
81
|
return new Promise((resolve2, reject) => {
|
|
@@ -741,8 +745,14 @@ import { randomUUID as randomUUID2 } from "crypto";
|
|
|
741
745
|
|
|
742
746
|
// src/core/sqlite-wrapper.ts
|
|
743
747
|
import Database from "better-sqlite3";
|
|
744
|
-
|
|
745
|
-
|
|
748
|
+
import * as fs from "fs";
|
|
749
|
+
import * as nodePath from "path";
|
|
750
|
+
function createSQLiteDatabase(path3, options) {
|
|
751
|
+
const dir = nodePath.dirname(path3);
|
|
752
|
+
if (!fs.existsSync(dir)) {
|
|
753
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
754
|
+
}
|
|
755
|
+
const db = new Database(path3, {
|
|
746
756
|
readonly: options?.readonly ?? false
|
|
747
757
|
});
|
|
748
758
|
if (!options?.readonly && (options?.walMode ?? true)) {
|
|
@@ -1009,6 +1019,23 @@ var SQLiteEventStore = class {
|
|
|
1009
1019
|
updated_at TEXT DEFAULT (datetime('now'))
|
|
1010
1020
|
);
|
|
1011
1021
|
|
|
1022
|
+
-- Memory Helpfulness tracking
|
|
1023
|
+
CREATE TABLE IF NOT EXISTS memory_helpfulness (
|
|
1024
|
+
id TEXT PRIMARY KEY,
|
|
1025
|
+
event_id TEXT NOT NULL,
|
|
1026
|
+
session_id TEXT NOT NULL,
|
|
1027
|
+
retrieval_score REAL DEFAULT 0,
|
|
1028
|
+
query_preview TEXT,
|
|
1029
|
+
session_continued INTEGER DEFAULT 0,
|
|
1030
|
+
prompt_count_after INTEGER DEFAULT 0,
|
|
1031
|
+
tool_success_count INTEGER DEFAULT 0,
|
|
1032
|
+
tool_total_count INTEGER DEFAULT 0,
|
|
1033
|
+
was_reasked INTEGER DEFAULT 0,
|
|
1034
|
+
helpfulness_score REAL DEFAULT 0.5,
|
|
1035
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
1036
|
+
measured_at TEXT
|
|
1037
|
+
);
|
|
1038
|
+
|
|
1012
1039
|
-- Sync position tracking (for SQLite -> DuckDB sync)
|
|
1013
1040
|
CREATE TABLE IF NOT EXISTS sync_positions (
|
|
1014
1041
|
target_name TEXT PRIMARY KEY,
|
|
@@ -1034,6 +1061,9 @@ var SQLiteEventStore = class {
|
|
|
1034
1061
|
CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence);
|
|
1035
1062
|
CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at);
|
|
1036
1063
|
CREATE INDEX IF NOT EXISTS idx_embedding_outbox_status ON embedding_outbox(status);
|
|
1064
|
+
CREATE INDEX IF NOT EXISTS idx_helpfulness_event ON memory_helpfulness(event_id);
|
|
1065
|
+
CREATE INDEX IF NOT EXISTS idx_helpfulness_session ON memory_helpfulness(session_id);
|
|
1066
|
+
CREATE INDEX IF NOT EXISTS idx_helpfulness_score ON memory_helpfulness(helpfulness_score DESC);
|
|
1037
1067
|
|
|
1038
1068
|
-- FTS5 Full-Text Search for fast keyword search
|
|
1039
1069
|
CREATE VIRTUAL TABLE IF NOT EXISTS events_fts USING fts5(
|
|
@@ -1077,6 +1107,15 @@ var SQLiteEventStore = class {
|
|
|
1077
1107
|
console.error("Error adding last_accessed_at column:", err);
|
|
1078
1108
|
}
|
|
1079
1109
|
}
|
|
1110
|
+
if (!columnNames.includes("turn_id")) {
|
|
1111
|
+
try {
|
|
1112
|
+
sqliteExec(this.db, `
|
|
1113
|
+
ALTER TABLE events ADD COLUMN turn_id TEXT;
|
|
1114
|
+
`);
|
|
1115
|
+
} catch (err) {
|
|
1116
|
+
console.error("Error adding turn_id column:", err);
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1080
1119
|
try {
|
|
1081
1120
|
sqliteExec(this.db, `
|
|
1082
1121
|
CREATE INDEX IF NOT EXISTS idx_events_access_count ON events(access_count DESC);
|
|
@@ -1089,6 +1128,12 @@ var SQLiteEventStore = class {
|
|
|
1089
1128
|
`);
|
|
1090
1129
|
} catch (err) {
|
|
1091
1130
|
}
|
|
1131
|
+
try {
|
|
1132
|
+
sqliteExec(this.db, `
|
|
1133
|
+
CREATE INDEX IF NOT EXISTS idx_events_turn_id ON events(turn_id);
|
|
1134
|
+
`);
|
|
1135
|
+
} catch (err) {
|
|
1136
|
+
}
|
|
1092
1137
|
this.initialized = true;
|
|
1093
1138
|
}
|
|
1094
1139
|
/**
|
|
@@ -1113,9 +1158,11 @@ var SQLiteEventStore = class {
|
|
|
1113
1158
|
const id = randomUUID2();
|
|
1114
1159
|
const timestamp = toSQLiteTimestamp(input.timestamp);
|
|
1115
1160
|
try {
|
|
1161
|
+
const metadata = input.metadata || {};
|
|
1162
|
+
const turnId = metadata.turnId || null;
|
|
1116
1163
|
const insertEvent = this.db.prepare(`
|
|
1117
|
-
INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata)
|
|
1118
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
1164
|
+
INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata, turn_id)
|
|
1165
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1119
1166
|
`);
|
|
1120
1167
|
const insertDedup = this.db.prepare(`
|
|
1121
1168
|
INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)
|
|
@@ -1132,7 +1179,8 @@ var SQLiteEventStore = class {
|
|
|
1132
1179
|
input.content,
|
|
1133
1180
|
canonicalKey,
|
|
1134
1181
|
dedupeKey,
|
|
1135
|
-
JSON.stringify(
|
|
1182
|
+
JSON.stringify(metadata),
|
|
1183
|
+
turnId
|
|
1136
1184
|
);
|
|
1137
1185
|
insertDedup.run(dedupeKey, id);
|
|
1138
1186
|
insertLevel.run(id);
|
|
@@ -1482,11 +1530,11 @@ var SQLiteEventStore = class {
|
|
|
1482
1530
|
);
|
|
1483
1531
|
}
|
|
1484
1532
|
/**
|
|
1485
|
-
* Get most accessed memories
|
|
1533
|
+
* Get most accessed memories (falls back to recent events if none accessed)
|
|
1486
1534
|
*/
|
|
1487
1535
|
async getMostAccessed(limit = 10) {
|
|
1488
1536
|
await this.initialize();
|
|
1489
|
-
|
|
1537
|
+
let rows = sqliteAll(
|
|
1490
1538
|
this.db,
|
|
1491
1539
|
`SELECT * FROM events
|
|
1492
1540
|
WHERE access_count > 0
|
|
@@ -1494,8 +1542,166 @@ var SQLiteEventStore = class {
|
|
|
1494
1542
|
LIMIT ?`,
|
|
1495
1543
|
[limit]
|
|
1496
1544
|
);
|
|
1545
|
+
if (rows.length === 0) {
|
|
1546
|
+
rows = sqliteAll(
|
|
1547
|
+
this.db,
|
|
1548
|
+
`SELECT * FROM events
|
|
1549
|
+
ORDER BY timestamp DESC
|
|
1550
|
+
LIMIT ?`,
|
|
1551
|
+
[limit]
|
|
1552
|
+
);
|
|
1553
|
+
}
|
|
1497
1554
|
return rows.map((row) => this.rowToEvent(row));
|
|
1498
1555
|
}
|
|
1556
|
+
/**
|
|
1557
|
+
* Record a memory retrieval for helpfulness tracking
|
|
1558
|
+
*/
|
|
1559
|
+
async recordRetrieval(eventId, sessionId, score, query) {
|
|
1560
|
+
if (this.readOnly)
|
|
1561
|
+
return;
|
|
1562
|
+
await this.initialize();
|
|
1563
|
+
const id = randomUUID2();
|
|
1564
|
+
sqliteRun(
|
|
1565
|
+
this.db,
|
|
1566
|
+
`INSERT INTO memory_helpfulness (id, event_id, session_id, retrieval_score, query_preview, created_at)
|
|
1567
|
+
VALUES (?, ?, ?, ?, ?, datetime('now'))`,
|
|
1568
|
+
[id, eventId, sessionId, score, query.slice(0, 100)]
|
|
1569
|
+
);
|
|
1570
|
+
}
|
|
1571
|
+
/**
|
|
1572
|
+
* Evaluate helpfulness for all retrievals in a session
|
|
1573
|
+
* Called at session end - uses behavioral signals to compute score
|
|
1574
|
+
*/
|
|
1575
|
+
async evaluateSessionHelpfulness(sessionId) {
|
|
1576
|
+
if (this.readOnly)
|
|
1577
|
+
return;
|
|
1578
|
+
await this.initialize();
|
|
1579
|
+
const retrievals = sqliteAll(
|
|
1580
|
+
this.db,
|
|
1581
|
+
`SELECT * FROM memory_helpfulness WHERE session_id = ? AND measured_at IS NULL`,
|
|
1582
|
+
[sessionId]
|
|
1583
|
+
);
|
|
1584
|
+
if (retrievals.length === 0)
|
|
1585
|
+
return;
|
|
1586
|
+
const sessionEvents = sqliteAll(
|
|
1587
|
+
this.db,
|
|
1588
|
+
`SELECT * FROM events WHERE session_id = ? ORDER BY timestamp ASC`,
|
|
1589
|
+
[sessionId]
|
|
1590
|
+
);
|
|
1591
|
+
const promptEvents = sessionEvents.filter((e) => e.event_type === "user_prompt");
|
|
1592
|
+
const toolEvents = sessionEvents.filter((e) => e.event_type === "tool_observation");
|
|
1593
|
+
let toolSuccessCount = 0;
|
|
1594
|
+
let toolTotalCount = toolEvents.length;
|
|
1595
|
+
for (const t of toolEvents) {
|
|
1596
|
+
try {
|
|
1597
|
+
const content = JSON.parse(t.content);
|
|
1598
|
+
if (content.success !== false)
|
|
1599
|
+
toolSuccessCount++;
|
|
1600
|
+
} catch {
|
|
1601
|
+
toolSuccessCount++;
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
const toolSuccessRatio = toolTotalCount > 0 ? toolSuccessCount / toolTotalCount : 0.5;
|
|
1605
|
+
for (const retrieval of retrievals) {
|
|
1606
|
+
const retrievalTime = retrieval.created_at;
|
|
1607
|
+
const eventsAfter = sessionEvents.filter((e) => e.timestamp > retrievalTime);
|
|
1608
|
+
const sessionContinued = eventsAfter.length > 0 ? 1 : 0;
|
|
1609
|
+
const promptsAfter = promptEvents.filter((e) => e.timestamp > retrievalTime);
|
|
1610
|
+
const promptCountAfter = promptsAfter.length;
|
|
1611
|
+
const queryWords = new Set((retrieval.query_preview || "").toLowerCase().split(/\s+/).filter((w) => w.length > 2));
|
|
1612
|
+
let wasReasked = 0;
|
|
1613
|
+
for (const p of promptsAfter) {
|
|
1614
|
+
const pWords = new Set(p.content.toLowerCase().split(/\s+/).filter((w) => w.length > 2));
|
|
1615
|
+
let overlap = 0;
|
|
1616
|
+
for (const w of queryWords) {
|
|
1617
|
+
if (pWords.has(w))
|
|
1618
|
+
overlap++;
|
|
1619
|
+
}
|
|
1620
|
+
if (queryWords.size > 0 && overlap / queryWords.size > 0.5) {
|
|
1621
|
+
wasReasked = 1;
|
|
1622
|
+
break;
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
const retrievalScore = retrieval.retrieval_score || 0;
|
|
1626
|
+
const helpfulnessScore = 0.3 * Math.min(retrievalScore, 1) + 0.25 * (sessionContinued ? 1 : 0) + 0.25 * toolSuccessRatio + 0.2 * (wasReasked ? 0 : 1);
|
|
1627
|
+
sqliteRun(
|
|
1628
|
+
this.db,
|
|
1629
|
+
`UPDATE memory_helpfulness
|
|
1630
|
+
SET session_continued = ?, prompt_count_after = ?,
|
|
1631
|
+
tool_success_count = ?, tool_total_count = ?,
|
|
1632
|
+
was_reasked = ?, helpfulness_score = ?,
|
|
1633
|
+
measured_at = datetime('now')
|
|
1634
|
+
WHERE id = ?`,
|
|
1635
|
+
[
|
|
1636
|
+
sessionContinued,
|
|
1637
|
+
promptCountAfter,
|
|
1638
|
+
toolSuccessCount,
|
|
1639
|
+
toolTotalCount,
|
|
1640
|
+
wasReasked,
|
|
1641
|
+
helpfulnessScore,
|
|
1642
|
+
retrieval.id
|
|
1643
|
+
]
|
|
1644
|
+
);
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
/**
|
|
1648
|
+
* Get most helpful memories ranked by helpfulness score
|
|
1649
|
+
*/
|
|
1650
|
+
async getHelpfulMemories(limit = 10) {
|
|
1651
|
+
await this.initialize();
|
|
1652
|
+
const rows = sqliteAll(
|
|
1653
|
+
this.db,
|
|
1654
|
+
`SELECT
|
|
1655
|
+
mh.event_id,
|
|
1656
|
+
AVG(mh.helpfulness_score) as avg_score,
|
|
1657
|
+
COUNT(*) as eval_count,
|
|
1658
|
+
e.content,
|
|
1659
|
+
e.access_count
|
|
1660
|
+
FROM memory_helpfulness mh
|
|
1661
|
+
JOIN events e ON e.id = mh.event_id
|
|
1662
|
+
WHERE mh.measured_at IS NOT NULL
|
|
1663
|
+
GROUP BY mh.event_id
|
|
1664
|
+
ORDER BY avg_score DESC
|
|
1665
|
+
LIMIT ?`,
|
|
1666
|
+
[limit]
|
|
1667
|
+
);
|
|
1668
|
+
return rows.map((r) => ({
|
|
1669
|
+
eventId: r.event_id,
|
|
1670
|
+
summary: r.content.substring(0, 200) + (r.content.length > 200 ? "..." : ""),
|
|
1671
|
+
helpfulnessScore: Math.round(r.avg_score * 100) / 100,
|
|
1672
|
+
accessCount: r.access_count || 0,
|
|
1673
|
+
evaluationCount: r.eval_count
|
|
1674
|
+
}));
|
|
1675
|
+
}
|
|
1676
|
+
/**
|
|
1677
|
+
* Get helpfulness statistics for dashboard
|
|
1678
|
+
*/
|
|
1679
|
+
async getHelpfulnessStats() {
|
|
1680
|
+
await this.initialize();
|
|
1681
|
+
const stats = sqliteGet(
|
|
1682
|
+
this.db,
|
|
1683
|
+
`SELECT
|
|
1684
|
+
AVG(helpfulness_score) as avg_score,
|
|
1685
|
+
COUNT(*) as total_evaluated,
|
|
1686
|
+
SUM(CASE WHEN helpfulness_score >= 0.7 THEN 1 ELSE 0 END) as helpful,
|
|
1687
|
+
SUM(CASE WHEN helpfulness_score >= 0.4 AND helpfulness_score < 0.7 THEN 1 ELSE 0 END) as neutral,
|
|
1688
|
+
SUM(CASE WHEN helpfulness_score < 0.4 THEN 1 ELSE 0 END) as unhelpful
|
|
1689
|
+
FROM memory_helpfulness
|
|
1690
|
+
WHERE measured_at IS NOT NULL`
|
|
1691
|
+
);
|
|
1692
|
+
const totalRow = sqliteGet(
|
|
1693
|
+
this.db,
|
|
1694
|
+
`SELECT COUNT(*) as total FROM memory_helpfulness`
|
|
1695
|
+
);
|
|
1696
|
+
return {
|
|
1697
|
+
avgScore: Math.round((stats?.avg_score || 0) * 100) / 100,
|
|
1698
|
+
totalEvaluated: stats?.total_evaluated || 0,
|
|
1699
|
+
totalRetrievals: totalRow?.total || 0,
|
|
1700
|
+
helpful: stats?.helpful || 0,
|
|
1701
|
+
neutral: stats?.neutral || 0,
|
|
1702
|
+
unhelpful: stats?.unhelpful || 0
|
|
1703
|
+
};
|
|
1704
|
+
}
|
|
1499
1705
|
/**
|
|
1500
1706
|
* Fast keyword search using FTS5
|
|
1501
1707
|
* Returns events matching the search query, ranked by relevance
|
|
@@ -1564,6 +1770,143 @@ var SQLiteEventStore = class {
|
|
|
1564
1770
|
async close() {
|
|
1565
1771
|
sqliteClose(this.db);
|
|
1566
1772
|
}
|
|
1773
|
+
/**
|
|
1774
|
+
* Get events grouped by turn_id for a session
|
|
1775
|
+
* Returns turns ordered by first event timestamp (newest first)
|
|
1776
|
+
*/
|
|
1777
|
+
async getSessionTurns(sessionId, options) {
|
|
1778
|
+
await this.initialize();
|
|
1779
|
+
const limit = options?.limit || 20;
|
|
1780
|
+
const offset = options?.offset || 0;
|
|
1781
|
+
const turnRows = sqliteAll(
|
|
1782
|
+
this.db,
|
|
1783
|
+
`SELECT turn_id, MIN(timestamp) as min_ts
|
|
1784
|
+
FROM events
|
|
1785
|
+
WHERE session_id = ? AND turn_id IS NOT NULL
|
|
1786
|
+
GROUP BY turn_id
|
|
1787
|
+
ORDER BY min_ts DESC
|
|
1788
|
+
LIMIT ? OFFSET ?`,
|
|
1789
|
+
[sessionId, limit, offset]
|
|
1790
|
+
);
|
|
1791
|
+
const turns = [];
|
|
1792
|
+
for (const turnRow of turnRows) {
|
|
1793
|
+
const events = await this.getEventsByTurn(turnRow.turn_id);
|
|
1794
|
+
const promptEvent = events.find((e) => e.eventType === "user_prompt");
|
|
1795
|
+
const toolEvents = events.filter((e) => e.eventType === "tool_observation");
|
|
1796
|
+
const hasResponse = events.some((e) => e.eventType === "agent_response");
|
|
1797
|
+
turns.push({
|
|
1798
|
+
turnId: turnRow.turn_id,
|
|
1799
|
+
events,
|
|
1800
|
+
startedAt: toDateFromSQLite(turnRow.min_ts),
|
|
1801
|
+
promptPreview: promptEvent ? promptEvent.content.slice(0, 200) + (promptEvent.content.length > 200 ? "..." : "") : "(no prompt)",
|
|
1802
|
+
eventCount: events.length,
|
|
1803
|
+
toolCount: toolEvents.length,
|
|
1804
|
+
hasResponse
|
|
1805
|
+
});
|
|
1806
|
+
}
|
|
1807
|
+
return turns;
|
|
1808
|
+
}
|
|
1809
|
+
/**
|
|
1810
|
+
* Get all events for a specific turn_id
|
|
1811
|
+
*/
|
|
1812
|
+
async getEventsByTurn(turnId) {
|
|
1813
|
+
await this.initialize();
|
|
1814
|
+
const rows = sqliteAll(
|
|
1815
|
+
this.db,
|
|
1816
|
+
`SELECT * FROM events WHERE turn_id = ? ORDER BY timestamp ASC`,
|
|
1817
|
+
[turnId]
|
|
1818
|
+
);
|
|
1819
|
+
return rows.map(this.rowToEvent);
|
|
1820
|
+
}
|
|
1821
|
+
/**
|
|
1822
|
+
* Count total turns for a session
|
|
1823
|
+
*/
|
|
1824
|
+
async countSessionTurns(sessionId) {
|
|
1825
|
+
await this.initialize();
|
|
1826
|
+
const row = sqliteGet(
|
|
1827
|
+
this.db,
|
|
1828
|
+
`SELECT COUNT(DISTINCT turn_id) as count
|
|
1829
|
+
FROM events
|
|
1830
|
+
WHERE session_id = ? AND turn_id IS NOT NULL`,
|
|
1831
|
+
[sessionId]
|
|
1832
|
+
);
|
|
1833
|
+
return row?.count || 0;
|
|
1834
|
+
}
|
|
1835
|
+
/**
|
|
1836
|
+
* Migrate existing events: backfill turn_id for events that have turnId in metadata
|
|
1837
|
+
* but no turn_id column value (for events stored before this migration)
|
|
1838
|
+
*/
|
|
1839
|
+
async backfillTurnIds() {
|
|
1840
|
+
await this.initialize();
|
|
1841
|
+
const rows = sqliteAll(
|
|
1842
|
+
this.db,
|
|
1843
|
+
`SELECT id, metadata FROM events
|
|
1844
|
+
WHERE turn_id IS NULL AND metadata IS NOT NULL AND metadata LIKE '%turnId%'`
|
|
1845
|
+
);
|
|
1846
|
+
let updated = 0;
|
|
1847
|
+
for (const row of rows) {
|
|
1848
|
+
try {
|
|
1849
|
+
const metadata = JSON.parse(row.metadata);
|
|
1850
|
+
if (metadata.turnId) {
|
|
1851
|
+
sqliteRun(
|
|
1852
|
+
this.db,
|
|
1853
|
+
`UPDATE events SET turn_id = ? WHERE id = ?`,
|
|
1854
|
+
[metadata.turnId, row.id]
|
|
1855
|
+
);
|
|
1856
|
+
updated++;
|
|
1857
|
+
}
|
|
1858
|
+
} catch {
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
return updated;
|
|
1862
|
+
}
|
|
1863
|
+
/**
|
|
1864
|
+
* Delete all events for a session (for force reimport)
|
|
1865
|
+
*/
|
|
1866
|
+
async deleteSessionEvents(sessionId) {
|
|
1867
|
+
await this.initialize();
|
|
1868
|
+
const events = sqliteAll(
|
|
1869
|
+
this.db,
|
|
1870
|
+
`SELECT id FROM events WHERE session_id = ?`,
|
|
1871
|
+
[sessionId]
|
|
1872
|
+
);
|
|
1873
|
+
if (events.length === 0)
|
|
1874
|
+
return 0;
|
|
1875
|
+
const eventIds = events.map((e) => e.id);
|
|
1876
|
+
const placeholders = eventIds.map(() => "?").join(",");
|
|
1877
|
+
const ftsTriggersDropped = [];
|
|
1878
|
+
for (const triggerName of ["events_fts_delete", "events_fts_update", "events_fts_insert"]) {
|
|
1879
|
+
try {
|
|
1880
|
+
sqliteRun(this.db, `DROP TRIGGER IF EXISTS ${triggerName}`);
|
|
1881
|
+
ftsTriggersDropped.push(triggerName);
|
|
1882
|
+
} catch {
|
|
1883
|
+
}
|
|
1884
|
+
}
|
|
1885
|
+
for (const table of ["event_dedup", "memory_levels", "embedding_queue", "embedding_outbox", "vector_outbox"]) {
|
|
1886
|
+
try {
|
|
1887
|
+
sqliteRun(this.db, `DELETE FROM ${table} WHERE event_id IN (${placeholders})`, eventIds);
|
|
1888
|
+
} catch {
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
const result = sqliteRun(this.db, `DELETE FROM events WHERE session_id = ?`, [sessionId]);
|
|
1892
|
+
if (ftsTriggersDropped.length > 0) {
|
|
1893
|
+
try {
|
|
1894
|
+
sqliteRun(this.db, `INSERT INTO events_fts(events_fts) VALUES('rebuild')`);
|
|
1895
|
+
sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_insert AFTER INSERT ON events BEGIN
|
|
1896
|
+
INSERT INTO events_fts(rowid, content) VALUES (NEW.rowid, NEW.content);
|
|
1897
|
+
END`);
|
|
1898
|
+
sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_delete AFTER DELETE ON events BEGIN
|
|
1899
|
+
INSERT INTO events_fts(events_fts, rowid, content) VALUES('delete', OLD.rowid, OLD.content);
|
|
1900
|
+
END`);
|
|
1901
|
+
sqliteRun(this.db, `CREATE TRIGGER IF NOT EXISTS events_fts_update AFTER UPDATE ON events BEGIN
|
|
1902
|
+
INSERT INTO events_fts(events_fts, rowid, content) VALUES('delete', OLD.rowid, OLD.content);
|
|
1903
|
+
INSERT INTO events_fts(rowid, content) VALUES (NEW.rowid, NEW.content);
|
|
1904
|
+
END`);
|
|
1905
|
+
} catch {
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
return result.changes || 0;
|
|
1909
|
+
}
|
|
1567
1910
|
/**
|
|
1568
1911
|
* Convert database row to MemoryEvent
|
|
1569
1912
|
*/
|
|
@@ -1584,6 +1927,9 @@ var SQLiteEventStore = class {
|
|
|
1584
1927
|
if (row.last_accessed_at !== void 0) {
|
|
1585
1928
|
event.last_accessed_at = row.last_accessed_at;
|
|
1586
1929
|
}
|
|
1930
|
+
if (row.turn_id !== void 0 && row.turn_id !== null) {
|
|
1931
|
+
event.turn_id = row.turn_id;
|
|
1932
|
+
}
|
|
1587
1933
|
return event;
|
|
1588
1934
|
}
|
|
1589
1935
|
};
|
|
@@ -1795,7 +2141,16 @@ var VectorStore = class {
|
|
|
1795
2141
|
metadata: JSON.stringify(record.metadata || {})
|
|
1796
2142
|
};
|
|
1797
2143
|
if (!this.table) {
|
|
1798
|
-
|
|
2144
|
+
try {
|
|
2145
|
+
this.table = await this.db.createTable(this.tableName, [data]);
|
|
2146
|
+
} catch (e) {
|
|
2147
|
+
if (e?.message?.includes("already exists")) {
|
|
2148
|
+
this.table = await this.db.openTable(this.tableName);
|
|
2149
|
+
await this.table.add([data]);
|
|
2150
|
+
} else {
|
|
2151
|
+
throw e;
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
1799
2154
|
} else {
|
|
1800
2155
|
await this.table.add([data]);
|
|
1801
2156
|
}
|
|
@@ -1821,7 +2176,16 @@ var VectorStore = class {
|
|
|
1821
2176
|
metadata: JSON.stringify(record.metadata || {})
|
|
1822
2177
|
}));
|
|
1823
2178
|
if (!this.table) {
|
|
1824
|
-
|
|
2179
|
+
try {
|
|
2180
|
+
this.table = await this.db.createTable(this.tableName, data);
|
|
2181
|
+
} catch (e) {
|
|
2182
|
+
if (e?.message?.includes("already exists")) {
|
|
2183
|
+
this.table = await this.db.openTable(this.tableName);
|
|
2184
|
+
await this.table.add(data);
|
|
2185
|
+
} else {
|
|
2186
|
+
throw e;
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
1825
2189
|
} else {
|
|
1826
2190
|
await this.table.add(data);
|
|
1827
2191
|
}
|
|
@@ -4567,7 +4931,7 @@ function createGraduationWorker(eventStore, graduation, config) {
|
|
|
4567
4931
|
function normalizePath(projectPath) {
|
|
4568
4932
|
const expanded = projectPath.startsWith("~") ? path.join(os.homedir(), projectPath.slice(1)) : projectPath;
|
|
4569
4933
|
try {
|
|
4570
|
-
return
|
|
4934
|
+
return fs2.realpathSync(expanded);
|
|
4571
4935
|
} catch {
|
|
4572
4936
|
return path.resolve(expanded);
|
|
4573
4937
|
}
|
|
@@ -4584,8 +4948,8 @@ var REGISTRY_PATH = path.join(os.homedir(), ".claude-code", "memory", "session-r
|
|
|
4584
4948
|
var SHARED_STORAGE_PATH = path.join(os.homedir(), ".claude-code", "memory", "shared");
|
|
4585
4949
|
function loadSessionRegistry() {
|
|
4586
4950
|
try {
|
|
4587
|
-
if (
|
|
4588
|
-
const data =
|
|
4951
|
+
if (fs2.existsSync(REGISTRY_PATH)) {
|
|
4952
|
+
const data = fs2.readFileSync(REGISTRY_PATH, "utf-8");
|
|
4589
4953
|
return JSON.parse(data);
|
|
4590
4954
|
}
|
|
4591
4955
|
} catch (error) {
|
|
@@ -4630,8 +4994,8 @@ var MemoryService = class {
|
|
|
4630
4994
|
const storagePath = this.expandPath(config.storagePath);
|
|
4631
4995
|
this.readOnly = config.readOnly ?? false;
|
|
4632
4996
|
this.lightweightMode = config.lightweightMode ?? false;
|
|
4633
|
-
if (!this.readOnly && !
|
|
4634
|
-
|
|
4997
|
+
if (!this.readOnly && !fs2.existsSync(storagePath)) {
|
|
4998
|
+
fs2.mkdirSync(storagePath, { recursive: true });
|
|
4635
4999
|
}
|
|
4636
5000
|
this.projectHash = config.projectHash || null;
|
|
4637
5001
|
this.sharedStoreConfig = config.sharedStoreConfig ?? { enabled: true };
|
|
@@ -4726,8 +5090,8 @@ var MemoryService = class {
|
|
|
4726
5090
|
*/
|
|
4727
5091
|
async initializeSharedStore() {
|
|
4728
5092
|
const sharedPath = this.sharedStoreConfig?.sharedStoragePath ? this.expandPath(this.sharedStoreConfig.sharedStoragePath) : SHARED_STORAGE_PATH;
|
|
4729
|
-
if (!
|
|
4730
|
-
|
|
5093
|
+
if (!fs2.existsSync(sharedPath)) {
|
|
5094
|
+
fs2.mkdirSync(sharedPath, { recursive: true });
|
|
4731
5095
|
}
|
|
4732
5096
|
this.sharedEventStore = createSharedEventStore(
|
|
4733
5097
|
path.join(sharedPath, "shared.duckdb")
|
|
@@ -4824,6 +5188,7 @@ var MemoryService = class {
|
|
|
4824
5188
|
async storeToolObservation(sessionId, payload) {
|
|
4825
5189
|
await this.initialize();
|
|
4826
5190
|
const content = JSON.stringify(payload);
|
|
5191
|
+
const turnId = payload.metadata?.turnId;
|
|
4827
5192
|
const result = await this.sqliteStore.append({
|
|
4828
5193
|
eventType: "tool_observation",
|
|
4829
5194
|
sessionId,
|
|
@@ -4831,7 +5196,8 @@ var MemoryService = class {
|
|
|
4831
5196
|
content,
|
|
4832
5197
|
metadata: {
|
|
4833
5198
|
toolName: payload.toolName,
|
|
4834
|
-
success: payload.success
|
|
5199
|
+
success: payload.success,
|
|
5200
|
+
...turnId ? { turnId } : {}
|
|
4835
5201
|
}
|
|
4836
5202
|
});
|
|
4837
5203
|
if (result.success && !result.isDuplicate) {
|
|
@@ -5114,6 +5480,31 @@ var MemoryService = class {
|
|
|
5114
5480
|
return [];
|
|
5115
5481
|
return this.consolidatedStore.getAll({ limit });
|
|
5116
5482
|
}
|
|
5483
|
+
/**
|
|
5484
|
+
* Extract topic keywords from event content (markdown headings and key terms)
|
|
5485
|
+
*/
|
|
5486
|
+
extractTopicsFromContent(content) {
|
|
5487
|
+
const topics = /* @__PURE__ */ new Set();
|
|
5488
|
+
const headings = content.match(/^#{1,3}\s+(.+)$/gm);
|
|
5489
|
+
if (headings) {
|
|
5490
|
+
for (const h of headings.slice(0, 5)) {
|
|
5491
|
+
const text = h.replace(/^#+\s+/, "").replace(/[*_`#]/g, "").trim();
|
|
5492
|
+
if (text.length > 2 && text.length < 50) {
|
|
5493
|
+
topics.add(text);
|
|
5494
|
+
}
|
|
5495
|
+
}
|
|
5496
|
+
}
|
|
5497
|
+
const boldTerms = content.match(/\*\*([^*]+)\*\*/g);
|
|
5498
|
+
if (boldTerms) {
|
|
5499
|
+
for (const b of boldTerms.slice(0, 5)) {
|
|
5500
|
+
const text = b.replace(/\*\*/g, "").trim();
|
|
5501
|
+
if (text.length > 2 && text.length < 30) {
|
|
5502
|
+
topics.add(text);
|
|
5503
|
+
}
|
|
5504
|
+
}
|
|
5505
|
+
}
|
|
5506
|
+
return Array.from(topics).slice(0, 5);
|
|
5507
|
+
}
|
|
5117
5508
|
/**
|
|
5118
5509
|
* Increment access count for memories that were used in prompts
|
|
5119
5510
|
*/
|
|
@@ -5137,8 +5528,7 @@ var MemoryService = class {
|
|
|
5137
5528
|
return events.map((event) => ({
|
|
5138
5529
|
memoryId: event.id,
|
|
5139
5530
|
summary: event.content.substring(0, 200) + (event.content.length > 200 ? "..." : ""),
|
|
5140
|
-
topics:
|
|
5141
|
-
// Could extract topics from content if needed
|
|
5531
|
+
topics: this.extractTopicsFromContent(event.content),
|
|
5142
5532
|
accessCount: event.access_count || 0,
|
|
5143
5533
|
lastAccessed: event.last_accessed_at || null,
|
|
5144
5534
|
confidence: 1,
|
|
@@ -5159,6 +5549,34 @@ var MemoryService = class {
|
|
|
5159
5549
|
}
|
|
5160
5550
|
return [];
|
|
5161
5551
|
}
|
|
5552
|
+
/**
|
|
5553
|
+
* Record a memory retrieval for helpfulness tracking
|
|
5554
|
+
*/
|
|
5555
|
+
async recordRetrieval(eventId, sessionId, score, query) {
|
|
5556
|
+
await this.initialize();
|
|
5557
|
+
await this.sqliteStore.recordRetrieval(eventId, sessionId, score, query);
|
|
5558
|
+
}
|
|
5559
|
+
/**
|
|
5560
|
+
* Evaluate helpfulness of retrievals in a session (called at session end)
|
|
5561
|
+
*/
|
|
5562
|
+
async evaluateSessionHelpfulness(sessionId) {
|
|
5563
|
+
await this.initialize();
|
|
5564
|
+
await this.sqliteStore.evaluateSessionHelpfulness(sessionId);
|
|
5565
|
+
}
|
|
5566
|
+
/**
|
|
5567
|
+
* Get most helpful memories ranked by helpfulness score
|
|
5568
|
+
*/
|
|
5569
|
+
async getHelpfulMemories(limit = 10) {
|
|
5570
|
+
await this.initialize();
|
|
5571
|
+
return this.sqliteStore.getHelpfulMemories(limit);
|
|
5572
|
+
}
|
|
5573
|
+
/**
|
|
5574
|
+
* Get helpfulness statistics for dashboard
|
|
5575
|
+
*/
|
|
5576
|
+
async getHelpfulnessStats() {
|
|
5577
|
+
await this.initialize();
|
|
5578
|
+
return this.sqliteStore.getHelpfulnessStats();
|
|
5579
|
+
}
|
|
5162
5580
|
/**
|
|
5163
5581
|
* Mark a consolidated memory as accessed
|
|
5164
5582
|
*/
|
|
@@ -5222,6 +5640,44 @@ var MemoryService = class {
|
|
|
5222
5640
|
lastConsolidation
|
|
5223
5641
|
};
|
|
5224
5642
|
}
|
|
5643
|
+
// ============================================================
|
|
5644
|
+
// Turn Grouping Methods
|
|
5645
|
+
// ============================================================
|
|
5646
|
+
/**
|
|
5647
|
+
* Get events grouped by turn for a session
|
|
5648
|
+
*/
|
|
5649
|
+
async getSessionTurns(sessionId, options) {
|
|
5650
|
+
await this.initialize();
|
|
5651
|
+
return this.sqliteStore.getSessionTurns(sessionId, options);
|
|
5652
|
+
}
|
|
5653
|
+
/**
|
|
5654
|
+
* Get all events for a specific turn
|
|
5655
|
+
*/
|
|
5656
|
+
async getEventsByTurn(turnId) {
|
|
5657
|
+
await this.initialize();
|
|
5658
|
+
return this.sqliteStore.getEventsByTurn(turnId);
|
|
5659
|
+
}
|
|
5660
|
+
/**
|
|
5661
|
+
* Count total turns for a session
|
|
5662
|
+
*/
|
|
5663
|
+
async countSessionTurns(sessionId) {
|
|
5664
|
+
await this.initialize();
|
|
5665
|
+
return this.sqliteStore.countSessionTurns(sessionId);
|
|
5666
|
+
}
|
|
5667
|
+
/**
|
|
5668
|
+
* Backfill turn_ids from metadata for events stored before the migration
|
|
5669
|
+
*/
|
|
5670
|
+
async backfillTurnIds() {
|
|
5671
|
+
await this.initialize();
|
|
5672
|
+
return this.sqliteStore.backfillTurnIds();
|
|
5673
|
+
}
|
|
5674
|
+
/**
|
|
5675
|
+
* Delete all events for a session (for force reimport)
|
|
5676
|
+
*/
|
|
5677
|
+
async deleteSessionEvents(sessionId) {
|
|
5678
|
+
await this.initialize();
|
|
5679
|
+
return this.sqliteStore.deleteSessionEvents(sessionId);
|
|
5680
|
+
}
|
|
5225
5681
|
/**
|
|
5226
5682
|
* Format Endless Mode context for Claude
|
|
5227
5683
|
*/
|
|
@@ -5304,40 +5760,21 @@ var MemoryService = class {
|
|
|
5304
5760
|
}
|
|
5305
5761
|
};
|
|
5306
5762
|
var serviceCache = /* @__PURE__ */ new Map();
|
|
5307
|
-
|
|
5308
|
-
|
|
5309
|
-
|
|
5310
|
-
|
|
5311
|
-
|
|
5763
|
+
function getLightweightMemoryService(sessionId) {
|
|
5764
|
+
const projectInfo = getSessionProject(sessionId);
|
|
5765
|
+
const key = projectInfo ? `lightweight_${projectInfo.projectHash}` : "lightweight_global";
|
|
5766
|
+
if (!serviceCache.has(key)) {
|
|
5767
|
+
const storagePath = projectInfo ? getProjectStoragePath(projectInfo.projectPath) : path.join(os.homedir(), ".claude-code", "memory");
|
|
5768
|
+
serviceCache.set(key, new MemoryService({
|
|
5769
|
+
storagePath,
|
|
5770
|
+
projectHash: projectInfo?.projectHash,
|
|
5771
|
+
lightweightMode: true,
|
|
5772
|
+
// Skip embedder/vector/workers
|
|
5312
5773
|
analyticsEnabled: false,
|
|
5313
|
-
// Hooks don't need DuckDB
|
|
5314
5774
|
sharedStoreConfig: { enabled: false }
|
|
5315
|
-
// Shared store uses DuckDB too
|
|
5316
5775
|
}));
|
|
5317
5776
|
}
|
|
5318
|
-
return serviceCache.get(
|
|
5319
|
-
}
|
|
5320
|
-
function getMemoryServiceForProject(projectPath, sharedStoreConfig) {
|
|
5321
|
-
const hash = hashProjectPath(projectPath);
|
|
5322
|
-
if (!serviceCache.has(hash)) {
|
|
5323
|
-
const storagePath = getProjectStoragePath(projectPath);
|
|
5324
|
-
serviceCache.set(hash, new MemoryService({
|
|
5325
|
-
storagePath,
|
|
5326
|
-
projectHash: hash,
|
|
5327
|
-
// Override shared store config - hooks don't need DuckDB
|
|
5328
|
-
sharedStoreConfig: sharedStoreConfig ?? { enabled: false },
|
|
5329
|
-
analyticsEnabled: false
|
|
5330
|
-
// Hooks don't need DuckDB
|
|
5331
|
-
}));
|
|
5332
|
-
}
|
|
5333
|
-
return serviceCache.get(hash);
|
|
5334
|
-
}
|
|
5335
|
-
function getMemoryServiceForSession(sessionId) {
|
|
5336
|
-
const projectInfo = getSessionProject(sessionId);
|
|
5337
|
-
if (projectInfo) {
|
|
5338
|
-
return getMemoryServiceForProject(projectInfo.projectPath);
|
|
5339
|
-
}
|
|
5340
|
-
return getDefaultMemoryService();
|
|
5777
|
+
return serviceCache.get(key);
|
|
5341
5778
|
}
|
|
5342
5779
|
|
|
5343
5780
|
// src/core/privacy/tag-parser.ts
|
|
@@ -5485,6 +5922,52 @@ function applyPrivacyFilter(content, config) {
|
|
|
5485
5922
|
};
|
|
5486
5923
|
}
|
|
5487
5924
|
|
|
5925
|
+
// src/core/turn-state.ts
|
|
5926
|
+
import * as fs3 from "fs";
|
|
5927
|
+
import * as path2 from "path";
|
|
5928
|
+
import * as os2 from "os";
|
|
5929
|
+
var TURN_STATE_DIR = path2.join(os2.homedir(), ".claude-code", "memory");
|
|
5930
|
+
function getStatePath(sessionId) {
|
|
5931
|
+
return path2.join(TURN_STATE_DIR, `.turn-state-${sessionId}.json`);
|
|
5932
|
+
}
|
|
5933
|
+
function readTurnState(sessionId) {
|
|
5934
|
+
try {
|
|
5935
|
+
const filePath = getStatePath(sessionId);
|
|
5936
|
+
if (!fs3.existsSync(filePath)) {
|
|
5937
|
+
return null;
|
|
5938
|
+
}
|
|
5939
|
+
const data = fs3.readFileSync(filePath, "utf-8");
|
|
5940
|
+
const state = JSON.parse(data);
|
|
5941
|
+
if (state.sessionId !== sessionId) {
|
|
5942
|
+
return null;
|
|
5943
|
+
}
|
|
5944
|
+
const createdAt = new Date(state.createdAt).getTime();
|
|
5945
|
+
const now = Date.now();
|
|
5946
|
+
if (now - createdAt > 30 * 60 * 1e3) {
|
|
5947
|
+
clearTurnState(sessionId);
|
|
5948
|
+
return null;
|
|
5949
|
+
}
|
|
5950
|
+
return state.turnId;
|
|
5951
|
+
} catch (error) {
|
|
5952
|
+
if (process.env.CLAUDE_MEMORY_DEBUG) {
|
|
5953
|
+
console.error("Failed to read turn state:", error);
|
|
5954
|
+
}
|
|
5955
|
+
return null;
|
|
5956
|
+
}
|
|
5957
|
+
}
|
|
5958
|
+
function clearTurnState(sessionId) {
|
|
5959
|
+
try {
|
|
5960
|
+
const filePath = getStatePath(sessionId);
|
|
5961
|
+
if (fs3.existsSync(filePath)) {
|
|
5962
|
+
fs3.unlinkSync(filePath);
|
|
5963
|
+
}
|
|
5964
|
+
} catch (error) {
|
|
5965
|
+
if (process.env.CLAUDE_MEMORY_DEBUG) {
|
|
5966
|
+
console.error("Failed to clear turn state:", error);
|
|
5967
|
+
}
|
|
5968
|
+
}
|
|
5969
|
+
}
|
|
5970
|
+
|
|
5488
5971
|
// src/hooks/stop.ts
|
|
5489
5972
|
var DEFAULT_PRIVACY_CONFIG = {
|
|
5490
5973
|
excludePatterns: ["password", "secret", "api_key", "token", "bearer"],
|
|
@@ -5496,32 +5979,65 @@ var DEFAULT_PRIVACY_CONFIG = {
|
|
|
5496
5979
|
supportedFormats: ["xml"]
|
|
5497
5980
|
}
|
|
5498
5981
|
};
|
|
5982
|
+
async function extractAssistantMessages(transcriptPath) {
|
|
5983
|
+
if (!fs4.existsSync(transcriptPath))
|
|
5984
|
+
return [];
|
|
5985
|
+
const messages = [];
|
|
5986
|
+
const stats = fs4.statSync(transcriptPath);
|
|
5987
|
+
const readStart = Math.max(0, stats.size - 200 * 1024);
|
|
5988
|
+
const stream = fs4.createReadStream(transcriptPath, {
|
|
5989
|
+
start: readStart,
|
|
5990
|
+
encoding: "utf8"
|
|
5991
|
+
});
|
|
5992
|
+
const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
|
|
5993
|
+
for await (const line of rl) {
|
|
5994
|
+
try {
|
|
5995
|
+
const entry = JSON.parse(line);
|
|
5996
|
+
if (entry.type !== "assistant")
|
|
5997
|
+
continue;
|
|
5998
|
+
const content = entry.message?.content;
|
|
5999
|
+
if (!Array.isArray(content))
|
|
6000
|
+
continue;
|
|
6001
|
+
const textParts = content.filter((c) => c.type === "text").map((c) => c.text).filter(Boolean);
|
|
6002
|
+
if (textParts.length > 0) {
|
|
6003
|
+
messages.push(textParts.join("\n"));
|
|
6004
|
+
}
|
|
6005
|
+
} catch {
|
|
6006
|
+
}
|
|
6007
|
+
}
|
|
6008
|
+
return messages;
|
|
6009
|
+
}
|
|
5499
6010
|
async function main() {
|
|
5500
6011
|
const inputData = await readStdin();
|
|
5501
6012
|
const input = JSON.parse(inputData);
|
|
5502
|
-
const memoryService =
|
|
6013
|
+
const memoryService = getLightweightMemoryService(input.session_id);
|
|
5503
6014
|
try {
|
|
5504
|
-
|
|
5505
|
-
|
|
5506
|
-
|
|
5507
|
-
|
|
5508
|
-
|
|
5509
|
-
|
|
5510
|
-
|
|
5511
|
-
await memoryService.storeAgentResponse(
|
|
5512
|
-
input.session_id,
|
|
5513
|
-
content,
|
|
5514
|
-
{
|
|
5515
|
-
stopReason: input.stop_reason,
|
|
5516
|
-
privacy: filterResult.metadata
|
|
5517
|
-
}
|
|
5518
|
-
);
|
|
6015
|
+
const turnId = readTurnState(input.session_id);
|
|
6016
|
+
const assistantMessages = await extractAssistantMessages(input.transcript_path);
|
|
6017
|
+
for (const text of assistantMessages) {
|
|
6018
|
+
const filterResult = applyPrivacyFilter(text, DEFAULT_PRIVACY_CONFIG);
|
|
6019
|
+
let content = filterResult.content;
|
|
6020
|
+
if (content.length > 5e3) {
|
|
6021
|
+
content = content.slice(0, 5e3) + "...[truncated]";
|
|
5519
6022
|
}
|
|
6023
|
+
if (content.trim().length < 10)
|
|
6024
|
+
continue;
|
|
6025
|
+
await memoryService.storeAgentResponse(
|
|
6026
|
+
input.session_id,
|
|
6027
|
+
content,
|
|
6028
|
+
{
|
|
6029
|
+
privacy: filterResult.metadata,
|
|
6030
|
+
...turnId ? { turnId } : {}
|
|
6031
|
+
}
|
|
6032
|
+
);
|
|
5520
6033
|
}
|
|
6034
|
+
clearTurnState(input.session_id);
|
|
5521
6035
|
await memoryService.processPendingEmbeddings();
|
|
5522
6036
|
console.log(JSON.stringify({}));
|
|
5523
6037
|
} catch (error) {
|
|
5524
|
-
|
|
6038
|
+
if (process.env.CLAUDE_MEMORY_DEBUG) {
|
|
6039
|
+
console.error("Stop hook error:", error);
|
|
6040
|
+
}
|
|
5525
6041
|
console.log(JSON.stringify({}));
|
|
5526
6042
|
}
|
|
5527
6043
|
}
|