@elizaos/plugin-sql 1.0.10 → 1.0.12

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) {
@@ -1814,11 +1811,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
1814
1811
  * @param {string} [tableName] - The name of the table to count memories in.
1815
1812
  * @returns {Promise<number>} A Promise that resolves to the number of memories.
1816
1813
  */
1817
- async countMemories(roomId, unique6 = true, tableName = "") {
1814
+ async countMemories(roomId, unique4 = true, tableName = "") {
1818
1815
  if (!tableName) throw new Error("tableName is required");
1819
1816
  return this.withDatabase(async () => {
1820
1817
  const conditions = [eq(memoryTable.roomId, roomId), eq(memoryTable.type, tableName)];
1821
- if (unique6) {
1818
+ if (unique4) {
1822
1819
  conditions.push(eq(memoryTable.unique, true));
1823
1820
  }
1824
1821
  const result = await this.db.select({ count: sql16`count(*)` }).from(memoryTable).where(and(...conditions));
@@ -2950,14 +2947,13 @@ var PgliteDatabaseAdapter = class extends BaseDrizzleAdapter {
2950
2947
  // src/pglite/manager.ts
2951
2948
  import { PGlite } from "@electric-sql/pglite";
2952
2949
  import { fuzzystrmatch } from "@electric-sql/pglite/contrib/fuzzystrmatch";
2953
- import { vector as vector3 } from "@electric-sql/pglite/vector";
2950
+ import { vector as vector2 } from "@electric-sql/pglite/vector";
2954
2951
  var PGliteClientManager = class {
2955
2952
  static {
2956
2953
  __name(this, "PGliteClientManager");
2957
2954
  }
2958
2955
  client;
2959
2956
  shuttingDown = false;
2960
- shutdownTimeout = 500;
2961
2957
  /**
2962
2958
  * Constructor for creating a new instance of PGlite with the provided options.
2963
2959
  * Initializes the PGlite client with additional extensions.
@@ -2967,7 +2963,7 @@ var PGliteClientManager = class {
2967
2963
  this.client = new PGlite({
2968
2964
  ...options,
2969
2965
  extensions: {
2970
- vector: vector3,
2966
+ vector: vector2,
2971
2967
  fuzzystrmatch
2972
2968
  }
2973
2969
  });
@@ -2997,7 +2993,7 @@ var PgDatabaseAdapter = class extends BaseDrizzleAdapter {
2997
2993
  }
2998
2994
  embeddingDimension = DIMENSION_MAP[384];
2999
2995
  manager;
3000
- constructor(agentId, manager, schema) {
2996
+ constructor(agentId, manager, _schema) {
3001
2997
  super(agentId);
3002
2998
  this.manager = manager;
3003
2999
  this.db = manager.getDatabase();
@@ -3209,6 +3205,31 @@ import { logger as logger6 } from "@elizaos/core";
3209
3205
  // src/custom-migrator.ts
3210
3206
  import { sql as sql17 } from "drizzle-orm";
3211
3207
  import { logger as logger5 } from "@elizaos/core";
3208
+ function extractErrorMessage(error) {
3209
+ if (error instanceof Error && "cause" in error && error.cause) {
3210
+ return error.cause.message;
3211
+ } else if (error instanceof Error) {
3212
+ return error.message;
3213
+ }
3214
+ return "Unknown error";
3215
+ }
3216
+ __name(extractErrorMessage, "extractErrorMessage");
3217
+ function extractErrorDetails(error) {
3218
+ if (error instanceof Error && "cause" in error && error.cause) {
3219
+ const cause = error.cause;
3220
+ return {
3221
+ message: cause.message,
3222
+ stack: cause.stack || error.stack
3223
+ };
3224
+ } else if (error instanceof Error) {
3225
+ return {
3226
+ message: error.message,
3227
+ stack: error.stack
3228
+ };
3229
+ }
3230
+ return { message: "Unknown error" };
3231
+ }
3232
+ __name(extractErrorDetails, "extractErrorDetails");
3212
3233
  var KNOWN_COMPOSITE_PRIMARY_KEYS = {
3213
3234
  cache: { columns: ["key", "agent_id"] }
3214
3235
  // Add other tables with composite primary keys here if needed
@@ -3650,7 +3671,7 @@ var DrizzleSchemaIntrospector = class {
3650
3671
  }
3651
3672
  }
3652
3673
  } else if (extraConfig && typeof extraConfig === "object") {
3653
- for (const [key, value] of Object.entries(extraConfig)) {
3674
+ for (const [_key, value] of Object.entries(extraConfig)) {
3654
3675
  if (value && typeof value === "object" && value._) {
3655
3676
  const config = value._;
3656
3677
  if (config.name && config.columns) {
@@ -3945,11 +3966,12 @@ var PluginNamespaceManager = class {
3945
3966
  await this.db.execute(sql17.raw(constraintSQL));
3946
3967
  logger5.debug(`[CUSTOM MIGRATOR] Successfully added foreign key constraint: ${fk.name}`);
3947
3968
  } catch (error) {
3948
- if (error.message?.includes("already exists")) {
3969
+ const errorMessage = extractErrorMessage(error);
3970
+ if (errorMessage.includes("already exists")) {
3949
3971
  logger5.debug(`[CUSTOM MIGRATOR] Foreign key constraint already exists: ${fk.name}`);
3950
3972
  } else {
3951
3973
  logger5.warn(
3952
- `[CUSTOM MIGRATOR] Could not add foreign key constraint (may already exist): ${error.message}`
3974
+ `[CUSTOM MIGRATOR] Could not add foreign key constraint (may already exist): ${errorMessage}`
3953
3975
  );
3954
3976
  }
3955
3977
  }
@@ -3975,13 +3997,14 @@ var PluginNamespaceManager = class {
3975
3997
  `[CUSTOM MIGRATOR] Successfully added check constraint: ${checkConstraint.name}`
3976
3998
  );
3977
3999
  } catch (error) {
3978
- if (error.message?.includes("already exists")) {
4000
+ const errorMessage = extractErrorMessage(error);
4001
+ if (errorMessage.includes("already exists")) {
3979
4002
  logger5.debug(
3980
4003
  `[CUSTOM MIGRATOR] Check constraint already exists: ${checkConstraint.name}`
3981
4004
  );
3982
4005
  } else {
3983
4006
  logger5.warn(
3984
- `[CUSTOM MIGRATOR] Could not add check constraint ${checkConstraint.name} (may already exist): ${error.message}`
4007
+ `[CUSTOM MIGRATOR] Could not add check constraint ${checkConstraint.name} (may already exist): ${errorMessage}`
3985
4008
  );
3986
4009
  }
3987
4010
  }
@@ -4001,10 +4024,13 @@ var ExtensionManager = class {
4001
4024
  try {
4002
4025
  await this.db.execute(sql17.raw(`CREATE EXTENSION IF NOT EXISTS "${extension}"`));
4003
4026
  } catch (error) {
4004
- logger5.warn(`Could not install extension ${extension}:`, {
4005
- message: error.message,
4006
- stack: error.stack
4007
- });
4027
+ const errorDetails = extractErrorDetails(error);
4028
+ logger5.warn(`Could not install extension ${extension}: ${errorDetails.message}`);
4029
+ if (errorDetails.stack) {
4030
+ logger5.debug(
4031
+ `[CUSTOM MIGRATOR] Extension installation stack trace: ${errorDetails.stack}`
4032
+ );
4033
+ }
4008
4034
  }
4009
4035
  }
4010
4036
  }
@@ -4043,6 +4069,17 @@ function topologicalSort(tables) {
4043
4069
  __name(topologicalSort, "topologicalSort");
4044
4070
  async function runPluginMigrations(db, pluginName, schema) {
4045
4071
  logger5.debug(`[CUSTOM MIGRATOR] Starting migration for plugin: ${pluginName}`);
4072
+ try {
4073
+ await db.execute(sql17.raw("SELECT 1"));
4074
+ logger5.debug("[CUSTOM MIGRATOR] Database connection verified");
4075
+ } catch (error) {
4076
+ const errorDetails = extractErrorDetails(error);
4077
+ logger5.error(`[CUSTOM MIGRATOR] Database connection failed: ${errorDetails.message}`);
4078
+ if (errorDetails.stack) {
4079
+ logger5.error(`[CUSTOM MIGRATOR] Stack trace: ${errorDetails.stack}`);
4080
+ }
4081
+ throw new Error(`Database connection failed: ${errorDetails.message}`);
4082
+ }
4046
4083
  const namespaceManager = new PluginNamespaceManager(db);
4047
4084
  const introspector = new DrizzleSchemaIntrospector();
4048
4085
  const extensionManager = new ExtensionManager(db);
@@ -4060,32 +4097,54 @@ async function runPluginMigrations(db, pluginName, schema) {
4060
4097
  tableDefinitions.set(tableDef.name, tableDef);
4061
4098
  }
4062
4099
  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`);
4100
+ try {
4101
+ logger5.debug(`[CUSTOM MIGRATOR] Phase 1: Creating tables...`);
4102
+ for (const tableName of sortedTableNames) {
4103
+ const tableDef = tableDefinitions.get(tableName);
4104
+ if (!tableDef) continue;
4105
+ const tableExists = existingTables.includes(tableDef.name);
4106
+ logger5.debug(`[CUSTOM MIGRATOR] Table ${tableDef.name} exists: ${tableExists}`);
4107
+ if (!tableExists) {
4108
+ logger5.debug(`[CUSTOM MIGRATOR] Creating table: ${tableDef.name}`);
4109
+ try {
4110
+ await namespaceManager.createTable(tableDef, schemaName);
4111
+ } catch (error) {
4112
+ const errorDetails = extractErrorDetails(error);
4113
+ logger5.error(
4114
+ `[CUSTOM MIGRATOR] Failed to create table ${tableDef.name}: ${errorDetails.message}`
4115
+ );
4116
+ if (errorDetails.stack) {
4117
+ logger5.error(`[CUSTOM MIGRATOR] Table creation stack trace: ${errorDetails.stack}`);
4118
+ }
4119
+ throw new Error(`Failed to create table ${tableDef.name}: ${errorDetails.message}`);
4120
+ }
4121
+ } else {
4122
+ logger5.debug(`[CUSTOM MIGRATOR] Table ${tableDef.name} already exists, skipping creation`);
4123
+ }
4074
4124
  }
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);
4125
+ logger5.debug(`[CUSTOM MIGRATOR] Phase 2: Adding constraints...`);
4126
+ for (const tableName of sortedTableNames) {
4127
+ const tableDef = tableDefinitions.get(tableName);
4128
+ if (!tableDef) continue;
4129
+ if (tableDef.foreignKeys.length > 0 || tableDef.checkConstraints.length > 0) {
4130
+ logger5.debug(`[CUSTOM MIGRATOR] Adding constraints for table: ${tableDef.name}`, {
4131
+ foreignKeys: tableDef.foreignKeys.length,
4132
+ checkConstraints: tableDef.checkConstraints.length
4133
+ });
4134
+ await namespaceManager.addConstraints(tableDef, schemaName);
4135
+ }
4086
4136
  }
4137
+ logger5.debug(`[CUSTOM MIGRATOR] Completed migration for plugin: ${pluginName}`);
4138
+ } catch (error) {
4139
+ const errorDetails = extractErrorDetails(error);
4140
+ logger5.error(
4141
+ `[CUSTOM MIGRATOR] Migration failed for plugin ${pluginName}: ${errorDetails.message}`
4142
+ );
4143
+ if (errorDetails.stack) {
4144
+ logger5.error(`[CUSTOM MIGRATOR] Migration stack trace: ${errorDetails.stack}`);
4145
+ }
4146
+ throw new Error(`Migration failed for plugin ${pluginName}: ${errorDetails.message}`);
4087
4147
  }
4088
- logger5.debug(`[CUSTOM MIGRATOR] Completed migration for plugin: ${pluginName}`);
4089
4148
  }
4090
4149
  __name(runPluginMigrations, "runPluginMigrations");
4091
4150
 
@@ -4134,7 +4193,6 @@ if (!globalSymbols[GLOBAL_SINGLETONS]) {
4134
4193
  }
4135
4194
  var globalSingletons = globalSymbols[GLOBAL_SINGLETONS];
4136
4195
  function createDatabaseAdapter(config, agentId) {
4137
- const dataDir = resolvePgliteDir(config.dataDir);
4138
4196
  if (config.postgresUrl) {
4139
4197
  if (!globalSingletons.postgresConnectionManager) {
4140
4198
  globalSingletons.postgresConnectionManager = new PostgresConnectionManager(
@@ -4143,6 +4201,7 @@ function createDatabaseAdapter(config, agentId) {
4143
4201
  }
4144
4202
  return new PgDatabaseAdapter(agentId, globalSingletons.postgresConnectionManager);
4145
4203
  }
4204
+ const dataDir = resolvePgliteDir(config.dataDir);
4146
4205
  if (!globalSingletons.pgLiteClientManager) {
4147
4206
  globalSingletons.pgLiteClientManager = new PGliteClientManager({ dataDir });
4148
4207
  }