@agentforge/tools 0.16.14 → 0.16.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/index.d.cts CHANGED
@@ -4944,14 +4944,6 @@ type ConnectionConfig = {
4944
4944
  connection: SQLiteConnectionConfig | string;
4945
4945
  };
4946
4946
 
4947
- /**
4948
- * Connection manager for relational databases using Drizzle ORM
4949
- * @module connection/connection-manager
4950
- */
4951
-
4952
- /**
4953
- * Connection state enum
4954
- */
4955
4947
  declare enum ConnectionState {
4956
4948
  DISCONNECTED = "disconnected",
4957
4949
  CONNECTING = "connecting",
@@ -4959,23 +4951,27 @@ declare enum ConnectionState {
4959
4951
  RECONNECTING = "reconnecting",
4960
4952
  ERROR = "error"
4961
4953
  }
4962
- /**
4963
- * Connection lifecycle events
4964
- */
4965
- type ConnectionEvent = 'connected' | 'disconnected' | 'error' | 'reconnecting';
4966
- /**
4967
- * Reconnection configuration
4968
- */
4969
4954
  interface ReconnectionConfig {
4970
- /** Enable automatic reconnection on connection loss */
4955
+ /** Enable automatic reconnection on connection loss. */
4971
4956
  enabled: boolean;
4972
- /** Maximum number of reconnection attempts (0 = infinite) */
4957
+ /** Maximum number of reconnection attempts (0 = infinite). */
4973
4958
  maxAttempts: number;
4974
- /** Base delay in milliseconds for exponential backoff */
4959
+ /** Base delay in milliseconds for exponential backoff. */
4975
4960
  baseDelayMs: number;
4976
- /** Maximum delay in milliseconds between reconnection attempts */
4961
+ /** Maximum delay in milliseconds between reconnection attempts. */
4977
4962
  maxDelayMs: number;
4978
4963
  }
4964
+
4965
+ /**
4966
+ * Connection manager for relational databases using Drizzle ORM
4967
+ * @module connection/connection-manager
4968
+ */
4969
+
4970
+ /**
4971
+ * Connection lifecycle events
4972
+ */
4973
+ type ConnectionEvent = 'connected' | 'disconnected' | 'error' | 'reconnecting';
4974
+
4979
4975
  /**
4980
4976
  * Connection manager that handles database connections for PostgreSQL, MySQL, and SQLite
4981
4977
  * using Drizzle ORM with lifecycle management and automatic reconnection
package/dist/index.d.ts CHANGED
@@ -4944,14 +4944,6 @@ type ConnectionConfig = {
4944
4944
  connection: SQLiteConnectionConfig | string;
4945
4945
  };
4946
4946
 
4947
- /**
4948
- * Connection manager for relational databases using Drizzle ORM
4949
- * @module connection/connection-manager
4950
- */
4951
-
4952
- /**
4953
- * Connection state enum
4954
- */
4955
4947
  declare enum ConnectionState {
4956
4948
  DISCONNECTED = "disconnected",
4957
4949
  CONNECTING = "connecting",
@@ -4959,23 +4951,27 @@ declare enum ConnectionState {
4959
4951
  RECONNECTING = "reconnecting",
4960
4952
  ERROR = "error"
4961
4953
  }
4962
- /**
4963
- * Connection lifecycle events
4964
- */
4965
- type ConnectionEvent = 'connected' | 'disconnected' | 'error' | 'reconnecting';
4966
- /**
4967
- * Reconnection configuration
4968
- */
4969
4954
  interface ReconnectionConfig {
4970
- /** Enable automatic reconnection on connection loss */
4955
+ /** Enable automatic reconnection on connection loss. */
4971
4956
  enabled: boolean;
4972
- /** Maximum number of reconnection attempts (0 = infinite) */
4957
+ /** Maximum number of reconnection attempts (0 = infinite). */
4973
4958
  maxAttempts: number;
4974
- /** Base delay in milliseconds for exponential backoff */
4959
+ /** Base delay in milliseconds for exponential backoff. */
4975
4960
  baseDelayMs: number;
4976
- /** Maximum delay in milliseconds between reconnection attempts */
4961
+ /** Maximum delay in milliseconds between reconnection attempts. */
4977
4962
  maxDelayMs: number;
4978
4963
  }
4964
+
4965
+ /**
4966
+ * Connection manager for relational databases using Drizzle ORM
4967
+ * @module connection/connection-manager
4968
+ */
4969
+
4970
+ /**
4971
+ * Connection lifecycle events
4972
+ */
4973
+ type ConnectionEvent = 'connected' | 'disconnected' | 'error' | 'reconnecting';
4974
+
4979
4975
  /**
4980
4976
  * Connection manager that handles database connections for PostgreSQL, MySQL, and SQLite
4981
4977
  * using Drizzle ORM with lifecycle management and automatic reconnection
package/dist/index.js CHANGED
@@ -4017,6 +4017,94 @@ function quoteQualifiedIdentifier(qualifiedName, vendor) {
4017
4017
  const parts = qualifiedName.split(".");
4018
4018
  return parts.map((part) => quoteIdentifier(part, vendor)).join(".");
4019
4019
  }
4020
+
4021
+ // src/data/relational/connection/lifecycle.ts
4022
+ var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
4023
+ ConnectionState2["DISCONNECTED"] = "disconnected";
4024
+ ConnectionState2["CONNECTING"] = "connecting";
4025
+ ConnectionState2["CONNECTED"] = "connected";
4026
+ ConnectionState2["RECONNECTING"] = "reconnecting";
4027
+ ConnectionState2["ERROR"] = "error";
4028
+ return ConnectionState2;
4029
+ })(ConnectionState || {});
4030
+ function cancelPendingReconnection(timer, logger23, vendor, message) {
4031
+ if (!timer) {
4032
+ return null;
4033
+ }
4034
+ logger23.debug(message, { vendor });
4035
+ clearTimeout(timer);
4036
+ return null;
4037
+ }
4038
+ async function waitForInFlightConnection(promise, logger23, vendor, phase) {
4039
+ if (!promise) {
4040
+ return null;
4041
+ }
4042
+ logger23.debug(`Waiting for in-flight connection attempt to complete before ${phase}`, {
4043
+ vendor
4044
+ });
4045
+ try {
4046
+ await promise;
4047
+ } catch {
4048
+ }
4049
+ return null;
4050
+ }
4051
+ function shutdownClient(vendor, client) {
4052
+ if (vendor === "postgresql" || vendor === "mysql") {
4053
+ return client.end();
4054
+ }
4055
+ if (vendor === "sqlite") {
4056
+ client.close();
4057
+ }
4058
+ }
4059
+ function scheduleReconnection(context, logger23) {
4060
+ if (context.reconnectionConfig.maxAttempts > 0 && context.reconnectionAttempts >= context.reconnectionConfig.maxAttempts) {
4061
+ logger23.error("Max reconnection attempts reached", {
4062
+ vendor: context.vendor,
4063
+ attempts: context.reconnectionAttempts,
4064
+ maxAttempts: context.reconnectionConfig.maxAttempts
4065
+ });
4066
+ return;
4067
+ }
4068
+ const delay2 = Math.min(
4069
+ context.reconnectionConfig.baseDelayMs * Math.pow(2, context.reconnectionAttempts),
4070
+ context.reconnectionConfig.maxDelayMs
4071
+ );
4072
+ const nextAttempt = context.reconnectionAttempts + 1;
4073
+ context.setReconnectionAttempts(nextAttempt);
4074
+ context.setState("reconnecting" /* RECONNECTING */);
4075
+ logger23.info("Scheduling reconnection attempt", {
4076
+ vendor: context.vendor,
4077
+ attempt: nextAttempt,
4078
+ maxAttempts: context.reconnectionConfig.maxAttempts,
4079
+ delayMs: delay2
4080
+ });
4081
+ context.emitReconnecting({
4082
+ attempt: nextAttempt,
4083
+ maxAttempts: context.reconnectionConfig.maxAttempts,
4084
+ delayMs: delay2
4085
+ });
4086
+ const timer = setTimeout(async () => {
4087
+ context.setReconnectionTimer(null);
4088
+ try {
4089
+ logger23.info("Attempting reconnection", {
4090
+ vendor: context.vendor,
4091
+ attempt: nextAttempt
4092
+ });
4093
+ const promise = context.initialize().finally(() => {
4094
+ context.setConnectPromise(null);
4095
+ });
4096
+ context.setConnectPromise(promise);
4097
+ await promise;
4098
+ } catch (error) {
4099
+ logger23.error("Reconnection attempt failed", {
4100
+ vendor: context.vendor,
4101
+ attempt: nextAttempt,
4102
+ error: error instanceof Error ? error.message : String(error)
4103
+ });
4104
+ }
4105
+ }, delay2);
4106
+ context.setReconnectionTimer(timer);
4107
+ }
4020
4108
  var logger7 = createLogger("agentforge:tools:data:relational:connection:vendor-init");
4021
4109
  var SAFE_INITIALIZATION_PATTERNS = [
4022
4110
  "Pool max connections must be",
@@ -4172,14 +4260,6 @@ async function initializeSQLiteConnection(connection) {
4172
4260
  return { client, db };
4173
4261
  }
4174
4262
  var logger8 = createLogger("agentforge:tools:data:relational:connection");
4175
- var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
4176
- ConnectionState2["DISCONNECTED"] = "disconnected";
4177
- ConnectionState2["CONNECTING"] = "connecting";
4178
- ConnectionState2["CONNECTED"] = "connected";
4179
- ConnectionState2["RECONNECTING"] = "reconnecting";
4180
- ConnectionState2["ERROR"] = "error";
4181
- return ConnectionState2;
4182
- })(ConnectionState || {});
4183
4263
  var ConnectionManager = class extends EventEmitter {
4184
4264
  vendor;
4185
4265
  db;
@@ -4229,13 +4309,12 @@ var ConnectionManager = class extends EventEmitter {
4229
4309
  });
4230
4310
  return this.connectPromise;
4231
4311
  }
4232
- if (this.reconnectionTimer) {
4233
- logger8.debug("Clearing pending reconnection timer before manual connect", {
4234
- vendor: this.vendor
4235
- });
4236
- clearTimeout(this.reconnectionTimer);
4237
- this.reconnectionTimer = null;
4238
- }
4312
+ this.reconnectionTimer = cancelPendingReconnection(
4313
+ this.reconnectionTimer,
4314
+ logger8,
4315
+ this.vendor,
4316
+ "Clearing pending reconnection timer before manual connect"
4317
+ );
4239
4318
  this.connectPromise = this.initialize().finally(() => {
4240
4319
  this.connectPromise = null;
4241
4320
  });
@@ -4250,21 +4329,19 @@ var ConnectionManager = class extends EventEmitter {
4250
4329
  */
4251
4330
  async disconnect() {
4252
4331
  this.connectionGeneration++;
4253
- if (this.reconnectionTimer) {
4254
- clearTimeout(this.reconnectionTimer);
4255
- this.reconnectionTimer = null;
4256
- }
4332
+ this.reconnectionTimer = cancelPendingReconnection(
4333
+ this.reconnectionTimer,
4334
+ logger8,
4335
+ this.vendor,
4336
+ "Clearing pending reconnection timer before disconnect"
4337
+ );
4257
4338
  this.reconnectionAttempts = 0;
4258
- if (this.connectPromise) {
4259
- logger8.debug("Waiting for in-flight connection attempt to complete before disconnect", {
4260
- vendor: this.vendor
4261
- });
4262
- try {
4263
- await this.connectPromise;
4264
- } catch {
4265
- }
4266
- this.connectPromise = null;
4267
- }
4339
+ this.connectPromise = await waitForInFlightConnection(
4340
+ this.connectPromise,
4341
+ logger8,
4342
+ this.vendor,
4343
+ "disconnect"
4344
+ );
4268
4345
  await this.close();
4269
4346
  }
4270
4347
  /**
@@ -4419,50 +4496,30 @@ var ConnectionManager = class extends EventEmitter {
4419
4496
  * @private
4420
4497
  */
4421
4498
  scheduleReconnection() {
4422
- if (this.reconnectionConfig.maxAttempts > 0 && this.reconnectionAttempts >= this.reconnectionConfig.maxAttempts) {
4423
- logger8.error("Max reconnection attempts reached", {
4499
+ scheduleReconnection(
4500
+ {
4424
4501
  vendor: this.vendor,
4425
- attempts: this.reconnectionAttempts,
4426
- maxAttempts: this.reconnectionConfig.maxAttempts
4427
- });
4428
- return;
4429
- }
4430
- const delay2 = Math.min(
4431
- this.reconnectionConfig.baseDelayMs * Math.pow(2, this.reconnectionAttempts),
4432
- this.reconnectionConfig.maxDelayMs
4502
+ reconnectionConfig: this.reconnectionConfig,
4503
+ reconnectionAttempts: this.reconnectionAttempts,
4504
+ setReconnectionAttempts: (attempts) => {
4505
+ this.reconnectionAttempts = attempts;
4506
+ },
4507
+ setState: (state) => {
4508
+ this.setState(state);
4509
+ },
4510
+ emitReconnecting: (payload) => {
4511
+ this.emit("reconnecting", payload);
4512
+ },
4513
+ initialize: () => this.initialize(),
4514
+ setConnectPromise: (promise) => {
4515
+ this.connectPromise = promise;
4516
+ },
4517
+ setReconnectionTimer: (timer) => {
4518
+ this.reconnectionTimer = timer;
4519
+ }
4520
+ },
4521
+ logger8
4433
4522
  );
4434
- this.reconnectionAttempts++;
4435
- this.setState("reconnecting" /* RECONNECTING */);
4436
- logger8.info("Scheduling reconnection attempt", {
4437
- vendor: this.vendor,
4438
- attempt: this.reconnectionAttempts,
4439
- maxAttempts: this.reconnectionConfig.maxAttempts,
4440
- delayMs: delay2
4441
- });
4442
- this.emit("reconnecting", {
4443
- attempt: this.reconnectionAttempts,
4444
- maxAttempts: this.reconnectionConfig.maxAttempts,
4445
- delayMs: delay2
4446
- });
4447
- this.reconnectionTimer = setTimeout(async () => {
4448
- this.reconnectionTimer = null;
4449
- try {
4450
- logger8.info("Attempting reconnection", {
4451
- vendor: this.vendor,
4452
- attempt: this.reconnectionAttempts
4453
- });
4454
- this.connectPromise = this.initialize().finally(() => {
4455
- this.connectPromise = null;
4456
- });
4457
- await this.connectPromise;
4458
- } catch (error) {
4459
- logger8.error("Reconnection attempt failed", {
4460
- vendor: this.vendor,
4461
- attempt: this.reconnectionAttempts,
4462
- error: error instanceof Error ? error.message : String(error)
4463
- });
4464
- }
4465
- }, delay2);
4466
4523
  }
4467
4524
  /**
4468
4525
  * Determine whether an error thrown by drizzle-orm's better-sqlite3 adapter
@@ -4632,34 +4689,25 @@ var ConnectionManager = class extends EventEmitter {
4632
4689
  */
4633
4690
  async close() {
4634
4691
  this.connectionGeneration++;
4635
- if (this.reconnectionTimer) {
4636
- logger8.debug("Canceling pending reconnection timer during close", {
4637
- vendor: this.vendor
4638
- });
4639
- clearTimeout(this.reconnectionTimer);
4640
- this.reconnectionTimer = null;
4641
- }
4642
- if (this.connectPromise) {
4643
- logger8.debug("Waiting for in-flight connection attempt to complete before close", {
4644
- vendor: this.vendor
4645
- });
4646
- try {
4647
- await this.connectPromise;
4648
- } catch {
4649
- }
4650
- this.connectPromise = null;
4651
- }
4692
+ this.reconnectionTimer = cancelPendingReconnection(
4693
+ this.reconnectionTimer,
4694
+ logger8,
4695
+ this.vendor,
4696
+ "Canceling pending reconnection timer during close"
4697
+ );
4698
+ this.connectPromise = await waitForInFlightConnection(
4699
+ this.connectPromise,
4700
+ logger8,
4701
+ this.vendor,
4702
+ "close"
4703
+ );
4652
4704
  if (this.client) {
4653
4705
  logger8.info("Closing database connection", {
4654
4706
  vendor: this.vendor,
4655
4707
  state: this.state
4656
4708
  });
4657
4709
  try {
4658
- if (this.vendor === "postgresql" || this.vendor === "mysql") {
4659
- await this.client.end();
4660
- } else if (this.vendor === "sqlite") {
4661
- this.client.close();
4662
- }
4710
+ await shutdownClient(this.vendor, this.client);
4663
4711
  this.setState("disconnected" /* DISCONNECTED */);
4664
4712
  this.emit("disconnected");
4665
4713
  logger8.debug("Database connection closed successfully", {
@@ -4696,11 +4744,7 @@ var ConnectionManager = class extends EventEmitter {
4696
4744
  vendor: this.vendor
4697
4745
  });
4698
4746
  try {
4699
- if (this.vendor === "postgresql" || this.vendor === "mysql") {
4700
- await this.client.end();
4701
- } else if (this.vendor === "sqlite") {
4702
- this.client.close();
4703
- }
4747
+ await shutdownClient(this.vendor, this.client);
4704
4748
  } catch (error) {
4705
4749
  logger8.debug("Error during cancelled connection cleanup", {
4706
4750
  vendor: this.vendor,