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