@elizaos/plugin-sql 1.0.0-beta.38 → 1.0.0-beta.40

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/index.js CHANGED
@@ -28,7 +28,8 @@ import {
28
28
  inArray,
29
29
  lte,
30
30
  or,
31
- sql as sql12
31
+ sql as sql12,
32
+ not
32
33
  } from "drizzle-orm";
33
34
  import { v4 } from "uuid";
34
35
 
@@ -133,7 +134,7 @@ var entityTable = pgTable2(
133
134
  import { sql as sql4 } from "drizzle-orm";
134
135
  import { jsonb as jsonb4, pgTable as pgTable4, text as text4, uuid as uuid4 } from "drizzle-orm/pg-core";
135
136
 
136
- // src/schema/worldTable.ts
137
+ // src/schema/world.ts
137
138
  import { sql as sql3 } from "drizzle-orm";
138
139
  import { jsonb as jsonb3, pgTable as pgTable3, text as text3, uuid as uuid3 } from "drizzle-orm/pg-core";
139
140
  var worldTable = pgTable3("worlds", {
@@ -180,11 +181,15 @@ var memoryTable = pgTable5(
180
181
  roomId: uuid5("roomId").references(() => roomTable.id, {
181
182
  onDelete: "cascade"
182
183
  }),
184
+ worldId: uuid5("worldId").references(() => worldTable.id, {
185
+ onDelete: "set null"
186
+ }),
183
187
  unique: boolean2("unique").default(true).notNull(),
184
188
  metadata: jsonb5("metadata").default({}).notNull()
185
189
  },
186
190
  (table) => [
187
191
  index("idx_memories_type_room").on(table.type, table.roomId),
192
+ index("idx_memories_world_id").on(table.worldId),
188
193
  foreignKey({
189
194
  name: "fk_room",
190
195
  columns: [table.roomId],
@@ -200,6 +205,11 @@ var memoryTable = pgTable5(
200
205
  columns: [table.agentId],
201
206
  foreignColumns: [agentTable.id]
202
207
  }).onDelete("cascade"),
208
+ foreignKey({
209
+ name: "fk_world",
210
+ columns: [table.worldId],
211
+ foreignColumns: [worldTable.id]
212
+ }).onDelete("set null"),
203
213
  index("idx_memories_metadata_type").on(sql5`((metadata->>'type'))`),
204
214
  index("idx_memories_document_id").on(sql5`((metadata->>'documentId'))`),
205
215
  index("idx_fragments_order").on(
@@ -752,10 +762,15 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
752
762
  logger.debug(`[DB] Checking for world references`);
753
763
  const worlds = await tx.select({ id: worldTable.id }).from(worldTable).where(eq(worldTable.agentId, agentId));
754
764
  if (worlds.length > 0) {
755
- const worldIds = worlds.map((w) => w.id);
756
- logger.debug(`[DB] Found ${worldIds.length} worlds to delete`);
757
- await tx.delete(worldTable).where(inArray(worldTable.id, worldIds));
758
- logger.debug(`[DB] Worlds deleted successfully`);
765
+ const newAgent = await tx.select({ newAgentId: agentTable.id }).from(agentTable).where(not(eq(agentTable.id, agentId))).limit(1);
766
+ if (newAgent.length > 0) {
767
+ await tx.update(worldTable).set({ agentId: newAgent[0].newAgentId }).where(eq(worldTable.agentId, agentId));
768
+ } else {
769
+ const worldIds = worlds.map((w) => w.id);
770
+ logger.debug(`[DB] Found ${worldIds.length} worlds to delete`);
771
+ await tx.delete(worldTable).where(inArray(worldTable.id, worldIds));
772
+ logger.debug(`[DB] Worlds deleted successfully`);
773
+ }
759
774
  } else {
760
775
  logger.debug(`[DB] No worlds found for this agent`);
761
776
  }
@@ -1028,10 +1043,8 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
1028
1043
  * @returns {Promise<Memory[]>} A Promise that resolves to an array of memories.
1029
1044
  */
1030
1045
  async getMemories(params) {
1031
- const { entityId, agentId, roomId, tableName, count: count2, unique: unique7, start, end } = params;
1046
+ const { entityId, agentId, roomId, worldId, tableName, count: count2, unique: unique7, start, end } = params;
1032
1047
  if (!tableName) throw new Error("tableName is required");
1033
- if (!roomId && !entityId && !agentId)
1034
- throw new Error("roomId, entityId, or agentId is required");
1035
1048
  return this.withDatabase(async () => {
1036
1049
  const conditions = [eq(memoryTable.type, tableName)];
1037
1050
  if (start) {
@@ -1043,6 +1056,9 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
1043
1056
  if (roomId) {
1044
1057
  conditions.push(eq(memoryTable.roomId, roomId));
1045
1058
  }
1059
+ if (worldId) {
1060
+ conditions.push(eq(memoryTable.worldId, worldId));
1061
+ }
1046
1062
  if (end) {
1047
1063
  conditions.push(lte(memoryTable.createdAt, end));
1048
1064
  }
@@ -1311,18 +1327,24 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
1311
1327
  * Asynchronously searches for memories in the database based on the provided parameters.
1312
1328
  * @param {Object} params - The parameters for searching for memories.
1313
1329
  * @param {string} params.tableName - The name of the table to search for memories in.
1314
- * @param {UUID} params.roomId - The ID of the room to search for memories in.
1315
1330
  * @param {number[]} params.embedding - The embedding to search for.
1316
1331
  * @param {number} [params.match_threshold] - The threshold for the cosine distance.
1317
1332
  * @param {number} [params.count] - The maximum number of memories to retrieve.
1318
1333
  * @param {boolean} [params.unique] - Whether to retrieve unique memories only.
1334
+ * @param {string} [params.query] - Optional query string for potential reranking.
1335
+ * @param {UUID} [params.roomId] - Optional room ID to filter by.
1336
+ * @param {UUID} [params.worldId] - Optional world ID to filter by.
1337
+ * @param {UUID} [params.entityId] - Optional entity ID to filter by.
1319
1338
  * @returns {Promise<Memory[]>} A Promise that resolves to an array of memories.
1320
1339
  */
1321
1340
  async searchMemories(params) {
1322
1341
  return await this.searchMemoriesByEmbedding(params.embedding, {
1323
1342
  match_threshold: params.match_threshold,
1324
1343
  count: params.count,
1344
+ // Pass direct scope fields down
1325
1345
  roomId: params.roomId,
1346
+ worldId: params.worldId,
1347
+ entityId: params.entityId,
1326
1348
  unique: params.unique,
1327
1349
  tableName: params.tableName
1328
1350
  });
@@ -1333,7 +1355,9 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
1333
1355
  * @param {Object} params - The parameters for searching for memories.
1334
1356
  * @param {number} [params.match_threshold] - The threshold for the cosine distance.
1335
1357
  * @param {number} [params.count] - The maximum number of memories to retrieve.
1336
- * @param {UUID} [params.roomId] - The ID of the room to search for memories in.
1358
+ * @param {UUID} [params.roomId] - Optional room ID to filter by.
1359
+ * @param {UUID} [params.worldId] - Optional world ID to filter by.
1360
+ * @param {UUID} [params.entityId] - Optional entity ID to filter by.
1337
1361
  * @param {boolean} [params.unique] - Whether to retrieve unique memories only.
1338
1362
  * @param {string} [params.tableName] - The name of the table to search for memories in.
1339
1363
  * @returns {Promise<Memory[]>} A Promise that resolves to an array of memories.
@@ -1353,6 +1377,12 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
1353
1377
  if (params.roomId) {
1354
1378
  conditions.push(eq(memoryTable.roomId, params.roomId));
1355
1379
  }
1380
+ if (params.worldId) {
1381
+ conditions.push(eq(memoryTable.worldId, params.worldId));
1382
+ }
1383
+ if (params.entityId) {
1384
+ conditions.push(eq(memoryTable.entityId, params.entityId));
1385
+ }
1356
1386
  if (params.match_threshold) {
1357
1387
  conditions.push(gte(similarity, params.match_threshold));
1358
1388
  }
@@ -1369,6 +1399,8 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
1369
1399
  entityId: row.memory.entityId,
1370
1400
  agentId: row.memory.agentId,
1371
1401
  roomId: row.memory.roomId,
1402
+ worldId: row.memory.worldId,
1403
+ // Include worldId
1372
1404
  unique: row.memory.unique,
1373
1405
  metadata: row.memory.metadata,
1374
1406
  embedding: row.embedding ?? void 0,
@@ -1400,7 +1432,10 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
1400
1432
  if (memory.embedding && Array.isArray(memory.embedding)) {
1401
1433
  const similarMemories = await this.searchMemoriesByEmbedding(memory.embedding, {
1402
1434
  tableName,
1435
+ // Use the scope fields from the memory object for similarity check
1403
1436
  roomId: memory.roomId,
1437
+ worldId: memory.worldId,
1438
+ entityId: memory.entityId,
1404
1439
  match_threshold: 0.95,
1405
1440
  count: 1
1406
1441
  });
@@ -1416,6 +1451,8 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
1416
1451
  metadata: sql12`${memory.metadata || {}}::jsonb`,
1417
1452
  entityId: memory.entityId,
1418
1453
  roomId: memory.roomId,
1454
+ worldId: memory.worldId,
1455
+ // Include worldId
1419
1456
  agentId: memory.agentId,
1420
1457
  unique: memory.unique ?? isUnique,
1421
1458
  createdAt: memory.createdAt