@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,253 +0,0 @@
1
- import type { ISdk } from "iii-sdk";
2
- import type { StateKV } from "../state/kv.js";
3
- import { KV, fingerprintId } from "../state/schema.js";
4
- import type { Lesson } from "../types.js";
5
- import { recordAudit } from "./audit.js";
6
-
7
- function reinforceLesson(lesson: Lesson): void {
8
- const now = new Date().toISOString();
9
- lesson.reinforcements++;
10
- lesson.confidence = Math.min(
11
- 1.0,
12
- lesson.confidence + 0.1 * (1 - lesson.confidence),
13
- );
14
- lesson.lastReinforcedAt = now;
15
- lesson.updatedAt = now;
16
- }
17
-
18
- export function registerLessonsFunctions(sdk: ISdk, kv: StateKV): void {
19
- sdk.registerFunction(
20
- { id: "mem::lesson-save" },
21
- async (data: {
22
- content: string;
23
- context?: string;
24
- confidence?: number;
25
- project?: string;
26
- tags?: string[];
27
- source?: "crystal" | "manual" | "consolidation";
28
- sourceIds?: string[];
29
- }) => {
30
- if (!data.content?.trim()) {
31
- return { success: false, error: "content is required" };
32
- }
33
-
34
- const fp = fingerprintId("lsn", data.content.trim().toLowerCase());
35
- const existing = await kv.get<Lesson>(KV.lessons, fp);
36
-
37
- if (existing && !existing.deleted) {
38
- reinforceLesson(existing);
39
- if (data.context && !existing.context) {
40
- existing.context = data.context;
41
- }
42
- await kv.set(KV.lessons, existing.id, existing);
43
-
44
- try {
45
- await recordAudit(kv, "lesson_strengthen", "mem::lesson-save", [
46
- existing.id,
47
- ]);
48
- } catch {}
49
-
50
- return {
51
- success: true,
52
- action: "strengthened",
53
- lesson: existing,
54
- };
55
- }
56
-
57
- const confidence =
58
- typeof data.confidence === "number" &&
59
- data.confidence >= 0 &&
60
- data.confidence <= 1
61
- ? data.confidence
62
- : 0.5;
63
-
64
- const now = new Date().toISOString();
65
- const lesson: Lesson = {
66
- id: fp,
67
- content: data.content.trim(),
68
- context: data.context?.trim() || "",
69
- confidence,
70
- reinforcements: 0,
71
- source: data.source || "manual",
72
- sourceIds: data.sourceIds || [],
73
- project: data.project,
74
- tags: data.tags || [],
75
- createdAt: now,
76
- updatedAt: now,
77
- decayRate: 0.05,
78
- };
79
-
80
- await kv.set(KV.lessons, lesson.id, lesson);
81
-
82
- try {
83
- await recordAudit(kv, "lesson_save", "mem::lesson-save", [lesson.id]);
84
- } catch {}
85
-
86
- return { success: true, action: "created", lesson };
87
- },
88
- );
89
-
90
- sdk.registerFunction(
91
- { id: "mem::lesson-recall" },
92
- async (data: {
93
- query: string;
94
- project?: string;
95
- minConfidence?: number;
96
- limit?: number;
97
- }) => {
98
- if (!data.query?.trim()) {
99
- return { success: false, error: "query is required" };
100
- }
101
-
102
- const query = data.query.toLowerCase();
103
- const minConfidence = data.minConfidence ?? 0.1;
104
- const limit = data.limit ?? 10;
105
-
106
- let lessons = await kv.list<Lesson>(KV.lessons);
107
-
108
- lessons = lessons.filter(
109
- (l) => !l.deleted && l.confidence >= minConfidence,
110
- );
111
-
112
- if (data.project) {
113
- lessons = lessons.filter((l) => l.project === data.project);
114
- }
115
-
116
- const scored = lessons
117
- .map((l) => {
118
- const text = `${l.content} ${l.context} ${l.tags.join(" ")}`.toLowerCase();
119
- const terms = query.split(/\s+/).filter((t) => t.length > 1);
120
- const matchCount = terms.filter((t) => text.includes(t)).length;
121
- if (matchCount === 0) return null;
122
-
123
- const relevance = matchCount / terms.length;
124
- const daysSinceReinforced = l.lastReinforcedAt
125
- ? (Date.now() - new Date(l.lastReinforcedAt).getTime()) /
126
- (1000 * 60 * 60 * 24)
127
- : (Date.now() - new Date(l.createdAt).getTime()) /
128
- (1000 * 60 * 60 * 24);
129
- const recencyBoost = 1 / (1 + daysSinceReinforced * 0.01);
130
- const score = l.confidence * relevance * recencyBoost;
131
-
132
- return { lesson: l, score };
133
- })
134
- .filter(Boolean) as Array<{ lesson: Lesson; score: number }>;
135
-
136
- scored.sort((a, b) => b.score - a.score);
137
-
138
- try {
139
- await recordAudit(kv, "lesson_recall", "mem::lesson-recall", [], {
140
- query: data.query,
141
- resultCount: scored.length,
142
- });
143
- } catch {}
144
-
145
- return {
146
- success: true,
147
- lessons: scored.slice(0, limit).map((s) => ({
148
- ...s.lesson,
149
- score: Math.round(s.score * 1000) / 1000,
150
- })),
151
- };
152
- },
153
- );
154
-
155
- sdk.registerFunction(
156
- { id: "mem::lesson-list" },
157
- async (data: {
158
- project?: string;
159
- source?: string;
160
- minConfidence?: number;
161
- limit?: number;
162
- }) => {
163
- const limit = data.limit ?? 50;
164
- const minConfidence = data.minConfidence ?? 0;
165
- let lessons = await kv.list<Lesson>(KV.lessons);
166
-
167
- lessons = lessons.filter(
168
- (l) => !l.deleted && l.confidence >= minConfidence,
169
- );
170
-
171
- if (data.project) {
172
- lessons = lessons.filter((l) => l.project === data.project);
173
- }
174
- if (data.source) {
175
- lessons = lessons.filter((l) => l.source === data.source);
176
- }
177
-
178
- lessons.sort((a, b) => b.confidence - a.confidence);
179
-
180
- return { success: true, lessons: lessons.slice(0, limit) };
181
- },
182
- );
183
-
184
- sdk.registerFunction(
185
- { id: "mem::lesson-strengthen" },
186
- async (data: { lessonId: string }) => {
187
- if (!data.lessonId) {
188
- return { success: false, error: "lessonId is required" };
189
- }
190
-
191
- const lesson = await kv.get<Lesson>(KV.lessons, data.lessonId);
192
- if (!lesson || lesson.deleted) {
193
- return { success: false, error: "lesson not found" };
194
- }
195
-
196
- reinforceLesson(lesson);
197
-
198
- await kv.set(KV.lessons, lesson.id, lesson);
199
-
200
- try {
201
- await recordAudit(kv, "lesson_strengthen", "mem::lesson-strengthen", [
202
- lesson.id,
203
- ]);
204
- } catch {}
205
-
206
- return { success: true, lesson };
207
- },
208
- );
209
-
210
- sdk.registerFunction(
211
- { id: "mem::lesson-decay-sweep" },
212
- async () => {
213
- const lessons = await kv.list<Lesson>(KV.lessons);
214
- let decayed = 0;
215
- let softDeleted = 0;
216
- const now = Date.now();
217
- const timestamp = new Date().toISOString();
218
- const dirty: Lesson[] = [];
219
-
220
- for (const lesson of lessons) {
221
- if (lesson.deleted) continue;
222
-
223
- const baseline = lesson.lastDecayedAt || lesson.lastReinforcedAt || lesson.createdAt;
224
- const weeksSinceBaseline =
225
- (now - new Date(baseline).getTime()) / (1000 * 60 * 60 * 24 * 7);
226
-
227
- if (weeksSinceBaseline < 1) continue;
228
-
229
- const decay = lesson.decayRate * weeksSinceBaseline;
230
- const newConfidence = Math.max(0.05, lesson.confidence - decay);
231
-
232
- if (newConfidence !== lesson.confidence) {
233
- lesson.confidence = Math.round(newConfidence * 1000) / 1000;
234
- lesson.lastDecayedAt = timestamp;
235
- lesson.updatedAt = timestamp;
236
-
237
- if (lesson.confidence <= 0.1 && lesson.reinforcements === 0) {
238
- lesson.deleted = true;
239
- softDeleted++;
240
- } else {
241
- decayed++;
242
- }
243
-
244
- dirty.push(lesson);
245
- }
246
- }
247
-
248
- await Promise.all(dirty.map((l) => kv.set(KV.lessons, l.id, l)));
249
-
250
- return { success: true, decayed, softDeleted, total: lessons.length };
251
- },
252
- );
253
- }
@@ -1,434 +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 {
6
- MeshPeer,
7
- Memory,
8
- Action,
9
- SemanticMemory,
10
- ProceduralMemory,
11
- MemoryRelation,
12
- GraphNode,
13
- GraphEdge,
14
- } from "../types.js";
15
- import { lookup } from "node:dns/promises";
16
- import { isIP } from "node:net";
17
-
18
- function isPrivateIP(ip: string): boolean {
19
- if (ip === "127.0.0.1" || ip === "::1" || ip === "0.0.0.0") return true;
20
- if (ip.startsWith("10.") || ip.startsWith("192.168.")) return true;
21
- if (/^172\.(1[6-9]|2\d|3[01])\./.test(ip)) return true;
22
- if (ip === "169.254.169.254") return true;
23
- if (ip.startsWith("fe80:") || ip.startsWith("fc00:") || ip.startsWith("fd")) return true;
24
- if (ip.startsWith("::ffff:")) {
25
- const v4 = ip.slice(7);
26
- return isPrivateIP(v4);
27
- }
28
- return false;
29
- }
30
-
31
- async function isAllowedUrl(urlStr: string): Promise<boolean> {
32
- try {
33
- const parsed = new URL(urlStr);
34
- if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return false;
35
- if (parsed.username || parsed.password) return false;
36
- const host = parsed.hostname.toLowerCase();
37
-
38
- if (host === "localhost") return false;
39
- if (isIP(host) && isPrivateIP(host)) return false;
40
-
41
- if (!isIP(host)) {
42
- try {
43
- const resolved = await lookup(host, { all: true });
44
- if (resolved.some((r) => isPrivateIP(r.address))) return false;
45
- } catch {
46
- // DNS resolution failed — allow the URL (the actual fetch will fail if unreachable)
47
- }
48
- }
49
-
50
- return true;
51
- } catch {
52
- return false;
53
- }
54
- }
55
-
56
- const DEFAULT_SHARED_SCOPES = [
57
- "memories",
58
- "actions",
59
- "semantic",
60
- "procedural",
61
- "relations",
62
- "graph:nodes",
63
- "graph:edges",
64
- ];
65
-
66
- interface MeshSyncPayload {
67
- memories?: Memory[];
68
- actions?: Action[];
69
- semantic?: SemanticMemory[];
70
- procedural?: ProceduralMemory[];
71
- relations?: MemoryRelation[];
72
- graphNodes?: GraphNode[];
73
- graphEdges?: GraphEdge[];
74
- }
75
-
76
- async function lwwMergeList<T extends { id: string }>(
77
- kv: StateKV,
78
- scope: string,
79
- items: T[] | undefined,
80
- lockPrefix: string,
81
- tsField: "updatedAt" | "createdAt",
82
- ): Promise<number> {
83
- if (!items || !Array.isArray(items)) return 0;
84
- let count = 0;
85
- for (const item of items) {
86
- if (!item.id || typeof item.id !== "string") continue;
87
- const ts = (item as Record<string, unknown>)[tsField];
88
- if (typeof ts !== "string" || Number.isNaN(new Date(ts).getTime())) continue;
89
- const wrote = await withKeyedLock(`${lockPrefix}:${item.id}`, async () => {
90
- const existing = await kv.get<T>(scope, item.id);
91
- if (!existing) {
92
- await kv.set(scope, item.id, item);
93
- return true;
94
- }
95
- const existingTs = (existing as Record<string, unknown>)[tsField] as string;
96
- if (new Date(ts) > new Date(existingTs)) {
97
- await kv.set(scope, item.id, item);
98
- return true;
99
- }
100
- return false;
101
- });
102
- if (wrote) count++;
103
- }
104
- return count;
105
- }
106
-
107
- function graphNodeTs(node: GraphNode): string {
108
- return node.updatedAt || node.createdAt;
109
- }
110
-
111
- async function lwwMergeGraphNodes(
112
- kv: StateKV,
113
- items: GraphNode[] | undefined,
114
- ): Promise<number> {
115
- if (!items || !Array.isArray(items)) return 0;
116
- let count = 0;
117
- for (const item of items) {
118
- if (!item.id || typeof item.id !== "string") continue;
119
- const ts = graphNodeTs(item);
120
- if (!ts || Number.isNaN(new Date(ts).getTime())) continue;
121
- const wrote = await withKeyedLock(`mem:gnode:${item.id}`, async () => {
122
- const existing = await kv.get<GraphNode>(KV.graphNodes, item.id);
123
- if (!existing) {
124
- await kv.set(KV.graphNodes, item.id, item);
125
- return true;
126
- }
127
- if (new Date(ts) > new Date(graphNodeTs(existing))) {
128
- await kv.set(KV.graphNodes, item.id, item);
129
- return true;
130
- }
131
- return false;
132
- });
133
- if (wrote) count++;
134
- }
135
- return count;
136
- }
137
-
138
- export function registerMeshFunction(sdk: ISdk, kv: StateKV): void {
139
- sdk.registerFunction(
140
- { id: "mem::mesh-register" },
141
- async (data: {
142
- url: string;
143
- name: string;
144
- sharedScopes?: string[];
145
- syncFilter?: { project?: string };
146
- }) => {
147
- if (!data.url || !data.name) {
148
- return { success: false, error: "url and name are required" };
149
- }
150
-
151
- if (!(await isAllowedUrl(data.url))) {
152
- return { success: false, error: "URL blocked: private/local address not allowed" };
153
- }
154
-
155
- const existing = await kv.list<MeshPeer>(KV.mesh);
156
- const duplicate = existing.find((p) => p.url === data.url);
157
- if (duplicate) {
158
- return { success: false, error: "peer already registered", peerId: duplicate.id };
159
- }
160
-
161
- const peer: MeshPeer = {
162
- id: generateId("peer"),
163
- url: data.url,
164
- name: data.name,
165
- status: "disconnected",
166
- sharedScopes: data.sharedScopes || DEFAULT_SHARED_SCOPES,
167
- syncFilter: data.syncFilter,
168
- };
169
-
170
- await kv.set(KV.mesh, peer.id, peer);
171
- return { success: true, peer };
172
- },
173
- );
174
-
175
- sdk.registerFunction(
176
- { id: "mem::mesh-list" },
177
- async () => {
178
- const peers = await kv.list<MeshPeer>(KV.mesh);
179
- return { success: true, peers };
180
- },
181
- );
182
-
183
- sdk.registerFunction(
184
- { id: "mem::mesh-sync" },
185
- async (data: { peerId?: string; scopes?: string[]; direction?: "push" | "pull" | "both" }) => {
186
- const direction = data.direction || "both";
187
- let peers: MeshPeer[];
188
-
189
- if (data.peerId) {
190
- const peer = await kv.get<MeshPeer>(KV.mesh, data.peerId);
191
- if (!peer) return { success: false, error: "peer not found" };
192
- peers = [peer];
193
- } else {
194
- peers = await kv.list<MeshPeer>(KV.mesh);
195
- }
196
-
197
- const results: Array<{
198
- peerId: string;
199
- peerName: string;
200
- pushed: number;
201
- pulled: number;
202
- errors: string[];
203
- }> = [];
204
-
205
- for (const peer of peers) {
206
- const result = {
207
- peerId: peer.id,
208
- peerName: peer.name,
209
- pushed: 0,
210
- pulled: 0,
211
- errors: [] as string[],
212
- };
213
-
214
- peer.status = "syncing";
215
- await kv.set(KV.mesh, peer.id, peer);
216
-
217
- const scopes = data.scopes || peer.sharedScopes;
218
-
219
- try {
220
- if (!(await isAllowedUrl(peer.url))) {
221
- result.errors.push("peer URL blocked: private/local address not allowed");
222
- peer.status = "error";
223
- await kv.set(KV.mesh, peer.id, peer);
224
- results.push(result);
225
- continue;
226
- }
227
-
228
- if (direction === "push" || direction === "both") {
229
- const pushData = await collectSyncData(kv, scopes, peer.lastSyncAt, peer.syncFilter);
230
- try {
231
- const response = await fetch(`${peer.url}/agentmemory/mesh/receive`, {
232
- method: "POST",
233
- headers: { "Content-Type": "application/json" },
234
- body: JSON.stringify(pushData),
235
- signal: AbortSignal.timeout(30000),
236
- redirect: "error",
237
- });
238
- if (response.ok) {
239
- const body = (await response.json()) as { accepted: number };
240
- result.pushed = body.accepted || 0;
241
- } else {
242
- result.errors.push(`push failed: HTTP ${response.status}`);
243
- }
244
- } catch (err) {
245
- result.errors.push(`push failed: ${String(err)}`);
246
- }
247
- }
248
-
249
- if (direction === "pull" || direction === "both") {
250
- try {
251
- const response = await fetch(
252
- `${peer.url}/agentmemory/mesh/export?since=${peer.lastSyncAt || ""}`,
253
- { signal: AbortSignal.timeout(30000), redirect: "error" },
254
- );
255
- if (response.ok) {
256
- const pullData = (await response.json()) as {
257
- memories?: Memory[];
258
- actions?: Action[];
259
- };
260
- result.pulled = await applySyncData(kv, pullData, scopes);
261
- } else {
262
- result.errors.push(`pull failed: HTTP ${response.status}`);
263
- }
264
- } catch (err) {
265
- result.errors.push(`pull failed: ${String(err)}`);
266
- }
267
- }
268
-
269
- peer.status = result.errors.length > 0 ? "error" : "connected";
270
- if (result.errors.length === 0) {
271
- peer.lastSyncAt = new Date().toISOString();
272
- }
273
- } catch (err) {
274
- peer.status = "disconnected";
275
- result.errors.push(String(err));
276
- }
277
-
278
- await kv.set(KV.mesh, peer.id, peer);
279
- results.push(result);
280
- }
281
-
282
- return { success: true, results };
283
- },
284
- );
285
-
286
- sdk.registerFunction(
287
- { id: "mem::mesh-receive" },
288
- async (data: MeshSyncPayload) => {
289
- let accepted = 0;
290
-
291
- accepted += await lwwMergeList(kv, KV.memories, data.memories, "mem:memory", "updatedAt");
292
- accepted += await lwwMergeList(kv, KV.actions, data.actions, "mem:action", "updatedAt");
293
- accepted += await lwwMergeList(kv, KV.semantic, data.semantic, "mem:semantic", "updatedAt");
294
- accepted += await lwwMergeList(kv, KV.procedural, data.procedural, "mem:procedural", "updatedAt");
295
- if (data.relations && Array.isArray(data.relations)) {
296
- for (const rel of data.relations) {
297
- if (!rel.sourceId || !rel.targetId || !rel.type) continue;
298
- const relKey = `${rel.sourceId}:${rel.targetId}:${rel.type}`;
299
- await withKeyedLock(`mem:relation:${relKey}`, async () => {
300
- const existing = await kv.get<MemoryRelation>(KV.relations, relKey);
301
- if (!existing) {
302
- await kv.set(KV.relations, relKey, rel);
303
- accepted++;
304
- }
305
- });
306
- }
307
- }
308
- accepted += await lwwMergeGraphNodes(kv, data.graphNodes);
309
- accepted += await lwwMergeList(kv, KV.graphEdges, data.graphEdges, "mem:gedge", "createdAt");
310
-
311
- return { success: true, accepted };
312
- },
313
- );
314
-
315
- sdk.registerFunction(
316
- { id: "mem::mesh-remove" },
317
- async (data: { peerId: string }) => {
318
- if (!data.peerId) {
319
- return { success: false, error: "peerId is required" };
320
- }
321
- await kv.delete(KV.mesh, data.peerId);
322
- return { success: true };
323
- },
324
- );
325
- }
326
-
327
- function deltaFilter<T>(
328
- items: T[],
329
- sinceTime: number,
330
- tsField: "updatedAt" | "createdAt",
331
- ): T[] {
332
- return items.filter(
333
- (item) => new Date((item as Record<string, unknown>)[tsField] as string).getTime() > sinceTime,
334
- );
335
- }
336
-
337
- async function collectSyncData(
338
- kv: StateKV,
339
- scopes: string[],
340
- since?: string,
341
- syncFilter?: { project?: string },
342
- ): Promise<MeshSyncPayload> {
343
- const result: MeshSyncPayload = {};
344
- const parsed = since ? new Date(since).getTime() : 0;
345
- const sinceTime = Number.isNaN(parsed) ? 0 : parsed;
346
-
347
- if (scopes.includes("memories")) {
348
- const all = await kv.list<Memory>(KV.memories);
349
- result.memories = deltaFilter(all, sinceTime, "updatedAt");
350
- }
351
-
352
- if (scopes.includes("actions")) {
353
- let all = await kv.list<Action>(KV.actions);
354
- if (syncFilter?.project) {
355
- all = all.filter((a) => a.project === syncFilter.project);
356
- }
357
- result.actions = deltaFilter(all, sinceTime, "updatedAt");
358
- }
359
-
360
- const projectScoped = !!syncFilter?.project;
361
-
362
- if (scopes.includes("semantic") && !projectScoped) {
363
- const all = await kv.list<SemanticMemory>(KV.semantic);
364
- result.semantic = deltaFilter(all, sinceTime, "updatedAt");
365
- }
366
-
367
- if (scopes.includes("procedural") && !projectScoped) {
368
- const all = await kv.list<ProceduralMemory>(KV.procedural);
369
- result.procedural = deltaFilter(all, sinceTime, "updatedAt");
370
- }
371
-
372
- if (scopes.includes("relations") && !projectScoped) {
373
- const all = await kv.list<MemoryRelation>(KV.relations);
374
- result.relations = deltaFilter(all, sinceTime, "createdAt");
375
- }
376
-
377
- if (scopes.includes("graph:nodes") && !projectScoped) {
378
- const all = await kv.list<GraphNode>(KV.graphNodes);
379
- result.graphNodes = all.filter(
380
- (n) => new Date(graphNodeTs(n)).getTime() > sinceTime,
381
- );
382
- }
383
-
384
- if (scopes.includes("graph:edges") && !projectScoped) {
385
- const all = await kv.list<GraphEdge>(KV.graphEdges);
386
- result.graphEdges = deltaFilter(all, sinceTime, "createdAt");
387
- }
388
-
389
- return result;
390
- }
391
-
392
- async function applySyncData(
393
- kv: StateKV,
394
- data: MeshSyncPayload,
395
- scopes: string[],
396
- ): Promise<number> {
397
- let applied = 0;
398
-
399
- if (scopes.includes("memories")) {
400
- applied += await lwwMergeList(kv, KV.memories, data.memories, "mem:memory", "updatedAt");
401
- }
402
- if (scopes.includes("actions")) {
403
- applied += await lwwMergeList(kv, KV.actions, data.actions, "mem:action", "updatedAt");
404
- }
405
- if (scopes.includes("semantic")) {
406
- applied += await lwwMergeList(kv, KV.semantic, data.semantic, "mem:semantic", "updatedAt");
407
- }
408
- if (scopes.includes("procedural")) {
409
- applied += await lwwMergeList(kv, KV.procedural, data.procedural, "mem:procedural", "updatedAt");
410
- }
411
- if (scopes.includes("relations") && data.relations) {
412
- for (const rel of data.relations) {
413
- if (!rel.sourceId || !rel.targetId || !rel.type) continue;
414
- const relKey = `${rel.sourceId}:${rel.targetId}:${rel.type}`;
415
- const wrote = await withKeyedLock(`mem:relation:${relKey}`, async () => {
416
- const existing = await kv.get<MemoryRelation>(KV.relations, relKey);
417
- if (!existing) {
418
- await kv.set(KV.relations, relKey, rel);
419
- return true;
420
- }
421
- return false;
422
- });
423
- if (wrote) applied++;
424
- }
425
- }
426
- if (scopes.includes("graph:nodes")) {
427
- applied += await lwwMergeGraphNodes(kv, data.graphNodes);
428
- }
429
- if (scopes.includes("graph:edges")) {
430
- applied += await lwwMergeList(kv, KV.graphEdges, data.graphEdges, "mem:gedge", "createdAt");
431
- }
432
-
433
- return applied;
434
- }