@agenticmail/enterprise 0.5.261 → 0.5.262
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/dist/agent-heartbeat-ONI44OWS.js +510 -0
- package/dist/agent-memory-DYLBAY2M.js +10 -0
- package/dist/agent-tools-3HZX7YZD.js +13872 -0
- package/dist/chunk-4BBEW5L4.js +595 -0
- package/dist/chunk-5Y3IDYBB.js +3778 -0
- package/dist/chunk-TM5DXSW2.js +1224 -0
- package/dist/chunk-TN3VSJSA.js +4732 -0
- package/dist/cli-agent-ADGEHIJT.js +1768 -0
- package/dist/cli-serve-NPMAGGZA.js +114 -0
- package/dist/cli.js +3 -3
- package/dist/index.js +4 -4
- package/dist/routes-6VR7R2Y4.js +13531 -0
- package/dist/runtime-OYQGJ3S5.js +45 -0
- package/dist/server-VSEPJEH5.js +15 -0
- package/dist/setup-J54YNSDJ.js +20 -0
- package/package.json +1 -1
- package/src/agent-tools/tools/memory.ts +1 -0
- package/src/engine/agent-memory.ts +8 -2
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MemorySearchIndex
|
|
3
|
+
} from "./chunk-TYW5XTOW.js";
|
|
4
|
+
|
|
5
|
+
// src/engine/agent-memory.ts
|
|
6
|
+
function sj(v, fb = {}) {
|
|
7
|
+
if (!v) return fb;
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(v);
|
|
10
|
+
} catch {
|
|
11
|
+
return fb;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
var MEMORY_CATEGORIES = {
|
|
15
|
+
org_knowledge: {
|
|
16
|
+
label: "Organization Knowledge",
|
|
17
|
+
description: "Policies, procedures, and organizational information"
|
|
18
|
+
},
|
|
19
|
+
interaction_pattern: {
|
|
20
|
+
label: "Interaction Patterns",
|
|
21
|
+
description: "Learned patterns from past interactions"
|
|
22
|
+
},
|
|
23
|
+
preference: {
|
|
24
|
+
label: "Preferences",
|
|
25
|
+
description: "User and organizational preferences"
|
|
26
|
+
},
|
|
27
|
+
correction: {
|
|
28
|
+
label: "Corrections",
|
|
29
|
+
description: "Corrections and feedback received"
|
|
30
|
+
},
|
|
31
|
+
skill: {
|
|
32
|
+
label: "Skills",
|
|
33
|
+
description: "Learned abilities and competencies"
|
|
34
|
+
},
|
|
35
|
+
context: {
|
|
36
|
+
label: "Context",
|
|
37
|
+
description: "Contextual information and background knowledge"
|
|
38
|
+
},
|
|
39
|
+
reflection: {
|
|
40
|
+
label: "Reflections",
|
|
41
|
+
description: "Self-reflective insights and learnings"
|
|
42
|
+
},
|
|
43
|
+
session_learning: {
|
|
44
|
+
label: "Session Learnings",
|
|
45
|
+
description: "Insights captured during conversation sessions"
|
|
46
|
+
},
|
|
47
|
+
system_notice: {
|
|
48
|
+
label: "System Notices",
|
|
49
|
+
description: "System-generated notifications about configuration changes"
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var IMPORTANCE_WEIGHT = {
|
|
53
|
+
critical: 4,
|
|
54
|
+
high: 3,
|
|
55
|
+
normal: 2,
|
|
56
|
+
low: 1
|
|
57
|
+
};
|
|
58
|
+
var AgentMemoryManager = class {
|
|
59
|
+
memories = /* @__PURE__ */ new Map();
|
|
60
|
+
/** Per-agent index: agentId → Set of memory IDs for O(1) agent lookups */
|
|
61
|
+
agentIndex = /* @__PURE__ */ new Map();
|
|
62
|
+
/** Full-text search index (BM25F + stemming + inverted index) */
|
|
63
|
+
searchIndex = new MemorySearchIndex();
|
|
64
|
+
engineDb;
|
|
65
|
+
// ─── Database Lifecycle ─────────────────────────────
|
|
66
|
+
async setDb(db) {
|
|
67
|
+
this.engineDb = db;
|
|
68
|
+
await this.loadFromDb();
|
|
69
|
+
}
|
|
70
|
+
async loadFromDb() {
|
|
71
|
+
if (!this.engineDb) return;
|
|
72
|
+
try {
|
|
73
|
+
const rows = await this.engineDb.query("SELECT * FROM agent_memory");
|
|
74
|
+
console.log(`[memory] Loaded ${rows.length} memories from DB`);
|
|
75
|
+
for (const r of rows) {
|
|
76
|
+
try {
|
|
77
|
+
const entry = this.rowToEntry(r);
|
|
78
|
+
this.memories.set(entry.id, entry);
|
|
79
|
+
this.indexAdd(entry.agentId, entry.id);
|
|
80
|
+
this.searchIndex.addDocument(entry.id, entry);
|
|
81
|
+
} catch (entryErr) {
|
|
82
|
+
console.warn(`[memory] Skipped entry ${r.id}: ${entryErr.message}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
} catch (loadErr) {
|
|
86
|
+
console.error("[memory] loadFromDb failed:", loadErr.message);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** Add a memory ID to the per-agent index. */
|
|
90
|
+
indexAdd(agentId, memoryId) {
|
|
91
|
+
let set = this.agentIndex.get(agentId);
|
|
92
|
+
if (!set) {
|
|
93
|
+
set = /* @__PURE__ */ new Set();
|
|
94
|
+
this.agentIndex.set(agentId, set);
|
|
95
|
+
}
|
|
96
|
+
set.add(memoryId);
|
|
97
|
+
}
|
|
98
|
+
/** Remove a memory ID from the per-agent index. */
|
|
99
|
+
indexRemove(agentId, memoryId) {
|
|
100
|
+
const set = this.agentIndex.get(agentId);
|
|
101
|
+
if (set) {
|
|
102
|
+
set.delete(memoryId);
|
|
103
|
+
if (set.size === 0) this.agentIndex.delete(agentId);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** Get all memory entries for an agent via the index — O(n) where n is that agent's entries, not total. */
|
|
107
|
+
getAgentMemories(agentId) {
|
|
108
|
+
const ids = this.agentIndex.get(agentId);
|
|
109
|
+
if (!ids || ids.size === 0) return [];
|
|
110
|
+
const result = [];
|
|
111
|
+
for (const id of ids) {
|
|
112
|
+
const entry = this.memories.get(id);
|
|
113
|
+
if (entry) result.push(entry);
|
|
114
|
+
}
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
// ─── Convenience Methods ─────────────────────────────
|
|
118
|
+
/**
|
|
119
|
+
* Convenience method for storing a memory with minimal input.
|
|
120
|
+
* Handles the common case where you just want to store a piece of text.
|
|
121
|
+
*/
|
|
122
|
+
async storeMemory(agentId, opts) {
|
|
123
|
+
return this.createMemory({
|
|
124
|
+
agentId,
|
|
125
|
+
orgId: opts.orgId || "default",
|
|
126
|
+
content: opts.content,
|
|
127
|
+
category: opts.category || "general",
|
|
128
|
+
importance: opts.importance || "normal",
|
|
129
|
+
confidence: opts.confidence ?? 1,
|
|
130
|
+
title: opts.title || "",
|
|
131
|
+
source: "system",
|
|
132
|
+
tags: [],
|
|
133
|
+
metadata: {}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Convenience method for searching memories by text query.
|
|
138
|
+
* Returns matching entries sorted by relevance.
|
|
139
|
+
*/
|
|
140
|
+
async recall(agentId, query, limit = 5) {
|
|
141
|
+
return this.queryMemories({ agentId, query, limit });
|
|
142
|
+
}
|
|
143
|
+
// ─── CRUD Operations ────────────────────────────────
|
|
144
|
+
/**
|
|
145
|
+
* Creates a new memory entry with auto-generated id and timestamps.
|
|
146
|
+
* Persists to both the in-memory Map and the database.
|
|
147
|
+
*/
|
|
148
|
+
async createMemory(input) {
|
|
149
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
150
|
+
const entry = {
|
|
151
|
+
...input,
|
|
152
|
+
confidence: input.confidence ?? 0.8,
|
|
153
|
+
tags: input.tags ?? [],
|
|
154
|
+
metadata: input.metadata ?? {},
|
|
155
|
+
id: crypto.randomUUID(),
|
|
156
|
+
accessCount: 0,
|
|
157
|
+
createdAt: now,
|
|
158
|
+
updatedAt: now
|
|
159
|
+
};
|
|
160
|
+
this.memories.set(entry.id, entry);
|
|
161
|
+
this.indexAdd(entry.agentId, entry.id);
|
|
162
|
+
this.searchIndex.addDocument(entry.id, entry);
|
|
163
|
+
await this.engineDb?.execute(
|
|
164
|
+
`INSERT INTO agent_memory (id, agent_id, org_id, category, title, content, source, importance, confidence, access_count, last_accessed_at, expires_at, tags, metadata, created_at, updated_at)
|
|
165
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
166
|
+
[
|
|
167
|
+
entry.id,
|
|
168
|
+
entry.agentId,
|
|
169
|
+
entry.orgId,
|
|
170
|
+
entry.category,
|
|
171
|
+
entry.title,
|
|
172
|
+
entry.content,
|
|
173
|
+
entry.source,
|
|
174
|
+
entry.importance,
|
|
175
|
+
entry.confidence,
|
|
176
|
+
entry.accessCount,
|
|
177
|
+
entry.lastAccessedAt || null,
|
|
178
|
+
entry.expiresAt || null,
|
|
179
|
+
JSON.stringify(entry.tags),
|
|
180
|
+
JSON.stringify(entry.metadata),
|
|
181
|
+
entry.createdAt,
|
|
182
|
+
entry.updatedAt
|
|
183
|
+
]
|
|
184
|
+
).catch((err) => {
|
|
185
|
+
console.error("[agent-memory] Failed to persist memory entry:", err);
|
|
186
|
+
});
|
|
187
|
+
return entry;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Updates an existing memory entry by merging provided fields.
|
|
191
|
+
* Returns the updated entry or null if not found.
|
|
192
|
+
*/
|
|
193
|
+
async updateMemory(id, updates) {
|
|
194
|
+
const existing = this.memories.get(id);
|
|
195
|
+
if (!existing) return null;
|
|
196
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
197
|
+
const updated = {
|
|
198
|
+
...existing,
|
|
199
|
+
...updates,
|
|
200
|
+
id: existing.id,
|
|
201
|
+
agentId: existing.agentId,
|
|
202
|
+
orgId: existing.orgId,
|
|
203
|
+
createdAt: existing.createdAt,
|
|
204
|
+
updatedAt: now
|
|
205
|
+
};
|
|
206
|
+
this.memories.set(id, updated);
|
|
207
|
+
if (updates.title !== void 0 || updates.content !== void 0 || updates.tags !== void 0) {
|
|
208
|
+
this.searchIndex.addDocument(id, updated);
|
|
209
|
+
}
|
|
210
|
+
await this.engineDb?.execute(
|
|
211
|
+
`UPDATE agent_memory SET
|
|
212
|
+
category = ?, title = ?, content = ?, source = ?,
|
|
213
|
+
importance = ?, confidence = ?, access_count = ?,
|
|
214
|
+
last_accessed_at = ?, expires_at = ?, tags = ?,
|
|
215
|
+
metadata = ?, updated_at = ?
|
|
216
|
+
WHERE id = ?`,
|
|
217
|
+
[
|
|
218
|
+
updated.category,
|
|
219
|
+
updated.title,
|
|
220
|
+
updated.content,
|
|
221
|
+
updated.source,
|
|
222
|
+
updated.importance,
|
|
223
|
+
updated.confidence,
|
|
224
|
+
updated.accessCount,
|
|
225
|
+
updated.lastAccessedAt || null,
|
|
226
|
+
updated.expiresAt || null,
|
|
227
|
+
JSON.stringify(updated.tags),
|
|
228
|
+
JSON.stringify(updated.metadata),
|
|
229
|
+
updated.updatedAt,
|
|
230
|
+
id
|
|
231
|
+
]
|
|
232
|
+
).catch((err) => {
|
|
233
|
+
console.error("[agent-memory] Failed to update memory entry:", err);
|
|
234
|
+
});
|
|
235
|
+
return updated;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Deletes a memory entry from both the in-memory Map and the database.
|
|
239
|
+
* Returns true if the entry existed and was deleted.
|
|
240
|
+
*/
|
|
241
|
+
async deleteMemory(id) {
|
|
242
|
+
const entry = this.memories.get(id);
|
|
243
|
+
const existed = this.memories.delete(id);
|
|
244
|
+
if (entry) this.indexRemove(entry.agentId, id);
|
|
245
|
+
this.searchIndex.removeDocument(id);
|
|
246
|
+
await this.engineDb?.execute(
|
|
247
|
+
"DELETE FROM agent_memory WHERE id = ?",
|
|
248
|
+
[id]
|
|
249
|
+
).catch((err) => {
|
|
250
|
+
console.error("[agent-memory] Failed to delete memory entry:", err);
|
|
251
|
+
});
|
|
252
|
+
return existed;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Retrieves a single memory entry by id.
|
|
256
|
+
* Returns from the in-memory Map (async for interface compatibility).
|
|
257
|
+
*/
|
|
258
|
+
async getMemory(id) {
|
|
259
|
+
return this.memories.get(id);
|
|
260
|
+
}
|
|
261
|
+
// ─── Query Operations ───────────────────────────────
|
|
262
|
+
/**
|
|
263
|
+
* Queries memory entries for a specific agent with optional filters.
|
|
264
|
+
* Supports filtering by category, importance, source, and text search on title/content.
|
|
265
|
+
*/
|
|
266
|
+
async queryMemories(opts) {
|
|
267
|
+
let results = this.getAgentMemories(opts.agentId);
|
|
268
|
+
if (results.length === 0 && this.engineDb && opts.agentId) {
|
|
269
|
+
try {
|
|
270
|
+
const rows = await this.engineDb.query(
|
|
271
|
+
"SELECT * FROM agent_memory WHERE agent_id = ?",
|
|
272
|
+
[opts.agentId]
|
|
273
|
+
);
|
|
274
|
+
console.log(`[memory] Lazy-loaded ${rows.length} memories for agent ${opts.agentId}`);
|
|
275
|
+
for (const r of rows) {
|
|
276
|
+
try {
|
|
277
|
+
const entry = this.rowToEntry(r);
|
|
278
|
+
this.memories.set(entry.id, entry);
|
|
279
|
+
this.indexAdd(entry.agentId, entry.id);
|
|
280
|
+
this.searchIndex.addDocument(entry.id, entry);
|
|
281
|
+
} catch {
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
results = this.getAgentMemories(opts.agentId);
|
|
285
|
+
} catch (e) {
|
|
286
|
+
console.error("[memory] Lazy-load failed:", e.message);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if (opts.category) {
|
|
290
|
+
results = results.filter((m) => m.category === opts.category);
|
|
291
|
+
}
|
|
292
|
+
if (opts.importance) {
|
|
293
|
+
results = results.filter((m) => m.importance === opts.importance);
|
|
294
|
+
}
|
|
295
|
+
if (opts.source) {
|
|
296
|
+
results = results.filter((m) => m.source === opts.source);
|
|
297
|
+
}
|
|
298
|
+
if (opts.query) {
|
|
299
|
+
const candidateIds = new Set(results.map((m) => m.id));
|
|
300
|
+
const searchResults = this.searchIndex.search(opts.query, candidateIds);
|
|
301
|
+
if (searchResults.length > 0) {
|
|
302
|
+
const scored = searchResults.map((r) => {
|
|
303
|
+
const entry = this.memories.get(r.id);
|
|
304
|
+
return entry ? { entry, score: r.score * IMPORTANCE_WEIGHT[entry.importance] } : null;
|
|
305
|
+
}).filter((r) => r !== null);
|
|
306
|
+
scored.sort((a, b) => b.score - a.score);
|
|
307
|
+
return scored.slice(0, opts.limit || 100).map((d) => d.entry);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
results.sort((a, b) => {
|
|
311
|
+
const weightDiff = IMPORTANCE_WEIGHT[b.importance] - IMPORTANCE_WEIGHT[a.importance];
|
|
312
|
+
if (weightDiff !== 0) return weightDiff;
|
|
313
|
+
return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime();
|
|
314
|
+
});
|
|
315
|
+
const limit = opts.limit || 100;
|
|
316
|
+
return results.slice(0, limit);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Returns memories created within the last N hours for an agent.
|
|
320
|
+
*/
|
|
321
|
+
async getRecentMemories(agentId, hours = 24) {
|
|
322
|
+
const cutoff = new Date(Date.now() - hours * 36e5).toISOString();
|
|
323
|
+
return this.getAgentMemories(agentId).filter((m) => m.createdAt >= cutoff).sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
|
|
324
|
+
}
|
|
325
|
+
// ─── Access Tracking ────────────────────────────────
|
|
326
|
+
/**
|
|
327
|
+
* Increments the access count and updates lastAccessedAt for a memory entry.
|
|
328
|
+
* Used to track frequently referenced knowledge.
|
|
329
|
+
*/
|
|
330
|
+
async recordAccess(memoryId) {
|
|
331
|
+
const entry = this.memories.get(memoryId);
|
|
332
|
+
if (!entry) return;
|
|
333
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
334
|
+
entry.accessCount += 1;
|
|
335
|
+
entry.lastAccessedAt = now;
|
|
336
|
+
entry.updatedAt = now;
|
|
337
|
+
await this.engineDb?.execute(
|
|
338
|
+
`UPDATE agent_memory SET access_count = ?, last_accessed_at = ?, updated_at = ? WHERE id = ?`,
|
|
339
|
+
[entry.accessCount, entry.lastAccessedAt, entry.updatedAt, memoryId]
|
|
340
|
+
).catch((err) => {
|
|
341
|
+
console.error("[agent-memory] Failed to record access:", err);
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
// ─── Context Generation ─────────────────────────────
|
|
345
|
+
/**
|
|
346
|
+
* Generates a markdown context string suitable for injection into an agent's system prompt.
|
|
347
|
+
*
|
|
348
|
+
* Ranking strategy:
|
|
349
|
+
* 1. Critical importance entries always come first
|
|
350
|
+
* 2. Remaining entries scored by: confidence * accessWeight * recencyWeight
|
|
351
|
+
* 3. If a query is provided, entries matching query terms get a relevance boost
|
|
352
|
+
* 4. Output is grouped by category with markdown headers
|
|
353
|
+
* 5. Truncated to maxTokens (estimated at ~4 chars per token)
|
|
354
|
+
*/
|
|
355
|
+
async generateMemoryContext(agentId, query, maxTokens = 1500) {
|
|
356
|
+
const entries = this.getAgentMemories(agentId).filter((m) => m.confidence >= 0.1);
|
|
357
|
+
if (entries.length === 0) return "";
|
|
358
|
+
const now = Date.now();
|
|
359
|
+
let relevanceMap;
|
|
360
|
+
if (query) {
|
|
361
|
+
const candidateIds = new Set(entries.map((e) => e.id));
|
|
362
|
+
const searchResults = this.searchIndex.search(query, candidateIds);
|
|
363
|
+
if (searchResults.length > 0) {
|
|
364
|
+
relevanceMap = /* @__PURE__ */ new Map();
|
|
365
|
+
const maxScore = searchResults[0].score;
|
|
366
|
+
for (const r of searchResults) {
|
|
367
|
+
relevanceMap.set(r.id, maxScore > 0 ? r.score / maxScore : 0);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
const scored = entries.map((entry) => {
|
|
372
|
+
const accessWeight = 1 + Math.log1p(entry.accessCount) * 0.3;
|
|
373
|
+
const lastTouch = entry.lastAccessedAt || entry.createdAt;
|
|
374
|
+
const ageHours = Math.max(1, (now - new Date(lastTouch).getTime()) / 36e5);
|
|
375
|
+
const recencyWeight = 1 / (1 + Math.log1p(ageHours / 24) * 0.2);
|
|
376
|
+
let score = entry.confidence * accessWeight * recencyWeight;
|
|
377
|
+
score *= IMPORTANCE_WEIGHT[entry.importance];
|
|
378
|
+
if (relevanceMap) {
|
|
379
|
+
const relevance = relevanceMap.get(entry.id) || 0;
|
|
380
|
+
if (relevance > 0) {
|
|
381
|
+
score *= 1 + relevance * 3;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return { entry, score };
|
|
385
|
+
});
|
|
386
|
+
scored.sort((a, b) => b.score - a.score);
|
|
387
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
388
|
+
for (const { entry } of scored) {
|
|
389
|
+
const group = grouped.get(entry.category) || [];
|
|
390
|
+
group.push(entry);
|
|
391
|
+
grouped.set(entry.category, group);
|
|
392
|
+
}
|
|
393
|
+
const maxChars = maxTokens * 4;
|
|
394
|
+
const lines = ["## Agent Memory", ""];
|
|
395
|
+
let charCount = lines.join("\n").length;
|
|
396
|
+
for (const [category, categoryEntries] of Array.from(grouped.entries())) {
|
|
397
|
+
const meta = MEMORY_CATEGORIES[category];
|
|
398
|
+
if (!meta) continue;
|
|
399
|
+
const header = `### ${meta.label}`;
|
|
400
|
+
if (charCount + header.length + 2 > maxChars) break;
|
|
401
|
+
lines.push(header);
|
|
402
|
+
lines.push("");
|
|
403
|
+
charCount += header.length + 2;
|
|
404
|
+
for (const entry of categoryEntries) {
|
|
405
|
+
const badge = entry.importance === "critical" ? "[CRITICAL] " : entry.importance === "high" ? "[HIGH] " : "";
|
|
406
|
+
const entryLine = `- **${badge}${entry.title}**: ${entry.content}`;
|
|
407
|
+
if (charCount + entryLine.length + 1 > maxChars) break;
|
|
408
|
+
lines.push(entryLine);
|
|
409
|
+
charCount += entryLine.length + 1;
|
|
410
|
+
}
|
|
411
|
+
lines.push("");
|
|
412
|
+
charCount += 1;
|
|
413
|
+
}
|
|
414
|
+
return lines.join("\n").trim();
|
|
415
|
+
}
|
|
416
|
+
// ─── Memory Lifecycle ───────────────────────────────
|
|
417
|
+
/**
|
|
418
|
+
* Decays confidence scores for entries that have not been accessed in 7+ days.
|
|
419
|
+
* Critical importance entries are exempt from decay.
|
|
420
|
+
*
|
|
421
|
+
* @param agentId - The agent whose memories to decay
|
|
422
|
+
* @param decayRate - How much to reduce confidence (default 0.05)
|
|
423
|
+
* @returns Number of entries that were decayed
|
|
424
|
+
*/
|
|
425
|
+
async decayConfidence(agentId, decayRate = 0.05) {
|
|
426
|
+
const cutoff = new Date(Date.now() - 7 * 864e5).toISOString();
|
|
427
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
428
|
+
const decayed = [];
|
|
429
|
+
for (const entry of this.getAgentMemories(agentId)) {
|
|
430
|
+
if (entry.importance === "critical") continue;
|
|
431
|
+
const lastTouch = entry.lastAccessedAt || entry.createdAt;
|
|
432
|
+
if (lastTouch >= cutoff) continue;
|
|
433
|
+
const newConfidence = Math.max(0, entry.confidence - decayRate);
|
|
434
|
+
if (newConfidence === entry.confidence) continue;
|
|
435
|
+
entry.confidence = parseFloat(newConfidence.toFixed(4));
|
|
436
|
+
entry.updatedAt = now;
|
|
437
|
+
decayed.push({ id: entry.id, confidence: entry.confidence });
|
|
438
|
+
}
|
|
439
|
+
if (decayed.length > 0 && this.engineDb) {
|
|
440
|
+
await Promise.all(
|
|
441
|
+
decayed.map(
|
|
442
|
+
(d) => this.engineDb.execute(
|
|
443
|
+
"UPDATE agent_memory SET confidence = ?, updated_at = ? WHERE id = ?",
|
|
444
|
+
[d.confidence, now, d.id]
|
|
445
|
+
).catch((err) => console.error("[agent-memory] Failed to decay confidence:", err))
|
|
446
|
+
)
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
return decayed.length;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Prunes (deletes) memory entries that are expired or have very low confidence.
|
|
453
|
+
* An entry is pruned if:
|
|
454
|
+
* - confidence < 0.1, OR
|
|
455
|
+
* - expiresAt is set and has passed
|
|
456
|
+
*
|
|
457
|
+
* @param agentId - Optional: limit pruning to a specific agent. If omitted, prunes all.
|
|
458
|
+
* @returns Number of entries pruned
|
|
459
|
+
*/
|
|
460
|
+
async pruneExpired(agentId) {
|
|
461
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
462
|
+
const toDelete = [];
|
|
463
|
+
const entries = agentId ? this.getAgentMemories(agentId) : Array.from(this.memories.values());
|
|
464
|
+
for (const entry of entries) {
|
|
465
|
+
const isLowConfidence = entry.confidence < 0.1;
|
|
466
|
+
const isExpired = entry.expiresAt && entry.expiresAt <= now;
|
|
467
|
+
if (isLowConfidence || isExpired) {
|
|
468
|
+
toDelete.push({ id: entry.id, agentId: entry.agentId });
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
for (const item of toDelete) {
|
|
472
|
+
this.memories.delete(item.id);
|
|
473
|
+
this.indexRemove(item.agentId, item.id);
|
|
474
|
+
this.searchIndex.removeDocument(item.id);
|
|
475
|
+
}
|
|
476
|
+
if (toDelete.length > 0 && this.engineDb) {
|
|
477
|
+
await Promise.all(
|
|
478
|
+
toDelete.map(
|
|
479
|
+
(item) => this.engineDb.execute("DELETE FROM agent_memory WHERE id = ?", [item.id]).catch((err) => console.error("[agent-memory] Failed to prune memory entry:", err))
|
|
480
|
+
)
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
return toDelete.length;
|
|
484
|
+
}
|
|
485
|
+
// ─── Statistics ─────────────────────────────────────
|
|
486
|
+
/**
|
|
487
|
+
* Returns aggregate statistics for a specific agent's memory entries.
|
|
488
|
+
*/
|
|
489
|
+
async getStats(agentId) {
|
|
490
|
+
let entries = this.getAgentMemories(agentId);
|
|
491
|
+
if (entries.length === 0 && this.engineDb) {
|
|
492
|
+
await this.queryMemories({ agentId, limit: 1e3 });
|
|
493
|
+
entries = this.getAgentMemories(agentId);
|
|
494
|
+
}
|
|
495
|
+
return this.computeStats(entries);
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Returns per-agent memory statistics for all agents in an organization.
|
|
499
|
+
*/
|
|
500
|
+
async getStatsByOrg(orgId) {
|
|
501
|
+
const agentMap = /* @__PURE__ */ new Map();
|
|
502
|
+
for (const entry of this.memories.values()) {
|
|
503
|
+
if (entry.orgId !== orgId) continue;
|
|
504
|
+
const group = agentMap.get(entry.agentId) || [];
|
|
505
|
+
group.push(entry);
|
|
506
|
+
agentMap.set(entry.agentId, group);
|
|
507
|
+
}
|
|
508
|
+
const results = [];
|
|
509
|
+
for (const [agentId, entries] of Array.from(agentMap.entries())) {
|
|
510
|
+
results.push({ agentId, stats: this.computeStats(entries) });
|
|
511
|
+
}
|
|
512
|
+
return results;
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Computes statistics from a set of memory entries.
|
|
516
|
+
*/
|
|
517
|
+
computeStats(entries) {
|
|
518
|
+
const byCategory = {};
|
|
519
|
+
const byImportance = {};
|
|
520
|
+
const bySource = {};
|
|
521
|
+
let totalConfidence = 0;
|
|
522
|
+
for (const entry of entries) {
|
|
523
|
+
byCategory[entry.category] = (byCategory[entry.category] || 0) + 1;
|
|
524
|
+
byImportance[entry.importance] = (byImportance[entry.importance] || 0) + 1;
|
|
525
|
+
bySource[entry.source] = (bySource[entry.source] || 0) + 1;
|
|
526
|
+
totalConfidence += entry.confidence;
|
|
527
|
+
}
|
|
528
|
+
return {
|
|
529
|
+
totalEntries: entries.length,
|
|
530
|
+
byCategory,
|
|
531
|
+
byImportance,
|
|
532
|
+
bySource,
|
|
533
|
+
avgConfidence: entries.length > 0 ? parseFloat((totalConfidence / entries.length).toFixed(4)) : 0
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
// ─── Policy Integration ─────────────────────────────
|
|
537
|
+
/**
|
|
538
|
+
* Creates a memory entry from an organization policy.
|
|
539
|
+
* Used during onboarding to seed agent memory with policy knowledge.
|
|
540
|
+
*
|
|
541
|
+
* Importance is derived from policy enforcement level:
|
|
542
|
+
* - mandatory -> critical
|
|
543
|
+
* - recommended -> high
|
|
544
|
+
* - informational / other -> normal
|
|
545
|
+
*/
|
|
546
|
+
async createFromPolicy(agentId, policy) {
|
|
547
|
+
const importance = policy.enforcement === "mandatory" ? "critical" : policy.enforcement === "recommended" ? "high" : "normal";
|
|
548
|
+
return this.createMemory({
|
|
549
|
+
agentId,
|
|
550
|
+
orgId: policy.orgId,
|
|
551
|
+
category: "org_knowledge",
|
|
552
|
+
title: policy.name,
|
|
553
|
+
content: policy.content,
|
|
554
|
+
source: "onboarding",
|
|
555
|
+
importance,
|
|
556
|
+
confidence: 1,
|
|
557
|
+
lastAccessedAt: void 0,
|
|
558
|
+
expiresAt: void 0,
|
|
559
|
+
tags: ["policy", policy.category],
|
|
560
|
+
metadata: {
|
|
561
|
+
policyId: policy.id,
|
|
562
|
+
enforcement: policy.enforcement
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
// ─── Row Mapper ─────────────────────────────────────
|
|
567
|
+
/**
|
|
568
|
+
* Converts a database row into an AgentMemoryEntry.
|
|
569
|
+
*/
|
|
570
|
+
rowToEntry(row) {
|
|
571
|
+
return {
|
|
572
|
+
id: row.id,
|
|
573
|
+
agentId: row.agent_id,
|
|
574
|
+
orgId: row.org_id,
|
|
575
|
+
category: row.category,
|
|
576
|
+
title: row.title,
|
|
577
|
+
content: row.content,
|
|
578
|
+
source: row.source,
|
|
579
|
+
importance: row.importance,
|
|
580
|
+
confidence: row.confidence,
|
|
581
|
+
accessCount: row.access_count || 0,
|
|
582
|
+
lastAccessedAt: row.last_accessed_at || void 0,
|
|
583
|
+
expiresAt: row.expires_at || void 0,
|
|
584
|
+
tags: Array.isArray(row.tags) ? row.tags : Array.isArray(sj(row.tags)) ? sj(row.tags) : [],
|
|
585
|
+
metadata: sj(row.metadata || "{}"),
|
|
586
|
+
createdAt: row.created_at,
|
|
587
|
+
updatedAt: row.updated_at
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
|
|
592
|
+
export {
|
|
593
|
+
MEMORY_CATEGORIES,
|
|
594
|
+
AgentMemoryManager
|
|
595
|
+
};
|