@elizaos/plugin-inmemorydb 2.0.0-alpha.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.
@@ -0,0 +1,989 @@
1
+ // typescript/index.ts
2
+ import {
3
+ logger as logger2
4
+ } from "@elizaos/core";
5
+
6
+ // typescript/adapter.ts
7
+ import {
8
+ DatabaseAdapter,
9
+ logger
10
+ } from "@elizaos/core";
11
+
12
+ // typescript/hnsw.ts
13
+ function cosineDistance(a, b) {
14
+ if (a.length !== b.length) {
15
+ throw new Error(`Vector dimension mismatch: ${a.length} vs ${b.length}`);
16
+ }
17
+ let dotProduct = 0;
18
+ let normA = 0;
19
+ let normB = 0;
20
+ for (let i = 0;i < a.length; i++) {
21
+ dotProduct += a[i] * b[i];
22
+ normA += a[i] * a[i];
23
+ normB += b[i] * b[i];
24
+ }
25
+ const magnitude = Math.sqrt(normA) * Math.sqrt(normB);
26
+ if (magnitude === 0)
27
+ return 1;
28
+ return 1 - dotProduct / magnitude;
29
+ }
30
+
31
+ class EphemeralHNSW {
32
+ nodes = new Map;
33
+ entryPoint = null;
34
+ maxLevel = 0;
35
+ dimension = 0;
36
+ config;
37
+ constructor() {
38
+ this.config = {
39
+ M: 16,
40
+ efConstruction: 200,
41
+ efSearch: 50,
42
+ mL: 1 / Math.log(16)
43
+ };
44
+ }
45
+ async init(dimension) {
46
+ this.dimension = dimension;
47
+ }
48
+ getRandomLevel() {
49
+ let level = 0;
50
+ while (Math.random() < Math.exp(-level * this.config.mL) && level < 16) {
51
+ level++;
52
+ }
53
+ return level;
54
+ }
55
+ async add(id, vector) {
56
+ if (vector.length !== this.dimension) {
57
+ throw new Error(`Vector dimension mismatch: expected ${this.dimension}, got ${vector.length}`);
58
+ }
59
+ const existing = this.nodes.get(id);
60
+ if (existing) {
61
+ existing.vector = vector;
62
+ return;
63
+ }
64
+ const level = this.getRandomLevel();
65
+ const newNode = {
66
+ id,
67
+ vector,
68
+ level,
69
+ neighbors: new Map
70
+ };
71
+ for (let l = 0;l <= level; l++) {
72
+ newNode.neighbors.set(l, new Set);
73
+ }
74
+ if (this.entryPoint === null) {
75
+ this.entryPoint = id;
76
+ this.maxLevel = level;
77
+ this.nodes.set(id, newNode);
78
+ return;
79
+ }
80
+ let currentNode = this.entryPoint;
81
+ for (let l = this.maxLevel;l > level; l--) {
82
+ currentNode = this.searchLayer(vector, currentNode, 1, l)[0]?.id ?? currentNode;
83
+ }
84
+ for (let l = Math.min(level, this.maxLevel);l >= 0; l--) {
85
+ const neighbors = this.searchLayer(vector, currentNode, this.config.efConstruction, l);
86
+ const M = this.config.M;
87
+ const selectedNeighbors = neighbors.slice(0, M);
88
+ for (const neighbor of selectedNeighbors) {
89
+ const neighborSet = newNode.neighbors.get(l);
90
+ if (neighborSet) {
91
+ neighborSet.add(neighbor.id);
92
+ }
93
+ const neighborNode = this.nodes.get(neighbor.id);
94
+ if (neighborNode) {
95
+ let neighborSet2 = neighborNode.neighbors.get(l);
96
+ if (!neighborSet2) {
97
+ neighborSet2 = new Set;
98
+ neighborNode.neighbors.set(l, neighborSet2);
99
+ }
100
+ neighborSet2.add(id);
101
+ if (neighborSet2.size > M) {
102
+ const toKeep = this.selectBestNeighbors(neighborNode.vector, neighborSet2, M);
103
+ neighborNode.neighbors.set(l, new Set(toKeep.map((n) => n.id)));
104
+ }
105
+ }
106
+ }
107
+ if (neighbors.length > 0) {
108
+ currentNode = neighbors[0].id;
109
+ }
110
+ }
111
+ this.nodes.set(id, newNode);
112
+ if (level > this.maxLevel) {
113
+ this.maxLevel = level;
114
+ this.entryPoint = id;
115
+ }
116
+ }
117
+ searchLayer(query, entryId, ef, level) {
118
+ const visited = new Set([entryId]);
119
+ const entryNode = this.nodes.get(entryId);
120
+ if (!entryNode)
121
+ return [];
122
+ const entryDist = cosineDistance(query, entryNode.vector);
123
+ const candidates = [
124
+ { id: entryId, distance: entryDist }
125
+ ];
126
+ const results = [{ id: entryId, distance: entryDist }];
127
+ while (candidates.length > 0) {
128
+ candidates.sort((a, b) => a.distance - b.distance);
129
+ const current = candidates.shift();
130
+ if (!current)
131
+ break;
132
+ results.sort((a, b) => b.distance - a.distance);
133
+ const furthestResult = results[0];
134
+ if (current.distance > furthestResult.distance) {
135
+ break;
136
+ }
137
+ const currentNode = this.nodes.get(current.id);
138
+ if (!currentNode)
139
+ continue;
140
+ const neighbors = currentNode.neighbors.get(level);
141
+ if (!neighbors)
142
+ continue;
143
+ for (const neighborId of neighbors) {
144
+ if (visited.has(neighborId))
145
+ continue;
146
+ visited.add(neighborId);
147
+ const neighborNode = this.nodes.get(neighborId);
148
+ if (!neighborNode)
149
+ continue;
150
+ const dist = cosineDistance(query, neighborNode.vector);
151
+ if (results.length < ef || dist < furthestResult.distance) {
152
+ candidates.push({ id: neighborId, distance: dist });
153
+ results.push({ id: neighborId, distance: dist });
154
+ if (results.length > ef) {
155
+ results.sort((a, b) => b.distance - a.distance);
156
+ results.pop();
157
+ }
158
+ }
159
+ }
160
+ }
161
+ results.sort((a, b) => a.distance - b.distance);
162
+ return results;
163
+ }
164
+ selectBestNeighbors(nodeVector, neighborIds, M) {
165
+ const neighbors = [];
166
+ for (const id of neighborIds) {
167
+ const node = this.nodes.get(id);
168
+ if (node) {
169
+ neighbors.push({
170
+ id,
171
+ distance: cosineDistance(nodeVector, node.vector)
172
+ });
173
+ }
174
+ }
175
+ neighbors.sort((a, b) => a.distance - b.distance);
176
+ return neighbors.slice(0, M);
177
+ }
178
+ async remove(id) {
179
+ const node = this.nodes.get(id);
180
+ if (!node)
181
+ return;
182
+ for (const [level, neighbors] of node.neighbors) {
183
+ for (const neighborId of neighbors) {
184
+ const neighborNode = this.nodes.get(neighborId);
185
+ if (neighborNode) {
186
+ neighborNode.neighbors.get(level)?.delete(id);
187
+ }
188
+ }
189
+ }
190
+ this.nodes.delete(id);
191
+ if (this.entryPoint === id) {
192
+ if (this.nodes.size === 0) {
193
+ this.entryPoint = null;
194
+ this.maxLevel = 0;
195
+ } else {
196
+ let maxLevel = 0;
197
+ let newEntry = null;
198
+ for (const [nodeId, n] of this.nodes) {
199
+ if (n.level >= maxLevel) {
200
+ maxLevel = n.level;
201
+ newEntry = nodeId;
202
+ }
203
+ }
204
+ this.entryPoint = newEntry;
205
+ this.maxLevel = maxLevel;
206
+ }
207
+ }
208
+ }
209
+ async search(query, k, threshold = 0.5) {
210
+ if (this.entryPoint === null || this.nodes.size === 0) {
211
+ return [];
212
+ }
213
+ if (query.length !== this.dimension) {
214
+ throw new Error(`Query dimension mismatch: expected ${this.dimension}, got ${query.length}`);
215
+ }
216
+ let currentNode = this.entryPoint;
217
+ for (let l = this.maxLevel;l > 0; l--) {
218
+ const closest = this.searchLayer(query, currentNode, 1, l);
219
+ if (closest.length > 0) {
220
+ currentNode = closest[0].id;
221
+ }
222
+ }
223
+ const results = this.searchLayer(query, currentNode, Math.max(k, this.config.efSearch), 0);
224
+ return results.slice(0, k).filter((r) => 1 - r.distance >= threshold).map((r) => ({
225
+ id: r.id,
226
+ distance: r.distance,
227
+ similarity: 1 - r.distance
228
+ }));
229
+ }
230
+ async clear() {
231
+ this.nodes.clear();
232
+ this.entryPoint = null;
233
+ this.maxLevel = 0;
234
+ }
235
+ size() {
236
+ return this.nodes.size;
237
+ }
238
+ }
239
+
240
+ // typescript/types.ts
241
+ var COLLECTIONS = {
242
+ AGENTS: "agents",
243
+ ENTITIES: "entities",
244
+ MEMORIES: "memories",
245
+ ROOMS: "rooms",
246
+ WORLDS: "worlds",
247
+ COMPONENTS: "components",
248
+ RELATIONSHIPS: "relationships",
249
+ PARTICIPANTS: "participants",
250
+ TASKS: "tasks",
251
+ CACHE: "cache",
252
+ LOGS: "logs",
253
+ EMBEDDINGS: "embeddings"
254
+ };
255
+
256
+ // typescript/adapter.ts
257
+ function toMemory(stored) {
258
+ return {
259
+ id: stored.id,
260
+ entityId: stored.entityId,
261
+ agentId: stored.agentId,
262
+ createdAt: stored.createdAt,
263
+ content: stored.content,
264
+ embedding: stored.embedding,
265
+ roomId: stored.roomId,
266
+ worldId: stored.worldId,
267
+ unique: stored.unique,
268
+ similarity: stored.similarity,
269
+ metadata: stored.metadata
270
+ };
271
+ }
272
+ function toMemories(stored) {
273
+ return stored.map(toMemory);
274
+ }
275
+
276
+ class InMemoryDatabaseAdapter extends DatabaseAdapter {
277
+ storage;
278
+ vectorIndex;
279
+ embeddingDimension = 384;
280
+ ready = false;
281
+ agentId;
282
+ constructor(storage, agentId) {
283
+ super();
284
+ this.storage = storage;
285
+ this.agentId = agentId;
286
+ this.db = storage;
287
+ this.vectorIndex = new EphemeralHNSW;
288
+ }
289
+ async initialize() {
290
+ await this.init();
291
+ }
292
+ async init() {
293
+ await this.storage.init();
294
+ await this.vectorIndex.init(this.embeddingDimension);
295
+ this.ready = true;
296
+ logger.info({ src: "plugin:inmemorydb" }, "In-memory database initialized");
297
+ }
298
+ async runPluginMigrations(_plugins, _options) {
299
+ logger.debug({ src: "plugin:inmemorydb" }, "Plugin migrations not needed for in-memory storage");
300
+ }
301
+ async isReady() {
302
+ return this.ready && await this.storage.isReady();
303
+ }
304
+ async close() {
305
+ await this.vectorIndex.clear();
306
+ await this.storage.close();
307
+ this.ready = false;
308
+ logger.info({ src: "plugin:inmemorydb" }, "In-memory database closed");
309
+ }
310
+ async getConnection() {
311
+ return this.storage;
312
+ }
313
+ async getAgent(agentId) {
314
+ return this.storage.get(COLLECTIONS.AGENTS, agentId);
315
+ }
316
+ async getAgents() {
317
+ return this.storage.getAll(COLLECTIONS.AGENTS);
318
+ }
319
+ async createAgent(agent) {
320
+ if (!agent.id)
321
+ return false;
322
+ await this.storage.set(COLLECTIONS.AGENTS, agent.id, agent);
323
+ return true;
324
+ }
325
+ async updateAgent(agentId, agent) {
326
+ const existing = await this.getAgent(agentId);
327
+ if (!existing)
328
+ return false;
329
+ await this.storage.set(COLLECTIONS.AGENTS, agentId, {
330
+ ...existing,
331
+ ...agent
332
+ });
333
+ return true;
334
+ }
335
+ async deleteAgent(agentId) {
336
+ return this.storage.delete(COLLECTIONS.AGENTS, agentId);
337
+ }
338
+ async ensureEmbeddingDimension(dimension) {
339
+ if (this.embeddingDimension !== dimension) {
340
+ this.embeddingDimension = dimension;
341
+ await this.vectorIndex.init(dimension);
342
+ }
343
+ }
344
+ async getEntitiesByIds(entityIds) {
345
+ const entities = [];
346
+ for (const id of entityIds) {
347
+ const entity = await this.storage.get(COLLECTIONS.ENTITIES, id);
348
+ if (entity)
349
+ entities.push(entity);
350
+ }
351
+ return entities.length > 0 ? entities : null;
352
+ }
353
+ async getEntitiesForRoom(roomId, includeComponents = false) {
354
+ const participants = await this.storage.getWhere(COLLECTIONS.PARTICIPANTS, (p) => p.roomId === roomId);
355
+ const entityIds = participants.map((p) => p.entityId);
356
+ const entities = [];
357
+ for (const entityId of entityIds) {
358
+ const entity = await this.storage.get(COLLECTIONS.ENTITIES, entityId);
359
+ if (entity) {
360
+ if (includeComponents) {
361
+ const components = await this.getComponents(entityId);
362
+ entity.components = components;
363
+ }
364
+ entities.push(entity);
365
+ }
366
+ }
367
+ return entities;
368
+ }
369
+ async createEntities(entities) {
370
+ for (const entity of entities) {
371
+ if (!entity.id)
372
+ continue;
373
+ await this.storage.set(COLLECTIONS.ENTITIES, entity.id, entity);
374
+ }
375
+ return true;
376
+ }
377
+ async updateEntity(entity) {
378
+ if (!entity.id)
379
+ return;
380
+ await this.storage.set(COLLECTIONS.ENTITIES, entity.id, entity);
381
+ }
382
+ async getComponent(entityId, type, worldId, sourceEntityId) {
383
+ const components = await this.storage.getWhere(COLLECTIONS.COMPONENTS, (c) => c.entityId === entityId && c.type === type && (worldId === undefined || c.worldId === worldId) && (sourceEntityId === undefined || c.sourceEntityId === sourceEntityId));
384
+ return components[0] ?? null;
385
+ }
386
+ async getComponents(entityId, worldId, sourceEntityId) {
387
+ return this.storage.getWhere(COLLECTIONS.COMPONENTS, (c) => c.entityId === entityId && (worldId === undefined || c.worldId === worldId) && (sourceEntityId === undefined || c.sourceEntityId === sourceEntityId));
388
+ }
389
+ async createComponent(component) {
390
+ if (!component.id)
391
+ return false;
392
+ await this.storage.set(COLLECTIONS.COMPONENTS, component.id, component);
393
+ return true;
394
+ }
395
+ async updateComponent(component) {
396
+ if (!component.id)
397
+ return;
398
+ await this.storage.set(COLLECTIONS.COMPONENTS, component.id, component);
399
+ }
400
+ async deleteComponent(componentId) {
401
+ await this.storage.delete(COLLECTIONS.COMPONENTS, componentId);
402
+ }
403
+ async getMemories(params) {
404
+ let memories = await this.storage.getWhere(COLLECTIONS.MEMORIES, (m) => {
405
+ if (params.entityId && m.entityId !== params.entityId)
406
+ return false;
407
+ if (params.agentId && m.agentId !== params.agentId)
408
+ return false;
409
+ if (params.roomId && m.roomId !== params.roomId)
410
+ return false;
411
+ if (params.worldId && m.worldId !== params.worldId)
412
+ return false;
413
+ if (params.tableName && m.metadata?.type !== params.tableName)
414
+ return false;
415
+ if (params.start && m.createdAt && m.createdAt < params.start)
416
+ return false;
417
+ if (params.end && m.createdAt && m.createdAt > params.end)
418
+ return false;
419
+ if (params.unique && !m.unique)
420
+ return false;
421
+ return true;
422
+ });
423
+ memories.sort((a, b) => (b.createdAt ?? 0) - (a.createdAt ?? 0));
424
+ if (params.offset) {
425
+ memories = memories.slice(params.offset);
426
+ }
427
+ if (params.count) {
428
+ memories = memories.slice(0, params.count);
429
+ }
430
+ return toMemories(memories);
431
+ }
432
+ async getMemoriesByRoomIds(params) {
433
+ const memories = await this.storage.getWhere(COLLECTIONS.MEMORIES, (m) => params.roomIds.includes(m.roomId) && (params.tableName ? m.metadata?.type === params.tableName : true));
434
+ memories.sort((a, b) => (b.createdAt ?? 0) - (a.createdAt ?? 0));
435
+ if (params.limit) {
436
+ return toMemories(memories.slice(0, params.limit));
437
+ }
438
+ return toMemories(memories);
439
+ }
440
+ async getMemoryById(id) {
441
+ return this.storage.get(COLLECTIONS.MEMORIES, id);
442
+ }
443
+ async getMemoriesByIds(memoryIds, tableName) {
444
+ const memories = [];
445
+ for (const id of memoryIds) {
446
+ const memory = await this.storage.get(COLLECTIONS.MEMORIES, id);
447
+ if (memory) {
448
+ if (tableName && memory.metadata?.type !== tableName)
449
+ continue;
450
+ memories.push(toMemory(memory));
451
+ }
452
+ }
453
+ return memories;
454
+ }
455
+ async getCachedEmbeddings(params) {
456
+ const memories = await this.storage.getWhere(COLLECTIONS.MEMORIES, (m) => m.metadata?.type === params.query_table_name);
457
+ const results = [];
458
+ for (const memory of memories) {
459
+ if (!memory.embedding)
460
+ continue;
461
+ const memoryRecord = memory;
462
+ const fieldValue = memoryRecord[params.query_field_name];
463
+ const content = String(fieldValue ?? "");
464
+ const score = this.simpleStringScore(params.query_input, content);
465
+ if (score <= params.query_threshold) {
466
+ results.push({
467
+ embedding: memory.embedding,
468
+ levenshtein_score: score
469
+ });
470
+ }
471
+ }
472
+ return results.slice(0, params.query_match_count);
473
+ }
474
+ simpleStringScore(a, b) {
475
+ if (a === b)
476
+ return 0;
477
+ const aLower = a.toLowerCase();
478
+ const bLower = b.toLowerCase();
479
+ if (aLower === bLower)
480
+ return 0.1;
481
+ if (aLower.includes(bLower) || bLower.includes(aLower))
482
+ return 0.3;
483
+ return 1;
484
+ }
485
+ async log(params) {
486
+ const id = crypto.randomUUID();
487
+ const log = {
488
+ id,
489
+ entityId: params.entityId,
490
+ roomId: params.roomId,
491
+ body: params.body,
492
+ type: params.type,
493
+ createdAt: new Date
494
+ };
495
+ await this.storage.set(COLLECTIONS.LOGS, id, log);
496
+ }
497
+ async getLogs(params) {
498
+ let logs = await this.storage.getWhere(COLLECTIONS.LOGS, (l) => {
499
+ if (params.entityId && l.entityId !== params.entityId)
500
+ return false;
501
+ if (params.roomId && l.roomId !== params.roomId)
502
+ return false;
503
+ if (params.type && l.type !== params.type)
504
+ return false;
505
+ return true;
506
+ });
507
+ logs.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
508
+ if (params.offset) {
509
+ logs = logs.slice(params.offset);
510
+ }
511
+ if (params.count) {
512
+ logs = logs.slice(0, params.count);
513
+ }
514
+ return logs;
515
+ }
516
+ async deleteLog(logId) {
517
+ await this.storage.delete(COLLECTIONS.LOGS, logId);
518
+ }
519
+ async searchMemories(params) {
520
+ const threshold = params.match_threshold ?? 0.5;
521
+ const count = params.count ?? 10;
522
+ const results = await this.vectorIndex.search(params.embedding, count * 2, threshold);
523
+ const memories = [];
524
+ for (const result of results) {
525
+ const memory = await this.storage.get(COLLECTIONS.MEMORIES, result.id);
526
+ if (!memory)
527
+ continue;
528
+ if (params.tableName && memory.metadata?.type !== params.tableName)
529
+ continue;
530
+ if (params.roomId && memory.roomId !== params.roomId)
531
+ continue;
532
+ if (params.worldId && memory.worldId !== params.worldId)
533
+ continue;
534
+ if (params.entityId && memory.entityId !== params.entityId)
535
+ continue;
536
+ if (params.unique && !memory.unique)
537
+ continue;
538
+ memories.push({
539
+ ...toMemory(memory),
540
+ similarity: result.similarity
541
+ });
542
+ }
543
+ return memories.slice(0, count);
544
+ }
545
+ async createMemory(memory, tableName, unique = false) {
546
+ const id = memory.id ?? crypto.randomUUID();
547
+ const now = Date.now();
548
+ const storedMemory = {
549
+ ...memory,
550
+ id,
551
+ agentId: memory.agentId ?? this.agentId,
552
+ unique: unique || memory.unique,
553
+ createdAt: memory.createdAt ?? now,
554
+ metadata: {
555
+ ...memory.metadata ?? {},
556
+ type: tableName
557
+ }
558
+ };
559
+ await this.storage.set(COLLECTIONS.MEMORIES, id, storedMemory);
560
+ if (memory.embedding && memory.embedding.length > 0) {
561
+ await this.vectorIndex.add(id, memory.embedding);
562
+ }
563
+ return id;
564
+ }
565
+ async updateMemory(memory) {
566
+ const existing = await this.getMemoryById(memory.id);
567
+ if (!existing)
568
+ return false;
569
+ const updated = {
570
+ ...existing,
571
+ ...memory,
572
+ metadata: {
573
+ ...existing.metadata ?? {},
574
+ ...memory.metadata ?? {}
575
+ }
576
+ };
577
+ await this.storage.set(COLLECTIONS.MEMORIES, memory.id, updated);
578
+ if (memory.embedding && memory.embedding.length > 0) {
579
+ await this.vectorIndex.add(memory.id, memory.embedding);
580
+ }
581
+ return true;
582
+ }
583
+ async deleteMemory(memoryId) {
584
+ await this.storage.delete(COLLECTIONS.MEMORIES, memoryId);
585
+ await this.vectorIndex.remove(memoryId);
586
+ }
587
+ async deleteManyMemories(memoryIds) {
588
+ for (const id of memoryIds) {
589
+ await this.deleteMemory(id);
590
+ }
591
+ }
592
+ async deleteAllMemories(roomId, tableName) {
593
+ const memories = await this.getMemories({ roomId, tableName });
594
+ await this.deleteManyMemories(memories.map((m) => m.id).filter((id) => id !== undefined));
595
+ }
596
+ async countMemories(roomId, unique = false, tableName) {
597
+ return this.storage.count(COLLECTIONS.MEMORIES, (memory) => {
598
+ if (memory.roomId !== roomId)
599
+ return false;
600
+ if (unique && !memory.unique)
601
+ return false;
602
+ if (tableName && memory.metadata?.type !== tableName)
603
+ return false;
604
+ return true;
605
+ });
606
+ }
607
+ async getMemoriesByWorldId(params) {
608
+ const memories = await this.storage.getWhere(COLLECTIONS.MEMORIES, (m) => m.worldId === params.worldId && (params.tableName ? m.metadata?.type === params.tableName : true));
609
+ memories.sort((a, b) => (b.createdAt ?? 0) - (a.createdAt ?? 0));
610
+ if (params.count) {
611
+ return toMemories(memories.slice(0, params.count));
612
+ }
613
+ return toMemories(memories);
614
+ }
615
+ async createWorld(world) {
616
+ const id = world.id ?? crypto.randomUUID();
617
+ await this.storage.set(COLLECTIONS.WORLDS, id, { ...world, id });
618
+ return id;
619
+ }
620
+ async getWorld(id) {
621
+ return this.storage.get(COLLECTIONS.WORLDS, id);
622
+ }
623
+ async removeWorld(id) {
624
+ await this.storage.delete(COLLECTIONS.WORLDS, id);
625
+ }
626
+ async getAllWorlds() {
627
+ return this.storage.getAll(COLLECTIONS.WORLDS);
628
+ }
629
+ async updateWorld(world) {
630
+ if (!world.id)
631
+ return;
632
+ await this.storage.set(COLLECTIONS.WORLDS, world.id, world);
633
+ }
634
+ async getRoomsByIds(roomIds) {
635
+ const rooms = [];
636
+ for (const id of roomIds) {
637
+ const room = await this.storage.get(COLLECTIONS.ROOMS, id);
638
+ if (room)
639
+ rooms.push(room);
640
+ }
641
+ return rooms.length > 0 ? rooms : null;
642
+ }
643
+ async createRooms(rooms) {
644
+ const ids = [];
645
+ for (const room of rooms) {
646
+ const id = room.id ?? crypto.randomUUID();
647
+ await this.storage.set(COLLECTIONS.ROOMS, id, { ...room, id });
648
+ ids.push(id);
649
+ }
650
+ return ids;
651
+ }
652
+ async deleteRoom(roomId) {
653
+ await this.storage.delete(COLLECTIONS.ROOMS, roomId);
654
+ await this.storage.deleteWhere(COLLECTIONS.PARTICIPANTS, (p) => p.roomId === roomId);
655
+ await this.storage.deleteWhere(COLLECTIONS.MEMORIES, (m) => m.roomId === roomId);
656
+ }
657
+ async deleteRoomsByWorldId(worldId) {
658
+ const rooms = await this.getRoomsByWorld(worldId);
659
+ for (const room of rooms) {
660
+ if (room.id) {
661
+ await this.deleteRoom(room.id);
662
+ }
663
+ }
664
+ }
665
+ async updateRoom(room) {
666
+ if (!room.id)
667
+ return;
668
+ await this.storage.set(COLLECTIONS.ROOMS, room.id, room);
669
+ }
670
+ async getRoomsForParticipant(entityId) {
671
+ const participants = await this.storage.getWhere(COLLECTIONS.PARTICIPANTS, (p) => p.entityId === entityId);
672
+ return participants.map((p) => p.roomId);
673
+ }
674
+ async getRoomsForParticipants(userIds) {
675
+ const participants = await this.storage.getWhere(COLLECTIONS.PARTICIPANTS, (p) => userIds.includes(p.entityId));
676
+ return [...new Set(participants.map((p) => p.roomId))];
677
+ }
678
+ async getRoomsByWorld(worldId) {
679
+ return this.storage.getWhere(COLLECTIONS.ROOMS, (r) => r.worldId === worldId);
680
+ }
681
+ async removeParticipant(entityId, roomId) {
682
+ const participants = await this.storage.getWhere(COLLECTIONS.PARTICIPANTS, (p) => p.entityId === entityId && p.roomId === roomId);
683
+ if (participants.length === 0)
684
+ return false;
685
+ for (const p of participants) {
686
+ if (p.id) {
687
+ await this.storage.delete(COLLECTIONS.PARTICIPANTS, p.id);
688
+ }
689
+ }
690
+ return true;
691
+ }
692
+ async getParticipantsForEntity(entityId) {
693
+ const stored = await this.storage.getWhere(COLLECTIONS.PARTICIPANTS, (p) => p.entityId === entityId);
694
+ const participants = [];
695
+ for (const p of stored) {
696
+ const entity = await this.storage.get(COLLECTIONS.ENTITIES, p.entityId);
697
+ if (entity) {
698
+ participants.push({
699
+ id: p.id,
700
+ entity
701
+ });
702
+ }
703
+ }
704
+ return participants;
705
+ }
706
+ async getParticipantsForRoom(roomId) {
707
+ const participants = await this.storage.getWhere(COLLECTIONS.PARTICIPANTS, (p) => p.roomId === roomId);
708
+ return participants.map((p) => p.entityId);
709
+ }
710
+ async isRoomParticipant(roomId, entityId) {
711
+ const participants = await this.storage.getWhere(COLLECTIONS.PARTICIPANTS, (p) => p.roomId === roomId && p.entityId === entityId);
712
+ return participants.length > 0;
713
+ }
714
+ async addParticipantsRoom(entityIds, roomId) {
715
+ for (const entityId of entityIds) {
716
+ const exists = await this.isRoomParticipant(roomId, entityId);
717
+ if (!exists) {
718
+ const id = crypto.randomUUID();
719
+ const participant = {
720
+ id,
721
+ entityId,
722
+ roomId
723
+ };
724
+ await this.storage.set(COLLECTIONS.PARTICIPANTS, id, participant);
725
+ }
726
+ }
727
+ return true;
728
+ }
729
+ async getParticipantUserState(roomId, entityId) {
730
+ const participants = await this.storage.getWhere(COLLECTIONS.PARTICIPANTS, (p) => p.roomId === roomId && p.entityId === entityId);
731
+ if (participants.length === 0)
732
+ return null;
733
+ const state = participants[0].userState;
734
+ if (state === "FOLLOWED" || state === "MUTED")
735
+ return state;
736
+ return null;
737
+ }
738
+ async setParticipantUserState(roomId, entityId, state) {
739
+ const participants = await this.storage.getWhere(COLLECTIONS.PARTICIPANTS, (p) => p.roomId === roomId && p.entityId === entityId);
740
+ for (const p of participants) {
741
+ if (p.id) {
742
+ await this.storage.set(COLLECTIONS.PARTICIPANTS, p.id, {
743
+ ...p,
744
+ userState: state
745
+ });
746
+ }
747
+ }
748
+ }
749
+ async createRelationship(params) {
750
+ const id = crypto.randomUUID();
751
+ const relationship = {
752
+ id,
753
+ sourceEntityId: params.sourceEntityId,
754
+ targetEntityId: params.targetEntityId,
755
+ agentId: this.agentId,
756
+ tags: params.tags ?? [],
757
+ metadata: params.metadata ?? {},
758
+ createdAt: new Date().toISOString()
759
+ };
760
+ await this.storage.set(COLLECTIONS.RELATIONSHIPS, id, relationship);
761
+ return true;
762
+ }
763
+ async getRelationship(params) {
764
+ const relationships = await this.storage.getWhere(COLLECTIONS.RELATIONSHIPS, (r2) => r2.sourceEntityId === params.sourceEntityId && r2.targetEntityId === params.targetEntityId);
765
+ if (relationships.length === 0)
766
+ return null;
767
+ const r = relationships[0];
768
+ return {
769
+ id: r.id,
770
+ sourceEntityId: r.sourceEntityId,
771
+ targetEntityId: r.targetEntityId,
772
+ agentId: r.agentId ?? this.agentId,
773
+ tags: r.tags ?? [],
774
+ metadata: r.metadata ?? {},
775
+ createdAt: r.createdAt
776
+ };
777
+ }
778
+ async getRelationships(params) {
779
+ const stored = await this.storage.getWhere(COLLECTIONS.RELATIONSHIPS, (r) => {
780
+ const isInvolved = r.sourceEntityId === params.entityId || r.targetEntityId === params.entityId;
781
+ if (!isInvolved)
782
+ return false;
783
+ if (params.tags && params.tags.length > 0) {
784
+ return params.tags.some((tag) => r.tags?.includes(tag));
785
+ }
786
+ return true;
787
+ });
788
+ return stored.map((r) => ({
789
+ id: r.id,
790
+ sourceEntityId: r.sourceEntityId,
791
+ targetEntityId: r.targetEntityId,
792
+ agentId: r.agentId ?? this.agentId,
793
+ tags: r.tags ?? [],
794
+ metadata: r.metadata ?? {},
795
+ createdAt: r.createdAt
796
+ }));
797
+ }
798
+ async updateRelationship(relationship) {
799
+ const existing = await this.getRelationship({
800
+ sourceEntityId: relationship.sourceEntityId,
801
+ targetEntityId: relationship.targetEntityId
802
+ });
803
+ if (!existing || !existing.id)
804
+ return;
805
+ const stored = {
806
+ id: existing.id,
807
+ sourceEntityId: relationship.sourceEntityId,
808
+ targetEntityId: relationship.targetEntityId,
809
+ agentId: relationship.agentId,
810
+ tags: relationship.tags ?? existing.tags ?? [],
811
+ metadata: { ...existing.metadata ?? {}, ...relationship.metadata ?? {} },
812
+ createdAt: existing.createdAt ?? new Date().toISOString()
813
+ };
814
+ await this.storage.set(COLLECTIONS.RELATIONSHIPS, existing.id, stored);
815
+ }
816
+ async getCache(key) {
817
+ const cached = await this.storage.get(COLLECTIONS.CACHE, key);
818
+ if (!cached)
819
+ return;
820
+ if (cached.expiresAt && Date.now() > cached.expiresAt) {
821
+ await this.deleteCache(key);
822
+ return;
823
+ }
824
+ return cached.value;
825
+ }
826
+ async setCache(key, value) {
827
+ await this.storage.set(COLLECTIONS.CACHE, key, { value });
828
+ return true;
829
+ }
830
+ async deleteCache(key) {
831
+ return this.storage.delete(COLLECTIONS.CACHE, key);
832
+ }
833
+ async createTask(task) {
834
+ const id = task.id ?? crypto.randomUUID();
835
+ await this.storage.set(COLLECTIONS.TASKS, id, { ...task, id });
836
+ return id;
837
+ }
838
+ async getTasks(params) {
839
+ return this.storage.getWhere(COLLECTIONS.TASKS, (t) => {
840
+ if (params.roomId && t.roomId !== params.roomId)
841
+ return false;
842
+ if (params.entityId && t.entityId !== params.entityId)
843
+ return false;
844
+ if (params.tags && params.tags.length > 0) {
845
+ if (!t.tags?.some((tag) => params.tags?.includes(tag)))
846
+ return false;
847
+ }
848
+ return true;
849
+ });
850
+ }
851
+ async getTask(id) {
852
+ return this.storage.get(COLLECTIONS.TASKS, id);
853
+ }
854
+ async getTasksByName(name) {
855
+ return this.storage.getWhere(COLLECTIONS.TASKS, (t) => t.name === name);
856
+ }
857
+ async updateTask(id, task) {
858
+ const existing = await this.getTask(id);
859
+ if (!existing)
860
+ return;
861
+ await this.storage.set(COLLECTIONS.TASKS, id, { ...existing, ...task });
862
+ }
863
+ async deleteTask(id) {
864
+ await this.storage.delete(COLLECTIONS.TASKS, id);
865
+ }
866
+ }
867
+
868
+ // typescript/storage-memory.ts
869
+ class MemoryStorage {
870
+ collections = new Map;
871
+ ready = false;
872
+ async init() {
873
+ this.ready = true;
874
+ }
875
+ async close() {
876
+ this.collections.clear();
877
+ this.ready = false;
878
+ }
879
+ async isReady() {
880
+ return this.ready;
881
+ }
882
+ getCollection(collection) {
883
+ let col = this.collections.get(collection);
884
+ if (!col) {
885
+ col = new Map;
886
+ this.collections.set(collection, col);
887
+ }
888
+ return col;
889
+ }
890
+ async get(collection, id) {
891
+ const col = this.getCollection(collection);
892
+ const item = col.get(id);
893
+ return item !== undefined ? item : null;
894
+ }
895
+ async getAll(collection) {
896
+ const col = this.getCollection(collection);
897
+ return Array.from(col.values());
898
+ }
899
+ async getWhere(collection, predicate) {
900
+ const all = await this.getAll(collection);
901
+ return all.filter(predicate);
902
+ }
903
+ async set(collection, id, data) {
904
+ const col = this.getCollection(collection);
905
+ col.set(id, data);
906
+ }
907
+ async delete(collection, id) {
908
+ const col = this.getCollection(collection);
909
+ return col.delete(id);
910
+ }
911
+ async deleteMany(collection, ids) {
912
+ const col = this.getCollection(collection);
913
+ for (const id of ids) {
914
+ col.delete(id);
915
+ }
916
+ }
917
+ async deleteWhere(collection, predicate) {
918
+ const col = this.getCollection(collection);
919
+ const toDelete = [];
920
+ for (const [id, item] of col) {
921
+ if (predicate(item)) {
922
+ toDelete.push(id);
923
+ }
924
+ }
925
+ for (const id of toDelete) {
926
+ col.delete(id);
927
+ }
928
+ }
929
+ async count(collection, predicate) {
930
+ const col = this.getCollection(collection);
931
+ if (!predicate) {
932
+ return col.size;
933
+ }
934
+ let count = 0;
935
+ for (const item of col.values()) {
936
+ if (predicate(item)) {
937
+ count++;
938
+ }
939
+ }
940
+ return count;
941
+ }
942
+ async clear() {
943
+ this.collections.clear();
944
+ }
945
+ }
946
+
947
+ // typescript/index.ts
948
+ var GLOBAL_SINGLETONS = Symbol.for("@elizaos/plugin-inmemorydb/global-singletons");
949
+ var globalSymbols = globalThis;
950
+ if (!globalSymbols[GLOBAL_SINGLETONS]) {
951
+ globalSymbols[GLOBAL_SINGLETONS] = {};
952
+ }
953
+ var globalSingletons = globalSymbols[GLOBAL_SINGLETONS];
954
+ function createDatabaseAdapter(agentId) {
955
+ if (!globalSingletons.storageManager) {
956
+ globalSingletons.storageManager = new MemoryStorage;
957
+ }
958
+ return new InMemoryDatabaseAdapter(globalSingletons.storageManager, agentId);
959
+ }
960
+ var plugin = {
961
+ name: "@elizaos/plugin-inmemorydb",
962
+ description: "Pure in-memory, ephemeral database storage for elizaOS - no persistence",
963
+ async init(_config, runtime) {
964
+ logger2.info({ src: "plugin:inmemorydb" }, "Initializing in-memory database plugin");
965
+ const runtimeWithAdapter = runtime;
966
+ const hasAdapter = runtimeWithAdapter.adapter !== undefined || runtimeWithAdapter.databaseAdapter !== undefined || (runtimeWithAdapter.hasDatabaseAdapter?.() ?? false);
967
+ if (hasAdapter) {
968
+ logger2.debug({ src: "plugin:inmemorydb" }, "Database adapter already exists, skipping initialization");
969
+ return;
970
+ }
971
+ const adapter = createDatabaseAdapter(runtime.agentId);
972
+ await adapter.init();
973
+ runtime.registerDatabaseAdapter(adapter);
974
+ logger2.success({ src: "plugin:inmemorydb" }, "In-memory database adapter registered successfully");
975
+ }
976
+ };
977
+ var typescript_default = plugin;
978
+ export {
979
+ plugin,
980
+ typescript_default as default,
981
+ createDatabaseAdapter,
982
+ MemoryStorage,
983
+ InMemoryDatabaseAdapter,
984
+ EphemeralHNSW,
985
+ COLLECTIONS
986
+ };
987
+
988
+ //# debugId=5063BCFF84A0E87D64756E2164756E21
989
+ //# sourceMappingURL=index.js.map