-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarize.ts
More file actions
220 lines (200 loc) · 7.47 KB
/
summarize.ts
File metadata and controls
220 lines (200 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Fork note: modified in this fork from upstream rohitg00/agentmemory. See NOTICE and LICENSE.
import type { ISdk } from "iii-sdk";
import type {
CompressedObservation,
SessionSummary,
MemoryProvider,
Session,
} from "../types.js";
import { KV } from "../state/schema.js";
import { StateKV } from "../state/kv.js";
import { SUMMARY_SYSTEM, buildSummaryPrompt } from "../prompts/summary.js";
import { getXmlTag, getXmlChildren } from "../prompts/xml.js";
import { SummaryOutputSchema } from "../eval/schemas.js";
import { validateOutput } from "../eval/validator.js";
import { scoreSummary } from "../eval/quality.js";
import type { MetricsStore } from "../eval/metrics-store.js";
import { safeAudit } from "./audit.js";
import { logger } from "../logger.js";
import {
upsertProceduralRetrievalBlock,
upsertSemanticRetrievalBlock,
upsertSummaryRetrievalBlock,
} from "./retrieval-blocks.js";
function parseSummaryXml(
xml: string,
sessionId: string,
project: string,
obsCount: number,
): SessionSummary | null {
const title = getXmlTag(xml, "title");
if (!title) return null;
return {
sessionId,
project,
createdAt: new Date().toISOString(),
title,
narrative: getXmlTag(xml, "narrative"),
keyDecisions: getXmlChildren(xml, "decisions", "decision"),
filesModified: getXmlChildren(xml, "files", "file"),
concepts: getXmlChildren(xml, "concepts", "concept"),
observationCount: obsCount,
};
}
export function registerSummarizeFunction(
sdk: ISdk,
kv: StateKV,
provider: MemoryProvider,
metricsStore?: MetricsStore,
): void {
sdk.registerFunction("mem::summarize",
async (data: { sessionId: string } | undefined) => {
const startMs = Date.now();
if (!data || typeof data.sessionId !== "string" || !data.sessionId.trim()) {
return { success: false, error: "sessionId is required" };
}
const sessionId = data.sessionId.trim();
const session = await kv.get<Session>(KV.sessions, sessionId);
if (!session) {
logger.warn("Session not found for summarize", {
sessionId,
});
return { success: false, error: "session_not_found" };
}
const observations = await kv.list<CompressedObservation>(
KV.observations(sessionId),
);
const compressed = observations.filter((o) => o.title);
if (compressed.length === 0) {
logger.info("No observations to summarize", {
sessionId,
});
return { success: false, error: "no_observations" };
}
try {
const prompt = buildSummaryPrompt(compressed);
const response = await provider.summarize(SUMMARY_SYSTEM, prompt);
const summary = parseSummaryXml(
response,
sessionId,
session.project,
compressed.length,
);
if (!summary) {
const latencyMs = Date.now() - startMs;
if (metricsStore) {
await metricsStore.record("mem::summarize", latencyMs, false);
}
logger.warn("Failed to parse summary XML", {
sessionId,
});
return { success: false, error: "parse_failed" };
}
const summaryForValidation = {
title: summary.title,
narrative: summary.narrative,
keyDecisions: summary.keyDecisions,
filesModified: summary.filesModified,
concepts: summary.concepts,
};
const validation = validateOutput(
SummaryOutputSchema,
summaryForValidation,
"mem::summarize",
);
if (!validation.valid) {
const latencyMs = Date.now() - startMs;
if (metricsStore) {
await metricsStore.record("mem::summarize", latencyMs, false);
}
logger.warn("Summary validation failed", {
sessionId,
errors: validation.result.errors,
});
return { success: false, error: "validation_failed" };
}
const qualityScore = scoreSummary(summaryForValidation);
await kv.set(KV.summaries, sessionId, summary);
await upsertSummaryRetrievalBlock(kv, summary);
await safeAudit(kv, "compress", "mem::summarize", [sessionId], {
title: summary.title,
observationCount: compressed.length,
});
// Memory usefulness feedback loop
const injections = await kv.get<{
sessionId: string;
memoryIds: string[];
timestamp: string;
}>(KV.contextInjections, data.sessionId).catch(() => null);
if (injections && injections.memoryIds.length > 0) {
const sessionQuality = compressed.length >= 3 ? "good" : "low";
const strengthDelta = sessionQuality === "good" ? 0.2 : -0.1;
for (const memId of injections.memoryIds) {
// Try Memory store
const mem = await kv.get<any>(KV.memories, memId).catch(() => null);
if (mem && typeof mem.strength === "number") {
mem.strength = Math.max(1, Math.min(10, mem.strength + strengthDelta));
mem.lastAccessedAt = new Date().toISOString();
await kv.set(KV.memories, memId, mem).catch(() => {});
continue;
}
// Try Semantic store
const sem = await kv.get<any>(KV.semantic, memId).catch(() => null);
if (sem && typeof sem.strength === "number") {
sem.strength = Math.max(0.1, Math.min(1, sem.strength + strengthDelta / 10));
sem.accessCount = (sem.accessCount || 0) + 1;
sem.lastAccessedAt = new Date().toISOString();
await kv.set(KV.semantic, memId, sem).catch(() => {});
await upsertSemanticRetrievalBlock(kv, sem).catch(() => {});
continue;
}
// Try Procedural store
const proc = await kv.get<any>(KV.procedural, memId).catch(() => null);
if (proc && typeof proc.strength === "number") {
proc.strength = Math.max(0.1, Math.min(1, proc.strength + strengthDelta / 10));
proc.frequency = (proc.frequency || 0) + 1;
await kv.set(KV.procedural, memId, proc).catch(() => {});
await upsertProceduralRetrievalBlock(kv, proc).catch(() => {});
}
}
// Cleanup injection record
await kv.delete(KV.contextInjections, data.sessionId).catch(() => {});
logger.info("Memory feedback applied", {
sessionId: data.sessionId,
memoriesAdjusted: injections.memoryIds.length,
sessionQuality,
strengthDelta,
});
}
const latencyMs = Date.now() - startMs;
if (metricsStore) {
await metricsStore.record(
"mem::summarize",
latencyMs,
true,
qualityScore,
);
}
logger.info("Session summarized", {
sessionId,
title: summary.title,
decisions: summary.keyDecisions.length,
qualityScore,
valid: validation.valid,
});
return { success: true, summary, qualityScore };
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
const latencyMs = Date.now() - startMs;
if (metricsStore) {
await metricsStore.record("mem::summarize", latencyMs, false);
}
logger.error("Summarize failed", {
sessionId,
error: msg,
});
return { success: false, error: msg };
}
},
);
}