@xapp/chat-widget 1.111.0 → 1.111.1

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
@@ -7138,10 +7138,28 @@ function requestToMessage(botRequest, now) {
7138
7138
  }
7139
7139
 
7140
7140
  var ROUTER_USER = "router";
7141
+ /**
7142
+ * Shown when the server reports an error outside the router protocol and no failure
7143
+ * messages are configured.
7144
+ */
7145
+ var DEFAULT_GATEWAY_ERROR_TEXT = "Sorry, we cannot communicate with the assistant right now. Please check your connection or try again later.";
7146
+ function isGatewayErrorFrame(frame) {
7147
+ if (!frame || typeof frame !== "object") {
7148
+ return false;
7149
+ }
7150
+ var f = frame;
7151
+ return f.event === undefined && typeof f.message === "string";
7152
+ }
7141
7153
  var serverInfo = {
7142
7154
  deviceId: "Router",
7143
7155
  userId: ROUTER_USER,
7144
- displayName: "XAPP Server"};
7156
+ isAdmin: false,
7157
+ displayName: "XAPP Server",
7158
+ urlAttributes: {
7159
+ path: [],
7160
+ query: {}
7161
+ }
7162
+ };
7145
7163
  var StentorRouterChat = /** @class */ (function () {
7146
7164
  function StentorRouterChat(config, options) {
7147
7165
  var _a, _b, _c, _d, _e;
@@ -7154,6 +7172,8 @@ var StentorRouterChat = /** @class */ (function () {
7154
7172
  this.isAdmin = false;
7155
7173
  this.urlAttributes = {};
7156
7174
  this.handlers = {};
7175
+ // Senders whose typing indicator is currently on, so it can be cleared when a request fails
7176
+ this.typingSenders = new Map();
7157
7177
  // Sleep delay management
7158
7178
  // Use number | NodeJS.Timeout for cross-environment compatibility (browser vs Node.js)
7159
7179
  this.sleepTimeoutId = null;
@@ -7337,6 +7357,7 @@ var StentorRouterChat = /** @class */ (function () {
7337
7357
  });
7338
7358
  };
7339
7359
  this.handlers["typing"] = function (_data, sender, ts, _attributes) {
7360
+ _this.typingSenders.set(senderToNick(sender), sender);
7340
7361
  _this.dispatch({
7341
7362
  type: "chat",
7342
7363
  detail: {
@@ -7350,6 +7371,7 @@ var StentorRouterChat = /** @class */ (function () {
7350
7371
  });
7351
7372
  };
7352
7373
  this.handlers["stop typing"] = function (_data, sender, ts, _attributes) {
7374
+ _this.typingSenders.delete(senderToNick(sender));
7353
7375
  _this.dispatch({
7354
7376
  type: "chat",
7355
7377
  detail: {
@@ -7590,7 +7612,18 @@ var StentorRouterChat = /** @class */ (function () {
7590
7612
  };
7591
7613
  this.ws.onmessage = function (me) {
7592
7614
  log("ROUTER says: ".concat(me.data));
7593
- var message = JSON.parse(me.data);
7615
+ var message;
7616
+ try {
7617
+ message = JSON.parse(me.data);
7618
+ }
7619
+ catch (e) {
7620
+ err("Unparseable router message: ".concat(me.data));
7621
+ return;
7622
+ }
7623
+ if (isGatewayErrorFrame(message)) {
7624
+ _this.handleGatewayError(message);
7625
+ return;
7626
+ }
7594
7627
  var handler = _this.handlers[message.event];
7595
7628
  if (handler) {
7596
7629
  _this.handlers[message.event](message.data, message.sender, message.timeMs, message.attributes, message.messageId);
@@ -7699,6 +7732,32 @@ var StentorRouterChat = /** @class */ (function () {
7699
7732
  }
7700
7733
  });
7701
7734
  };
7735
+ /**
7736
+ * The gateway answered instead of the router (typically a Lambda timeout). The request is
7737
+ * lost and nothing else will arrive for it, so stop any typing indicator and tell the visitor.
7738
+ */
7739
+ StentorRouterChat.prototype.handleGatewayError = function (frame) {
7740
+ var _this = this;
7741
+ err("SERVER: gateway error: \"".concat(frame.message, "\" (requestId: ").concat(frame.requestId || "?", ")"));
7742
+ this.typingSenders.forEach(function (sender) {
7743
+ _this.dispatch({
7744
+ type: "chat",
7745
+ detail: {
7746
+ type: "chat.typing",
7747
+ user: {
7748
+ nick: senderToNick(sender),
7749
+ },
7750
+ typing: false,
7751
+ timestamp: +new Date(),
7752
+ },
7753
+ });
7754
+ });
7755
+ this.typingSenders.clear();
7756
+ // The last configured message is the final, no-retry one
7757
+ var last = this.configurableMessages[this.configurableMessages.length - 1];
7758
+ var configured = last && last.text ? last : undefined;
7759
+ this.sendFailureMessage(configured ? configured.retry : 1, 0, configured ? configured.text : DEFAULT_GATEWAY_ERROR_TEXT, serverInfo);
7760
+ };
7702
7761
  StentorRouterChat.prototype.sendDisconnectMessage = function () {
7703
7762
  this.dispatch({
7704
7763
  type: "chat",
@@ -16208,7 +16267,7 @@ const createQueueWithTimer = (timeout) => {
16208
16267
  setTimeout(notify, timeout);
16209
16268
  };
16210
16269
  };
16211
- const createRafWithFallbackTimer = (raf, timeout) => {
16270
+ const createRafWithFallbackTimer = (raf, caf, timeout) => {
16212
16271
  return (notify) => {
16213
16272
  let called = false;
16214
16273
  let rafId;
@@ -16216,7 +16275,7 @@ const createRafWithFallbackTimer = (raf, timeout) => {
16216
16275
  const callback = () => {
16217
16276
  if (called) return;
16218
16277
  called = true;
16219
- cancelAnimationFrame(rafId);
16278
+ caf?.(rafId);
16220
16279
  clearTimeout(timerId);
16221
16280
  notify();
16222
16281
  };
@@ -16230,7 +16289,7 @@ const autoBatchEnhancer = (options = { type: "raf" }) => (next) => (...args) =>
16230
16289
  let shouldNotifyAtEndOfTick = false;
16231
16290
  let notificationQueued = false;
16232
16291
  const listeners = /* @__PURE__ */ new Set();
16233
- const queueCallback = options.type === "tick" ? queueMicrotask : options.type === "raf" ? typeof window !== "undefined" && window.requestAnimationFrame ? createRafWithFallbackTimer(window.requestAnimationFrame, 100) : createQueueWithTimer(10) : options.type === "callback" ? options.queueNotification : createQueueWithTimer(options.timeout);
16292
+ const queueCallback = options.type === "tick" ? queueMicrotask : options.type === "raf" ? typeof window !== "undefined" && window.requestAnimationFrame ? createRafWithFallbackTimer(window.requestAnimationFrame, window.cancelAnimationFrame, 100) : createQueueWithTimer(10) : options.type === "callback" ? options.queueNotification : createQueueWithTimer(options.timeout);
16234
16293
  const notifyListeners = () => {
16235
16294
  notificationQueued = false;
16236
16295
  if (shouldNotifyAtEndOfTick) {