@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.cjs CHANGED
@@ -4044,6 +4044,94 @@ function quoteQualifiedIdentifier(qualifiedName, vendor) {
4044
4044
  const parts = qualifiedName.split(".");
4045
4045
  return parts.map((part) => quoteIdentifier(part, vendor)).join(".");
4046
4046
  }
4047
+
4048
+ // src/data/relational/connection/lifecycle.ts
4049
+ var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
4050
+ ConnectionState2["DISCONNECTED"] = "disconnected";
4051
+ ConnectionState2["CONNECTING"] = "connecting";
4052
+ ConnectionState2["CONNECTED"] = "connected";
4053
+ ConnectionState2["RECONNECTING"] = "reconnecting";
4054
+ ConnectionState2["ERROR"] = "error";
4055
+ return ConnectionState2;
4056
+ })(ConnectionState || {});
4057
+ function cancelPendingReconnection(timer, logger23, vendor, message) {
4058
+ if (!timer) {
4059
+ return null;
4060
+ }
4061
+ logger23.debug(message, { vendor });
4062
+ clearTimeout(timer);
4063
+ return null;
4064
+ }
4065
+ async function waitForInFlightConnection(promise, logger23, vendor, phase) {
4066
+ if (!promise) {
4067
+ return null;
4068
+ }
4069
+ logger23.debug(`Waiting for in-flight connection attempt to complete before ${phase}`, {
4070
+ vendor
4071
+ });
4072
+ try {
4073
+ await promise;
4074
+ } catch {
4075
+ }
4076
+ return null;
4077
+ }
4078
+ function shutdownClient(vendor, client) {
4079
+ if (vendor === "postgresql" || vendor === "mysql") {
4080
+ return client.end();
4081
+ }
4082
+ if (vendor === "sqlite") {
4083
+ client.close();
4084
+ }
4085
+ }
4086
+ function scheduleReconnection(context, logger23) {
4087
+ if (context.reconnectionConfig.maxAttempts > 0 && context.reconnectionAttempts >= context.reconnectionConfig.maxAttempts) {
4088
+ logger23.error("Max reconnection attempts reached", {
4089
+ vendor: context.vendor,
4090
+ attempts: context.reconnectionAttempts,
4091
+ maxAttempts: context.reconnectionConfig.maxAttempts
4092
+ });
4093
+ return;
4094
+ }
4095
+ const delay2 = Math.min(
4096
+ context.reconnectionConfig.baseDelayMs * Math.pow(2, context.reconnectionAttempts),
4097
+ context.reconnectionConfig.maxDelayMs
4098
+ );
4099
+ const nextAttempt = context.reconnectionAttempts + 1;
4100
+ context.setReconnectionAttempts(nextAttempt);
4101
+ context.setState("reconnecting" /* RECONNECTING */);
4102
+ logger23.info("Scheduling reconnection attempt", {
4103
+ vendor: context.vendor,
4104
+ attempt: nextAttempt,
4105
+ maxAttempts: context.reconnectionConfig.maxAttempts,
4106
+ delayMs: delay2
4107
+ });
4108
+ context.emitReconnecting({
4109
+ attempt: nextAttempt,
4110
+ maxAttempts: context.reconnectionConfig.maxAttempts,
4111
+ delayMs: delay2
4112
+ });
4113
+ const timer = setTimeout(async () => {
4114
+ context.setReconnectionTimer(null);
4115
+ try {
4116
+ logger23.info("Attempting reconnection", {
4117
+ vendor: context.vendor,
4118
+ attempt: nextAttempt
4119
+ });
4120
+ const promise = context.initialize().finally(() => {
4121
+ context.setConnectPromise(null);
4122
+ });
4123
+ context.setConnectPromise(promise);
4124
+ await promise;
4125
+ } catch (error) {
4126
+ logger23.error("Reconnection attempt failed", {
4127
+ vendor: context.vendor,
4128
+ attempt: nextAttempt,
4129
+ error: error instanceof Error ? error.message : String(error)
4130
+ });
4131
+ }
4132
+ }, delay2);
4133
+ context.setReconnectionTimer(timer);
4134
+ }
4047
4135
  var logger7 = core.createLogger("agentforge:tools:data:relational:connection:vendor-init");
4048
4136
  var SAFE_INITIALIZATION_PATTERNS = [
4049
4137
  "Pool max connections must be",
@@ -4199,14 +4287,6 @@ async function initializeSQLiteConnection(connection) {
4199
4287
  return { client, db };
4200
4288
  }
4201
4289
  var logger8 = core.createLogger("agentforge:tools:data:relational:connection");
4202
- var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
4203
- ConnectionState2["DISCONNECTED"] = "disconnected";
4204
- ConnectionState2["CONNECTING"] = "connecting";
4205
- ConnectionState2["CONNECTED"] = "connected";
4206
- ConnectionState2["RECONNECTING"] = "reconnecting";
4207
- ConnectionState2["ERROR"] = "error";
4208
- return ConnectionState2;
4209
- })(ConnectionState || {});
4210
4290
  var ConnectionManager = class extends events.EventEmitter {
4211
4291
  vendor;
4212
4292
  db;
@@ -4256,13 +4336,12 @@ var ConnectionManager = class extends events.EventEmitter {
4256
4336
  });
4257
4337
  return this.connectPromise;
4258
4338
  }
4259
- if (this.reconnectionTimer) {
4260
- logger8.debug("Clearing pending reconnection timer before manual connect", {
4261
- vendor: this.vendor
4262
- });
4263
- clearTimeout(this.reconnectionTimer);
4264
- this.reconnectionTimer = null;
4265
- }
4339
+ this.reconnectionTimer = cancelPendingReconnection(
4340
+ this.reconnectionTimer,
4341
+ logger8,
4342
+ this.vendor,
4343
+ "Clearing pending reconnection timer before manual connect"
4344
+ );
4266
4345
  this.connectPromise = this.initialize().finally(() => {
4267
4346
  this.connectPromise = null;
4268
4347
  });
@@ -4277,21 +4356,19 @@ var ConnectionManager = class extends events.EventEmitter {
4277
4356
  */
4278
4357
  async disconnect() {
4279
4358
  this.connectionGeneration++;
4280
- if (this.reconnectionTimer) {
4281
- clearTimeout(this.reconnectionTimer);
4282
- this.reconnectionTimer = null;
4283
- }
4359
+ this.reconnectionTimer = cancelPendingReconnection(
4360
+ this.reconnectionTimer,
4361
+ logger8,
4362
+ this.vendor,
4363
+ "Clearing pending reconnection timer before disconnect"
4364
+ );
4284
4365
  this.reconnectionAttempts = 0;
4285
- if (this.connectPromise) {
4286
- logger8.debug("Waiting for in-flight connection attempt to complete before disconnect", {
4287
- vendor: this.vendor
4288
- });
4289
- try {
4290
- await this.connectPromise;
4291
- } catch {
4292
- }
4293
- this.connectPromise = null;
4294
- }
4366
+ this.connectPromise = await waitForInFlightConnection(
4367
+ this.connectPromise,
4368
+ logger8,
4369
+ this.vendor,
4370
+ "disconnect"
4371
+ );
4295
4372
  await this.close();
4296
4373
  }
4297
4374
  /**
@@ -4446,50 +4523,30 @@ var ConnectionManager = class extends events.EventEmitter {
4446
4523
  * @private
4447
4524
  */
4448
4525
  scheduleReconnection() {
4449
- if (this.reconnectionConfig.maxAttempts > 0 && this.reconnectionAttempts >= this.reconnectionConfig.maxAttempts) {
4450
- logger8.error("Max reconnection attempts reached", {
4526
+ scheduleReconnection(
4527
+ {
4451
4528
  vendor: this.vendor,
4452
- attempts: this.reconnectionAttempts,
4453
- maxAttempts: this.reconnectionConfig.maxAttempts
4454
- });
4455
- return;
4456
- }
4457
- const delay2 = Math.min(
4458
- this.reconnectionConfig.baseDelayMs * Math.pow(2, this.reconnectionAttempts),
4459
- this.reconnectionConfig.maxDelayMs
4529
+ reconnectionConfig: this.reconnectionConfig,
4530
+ reconnectionAttempts: this.reconnectionAttempts,
4531
+ setReconnectionAttempts: (attempts) => {
4532
+ this.reconnectionAttempts = attempts;
4533
+ },
4534
+ setState: (state) => {
4535
+ this.setState(state);
4536
+ },
4537
+ emitReconnecting: (payload) => {
4538
+ this.emit("reconnecting", payload);
4539
+ },
4540
+ initialize: () => this.initialize(),
4541
+ setConnectPromise: (promise) => {
4542
+ this.connectPromise = promise;
4543
+ },
4544
+ setReconnectionTimer: (timer) => {
4545
+ this.reconnectionTimer = timer;
4546
+ }
4547
+ },
4548
+ logger8
4460
4549
  );
4461
- this.reconnectionAttempts++;
4462
- this.setState("reconnecting" /* RECONNECTING */);
4463
- logger8.info("Scheduling reconnection attempt", {
4464
- vendor: this.vendor,
4465
- attempt: this.reconnectionAttempts,
4466
- maxAttempts: this.reconnectionConfig.maxAttempts,
4467
- delayMs: delay2
4468
- });
4469
- this.emit("reconnecting", {
4470
- attempt: this.reconnectionAttempts,
4471
- maxAttempts: this.reconnectionConfig.maxAttempts,
4472
- delayMs: delay2
4473
- });
4474
- this.reconnectionTimer = setTimeout(async () => {
4475
- this.reconnectionTimer = null;
4476
- try {
4477
- logger8.info("Attempting reconnection", {
4478
- vendor: this.vendor,
4479
- attempt: this.reconnectionAttempts
4480
- });
4481
- this.connectPromise = this.initialize().finally(() => {
4482
- this.connectPromise = null;
4483
- });
4484
- await this.connectPromise;
4485
- } catch (error) {
4486
- logger8.error("Reconnection attempt failed", {
4487
- vendor: this.vendor,
4488
- attempt: this.reconnectionAttempts,
4489
- error: error instanceof Error ? error.message : String(error)
4490
- });
4491
- }
4492
- }, delay2);
4493
4550
  }
4494
4551
  /**
4495
4552
  * Determine whether an error thrown by drizzle-orm's better-sqlite3 adapter
@@ -4659,34 +4716,25 @@ var ConnectionManager = class extends events.EventEmitter {
4659
4716
  */
4660
4717
  async close() {
4661
4718
  this.connectionGeneration++;
4662
- if (this.reconnectionTimer) {
4663
- logger8.debug("Canceling pending reconnection timer during close", {
4664
- vendor: this.vendor
4665
- });
4666
- clearTimeout(this.reconnectionTimer);
4667
- this.reconnectionTimer = null;
4668
- }
4669
- if (this.connectPromise) {
4670
- logger8.debug("Waiting for in-flight connection attempt to complete before close", {
4671
- vendor: this.vendor
4672
- });
4673
- try {
4674
- await this.connectPromise;
4675
- } catch {
4676
- }
4677
- this.connectPromise = null;
4678
- }
4719
+ this.reconnectionTimer = cancelPendingReconnection(
4720
+ this.reconnectionTimer,
4721
+ logger8,
4722
+ this.vendor,
4723
+ "Canceling pending reconnection timer during close"
4724
+ );
4725
+ this.connectPromise = await waitForInFlightConnection(
4726
+ this.connectPromise,
4727
+ logger8,
4728
+ this.vendor,
4729
+ "close"
4730
+ );
4679
4731
  if (this.client) {
4680
4732
  logger8.info("Closing database connection", {
4681
4733
  vendor: this.vendor,
4682
4734
  state: this.state
4683
4735
  });
4684
4736
  try {
4685
- if (this.vendor === "postgresql" || this.vendor === "mysql") {
4686
- await this.client.end();
4687
- } else if (this.vendor === "sqlite") {
4688
- this.client.close();
4689
- }
4737
+ await shutdownClient(this.vendor, this.client);
4690
4738
  this.setState("disconnected" /* DISCONNECTED */);
4691
4739
  this.emit("disconnected");
4692
4740
  logger8.debug("Database connection closed successfully", {
@@ -4723,11 +4771,7 @@ var ConnectionManager = class extends events.EventEmitter {
4723
4771
  vendor: this.vendor
4724
4772
  });
4725
4773
  try {
4726
- if (this.vendor === "postgresql" || this.vendor === "mysql") {
4727
- await this.client.end();
4728
- } else if (this.vendor === "sqlite") {
4729
- this.client.close();
4730
- }
4774
+ await shutdownClient(this.vendor, this.client);
4731
4775
  } catch (error) {
4732
4776
  logger8.debug("Error during cancelled connection cleanup", {
4733
4777
  vendor: this.vendor,