@elizaos/plugin-localdb 2.0.0-alpha.4 → 2.0.0-alpha.5

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