@agentmemory/agentmemory 0.7.2 → 0.7.3

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 (217) hide show
  1. package/AGENTS.md +2 -2
  2. package/README.md +41 -68
  3. package/dist/cli.mjs +3 -3
  4. package/dist/index.mjs +3 -2
  5. package/dist/index.mjs.map +1 -1
  6. package/dist/{src-1fTKFEtN.mjs → src-sYZDDbiA.mjs} +4 -3
  7. package/dist/src-sYZDDbiA.mjs.map +1 -0
  8. package/dist/standalone.mjs +1 -1
  9. package/dist/standalone.mjs.map +1 -1
  10. package/package.json +10 -1
  11. package/plugin/.claude-plugin/plugin.json +1 -1
  12. package/plugin/scripts/notification.d.mts +1 -0
  13. package/plugin/scripts/notification.mjs.map +1 -0
  14. package/plugin/scripts/post-tool-failure.d.mts +1 -0
  15. package/plugin/scripts/post-tool-failure.mjs.map +1 -0
  16. package/plugin/scripts/post-tool-use.d.mts +1 -0
  17. package/plugin/scripts/post-tool-use.mjs.map +1 -0
  18. package/plugin/scripts/pre-compact.d.mts +1 -0
  19. package/plugin/scripts/pre-compact.mjs.map +1 -0
  20. package/plugin/scripts/pre-tool-use.d.mts +1 -0
  21. package/plugin/scripts/pre-tool-use.mjs.map +1 -0
  22. package/plugin/scripts/prompt-submit.d.mts +1 -0
  23. package/plugin/scripts/prompt-submit.mjs.map +1 -0
  24. package/plugin/scripts/session-end.d.mts +1 -0
  25. package/plugin/scripts/session-end.mjs.map +1 -0
  26. package/plugin/scripts/session-start.d.mts +1 -0
  27. package/plugin/scripts/session-start.mjs.map +1 -0
  28. package/plugin/scripts/stop.d.mts +1 -0
  29. package/plugin/scripts/stop.mjs.map +1 -0
  30. package/plugin/scripts/subagent-start.d.mts +1 -0
  31. package/plugin/scripts/subagent-start.mjs.map +1 -0
  32. package/plugin/scripts/subagent-stop.d.mts +1 -0
  33. package/plugin/scripts/subagent-stop.mjs.map +1 -0
  34. package/plugin/scripts/task-completed.d.mts +1 -0
  35. package/plugin/scripts/task-completed.mjs.map +1 -0
  36. package/.claude-plugin/marketplace.json +0 -14
  37. package/.github/workflows/ci.yml +0 -22
  38. package/.github/workflows/publish.yml +0 -28
  39. package/assets/banner.png +0 -0
  40. package/assets/demo.gif +0 -0
  41. package/assets/demo.mp4 +0 -0
  42. package/benchmark/QUALITY.md +0 -73
  43. package/benchmark/REAL-EMBEDDINGS.md +0 -67
  44. package/benchmark/SCALE.md +0 -110
  45. package/benchmark/dataset.ts +0 -293
  46. package/benchmark/quality-eval.ts +0 -643
  47. package/benchmark/real-embeddings-eval.ts +0 -405
  48. package/benchmark/scale-eval.ts +0 -398
  49. package/dist/src-1fTKFEtN.mjs.map +0 -1
  50. package/src/auth.ts +0 -12
  51. package/src/cli.ts +0 -251
  52. package/src/config.ts +0 -221
  53. package/src/eval/metrics-store.ts +0 -65
  54. package/src/eval/quality.ts +0 -51
  55. package/src/eval/schemas.ts +0 -124
  56. package/src/eval/self-correct.ts +0 -28
  57. package/src/eval/validator.ts +0 -31
  58. package/src/functions/actions.ts +0 -288
  59. package/src/functions/audit.ts +0 -61
  60. package/src/functions/auto-forget.ts +0 -169
  61. package/src/functions/branch-aware.ts +0 -169
  62. package/src/functions/cascade.ts +0 -80
  63. package/src/functions/checkpoints.ts +0 -209
  64. package/src/functions/claude-bridge.ts +0 -161
  65. package/src/functions/compress.ts +0 -194
  66. package/src/functions/consolidate.ts +0 -212
  67. package/src/functions/consolidation-pipeline.ts +0 -258
  68. package/src/functions/context.ts +0 -169
  69. package/src/functions/crystallize.ts +0 -293
  70. package/src/functions/dedup.ts +0 -57
  71. package/src/functions/diagnostics.ts +0 -785
  72. package/src/functions/enrich.ts +0 -132
  73. package/src/functions/evict.ts +0 -163
  74. package/src/functions/export-import.ts +0 -508
  75. package/src/functions/facets.ts +0 -248
  76. package/src/functions/file-index.ts +0 -106
  77. package/src/functions/flow-compress.ts +0 -214
  78. package/src/functions/frontier.ts +0 -196
  79. package/src/functions/governance.ts +0 -131
  80. package/src/functions/graph-retrieval.ts +0 -277
  81. package/src/functions/graph.ts +0 -275
  82. package/src/functions/leases.ts +0 -216
  83. package/src/functions/lessons.ts +0 -253
  84. package/src/functions/mesh.ts +0 -434
  85. package/src/functions/migrate.ts +0 -165
  86. package/src/functions/observe.ts +0 -144
  87. package/src/functions/obsidian-export.ts +0 -310
  88. package/src/functions/patterns.ts +0 -138
  89. package/src/functions/privacy.ts +0 -39
  90. package/src/functions/profile.ts +0 -155
  91. package/src/functions/query-expansion.ts +0 -186
  92. package/src/functions/relations.ts +0 -237
  93. package/src/functions/remember.ts +0 -162
  94. package/src/functions/retention.ts +0 -235
  95. package/src/functions/routines.ts +0 -289
  96. package/src/functions/search.ts +0 -80
  97. package/src/functions/sentinels.ts +0 -417
  98. package/src/functions/signals.ts +0 -186
  99. package/src/functions/sketches.ts +0 -274
  100. package/src/functions/sliding-window.ts +0 -257
  101. package/src/functions/smart-search.ts +0 -115
  102. package/src/functions/snapshot.ts +0 -219
  103. package/src/functions/summarize.ts +0 -155
  104. package/src/functions/team.ts +0 -147
  105. package/src/functions/temporal-graph.ts +0 -476
  106. package/src/functions/timeline.ts +0 -138
  107. package/src/functions/verify.ts +0 -117
  108. package/src/health/monitor.ts +0 -110
  109. package/src/health/thresholds.ts +0 -73
  110. package/src/hooks/notification.ts +0 -52
  111. package/src/hooks/post-tool-failure.ts +0 -58
  112. package/src/hooks/post-tool-use.ts +0 -62
  113. package/src/hooks/pre-compact.ts +0 -60
  114. package/src/hooks/pre-tool-use.ts +0 -72
  115. package/src/hooks/prompt-submit.ts +0 -46
  116. package/src/hooks/session-end.ts +0 -71
  117. package/src/hooks/session-start.ts +0 -48
  118. package/src/hooks/stop.ts +0 -39
  119. package/src/hooks/subagent-start.ts +0 -49
  120. package/src/hooks/subagent-stop.ts +0 -54
  121. package/src/hooks/task-completed.ts +0 -54
  122. package/src/index.ts +0 -342
  123. package/src/mcp/in-memory-kv.ts +0 -61
  124. package/src/mcp/server.ts +0 -1455
  125. package/src/mcp/standalone.ts +0 -177
  126. package/src/mcp/tools-registry.ts +0 -769
  127. package/src/mcp/transport.ts +0 -91
  128. package/src/prompts/compression.ts +0 -67
  129. package/src/prompts/consolidation.ts +0 -48
  130. package/src/prompts/graph-extraction.ts +0 -35
  131. package/src/prompts/summary.ts +0 -38
  132. package/src/prompts/xml.ts +0 -26
  133. package/src/providers/agent-sdk.ts +0 -34
  134. package/src/providers/anthropic.ts +0 -35
  135. package/src/providers/circuit-breaker.ts +0 -82
  136. package/src/providers/embedding/cohere.ts +0 -46
  137. package/src/providers/embedding/gemini.ts +0 -54
  138. package/src/providers/embedding/index.ts +0 -39
  139. package/src/providers/embedding/local.ts +0 -52
  140. package/src/providers/embedding/openai.ts +0 -45
  141. package/src/providers/embedding/openrouter.ts +0 -51
  142. package/src/providers/embedding/voyage.ts +0 -46
  143. package/src/providers/fallback-chain.ts +0 -31
  144. package/src/providers/index.ts +0 -84
  145. package/src/providers/openrouter.ts +0 -71
  146. package/src/providers/resilient.ts +0 -37
  147. package/src/state/hybrid-search.ts +0 -295
  148. package/src/state/index-persistence.ts +0 -63
  149. package/src/state/keyed-mutex.ts +0 -18
  150. package/src/state/kv.ts +0 -33
  151. package/src/state/schema.ts +0 -71
  152. package/src/state/search-index.ts +0 -245
  153. package/src/state/stemmer.ts +0 -104
  154. package/src/state/synonyms.ts +0 -63
  155. package/src/state/vector-index.ts +0 -130
  156. package/src/telemetry/setup.ts +0 -116
  157. package/src/triggers/api.ts +0 -1904
  158. package/src/triggers/events.ts +0 -71
  159. package/src/types.ts +0 -769
  160. package/src/version.ts +0 -1
  161. package/src/viewer/index.html +0 -2556
  162. package/src/viewer/server.ts +0 -207
  163. package/src/xenova.d.ts +0 -3
  164. package/test/actions.test.ts +0 -490
  165. package/test/audit.test.ts +0 -108
  166. package/test/auto-forget.test.ts +0 -188
  167. package/test/cascade.test.ts +0 -277
  168. package/test/checkpoints.test.ts +0 -493
  169. package/test/circuit-breaker.test.ts +0 -107
  170. package/test/claude-bridge.test.ts +0 -178
  171. package/test/confidence.test.ts +0 -247
  172. package/test/consistency.test.ts +0 -61
  173. package/test/consolidation-pipeline.test.ts +0 -251
  174. package/test/crystallize.test.ts +0 -521
  175. package/test/diagnostics.test.ts +0 -638
  176. package/test/embedding-provider.test.ts +0 -49
  177. package/test/enrich.test.ts +0 -209
  178. package/test/eval.test.ts +0 -300
  179. package/test/export-import.test.ts +0 -251
  180. package/test/facets.test.ts +0 -448
  181. package/test/fallback-chain.test.ts +0 -93
  182. package/test/frontier.test.ts +0 -485
  183. package/test/governance.test.ts +0 -147
  184. package/test/graph-retrieval.test.ts +0 -186
  185. package/test/graph.test.ts +0 -160
  186. package/test/helpers/mocks.ts +0 -40
  187. package/test/hybrid-search.test.ts +0 -145
  188. package/test/index-persistence.test.ts +0 -124
  189. package/test/integration.test.ts +0 -265
  190. package/test/leases.test.ts +0 -399
  191. package/test/mcp-prompts.test.ts +0 -218
  192. package/test/mcp-resources.test.ts +0 -286
  193. package/test/mcp-standalone.test.ts +0 -113
  194. package/test/mesh.test.ts +0 -700
  195. package/test/privacy.test.ts +0 -87
  196. package/test/profile.test.ts +0 -161
  197. package/test/query-expansion.test.ts +0 -154
  198. package/test/relations.test.ts +0 -198
  199. package/test/retention.test.ts +0 -245
  200. package/test/routines.test.ts +0 -497
  201. package/test/schema-fingerprint.test.ts +0 -81
  202. package/test/schema.test.ts +0 -42
  203. package/test/search-index.test.ts +0 -128
  204. package/test/sentinels.test.ts +0 -626
  205. package/test/signals.test.ts +0 -410
  206. package/test/sketches.test.ts +0 -549
  207. package/test/sliding-window.test.ts +0 -199
  208. package/test/smart-search.test.ts +0 -169
  209. package/test/snapshot.test.ts +0 -165
  210. package/test/team.test.ts +0 -156
  211. package/test/temporal-graph.test.ts +0 -378
  212. package/test/timeline.test.ts +0 -148
  213. package/test/vector-index.test.ts +0 -79
  214. package/test/verify.test.ts +0 -209
  215. package/test/xml.test.ts +0 -65
  216. package/tsconfig.json +0 -22
  217. package/tsdown.config.ts +0 -62
@@ -1,274 +0,0 @@
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 { withKeyedLock } from "../state/keyed-mutex.js";
5
- import type { Action, ActionEdge, Sketch } from "../types.js";
6
-
7
- export function registerSketchesFunction(sdk: ISdk, kv: StateKV): void {
8
- sdk.registerFunction(
9
- { id: "mem::sketch-create" },
10
- async (data: {
11
- title: string;
12
- description?: string;
13
- expiresInMs?: number;
14
- project?: string;
15
- }) => {
16
- if (!data.title || typeof data.title !== "string") {
17
- return { success: false, error: "title is required" };
18
- }
19
-
20
- const now = new Date();
21
- const expiresInMs = data.expiresInMs || 3600000;
22
- const sketch: Sketch = {
23
- id: generateId("sk"),
24
- title: data.title.trim(),
25
- description: (data.description || "").trim(),
26
- status: "active",
27
- actionIds: [],
28
- project: data.project,
29
- createdAt: now.toISOString(),
30
- expiresAt: new Date(now.getTime() + expiresInMs).toISOString(),
31
- };
32
-
33
- await kv.set(KV.sketches, sketch.id, sketch);
34
- return { success: true, sketch };
35
- },
36
- );
37
-
38
- sdk.registerFunction(
39
- { id: "mem::sketch-add" },
40
- async (data: {
41
- sketchId: string;
42
- title: string;
43
- description?: string;
44
- priority?: number;
45
- dependsOn?: string[];
46
- }) => {
47
- if (!data.sketchId) {
48
- return { success: false, error: "sketchId is required" };
49
- }
50
- if (!data.title || typeof data.title !== "string") {
51
- return { success: false, error: "title is required" };
52
- }
53
-
54
- return withKeyedLock(`mem:sketch:${data.sketchId}`, async () => {
55
- const sketch = await kv.get<Sketch>(KV.sketches, data.sketchId);
56
- if (!sketch) {
57
- return { success: false, error: "sketch not found" };
58
- }
59
- if (sketch.status !== "active") {
60
- return { success: false, error: "sketch is not active" };
61
- }
62
-
63
- const now = new Date().toISOString();
64
- const action: Action = {
65
- id: generateId("act"),
66
- title: data.title.trim(),
67
- description: (data.description || "").trim(),
68
- status: "pending",
69
- priority: Math.max(1, Math.min(10, data.priority || 5)),
70
- createdAt: now,
71
- updatedAt: now,
72
- createdBy: "sketch",
73
- project: sketch.project,
74
- tags: [],
75
- sourceObservationIds: [],
76
- sourceMemoryIds: [],
77
- sketchId: data.sketchId,
78
- };
79
-
80
- if (data.dependsOn && data.dependsOn.length > 0) {
81
- const sketchActionSet = new Set(sketch.actionIds);
82
- for (const depId of data.dependsOn) {
83
- if (!sketchActionSet.has(depId)) {
84
- return {
85
- success: false,
86
- error: `dependency ${depId} not found in this sketch`,
87
- };
88
- }
89
- }
90
- }
91
-
92
- await kv.set(KV.actions, action.id, action);
93
-
94
- const createdEdges: ActionEdge[] = [];
95
- if (data.dependsOn && data.dependsOn.length > 0) {
96
- for (const depId of data.dependsOn) {
97
- const edge: ActionEdge = {
98
- id: generateId("ae"),
99
- type: "requires",
100
- sourceActionId: action.id,
101
- targetActionId: depId,
102
- createdAt: now,
103
- };
104
- await kv.set(KV.actionEdges, edge.id, edge);
105
- createdEdges.push(edge);
106
- }
107
- }
108
-
109
- sketch.actionIds.push(action.id);
110
- await kv.set(KV.sketches, sketch.id, sketch);
111
-
112
- return { success: true, action, edges: createdEdges };
113
- });
114
- },
115
- );
116
-
117
- sdk.registerFunction(
118
- { id: "mem::sketch-promote" },
119
- async (data: { sketchId: string; project?: string }) => {
120
- if (!data.sketchId) {
121
- return { success: false, error: "sketchId is required" };
122
- }
123
-
124
- return withKeyedLock(`mem:sketch:${data.sketchId}`, async () => {
125
- const sketch = await kv.get<Sketch>(KV.sketches, data.sketchId);
126
- if (!sketch) {
127
- return { success: false, error: "sketch not found" };
128
- }
129
- if (sketch.status !== "active") {
130
- return { success: false, error: "sketch is not active" };
131
- }
132
-
133
- const promotedIds: string[] = [];
134
- for (const actionId of sketch.actionIds) {
135
- const action = await kv.get<Action>(KV.actions, actionId);
136
- if (action) {
137
- delete action.sketchId;
138
- if (data.project) {
139
- action.project = data.project;
140
- }
141
- action.updatedAt = new Date().toISOString();
142
- await kv.set(KV.actions, action.id, action);
143
- promotedIds.push(action.id);
144
- }
145
- }
146
-
147
- sketch.status = "promoted";
148
- sketch.promotedAt = new Date().toISOString();
149
- await kv.set(KV.sketches, sketch.id, sketch);
150
-
151
- return { success: true, promotedIds };
152
- });
153
- },
154
- );
155
-
156
- sdk.registerFunction(
157
- { id: "mem::sketch-discard" },
158
- async (data: { sketchId: string }) => {
159
- if (!data.sketchId) {
160
- return { success: false, error: "sketchId is required" };
161
- }
162
-
163
- return withKeyedLock(`mem:sketch:${data.sketchId}`, async () => {
164
- const sketch = await kv.get<Sketch>(KV.sketches, data.sketchId);
165
- if (!sketch) {
166
- return { success: false, error: "sketch not found" };
167
- }
168
- if (sketch.status !== "active") {
169
- return { success: false, error: "sketch is not active" };
170
- }
171
-
172
- const actionIdSet = new Set(sketch.actionIds);
173
-
174
- const allEdges = await kv.list<ActionEdge>(KV.actionEdges);
175
- for (const edge of allEdges) {
176
- if (
177
- actionIdSet.has(edge.sourceActionId) ||
178
- actionIdSet.has(edge.targetActionId)
179
- ) {
180
- await kv.delete(KV.actionEdges, edge.id);
181
- }
182
- }
183
-
184
- for (const actionId of sketch.actionIds) {
185
- await kv.delete(KV.actions, actionId);
186
- }
187
-
188
- sketch.status = "discarded";
189
- sketch.discardedAt = new Date().toISOString();
190
- await kv.set(KV.sketches, sketch.id, sketch);
191
-
192
- return { success: true, discardedCount: sketch.actionIds.length };
193
- });
194
- },
195
- );
196
-
197
- sdk.registerFunction(
198
- { id: "mem::sketch-list" },
199
- async (data: { status?: string; project?: string }) => {
200
- let sketches = await kv.list<Sketch>(KV.sketches);
201
-
202
- if (data.status) {
203
- sketches = sketches.filter((s) => s.status === data.status);
204
- }
205
- if (data.project) {
206
- sketches = sketches.filter((s) => s.project === data.project);
207
- }
208
-
209
- sketches.sort(
210
- (a, b) =>
211
- new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(),
212
- );
213
-
214
- const results = sketches.map((s) => ({
215
- ...s,
216
- actionCount: s.actionIds.length,
217
- }));
218
-
219
- return { success: true, sketches: results };
220
- },
221
- );
222
-
223
- sdk.registerFunction(
224
- { id: "mem::sketch-gc" },
225
- async () => {
226
- const sketches = await kv.list<Sketch>(KV.sketches);
227
- const now = Date.now();
228
- let collected = 0;
229
-
230
- for (const sketch of sketches) {
231
- if (
232
- sketch.status !== "active" ||
233
- new Date(sketch.expiresAt).getTime() > now
234
- ) {
235
- continue;
236
- }
237
-
238
- await withKeyedLock(`mem:sketch:${sketch.id}`, async () => {
239
- const current = await kv.get<Sketch>(KV.sketches, sketch.id);
240
- if (
241
- !current ||
242
- current.status !== "active" ||
243
- new Date(current.expiresAt).getTime() > now
244
- ) {
245
- return;
246
- }
247
-
248
- const actionIdSet = new Set(current.actionIds);
249
-
250
- const allEdges = await kv.list<ActionEdge>(KV.actionEdges);
251
- for (const edge of allEdges) {
252
- if (
253
- actionIdSet.has(edge.sourceActionId) ||
254
- actionIdSet.has(edge.targetActionId)
255
- ) {
256
- await kv.delete(KV.actionEdges, edge.id);
257
- }
258
- }
259
-
260
- for (const actionId of current.actionIds) {
261
- await kv.delete(KV.actions, actionId);
262
- }
263
-
264
- current.status = "discarded";
265
- current.discardedAt = new Date().toISOString();
266
- await kv.set(KV.sketches, current.id, current);
267
- collected++;
268
- });
269
- }
270
-
271
- return { success: true, collected };
272
- },
273
- );
274
- }
@@ -1,257 +0,0 @@
1
- import type { ISdk } from "iii-sdk";
2
- import { getContext } from "iii-sdk";
3
- import type {
4
- CompressedObservation,
5
- EnrichedChunk,
6
- MemoryProvider,
7
- } from "../types.js";
8
- import { KV, generateId } from "../state/schema.js";
9
- import type { StateKV } from "../state/kv.js";
10
-
11
- const SLIDING_WINDOW_SYSTEM = `You are a contextual enrichment engine. Given a primary observation and its surrounding context window (previous and next observations from the same session), produce an enriched version.
12
-
13
- Your tasks:
14
- 1. ENTITY RESOLUTION: Replace all pronouns, implicit references ("that framework", "the file", "it", "he/she") with the explicit entity names found in the context window.
15
- 2. PREFERENCE MAPPING: Extract any user preferences, constraints, or opinions expressed directly or indirectly.
16
- 3. CONTEXT BRIDGES: Add brief contextual links that make this chunk self-contained without reading adjacent chunks.
17
-
18
- Output EXACTLY this XML:
19
- <enriched>
20
- <content>The fully enriched, self-contained text with all references resolved</content>
21
- <resolved_entities>
22
- <entity original="pronoun or reference" resolved="explicit entity name"/>
23
- </resolved_entities>
24
- <preferences>
25
- <preference>extracted user preference or constraint</preference>
26
- </preferences>
27
- <context_bridges>
28
- <bridge>contextual link to adjacent information</bridge>
29
- </context_bridges>
30
- </enriched>
31
-
32
- Rules:
33
- - The enriched content MUST be understandable in complete isolation
34
- - Resolve ALL ambiguous references using the context window
35
- - Do not hallucinate entities not present in the window
36
- - Preserve factual accuracy while adding clarity`;
37
-
38
- function buildWindowPrompt(
39
- primary: CompressedObservation,
40
- before: CompressedObservation[],
41
- after: CompressedObservation[],
42
- ): string {
43
- const parts: string[] = [];
44
-
45
- if (before.length > 0) {
46
- parts.push("=== PRECEDING CONTEXT ===");
47
- for (const obs of before) {
48
- parts.push(`[${obs.type}] ${obs.title}: ${obs.narrative}`);
49
- if (obs.facts.length > 0) parts.push(`Facts: ${obs.facts.join("; ")}`);
50
- if (obs.concepts.length > 0)
51
- parts.push(`Concepts: ${obs.concepts.join(", ")}`);
52
- }
53
- }
54
-
55
- parts.push("\n=== PRIMARY OBSERVATION (enrich this) ===");
56
- parts.push(`Type: ${primary.type}`);
57
- parts.push(`Title: ${primary.title}`);
58
- if (primary.subtitle) parts.push(`Subtitle: ${primary.subtitle}`);
59
- parts.push(`Narrative: ${primary.narrative}`);
60
- if (primary.facts.length > 0)
61
- parts.push(`Facts: ${primary.facts.join("; ")}`);
62
- if (primary.concepts.length > 0)
63
- parts.push(`Concepts: ${primary.concepts.join(", ")}`);
64
- if (primary.files.length > 0)
65
- parts.push(`Files: ${primary.files.join(", ")}`);
66
-
67
- if (after.length > 0) {
68
- parts.push("\n=== FOLLOWING CONTEXT ===");
69
- for (const obs of after) {
70
- parts.push(`[${obs.type}] ${obs.title}: ${obs.narrative}`);
71
- if (obs.facts.length > 0) parts.push(`Facts: ${obs.facts.join("; ")}`);
72
- }
73
- }
74
-
75
- return parts.join("\n");
76
- }
77
-
78
- function parseEnrichedXml(xml: string): {
79
- content: string;
80
- resolvedEntities: Record<string, string>;
81
- preferences: string[];
82
- contextBridges: string[];
83
- } | null {
84
- const contentMatch = xml.match(/<content>([\s\S]*?)<\/content>/);
85
- if (!contentMatch) return null;
86
-
87
- const resolvedEntities: Record<string, string> = {};
88
- const entityRegex =
89
- /<entity\s+original="([^"]+)"\s+resolved="([^"]+)"\s*\/>/g;
90
- let match;
91
- while ((match = entityRegex.exec(xml)) !== null) {
92
- resolvedEntities[match[1]] = match[2];
93
- }
94
-
95
- const preferences: string[] = [];
96
- const prefRegex = /<preference>([^<]+)<\/preference>/g;
97
- while ((match = prefRegex.exec(xml)) !== null) {
98
- preferences.push(match[1]);
99
- }
100
-
101
- const contextBridges: string[] = [];
102
- const bridgeRegex = /<bridge>([^<]+)<\/bridge>/g;
103
- while ((match = bridgeRegex.exec(xml)) !== null) {
104
- contextBridges.push(match[1]);
105
- }
106
-
107
- return {
108
- content: contentMatch[1].trim(),
109
- resolvedEntities,
110
- preferences,
111
- contextBridges,
112
- };
113
- }
114
-
115
- export function registerSlidingWindowFunction(
116
- sdk: ISdk,
117
- kv: StateKV,
118
- provider: MemoryProvider,
119
- ): void {
120
- sdk.registerFunction(
121
- {
122
- id: "mem::enrich-window",
123
- description:
124
- "Enrich observation using sliding window context for self-containment",
125
- },
126
- async (data: {
127
- observationId: string;
128
- sessionId: string;
129
- lookback?: number;
130
- lookahead?: number;
131
- }) => {
132
- const ctx = getContext();
133
- const hprev = data.lookback ?? 3;
134
- const hnext = data.lookahead ?? 2;
135
-
136
- const allObs = await kv.list<CompressedObservation>(
137
- KV.observations(data.sessionId),
138
- );
139
- allObs.sort(
140
- (a, b) =>
141
- new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(),
142
- );
143
-
144
- const primaryIdx = allObs.findIndex((o) => o.id === data.observationId);
145
- if (primaryIdx === -1) {
146
- return { success: false, error: "Observation not found" };
147
- }
148
-
149
- const primary = allObs[primaryIdx];
150
- const before = allObs.slice(Math.max(0, primaryIdx - hprev), primaryIdx);
151
- const after = allObs.slice(primaryIdx + 1, primaryIdx + 1 + hnext);
152
-
153
- if (before.length === 0 && after.length === 0) {
154
- return {
155
- success: true,
156
- enriched: null,
157
- reason: "No adjacent context available",
158
- };
159
- }
160
-
161
- try {
162
- const prompt = buildWindowPrompt(primary, before, after);
163
- const response = await provider.compress(
164
- SLIDING_WINDOW_SYSTEM,
165
- prompt,
166
- );
167
- const parsed = parseEnrichedXml(response);
168
-
169
- if (!parsed) {
170
- ctx.logger.warn("Failed to parse enrichment XML", {
171
- obsId: data.observationId,
172
- });
173
- return { success: false, error: "parse_failed" };
174
- }
175
-
176
- const enriched: EnrichedChunk = {
177
- id: generateId("ec"),
178
- originalObsId: data.observationId,
179
- sessionId: data.sessionId,
180
- content: parsed.content,
181
- resolvedEntities: parsed.resolvedEntities,
182
- preferences: parsed.preferences,
183
- contextBridges: parsed.contextBridges,
184
- windowStart: Math.max(0, primaryIdx - hprev),
185
- windowEnd: Math.min(allObs.length - 1, primaryIdx + hnext),
186
- createdAt: new Date().toISOString(),
187
- };
188
-
189
- await kv.set(
190
- KV.enrichedChunks(data.sessionId),
191
- data.observationId,
192
- enriched,
193
- );
194
-
195
- ctx.logger.info("Observation enriched via sliding window", {
196
- obsId: data.observationId,
197
- entitiesResolved: Object.keys(parsed.resolvedEntities).length,
198
- preferencesFound: parsed.preferences.length,
199
- bridges: parsed.contextBridges.length,
200
- });
201
-
202
- return { success: true, enriched };
203
- } catch (err) {
204
- const msg = err instanceof Error ? err.message : String(err);
205
- ctx.logger.error("Sliding window enrichment failed", { error: msg });
206
- return { success: false, error: msg };
207
- }
208
- },
209
- );
210
-
211
- sdk.registerFunction(
212
- {
213
- id: "mem::enrich-session",
214
- description: "Enrich all observations in a session using sliding windows",
215
- },
216
- async (data: {
217
- sessionId: string;
218
- lookback?: number;
219
- lookahead?: number;
220
- minImportance?: number;
221
- }) => {
222
- const ctx = getContext();
223
- const allObs = await kv.list<CompressedObservation>(
224
- KV.observations(data.sessionId),
225
- );
226
- const minImp = data.minImportance ?? 4;
227
- const toEnrich = allObs.filter((o) => o.importance >= minImp);
228
-
229
- let enriched = 0;
230
- let failed = 0;
231
-
232
- for (const obs of toEnrich) {
233
- try {
234
- const result = (await sdk.trigger("mem::enrich-window", {
235
- observationId: obs.id,
236
- sessionId: data.sessionId,
237
- lookback: data.lookback ?? 3,
238
- lookahead: data.lookahead ?? 2,
239
- })) as { success?: boolean } | undefined;
240
- if (result?.success) enriched++;
241
- else failed++;
242
- } catch {
243
- failed++;
244
- }
245
- }
246
-
247
- ctx.logger.info("Session enrichment complete", {
248
- sessionId: data.sessionId,
249
- total: toEnrich.length,
250
- enriched,
251
- failed,
252
- });
253
-
254
- return { success: true, total: toEnrich.length, enriched, failed };
255
- },
256
- );
257
- }
@@ -1,115 +0,0 @@
1
- import type { ISdk } from "iii-sdk";
2
- import { getContext } from "iii-sdk";
3
- import type {
4
- CompactSearchResult,
5
- CompressedObservation,
6
- HybridSearchResult,
7
- } from "../types.js";
8
- import { KV } from "../state/schema.js";
9
- import { StateKV } from "../state/kv.js";
10
-
11
- export function registerSmartSearchFunction(
12
- sdk: ISdk,
13
- kv: StateKV,
14
- searchFn: (query: string, limit: number) => Promise<HybridSearchResult[]>,
15
- ): void {
16
- sdk.registerFunction(
17
- {
18
- id: "mem::smart-search",
19
- description:
20
- "Search with progressive disclosure: compact results first, expand specific IDs for full details",
21
- },
22
- async (data: {
23
- query?: string;
24
- expandIds?: Array<string | { obsId: string; sessionId: string }>;
25
- limit?: number;
26
- }) => {
27
- const ctx = getContext();
28
-
29
- if (data.expandIds && data.expandIds.length > 0) {
30
- const raw = data.expandIds.slice(0, 20);
31
- const items = raw.map((entry) => {
32
- if (typeof entry === "string") return { obsId: entry, sessionId: undefined as string | undefined };
33
- if (entry && typeof entry === "object" && typeof (entry as any).obsId === "string") {
34
- return { obsId: (entry as any).obsId, sessionId: (entry as any).sessionId as string | undefined };
35
- }
36
- return null;
37
- }).filter((item): item is NonNullable<typeof item> => item !== null);
38
-
39
- const expanded: Array<{
40
- obsId: string;
41
- sessionId: string;
42
- observation: CompressedObservation;
43
- }> = [];
44
-
45
- const results = await Promise.all(
46
- items.map(({ obsId, sessionId }) =>
47
- findObservation(kv, obsId, sessionId).then((obs) =>
48
- obs ? { obsId, sessionId: obs.sessionId, observation: obs } : null,
49
- ),
50
- ),
51
- );
52
- for (const r of results) {
53
- if (r) expanded.push(r);
54
- }
55
-
56
- const truncated = data.expandIds.length > raw.length;
57
- ctx.logger.info("Smart search expanded", {
58
- requested: data.expandIds.length,
59
- attempted: raw.length,
60
- returned: expanded.length,
61
- truncated,
62
- });
63
- return { mode: "expanded", results: expanded, truncated };
64
- }
65
-
66
- if (!data.query || typeof data.query !== "string" || !data.query.trim()) {
67
- return { mode: "compact", results: [], error: "query is required" };
68
- }
69
-
70
- const limit = Math.max(1, Math.min(data.limit ?? 20, 100));
71
- const hybridResults = await searchFn(data.query, limit);
72
-
73
- const compact: CompactSearchResult[] = hybridResults.map((r) => ({
74
- obsId: r.observation.id,
75
- sessionId: r.sessionId,
76
- title: r.observation.title,
77
- type: r.observation.type,
78
- score: r.combinedScore,
79
- timestamp: r.observation.timestamp,
80
- }));
81
-
82
- ctx.logger.info("Smart search compact", {
83
- query: data.query,
84
- results: compact.length,
85
- });
86
- return { mode: "compact", results: compact };
87
- },
88
- );
89
- }
90
-
91
- async function findObservation(
92
- kv: StateKV,
93
- obsId: string,
94
- sessionIdHint?: string,
95
- ): Promise<CompressedObservation | null> {
96
- if (sessionIdHint) {
97
- const obs = await kv
98
- .get<CompressedObservation>(KV.observations(sessionIdHint), obsId)
99
- .catch(() => null);
100
- if (obs) return obs;
101
- }
102
-
103
- const sessions = await kv.list<{ id: string }>(KV.sessions);
104
- for (let i = 0; i < sessions.length; i += 5) {
105
- const batch = sessions.slice(i, i + 5);
106
- const results = await Promise.all(
107
- batch.map((s) =>
108
- kv.get<CompressedObservation>(KV.observations(s.id), obsId).catch(() => null),
109
- ),
110
- );
111
- const found = results.find((r) => r !== null);
112
- if (found) return found;
113
- }
114
- return null;
115
- }