@agentmemory/agentmemory 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. package/.claude-plugin/marketplace.json +14 -0
  2. package/.github/workflows/ci.yml +22 -0
  3. package/.github/workflows/publish.yml +28 -0
  4. package/AGENTS.md +113 -0
  5. package/LICENSE +190 -0
  6. package/README.md +828 -0
  7. package/assets/banner.png +0 -0
  8. package/assets/demo.gif +0 -0
  9. package/assets/demo.mp4 +0 -0
  10. package/benchmark/QUALITY.md +73 -0
  11. package/benchmark/REAL-EMBEDDINGS.md +67 -0
  12. package/benchmark/SCALE.md +110 -0
  13. package/benchmark/dataset.ts +293 -0
  14. package/benchmark/quality-eval.ts +643 -0
  15. package/benchmark/real-embeddings-eval.ts +405 -0
  16. package/benchmark/scale-eval.ts +398 -0
  17. package/dist/cli.d.mts +1 -0
  18. package/dist/cli.mjs +137 -0
  19. package/dist/cli.mjs.map +1 -0
  20. package/dist/docker-compose.yml +14 -0
  21. package/dist/hooks/notification.d.mts +1 -0
  22. package/dist/hooks/notification.mjs +45 -0
  23. package/dist/hooks/notification.mjs.map +1 -0
  24. package/dist/hooks/post-tool-failure.d.mts +1 -0
  25. package/dist/hooks/post-tool-failure.mjs +45 -0
  26. package/dist/hooks/post-tool-failure.mjs.map +1 -0
  27. package/dist/hooks/post-tool-use.d.mts +1 -0
  28. package/dist/hooks/post-tool-use.mjs +53 -0
  29. package/dist/hooks/post-tool-use.mjs.map +1 -0
  30. package/dist/hooks/pre-compact.d.mts +1 -0
  31. package/dist/hooks/pre-compact.mjs +50 -0
  32. package/dist/hooks/pre-compact.mjs.map +1 -0
  33. package/dist/hooks/pre-tool-use.d.mts +1 -0
  34. package/dist/hooks/pre-tool-use.mjs +69 -0
  35. package/dist/hooks/pre-tool-use.mjs.map +1 -0
  36. package/dist/hooks/prompt-submit.d.mts +1 -0
  37. package/dist/hooks/prompt-submit.mjs +40 -0
  38. package/dist/hooks/prompt-submit.mjs.map +1 -0
  39. package/dist/hooks/session-end.d.mts +1 -0
  40. package/dist/hooks/session-end.mjs +61 -0
  41. package/dist/hooks/session-end.mjs.map +1 -0
  42. package/dist/hooks/session-start.d.mts +1 -0
  43. package/dist/hooks/session-start.mjs +42 -0
  44. package/dist/hooks/session-start.mjs.map +1 -0
  45. package/dist/hooks/stop.d.mts +1 -0
  46. package/dist/hooks/stop.mjs +33 -0
  47. package/dist/hooks/stop.mjs.map +1 -0
  48. package/dist/hooks/subagent-start.d.mts +1 -0
  49. package/dist/hooks/subagent-start.mjs +43 -0
  50. package/dist/hooks/subagent-start.mjs.map +1 -0
  51. package/dist/hooks/subagent-stop.d.mts +1 -0
  52. package/dist/hooks/subagent-stop.mjs +45 -0
  53. package/dist/hooks/subagent-stop.mjs.map +1 -0
  54. package/dist/hooks/task-completed.d.mts +1 -0
  55. package/dist/hooks/task-completed.mjs +46 -0
  56. package/dist/hooks/task-completed.mjs.map +1 -0
  57. package/dist/iii-config.yaml +51 -0
  58. package/dist/index.d.mts +2 -0
  59. package/dist/index.mjs +13776 -0
  60. package/dist/index.mjs.map +1 -0
  61. package/dist/src-QxitMPfJ.mjs +13775 -0
  62. package/dist/src-QxitMPfJ.mjs.map +1 -0
  63. package/dist/standalone.d.mts +1 -0
  64. package/dist/standalone.mjs +1155 -0
  65. package/dist/standalone.mjs.map +1 -0
  66. package/dist/transformers-BX_tgxdO.mjs +38684 -0
  67. package/dist/transformers-BX_tgxdO.mjs.map +1 -0
  68. package/dist/transformers-KMm1i9no.mjs +38683 -0
  69. package/dist/transformers-KMm1i9no.mjs.map +1 -0
  70. package/docker-compose.yml +14 -0
  71. package/iii-config.yaml +51 -0
  72. package/package.json +59 -0
  73. package/plugin/.claude-plugin/plugin.json +10 -0
  74. package/plugin/hooks/hooks.json +77 -0
  75. package/plugin/scripts/diagnostics.mjs +551 -0
  76. package/plugin/scripts/notification.mjs +45 -0
  77. package/plugin/scripts/post-tool-failure.mjs +45 -0
  78. package/plugin/scripts/post-tool-use.mjs +53 -0
  79. package/plugin/scripts/pre-compact.mjs +50 -0
  80. package/plugin/scripts/pre-tool-use.mjs +69 -0
  81. package/plugin/scripts/prompt-submit.mjs +40 -0
  82. package/plugin/scripts/session-end.mjs +61 -0
  83. package/plugin/scripts/session-start.mjs +42 -0
  84. package/plugin/scripts/stop.mjs +33 -0
  85. package/plugin/scripts/subagent-start.mjs +43 -0
  86. package/plugin/scripts/subagent-stop.mjs +45 -0
  87. package/plugin/scripts/task-completed.mjs +46 -0
  88. package/plugin/skills/forget/SKILL.md +32 -0
  89. package/plugin/skills/recall/SKILL.md +18 -0
  90. package/plugin/skills/remember/SKILL.md +25 -0
  91. package/plugin/skills/session-history/SKILL.md +17 -0
  92. package/src/auth.ts +12 -0
  93. package/src/cli.ts +159 -0
  94. package/src/config.ts +221 -0
  95. package/src/eval/metrics-store.ts +65 -0
  96. package/src/eval/quality.ts +51 -0
  97. package/src/eval/schemas.ts +124 -0
  98. package/src/eval/self-correct.ts +28 -0
  99. package/src/eval/validator.ts +31 -0
  100. package/src/functions/actions.ts +288 -0
  101. package/src/functions/audit.ts +61 -0
  102. package/src/functions/auto-forget.ts +169 -0
  103. package/src/functions/branch-aware.ts +169 -0
  104. package/src/functions/cascade.ts +80 -0
  105. package/src/functions/checkpoints.ts +209 -0
  106. package/src/functions/claude-bridge.ts +161 -0
  107. package/src/functions/compress.ts +194 -0
  108. package/src/functions/consolidate.ts +212 -0
  109. package/src/functions/consolidation-pipeline.ts +258 -0
  110. package/src/functions/context.ts +169 -0
  111. package/src/functions/crystallize.ts +293 -0
  112. package/src/functions/dedup.ts +57 -0
  113. package/src/functions/diagnostics.ts +785 -0
  114. package/src/functions/enrich.ts +132 -0
  115. package/src/functions/evict.ts +163 -0
  116. package/src/functions/export-import.ts +508 -0
  117. package/src/functions/facets.ts +248 -0
  118. package/src/functions/file-index.ts +106 -0
  119. package/src/functions/flow-compress.ts +214 -0
  120. package/src/functions/frontier.ts +196 -0
  121. package/src/functions/governance.ts +131 -0
  122. package/src/functions/graph-retrieval.ts +277 -0
  123. package/src/functions/graph.ts +275 -0
  124. package/src/functions/leases.ts +216 -0
  125. package/src/functions/lessons.ts +253 -0
  126. package/src/functions/mesh.ts +434 -0
  127. package/src/functions/migrate.ts +165 -0
  128. package/src/functions/observe.ts +144 -0
  129. package/src/functions/obsidian-export.ts +310 -0
  130. package/src/functions/patterns.ts +138 -0
  131. package/src/functions/privacy.ts +39 -0
  132. package/src/functions/profile.ts +155 -0
  133. package/src/functions/query-expansion.ts +186 -0
  134. package/src/functions/relations.ts +237 -0
  135. package/src/functions/remember.ts +162 -0
  136. package/src/functions/retention.ts +235 -0
  137. package/src/functions/routines.ts +289 -0
  138. package/src/functions/search.ts +80 -0
  139. package/src/functions/sentinels.ts +417 -0
  140. package/src/functions/signals.ts +186 -0
  141. package/src/functions/sketches.ts +274 -0
  142. package/src/functions/sliding-window.ts +257 -0
  143. package/src/functions/smart-search.ts +115 -0
  144. package/src/functions/snapshot.ts +219 -0
  145. package/src/functions/summarize.ts +155 -0
  146. package/src/functions/team.ts +147 -0
  147. package/src/functions/temporal-graph.ts +476 -0
  148. package/src/functions/timeline.ts +138 -0
  149. package/src/functions/verify.ts +117 -0
  150. package/src/health/monitor.ts +110 -0
  151. package/src/health/thresholds.ts +73 -0
  152. package/src/hooks/notification.ts +52 -0
  153. package/src/hooks/post-tool-failure.ts +58 -0
  154. package/src/hooks/post-tool-use.ts +62 -0
  155. package/src/hooks/pre-compact.ts +60 -0
  156. package/src/hooks/pre-tool-use.ts +72 -0
  157. package/src/hooks/prompt-submit.ts +46 -0
  158. package/src/hooks/session-end.ts +71 -0
  159. package/src/hooks/session-start.ts +48 -0
  160. package/src/hooks/stop.ts +39 -0
  161. package/src/hooks/subagent-start.ts +49 -0
  162. package/src/hooks/subagent-stop.ts +54 -0
  163. package/src/hooks/task-completed.ts +54 -0
  164. package/src/index.ts +342 -0
  165. package/src/mcp/in-memory-kv.ts +61 -0
  166. package/src/mcp/server.ts +1455 -0
  167. package/src/mcp/standalone.ts +177 -0
  168. package/src/mcp/tools-registry.ts +769 -0
  169. package/src/mcp/transport.ts +91 -0
  170. package/src/prompts/compression.ts +67 -0
  171. package/src/prompts/consolidation.ts +48 -0
  172. package/src/prompts/graph-extraction.ts +35 -0
  173. package/src/prompts/summary.ts +38 -0
  174. package/src/prompts/xml.ts +26 -0
  175. package/src/providers/agent-sdk.ts +34 -0
  176. package/src/providers/anthropic.ts +35 -0
  177. package/src/providers/circuit-breaker.ts +82 -0
  178. package/src/providers/embedding/cohere.ts +46 -0
  179. package/src/providers/embedding/gemini.ts +54 -0
  180. package/src/providers/embedding/index.ts +39 -0
  181. package/src/providers/embedding/local.ts +52 -0
  182. package/src/providers/embedding/openai.ts +45 -0
  183. package/src/providers/embedding/openrouter.ts +51 -0
  184. package/src/providers/embedding/voyage.ts +46 -0
  185. package/src/providers/fallback-chain.ts +31 -0
  186. package/src/providers/index.ts +84 -0
  187. package/src/providers/openrouter.ts +71 -0
  188. package/src/providers/resilient.ts +37 -0
  189. package/src/state/hybrid-search.ts +295 -0
  190. package/src/state/index-persistence.ts +63 -0
  191. package/src/state/keyed-mutex.ts +18 -0
  192. package/src/state/kv.ts +33 -0
  193. package/src/state/schema.ts +71 -0
  194. package/src/state/search-index.ts +245 -0
  195. package/src/state/stemmer.ts +104 -0
  196. package/src/state/synonyms.ts +63 -0
  197. package/src/state/vector-index.ts +130 -0
  198. package/src/telemetry/setup.ts +116 -0
  199. package/src/triggers/api.ts +1904 -0
  200. package/src/triggers/events.ts +71 -0
  201. package/src/types.ts +769 -0
  202. package/src/version.ts +1 -0
  203. package/src/viewer/index.html +2497 -0
  204. package/src/viewer/server.ts +207 -0
  205. package/src/xenova.d.ts +3 -0
  206. package/test/actions.test.ts +490 -0
  207. package/test/audit.test.ts +108 -0
  208. package/test/auto-forget.test.ts +188 -0
  209. package/test/cascade.test.ts +277 -0
  210. package/test/checkpoints.test.ts +493 -0
  211. package/test/circuit-breaker.test.ts +107 -0
  212. package/test/claude-bridge.test.ts +178 -0
  213. package/test/confidence.test.ts +247 -0
  214. package/test/consistency.test.ts +61 -0
  215. package/test/consolidation-pipeline.test.ts +251 -0
  216. package/test/crystallize.test.ts +521 -0
  217. package/test/diagnostics.test.ts +638 -0
  218. package/test/embedding-provider.test.ts +49 -0
  219. package/test/enrich.test.ts +209 -0
  220. package/test/eval.test.ts +300 -0
  221. package/test/export-import.test.ts +251 -0
  222. package/test/facets.test.ts +448 -0
  223. package/test/fallback-chain.test.ts +93 -0
  224. package/test/frontier.test.ts +485 -0
  225. package/test/governance.test.ts +147 -0
  226. package/test/graph-retrieval.test.ts +186 -0
  227. package/test/graph.test.ts +160 -0
  228. package/test/helpers/mocks.ts +40 -0
  229. package/test/hybrid-search.test.ts +145 -0
  230. package/test/index-persistence.test.ts +124 -0
  231. package/test/integration.test.ts +265 -0
  232. package/test/leases.test.ts +399 -0
  233. package/test/mcp-prompts.test.ts +218 -0
  234. package/test/mcp-resources.test.ts +286 -0
  235. package/test/mcp-standalone.test.ts +113 -0
  236. package/test/mesh.test.ts +700 -0
  237. package/test/privacy.test.ts +87 -0
  238. package/test/profile.test.ts +161 -0
  239. package/test/query-expansion.test.ts +154 -0
  240. package/test/relations.test.ts +198 -0
  241. package/test/retention.test.ts +245 -0
  242. package/test/routines.test.ts +497 -0
  243. package/test/schema-fingerprint.test.ts +81 -0
  244. package/test/schema.test.ts +42 -0
  245. package/test/search-index.test.ts +128 -0
  246. package/test/sentinels.test.ts +626 -0
  247. package/test/signals.test.ts +410 -0
  248. package/test/sketches.test.ts +549 -0
  249. package/test/sliding-window.test.ts +199 -0
  250. package/test/smart-search.test.ts +169 -0
  251. package/test/snapshot.test.ts +165 -0
  252. package/test/team.test.ts +156 -0
  253. package/test/temporal-graph.test.ts +378 -0
  254. package/test/timeline.test.ts +148 -0
  255. package/test/vector-index.test.ts +79 -0
  256. package/test/verify.test.ts +209 -0
  257. package/test/xml.test.ts +65 -0
  258. package/tsconfig.json +22 -0
  259. package/tsdown.config.ts +62 -0
@@ -0,0 +1,293 @@
1
+ import type { ISdk } from "iii-sdk";
2
+ import type { StateKV } from "../state/kv.js";
3
+ import { KV, generateId } from "../state/schema.js";
4
+ import type { Action, ActionEdge, Crystal, MemoryProvider } from "../types.js";
5
+
6
+ interface CrystalDigest {
7
+ narrative: string;
8
+ keyOutcomes: string[];
9
+ filesAffected: string[];
10
+ lessons: string[];
11
+ }
12
+
13
+ const CRYSTALLIZE_SYSTEM = `You are summarizing a completed chain of agent actions into a compact digest.
14
+ Extract: (1) what was accomplished in 1-2 sentences, (2) key decisions as bullet points,
15
+ (3) files affected, (4) any lessons or patterns worth remembering.
16
+ Return as JSON: { "narrative": "...", "keyOutcomes": ["..."], "filesAffected": ["..."], "lessons": ["..."] }`;
17
+
18
+ export function registerCrystallizeFunction(
19
+ sdk: ISdk,
20
+ kv: StateKV,
21
+ provider: MemoryProvider,
22
+ ): void {
23
+ sdk.registerFunction(
24
+ { id: "mem::crystallize" },
25
+ async (data: {
26
+ actionIds: string[];
27
+ sessionId?: string;
28
+ project?: string;
29
+ }) => {
30
+ if (!data.actionIds || data.actionIds.length === 0) {
31
+ return { success: false, error: "actionIds is required" };
32
+ }
33
+
34
+ const actions: Action[] = [];
35
+ for (const id of data.actionIds) {
36
+ const action = await kv.get<Action>(KV.actions, id);
37
+ if (!action) {
38
+ return { success: false, error: `action not found: ${id}` };
39
+ }
40
+ if (action.status !== "done" && action.status !== "cancelled") {
41
+ return {
42
+ success: false,
43
+ error: `action ${id} has status "${action.status}", expected "done" or "cancelled"`,
44
+ };
45
+ }
46
+ actions.push(action);
47
+ }
48
+
49
+ const allEdges = await kv.list<ActionEdge>(KV.actionEdges);
50
+ const idSet = new Set(data.actionIds);
51
+ const relevantEdges = allEdges.filter(
52
+ (e) => idSet.has(e.sourceActionId) || idSet.has(e.targetActionId),
53
+ );
54
+
55
+ const prompt = buildChainText(actions, relevantEdges);
56
+
57
+ try {
58
+ const response = await provider.summarize(CRYSTALLIZE_SYSTEM, prompt);
59
+ const digest = parseDigest(response);
60
+
61
+ const crystal: Crystal = {
62
+ id: generateId("crys"),
63
+ narrative: digest.narrative,
64
+ keyOutcomes: digest.keyOutcomes,
65
+ filesAffected: digest.filesAffected,
66
+ lessons: digest.lessons,
67
+ sourceActionIds: data.actionIds,
68
+ sessionId: data.sessionId,
69
+ project: data.project,
70
+ createdAt: new Date().toISOString(),
71
+ };
72
+
73
+ await kv.set(KV.crystals, crystal.id, crystal);
74
+
75
+ await Promise.all(
76
+ digest.lessons.map((lesson) =>
77
+ sdk.trigger("mem::lesson-save", {
78
+ content: lesson,
79
+ context: crystal.narrative,
80
+ confidence: 0.6,
81
+ project: data.project,
82
+ tags: [],
83
+ source: "crystal",
84
+ sourceIds: [crystal.id],
85
+ }).catch(() => {}),
86
+ ),
87
+ );
88
+
89
+ for (const action of actions) {
90
+ const updated = { ...action, crystallizedInto: crystal.id };
91
+ await kv.set(KV.actions, action.id, updated);
92
+ }
93
+
94
+ return { success: true, crystal };
95
+ } catch (err) {
96
+ return {
97
+ success: false,
98
+ error: `crystallization failed: ${String(err)}`,
99
+ };
100
+ }
101
+ },
102
+ );
103
+
104
+ sdk.registerFunction(
105
+ { id: "mem::crystal-list" },
106
+ async (data: {
107
+ project?: string;
108
+ sessionId?: string;
109
+ limit?: number;
110
+ }) => {
111
+ const limit = data.limit ?? 20;
112
+ let crystals = await kv.list<Crystal>(KV.crystals);
113
+
114
+ if (data.project) {
115
+ crystals = crystals.filter((c) => c.project === data.project);
116
+ }
117
+ if (data.sessionId) {
118
+ crystals = crystals.filter((c) => c.sessionId === data.sessionId);
119
+ }
120
+
121
+ crystals.sort(
122
+ (a, b) =>
123
+ new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(),
124
+ );
125
+
126
+ return { success: true, crystals: crystals.slice(0, limit) };
127
+ },
128
+ );
129
+
130
+ sdk.registerFunction(
131
+ { id: "mem::crystal-get" },
132
+ async (data: { crystalId: string }) => {
133
+ if (!data.crystalId) {
134
+ return { success: false, error: "crystalId is required" };
135
+ }
136
+
137
+ const crystal = await kv.get<Crystal>(KV.crystals, data.crystalId);
138
+ if (!crystal) {
139
+ return { success: false, error: "crystal not found" };
140
+ }
141
+
142
+ return { success: true, crystal };
143
+ },
144
+ );
145
+
146
+ sdk.registerFunction(
147
+ { id: "mem::auto-crystallize" },
148
+ async (data: {
149
+ olderThanDays?: number;
150
+ project?: string;
151
+ dryRun?: boolean;
152
+ }) => {
153
+ const olderThanDays = data.olderThanDays ?? 7;
154
+ const dryRun = data.dryRun ?? false;
155
+ const cutoff = Date.now() - olderThanDays * 24 * 60 * 60 * 1000;
156
+
157
+ let allActions = await kv.list<CrystallizableAction>(KV.actions);
158
+
159
+ allActions = allActions.filter(
160
+ (a) =>
161
+ a.status === "done" &&
162
+ !a.crystallizedInto &&
163
+ new Date(a.createdAt).getTime() < cutoff,
164
+ );
165
+
166
+ if (data.project) {
167
+ allActions = allActions.filter((a) => a.project === data.project);
168
+ }
169
+
170
+ if (allActions.length === 0) {
171
+ return { success: true, groupCount: 0, crystalIds: [] };
172
+ }
173
+
174
+ const groups = new Map<string, Action[]>();
175
+ for (const action of allActions) {
176
+ const key = action.parentId ?? action.project ?? "_ungrouped";
177
+ const group = groups.get(key);
178
+ if (group) {
179
+ group.push(action);
180
+ } else {
181
+ groups.set(key, [action]);
182
+ }
183
+ }
184
+
185
+ if (dryRun) {
186
+ const groupSummaries = Array.from(groups.entries()).map(
187
+ ([key, actions]) => ({
188
+ groupKey: key,
189
+ actionCount: actions.length,
190
+ actionIds: actions.map((a) => a.id),
191
+ }),
192
+ );
193
+ return {
194
+ success: true,
195
+ dryRun: true,
196
+ groupCount: groups.size,
197
+ groups: groupSummaries,
198
+ crystalIds: [],
199
+ };
200
+ }
201
+
202
+ const crystalIds: string[] = [];
203
+ for (const [, groupActions] of groups) {
204
+ const actionIds = groupActions.map((a) => a.id);
205
+ const project = groupActions[0].project;
206
+
207
+ try {
208
+ const result = (await sdk.trigger("mem::crystallize", {
209
+ actionIds,
210
+ project,
211
+ })) as { success: boolean; crystal?: Crystal };
212
+
213
+ if (result.success && result.crystal) {
214
+ crystalIds.push(result.crystal.id);
215
+ }
216
+ } catch {
217
+ continue;
218
+ }
219
+ }
220
+
221
+ return {
222
+ success: true,
223
+ groupCount: groups.size,
224
+ crystalIds,
225
+ };
226
+ },
227
+ );
228
+ }
229
+
230
+ function buildChainText(actions: Action[], edges: ActionEdge[]): string {
231
+ const lines: string[] = ["## Completed Action Chain\n"];
232
+
233
+ const sorted = [...actions].sort(
234
+ (a, b) =>
235
+ new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime(),
236
+ );
237
+
238
+ for (const action of sorted) {
239
+ lines.push(`### ${action.title}`);
240
+ if (action.description) lines.push(action.description);
241
+ if (action.result) lines.push(`Result: ${action.result}`);
242
+ lines.push(
243
+ `Tags: ${(action.tags ?? []).join(", ")}`,
244
+ );
245
+ lines.push("");
246
+ }
247
+
248
+ if (edges.length > 0) {
249
+ lines.push("## Dependencies");
250
+ for (const edge of edges) {
251
+ lines.push(
252
+ `- ${edge.sourceActionId} --${edge.type}--> ${edge.targetActionId}`,
253
+ );
254
+ }
255
+ }
256
+
257
+ return lines.join("\n");
258
+ }
259
+
260
+ function parseDigest(response: string): CrystalDigest {
261
+ try {
262
+ const jsonMatch = response.match(/\{[\s\S]*\}/);
263
+ if (!jsonMatch) {
264
+ return {
265
+ narrative: response,
266
+ keyOutcomes: [],
267
+ filesAffected: [],
268
+ lessons: [],
269
+ };
270
+ }
271
+ const parsed = JSON.parse(jsonMatch[0]) as Record<string, unknown>;
272
+ return {
273
+ narrative:
274
+ typeof parsed.narrative === "string" ? parsed.narrative : response,
275
+ keyOutcomes: Array.isArray(parsed.keyOutcomes)
276
+ ? (parsed.keyOutcomes as string[])
277
+ : [],
278
+ filesAffected: Array.isArray(parsed.filesAffected)
279
+ ? (parsed.filesAffected as string[])
280
+ : [],
281
+ lessons: Array.isArray(parsed.lessons)
282
+ ? (parsed.lessons as string[])
283
+ : [],
284
+ };
285
+ } catch {
286
+ return {
287
+ narrative: response,
288
+ keyOutcomes: [],
289
+ filesAffected: [],
290
+ lessons: [],
291
+ };
292
+ }
293
+ }
@@ -0,0 +1,57 @@
1
+ import { createHash } from "node:crypto";
2
+
3
+ const TTL_MS = 5 * 60 * 1000;
4
+ const CLEANUP_INTERVAL_MS = 60_000;
5
+
6
+ interface DedupEntry {
7
+ hash: string;
8
+ expiresAt: number;
9
+ }
10
+
11
+ export class DedupMap {
12
+ private entries = new Map<string, DedupEntry>();
13
+ private cleanupTimer: ReturnType<typeof setInterval>;
14
+
15
+ constructor() {
16
+ this.cleanupTimer = setInterval(() => this.cleanup(), CLEANUP_INTERVAL_MS);
17
+ this.cleanupTimer.unref();
18
+ }
19
+
20
+ computeHash(sessionId: string, toolName: string, toolInput: unknown): string {
21
+ const input =
22
+ typeof toolInput === "string"
23
+ ? toolInput.slice(0, 500)
24
+ : JSON.stringify(toolInput ?? "").slice(0, 500);
25
+ const raw = `${sessionId}:${toolName}:${input}`;
26
+ return createHash("sha256").update(raw).digest("hex");
27
+ }
28
+
29
+ isDuplicate(hash: string): boolean {
30
+ const entry = this.entries.get(hash);
31
+ if (!entry) return false;
32
+ if (Date.now() > entry.expiresAt) {
33
+ this.entries.delete(hash);
34
+ return false;
35
+ }
36
+ return true;
37
+ }
38
+
39
+ record(hash: string): void {
40
+ this.entries.set(hash, { hash, expiresAt: Date.now() + TTL_MS });
41
+ }
42
+
43
+ private cleanup(): void {
44
+ const now = Date.now();
45
+ for (const [key, entry] of this.entries) {
46
+ if (now > entry.expiresAt) this.entries.delete(key);
47
+ }
48
+ }
49
+
50
+ stop(): void {
51
+ clearInterval(this.cleanupTimer);
52
+ }
53
+
54
+ get size(): number {
55
+ return this.entries.size;
56
+ }
57
+ }