@elizaos/plugin-sql 1.0.11 → 1.0.13

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
@@ -35,7 +35,7 @@ import { v4 } from "uuid";
35
35
 
36
36
  // src/schema/embedding.ts
37
37
  import { sql as sql5 } from "drizzle-orm";
38
- import { check as check2, foreignKey as foreignKey2, index as index2, pgTable as pgTable5, timestamp as timestamp5, uuid as uuid5, vector as vector2 } from "drizzle-orm/pg-core";
38
+ import { check as check2, foreignKey as foreignKey2, index as index2, pgTable as pgTable5, timestamp as timestamp5, uuid as uuid5, vector } from "drizzle-orm/pg-core";
39
39
  import { VECTOR_DIMS } from "@elizaos/core";
40
40
 
41
41
  // src/schema/memory.ts
@@ -221,12 +221,12 @@ var embeddingTable = pgTable5(
221
221
  id: uuid5("id").primaryKey().defaultRandom().notNull(),
222
222
  memoryId: uuid5("memory_id").references(() => memoryTable.id, { onDelete: "cascade" }),
223
223
  createdAt: timestamp5("created_at").default(sql5`now()`).notNull(),
224
- dim384: vector2("dim_384", { dimensions: VECTOR_DIMS.SMALL }),
225
- dim512: vector2("dim_512", { dimensions: VECTOR_DIMS.MEDIUM }),
226
- dim768: vector2("dim_768", { dimensions: VECTOR_DIMS.LARGE }),
227
- dim1024: vector2("dim_1024", { dimensions: VECTOR_DIMS.XL }),
228
- dim1536: vector2("dim_1536", { dimensions: VECTOR_DIMS.XXL }),
229
- dim3072: vector2("dim_3072", { dimensions: VECTOR_DIMS.XXXL })
224
+ dim384: vector("dim_384", { dimensions: VECTOR_DIMS.SMALL }),
225
+ dim512: vector("dim_512", { dimensions: VECTOR_DIMS.MEDIUM }),
226
+ dim768: vector("dim_768", { dimensions: VECTOR_DIMS.LARGE }),
227
+ dim1024: vector("dim_1024", { dimensions: VECTOR_DIMS.XL }),
228
+ dim1536: vector("dim_1536", { dimensions: VECTOR_DIMS.XXL }),
229
+ dim3072: vector("dim_3072", { dimensions: VECTOR_DIMS.XXXL })
230
230
  },
231
231
  (table) => [
232
232
  check2("embedding_source_check", sql5`"memory_id" IS NOT NULL`),
@@ -382,7 +382,7 @@ import {
382
382
  pgTable as pgTable11,
383
383
  text as text10,
384
384
  timestamp as timestamp11,
385
- unique as unique5,
385
+ unique as unique3,
386
386
  uuid as uuid11
387
387
  } from "drizzle-orm/pg-core";
388
388
  var relationshipTable = pgTable11(
@@ -398,7 +398,7 @@ var relationshipTable = pgTable11(
398
398
  },
399
399
  (table) => [
400
400
  index4("idx_relationships_users").on(table.sourceEntityId, table.targetEntityId),
401
- unique5("unique_relationship").on(table.sourceEntityId, table.targetEntityId, table.agentId),
401
+ unique3("unique_relationship").on(table.sourceEntityId, table.targetEntityId, table.agentId),
402
402
  foreignKey5({
403
403
  name: "fk_user_a",
404
404
  columns: [table.sourceEntityId],
@@ -583,7 +583,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
583
583
  return this.withDatabase(async () => {
584
584
  const existingMemory = await this.db.select().from(memoryTable).innerJoin(embeddingTable, eq(embeddingTable.memoryId, memoryTable.id)).where(eq(memoryTable.agentId, this.agentId)).limit(1);
585
585
  if (existingMemory.length > 0) {
586
- const usedDimension = Object.entries(DIMENSION_MAP).find(
586
+ Object.entries(DIMENSION_MAP).find(
587
587
  ([_, colName]) => existingMemory[0].embeddings[colName] !== null
588
588
  );
589
589
  }
@@ -744,7 +744,6 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
744
744
  return source;
745
745
  }
746
746
  const output = typeof target === "object" && target !== null && !Array.isArray(target) ? { ...target } : {};
747
- let isEmpty = true;
748
747
  for (const key of Object.keys(source)) {
749
748
  const sourceValue = source[key];
750
749
  if (sourceValue === null) {
@@ -755,11 +754,9 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
755
754
  delete output[key];
756
755
  } else {
757
756
  output[key] = nestedMergeResult;
758
- isEmpty = false;
759
757
  }
760
758
  } else {
761
759
  output[key] = sourceValue;
762
- isEmpty = false;
763
760
  }
764
761
  }
765
762
  if (Object.keys(output).length === 0) {
@@ -1170,7 +1167,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
1170
1167
  * @returns {Promise<Memory[]>} A Promise that resolves to an array of memories.
1171
1168
  */
1172
1169
  async getMemories(params) {
1173
- const { entityId, agentId, roomId, worldId, tableName, count: count2, unique: unique6, start, end } = params;
1170
+ const { entityId, agentId, roomId, worldId, tableName, unique: unique4, start, end } = params;
1174
1171
  if (!tableName) throw new Error("tableName is required");
1175
1172
  return this.withDatabase(async () => {
1176
1173
  const conditions = [eq(memoryTable.type, tableName)];
@@ -1189,7 +1186,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
1189
1186
  if (end) {
1190
1187
  conditions.push(lte(memoryTable.createdAt, new Date(end)));
1191
1188
  }
1192
- if (unique6) {
1189
+ if (unique4) {
1193
1190
  conditions.push(eq(memoryTable.unique, true));
1194
1191
  }
1195
1192
  if (agentId) {
@@ -1681,8 +1678,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
1681
1678
  } else {
1682
1679
  const embeddingValues = {
1683
1680
  id: v4(),
1684
- memoryId: memory.id,
1685
- createdAt: (/* @__PURE__ */ new Date()).getTime()
1681
+ memoryId: memory.id
1686
1682
  };
1687
1683
  embeddingValues[this.embeddingDimension] = cleanVector;
1688
1684
  await tx.insert(embeddingTable).values([embeddingValues]);
@@ -1814,11 +1810,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
1814
1810
  * @param {string} [tableName] - The name of the table to count memories in.
1815
1811
  * @returns {Promise<number>} A Promise that resolves to the number of memories.
1816
1812
  */
1817
- async countMemories(roomId, unique6 = true, tableName = "") {
1813
+ async countMemories(roomId, unique4 = true, tableName = "") {
1818
1814
  if (!tableName) throw new Error("tableName is required");
1819
1815
  return this.withDatabase(async () => {
1820
1816
  const conditions = [eq(memoryTable.roomId, roomId), eq(memoryTable.type, tableName)];
1821
- if (unique6) {
1817
+ if (unique4) {
1822
1818
  conditions.push(eq(memoryTable.unique, true));
1823
1819
  }
1824
1820
  const result = await this.db.select({ count: sql16`count(*)` }).from(memoryTable).where(and(...conditions));
@@ -2950,14 +2946,13 @@ var PgliteDatabaseAdapter = class extends BaseDrizzleAdapter {
2950
2946
  // src/pglite/manager.ts
2951
2947
  import { PGlite } from "@electric-sql/pglite";
2952
2948
  import { fuzzystrmatch } from "@electric-sql/pglite/contrib/fuzzystrmatch";
2953
- import { vector as vector3 } from "@electric-sql/pglite/vector";
2949
+ import { vector as vector2 } from "@electric-sql/pglite/vector";
2954
2950
  var PGliteClientManager = class {
2955
2951
  static {
2956
2952
  __name(this, "PGliteClientManager");
2957
2953
  }
2958
2954
  client;
2959
2955
  shuttingDown = false;
2960
- shutdownTimeout = 500;
2961
2956
  /**
2962
2957
  * Constructor for creating a new instance of PGlite with the provided options.
2963
2958
  * Initializes the PGlite client with additional extensions.
@@ -2967,7 +2962,7 @@ var PGliteClientManager = class {
2967
2962
  this.client = new PGlite({
2968
2963
  ...options,
2969
2964
  extensions: {
2970
- vector: vector3,
2965
+ vector: vector2,
2971
2966
  fuzzystrmatch
2972
2967
  }
2973
2968
  });
@@ -2997,7 +2992,7 @@ var PgDatabaseAdapter = class extends BaseDrizzleAdapter {
2997
2992
  }
2998
2993
  embeddingDimension = DIMENSION_MAP[384];
2999
2994
  manager;
3000
- constructor(agentId, manager, schema) {
2995
+ constructor(agentId, manager, _schema) {
3001
2996
  super(agentId);
3002
2997
  this.manager = manager;
3003
2998
  this.db = manager.getDatabase();
@@ -3209,6 +3204,31 @@ import { logger as logger6 } from "@elizaos/core";
3209
3204
  // src/custom-migrator.ts
3210
3205
  import { sql as sql17 } from "drizzle-orm";
3211
3206
  import { logger as logger5 } from "@elizaos/core";
3207
+ function extractErrorMessage(error) {
3208
+ if (error instanceof Error && "cause" in error && error.cause) {
3209
+ return error.cause.message;
3210
+ } else if (error instanceof Error) {
3211
+ return error.message;
3212
+ }
3213
+ return "Unknown error";
3214
+ }
3215
+ __name(extractErrorMessage, "extractErrorMessage");
3216
+ function extractErrorDetails(error) {
3217
+ if (error instanceof Error && "cause" in error && error.cause) {
3218
+ const cause = error.cause;
3219
+ return {
3220
+ message: cause.message,
3221
+ stack: cause.stack || error.stack
3222
+ };
3223
+ } else if (error instanceof Error) {
3224
+ return {
3225
+ message: error.message,
3226
+ stack: error.stack
3227
+ };
3228
+ }
3229
+ return { message: "Unknown error" };
3230
+ }
3231
+ __name(extractErrorDetails, "extractErrorDetails");
3212
3232
  var KNOWN_COMPOSITE_PRIMARY_KEYS = {
3213
3233
  cache: { columns: ["key", "agent_id"] }
3214
3234
  // Add other tables with composite primary keys here if needed
@@ -3650,7 +3670,7 @@ var DrizzleSchemaIntrospector = class {
3650
3670
  }
3651
3671
  }
3652
3672
  } else if (extraConfig && typeof extraConfig === "object") {
3653
- for (const [key, value] of Object.entries(extraConfig)) {
3673
+ for (const [_key, value] of Object.entries(extraConfig)) {
3654
3674
  if (value && typeof value === "object" && value._) {
3655
3675
  const config = value._;
3656
3676
  if (config.name && config.columns) {
@@ -3945,11 +3965,12 @@ var PluginNamespaceManager = class {
3945
3965
  await this.db.execute(sql17.raw(constraintSQL));
3946
3966
  logger5.debug(`[CUSTOM MIGRATOR] Successfully added foreign key constraint: ${fk.name}`);
3947
3967
  } catch (error) {
3948
- if (error.message?.includes("already exists")) {
3968
+ const errorMessage = extractErrorMessage(error);
3969
+ if (errorMessage.includes("already exists")) {
3949
3970
  logger5.debug(`[CUSTOM MIGRATOR] Foreign key constraint already exists: ${fk.name}`);
3950
3971
  } else {
3951
3972
  logger5.warn(
3952
- `[CUSTOM MIGRATOR] Could not add foreign key constraint (may already exist): ${error.message}`
3973
+ `[CUSTOM MIGRATOR] Could not add foreign key constraint (may already exist): ${errorMessage}`
3953
3974
  );
3954
3975
  }
3955
3976
  }
@@ -3975,13 +3996,14 @@ var PluginNamespaceManager = class {
3975
3996
  `[CUSTOM MIGRATOR] Successfully added check constraint: ${checkConstraint.name}`
3976
3997
  );
3977
3998
  } catch (error) {
3978
- if (error.message?.includes("already exists")) {
3999
+ const errorMessage = extractErrorMessage(error);
4000
+ if (errorMessage.includes("already exists")) {
3979
4001
  logger5.debug(
3980
4002
  `[CUSTOM MIGRATOR] Check constraint already exists: ${checkConstraint.name}`
3981
4003
  );
3982
4004
  } else {
3983
4005
  logger5.warn(
3984
- `[CUSTOM MIGRATOR] Could not add check constraint ${checkConstraint.name} (may already exist): ${error.message}`
4006
+ `[CUSTOM MIGRATOR] Could not add check constraint ${checkConstraint.name} (may already exist): ${errorMessage}`
3985
4007
  );
3986
4008
  }
3987
4009
  }
@@ -4001,10 +4023,13 @@ var ExtensionManager = class {
4001
4023
  try {
4002
4024
  await this.db.execute(sql17.raw(`CREATE EXTENSION IF NOT EXISTS "${extension}"`));
4003
4025
  } catch (error) {
4004
- logger5.warn(`Could not install extension ${extension}:`, {
4005
- message: error.message,
4006
- stack: error.stack
4007
- });
4026
+ const errorDetails = extractErrorDetails(error);
4027
+ logger5.warn(`Could not install extension ${extension}: ${errorDetails.message}`);
4028
+ if (errorDetails.stack) {
4029
+ logger5.debug(
4030
+ `[CUSTOM MIGRATOR] Extension installation stack trace: ${errorDetails.stack}`
4031
+ );
4032
+ }
4008
4033
  }
4009
4034
  }
4010
4035
  }
@@ -4043,6 +4068,17 @@ function topologicalSort(tables) {
4043
4068
  __name(topologicalSort, "topologicalSort");
4044
4069
  async function runPluginMigrations(db, pluginName, schema) {
4045
4070
  logger5.debug(`[CUSTOM MIGRATOR] Starting migration for plugin: ${pluginName}`);
4071
+ try {
4072
+ await db.execute(sql17.raw("SELECT 1"));
4073
+ logger5.debug("[CUSTOM MIGRATOR] Database connection verified");
4074
+ } catch (error) {
4075
+ const errorDetails = extractErrorDetails(error);
4076
+ logger5.error(`[CUSTOM MIGRATOR] Database connection failed: ${errorDetails.message}`);
4077
+ if (errorDetails.stack) {
4078
+ logger5.error(`[CUSTOM MIGRATOR] Stack trace: ${errorDetails.stack}`);
4079
+ }
4080
+ throw new Error(`Database connection failed: ${errorDetails.message}`);
4081
+ }
4046
4082
  const namespaceManager = new PluginNamespaceManager(db);
4047
4083
  const introspector = new DrizzleSchemaIntrospector();
4048
4084
  const extensionManager = new ExtensionManager(db);
@@ -4060,32 +4096,54 @@ async function runPluginMigrations(db, pluginName, schema) {
4060
4096
  tableDefinitions.set(tableDef.name, tableDef);
4061
4097
  }
4062
4098
  const sortedTableNames = topologicalSort(tableDefinitions);
4063
- logger5.debug(`[CUSTOM MIGRATOR] Phase 1: Creating tables...`);
4064
- for (const tableName of sortedTableNames) {
4065
- const tableDef = tableDefinitions.get(tableName);
4066
- if (!tableDef) continue;
4067
- const tableExists = existingTables.includes(tableDef.name);
4068
- logger5.debug(`[CUSTOM MIGRATOR] Table ${tableDef.name} exists: ${tableExists}`);
4069
- if (!tableExists) {
4070
- logger5.debug(`[CUSTOM MIGRATOR] Creating table: ${tableDef.name}`);
4071
- await namespaceManager.createTable(tableDef, schemaName);
4072
- } else {
4073
- logger5.debug(`[CUSTOM MIGRATOR] Table ${tableDef.name} already exists, skipping creation`);
4099
+ try {
4100
+ logger5.debug(`[CUSTOM MIGRATOR] Phase 1: Creating tables...`);
4101
+ for (const tableName of sortedTableNames) {
4102
+ const tableDef = tableDefinitions.get(tableName);
4103
+ if (!tableDef) continue;
4104
+ const tableExists = existingTables.includes(tableDef.name);
4105
+ logger5.debug(`[CUSTOM MIGRATOR] Table ${tableDef.name} exists: ${tableExists}`);
4106
+ if (!tableExists) {
4107
+ logger5.debug(`[CUSTOM MIGRATOR] Creating table: ${tableDef.name}`);
4108
+ try {
4109
+ await namespaceManager.createTable(tableDef, schemaName);
4110
+ } catch (error) {
4111
+ const errorDetails = extractErrorDetails(error);
4112
+ logger5.error(
4113
+ `[CUSTOM MIGRATOR] Failed to create table ${tableDef.name}: ${errorDetails.message}`
4114
+ );
4115
+ if (errorDetails.stack) {
4116
+ logger5.error(`[CUSTOM MIGRATOR] Table creation stack trace: ${errorDetails.stack}`);
4117
+ }
4118
+ throw new Error(`Failed to create table ${tableDef.name}: ${errorDetails.message}`);
4119
+ }
4120
+ } else {
4121
+ logger5.debug(`[CUSTOM MIGRATOR] Table ${tableDef.name} already exists, skipping creation`);
4122
+ }
4074
4123
  }
4075
- }
4076
- logger5.debug(`[CUSTOM MIGRATOR] Phase 2: Adding constraints...`);
4077
- for (const tableName of sortedTableNames) {
4078
- const tableDef = tableDefinitions.get(tableName);
4079
- if (!tableDef) continue;
4080
- if (tableDef.foreignKeys.length > 0 || tableDef.checkConstraints.length > 0) {
4081
- logger5.debug(`[CUSTOM MIGRATOR] Adding constraints for table: ${tableDef.name}`, {
4082
- foreignKeys: tableDef.foreignKeys.length,
4083
- checkConstraints: tableDef.checkConstraints.length
4084
- });
4085
- await namespaceManager.addConstraints(tableDef, schemaName);
4124
+ logger5.debug(`[CUSTOM MIGRATOR] Phase 2: Adding constraints...`);
4125
+ for (const tableName of sortedTableNames) {
4126
+ const tableDef = tableDefinitions.get(tableName);
4127
+ if (!tableDef) continue;
4128
+ if (tableDef.foreignKeys.length > 0 || tableDef.checkConstraints.length > 0) {
4129
+ logger5.debug(`[CUSTOM MIGRATOR] Adding constraints for table: ${tableDef.name}`, {
4130
+ foreignKeys: tableDef.foreignKeys.length,
4131
+ checkConstraints: tableDef.checkConstraints.length
4132
+ });
4133
+ await namespaceManager.addConstraints(tableDef, schemaName);
4134
+ }
4086
4135
  }
4136
+ logger5.debug(`[CUSTOM MIGRATOR] Completed migration for plugin: ${pluginName}`);
4137
+ } catch (error) {
4138
+ const errorDetails = extractErrorDetails(error);
4139
+ logger5.error(
4140
+ `[CUSTOM MIGRATOR] Migration failed for plugin ${pluginName}: ${errorDetails.message}`
4141
+ );
4142
+ if (errorDetails.stack) {
4143
+ logger5.error(`[CUSTOM MIGRATOR] Migration stack trace: ${errorDetails.stack}`);
4144
+ }
4145
+ throw new Error(`Migration failed for plugin ${pluginName}: ${errorDetails.message}`);
4087
4146
  }
4088
- logger5.debug(`[CUSTOM MIGRATOR] Completed migration for plugin: ${pluginName}`);
4089
4147
  }
4090
4148
  __name(runPluginMigrations, "runPluginMigrations");
4091
4149
 
@@ -4134,7 +4192,6 @@ if (!globalSymbols[GLOBAL_SINGLETONS]) {
4134
4192
  }
4135
4193
  var globalSingletons = globalSymbols[GLOBAL_SINGLETONS];
4136
4194
  function createDatabaseAdapter(config, agentId) {
4137
- const dataDir = resolvePgliteDir(config.dataDir);
4138
4195
  if (config.postgresUrl) {
4139
4196
  if (!globalSingletons.postgresConnectionManager) {
4140
4197
  globalSingletons.postgresConnectionManager = new PostgresConnectionManager(
@@ -4143,6 +4200,7 @@ function createDatabaseAdapter(config, agentId) {
4143
4200
  }
4144
4201
  return new PgDatabaseAdapter(agentId, globalSingletons.postgresConnectionManager);
4145
4202
  }
4203
+ const dataDir = resolvePgliteDir(config.dataDir);
4146
4204
  if (!globalSingletons.pgLiteClientManager) {
4147
4205
  globalSingletons.pgLiteClientManager = new PGliteClientManager({ dataDir });
4148
4206
  }