@lov3kaizen/agentsea-memory 0.5.1
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/LICENSE +21 -0
- package/README.md +450 -0
- package/dist/chunk-GACX3FPR.js +1402 -0
- package/dist/chunk-M44NB53O.js +1226 -0
- package/dist/chunk-MQDWBPZU.js +972 -0
- package/dist/chunk-TPC7MYWK.js +1495 -0
- package/dist/chunk-XD2CQGSD.js +1540 -0
- package/dist/chunk-YI7RPDEV.js +1215 -0
- package/dist/core.types-lkxKv-bW.d.cts +242 -0
- package/dist/core.types-lkxKv-bW.d.ts +242 -0
- package/dist/debug/index.cjs +1248 -0
- package/dist/debug/index.d.cts +3 -0
- package/dist/debug/index.d.ts +3 -0
- package/dist/debug/index.js +20 -0
- package/dist/index-7SsAJ4et.d.ts +525 -0
- package/dist/index-BGxYqpFb.d.cts +601 -0
- package/dist/index-BX62efZu.d.ts +565 -0
- package/dist/index-Bbc3COw0.d.cts +748 -0
- package/dist/index-Bczz1Eyk.d.ts +637 -0
- package/dist/index-C7pEiT8L.d.cts +637 -0
- package/dist/index-CHetLTb0.d.ts +389 -0
- package/dist/index-CloeiFyx.d.ts +748 -0
- package/dist/index-DNOhq-3y.d.cts +525 -0
- package/dist/index-Da-M8FOV.d.cts +389 -0
- package/dist/index-Dy8UjRFz.d.cts +565 -0
- package/dist/index-aVcITW0B.d.ts +601 -0
- package/dist/index.cjs +8554 -0
- package/dist/index.d.cts +293 -0
- package/dist/index.d.ts +293 -0
- package/dist/index.js +742 -0
- package/dist/processing/index.cjs +1575 -0
- package/dist/processing/index.d.cts +2 -0
- package/dist/processing/index.d.ts +2 -0
- package/dist/processing/index.js +24 -0
- package/dist/retrieval/index.cjs +1262 -0
- package/dist/retrieval/index.d.cts +2 -0
- package/dist/retrieval/index.d.ts +2 -0
- package/dist/retrieval/index.js +26 -0
- package/dist/sharing/index.cjs +1003 -0
- package/dist/sharing/index.d.cts +3 -0
- package/dist/sharing/index.d.ts +3 -0
- package/dist/sharing/index.js +16 -0
- package/dist/stores/index.cjs +1445 -0
- package/dist/stores/index.d.cts +2 -0
- package/dist/stores/index.d.ts +2 -0
- package/dist/stores/index.js +20 -0
- package/dist/structures/index.cjs +1530 -0
- package/dist/structures/index.d.cts +3 -0
- package/dist/structures/index.d.ts +3 -0
- package/dist/structures/index.js +24 -0
- package/package.json +141 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,742 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Debugger,
|
|
3
|
+
Exporter,
|
|
4
|
+
Inspector,
|
|
5
|
+
Timeline,
|
|
6
|
+
createDebugger,
|
|
7
|
+
createExporter,
|
|
8
|
+
createInspector,
|
|
9
|
+
createTimeline
|
|
10
|
+
} from "./chunk-YI7RPDEV.js";
|
|
11
|
+
import {
|
|
12
|
+
Compressor,
|
|
13
|
+
Consolidator,
|
|
14
|
+
Extractor,
|
|
15
|
+
Forgetter,
|
|
16
|
+
Summarizer,
|
|
17
|
+
createCompressor,
|
|
18
|
+
createConsolidator,
|
|
19
|
+
createExtractor,
|
|
20
|
+
createForgetter,
|
|
21
|
+
createSummarizer
|
|
22
|
+
} from "./chunk-XD2CQGSD.js";
|
|
23
|
+
import {
|
|
24
|
+
HybridRetrieval,
|
|
25
|
+
PipelineBuilder,
|
|
26
|
+
RetrievalPipeline,
|
|
27
|
+
SemanticRetrieval,
|
|
28
|
+
TemporalRetrieval,
|
|
29
|
+
TimeWindows,
|
|
30
|
+
createHybridRetrieval,
|
|
31
|
+
createPipelineBuilder,
|
|
32
|
+
createRetrievalPipeline,
|
|
33
|
+
createSemanticRetrieval,
|
|
34
|
+
createTemporalRetrieval
|
|
35
|
+
} from "./chunk-M44NB53O.js";
|
|
36
|
+
import {
|
|
37
|
+
AccessControl,
|
|
38
|
+
NamespaceManager,
|
|
39
|
+
SharedMemory,
|
|
40
|
+
createAccessControl,
|
|
41
|
+
createNamespaceManager,
|
|
42
|
+
createSharedMemory
|
|
43
|
+
} from "./chunk-MQDWBPZU.js";
|
|
44
|
+
import {
|
|
45
|
+
InMemoryStore,
|
|
46
|
+
PostgresStore,
|
|
47
|
+
RedisStore,
|
|
48
|
+
SQLiteStore,
|
|
49
|
+
createInMemoryStore,
|
|
50
|
+
createPostgresStore,
|
|
51
|
+
createRedisStore,
|
|
52
|
+
createSQLiteStore
|
|
53
|
+
} from "./chunk-GACX3FPR.js";
|
|
54
|
+
import {
|
|
55
|
+
EpisodicMemory,
|
|
56
|
+
HierarchicalMemory,
|
|
57
|
+
LongTermMemory,
|
|
58
|
+
SemanticMemory,
|
|
59
|
+
WorkingMemory,
|
|
60
|
+
createEpisodicMemory,
|
|
61
|
+
createHierarchicalMemory,
|
|
62
|
+
createLongTermMemory,
|
|
63
|
+
createSemanticMemory,
|
|
64
|
+
createWorkingMemory
|
|
65
|
+
} from "./chunk-TPC7MYWK.js";
|
|
66
|
+
|
|
67
|
+
// src/core/MemoryManager.ts
|
|
68
|
+
import { EventEmitter } from "eventemitter3";
|
|
69
|
+
import { nanoid } from "nanoid";
|
|
70
|
+
var MemoryManager = class extends EventEmitter {
|
|
71
|
+
store;
|
|
72
|
+
embedding;
|
|
73
|
+
retrieval;
|
|
74
|
+
defaultNamespace;
|
|
75
|
+
autoEmbed;
|
|
76
|
+
importanceCalculator;
|
|
77
|
+
constructor(config) {
|
|
78
|
+
super();
|
|
79
|
+
this.store = config.store;
|
|
80
|
+
this.embedding = config.embedding;
|
|
81
|
+
this.retrieval = config.retrieval;
|
|
82
|
+
this.defaultNamespace = config.defaultNamespace ?? "default";
|
|
83
|
+
this.autoEmbed = config.autoEmbed ?? true;
|
|
84
|
+
this.importanceCalculator = config.importanceCalculator ?? this.defaultImportanceCalculator;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Add a memory
|
|
88
|
+
*/
|
|
89
|
+
async add(input) {
|
|
90
|
+
const now = Date.now();
|
|
91
|
+
const importance = input.importance ?? this.importanceCalculator(input.content, input.type ?? "context", {
|
|
92
|
+
source: input.metadata?.source ?? "explicit",
|
|
93
|
+
confidence: input.metadata?.confidence ?? 1,
|
|
94
|
+
namespace: input.metadata?.namespace ?? this.defaultNamespace,
|
|
95
|
+
...input.metadata
|
|
96
|
+
});
|
|
97
|
+
let embedding;
|
|
98
|
+
if (this.embedding && this.autoEmbed) {
|
|
99
|
+
embedding = await this.embedding.embed(input.content);
|
|
100
|
+
}
|
|
101
|
+
const entry = {
|
|
102
|
+
id: nanoid(),
|
|
103
|
+
content: input.content,
|
|
104
|
+
embedding,
|
|
105
|
+
type: input.type ?? "context",
|
|
106
|
+
importance,
|
|
107
|
+
metadata: {
|
|
108
|
+
source: "explicit",
|
|
109
|
+
confidence: 1,
|
|
110
|
+
namespace: this.defaultNamespace,
|
|
111
|
+
...input.metadata
|
|
112
|
+
},
|
|
113
|
+
timestamp: now,
|
|
114
|
+
expiresAt: input.expiresAt,
|
|
115
|
+
parentId: input.parentId,
|
|
116
|
+
accessCount: 0,
|
|
117
|
+
createdAt: now,
|
|
118
|
+
updatedAt: now
|
|
119
|
+
};
|
|
120
|
+
const id = await this.store.add(entry);
|
|
121
|
+
this.emit("memory:added", {
|
|
122
|
+
type: "memory:added",
|
|
123
|
+
memoryId: id,
|
|
124
|
+
memory: entry,
|
|
125
|
+
timestamp: now
|
|
126
|
+
});
|
|
127
|
+
return id;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Add multiple memories
|
|
131
|
+
*/
|
|
132
|
+
async addBatch(inputs) {
|
|
133
|
+
const ids = [];
|
|
134
|
+
if (this.embedding && this.autoEmbed && inputs.length > 1) {
|
|
135
|
+
const contents = inputs.map((i) => i.content);
|
|
136
|
+
const embeddings = await this.embedding.embedBatch(contents);
|
|
137
|
+
for (let i = 0; i < inputs.length; i++) {
|
|
138
|
+
const id = await this.addWithEmbedding(inputs[i], embeddings[i]);
|
|
139
|
+
ids.push(id);
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
for (const input of inputs) {
|
|
143
|
+
const id = await this.add(input);
|
|
144
|
+
ids.push(id);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return ids;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Add memory with pre-computed embedding
|
|
151
|
+
*/
|
|
152
|
+
async addWithEmbedding(input, embedding) {
|
|
153
|
+
const now = Date.now();
|
|
154
|
+
const importance = input.importance ?? this.importanceCalculator(input.content, input.type ?? "context", {
|
|
155
|
+
source: input.metadata?.source ?? "explicit",
|
|
156
|
+
confidence: input.metadata?.confidence ?? 1,
|
|
157
|
+
namespace: input.metadata?.namespace ?? this.defaultNamespace,
|
|
158
|
+
...input.metadata
|
|
159
|
+
});
|
|
160
|
+
const entry = {
|
|
161
|
+
id: nanoid(),
|
|
162
|
+
content: input.content,
|
|
163
|
+
embedding,
|
|
164
|
+
type: input.type ?? "context",
|
|
165
|
+
importance,
|
|
166
|
+
metadata: {
|
|
167
|
+
source: "explicit",
|
|
168
|
+
confidence: 1,
|
|
169
|
+
namespace: this.defaultNamespace,
|
|
170
|
+
...input.metadata
|
|
171
|
+
},
|
|
172
|
+
timestamp: now,
|
|
173
|
+
expiresAt: input.expiresAt,
|
|
174
|
+
parentId: input.parentId,
|
|
175
|
+
accessCount: 0,
|
|
176
|
+
createdAt: now,
|
|
177
|
+
updatedAt: now
|
|
178
|
+
};
|
|
179
|
+
const id = await this.store.add(entry);
|
|
180
|
+
this.emit("memory:added", {
|
|
181
|
+
type: "memory:added",
|
|
182
|
+
memoryId: id,
|
|
183
|
+
memory: entry,
|
|
184
|
+
timestamp: now
|
|
185
|
+
});
|
|
186
|
+
return id;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Get a memory by ID
|
|
190
|
+
*/
|
|
191
|
+
async get(id) {
|
|
192
|
+
const entry = await this.store.get(id);
|
|
193
|
+
if (entry) {
|
|
194
|
+
await this.store.update(id, {});
|
|
195
|
+
}
|
|
196
|
+
return entry;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Update a memory
|
|
200
|
+
*/
|
|
201
|
+
async update(id, updates) {
|
|
202
|
+
const existing = await this.store.get(id);
|
|
203
|
+
if (!existing) {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
if (updates.content && this.embedding && this.autoEmbed) {
|
|
207
|
+
const embedding = await this.embedding.embed(updates.content);
|
|
208
|
+
updates.embedding = embedding;
|
|
209
|
+
}
|
|
210
|
+
const success = await this.store.update(id, updates);
|
|
211
|
+
if (success) {
|
|
212
|
+
const updated = await this.store.get(id);
|
|
213
|
+
this.emit("memory:updated", {
|
|
214
|
+
type: "memory:updated",
|
|
215
|
+
memoryId: id,
|
|
216
|
+
memory: updated ?? void 0,
|
|
217
|
+
timestamp: Date.now()
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
return success;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Delete a memory
|
|
224
|
+
*/
|
|
225
|
+
async delete(id) {
|
|
226
|
+
const success = await this.store.delete(id);
|
|
227
|
+
if (success) {
|
|
228
|
+
this.emit("memory:deleted", {
|
|
229
|
+
type: "memory:deleted",
|
|
230
|
+
memoryId: id,
|
|
231
|
+
timestamp: Date.now()
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
return success;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Retrieve relevant memories using semantic search
|
|
238
|
+
*/
|
|
239
|
+
async retrieve(query, options = {}) {
|
|
240
|
+
if (!this.embedding) {
|
|
241
|
+
throw new Error("Embedding provider required for semantic retrieval");
|
|
242
|
+
}
|
|
243
|
+
let results;
|
|
244
|
+
if (this.retrieval) {
|
|
245
|
+
results = await this.retrieval.retrieve(
|
|
246
|
+
query,
|
|
247
|
+
this.store,
|
|
248
|
+
this.embedding,
|
|
249
|
+
options
|
|
250
|
+
);
|
|
251
|
+
} else {
|
|
252
|
+
const queryEmbedding = await this.embedding.embed(query);
|
|
253
|
+
results = await this.store.search(queryEmbedding, {
|
|
254
|
+
topK: options.limit ?? 10,
|
|
255
|
+
minScore: options.minScore ?? 0.5,
|
|
256
|
+
filter: {
|
|
257
|
+
userId: options.userId,
|
|
258
|
+
agentId: options.agentId,
|
|
259
|
+
conversationId: options.conversationId,
|
|
260
|
+
namespace: options.namespace ?? this.defaultNamespace,
|
|
261
|
+
types: options.types
|
|
262
|
+
},
|
|
263
|
+
namespace: options.namespace ?? this.defaultNamespace
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
this.emit("memory:retrieved", {
|
|
267
|
+
type: "memory:retrieved",
|
|
268
|
+
metadata: { query, count: results.length },
|
|
269
|
+
timestamp: Date.now()
|
|
270
|
+
});
|
|
271
|
+
return results;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Search memories with filters (non-semantic)
|
|
275
|
+
*/
|
|
276
|
+
search(options) {
|
|
277
|
+
const queryOptions = {
|
|
278
|
+
...options,
|
|
279
|
+
namespace: options.namespace ?? this.defaultNamespace
|
|
280
|
+
};
|
|
281
|
+
return this.store.query(queryOptions);
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Query memories (alias for search)
|
|
285
|
+
*/
|
|
286
|
+
query(options) {
|
|
287
|
+
return this.search(options);
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Clear memories
|
|
291
|
+
*/
|
|
292
|
+
clear(options) {
|
|
293
|
+
return this.store.clear({
|
|
294
|
+
namespace: options?.namespace ?? this.defaultNamespace,
|
|
295
|
+
userId: options?.userId
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Get memory count
|
|
300
|
+
*/
|
|
301
|
+
count(options) {
|
|
302
|
+
return this.store.count({
|
|
303
|
+
...options,
|
|
304
|
+
namespace: options?.namespace ?? this.defaultNamespace
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Get memory statistics
|
|
309
|
+
*/
|
|
310
|
+
async getStats() {
|
|
311
|
+
const result = await this.store.query({ limit: 1e5 });
|
|
312
|
+
const entries = result.entries;
|
|
313
|
+
const byType = {
|
|
314
|
+
fact: 0,
|
|
315
|
+
preference: 0,
|
|
316
|
+
event: 0,
|
|
317
|
+
context: 0,
|
|
318
|
+
summary: 0,
|
|
319
|
+
entity: 0,
|
|
320
|
+
relation: 0,
|
|
321
|
+
conversation: 0,
|
|
322
|
+
custom: 0
|
|
323
|
+
};
|
|
324
|
+
const byNamespace = {};
|
|
325
|
+
let totalImportance = 0;
|
|
326
|
+
let embeddedCount = 0;
|
|
327
|
+
let oldestTimestamp = Infinity;
|
|
328
|
+
let newestTimestamp = 0;
|
|
329
|
+
let totalSize = 0;
|
|
330
|
+
for (const entry of entries) {
|
|
331
|
+
byType[entry.type] = (byType[entry.type] ?? 0) + 1;
|
|
332
|
+
const ns = entry.metadata.namespace ?? "default";
|
|
333
|
+
byNamespace[ns] = (byNamespace[ns] ?? 0) + 1;
|
|
334
|
+
totalImportance += entry.importance;
|
|
335
|
+
if (entry.embedding) {
|
|
336
|
+
embeddedCount++;
|
|
337
|
+
}
|
|
338
|
+
if (entry.timestamp < oldestTimestamp) {
|
|
339
|
+
oldestTimestamp = entry.timestamp;
|
|
340
|
+
}
|
|
341
|
+
if (entry.timestamp > newestTimestamp) {
|
|
342
|
+
newestTimestamp = entry.timestamp;
|
|
343
|
+
}
|
|
344
|
+
totalSize += JSON.stringify(entry).length;
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
totalCount: entries.length,
|
|
348
|
+
byType,
|
|
349
|
+
byNamespace,
|
|
350
|
+
sizeBytes: totalSize,
|
|
351
|
+
oldestTimestamp: oldestTimestamp === Infinity ? 0 : oldestTimestamp,
|
|
352
|
+
newestTimestamp,
|
|
353
|
+
averageImportance: entries.length > 0 ? totalImportance / entries.length : 0,
|
|
354
|
+
embeddedCount
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Set embedding provider
|
|
359
|
+
*/
|
|
360
|
+
setEmbedding(embedding) {
|
|
361
|
+
this.embedding = embedding;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Set retrieval strategy
|
|
365
|
+
*/
|
|
366
|
+
setRetrieval(retrieval) {
|
|
367
|
+
this.retrieval = retrieval;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Set importance calculator
|
|
371
|
+
*/
|
|
372
|
+
setImportanceCalculator(calculator) {
|
|
373
|
+
this.importanceCalculator = calculator;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Get the underlying store
|
|
377
|
+
*/
|
|
378
|
+
getStore() {
|
|
379
|
+
return this.store;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Close the memory manager
|
|
383
|
+
*/
|
|
384
|
+
async close() {
|
|
385
|
+
if (this.store.close) {
|
|
386
|
+
await this.store.close();
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Default importance calculator
|
|
391
|
+
*/
|
|
392
|
+
defaultImportanceCalculator = (_content, type, metadata) => {
|
|
393
|
+
let importance = 0.5;
|
|
394
|
+
switch (type) {
|
|
395
|
+
case "fact":
|
|
396
|
+
case "preference":
|
|
397
|
+
importance = 0.8;
|
|
398
|
+
break;
|
|
399
|
+
case "event":
|
|
400
|
+
importance = 0.6;
|
|
401
|
+
break;
|
|
402
|
+
case "summary":
|
|
403
|
+
importance = 0.7;
|
|
404
|
+
break;
|
|
405
|
+
case "context":
|
|
406
|
+
importance = 0.5;
|
|
407
|
+
break;
|
|
408
|
+
case "entity":
|
|
409
|
+
case "relation":
|
|
410
|
+
importance = 0.7;
|
|
411
|
+
break;
|
|
412
|
+
default:
|
|
413
|
+
importance = 0.5;
|
|
414
|
+
}
|
|
415
|
+
if (metadata.confidence !== void 0) {
|
|
416
|
+
importance *= 0.5 + 0.5 * metadata.confidence;
|
|
417
|
+
}
|
|
418
|
+
if (metadata.source === "explicit") {
|
|
419
|
+
importance *= 1.1;
|
|
420
|
+
}
|
|
421
|
+
return Math.min(importance, 1);
|
|
422
|
+
};
|
|
423
|
+
};
|
|
424
|
+
function createMemoryManager(config) {
|
|
425
|
+
return new MemoryManager(config);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// src/integrations/AgentMemory.ts
|
|
429
|
+
import { EventEmitter as EventEmitter2 } from "eventemitter3";
|
|
430
|
+
var AgentMemory = class extends EventEmitter2 {
|
|
431
|
+
agentId;
|
|
432
|
+
namespace;
|
|
433
|
+
workingMemorySize;
|
|
434
|
+
store;
|
|
435
|
+
manager;
|
|
436
|
+
working;
|
|
437
|
+
episodic;
|
|
438
|
+
semantic;
|
|
439
|
+
conversationHistory = [];
|
|
440
|
+
constructor(config) {
|
|
441
|
+
super();
|
|
442
|
+
this.agentId = config.agentId;
|
|
443
|
+
this.namespace = config.namespace ?? "default";
|
|
444
|
+
this.workingMemorySize = config.workingMemorySize ?? 20;
|
|
445
|
+
this.store = config.store;
|
|
446
|
+
this.manager = new MemoryManager({
|
|
447
|
+
store: config.store,
|
|
448
|
+
defaultNamespace: this.namespace
|
|
449
|
+
});
|
|
450
|
+
this.working = new WorkingMemory(config.store, {
|
|
451
|
+
maxSize: this.workingMemorySize
|
|
452
|
+
});
|
|
453
|
+
this.episodic = new EpisodicMemory(config.store);
|
|
454
|
+
this.semantic = new SemanticMemory(config.store);
|
|
455
|
+
this.setupEventForwarding();
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Remember information
|
|
459
|
+
*/
|
|
460
|
+
async remember(content, options) {
|
|
461
|
+
const entryId = await this.manager.add({
|
|
462
|
+
content,
|
|
463
|
+
type: options?.type ?? "context",
|
|
464
|
+
importance: options?.importance ?? 0.5,
|
|
465
|
+
metadata: {
|
|
466
|
+
source: "explicit",
|
|
467
|
+
confidence: 1,
|
|
468
|
+
...options?.metadata,
|
|
469
|
+
agentId: this.agentId,
|
|
470
|
+
tags: options?.tags
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
const entry = await this.manager.get(entryId);
|
|
474
|
+
if (entry) {
|
|
475
|
+
await this.working.add(entry);
|
|
476
|
+
this.emit("memoryAdded", entry);
|
|
477
|
+
}
|
|
478
|
+
return entryId;
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Recall information
|
|
482
|
+
*/
|
|
483
|
+
async recall(query, options) {
|
|
484
|
+
this.working.setQuery(query);
|
|
485
|
+
const results = await this.manager.retrieve(query, {
|
|
486
|
+
limit: options?.limit ?? 10,
|
|
487
|
+
minScore: options?.minScore ?? 0.5
|
|
488
|
+
});
|
|
489
|
+
const entries = results.map((r) => r.entry);
|
|
490
|
+
let combined = entries;
|
|
491
|
+
if (options?.includeContext !== false) {
|
|
492
|
+
const workingContext = this.working.getFocused(5);
|
|
493
|
+
const workingIds = new Set(workingContext.map((e) => e.id));
|
|
494
|
+
combined = [
|
|
495
|
+
...workingContext,
|
|
496
|
+
...entries.filter((e) => !workingIds.has(e.id))
|
|
497
|
+
];
|
|
498
|
+
}
|
|
499
|
+
this.emit("memoryRetrieved", combined, query);
|
|
500
|
+
return combined;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Learn a fact
|
|
504
|
+
*/
|
|
505
|
+
async learnFact(content, options) {
|
|
506
|
+
const fact = await this.semantic.learnFact(content, {
|
|
507
|
+
agentId: this.agentId,
|
|
508
|
+
confidence: options?.confidence ?? 0.8,
|
|
509
|
+
source: options?.source
|
|
510
|
+
});
|
|
511
|
+
this.emit("memoryAdded", fact);
|
|
512
|
+
return fact.id;
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Record an event/experience
|
|
516
|
+
*/
|
|
517
|
+
async recordEvent(content, options) {
|
|
518
|
+
const entry = {
|
|
519
|
+
id: `event-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`,
|
|
520
|
+
content,
|
|
521
|
+
type: "event",
|
|
522
|
+
importance: options?.importance ?? 0.5,
|
|
523
|
+
metadata: {
|
|
524
|
+
source: "explicit",
|
|
525
|
+
confidence: 1,
|
|
526
|
+
agentId: this.agentId,
|
|
527
|
+
namespace: this.namespace,
|
|
528
|
+
participants: options?.participants,
|
|
529
|
+
location: options?.location
|
|
530
|
+
},
|
|
531
|
+
timestamp: Date.now(),
|
|
532
|
+
accessCount: 0,
|
|
533
|
+
createdAt: Date.now(),
|
|
534
|
+
updatedAt: Date.now()
|
|
535
|
+
};
|
|
536
|
+
await this.episodic.recordEvent(entry);
|
|
537
|
+
this.emit("memoryAdded", entry);
|
|
538
|
+
return entry.id;
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Add a conversation turn
|
|
542
|
+
*/
|
|
543
|
+
async addConversationTurn(turn) {
|
|
544
|
+
const fullTurn = {
|
|
545
|
+
...turn,
|
|
546
|
+
timestamp: turn.timestamp ?? Date.now()
|
|
547
|
+
};
|
|
548
|
+
this.conversationHistory.push(fullTurn);
|
|
549
|
+
if (turn.content.length > 20) {
|
|
550
|
+
await this.remember(`[${turn.role}]: ${turn.content}`, {
|
|
551
|
+
type: "context",
|
|
552
|
+
importance: turn.role === "user" ? 0.7 : 0.5,
|
|
553
|
+
metadata: {
|
|
554
|
+
conversationTurn: true,
|
|
555
|
+
role: turn.role,
|
|
556
|
+
...turn.metadata
|
|
557
|
+
}
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
this.emit("conversationUpdated", fullTurn);
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Get recent conversation
|
|
564
|
+
*/
|
|
565
|
+
getRecentConversation(turns = 10) {
|
|
566
|
+
return this.conversationHistory.slice(-turns);
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Format conversation for LLM context
|
|
570
|
+
*/
|
|
571
|
+
formatConversationForContext(turns = 10) {
|
|
572
|
+
return this.getRecentConversation(turns).map((turn) => `${turn.role}: ${turn.content}`).join("\n");
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Get working memory context
|
|
576
|
+
*/
|
|
577
|
+
getWorkingContext() {
|
|
578
|
+
return this.working.getContext();
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Clear working memory
|
|
582
|
+
*/
|
|
583
|
+
clearWorkingMemory() {
|
|
584
|
+
this.working.clear();
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Clear conversation history
|
|
588
|
+
*/
|
|
589
|
+
clearConversation() {
|
|
590
|
+
this.conversationHistory = [];
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Forget a specific memory
|
|
594
|
+
*/
|
|
595
|
+
async forget(id) {
|
|
596
|
+
return Promise.resolve(this.manager.delete(id));
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Update a memory
|
|
600
|
+
*/
|
|
601
|
+
update(id, updates) {
|
|
602
|
+
return Promise.resolve(this.manager.update(id, updates));
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Search by semantic similarity
|
|
606
|
+
*/
|
|
607
|
+
async searchSimilar(text, options) {
|
|
608
|
+
return this.manager.retrieve(text, {
|
|
609
|
+
limit: options?.topK ?? 10,
|
|
610
|
+
minScore: options?.minScore
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Get memory by ID
|
|
615
|
+
*/
|
|
616
|
+
get(id) {
|
|
617
|
+
return Promise.resolve(this.manager.get(id));
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Get agent statistics
|
|
621
|
+
*/
|
|
622
|
+
async getStats() {
|
|
623
|
+
const stats = await this.manager.getStats();
|
|
624
|
+
const semanticStats = this.semantic.getStats();
|
|
625
|
+
const currentEpisode = this.episodic.getCurrentEpisode();
|
|
626
|
+
return {
|
|
627
|
+
totalMemories: stats.byType ? Object.values(stats.byType).reduce((a, b) => a + b, 0) : 0,
|
|
628
|
+
workingMemorySize: this.working.size,
|
|
629
|
+
conversationLength: this.conversationHistory.length,
|
|
630
|
+
currentEpisode: currentEpisode ? { eventCount: currentEpisode.events.length } : null,
|
|
631
|
+
factCount: semanticStats.conceptCount
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Consolidate memories
|
|
636
|
+
*/
|
|
637
|
+
async consolidate() {
|
|
638
|
+
return Promise.resolve(this.working.consolidate(this.store));
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Get related concepts
|
|
642
|
+
*/
|
|
643
|
+
getRelatedConcepts(conceptId, depth = 1) {
|
|
644
|
+
return Promise.resolve(
|
|
645
|
+
this.semantic.getRelatedConcepts(conceptId, void 0, depth)
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Export agent memory
|
|
650
|
+
*/
|
|
651
|
+
async export() {
|
|
652
|
+
return {
|
|
653
|
+
agentId: this.agentId,
|
|
654
|
+
namespace: this.namespace,
|
|
655
|
+
conversationHistory: [...this.conversationHistory],
|
|
656
|
+
workingContext: this.working.getContext(),
|
|
657
|
+
stats: await this.getStats(),
|
|
658
|
+
exportedAt: Date.now()
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Set up event forwarding
|
|
663
|
+
*/
|
|
664
|
+
setupEventForwarding() {
|
|
665
|
+
this.working.on("contextUpdate", (context) => {
|
|
666
|
+
this.emit("contextUpdated", context);
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* Access underlying components
|
|
671
|
+
*/
|
|
672
|
+
get components() {
|
|
673
|
+
return {
|
|
674
|
+
manager: this.manager,
|
|
675
|
+
working: this.working,
|
|
676
|
+
episodic: this.episodic,
|
|
677
|
+
semantic: this.semantic
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
};
|
|
681
|
+
function createAgentMemory(config) {
|
|
682
|
+
return new AgentMemory(config);
|
|
683
|
+
}
|
|
684
|
+
export {
|
|
685
|
+
AccessControl,
|
|
686
|
+
AgentMemory,
|
|
687
|
+
Compressor,
|
|
688
|
+
Consolidator,
|
|
689
|
+
Debugger,
|
|
690
|
+
EpisodicMemory,
|
|
691
|
+
Exporter,
|
|
692
|
+
Extractor,
|
|
693
|
+
Forgetter,
|
|
694
|
+
HierarchicalMemory,
|
|
695
|
+
HybridRetrieval,
|
|
696
|
+
InMemoryStore,
|
|
697
|
+
Inspector,
|
|
698
|
+
LongTermMemory,
|
|
699
|
+
MemoryManager,
|
|
700
|
+
NamespaceManager,
|
|
701
|
+
PipelineBuilder,
|
|
702
|
+
PostgresStore,
|
|
703
|
+
RedisStore,
|
|
704
|
+
RetrievalPipeline,
|
|
705
|
+
SQLiteStore,
|
|
706
|
+
SemanticMemory,
|
|
707
|
+
SemanticRetrieval,
|
|
708
|
+
SharedMemory,
|
|
709
|
+
Summarizer,
|
|
710
|
+
TemporalRetrieval,
|
|
711
|
+
TimeWindows,
|
|
712
|
+
Timeline,
|
|
713
|
+
WorkingMemory,
|
|
714
|
+
createAccessControl,
|
|
715
|
+
createAgentMemory,
|
|
716
|
+
createCompressor,
|
|
717
|
+
createConsolidator,
|
|
718
|
+
createDebugger,
|
|
719
|
+
createEpisodicMemory,
|
|
720
|
+
createExporter,
|
|
721
|
+
createExtractor,
|
|
722
|
+
createForgetter,
|
|
723
|
+
createHierarchicalMemory,
|
|
724
|
+
createHybridRetrieval,
|
|
725
|
+
createInMemoryStore,
|
|
726
|
+
createInspector,
|
|
727
|
+
createLongTermMemory,
|
|
728
|
+
createMemoryManager,
|
|
729
|
+
createNamespaceManager,
|
|
730
|
+
createPipelineBuilder,
|
|
731
|
+
createPostgresStore,
|
|
732
|
+
createRedisStore,
|
|
733
|
+
createRetrievalPipeline,
|
|
734
|
+
createSQLiteStore,
|
|
735
|
+
createSemanticMemory,
|
|
736
|
+
createSemanticRetrieval,
|
|
737
|
+
createSharedMemory,
|
|
738
|
+
createSummarizer,
|
|
739
|
+
createTemporalRetrieval,
|
|
740
|
+
createTimeline,
|
|
741
|
+
createWorkingMemory
|
|
742
|
+
};
|