bruce-models 7.1.107 → 7.1.108

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.
@@ -18909,6 +18909,8 @@
18909
18909
  constructor(toolHandler, options = {}) {
18910
18910
  this.ws = null;
18911
18911
  this.isConnecting = false;
18912
+ this.responseGeneration = 0;
18913
+ this.pendingResponses = [];
18912
18914
  this.toolHandler = toolHandler;
18913
18915
  this.options = options;
18914
18916
  this.currentAuth = options.auth;
@@ -18919,7 +18921,7 @@
18919
18921
  connect(auth) {
18920
18922
  var _a, _b, _c, _d, _e, _f, _g;
18921
18923
  if (auth) {
18922
- this.currentAuth = auth;
18924
+ this.setAuth(auth);
18923
18925
  }
18924
18926
  if (this.isConnecting || ((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
18925
18927
  (_c = (_b = this.logger()).debug) === null || _c === void 0 ? void 0 : _c.call(_b, "[NavigatorMcpWS] Connection already active or in-flight; skipping connect.");
@@ -18930,24 +18932,34 @@
18930
18932
  const url = this.buildUrl();
18931
18933
  (_e = (_d = this.logger()).log) === null || _e === void 0 ? void 0 : _e.call(_d, "[NavigatorMcpWS] Connecting to", url);
18932
18934
  this.ws = new WebSocket(url);
18935
+ const socket = this.ws;
18933
18936
  this.ws.onopen = (event) => {
18934
18937
  var _a, _b, _c, _d;
18938
+ if (this.ws !== socket)
18939
+ return;
18935
18940
  (_b = (_a = this.logger()).log) === null || _b === void 0 ? void 0 : _b.call(_a, "[NavigatorMcpWS] Connected");
18936
18941
  this.isConnecting = false;
18937
18942
  this.send({ method: "server_ready" });
18943
+ this.flushResponses();
18938
18944
  (_d = (_c = this.options).onOpen) === null || _d === void 0 ? void 0 : _d.call(_c, event);
18939
18945
  };
18940
18946
  this.ws.onmessage = (event) => {
18947
+ if (this.ws !== socket)
18948
+ return;
18941
18949
  this.handleMessage(event.data);
18942
18950
  };
18943
18951
  this.ws.onerror = (event) => {
18944
18952
  var _a, _b, _c, _d;
18953
+ if (this.ws !== socket)
18954
+ return;
18945
18955
  (_b = (_a = this.logger()).error) === null || _b === void 0 ? void 0 : _b.call(_a, "[NavigatorMcpWS] Error", event);
18946
18956
  (_d = (_c = this.options).onError) === null || _d === void 0 ? void 0 : _d.call(_c, event);
18947
18957
  this.isConnecting = false;
18948
18958
  };
18949
18959
  this.ws.onclose = (event) => {
18950
18960
  var _a, _b, _c, _d;
18961
+ if (this.ws !== socket)
18962
+ return;
18951
18963
  (_b = (_a = this.logger()).log) === null || _b === void 0 ? void 0 : _b.call(_a, "[NavigatorMcpWS] Disconnected");
18952
18964
  this.ws = null;
18953
18965
  this.isConnecting = false;
@@ -18961,6 +18973,9 @@
18961
18973
  }
18962
18974
  disconnect() {
18963
18975
  var _a, _b;
18976
+ // An explicit disconnect ends the logical session; never replay its results.
18977
+ this.responseGeneration++;
18978
+ this.pendingResponses = [];
18964
18979
  if (this.ws) {
18965
18980
  (_b = (_a = this.logger()).debug) === null || _b === void 0 ? void 0 : _b.call(_a, "[NavigatorMcpWS] Closing connection");
18966
18981
  this.ws.close();
@@ -18977,6 +18992,16 @@
18977
18992
  * Call connect() afterwards to establish a connection with the new auth.
18978
18993
  */
18979
18994
  setAuth(auth) {
18995
+ const key = (value) => JSON.stringify([
18996
+ value === null || value === void 0 ? void 0 : value.accountId,
18997
+ value === null || value === void 0 ? void 0 : value.sessionId,
18998
+ value === null || value === void 0 ? void 0 : value.fingerprint,
18999
+ value === null || value === void 0 ? void 0 : value.apiBaseUrl
19000
+ ]);
19001
+ if (key(auth) !== key(this.currentAuth)) {
19002
+ this.responseGeneration++;
19003
+ this.pendingResponses = [];
19004
+ }
18980
19005
  this.currentAuth = auth;
18981
19006
  }
18982
19007
  sendChatEvent(event) {
@@ -19021,7 +19046,7 @@
19021
19046
  return urlInstance.toString();
19022
19047
  }
19023
19048
  handleMessage(raw) {
19024
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
19049
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
19025
19050
  return __awaiter(this, void 0, void 0, function* () {
19026
19051
  try {
19027
19052
  const message = typeof raw === "string" ? JSON.parse(raw) : raw;
@@ -19043,6 +19068,7 @@
19043
19068
  return;
19044
19069
  }
19045
19070
  let result;
19071
+ const generation = this.responseGeneration;
19046
19072
  try {
19047
19073
  result = yield this.toolHandler({
19048
19074
  name: toolName,
@@ -19054,23 +19080,62 @@
19054
19080
  (_l = (_k = this.logger()).error) === null || _l === void 0 ? void 0 : _l.call(_k, "[NavigatorMcpWS] Tool handler threw", error);
19055
19081
  result = `Error: ${error instanceof Error ? error.message : String(error)}`;
19056
19082
  }
19057
- this.send({
19083
+ if (generation !== this.responseGeneration) {
19084
+ (_o = (_m = this.logger()).warn) === null || _o === void 0 ? void 0 : _o.call(_m, "[NavigatorMcpWS] Discarding result from a previous session", messageId);
19085
+ return;
19086
+ }
19087
+ this.sendResponse({
19058
19088
  method: "tool_response",
19059
19089
  id: messageId,
19060
19090
  result
19061
19091
  });
19062
19092
  }
19063
19093
  catch (error) {
19064
- (_o = (_m = this.logger()).error) === null || _o === void 0 ? void 0 : _o.call(_m, "[NavigatorMcpWS] Invalid message", error);
19094
+ (_q = (_p = this.logger()).error) === null || _q === void 0 ? void 0 : _q.call(_p, "[NavigatorMcpWS] Invalid message", error);
19065
19095
  }
19066
19096
  });
19067
19097
  }
19098
+ sendResponse(message) {
19099
+ var _a, _b, _c, _d, _e;
19100
+ if (this.send(message))
19101
+ return;
19102
+ this.pendingResponses = this.pendingResponses.filter((entry) => entry.expiresAt > Date.now());
19103
+ if (this.pendingResponses.length >= NavigatorMcpWebSocketClient.MAX_PENDING_RESPONSES) {
19104
+ const dropped = this.pendingResponses.shift();
19105
+ (_b = (_a = this.logger()).warn) === null || _b === void 0 ? void 0 : _b.call(_a, "[NavigatorMcpWS] Response queue full; dropping oldest result", (_c = dropped === null || dropped === void 0 ? void 0 : dropped.message) === null || _c === void 0 ? void 0 : _c.id);
19106
+ }
19107
+ this.pendingResponses.push({ message, expiresAt: Date.now() + NavigatorMcpWebSocketClient.RESPONSE_TTL_MS });
19108
+ (_e = (_d = this.logger()).warn) === null || _e === void 0 ? void 0 : _e.call(_d, "[NavigatorMcpWS] Tool result queued until reconnect", message.id);
19109
+ }
19110
+ flushResponses() {
19111
+ var _a, _b;
19112
+ const pending = this.pendingResponses;
19113
+ this.pendingResponses = [];
19114
+ for (const entry of pending) {
19115
+ if (entry.expiresAt <= Date.now()) {
19116
+ (_b = (_a = this.logger()).warn) === null || _b === void 0 ? void 0 : _b.call(_a, "[NavigatorMcpWS] Tool result expired before reconnect", entry.message.id);
19117
+ }
19118
+ else if (!this.send(entry.message)) {
19119
+ this.pendingResponses.push(entry);
19120
+ }
19121
+ }
19122
+ }
19068
19123
  send(message) {
19069
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
19124
+ var _a, _b;
19125
+ if (!this.ws || this.ws.readyState !== WebSocket.OPEN)
19126
+ return false;
19127
+ try {
19070
19128
  this.ws.send(JSON.stringify(message));
19129
+ return true;
19130
+ }
19131
+ catch (error) {
19132
+ (_b = (_a = this.logger()).error) === null || _b === void 0 ? void 0 : _b.call(_a, "[NavigatorMcpWS] Send failed", error);
19133
+ return false;
19071
19134
  }
19072
19135
  }
19073
19136
  }
19137
+ NavigatorMcpWebSocketClient.RESPONSE_TTL_MS = 60000;
19138
+ NavigatorMcpWebSocketClient.MAX_PENDING_RESPONSES = 32;
19074
19139
 
19075
19140
  (function (Plugin) {
19076
19141
  /**
@@ -24359,7 +24424,7 @@
24359
24424
  }
24360
24425
 
24361
24426
  // This is updated with the package.json version on build.
24362
- const VERSION = "7.1.107";
24427
+ const VERSION = "7.1.108";
24363
24428
 
24364
24429
  exports.VERSION = VERSION;
24365
24430
  exports.AbstractApi = AbstractApi;