@automagik/omni 2.260706.3 → 2.260706.4

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
@@ -30805,6 +30805,7 @@ class NatsEventBus {
30805
30805
  subscriptionManager = new SubscriptionManager;
30806
30806
  connectionAttempts = 0;
30807
30807
  isClosing = false;
30808
+ pendingReconnect = null;
30808
30809
  constructor(config2 = {}) {
30809
30810
  this.config = {
30810
30811
  url: config2.url ?? DEFAULTS.url,
@@ -30824,7 +30825,7 @@ class NatsEventBus {
30824
30825
  servers: this.config.url,
30825
30826
  name: this.config.serviceName,
30826
30827
  reconnect: true,
30827
- maxReconnectAttempts: this.config.reconnect.maxRetries,
30828
+ maxReconnectAttempts: -1,
30828
30829
  reconnectTimeWait: this.config.reconnect.delayMs,
30829
30830
  waitOnFirstConnect: true
30830
30831
  };
@@ -30863,7 +30864,7 @@ class NatsEventBus {
30863
30864
  return this.publishInternal(type, payload, metadata);
30864
30865
  }
30865
30866
  async publishInternal(type, payload, metadata) {
30866
- this.ensureConnected();
30867
+ await this.ensureConnected();
30867
30868
  const eventId = crypto.randomUUID();
30868
30869
  const timestamp = Date.now();
30869
30870
  const event = {
@@ -31049,32 +31050,67 @@ class NatsEventBus {
31049
31050
  getSubscriptionCount() {
31050
31051
  return this.subscriptionManager.size;
31051
31052
  }
31052
- ensureConnected() {
31053
+ async ensureConnected() {
31054
+ if (this.isConnected())
31055
+ return;
31056
+ if (this.isClosing) {
31057
+ throw new Error("Not connected to NATS. Call connect() first.");
31058
+ }
31059
+ const reconnect = this.scheduleReconnect();
31060
+ const winner = await Promise.race([
31061
+ reconnect.then(() => "connected"),
31062
+ this.sleep(ENSURE_CONNECTED_WAIT_MS).then(() => "timeout")
31063
+ ]);
31064
+ if (winner === "timeout" || !this.isConnected()) {
31065
+ throw new Error("Not connected to NATS. Reconnect in progress.");
31066
+ }
31067
+ }
31068
+ scheduleReconnect() {
31069
+ if (!this.pendingReconnect) {
31070
+ log4.warn("Connection lost, rebuilding", { url: this.config.url });
31071
+ this.pendingReconnect = (async () => {
31072
+ this.nc = null;
31073
+ this.js = null;
31074
+ this.jsm = null;
31075
+ await this.connect();
31076
+ })().finally(() => {
31077
+ this.pendingReconnect = null;
31078
+ });
31079
+ this.pendingReconnect.catch(() => {});
31080
+ }
31081
+ return this.pendingReconnect;
31082
+ }
31083
+ assertConnected() {
31053
31084
  if (!this.isConnected()) {
31054
31085
  throw new Error("Not connected to NATS. Call connect() first.");
31055
31086
  }
31056
31087
  }
31057
31088
  requireJetStream() {
31058
- this.ensureConnected();
31089
+ this.assertConnected();
31059
31090
  if (!this.js) {
31060
31091
  throw new Error("JetStream not initialized");
31061
31092
  }
31062
31093
  return this.js;
31063
31094
  }
31064
31095
  requireJetStreamManager() {
31065
- this.ensureConnected();
31096
+ this.assertConnected();
31066
31097
  if (!this.jsm) {
31067
31098
  throw new Error("JetStreamManager not initialized");
31068
31099
  }
31069
31100
  return this.jsm;
31070
31101
  }
31071
31102
  setupConnectionHandlers() {
31072
- if (!this.nc)
31103
+ const nc = this.nc;
31104
+ if (!nc)
31073
31105
  return;
31074
- (async () => {
31075
- if (!this.nc)
31106
+ nc.closed().then((err) => {
31107
+ if (this.isClosing || this.nc !== nc)
31076
31108
  return;
31077
- for await (const status of this.nc.status()) {
31109
+ log4.error("Connection closed unexpectedly", { error: err ? String(err) : "none" });
31110
+ this.scheduleReconnect();
31111
+ });
31112
+ (async () => {
31113
+ for await (const status of nc.status()) {
31078
31114
  switch (status.type) {
31079
31115
  case "disconnect":
31080
31116
  log4.warn("Disconnected from NATS");
@@ -31109,7 +31145,7 @@ async function connectEventBus(config2) {
31109
31145
  await bus.connect();
31110
31146
  return bus;
31111
31147
  }
31112
- var import_api2, import_nats, log4, DEFAULTS;
31148
+ var import_api2, import_nats, log4, DEFAULTS, ENSURE_CONNECTED_WAIT_MS = 5000;
31113
31149
  var init_client = __esm(() => {
31114
31150
  init_logger();
31115
31151
  init_consumer();
@@ -125020,7 +125056,7 @@ import { fileURLToPath } from "url";
125020
125056
  // package.json
125021
125057
  var package_default = {
125022
125058
  name: "@automagik/omni",
125023
- version: "2.260706.3",
125059
+ version: "2.260706.4",
125024
125060
  description: "LLM-optimized CLI for Omni",
125025
125061
  type: "module",
125026
125062
  bin: {
@@ -22765,6 +22765,7 @@ class NatsEventBus {
22765
22765
  subscriptionManager = new SubscriptionManager;
22766
22766
  connectionAttempts = 0;
22767
22767
  isClosing = false;
22768
+ pendingReconnect = null;
22768
22769
  constructor(config2 = {}) {
22769
22770
  this.config = {
22770
22771
  url: config2.url ?? DEFAULTS.url,
@@ -22784,7 +22785,7 @@ class NatsEventBus {
22784
22785
  servers: this.config.url,
22785
22786
  name: this.config.serviceName,
22786
22787
  reconnect: true,
22787
- maxReconnectAttempts: this.config.reconnect.maxRetries,
22788
+ maxReconnectAttempts: -1,
22788
22789
  reconnectTimeWait: this.config.reconnect.delayMs,
22789
22790
  waitOnFirstConnect: true
22790
22791
  };
@@ -22823,7 +22824,7 @@ class NatsEventBus {
22823
22824
  return this.publishInternal(type, payload, metadata);
22824
22825
  }
22825
22826
  async publishInternal(type, payload, metadata) {
22826
- this.ensureConnected();
22827
+ await this.ensureConnected();
22827
22828
  const eventId = crypto.randomUUID();
22828
22829
  const timestamp = Date.now();
22829
22830
  const event = {
@@ -23009,32 +23010,67 @@ class NatsEventBus {
23009
23010
  getSubscriptionCount() {
23010
23011
  return this.subscriptionManager.size;
23011
23012
  }
23012
- ensureConnected() {
23013
+ async ensureConnected() {
23014
+ if (this.isConnected())
23015
+ return;
23016
+ if (this.isClosing) {
23017
+ throw new Error("Not connected to NATS. Call connect() first.");
23018
+ }
23019
+ const reconnect = this.scheduleReconnect();
23020
+ const winner = await Promise.race([
23021
+ reconnect.then(() => "connected"),
23022
+ this.sleep(ENSURE_CONNECTED_WAIT_MS).then(() => "timeout")
23023
+ ]);
23024
+ if (winner === "timeout" || !this.isConnected()) {
23025
+ throw new Error("Not connected to NATS. Reconnect in progress.");
23026
+ }
23027
+ }
23028
+ scheduleReconnect() {
23029
+ if (!this.pendingReconnect) {
23030
+ log4.warn("Connection lost, rebuilding", { url: this.config.url });
23031
+ this.pendingReconnect = (async () => {
23032
+ this.nc = null;
23033
+ this.js = null;
23034
+ this.jsm = null;
23035
+ await this.connect();
23036
+ })().finally(() => {
23037
+ this.pendingReconnect = null;
23038
+ });
23039
+ this.pendingReconnect.catch(() => {});
23040
+ }
23041
+ return this.pendingReconnect;
23042
+ }
23043
+ assertConnected() {
23013
23044
  if (!this.isConnected()) {
23014
23045
  throw new Error("Not connected to NATS. Call connect() first.");
23015
23046
  }
23016
23047
  }
23017
23048
  requireJetStream() {
23018
- this.ensureConnected();
23049
+ this.assertConnected();
23019
23050
  if (!this.js) {
23020
23051
  throw new Error("JetStream not initialized");
23021
23052
  }
23022
23053
  return this.js;
23023
23054
  }
23024
23055
  requireJetStreamManager() {
23025
- this.ensureConnected();
23056
+ this.assertConnected();
23026
23057
  if (!this.jsm) {
23027
23058
  throw new Error("JetStreamManager not initialized");
23028
23059
  }
23029
23060
  return this.jsm;
23030
23061
  }
23031
23062
  setupConnectionHandlers() {
23032
- if (!this.nc)
23063
+ const nc = this.nc;
23064
+ if (!nc)
23033
23065
  return;
23034
- (async () => {
23035
- if (!this.nc)
23066
+ nc.closed().then((err) => {
23067
+ if (this.isClosing || this.nc !== nc)
23036
23068
  return;
23037
- for await (const status of this.nc.status()) {
23069
+ log4.error("Connection closed unexpectedly", { error: err ? String(err) : "none" });
23070
+ this.scheduleReconnect();
23071
+ });
23072
+ (async () => {
23073
+ for await (const status of nc.status()) {
23038
23074
  switch (status.type) {
23039
23075
  case "disconnect":
23040
23076
  log4.warn("Disconnected from NATS");
@@ -23066,7 +23102,7 @@ async function connectEventBus(config2) {
23066
23102
  await bus.connect();
23067
23103
  return bus;
23068
23104
  }
23069
- var import_api2, import_nats, log4, DEFAULTS;
23105
+ var import_api2, import_nats, log4, DEFAULTS, ENSURE_CONNECTED_WAIT_MS = 5000;
23070
23106
  var init_client = __esm(() => {
23071
23107
  init_logger();
23072
23108
  init_consumer();
@@ -225845,7 +225881,7 @@ var init_sentry_scrub = __esm(() => {
225845
225881
  var require_package7 = __commonJS((exports, module) => {
225846
225882
  module.exports = {
225847
225883
  name: "@omni/api",
225848
- version: "2.260706.3",
225884
+ version: "2.260706.4",
225849
225885
  type: "module",
225850
225886
  exports: {
225851
225887
  ".": {
@@ -344234,7 +344270,8 @@ var import__package2, VERSION6, startTime2, healthRoutes, getHealth = async (c)
344234
344270
  }
344235
344271
  let natsCheck;
344236
344272
  if (eventBus) {
344237
- natsCheck = { status: "ok", details: { connected: true } };
344273
+ const connected = eventBus.isConnected();
344274
+ natsCheck = connected ? { status: "ok", details: { connected: true } } : { status: "error", details: { connected: false }, error: "NATS connection is not established" };
344238
344275
  } else {
344239
344276
  natsCheck = { status: "ok", details: { connected: false, reason: "Not configured" } };
344240
344277
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automagik/omni",
3
- "version": "2.260706.3",
3
+ "version": "2.260706.4",
4
4
  "description": "LLM-optimized CLI for Omni",
5
5
  "type": "module",
6
6
  "bin": {