@elizaos/plugin-sql 1.6.5-alpha.15 → 1.6.5-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.
- package/dist/browser/index.browser.js +96 -160
- package/dist/browser/index.browser.js.map +12 -12
- package/dist/browser/tsconfig.build.tsbuildinfo +1 -1
- package/dist/node/index.node.js +120 -185
- package/dist/node/index.node.js.map +14 -14
- package/dist/node/tsconfig.build.node.tsbuildinfo +1 -1
- package/package.json +6 -4
|
@@ -251,14 +251,14 @@ class ExtensionManager {
|
|
|
251
251
|
for (const extension of extensions) {
|
|
252
252
|
try {
|
|
253
253
|
if (!/^[a-zA-Z0-9_-]+$/.test(extension)) {
|
|
254
|
-
logger.warn(
|
|
254
|
+
logger.warn({ src: "plugin:sql", extension }, "Invalid extension name - contains invalid characters");
|
|
255
255
|
continue;
|
|
256
256
|
}
|
|
257
257
|
await this.db.execute(sql20`CREATE EXTENSION IF NOT EXISTS ${sql20.identifier(extension)}`);
|
|
258
|
-
logger.debug(
|
|
258
|
+
logger.debug({ src: "plugin:sql", extension }, "Extension installed");
|
|
259
259
|
} catch (error) {
|
|
260
260
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
261
|
-
logger.warn(
|
|
261
|
+
logger.warn({ src: "plugin:sql", extension, error: errorMessage }, "Could not install extension");
|
|
262
262
|
}
|
|
263
263
|
}
|
|
264
264
|
}
|
|
@@ -18378,10 +18378,7 @@ async function generateMigrationSQL(previousSnapshot, currentSnapshot, diff) {
|
|
|
18378
18378
|
}
|
|
18379
18379
|
const dataLossCheck = checkForDataLoss(diff);
|
|
18380
18380
|
if (dataLossCheck.warnings.length > 0) {
|
|
18381
|
-
logger2.warn("
|
|
18382
|
-
for (const warning of dataLossCheck.warnings) {
|
|
18383
|
-
logger2.warn(` - ${warning}`);
|
|
18384
|
-
}
|
|
18381
|
+
logger2.warn({ src: "plugin:sql", warnings: dataLossCheck.warnings }, "Schema changes may cause data loss");
|
|
18385
18382
|
}
|
|
18386
18383
|
const schemasToCreate = new Set;
|
|
18387
18384
|
for (const tableName of diff.tables.created) {
|
|
@@ -18806,7 +18803,7 @@ class DatabaseIntrospector {
|
|
|
18806
18803
|
this.db = db;
|
|
18807
18804
|
}
|
|
18808
18805
|
async introspectSchema(schemaName = "public") {
|
|
18809
|
-
logger4.info(
|
|
18806
|
+
logger4.info({ src: "plugin:sql", schemaName }, "Starting database introspection");
|
|
18810
18807
|
const tables = {};
|
|
18811
18808
|
const schemas = {};
|
|
18812
18809
|
const enums = {};
|
|
@@ -18814,7 +18811,7 @@ class DatabaseIntrospector {
|
|
|
18814
18811
|
for (const tableInfo of allTables) {
|
|
18815
18812
|
const tableName = tableInfo.table_name;
|
|
18816
18813
|
const tableSchema = tableInfo.table_schema || "public";
|
|
18817
|
-
logger4.debug(
|
|
18814
|
+
logger4.debug({ src: "plugin:sql", tableSchema, tableName }, "Introspecting table");
|
|
18818
18815
|
const columns = await this.getColumns(tableSchema, tableName);
|
|
18819
18816
|
const columnsObject = {};
|
|
18820
18817
|
const uniqueConstraintObject = {};
|
|
@@ -18909,7 +18906,7 @@ class DatabaseIntrospector {
|
|
|
18909
18906
|
}
|
|
18910
18907
|
enums[key].values.push(enumInfo.value);
|
|
18911
18908
|
}
|
|
18912
|
-
logger4.info(
|
|
18909
|
+
logger4.info({ src: "plugin:sql", tableCount: Object.keys(tables).length }, "Database introspection complete");
|
|
18913
18910
|
return {
|
|
18914
18911
|
version: "7",
|
|
18915
18912
|
dialect: "postgresql",
|
|
@@ -19169,7 +19166,7 @@ class RuntimeMigrator {
|
|
|
19169
19166
|
}
|
|
19170
19167
|
}
|
|
19171
19168
|
for (const schemaName of schemasToCreate) {
|
|
19172
|
-
logger5.debug(
|
|
19169
|
+
logger5.debug({ src: "plugin:sql", schemaName }, "Ensuring schema exists");
|
|
19173
19170
|
await this.db.execute(sql22.raw(`CREATE SCHEMA IF NOT EXISTS "${schemaName}"`));
|
|
19174
19171
|
}
|
|
19175
19172
|
}
|
|
@@ -19180,10 +19177,10 @@ class RuntimeMigrator {
|
|
|
19180
19177
|
const tableData = table;
|
|
19181
19178
|
const actualSchema = tableData.schema || "public";
|
|
19182
19179
|
if (!isCorePLugin && actualSchema === "public") {
|
|
19183
|
-
logger5.warn(
|
|
19180
|
+
logger5.warn({ src: "plugin:sql", pluginName, tableName: tableData.name, expectedSchema }, "Plugin table is using public schema - consider using pgSchema for better isolation");
|
|
19184
19181
|
}
|
|
19185
19182
|
if (isCorePLugin && actualSchema !== "public") {
|
|
19186
|
-
logger5.warn(
|
|
19183
|
+
logger5.warn({ src: "plugin:sql", pluginName: "@elizaos/plugin-sql", tableName: tableData.name, actualSchema }, "Core plugin table should use public schema");
|
|
19187
19184
|
}
|
|
19188
19185
|
}
|
|
19189
19186
|
}
|
|
@@ -19394,13 +19391,13 @@ class RuntimeMigrator {
|
|
|
19394
19391
|
}
|
|
19395
19392
|
}
|
|
19396
19393
|
}
|
|
19397
|
-
logger5.debug(
|
|
19394
|
+
logger5.debug({ src: "plugin:sql", urlPreview: url.substring(0, 50) }, "Connection string did not match any PostgreSQL patterns");
|
|
19398
19395
|
return false;
|
|
19399
19396
|
}
|
|
19400
19397
|
async initialize() {
|
|
19401
|
-
logger5.info("
|
|
19398
|
+
logger5.info({ src: "plugin:sql" }, "Initializing migration system");
|
|
19402
19399
|
await this.migrationTracker.ensureTables();
|
|
19403
|
-
logger5.info("
|
|
19400
|
+
logger5.info({ src: "plugin:sql" }, "Migration system initialized");
|
|
19404
19401
|
}
|
|
19405
19402
|
async migrate(pluginName, schema, options = {}) {
|
|
19406
19403
|
const lockId = this.getAdvisoryLockId(pluginName);
|
|
@@ -19409,30 +19406,30 @@ class RuntimeMigrator {
|
|
|
19409
19406
|
}
|
|
19410
19407
|
let lockAcquired = false;
|
|
19411
19408
|
try {
|
|
19412
|
-
logger5.info(
|
|
19409
|
+
logger5.info({ src: "plugin:sql", pluginName }, "Starting migration for plugin");
|
|
19413
19410
|
await this.initialize();
|
|
19414
19411
|
const postgresUrl = process.env.POSTGRES_URL || process.env.DATABASE_URL || "";
|
|
19415
19412
|
const isRealPostgres = this.isRealPostgresDatabase(postgresUrl);
|
|
19416
19413
|
if (isRealPostgres) {
|
|
19417
19414
|
try {
|
|
19418
|
-
logger5.debug(
|
|
19415
|
+
logger5.debug({ src: "plugin:sql", pluginName }, "Using PostgreSQL advisory locks");
|
|
19419
19416
|
const lockIdStr = lockId.toString();
|
|
19420
19417
|
const lockResult = await this.db.execute(sql22`SELECT pg_try_advisory_lock(CAST(${lockIdStr} AS bigint)) as acquired`);
|
|
19421
19418
|
lockAcquired = lockResult.rows[0]?.acquired === true;
|
|
19422
19419
|
if (!lockAcquired) {
|
|
19423
|
-
logger5.info(
|
|
19420
|
+
logger5.info({ src: "plugin:sql", pluginName }, "Migration already in progress, waiting for lock");
|
|
19424
19421
|
await this.db.execute(sql22`SELECT pg_advisory_lock(CAST(${lockIdStr} AS bigint))`);
|
|
19425
19422
|
lockAcquired = true;
|
|
19426
|
-
logger5.info(
|
|
19423
|
+
logger5.info({ src: "plugin:sql", pluginName }, "Lock acquired");
|
|
19427
19424
|
} else {
|
|
19428
|
-
logger5.debug(
|
|
19425
|
+
logger5.debug({ src: "plugin:sql", pluginName, lockId: lockIdStr }, "Advisory lock acquired");
|
|
19429
19426
|
}
|
|
19430
19427
|
} catch (lockError) {
|
|
19431
|
-
logger5.warn(
|
|
19428
|
+
logger5.warn({ src: "plugin:sql", pluginName, error: lockError instanceof Error ? lockError.message : String(lockError) }, "Failed to acquire advisory lock, continuing without lock");
|
|
19432
19429
|
lockAcquired = false;
|
|
19433
19430
|
}
|
|
19434
19431
|
} else {
|
|
19435
|
-
logger5.debug(
|
|
19432
|
+
logger5.debug({ src: "plugin:sql" }, "Development database detected, skipping advisory locks");
|
|
19436
19433
|
}
|
|
19437
19434
|
await this.extensionManager.installRequiredExtensions(["vector", "fuzzystrmatch", "pgcrypto"]);
|
|
19438
19435
|
const currentSnapshot = await generateSnapshot(schema);
|
|
@@ -19441,14 +19438,14 @@ class RuntimeMigrator {
|
|
|
19441
19438
|
const currentHash = hashSnapshot(currentSnapshot);
|
|
19442
19439
|
const lastMigration = await this.migrationTracker.getLastMigration(pluginName);
|
|
19443
19440
|
if (lastMigration && lastMigration.hash === currentHash) {
|
|
19444
|
-
logger5.info(
|
|
19441
|
+
logger5.info({ src: "plugin:sql", pluginName, hash: currentHash }, "No changes detected, skipping migration");
|
|
19445
19442
|
return;
|
|
19446
19443
|
}
|
|
19447
19444
|
let previousSnapshot = await this.snapshotStorage.getLatestSnapshot(pluginName);
|
|
19448
19445
|
if (!previousSnapshot && Object.keys(currentSnapshot.tables).length > 0) {
|
|
19449
19446
|
const hasExistingTables = await this.introspector.hasExistingTables(pluginName);
|
|
19450
19447
|
if (hasExistingTables) {
|
|
19451
|
-
logger5.info(
|
|
19448
|
+
logger5.info({ src: "plugin:sql", pluginName }, "No snapshot found but tables exist in database, introspecting");
|
|
19452
19449
|
const schemaName = this.getExpectedSchemaName(pluginName);
|
|
19453
19450
|
const introspectedSnapshot = await this.introspector.introspectSchema(schemaName);
|
|
19454
19451
|
if (Object.keys(introspectedSnapshot.tables).length > 0) {
|
|
@@ -19456,15 +19453,15 @@ class RuntimeMigrator {
|
|
|
19456
19453
|
await this.journalStorage.updateJournal(pluginName, 0, `introspected_${Date.now()}`, true);
|
|
19457
19454
|
const introspectedHash = hashSnapshot(introspectedSnapshot);
|
|
19458
19455
|
await this.migrationTracker.recordMigration(pluginName, introspectedHash, Date.now());
|
|
19459
|
-
logger5.info(
|
|
19456
|
+
logger5.info({ src: "plugin:sql", pluginName }, "Created initial snapshot from existing database");
|
|
19460
19457
|
previousSnapshot = introspectedSnapshot;
|
|
19461
19458
|
}
|
|
19462
19459
|
}
|
|
19463
19460
|
}
|
|
19464
19461
|
if (!hasChanges(previousSnapshot, currentSnapshot)) {
|
|
19465
|
-
logger5.info(
|
|
19462
|
+
logger5.info({ src: "plugin:sql", pluginName }, "No schema changes");
|
|
19466
19463
|
if (!previousSnapshot && Object.keys(currentSnapshot.tables).length === 0) {
|
|
19467
|
-
logger5.info(
|
|
19464
|
+
logger5.info({ src: "plugin:sql", pluginName }, "Recording empty schema");
|
|
19468
19465
|
await this.migrationTracker.recordMigration(pluginName, currentHash, Date.now());
|
|
19469
19466
|
const idx = await this.journalStorage.getNextIdx(pluginName);
|
|
19470
19467
|
const tag = this.generateMigrationTag(idx, pluginName);
|
|
@@ -19475,7 +19472,7 @@ class RuntimeMigrator {
|
|
|
19475
19472
|
}
|
|
19476
19473
|
const diff = await calculateDiff(previousSnapshot, currentSnapshot);
|
|
19477
19474
|
if (!hasDiffChanges(diff)) {
|
|
19478
|
-
logger5.info(
|
|
19475
|
+
logger5.info({ src: "plugin:sql", pluginName }, "No actionable changes");
|
|
19479
19476
|
return;
|
|
19480
19477
|
}
|
|
19481
19478
|
const dataLossCheck = checkForDataLoss(diff);
|
|
@@ -19483,55 +19480,34 @@ class RuntimeMigrator {
|
|
|
19483
19480
|
const isProduction = false;
|
|
19484
19481
|
const allowDestructive = options.force || options.allowDataLoss || process.env.ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS === "true";
|
|
19485
19482
|
if (!allowDestructive) {
|
|
19486
|
-
logger5.error("
|
|
19487
|
-
logger5.error(`[RuntimeMigrator] Plugin: ${pluginName}`);
|
|
19488
|
-
logger5.error(`[RuntimeMigrator] Environment: ${isProduction ? "PRODUCTION" : "DEVELOPMENT"}`);
|
|
19489
|
-
logger5.error("[RuntimeMigrator] Destructive operations detected:");
|
|
19490
|
-
for (const warning of dataLossCheck.warnings) {
|
|
19491
|
-
logger5.error(`[RuntimeMigrator] - ${warning}`);
|
|
19492
|
-
}
|
|
19493
|
-
logger5.error("[RuntimeMigrator] To proceed with destructive migrations:");
|
|
19494
|
-
logger5.error("[RuntimeMigrator] 1. Set environment variable: export ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS=true");
|
|
19495
|
-
logger5.error("[RuntimeMigrator] 2. Or use option: migrate(plugin, schema, { force: true })");
|
|
19496
|
-
if (isProduction) {
|
|
19497
|
-
logger5.error("[RuntimeMigrator] 3. For production, consider using drizzle-kit for manual migration");
|
|
19498
|
-
}
|
|
19483
|
+
logger5.error({ src: "plugin:sql", pluginName, environment: isProduction ? "PRODUCTION" : "DEVELOPMENT", warnings: dataLossCheck.warnings }, "Destructive migration blocked - set ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS=true or use force option");
|
|
19499
19484
|
const errorMessage = isProduction ? `Destructive migration blocked in production for ${pluginName}. Set ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS=true or use drizzle-kit.` : `Destructive migration blocked for ${pluginName}. Set ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS=true to proceed.`;
|
|
19500
19485
|
throw new Error(errorMessage);
|
|
19501
19486
|
}
|
|
19502
19487
|
if (dataLossCheck.requiresConfirmation) {
|
|
19503
|
-
logger5.warn("
|
|
19504
|
-
logger5.warn(`[RuntimeMigrator] Plugin: ${pluginName}`);
|
|
19505
|
-
logger5.warn("[RuntimeMigrator] The following operations will be performed:");
|
|
19506
|
-
for (const warning of dataLossCheck.warnings) {
|
|
19507
|
-
logger5.warn(`[RuntimeMigrator] ⚠️ ${warning}`);
|
|
19508
|
-
}
|
|
19488
|
+
logger5.warn({ src: "plugin:sql", pluginName, warnings: dataLossCheck.warnings }, "Proceeding with destructive migration");
|
|
19509
19489
|
}
|
|
19510
19490
|
}
|
|
19511
19491
|
const sqlStatements = await generateMigrationSQL(previousSnapshot, currentSnapshot, diff);
|
|
19512
19492
|
if (sqlStatements.length === 0) {
|
|
19513
|
-
logger5.info(
|
|
19493
|
+
logger5.info({ src: "plugin:sql", pluginName }, "No SQL statements to execute");
|
|
19514
19494
|
return;
|
|
19515
19495
|
}
|
|
19516
|
-
logger5.info(
|
|
19496
|
+
logger5.info({ src: "plugin:sql", pluginName, statementCount: sqlStatements.length }, "Executing SQL statements");
|
|
19517
19497
|
if (options.verbose) {
|
|
19518
19498
|
sqlStatements.forEach((stmt, i2) => {
|
|
19519
|
-
logger5.debug(
|
|
19499
|
+
logger5.debug({ src: "plugin:sql", statementIndex: i2 + 1, statement: stmt }, "SQL statement");
|
|
19520
19500
|
});
|
|
19521
19501
|
}
|
|
19522
19502
|
if (options.dryRun) {
|
|
19523
|
-
logger5.info("
|
|
19524
|
-
logger5.info("[RuntimeMigrator] Would execute:");
|
|
19525
|
-
sqlStatements.forEach((stmt, i2) => {
|
|
19526
|
-
logger5.info(` ${i2 + 1}. ${stmt}`);
|
|
19527
|
-
});
|
|
19503
|
+
logger5.info({ src: "plugin:sql", pluginName, statements: sqlStatements }, "DRY RUN mode - not executing statements");
|
|
19528
19504
|
return;
|
|
19529
19505
|
}
|
|
19530
19506
|
await this.executeMigration(pluginName, currentSnapshot, currentHash, sqlStatements);
|
|
19531
|
-
logger5.info(
|
|
19507
|
+
logger5.info({ src: "plugin:sql", pluginName }, "Migration completed successfully");
|
|
19532
19508
|
return;
|
|
19533
19509
|
} catch (error) {
|
|
19534
|
-
logger5.error(
|
|
19510
|
+
logger5.error({ src: "plugin:sql", pluginName, error: error instanceof Error ? error.message : String(error) }, "Migration failed");
|
|
19535
19511
|
throw error;
|
|
19536
19512
|
} finally {
|
|
19537
19513
|
const postgresUrl = process.env.POSTGRES_URL || process.env.DATABASE_URL || "";
|
|
@@ -19540,9 +19516,9 @@ class RuntimeMigrator {
|
|
|
19540
19516
|
try {
|
|
19541
19517
|
const lockIdStr = lockId.toString();
|
|
19542
19518
|
await this.db.execute(sql22`SELECT pg_advisory_unlock(CAST(${lockIdStr} AS bigint))`);
|
|
19543
|
-
logger5.debug(
|
|
19519
|
+
logger5.debug({ src: "plugin:sql", pluginName }, "Advisory lock released");
|
|
19544
19520
|
} catch (unlockError) {
|
|
19545
|
-
logger5.warn(
|
|
19521
|
+
logger5.warn({ src: "plugin:sql", pluginName, error: unlockError instanceof Error ? unlockError.message : String(unlockError) }, "Failed to release advisory lock");
|
|
19546
19522
|
}
|
|
19547
19523
|
}
|
|
19548
19524
|
}
|
|
@@ -19553,7 +19529,7 @@ class RuntimeMigrator {
|
|
|
19553
19529
|
await this.db.execute(sql22`BEGIN`);
|
|
19554
19530
|
transactionStarted = true;
|
|
19555
19531
|
for (const stmt of sqlStatements) {
|
|
19556
|
-
logger5.debug(
|
|
19532
|
+
logger5.debug({ src: "plugin:sql", statement: stmt }, "Executing SQL statement");
|
|
19557
19533
|
await this.db.execute(sql22.raw(stmt));
|
|
19558
19534
|
}
|
|
19559
19535
|
const idx = await this.journalStorage.getNextIdx(pluginName);
|
|
@@ -19562,14 +19538,14 @@ class RuntimeMigrator {
|
|
|
19562
19538
|
await this.journalStorage.updateJournal(pluginName, idx, tag, true);
|
|
19563
19539
|
await this.snapshotStorage.saveSnapshot(pluginName, idx, snapshot);
|
|
19564
19540
|
await this.db.execute(sql22`COMMIT`);
|
|
19565
|
-
logger5.info(
|
|
19541
|
+
logger5.info({ src: "plugin:sql", pluginName, tag }, "Recorded migration");
|
|
19566
19542
|
} catch (error) {
|
|
19567
19543
|
if (transactionStarted) {
|
|
19568
19544
|
try {
|
|
19569
19545
|
await this.db.execute(sql22`ROLLBACK`);
|
|
19570
|
-
logger5.error("
|
|
19546
|
+
logger5.error({ src: "plugin:sql", error: error instanceof Error ? error.message : String(error) }, "Migration failed, rolled back");
|
|
19571
19547
|
} catch (rollbackError) {
|
|
19572
|
-
logger5.error("
|
|
19548
|
+
logger5.error({ src: "plugin:sql", error: rollbackError instanceof Error ? rollbackError.message : String(rollbackError) }, "Failed to rollback transaction");
|
|
19573
19549
|
}
|
|
19574
19550
|
}
|
|
19575
19551
|
throw error;
|
|
@@ -19592,31 +19568,31 @@ class RuntimeMigrator {
|
|
|
19592
19568
|
};
|
|
19593
19569
|
}
|
|
19594
19570
|
async reset(pluginName) {
|
|
19595
|
-
logger5.warn(
|
|
19571
|
+
logger5.warn({ src: "plugin:sql", pluginName }, "Resetting migrations");
|
|
19596
19572
|
await this.db.execute(sql22`DELETE FROM migrations._migrations WHERE plugin_name = ${pluginName}`);
|
|
19597
19573
|
await this.db.execute(sql22`DELETE FROM migrations._journal WHERE plugin_name = ${pluginName}`);
|
|
19598
19574
|
await this.db.execute(sql22`DELETE FROM migrations._snapshots WHERE plugin_name = ${pluginName}`);
|
|
19599
|
-
logger5.warn(
|
|
19575
|
+
logger5.warn({ src: "plugin:sql", pluginName }, "Reset complete");
|
|
19600
19576
|
}
|
|
19601
19577
|
async checkMigration(pluginName, schema) {
|
|
19602
19578
|
try {
|
|
19603
|
-
logger5.info(
|
|
19579
|
+
logger5.info({ src: "plugin:sql", pluginName }, "Checking migration");
|
|
19604
19580
|
const currentSnapshot = await generateSnapshot(schema);
|
|
19605
19581
|
const previousSnapshot = await this.snapshotStorage.getLatestSnapshot(pluginName);
|
|
19606
19582
|
if (!hasChanges(previousSnapshot, currentSnapshot)) {
|
|
19607
|
-
logger5.info(
|
|
19583
|
+
logger5.info({ src: "plugin:sql", pluginName }, "No changes detected");
|
|
19608
19584
|
return null;
|
|
19609
19585
|
}
|
|
19610
19586
|
const diff = await calculateDiff(previousSnapshot, currentSnapshot);
|
|
19611
19587
|
const dataLossCheck = checkForDataLoss(diff);
|
|
19612
19588
|
if (dataLossCheck.hasDataLoss) {
|
|
19613
|
-
logger5.warn(
|
|
19589
|
+
logger5.warn({ src: "plugin:sql", pluginName }, "Migration would cause data loss");
|
|
19614
19590
|
} else {
|
|
19615
|
-
logger5.info(
|
|
19591
|
+
logger5.info({ src: "plugin:sql", pluginName }, "Migration is safe (no data loss)");
|
|
19616
19592
|
}
|
|
19617
19593
|
return dataLossCheck;
|
|
19618
19594
|
} catch (error) {
|
|
19619
|
-
logger5.error(
|
|
19595
|
+
logger5.error({ src: "plugin:sql", pluginName, error: error instanceof Error ? error.message : String(error) }, "Failed to check migration");
|
|
19620
19596
|
throw error;
|
|
19621
19597
|
}
|
|
19622
19598
|
}
|
|
@@ -20015,16 +19991,16 @@ async function installRLSFunctions(adapter) {
|
|
|
20015
19991
|
END;
|
|
20016
19992
|
$$ LANGUAGE plpgsql;
|
|
20017
19993
|
`);
|
|
20018
|
-
logger7.info("
|
|
19994
|
+
logger7.info({ src: "plugin:sql" }, "RLS PostgreSQL functions installed");
|
|
20019
19995
|
await installEntityRLS(adapter);
|
|
20020
19996
|
}
|
|
20021
19997
|
async function applyRLSToNewTables(adapter) {
|
|
20022
19998
|
const db = adapter.db;
|
|
20023
19999
|
try {
|
|
20024
20000
|
await db.execute(sql24`SELECT apply_rls_to_all_tables()`);
|
|
20025
|
-
logger7.info("
|
|
20001
|
+
logger7.info({ src: "plugin:sql" }, "RLS applied to all tables");
|
|
20026
20002
|
} catch (error) {
|
|
20027
|
-
logger7.warn("
|
|
20003
|
+
logger7.warn({ src: "plugin:sql", error: String(error) }, "Failed to apply RLS to some tables");
|
|
20028
20004
|
}
|
|
20029
20005
|
}
|
|
20030
20006
|
async function installEntityRLS(adapter) {
|
|
@@ -20303,20 +20279,19 @@ class DatabaseMigrationService {
|
|
|
20303
20279
|
await migrateToEntityRLS({ db });
|
|
20304
20280
|
this.migrator = new RuntimeMigrator(db);
|
|
20305
20281
|
await this.migrator.initialize();
|
|
20306
|
-
logger8.info(
|
|
20282
|
+
logger8.info({ src: "plugin:sql" }, "DatabaseMigrationService initialized");
|
|
20307
20283
|
}
|
|
20308
20284
|
discoverAndRegisterPluginSchemas(plugins) {
|
|
20309
20285
|
for (const plugin of plugins) {
|
|
20310
20286
|
if (plugin.schema) {
|
|
20311
20287
|
this.registeredSchemas.set(plugin.name, plugin.schema);
|
|
20312
|
-
logger8.info(`Registered schema for plugin: ${plugin.name}`);
|
|
20313
20288
|
}
|
|
20314
20289
|
}
|
|
20315
|
-
logger8.info(
|
|
20290
|
+
logger8.info({ src: "plugin:sql", schemasDiscovered: this.registeredSchemas.size, totalPlugins: plugins.length }, "Plugin schemas discovered");
|
|
20316
20291
|
}
|
|
20317
20292
|
registerSchema(pluginName, schema) {
|
|
20318
20293
|
this.registeredSchemas.set(pluginName, schema);
|
|
20319
|
-
logger8.
|
|
20294
|
+
logger8.debug({ src: "plugin:sql", pluginName }, "Schema registered");
|
|
20320
20295
|
}
|
|
20321
20296
|
async runAllPluginMigrations(options) {
|
|
20322
20297
|
if (!this.db || !this.migrator) {
|
|
@@ -20328,12 +20303,7 @@ class DatabaseMigrationService {
|
|
|
20328
20303
|
force: options?.force ?? false,
|
|
20329
20304
|
dryRun: options?.dryRun ?? false
|
|
20330
20305
|
};
|
|
20331
|
-
logger8.info("
|
|
20332
|
-
logger8.info(`[DatabaseMigrationService] Environment: ${isProduction ? "PRODUCTION" : "DEVELOPMENT"}`);
|
|
20333
|
-
logger8.info(`[DatabaseMigrationService] Plugins to migrate: ${this.registeredSchemas.size}`);
|
|
20334
|
-
if (migrationOptions.dryRun) {
|
|
20335
|
-
logger8.info("[DatabaseMigrationService] DRY RUN mode - no changes will be applied");
|
|
20336
|
-
}
|
|
20306
|
+
logger8.info({ src: "plugin:sql", environment: isProduction ? "PRODUCTION" : "DEVELOPMENT", pluginCount: this.registeredSchemas.size, dryRun: migrationOptions.dryRun }, "Starting migrations");
|
|
20337
20307
|
let successCount = 0;
|
|
20338
20308
|
let failureCount = 0;
|
|
20339
20309
|
const errors = [];
|
|
@@ -20341,43 +20311,37 @@ class DatabaseMigrationService {
|
|
|
20341
20311
|
try {
|
|
20342
20312
|
await this.migrator.migrate(pluginName, schema, migrationOptions);
|
|
20343
20313
|
successCount++;
|
|
20344
|
-
logger8.info(
|
|
20314
|
+
logger8.info({ src: "plugin:sql", pluginName }, "Migration completed");
|
|
20345
20315
|
} catch (error) {
|
|
20346
20316
|
failureCount++;
|
|
20347
20317
|
const errorMessage = error.message;
|
|
20348
20318
|
errors.push({ pluginName, error });
|
|
20349
20319
|
if (errorMessage.includes("Destructive migration blocked")) {
|
|
20350
|
-
logger8.error(
|
|
20351
|
-
if (!migrationOptions.force && process.env.ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS !== "true") {
|
|
20352
|
-
logger8.error("[DatabaseMigrationService] To allow destructive migrations:");
|
|
20353
|
-
logger8.error("[DatabaseMigrationService] - Set ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS=true");
|
|
20354
|
-
logger8.error("[DatabaseMigrationService] - Or pass { force: true } to this method");
|
|
20355
|
-
}
|
|
20320
|
+
logger8.error({ src: "plugin:sql", pluginName }, "Migration blocked - destructive changes detected. Set ELIZA_ALLOW_DESTRUCTIVE_MIGRATIONS=true or use force option");
|
|
20356
20321
|
} else {
|
|
20357
|
-
logger8.error(
|
|
20322
|
+
logger8.error({ src: "plugin:sql", pluginName, error: errorMessage }, "Migration failed");
|
|
20358
20323
|
}
|
|
20359
20324
|
}
|
|
20360
20325
|
}
|
|
20361
20326
|
if (failureCount === 0) {
|
|
20362
|
-
logger8.info(
|
|
20327
|
+
logger8.info({ src: "plugin:sql", successCount }, "All migrations completed successfully");
|
|
20363
20328
|
const dataIsolationEnabled = process.env.ENABLE_DATA_ISOLATION === "true";
|
|
20364
20329
|
if (dataIsolationEnabled) {
|
|
20365
20330
|
try {
|
|
20366
|
-
logger8.info("
|
|
20331
|
+
logger8.info({ src: "plugin:sql" }, "Re-applying Row Level Security...");
|
|
20367
20332
|
await installRLSFunctions({ db: this.db });
|
|
20368
20333
|
await applyRLSToNewTables({ db: this.db });
|
|
20369
20334
|
await applyEntityRLSToAllTables({ db: this.db });
|
|
20370
|
-
logger8.info("
|
|
20335
|
+
logger8.info({ src: "plugin:sql" }, "RLS re-applied successfully");
|
|
20371
20336
|
} catch (rlsError) {
|
|
20372
20337
|
const errorMsg = rlsError instanceof Error ? rlsError.message : String(rlsError);
|
|
20373
|
-
logger8.warn("
|
|
20374
|
-
logger8.warn("[DatabaseMigrationService] This is OK if server_id columns are not yet in schemas");
|
|
20338
|
+
logger8.warn({ src: "plugin:sql", error: errorMsg }, "Failed to re-apply RLS (this is OK if server_id columns are not yet in schemas)");
|
|
20375
20339
|
}
|
|
20376
20340
|
} else {
|
|
20377
|
-
logger8.info("
|
|
20341
|
+
logger8.info({ src: "plugin:sql" }, "Skipping RLS re-application (ENABLE_DATA_ISOLATION is not true)");
|
|
20378
20342
|
}
|
|
20379
20343
|
} else {
|
|
20380
|
-
logger8.error(
|
|
20344
|
+
logger8.error({ src: "plugin:sql", failureCount, successCount }, "Some migrations failed");
|
|
20381
20345
|
const errorSummary = errors.map((e) => `${e.pluginName}: ${e.error.message}`).join(`
|
|
20382
20346
|
`);
|
|
20383
20347
|
throw new Error(`${failureCount} migration(s) failed:
|
|
@@ -20918,10 +20882,10 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
20918
20882
|
const backoffDelay = Math.min(this.baseDelay * 2 ** (attempt - 1), this.maxDelay);
|
|
20919
20883
|
const jitter = Math.random() * this.jitterMax;
|
|
20920
20884
|
const delay = backoffDelay + jitter;
|
|
20921
|
-
logger9.warn(
|
|
20885
|
+
logger9.warn({ src: "plugin:sql", attempt, maxRetries: this.maxRetries, error: error instanceof Error ? error.message : String(error) }, "Database operation failed, retrying");
|
|
20922
20886
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
20923
20887
|
} else {
|
|
20924
|
-
logger9.error(
|
|
20888
|
+
logger9.error({ src: "plugin:sql", totalAttempts: attempt, error: error instanceof Error ? error.message : String(error) }, "Max retry attempts reached");
|
|
20925
20889
|
throw error instanceof Error ? error : new Error(String(error));
|
|
20926
20890
|
}
|
|
20927
20891
|
}
|
|
@@ -20975,7 +20939,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
20975
20939
|
if (agent.id) {
|
|
20976
20940
|
const existing = await this.db.select({ id: agentTable.id }).from(agentTable).where(eq2(agentTable.id, agent.id)).limit(1);
|
|
20977
20941
|
if (existing.length > 0) {
|
|
20978
|
-
logger9.warn(
|
|
20942
|
+
logger9.warn({ src: "plugin:sql", agentId: agent.id }, "Attempted to create agent with duplicate ID");
|
|
20979
20943
|
return false;
|
|
20980
20944
|
}
|
|
20981
20945
|
}
|
|
@@ -20986,10 +20950,9 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
20986
20950
|
updatedAt: new Date(agent.updatedAt || Date.now())
|
|
20987
20951
|
});
|
|
20988
20952
|
});
|
|
20989
|
-
logger9.debug(`Agent created successfully: ${agent.id}`);
|
|
20990
20953
|
return true;
|
|
20991
20954
|
} catch (error) {
|
|
20992
|
-
logger9.error(
|
|
20955
|
+
logger9.error({ src: "plugin:sql", agentId: agent.id, error: error instanceof Error ? error.message : String(error) }, "Failed to create agent");
|
|
20993
20956
|
return false;
|
|
20994
20957
|
}
|
|
20995
20958
|
});
|
|
@@ -21023,10 +20986,9 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21023
20986
|
}
|
|
21024
20987
|
await tx.update(agentTable).set(updateData).where(eq2(agentTable.id, agentId));
|
|
21025
20988
|
});
|
|
21026
|
-
logger9.debug(`Agent updated successfully: ${agentId}`);
|
|
21027
20989
|
return true;
|
|
21028
20990
|
} catch (error) {
|
|
21029
|
-
logger9.error(
|
|
20991
|
+
logger9.error({ src: "plugin:sql", agentId, error: error instanceof Error ? error.message : String(error) }, "Failed to update agent");
|
|
21030
20992
|
return false;
|
|
21031
20993
|
}
|
|
21032
20994
|
});
|
|
@@ -21068,22 +21030,16 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21068
21030
|
return finalSettings === undefined ? {} : finalSettings;
|
|
21069
21031
|
}
|
|
21070
21032
|
async deleteAgent(agentId) {
|
|
21071
|
-
logger9.debug(`[DB] Deleting agent with ID: ${agentId}`);
|
|
21072
21033
|
return this.withDatabase(async () => {
|
|
21073
21034
|
try {
|
|
21074
21035
|
const result = await this.db.delete(agentTable).where(eq2(agentTable.id, agentId)).returning();
|
|
21075
21036
|
if (result.length === 0) {
|
|
21076
|
-
logger9.warn(
|
|
21037
|
+
logger9.warn({ src: "plugin:sql", agentId }, "Agent not found for deletion");
|
|
21077
21038
|
return false;
|
|
21078
21039
|
}
|
|
21079
|
-
logger9.success(`[DB] Agent ${agentId} and all related data successfully deleted via cascade`);
|
|
21080
21040
|
return true;
|
|
21081
21041
|
} catch (error) {
|
|
21082
|
-
logger9.error(
|
|
21083
|
-
if (error instanceof Error) {
|
|
21084
|
-
logger9.error(`[DB] Error details: ${error.name} - ${error.message}`);
|
|
21085
|
-
logger9.error(`[DB] Stack trace: ${error.stack}`);
|
|
21086
|
-
}
|
|
21042
|
+
logger9.error({ src: "plugin:sql", agentId, error: error instanceof Error ? error.message : String(error) }, "Failed to delete agent");
|
|
21087
21043
|
throw error;
|
|
21088
21044
|
}
|
|
21089
21045
|
});
|
|
@@ -21094,7 +21050,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21094
21050
|
const result = await this.db.select({ count: count() }).from(agentTable);
|
|
21095
21051
|
return result[0]?.count || 0;
|
|
21096
21052
|
} catch (error) {
|
|
21097
|
-
logger9.error(
|
|
21053
|
+
logger9.error({ src: "plugin:sql", error: error instanceof Error ? error.message : String(error) }, "Failed to count agents");
|
|
21098
21054
|
return 0;
|
|
21099
21055
|
}
|
|
21100
21056
|
});
|
|
@@ -21103,9 +21059,8 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21103
21059
|
return this.withDatabase(async () => {
|
|
21104
21060
|
try {
|
|
21105
21061
|
await this.db.delete(agentTable);
|
|
21106
|
-
logger9.success("Successfully cleaned up agent table");
|
|
21107
21062
|
} catch (error) {
|
|
21108
|
-
logger9.error(
|
|
21063
|
+
logger9.error({ src: "plugin:sql", error: error instanceof Error ? error.message : String(error) }, "Failed to clean up agent table");
|
|
21109
21064
|
throw error;
|
|
21110
21065
|
}
|
|
21111
21066
|
});
|
|
@@ -21184,21 +21139,17 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21184
21139
|
metadata: entity.metadata || {}
|
|
21185
21140
|
}));
|
|
21186
21141
|
await tx.insert(entityTable).values(normalizedEntities);
|
|
21187
|
-
logger9.debug(`${entities.length} Entities created successfully`);
|
|
21188
21142
|
return true;
|
|
21189
21143
|
});
|
|
21190
21144
|
} catch (error) {
|
|
21191
|
-
logger9.error(
|
|
21192
|
-
if (error instanceof Error && error.stack) {
|
|
21193
|
-
logger9.trace("Stack trace:", error.stack);
|
|
21194
|
-
}
|
|
21145
|
+
logger9.error({ src: "plugin:sql", entityId: entities[0]?.id, error: error instanceof Error ? error.message : String(error) }, "Failed to create entities");
|
|
21195
21146
|
return false;
|
|
21196
21147
|
}
|
|
21197
21148
|
});
|
|
21198
21149
|
}
|
|
21199
21150
|
async ensureEntityExists(entity) {
|
|
21200
21151
|
if (!entity.id) {
|
|
21201
|
-
logger9.error("Entity ID is required for ensureEntityExists");
|
|
21152
|
+
logger9.error({ src: "plugin:sql" }, "Entity ID is required for ensureEntityExists");
|
|
21202
21153
|
return false;
|
|
21203
21154
|
}
|
|
21204
21155
|
try {
|
|
@@ -21208,7 +21159,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21208
21159
|
}
|
|
21209
21160
|
return true;
|
|
21210
21161
|
} catch (error) {
|
|
21211
|
-
logger9.error(
|
|
21162
|
+
logger9.error({ src: "plugin:sql", entityId: entity.id, error: error instanceof Error ? error.message : String(error) }, "Failed to ensure entity exists");
|
|
21212
21163
|
return false;
|
|
21213
21164
|
}
|
|
21214
21165
|
}
|
|
@@ -21558,7 +21509,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21558
21509
|
levenshtein_score: Number(row.levenshtein_score)
|
|
21559
21510
|
})).filter((row) => Array.isArray(row.embedding));
|
|
21560
21511
|
} catch (error) {
|
|
21561
|
-
logger9.error(
|
|
21512
|
+
logger9.error({ src: "plugin:sql", tableName: opts.query_table_name, fieldName: opts.query_field_name, error: error instanceof Error ? error.message : String(error) }, "Failed to get cached embeddings");
|
|
21562
21513
|
if (error instanceof Error && error.message === "levenshtein argument exceeds maximum length of 255 characters") {
|
|
21563
21514
|
return [];
|
|
21564
21515
|
}
|
|
@@ -21580,7 +21531,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21580
21531
|
});
|
|
21581
21532
|
});
|
|
21582
21533
|
} catch (error) {
|
|
21583
|
-
logger9.error(
|
|
21534
|
+
logger9.error({ src: "plugin:sql", type: params.type, roomId: params.roomId, entityId: params.entityId, error: error instanceof Error ? error.message : String(error) }, "Failed to create log entry");
|
|
21584
21535
|
throw error;
|
|
21585
21536
|
}
|
|
21586
21537
|
});
|
|
@@ -21857,11 +21808,9 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21857
21808
|
});
|
|
21858
21809
|
}
|
|
21859
21810
|
async createMemory(memory, tableName) {
|
|
21860
|
-
logger9.debug(`DrizzleAdapter createMemory: memoryId: ${memory.id}, embeddingLength: ${memory.embedding?.length}, contentLength: ${memory.content?.text?.length}`);
|
|
21861
21811
|
const memoryId = memory.id ?? v4_default();
|
|
21862
21812
|
const existing = await this.getMemoryById(memoryId);
|
|
21863
21813
|
if (existing) {
|
|
21864
|
-
logger9.debug(`Memory already exists, skipping creation: ${memoryId}`);
|
|
21865
21814
|
return memoryId;
|
|
21866
21815
|
}
|
|
21867
21816
|
if (memory.unique === undefined) {
|
|
@@ -21911,7 +21860,6 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21911
21860
|
async updateMemory(memory) {
|
|
21912
21861
|
return this.withDatabase(async () => {
|
|
21913
21862
|
try {
|
|
21914
|
-
logger9.debug(`Updating memory: memoryId: ${memory.id}, hasEmbedding: ${!!memory.embedding}`);
|
|
21915
21863
|
await this.db.transaction(async (tx) => {
|
|
21916
21864
|
if (memory.content) {
|
|
21917
21865
|
const contentToUpdate = typeof memory.content === "string" ? memory.content : JSON.stringify(memory.content ?? {});
|
|
@@ -21943,10 +21891,9 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21943
21891
|
}
|
|
21944
21892
|
}
|
|
21945
21893
|
});
|
|
21946
|
-
logger9.debug(`Memory updated successfully: ${memory.id}`);
|
|
21947
21894
|
return true;
|
|
21948
21895
|
} catch (error) {
|
|
21949
|
-
logger9.error(
|
|
21896
|
+
logger9.error({ src: "plugin:sql", memoryId: memory.id, error: error instanceof Error ? error.message : String(error) }, "Failed to update memory");
|
|
21950
21897
|
return false;
|
|
21951
21898
|
}
|
|
21952
21899
|
});
|
|
@@ -21958,7 +21905,6 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21958
21905
|
await tx.delete(embeddingTable).where(eq2(embeddingTable.memoryId, memoryId));
|
|
21959
21906
|
await tx.delete(memoryTable).where(eq2(memoryTable.id, memoryId));
|
|
21960
21907
|
});
|
|
21961
|
-
logger9.debug(`Memory and related fragments removed successfully: ${memoryId}`);
|
|
21962
21908
|
});
|
|
21963
21909
|
}
|
|
21964
21910
|
async deleteManyMemories(memoryIds) {
|
|
@@ -21977,7 +21923,6 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21977
21923
|
await tx.delete(memoryTable).where(inArray(memoryTable.id, batch));
|
|
21978
21924
|
}
|
|
21979
21925
|
});
|
|
21980
|
-
logger9.debug(`Batch memory deletion completed successfully: ${memoryIds.length}`);
|
|
21981
21926
|
});
|
|
21982
21927
|
}
|
|
21983
21928
|
async deleteMemoryFragments(tx, documentId) {
|
|
@@ -21986,7 +21931,6 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21986
21931
|
const fragmentIds = fragmentsToDelete.map((f) => f.id);
|
|
21987
21932
|
await tx.delete(embeddingTable).where(inArray(embeddingTable.memoryId, fragmentIds));
|
|
21988
21933
|
await tx.delete(memoryTable).where(inArray(memoryTable.id, fragmentIds));
|
|
21989
|
-
logger9.debug(`Deleted related fragments: documentId: ${documentId}, fragmentCount: ${fragmentsToDelete.length}`);
|
|
21990
21934
|
}
|
|
21991
21935
|
}
|
|
21992
21936
|
async getMemoryFragments(tx, documentId) {
|
|
@@ -21998,7 +21942,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
21998
21942
|
await this.db.transaction(async (tx) => {
|
|
21999
21943
|
const rows = await tx.select({ id: memoryTable.id }).from(memoryTable).where(and(eq2(memoryTable.roomId, roomId), eq2(memoryTable.type, tableName)));
|
|
22000
21944
|
const ids = rows.map((r) => r.id);
|
|
22001
|
-
logger9.debug(
|
|
21945
|
+
logger9.debug({ src: "plugin:sql", roomId, tableName, memoryCount: ids.length }, "Deleting all memories");
|
|
22002
21946
|
if (ids.length === 0) {
|
|
22003
21947
|
return;
|
|
22004
21948
|
}
|
|
@@ -22008,7 +21952,6 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
22008
21952
|
}));
|
|
22009
21953
|
await tx.delete(memoryTable).where(and(eq2(memoryTable.roomId, roomId), eq2(memoryTable.type, tableName)));
|
|
22010
21954
|
});
|
|
22011
|
-
logger9.debug(`All memories removed successfully: roomId: ${roomId}, tableName: ${tableName}`);
|
|
22012
21955
|
});
|
|
22013
21956
|
}
|
|
22014
21957
|
async countMemories(roomId, unique3 = true, tableName = "") {
|
|
@@ -22117,7 +22060,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
22117
22060
|
}).onConflictDoNothing();
|
|
22118
22061
|
return true;
|
|
22119
22062
|
} catch (error) {
|
|
22120
|
-
logger9.error(
|
|
22063
|
+
logger9.error({ src: "plugin:sql", entityId, roomId, agentId: this.agentId, error: error instanceof Error ? error.message : String(error) }, "Failed to add participant to room");
|
|
22121
22064
|
return false;
|
|
22122
22065
|
}
|
|
22123
22066
|
});
|
|
@@ -22131,10 +22074,9 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
22131
22074
|
agentId: this.agentId
|
|
22132
22075
|
}));
|
|
22133
22076
|
await this.db.insert(participantTable).values(values).onConflictDoNothing().execute();
|
|
22134
|
-
logger9.debug(`${entityIds.length} Entities linked successfully`);
|
|
22135
22077
|
return true;
|
|
22136
22078
|
} catch (error) {
|
|
22137
|
-
logger9.error(
|
|
22079
|
+
logger9.error({ src: "plugin:sql", roomId, agentId: this.agentId, error: error instanceof Error ? error.message : String(error) }, "Failed to add participants to room");
|
|
22138
22080
|
return false;
|
|
22139
22081
|
}
|
|
22140
22082
|
});
|
|
@@ -22146,10 +22088,9 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
22146
22088
|
return await tx.delete(participantTable).where(and(eq2(participantTable.entityId, entityId), eq2(participantTable.roomId, roomId))).returning();
|
|
22147
22089
|
});
|
|
22148
22090
|
const removed = result.length > 0;
|
|
22149
|
-
logger9.debug(`Participant ${removed ? "removed" : "not found"}: entityId: ${entityId}, roomId: ${roomId}, removed: ${removed}`);
|
|
22150
22091
|
return removed;
|
|
22151
22092
|
} catch (error) {
|
|
22152
|
-
logger9.error(
|
|
22093
|
+
logger9.error({ src: "plugin:sql", entityId, roomId, error: error instanceof Error ? error.message : String(error) }, "Failed to remove participant from room");
|
|
22153
22094
|
return false;
|
|
22154
22095
|
}
|
|
22155
22096
|
});
|
|
@@ -22196,7 +22137,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
22196
22137
|
await tx.update(participantTable).set({ roomState: state }).where(and(eq2(participantTable.roomId, roomId), eq2(participantTable.entityId, entityId), eq2(participantTable.agentId, this.agentId)));
|
|
22197
22138
|
});
|
|
22198
22139
|
} catch (error) {
|
|
22199
|
-
logger9.error(
|
|
22140
|
+
logger9.error({ src: "plugin:sql", roomId, entityId, state, error: error instanceof Error ? error.message : String(error) }, "Failed to set participant follow state");
|
|
22200
22141
|
throw error;
|
|
22201
22142
|
}
|
|
22202
22143
|
});
|
|
@@ -22216,7 +22157,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
22216
22157
|
await this.db.insert(relationshipTable).values(saveParams);
|
|
22217
22158
|
return true;
|
|
22218
22159
|
} catch (error) {
|
|
22219
|
-
logger9.error(
|
|
22160
|
+
logger9.error({ src: "plugin:sql", agentId: this.agentId, error: error instanceof Error ? error.message : String(error), saveParams }, "Error creating relationship");
|
|
22220
22161
|
return false;
|
|
22221
22162
|
}
|
|
22222
22163
|
});
|
|
@@ -22229,7 +22170,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
22229
22170
|
metadata: relationship.metadata || {}
|
|
22230
22171
|
}).where(eq2(relationshipTable.id, relationship.id));
|
|
22231
22172
|
} catch (error) {
|
|
22232
|
-
logger9.error(
|
|
22173
|
+
logger9.error({ src: "plugin:sql", agentId: this.agentId, error: error instanceof Error ? error.message : String(error), relationshipId: relationship.id }, "Error updating relationship");
|
|
22233
22174
|
throw error;
|
|
22234
22175
|
}
|
|
22235
22176
|
});
|
|
@@ -22291,7 +22232,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
22291
22232
|
}
|
|
22292
22233
|
return;
|
|
22293
22234
|
} catch (error) {
|
|
22294
|
-
logger9.error(
|
|
22235
|
+
logger9.error({ src: "plugin:sql", agentId: this.agentId, error: error instanceof Error ? error.message : String(error), key }, "Error fetching cache");
|
|
22295
22236
|
return;
|
|
22296
22237
|
}
|
|
22297
22238
|
});
|
|
@@ -22311,7 +22252,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
22311
22252
|
});
|
|
22312
22253
|
return true;
|
|
22313
22254
|
} catch (error) {
|
|
22314
|
-
logger9.error(
|
|
22255
|
+
logger9.error({ src: "plugin:sql", agentId: this.agentId, error: error instanceof Error ? error.message : String(error), key }, "Error setting cache");
|
|
22315
22256
|
return false;
|
|
22316
22257
|
}
|
|
22317
22258
|
});
|
|
@@ -22324,7 +22265,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
22324
22265
|
});
|
|
22325
22266
|
return true;
|
|
22326
22267
|
} catch (error) {
|
|
22327
|
-
logger9.error(
|
|
22268
|
+
logger9.error({ src: "plugin:sql", agentId: this.agentId, error: error instanceof Error ? error.message : String(error), key }, "Error deleting cache");
|
|
22328
22269
|
return false;
|
|
22329
22270
|
}
|
|
22330
22271
|
});
|
|
@@ -22487,25 +22428,20 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
|
|
|
22487
22428
|
return this.withDatabase(async () => {
|
|
22488
22429
|
const rooms = await this.db.select({ id: roomTable.id }).from(roomTable).where(and(eq2(roomTable.worldId, worldId), eq2(roomTable.agentId, this.agentId)));
|
|
22489
22430
|
if (rooms.length === 0) {
|
|
22490
|
-
logger9.debug(`No rooms found for worldId ${worldId} and agentId ${this.agentId} to delete.`);
|
|
22491
22431
|
return;
|
|
22492
22432
|
}
|
|
22493
22433
|
const roomIds = rooms.map((room) => room.id);
|
|
22494
22434
|
if (roomIds.length > 0) {
|
|
22495
22435
|
await this.db.delete(logTable).where(inArray(logTable.roomId, roomIds));
|
|
22496
|
-
logger9.debug(`Deleted logs for ${roomIds.length} rooms in world ${worldId}.`);
|
|
22497
22436
|
await this.db.delete(participantTable).where(inArray(participantTable.roomId, roomIds));
|
|
22498
|
-
logger9.debug(`Deleted participants for ${roomIds.length} rooms in world ${worldId}.`);
|
|
22499
22437
|
const memoriesInRooms = await this.db.select({ id: memoryTable.id }).from(memoryTable).where(inArray(memoryTable.roomId, roomIds));
|
|
22500
22438
|
const memoryIdsInRooms = memoriesInRooms.map((m) => m.id);
|
|
22501
22439
|
if (memoryIdsInRooms.length > 0) {
|
|
22502
22440
|
await this.db.delete(embeddingTable).where(inArray(embeddingTable.memoryId, memoryIdsInRooms));
|
|
22503
|
-
logger9.debug(`Deleted embeddings for ${memoryIdsInRooms.length} memories in world ${worldId}.`);
|
|
22504
22441
|
await this.db.delete(memoryTable).where(inArray(memoryTable.id, memoryIdsInRooms));
|
|
22505
|
-
logger9.debug(`Deleted ${memoryIdsInRooms.length} memories in world ${worldId}.`);
|
|
22506
22442
|
}
|
|
22507
22443
|
await this.db.delete(roomTable).where(inArray(roomTable.id, roomIds));
|
|
22508
|
-
logger9.debug(
|
|
22444
|
+
logger9.debug({ src: "plugin:sql", worldId, roomsDeleted: roomIds.length, memoriesDeleted: memoryIdsInRooms.length }, "World cleanup completed");
|
|
22509
22445
|
}
|
|
22510
22446
|
});
|
|
22511
22447
|
}
|
|
@@ -22853,7 +22789,7 @@ class PgliteDatabaseAdapter extends BaseDrizzleAdapter {
|
|
|
22853
22789
|
return this.getEntitiesByIds(entityIds);
|
|
22854
22790
|
}
|
|
22855
22791
|
async getMemoriesByServerId(_params) {
|
|
22856
|
-
logger10.warn("getMemoriesByServerId called but not implemented
|
|
22792
|
+
logger10.warn({ src: "plugin:sql" }, "getMemoriesByServerId called but not implemented");
|
|
22857
22793
|
return [];
|
|
22858
22794
|
}
|
|
22859
22795
|
async ensureAgentExists(agent) {
|
|
@@ -22878,13 +22814,13 @@ class PgliteDatabaseAdapter extends BaseDrizzleAdapter {
|
|
|
22878
22814
|
}
|
|
22879
22815
|
async withDatabase(operation) {
|
|
22880
22816
|
if (this.manager.isShuttingDown()) {
|
|
22881
|
-
logger10.warn("Database is shutting down");
|
|
22817
|
+
logger10.warn({ src: "plugin:sql" }, "Database is shutting down");
|
|
22882
22818
|
return null;
|
|
22883
22819
|
}
|
|
22884
22820
|
return operation();
|
|
22885
22821
|
}
|
|
22886
22822
|
async init() {
|
|
22887
|
-
logger10.debug("
|
|
22823
|
+
logger10.debug({ src: "plugin:sql" }, "PGliteDatabaseAdapter initialized");
|
|
22888
22824
|
}
|
|
22889
22825
|
async isReady() {
|
|
22890
22826
|
return !this.manager.isShuttingDown();
|
|
@@ -22948,17 +22884,17 @@ var plugin = {
|
|
|
22948
22884
|
priority: 0,
|
|
22949
22885
|
schema: exports_schema,
|
|
22950
22886
|
init: async (_config, runtime) => {
|
|
22951
|
-
logger11.info("plugin-sql (browser) init starting
|
|
22887
|
+
logger11.info({ src: "plugin:sql" }, "plugin-sql (browser) init starting");
|
|
22952
22888
|
try {
|
|
22953
22889
|
const isReady = await runtime.isReady();
|
|
22954
22890
|
if (isReady) {
|
|
22955
|
-
logger11.info("Database adapter already registered, skipping creation");
|
|
22891
|
+
logger11.info({ src: "plugin:sql" }, "Database adapter already registered, skipping creation");
|
|
22956
22892
|
return;
|
|
22957
22893
|
}
|
|
22958
22894
|
} catch (error) {}
|
|
22959
22895
|
const dbAdapter = createDatabaseAdapter({}, runtime.agentId);
|
|
22960
22896
|
runtime.registerDatabaseAdapter(dbAdapter);
|
|
22961
|
-
logger11.info("Browser database adapter (PGlite) created and registered");
|
|
22897
|
+
logger11.info({ src: "plugin:sql" }, "Browser database adapter (PGlite) created and registered");
|
|
22962
22898
|
}
|
|
22963
22899
|
};
|
|
22964
22900
|
var index_browser_default = plugin;
|
|
@@ -22969,5 +22905,5 @@ export {
|
|
|
22969
22905
|
DatabaseMigrationService
|
|
22970
22906
|
};
|
|
22971
22907
|
|
|
22972
|
-
//# debugId=
|
|
22908
|
+
//# debugId=7F64AB0B1EC2677C64756E2164756E21
|
|
22973
22909
|
//# sourceMappingURL=index.browser.js.map
|