@elizaos/plugin-sql 2.0.0-alpha.14 → 2.0.0-alpha.15

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.
@@ -6055,6 +6055,546 @@ class BaseDrizzleAdapter extends import_core10.DatabaseAdapter {
6055
6055
  await this.db.delete(pairingAllowlistTable).where(import_drizzle_orm28.eq(pairingAllowlistTable.id, id));
6056
6056
  });
6057
6057
  }
6058
+ async isReady() {
6059
+ try {
6060
+ await this.db.execute(import_drizzle_orm28.sql`SELECT 1`);
6061
+ return true;
6062
+ } catch {
6063
+ return false;
6064
+ }
6065
+ }
6066
+ async getConnection() {
6067
+ return this.db;
6068
+ }
6069
+ async transaction(callback, _options) {
6070
+ return callback(this);
6071
+ }
6072
+ async getComponentsByNaturalKeys(keys) {
6073
+ if (keys.length === 0)
6074
+ return [];
6075
+ const results = [];
6076
+ for (const key of keys) {
6077
+ const component = await this.getComponent(key.entityId, key.type, key.worldId, key.sourceEntityId);
6078
+ results.push(component);
6079
+ }
6080
+ return results;
6081
+ }
6082
+ async getComponentsForEntities(entityIds, worldId, sourceEntityId) {
6083
+ if (entityIds.length === 0)
6084
+ return [];
6085
+ return this.withDatabase(async () => {
6086
+ const conditions = [import_drizzle_orm28.inArray(componentTable.entityId, entityIds)];
6087
+ if (worldId) {
6088
+ conditions.push(import_drizzle_orm28.eq(componentTable.worldId, worldId));
6089
+ }
6090
+ if (sourceEntityId) {
6091
+ conditions.push(import_drizzle_orm28.eq(componentTable.sourceEntityId, sourceEntityId));
6092
+ }
6093
+ const result = await this.db.select().from(componentTable).where(import_drizzle_orm28.and(...conditions));
6094
+ return result.map((component) => ({
6095
+ ...component,
6096
+ id: component.id,
6097
+ entityId: component.entityId,
6098
+ agentId: component.agentId,
6099
+ roomId: component.roomId,
6100
+ worldId: component.worldId ?? "",
6101
+ sourceEntityId: component.sourceEntityId ?? "",
6102
+ data: component.data,
6103
+ createdAt: component.createdAt.getTime()
6104
+ }));
6105
+ });
6106
+ }
6107
+ async createComponents(components) {
6108
+ if (components.length === 0)
6109
+ return [];
6110
+ return this.withDatabase(async () => {
6111
+ const ids = [];
6112
+ for (const component of components) {
6113
+ const success = await this.createComponent(component);
6114
+ if (success)
6115
+ ids.push(component.id);
6116
+ }
6117
+ return ids;
6118
+ });
6119
+ }
6120
+ async getComponentsByIds(componentIds) {
6121
+ if (componentIds.length === 0)
6122
+ return [];
6123
+ return this.withDatabase(async () => {
6124
+ const result = await this.db.select().from(componentTable).where(import_drizzle_orm28.inArray(componentTable.id, componentIds));
6125
+ return result.map((component) => ({
6126
+ ...component,
6127
+ id: component.id,
6128
+ entityId: component.entityId,
6129
+ agentId: component.agentId,
6130
+ roomId: component.roomId,
6131
+ worldId: component.worldId ?? "",
6132
+ sourceEntityId: component.sourceEntityId ?? "",
6133
+ data: component.data,
6134
+ createdAt: component.createdAt.getTime()
6135
+ }));
6136
+ });
6137
+ }
6138
+ async updateComponents(components) {
6139
+ for (const component of components) {
6140
+ await this.updateComponent(component);
6141
+ }
6142
+ }
6143
+ async deleteComponents(componentIds) {
6144
+ if (componentIds.length === 0)
6145
+ return;
6146
+ return this.withDatabase(async () => {
6147
+ await this.db.delete(componentTable).where(import_drizzle_orm28.inArray(componentTable.id, componentIds));
6148
+ });
6149
+ }
6150
+ async upsertComponents(components, _options) {
6151
+ for (const component of components) {
6152
+ const existing = await this.getComponent(component.entityId, component.type, component.worldId, component.sourceEntityId);
6153
+ if (existing) {
6154
+ await this.updateComponent({ ...component, id: existing.id });
6155
+ } else {
6156
+ await this.createComponent(component);
6157
+ }
6158
+ }
6159
+ }
6160
+ async patchComponents(_updates, _options) {}
6161
+ async upsertEntities(entities) {
6162
+ for (const entity of entities) {
6163
+ const existing = await this.getEntitiesByIds([entity.id]);
6164
+ if (existing && existing.length > 0) {
6165
+ await this.updateEntity(entity);
6166
+ } else {
6167
+ await this.createEntities([entity]);
6168
+ }
6169
+ }
6170
+ }
6171
+ async queryEntities(params) {
6172
+ if (params.entityIds?.length) {
6173
+ const entities = await this.getEntitiesByIds(params.entityIds);
6174
+ return entities || [];
6175
+ }
6176
+ return this.withDatabase(async () => {
6177
+ const conditions = [];
6178
+ if (params.agentId) {
6179
+ conditions.push(import_drizzle_orm28.eq(entityTable.agentId, params.agentId));
6180
+ }
6181
+ if (params.componentType) {
6182
+ const subquery = import_drizzle_orm28.sql`EXISTS (
6183
+ SELECT 1 FROM ${componentTable}
6184
+ WHERE ${componentTable.entityId} = ${entityTable.id}
6185
+ AND ${componentTable.type} = ${params.componentType}
6186
+ )`;
6187
+ conditions.push(subquery);
6188
+ }
6189
+ let query = this.db.select().from(entityTable).where(conditions.length > 0 ? import_drizzle_orm28.and(...conditions) : undefined);
6190
+ if (params.limit) {
6191
+ query = query.limit(params.limit);
6192
+ }
6193
+ if (params.offset) {
6194
+ query = query.offset(params.offset);
6195
+ }
6196
+ const result = await query;
6197
+ return result.map((row) => ({
6198
+ ...row,
6199
+ id: row.id,
6200
+ agentId: row.agentId,
6201
+ names: row.names || [],
6202
+ metadata: row.metadata || {}
6203
+ }));
6204
+ });
6205
+ }
6206
+ async updateEntities(entities) {
6207
+ for (const entity of entities) {
6208
+ await this.updateEntity(entity);
6209
+ }
6210
+ }
6211
+ async deleteEntities(entityIds) {
6212
+ for (const entityId of entityIds) {
6213
+ await this.deleteEntity(entityId);
6214
+ }
6215
+ }
6216
+ async getEntitiesForRooms(roomIds, includeComponents) {
6217
+ const result = [];
6218
+ for (const roomId of roomIds) {
6219
+ const entities = await this.getEntitiesForRoom(roomId, includeComponents);
6220
+ result.push({ roomId, entities });
6221
+ }
6222
+ return result;
6223
+ }
6224
+ async createLogs(params) {
6225
+ for (const param of params) {
6226
+ await this.log(param);
6227
+ }
6228
+ }
6229
+ async getLogsByIds(logIds) {
6230
+ if (logIds.length === 0)
6231
+ return [];
6232
+ return this.withDatabase(async () => {
6233
+ const result = await this.db.select().from(logTable).where(import_drizzle_orm28.inArray(logTable.id, logIds));
6234
+ return result.map((log) => ({
6235
+ ...log,
6236
+ id: log.id,
6237
+ entityId: log.entityId,
6238
+ roomId: log.roomId,
6239
+ type: log.type,
6240
+ body: log.body,
6241
+ createdAt: new Date(log.createdAt)
6242
+ }));
6243
+ });
6244
+ }
6245
+ async updateLogs(logs) {
6246
+ return this.withDatabase(async () => {
6247
+ for (const { id, updates } of logs) {
6248
+ const setValues = {};
6249
+ if (updates.body !== undefined)
6250
+ setValues.body = updates.body;
6251
+ if (updates.type !== undefined)
6252
+ setValues.type = updates.type;
6253
+ if (Object.keys(setValues).length > 0) {
6254
+ await this.db.update(logTable).set(setValues).where(import_drizzle_orm28.eq(logTable.id, id));
6255
+ }
6256
+ }
6257
+ });
6258
+ }
6259
+ async deleteLogs(logIds) {
6260
+ if (logIds.length === 0)
6261
+ return;
6262
+ return this.withDatabase(async () => {
6263
+ await this.db.delete(logTable).where(import_drizzle_orm28.inArray(logTable.id, logIds));
6264
+ });
6265
+ }
6266
+ async createMemories(memories) {
6267
+ const ids = [];
6268
+ for (const { memory, tableName, unique: unique3 } of memories) {
6269
+ const memoryWithUnique = unique3 !== undefined ? { ...memory, unique: unique3 } : memory;
6270
+ const id = await this.createMemory(memoryWithUnique, tableName);
6271
+ ids.push(id);
6272
+ }
6273
+ return ids;
6274
+ }
6275
+ async updateMemories(memories) {
6276
+ for (const memory of memories) {
6277
+ await this.updateMemory(memory);
6278
+ }
6279
+ }
6280
+ async upsertMemories(memories, _options) {
6281
+ for (const { memory, tableName } of memories) {
6282
+ if (memory.id) {
6283
+ const existing = await this.getMemoryById(memory.id);
6284
+ if (existing) {
6285
+ await this.updateMemory(memory);
6286
+ continue;
6287
+ }
6288
+ }
6289
+ await this.createMemory(memory, tableName);
6290
+ }
6291
+ }
6292
+ async deleteMemories(memoryIds) {
6293
+ await this.deleteManyMemories(memoryIds);
6294
+ }
6295
+ async getWorldsByIds(worldIds) {
6296
+ if (worldIds.length === 0)
6297
+ return [];
6298
+ return this.withDatabase(async () => {
6299
+ const result = await this.db.select().from(worldTable).where(import_drizzle_orm28.inArray(worldTable.id, worldIds));
6300
+ return result.map((world) => this.mapWorldResult(world));
6301
+ });
6302
+ }
6303
+ async createWorlds(worlds) {
6304
+ const ids = [];
6305
+ for (const world of worlds) {
6306
+ const id = await this.createWorld(world);
6307
+ ids.push(id);
6308
+ }
6309
+ return ids;
6310
+ }
6311
+ async deleteWorlds(worldIds) {
6312
+ for (const id of worldIds) {
6313
+ await this.removeWorld(id);
6314
+ }
6315
+ }
6316
+ async updateWorlds(worlds) {
6317
+ for (const world of worlds) {
6318
+ await this.updateWorld(world);
6319
+ }
6320
+ }
6321
+ async upsertWorlds(worlds) {
6322
+ for (const world of worlds) {
6323
+ const existing = await this.getWorld(world.id);
6324
+ if (existing) {
6325
+ await this.updateWorld(world);
6326
+ } else {
6327
+ await this.createWorld(world);
6328
+ }
6329
+ }
6330
+ }
6331
+ async deleteRoomsByWorldIds(worldIds) {
6332
+ for (const worldId of worldIds) {
6333
+ await this.deleteRoomsByWorldId(worldId);
6334
+ }
6335
+ }
6336
+ async getRoomsByWorlds(worldIds, limit, offset) {
6337
+ if (worldIds.length === 0)
6338
+ return [];
6339
+ return this.withDatabase(async () => {
6340
+ const conditions = [
6341
+ import_drizzle_orm28.inArray(roomTable.worldId, worldIds),
6342
+ import_drizzle_orm28.eq(roomTable.agentId, this.agentId)
6343
+ ];
6344
+ let query = this.db.select().from(roomTable).where(import_drizzle_orm28.and(...conditions));
6345
+ if (offset) {
6346
+ query = query.offset(offset);
6347
+ }
6348
+ if (limit) {
6349
+ query = query.limit(limit);
6350
+ }
6351
+ const result = await query;
6352
+ return result.map((room) => ({
6353
+ ...room,
6354
+ id: room.id,
6355
+ name: room.name ?? undefined,
6356
+ agentId: room.agentId,
6357
+ messageServerId: room.messageServerId,
6358
+ serverId: room.messageServerId,
6359
+ worldId: room.worldId,
6360
+ channelId: room.channelId,
6361
+ type: room.type,
6362
+ metadata: room.metadata
6363
+ }));
6364
+ });
6365
+ }
6366
+ async upsertRooms(rooms) {
6367
+ for (const room of rooms) {
6368
+ const existing = await this.getRoomsByIds([room.id]);
6369
+ if (existing && existing.length > 0) {
6370
+ await this.updateRoom(room);
6371
+ } else {
6372
+ await this.createRooms([room]);
6373
+ }
6374
+ }
6375
+ }
6376
+ async createRoomParticipants(entityIds, roomId) {
6377
+ const ids = [];
6378
+ for (const entityId of entityIds) {
6379
+ const success = await this.addParticipant(entityId, roomId);
6380
+ if (success) {
6381
+ ids.push(`${roomId}:${entityId}`);
6382
+ }
6383
+ }
6384
+ return ids;
6385
+ }
6386
+ async deleteParticipants(participants) {
6387
+ for (const { entityId, roomId } of participants) {
6388
+ const success = await this.removeParticipant(entityId, roomId);
6389
+ if (!success)
6390
+ return false;
6391
+ }
6392
+ return true;
6393
+ }
6394
+ async updateParticipants(participants) {
6395
+ for (const { entityId, roomId, updates } of participants) {
6396
+ if (updates.roomState !== undefined) {
6397
+ await this.setParticipantUserState(roomId, entityId, updates.roomState);
6398
+ }
6399
+ }
6400
+ }
6401
+ async updateRooms(rooms) {
6402
+ for (const room of rooms) {
6403
+ await this.updateRoom(room);
6404
+ }
6405
+ }
6406
+ async deleteRooms(roomIds) {
6407
+ for (const roomId of roomIds) {
6408
+ await this.deleteRoom(roomId);
6409
+ }
6410
+ }
6411
+ async getParticipantsForEntities(entityIds) {
6412
+ const result = [];
6413
+ for (const entityId of entityIds) {
6414
+ const participants = await this.getParticipantsForEntity(entityId);
6415
+ result.push(...participants);
6416
+ }
6417
+ return result;
6418
+ }
6419
+ async getParticipantsForRooms(roomIds) {
6420
+ const result = [];
6421
+ for (const roomId of roomIds) {
6422
+ const entityIds = await this.getParticipantsForRoom(roomId);
6423
+ result.push({ roomId, entityIds });
6424
+ }
6425
+ return result;
6426
+ }
6427
+ async areRoomParticipants(pairs) {
6428
+ const results = [];
6429
+ for (const { roomId, entityId } of pairs) {
6430
+ const isParticipant = await this.isRoomParticipant(roomId, entityId);
6431
+ results.push(isParticipant);
6432
+ }
6433
+ return results;
6434
+ }
6435
+ async getParticipantUserStates(pairs) {
6436
+ const results = [];
6437
+ for (const { roomId, entityId } of pairs) {
6438
+ const state = await this.getParticipantUserState(roomId, entityId);
6439
+ results.push(state);
6440
+ }
6441
+ return results;
6442
+ }
6443
+ async updateParticipantUserStates(updates) {
6444
+ for (const { roomId, entityId, state } of updates) {
6445
+ await this.setParticipantUserState(roomId, entityId, state);
6446
+ }
6447
+ }
6448
+ async getRelationshipsByPairs(pairs) {
6449
+ const results = [];
6450
+ for (const pair of pairs) {
6451
+ const rel = await this.getRelationship(pair);
6452
+ results.push(rel);
6453
+ }
6454
+ return results;
6455
+ }
6456
+ async createRelationships(relationships) {
6457
+ const ids = [];
6458
+ for (const rel of relationships) {
6459
+ const id = import_uuid.v4();
6460
+ const success = await this.createRelationship(rel);
6461
+ if (success)
6462
+ ids.push(id);
6463
+ }
6464
+ return ids;
6465
+ }
6466
+ async getRelationshipsByIds(relationshipIds) {
6467
+ if (relationshipIds.length === 0)
6468
+ return [];
6469
+ return this.withDatabase(async () => {
6470
+ const result = await this.db.select().from(relationshipTable).where(import_drizzle_orm28.inArray(relationshipTable.id, relationshipIds));
6471
+ return result.map((relationship) => ({
6472
+ ...relationship,
6473
+ id: relationship.id,
6474
+ sourceEntityId: relationship.sourceEntityId,
6475
+ targetEntityId: relationship.targetEntityId,
6476
+ agentId: relationship.agentId,
6477
+ tags: relationship.tags ?? [],
6478
+ metadata: relationship.metadata ?? {},
6479
+ createdAt: relationship.createdAt.toISOString()
6480
+ }));
6481
+ });
6482
+ }
6483
+ async updateRelationships(relationships) {
6484
+ for (const relationship of relationships) {
6485
+ await this.updateRelationship(relationship);
6486
+ }
6487
+ }
6488
+ async deleteRelationships(relationshipIds) {
6489
+ if (relationshipIds.length === 0)
6490
+ return;
6491
+ return this.withDatabase(async () => {
6492
+ await this.db.delete(relationshipTable).where(import_drizzle_orm28.inArray(relationshipTable.id, relationshipIds));
6493
+ });
6494
+ }
6495
+ async getCaches(keys) {
6496
+ const result = new Map;
6497
+ for (const key of keys) {
6498
+ const value = await this.getCache(key);
6499
+ if (value !== undefined) {
6500
+ result.set(key, value);
6501
+ }
6502
+ }
6503
+ return result;
6504
+ }
6505
+ async setCaches(entries) {
6506
+ for (const { key, value } of entries) {
6507
+ const success = await this.setCache(key, value);
6508
+ if (!success)
6509
+ return false;
6510
+ }
6511
+ return true;
6512
+ }
6513
+ async deleteCaches(keys) {
6514
+ for (const key of keys) {
6515
+ const success = await this.deleteCache(key);
6516
+ if (!success)
6517
+ return false;
6518
+ }
6519
+ return true;
6520
+ }
6521
+ async createTasks(tasks) {
6522
+ const ids = [];
6523
+ for (const task of tasks) {
6524
+ const id = await this.createTask(task);
6525
+ ids.push(id);
6526
+ }
6527
+ return ids;
6528
+ }
6529
+ async getTasksByIds(taskIds) {
6530
+ const tasks = [];
6531
+ for (const taskId of taskIds) {
6532
+ const task = await this.getTask(taskId);
6533
+ if (task)
6534
+ tasks.push(task);
6535
+ }
6536
+ return tasks;
6537
+ }
6538
+ async updateTasks(updates) {
6539
+ for (const { id, task } of updates) {
6540
+ await this.updateTask(id, task);
6541
+ }
6542
+ }
6543
+ async deleteTasks(taskIds) {
6544
+ for (const taskId of taskIds) {
6545
+ await this.deleteTask(taskId);
6546
+ }
6547
+ }
6548
+ async getPairingAllowlists(queries) {
6549
+ const result = [];
6550
+ for (const { channel, agentId } of queries) {
6551
+ const entries = await this.getPairingAllowlist(channel, agentId);
6552
+ result.push({ channel, agentId, entries });
6553
+ }
6554
+ return result;
6555
+ }
6556
+ async createPairingRequests(requests) {
6557
+ const ids = [];
6558
+ for (const request of requests) {
6559
+ const id = await this.createPairingRequest(request);
6560
+ ids.push(id);
6561
+ }
6562
+ return ids;
6563
+ }
6564
+ async updatePairingRequests(requests) {
6565
+ for (const request of requests) {
6566
+ await this.updatePairingRequest(request);
6567
+ }
6568
+ }
6569
+ async deletePairingRequests(ids) {
6570
+ for (const id of ids) {
6571
+ await this.deletePairingRequest(id);
6572
+ }
6573
+ }
6574
+ async createPairingAllowlistEntries(entries) {
6575
+ const ids = [];
6576
+ for (const entry of entries) {
6577
+ const id = await this.createPairingAllowlistEntry(entry);
6578
+ ids.push(id);
6579
+ }
6580
+ return ids;
6581
+ }
6582
+ async updatePairingAllowlistEntries(entries) {
6583
+ return this.withDatabase(async () => {
6584
+ for (const entry of entries) {
6585
+ if (!entry.id)
6586
+ continue;
6587
+ await this.db.update(pairingAllowlistTable).set({
6588
+ metadata: entry.metadata || {}
6589
+ }).where(import_drizzle_orm28.eq(pairingAllowlistTable.id, entry.id));
6590
+ }
6591
+ });
6592
+ }
6593
+ async deletePairingAllowlistEntries(ids) {
6594
+ for (const id of ids) {
6595
+ await this.deletePairingAllowlistEntry(id);
6596
+ }
6597
+ }
6058
6598
  }
6059
6599
 
6060
6600
  // pg/adapter.ts
@@ -6557,5 +7097,5 @@ var plugin = {
6557
7097
  };
6558
7098
  var typescript_default = plugin;
6559
7099
 
6560
- //# debugId=540B34DC4BCF24E964756E2164756E21
7100
+ //# debugId=7B2895617A783CAE64756E2164756E21
6561
7101
  //# sourceMappingURL=index.node.cjs.map