@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.
- package/.claude-plugin/marketplace.json +14 -0
- package/.github/workflows/ci.yml +22 -0
- package/.github/workflows/publish.yml +28 -0
- package/AGENTS.md +113 -0
- package/LICENSE +190 -0
- package/README.md +828 -0
- package/assets/banner.png +0 -0
- package/assets/demo.gif +0 -0
- package/assets/demo.mp4 +0 -0
- package/benchmark/QUALITY.md +73 -0
- package/benchmark/REAL-EMBEDDINGS.md +67 -0
- package/benchmark/SCALE.md +110 -0
- package/benchmark/dataset.ts +293 -0
- package/benchmark/quality-eval.ts +643 -0
- package/benchmark/real-embeddings-eval.ts +405 -0
- package/benchmark/scale-eval.ts +398 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +137 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/docker-compose.yml +14 -0
- package/dist/hooks/notification.d.mts +1 -0
- package/dist/hooks/notification.mjs +45 -0
- package/dist/hooks/notification.mjs.map +1 -0
- package/dist/hooks/post-tool-failure.d.mts +1 -0
- package/dist/hooks/post-tool-failure.mjs +45 -0
- package/dist/hooks/post-tool-failure.mjs.map +1 -0
- package/dist/hooks/post-tool-use.d.mts +1 -0
- package/dist/hooks/post-tool-use.mjs +53 -0
- package/dist/hooks/post-tool-use.mjs.map +1 -0
- package/dist/hooks/pre-compact.d.mts +1 -0
- package/dist/hooks/pre-compact.mjs +50 -0
- package/dist/hooks/pre-compact.mjs.map +1 -0
- package/dist/hooks/pre-tool-use.d.mts +1 -0
- package/dist/hooks/pre-tool-use.mjs +69 -0
- package/dist/hooks/pre-tool-use.mjs.map +1 -0
- package/dist/hooks/prompt-submit.d.mts +1 -0
- package/dist/hooks/prompt-submit.mjs +40 -0
- package/dist/hooks/prompt-submit.mjs.map +1 -0
- package/dist/hooks/session-end.d.mts +1 -0
- package/dist/hooks/session-end.mjs +61 -0
- package/dist/hooks/session-end.mjs.map +1 -0
- package/dist/hooks/session-start.d.mts +1 -0
- package/dist/hooks/session-start.mjs +42 -0
- package/dist/hooks/session-start.mjs.map +1 -0
- package/dist/hooks/stop.d.mts +1 -0
- package/dist/hooks/stop.mjs +33 -0
- package/dist/hooks/stop.mjs.map +1 -0
- package/dist/hooks/subagent-start.d.mts +1 -0
- package/dist/hooks/subagent-start.mjs +43 -0
- package/dist/hooks/subagent-start.mjs.map +1 -0
- package/dist/hooks/subagent-stop.d.mts +1 -0
- package/dist/hooks/subagent-stop.mjs +45 -0
- package/dist/hooks/subagent-stop.mjs.map +1 -0
- package/dist/hooks/task-completed.d.mts +1 -0
- package/dist/hooks/task-completed.mjs +46 -0
- package/dist/hooks/task-completed.mjs.map +1 -0
- package/dist/iii-config.yaml +51 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +13776 -0
- package/dist/index.mjs.map +1 -0
- package/dist/src-QxitMPfJ.mjs +13775 -0
- package/dist/src-QxitMPfJ.mjs.map +1 -0
- package/dist/standalone.d.mts +1 -0
- package/dist/standalone.mjs +1155 -0
- package/dist/standalone.mjs.map +1 -0
- package/dist/transformers-BX_tgxdO.mjs +38684 -0
- package/dist/transformers-BX_tgxdO.mjs.map +1 -0
- package/dist/transformers-KMm1i9no.mjs +38683 -0
- package/dist/transformers-KMm1i9no.mjs.map +1 -0
- package/docker-compose.yml +14 -0
- package/iii-config.yaml +51 -0
- package/package.json +59 -0
- package/plugin/.claude-plugin/plugin.json +10 -0
- package/plugin/hooks/hooks.json +77 -0
- package/plugin/scripts/diagnostics.mjs +551 -0
- package/plugin/scripts/notification.mjs +45 -0
- package/plugin/scripts/post-tool-failure.mjs +45 -0
- package/plugin/scripts/post-tool-use.mjs +53 -0
- package/plugin/scripts/pre-compact.mjs +50 -0
- package/plugin/scripts/pre-tool-use.mjs +69 -0
- package/plugin/scripts/prompt-submit.mjs +40 -0
- package/plugin/scripts/session-end.mjs +61 -0
- package/plugin/scripts/session-start.mjs +42 -0
- package/plugin/scripts/stop.mjs +33 -0
- package/plugin/scripts/subagent-start.mjs +43 -0
- package/plugin/scripts/subagent-stop.mjs +45 -0
- package/plugin/scripts/task-completed.mjs +46 -0
- package/plugin/skills/forget/SKILL.md +32 -0
- package/plugin/skills/recall/SKILL.md +18 -0
- package/plugin/skills/remember/SKILL.md +25 -0
- package/plugin/skills/session-history/SKILL.md +17 -0
- package/src/auth.ts +12 -0
- package/src/cli.ts +159 -0
- package/src/config.ts +221 -0
- package/src/eval/metrics-store.ts +65 -0
- package/src/eval/quality.ts +51 -0
- package/src/eval/schemas.ts +124 -0
- package/src/eval/self-correct.ts +28 -0
- package/src/eval/validator.ts +31 -0
- package/src/functions/actions.ts +288 -0
- package/src/functions/audit.ts +61 -0
- package/src/functions/auto-forget.ts +169 -0
- package/src/functions/branch-aware.ts +169 -0
- package/src/functions/cascade.ts +80 -0
- package/src/functions/checkpoints.ts +209 -0
- package/src/functions/claude-bridge.ts +161 -0
- package/src/functions/compress.ts +194 -0
- package/src/functions/consolidate.ts +212 -0
- package/src/functions/consolidation-pipeline.ts +258 -0
- package/src/functions/context.ts +169 -0
- package/src/functions/crystallize.ts +293 -0
- package/src/functions/dedup.ts +57 -0
- package/src/functions/diagnostics.ts +785 -0
- package/src/functions/enrich.ts +132 -0
- package/src/functions/evict.ts +163 -0
- package/src/functions/export-import.ts +508 -0
- package/src/functions/facets.ts +248 -0
- package/src/functions/file-index.ts +106 -0
- package/src/functions/flow-compress.ts +214 -0
- package/src/functions/frontier.ts +196 -0
- package/src/functions/governance.ts +131 -0
- package/src/functions/graph-retrieval.ts +277 -0
- package/src/functions/graph.ts +275 -0
- package/src/functions/leases.ts +216 -0
- package/src/functions/lessons.ts +253 -0
- package/src/functions/mesh.ts +434 -0
- package/src/functions/migrate.ts +165 -0
- package/src/functions/observe.ts +144 -0
- package/src/functions/obsidian-export.ts +310 -0
- package/src/functions/patterns.ts +138 -0
- package/src/functions/privacy.ts +39 -0
- package/src/functions/profile.ts +155 -0
- package/src/functions/query-expansion.ts +186 -0
- package/src/functions/relations.ts +237 -0
- package/src/functions/remember.ts +162 -0
- package/src/functions/retention.ts +235 -0
- package/src/functions/routines.ts +289 -0
- package/src/functions/search.ts +80 -0
- package/src/functions/sentinels.ts +417 -0
- package/src/functions/signals.ts +186 -0
- package/src/functions/sketches.ts +274 -0
- package/src/functions/sliding-window.ts +257 -0
- package/src/functions/smart-search.ts +115 -0
- package/src/functions/snapshot.ts +219 -0
- package/src/functions/summarize.ts +155 -0
- package/src/functions/team.ts +147 -0
- package/src/functions/temporal-graph.ts +476 -0
- package/src/functions/timeline.ts +138 -0
- package/src/functions/verify.ts +117 -0
- package/src/health/monitor.ts +110 -0
- package/src/health/thresholds.ts +73 -0
- package/src/hooks/notification.ts +52 -0
- package/src/hooks/post-tool-failure.ts +58 -0
- package/src/hooks/post-tool-use.ts +62 -0
- package/src/hooks/pre-compact.ts +60 -0
- package/src/hooks/pre-tool-use.ts +72 -0
- package/src/hooks/prompt-submit.ts +46 -0
- package/src/hooks/session-end.ts +71 -0
- package/src/hooks/session-start.ts +48 -0
- package/src/hooks/stop.ts +39 -0
- package/src/hooks/subagent-start.ts +49 -0
- package/src/hooks/subagent-stop.ts +54 -0
- package/src/hooks/task-completed.ts +54 -0
- package/src/index.ts +342 -0
- package/src/mcp/in-memory-kv.ts +61 -0
- package/src/mcp/server.ts +1455 -0
- package/src/mcp/standalone.ts +177 -0
- package/src/mcp/tools-registry.ts +769 -0
- package/src/mcp/transport.ts +91 -0
- package/src/prompts/compression.ts +67 -0
- package/src/prompts/consolidation.ts +48 -0
- package/src/prompts/graph-extraction.ts +35 -0
- package/src/prompts/summary.ts +38 -0
- package/src/prompts/xml.ts +26 -0
- package/src/providers/agent-sdk.ts +34 -0
- package/src/providers/anthropic.ts +35 -0
- package/src/providers/circuit-breaker.ts +82 -0
- package/src/providers/embedding/cohere.ts +46 -0
- package/src/providers/embedding/gemini.ts +54 -0
- package/src/providers/embedding/index.ts +39 -0
- package/src/providers/embedding/local.ts +52 -0
- package/src/providers/embedding/openai.ts +45 -0
- package/src/providers/embedding/openrouter.ts +51 -0
- package/src/providers/embedding/voyage.ts +46 -0
- package/src/providers/fallback-chain.ts +31 -0
- package/src/providers/index.ts +84 -0
- package/src/providers/openrouter.ts +71 -0
- package/src/providers/resilient.ts +37 -0
- package/src/state/hybrid-search.ts +295 -0
- package/src/state/index-persistence.ts +63 -0
- package/src/state/keyed-mutex.ts +18 -0
- package/src/state/kv.ts +33 -0
- package/src/state/schema.ts +71 -0
- package/src/state/search-index.ts +245 -0
- package/src/state/stemmer.ts +104 -0
- package/src/state/synonyms.ts +63 -0
- package/src/state/vector-index.ts +130 -0
- package/src/telemetry/setup.ts +116 -0
- package/src/triggers/api.ts +1904 -0
- package/src/triggers/events.ts +71 -0
- package/src/types.ts +769 -0
- package/src/version.ts +1 -0
- package/src/viewer/index.html +2497 -0
- package/src/viewer/server.ts +207 -0
- package/src/xenova.d.ts +3 -0
- package/test/actions.test.ts +490 -0
- package/test/audit.test.ts +108 -0
- package/test/auto-forget.test.ts +188 -0
- package/test/cascade.test.ts +277 -0
- package/test/checkpoints.test.ts +493 -0
- package/test/circuit-breaker.test.ts +107 -0
- package/test/claude-bridge.test.ts +178 -0
- package/test/confidence.test.ts +247 -0
- package/test/consistency.test.ts +61 -0
- package/test/consolidation-pipeline.test.ts +251 -0
- package/test/crystallize.test.ts +521 -0
- package/test/diagnostics.test.ts +638 -0
- package/test/embedding-provider.test.ts +49 -0
- package/test/enrich.test.ts +209 -0
- package/test/eval.test.ts +300 -0
- package/test/export-import.test.ts +251 -0
- package/test/facets.test.ts +448 -0
- package/test/fallback-chain.test.ts +93 -0
- package/test/frontier.test.ts +485 -0
- package/test/governance.test.ts +147 -0
- package/test/graph-retrieval.test.ts +186 -0
- package/test/graph.test.ts +160 -0
- package/test/helpers/mocks.ts +40 -0
- package/test/hybrid-search.test.ts +145 -0
- package/test/index-persistence.test.ts +124 -0
- package/test/integration.test.ts +265 -0
- package/test/leases.test.ts +399 -0
- package/test/mcp-prompts.test.ts +218 -0
- package/test/mcp-resources.test.ts +286 -0
- package/test/mcp-standalone.test.ts +113 -0
- package/test/mesh.test.ts +700 -0
- package/test/privacy.test.ts +87 -0
- package/test/profile.test.ts +161 -0
- package/test/query-expansion.test.ts +154 -0
- package/test/relations.test.ts +198 -0
- package/test/retention.test.ts +245 -0
- package/test/routines.test.ts +497 -0
- package/test/schema-fingerprint.test.ts +81 -0
- package/test/schema.test.ts +42 -0
- package/test/search-index.test.ts +128 -0
- package/test/sentinels.test.ts +626 -0
- package/test/signals.test.ts +410 -0
- package/test/sketches.test.ts +549 -0
- package/test/sliding-window.test.ts +199 -0
- package/test/smart-search.test.ts +169 -0
- package/test/snapshot.test.ts +165 -0
- package/test/team.test.ts +156 -0
- package/test/temporal-graph.test.ts +378 -0
- package/test/timeline.test.ts +148 -0
- package/test/vector-index.test.ts +79 -0
- package/test/verify.test.ts +209 -0
- package/test/xml.test.ts +65 -0
- package/tsconfig.json +22 -0
- package/tsdown.config.ts +62 -0
|
@@ -0,0 +1,274 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
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
|
+
}
|