@elizaos/plugin-sql 1.7.1-alpha.9 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20029,11 +20029,8 @@ class RuntimeMigrator {
20029
20029
  } else {
20030
20030
  logger5.debug({ src: "plugin:sql" }, "Development database detected, skipping advisory locks");
20031
20031
  }
20032
- await this.extensionManager.installRequiredExtensions([
20033
- "vector",
20034
- "fuzzystrmatch",
20035
- "pgcrypto"
20036
- ]);
20032
+ const extensions = isRealPostgres ? ["vector", "fuzzystrmatch", "pgcrypto"] : ["vector", "fuzzystrmatch"];
20033
+ await this.extensionManager.installRequiredExtensions(extensions);
20037
20034
  const currentSnapshot = await generateSnapshot(schema);
20038
20035
  await this.ensureSchemasExist(currentSnapshot);
20039
20036
  this.validateSchemaUsage(pluginName, currentSnapshot);
@@ -20744,14 +20741,16 @@ async function installRLSFunctions(adapter) {
20744
20741
  await db.execute(sql24`
20745
20742
  CREATE OR REPLACE FUNCTION current_server_id() RETURNS UUID AS $$
20746
20743
  DECLARE
20747
- app_name TEXT;
20744
+ server_id_text TEXT;
20748
20745
  BEGIN
20749
- app_name := NULLIF(current_setting('application_name', TRUE), '');
20746
+ server_id_text := NULLIF(current_setting('app.server_id', TRUE), '');
20747
+
20748
+ IF server_id_text IS NULL OR server_id_text = '' THEN
20749
+ RETURN NULL;
20750
+ END IF;
20750
20751
 
20751
- -- Return NULL if application_name is not set or not a valid UUID
20752
- -- This allows admin queries to work without RLS restrictions
20753
20752
  BEGIN
20754
- RETURN app_name::UUID;
20753
+ RETURN server_id_text::UUID;
20755
20754
  EXCEPTION WHEN OTHERS THEN
20756
20755
  RETURN NULL;
20757
20756
  END;
@@ -22028,7 +22027,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22028
22027
  names: this.normalizeEntityNames(entity.names),
22029
22028
  metadata: entity.metadata || {}
22030
22029
  }));
22031
- await tx.insert(entityTable).values(normalizedEntities);
22030
+ await tx.insert(entityTable).values(normalizedEntities).onConflictDoNothing();
22032
22031
  return true;
22033
22032
  });
22034
22033
  } catch (error) {
@@ -22225,7 +22224,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22225
22224
  if (offset !== undefined && offset < 0) {
22226
22225
  throw new Error("offset must be a non-negative number");
22227
22226
  }
22228
- return this.withEntityContext(entityId ?? null, async (tx) => {
22227
+ return this.withIsolationContext(entityId ?? null, async (tx) => {
22229
22228
  const conditions = [eq2(memoryTable.type, tableName)];
22230
22229
  if (start) {
22231
22230
  conditions.push(gte(memoryTable.createdAt, new Date(start)));
@@ -22258,7 +22257,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22258
22257
  metadata: memoryTable.metadata
22259
22258
  },
22260
22259
  embedding: embeddingTable[this.embeddingDimension]
22261
- }).from(memoryTable).leftJoin(embeddingTable, eq2(embeddingTable.memoryId, memoryTable.id)).where(and(...conditions)).orderBy(desc(memoryTable.createdAt));
22260
+ }).from(memoryTable).leftJoin(embeddingTable, eq2(embeddingTable.memoryId, memoryTable.id)).where(and(...conditions)).orderBy(desc(memoryTable.createdAt), desc(memoryTable.id));
22262
22261
  const rows = await (async () => {
22263
22262
  if (params.count && offset !== undefined && offset > 0) {
22264
22263
  return baseQuery.limit(params.count).offset(offset);
@@ -22303,7 +22302,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22303
22302
  roomId: memoryTable.roomId,
22304
22303
  unique: memoryTable.unique,
22305
22304
  metadata: memoryTable.metadata
22306
- }).from(memoryTable).where(and(...conditions)).orderBy(desc(memoryTable.createdAt));
22305
+ }).from(memoryTable).where(and(...conditions)).orderBy(desc(memoryTable.createdAt), desc(memoryTable.id));
22307
22306
  const rows = params.limit ? await query.limit(params.limit) : await query;
22308
22307
  return rows.map((row) => ({
22309
22308
  id: row.id,
@@ -22350,7 +22349,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22350
22349
  const rows = await this.db.select({
22351
22350
  memory: memoryTable,
22352
22351
  embedding: embeddingTable[this.embeddingDimension]
22353
- }).from(memoryTable).leftJoin(embeddingTable, eq2(embeddingTable.memoryId, memoryTable.id)).where(and(...conditions)).orderBy(desc(memoryTable.createdAt));
22352
+ }).from(memoryTable).leftJoin(embeddingTable, eq2(embeddingTable.memoryId, memoryTable.id)).where(and(...conditions)).orderBy(desc(memoryTable.createdAt), desc(memoryTable.id));
22354
22353
  return rows.map((row) => ({
22355
22354
  id: row.memory.id,
22356
22355
  createdAt: row.memory.createdAt.getTime(),
@@ -22425,7 +22424,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22425
22424
  try {
22426
22425
  const sanitizedBody = this.sanitizeJsonObject(params.body);
22427
22426
  const jsonString = JSON.stringify(sanitizedBody);
22428
- await this.withEntityContext(params.entityId, async (tx) => {
22427
+ await this.withIsolationContext(params.entityId, async (tx) => {
22429
22428
  await tx.insert(logTable).values({
22430
22429
  body: sql25`${jsonString}::jsonb`,
22431
22430
  entityId: params.entityId,
@@ -22473,7 +22472,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22473
22472
  }
22474
22473
  async getLogs(params) {
22475
22474
  const { entityId, roomId, type, count: count2, offset } = params;
22476
- return this.withEntityContext(entityId ?? null, async (tx) => {
22475
+ return this.withIsolationContext(entityId ?? null, async (tx) => {
22477
22476
  const result = await tx.select().from(logTable).where(and(roomId ? eq2(logTable.roomId, roomId) : undefined, type ? eq2(logTable.type, type) : undefined)).orderBy(desc(logTable.createdAt)).limit(count2 ?? 10).offset(offset ?? 0);
22478
22477
  const logs = result.map((log2) => ({
22479
22478
  ...log2,
@@ -22493,7 +22492,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22493
22492
  const limit = Math.min(Math.max(params.limit ?? 20, 1), 100);
22494
22493
  const fromDate = typeof params.from === "number" ? new Date(params.from) : undefined;
22495
22494
  const toDate = typeof params.to === "number" ? new Date(params.to) : undefined;
22496
- return this.withEntityContext(params.entityId ?? null, async (tx) => {
22495
+ return this.withIsolationContext(params.entityId ?? null, async (tx) => {
22497
22496
  const runMap = new Map;
22498
22497
  const conditions = [
22499
22498
  eq2(logTable.type, "run_event"),
@@ -22719,10 +22718,6 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22719
22718
  }
22720
22719
  async createMemory(memory, tableName) {
22721
22720
  const memoryId = memory.id ?? v4_default();
22722
- const existing = await this.getMemoryById(memoryId);
22723
- if (existing) {
22724
- return memoryId;
22725
- }
22726
22721
  if (memory.unique === undefined) {
22727
22722
  memory.unique = true;
22728
22723
  if (memory.embedding && Array.isArray(memory.embedding)) {
@@ -22739,8 +22734,8 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22739
22734
  }
22740
22735
  const contentToInsert = typeof memory.content === "string" ? memory.content : JSON.stringify(memory.content ?? {});
22741
22736
  const metadataToInsert = typeof memory.metadata === "string" ? memory.metadata : JSON.stringify(memory.metadata ?? {});
22742
- await this.withEntityContext(memory.entityId, async (tx) => {
22743
- await tx.insert(memoryTable).values([
22737
+ await this.withIsolationContext(memory.entityId, async (tx) => {
22738
+ const inserted = await tx.insert(memoryTable).values([
22744
22739
  {
22745
22740
  id: memoryId,
22746
22741
  type: tableName,
@@ -22753,20 +22748,29 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22753
22748
  unique: memory.unique,
22754
22749
  createdAt: memory.createdAt ? new Date(memory.createdAt) : new Date
22755
22750
  }
22756
- ]);
22757
- if (memory.embedding && Array.isArray(memory.embedding)) {
22758
- const embeddingValues = {
22759
- id: v4_default(),
22760
- memoryId,
22761
- createdAt: memory.createdAt ? new Date(memory.createdAt) : new Date
22762
- };
22763
- const cleanVector = memory.embedding.map((n) => Number.isFinite(n) ? Number(n.toFixed(6)) : 0);
22764
- embeddingValues[this.embeddingDimension] = cleanVector;
22765
- await tx.insert(embeddingTable).values([embeddingValues]);
22751
+ ]).onConflictDoNothing().returning();
22752
+ if (inserted.length > 0 && memory.embedding && Array.isArray(memory.embedding)) {
22753
+ await this.upsertEmbedding(tx, memoryId, memory.embedding);
22766
22754
  }
22767
22755
  });
22768
22756
  return memoryId;
22769
22757
  }
22758
+ async upsertEmbedding(tx, memoryId, embedding) {
22759
+ const cleanVector = embedding.map((n) => Number.isFinite(n) ? Number(n.toFixed(6)) : 0);
22760
+ const existingEmbedding = await tx.select({ id: embeddingTable.id }).from(embeddingTable).where(eq2(embeddingTable.memoryId, memoryId)).limit(1);
22761
+ if (existingEmbedding.length > 0) {
22762
+ const updateValues = {};
22763
+ updateValues[this.embeddingDimension] = cleanVector;
22764
+ await tx.update(embeddingTable).set(updateValues).where(eq2(embeddingTable.memoryId, memoryId));
22765
+ } else {
22766
+ const embeddingValues = {
22767
+ id: v4_default(),
22768
+ memoryId
22769
+ };
22770
+ embeddingValues[this.embeddingDimension] = cleanVector;
22771
+ await tx.insert(embeddingTable).values([embeddingValues]);
22772
+ }
22773
+ }
22770
22774
  async updateMemory(memory) {
22771
22775
  return this.withDatabase(async () => {
22772
22776
  try {
@@ -22785,20 +22789,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22785
22789
  }).where(eq2(memoryTable.id, memory.id));
22786
22790
  }
22787
22791
  if (memory.embedding && Array.isArray(memory.embedding)) {
22788
- const cleanVector = memory.embedding.map((n) => Number.isFinite(n) ? Number(n.toFixed(6)) : 0);
22789
- const existingEmbedding = await tx.select({ id: embeddingTable.id }).from(embeddingTable).where(eq2(embeddingTable.memoryId, memory.id)).limit(1);
22790
- if (existingEmbedding.length > 0) {
22791
- const updateValues = {};
22792
- updateValues[this.embeddingDimension] = cleanVector;
22793
- await tx.update(embeddingTable).set(updateValues).where(eq2(embeddingTable.memoryId, memory.id));
22794
- } else {
22795
- const embeddingValues = {
22796
- id: v4_default(),
22797
- memoryId: memory.id
22798
- };
22799
- embeddingValues[this.embeddingDimension] = cleanVector;
22800
- await tx.insert(embeddingTable).values([embeddingValues]);
22801
- }
22792
+ await this.upsertEmbedding(tx, memory.id, memory.embedding);
22802
22793
  }
22803
22794
  });
22804
22795
  return true;
@@ -22938,9 +22929,8 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22938
22929
  agentId: this.agentId,
22939
22930
  id: room.id || v4_default()
22940
22931
  }));
22941
- const insertedRooms = await this.db.insert(roomTable).values(roomsWithIds).onConflictDoNothing().returning();
22942
- const insertedIds = insertedRooms.map((r) => r.id);
22943
- return insertedIds;
22932
+ await this.db.insert(roomTable).values(roomsWithIds).onConflictDoNothing();
22933
+ return roomsWithIds.map((r) => r.id);
22944
22934
  });
22945
22935
  }
22946
22936
  async deleteRoom(roomId) {
@@ -23238,7 +23228,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
23238
23228
  ...world,
23239
23229
  id: newWorldId,
23240
23230
  name: world.name || ""
23241
- });
23231
+ }).onConflictDoNothing();
23242
23232
  return newWorldId;
23243
23233
  });
23244
23234
  }
@@ -23510,16 +23500,14 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
23510
23500
  createdAt: now,
23511
23501
  updatedAt: now
23512
23502
  };
23513
- await this.db.transaction(async (tx) => {
23514
- await tx.insert(channelTable).values(channelToInsert);
23515
- if (participantIds && participantIds.length > 0) {
23516
- const participantValues = participantIds.map((entityId) => ({
23517
- channelId: newId,
23518
- entityId
23519
- }));
23520
- await tx.insert(channelParticipantsTable).values(participantValues).onConflictDoNothing();
23521
- }
23522
- });
23503
+ await this.db.insert(channelTable).values(channelToInsert).onConflictDoNothing();
23504
+ if (participantIds && participantIds.length > 0) {
23505
+ const participantValues = participantIds.map((entityId) => ({
23506
+ channelId: newId,
23507
+ entityId
23508
+ }));
23509
+ await this.db.insert(channelParticipantsTable).values(participantValues).onConflictDoNothing();
23510
+ }
23523
23511
  return channelToInsert;
23524
23512
  });
23525
23513
  }
@@ -23751,8 +23739,8 @@ class PgliteDatabaseAdapter extends BaseDrizzleAdapter {
23751
23739
  this.manager = manager;
23752
23740
  this.db = drizzle(this.manager.getConnection());
23753
23741
  }
23754
- async withEntityContext(_entityId, callback) {
23755
- return this.db.transaction(callback);
23742
+ async withIsolationContext(_entityId, callback) {
23743
+ return callback(this.db);
23756
23744
  }
23757
23745
  async getEntityByIds(entityIds) {
23758
23746
  return this.getEntitiesByIds(entityIds);
@@ -23880,5 +23868,5 @@ export {
23880
23868
  DatabaseMigrationService
23881
23869
  };
23882
23870
 
23883
- //# debugId=03BBEE81FC5927D264756E2164756E21
23871
+ //# debugId=B552FB2AF0F169C564756E2164756E21
23884
23872
  //# sourceMappingURL=index.browser.js.map