@elizaos/plugin-sql 1.7.1-alpha.14 → 1.7.1-alpha.16

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.
@@ -22027,7 +22027,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22027
22027
  names: this.normalizeEntityNames(entity.names),
22028
22028
  metadata: entity.metadata || {}
22029
22029
  }));
22030
- await tx.insert(entityTable).values(normalizedEntities);
22030
+ await tx.insert(entityTable).values(normalizedEntities).onConflictDoNothing();
22031
22031
  return true;
22032
22032
  });
22033
22033
  } catch (error) {
@@ -22257,7 +22257,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22257
22257
  metadata: memoryTable.metadata
22258
22258
  },
22259
22259
  embedding: embeddingTable[this.embeddingDimension]
22260
- }).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));
22261
22261
  const rows = await (async () => {
22262
22262
  if (params.count && offset !== undefined && offset > 0) {
22263
22263
  return baseQuery.limit(params.count).offset(offset);
@@ -22302,7 +22302,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22302
22302
  roomId: memoryTable.roomId,
22303
22303
  unique: memoryTable.unique,
22304
22304
  metadata: memoryTable.metadata
22305
- }).from(memoryTable).where(and(...conditions)).orderBy(desc(memoryTable.createdAt));
22305
+ }).from(memoryTable).where(and(...conditions)).orderBy(desc(memoryTable.createdAt), desc(memoryTable.id));
22306
22306
  const rows = params.limit ? await query.limit(params.limit) : await query;
22307
22307
  return rows.map((row) => ({
22308
22308
  id: row.id,
@@ -22349,7 +22349,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22349
22349
  const rows = await this.db.select({
22350
22350
  memory: memoryTable,
22351
22351
  embedding: embeddingTable[this.embeddingDimension]
22352
- }).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));
22353
22353
  return rows.map((row) => ({
22354
22354
  id: row.memory.id,
22355
22355
  createdAt: row.memory.createdAt.getTime(),
@@ -22718,10 +22718,6 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22718
22718
  }
22719
22719
  async createMemory(memory, tableName) {
22720
22720
  const memoryId = memory.id ?? v4_default();
22721
- const existing = await this.getMemoryById(memoryId);
22722
- if (existing) {
22723
- return memoryId;
22724
- }
22725
22721
  if (memory.unique === undefined) {
22726
22722
  memory.unique = true;
22727
22723
  if (memory.embedding && Array.isArray(memory.embedding)) {
@@ -22739,7 +22735,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22739
22735
  const contentToInsert = typeof memory.content === "string" ? memory.content : JSON.stringify(memory.content ?? {});
22740
22736
  const metadataToInsert = typeof memory.metadata === "string" ? memory.metadata : JSON.stringify(memory.metadata ?? {});
22741
22737
  await this.withIsolationContext(memory.entityId, async (tx) => {
22742
- await tx.insert(memoryTable).values([
22738
+ const inserted = await tx.insert(memoryTable).values([
22743
22739
  {
22744
22740
  id: memoryId,
22745
22741
  type: tableName,
@@ -22752,20 +22748,29 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22752
22748
  unique: memory.unique,
22753
22749
  createdAt: memory.createdAt ? new Date(memory.createdAt) : new Date
22754
22750
  }
22755
- ]);
22756
- if (memory.embedding && Array.isArray(memory.embedding)) {
22757
- const embeddingValues = {
22758
- id: v4_default(),
22759
- memoryId,
22760
- createdAt: memory.createdAt ? new Date(memory.createdAt) : new Date
22761
- };
22762
- const cleanVector = memory.embedding.map((n) => Number.isFinite(n) ? Number(n.toFixed(6)) : 0);
22763
- embeddingValues[this.embeddingDimension] = cleanVector;
22764
- 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);
22765
22754
  }
22766
22755
  });
22767
22756
  return memoryId;
22768
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
+ }
22769
22774
  async updateMemory(memory) {
22770
22775
  return this.withDatabase(async () => {
22771
22776
  try {
@@ -22784,20 +22789,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22784
22789
  }).where(eq2(memoryTable.id, memory.id));
22785
22790
  }
22786
22791
  if (memory.embedding && Array.isArray(memory.embedding)) {
22787
- const cleanVector = memory.embedding.map((n) => Number.isFinite(n) ? Number(n.toFixed(6)) : 0);
22788
- const existingEmbedding = await tx.select({ id: embeddingTable.id }).from(embeddingTable).where(eq2(embeddingTable.memoryId, memory.id)).limit(1);
22789
- if (existingEmbedding.length > 0) {
22790
- const updateValues = {};
22791
- updateValues[this.embeddingDimension] = cleanVector;
22792
- await tx.update(embeddingTable).set(updateValues).where(eq2(embeddingTable.memoryId, memory.id));
22793
- } else {
22794
- const embeddingValues = {
22795
- id: v4_default(),
22796
- memoryId: memory.id
22797
- };
22798
- embeddingValues[this.embeddingDimension] = cleanVector;
22799
- await tx.insert(embeddingTable).values([embeddingValues]);
22800
- }
22792
+ await this.upsertEmbedding(tx, memory.id, memory.embedding);
22801
22793
  }
22802
22794
  });
22803
22795
  return true;
@@ -22937,9 +22929,8 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
22937
22929
  agentId: this.agentId,
22938
22930
  id: room.id || v4_default()
22939
22931
  }));
22940
- const insertedRooms = await this.db.insert(roomTable).values(roomsWithIds).onConflictDoNothing().returning();
22941
- const insertedIds = insertedRooms.map((r) => r.id);
22942
- return insertedIds;
22932
+ await this.db.insert(roomTable).values(roomsWithIds).onConflictDoNothing();
22933
+ return roomsWithIds.map((r) => r.id);
22943
22934
  });
22944
22935
  }
22945
22936
  async deleteRoom(roomId) {
@@ -23237,7 +23228,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
23237
23228
  ...world,
23238
23229
  id: newWorldId,
23239
23230
  name: world.name || ""
23240
- });
23231
+ }).onConflictDoNothing();
23241
23232
  return newWorldId;
23242
23233
  });
23243
23234
  }
@@ -23509,16 +23500,14 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
23509
23500
  createdAt: now,
23510
23501
  updatedAt: now
23511
23502
  };
23512
- await this.db.transaction(async (tx) => {
23513
- await tx.insert(channelTable).values(channelToInsert);
23514
- if (participantIds && participantIds.length > 0) {
23515
- const participantValues = participantIds.map((entityId) => ({
23516
- channelId: newId,
23517
- entityId
23518
- }));
23519
- await tx.insert(channelParticipantsTable).values(participantValues).onConflictDoNothing();
23520
- }
23521
- });
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
+ }
23522
23511
  return channelToInsert;
23523
23512
  });
23524
23513
  }
@@ -23751,7 +23740,7 @@ class PgliteDatabaseAdapter extends BaseDrizzleAdapter {
23751
23740
  this.db = drizzle(this.manager.getConnection());
23752
23741
  }
23753
23742
  async withIsolationContext(_entityId, callback) {
23754
- return this.db.transaction(callback);
23743
+ return callback(this.db);
23755
23744
  }
23756
23745
  async getEntityByIds(entityIds) {
23757
23746
  return this.getEntitiesByIds(entityIds);
@@ -23879,5 +23868,5 @@ export {
23879
23868
  DatabaseMigrationService
23880
23869
  };
23881
23870
 
23882
- //# debugId=E88A6E483DDDF13C64756E2164756E21
23871
+ //# debugId=B552FB2AF0F169C564756E2164756E21
23883
23872
  //# sourceMappingURL=index.browser.js.map