@microsoft/omnichannel-chat-widget 1.7.8-main.29ce174 → 1.7.8-main.50111af

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.
Files changed (95) hide show
  1. package/lib/cjs/common/Constants.js +57 -18
  2. package/lib/cjs/common/KeyCodes.js +6 -1
  3. package/lib/cjs/common/contextDataStore/DataStoreManager.js +6 -1
  4. package/lib/cjs/common/facades/FacadeChatSDK.js +385 -292
  5. package/lib/cjs/common/storage/default/defaultCacheManager.js +6 -3
  6. package/lib/cjs/common/telemetry/TelemetryConstants.js +137 -116
  7. package/lib/cjs/common/telemetry/TelemetryHelper.js +219 -181
  8. package/lib/cjs/common/telemetry/TelemetryManager.js +9 -2
  9. package/lib/cjs/components/chatbuttonstateful/ChatButtonStateful.js +4 -5
  10. package/lib/cjs/components/headerstateful/HeaderStateful.js +3 -5
  11. package/lib/cjs/components/livechatwidget/common/ActivityStreamHandler.js +32 -19
  12. package/lib/cjs/components/livechatwidget/common/ActivitySubscriber/BotAuthActivitySubscriber.js +71 -56
  13. package/lib/cjs/components/livechatwidget/common/ActivitySubscriber/DefaultActivitySubscriber.js +17 -8
  14. package/lib/cjs/components/livechatwidget/common/ActivitySubscriber/HiddenAdaptiveCardActivitySubscriber.js +65 -52
  15. package/lib/cjs/components/livechatwidget/common/ActivitySubscriber/PauseActivitySubscriber.js +31 -18
  16. package/lib/cjs/components/livechatwidget/common/ChatAdapterShim.js +14 -6
  17. package/lib/cjs/components/livechatwidget/common/Deferred.js +14 -6
  18. package/lib/cjs/components/livechatwidget/common/chatDisconnectHelper.js +3 -1
  19. package/lib/cjs/components/livechatwidget/common/endChat.js +4 -18
  20. package/lib/cjs/components/livechatwidget/common/reconnectChatHelper.js +0 -5
  21. package/lib/cjs/components/livechatwidget/common/startChat.js +2 -23
  22. package/lib/cjs/components/livechatwidget/livechatwidgetstateful/LiveChatWidgetStateful.js +3 -3
  23. package/lib/cjs/components/ooohpanestateful/OOOHPaneStateful.js +6 -4
  24. package/lib/cjs/components/postchatsurveypanestateful/PostChatSurveyPaneStateful.js +7 -6
  25. package/lib/cjs/components/webchatcontainerstateful/WebChatContainerStateful.js +4 -3
  26. package/lib/cjs/components/webchatcontainerstateful/common/DemoChatAdapter.js +88 -65
  27. package/lib/cjs/components/webchatcontainerstateful/common/DemoChatSDK.js +26 -7
  28. package/lib/cjs/components/webchatcontainerstateful/common/DesignerChatAdapter.js +49 -26
  29. package/lib/cjs/components/webchatcontainerstateful/common/DesignerChatSDK.js +57 -35
  30. package/lib/cjs/components/webchatcontainerstateful/common/mockadapter.js +62 -52
  31. package/lib/cjs/components/webchatcontainerstateful/common/mockchatsdk.js +95 -61
  32. package/lib/cjs/components/webchatcontainerstateful/webchatcontroller/BotMagicCodeStore.js +6 -1
  33. package/lib/cjs/components/webchatcontainerstateful/webchatcontroller/WebChatStoreLoader.js +6 -1
  34. package/lib/cjs/components/webchatcontainerstateful/webchatcontroller/markdownrenderers/HyperlinkTextOverrideRenderer.js +53 -39
  35. package/lib/cjs/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/timestamps/DeliveredTimestamp.js +4 -0
  36. package/lib/cjs/components/webchatcontainerstateful/webchatcontroller/notification/NotificationHandler.js +88 -60
  37. package/lib/cjs/contexts/common/LiveChatWidgetContextInitialState.js +18 -6
  38. package/lib/cjs/firstresponselatency/Constants.js +13 -0
  39. package/lib/cjs/firstresponselatency/FirstMessageTrackerFromBot.js +118 -0
  40. package/lib/cjs/firstresponselatency/FirstResponseLatencyTracker.js +201 -0
  41. package/lib/cjs/firstresponselatency/util.js +98 -0
  42. package/lib/cjs/plugins/createChatTranscript.js +88 -58
  43. package/lib/cjs/plugins/newMessageEventHandler.js +102 -88
  44. package/lib/esm/common/Constants.js +57 -18
  45. package/lib/esm/common/KeyCodes.js +6 -1
  46. package/lib/esm/common/contextDataStore/DataStoreManager.js +6 -1
  47. package/lib/esm/common/facades/FacadeChatSDK.js +385 -292
  48. package/lib/esm/common/storage/default/defaultCacheManager.js +6 -1
  49. package/lib/esm/common/telemetry/TelemetryConstants.js +137 -116
  50. package/lib/esm/common/telemetry/TelemetryHelper.js +219 -181
  51. package/lib/esm/common/telemetry/TelemetryManager.js +9 -2
  52. package/lib/esm/components/chatbuttonstateful/ChatButtonStateful.js +4 -5
  53. package/lib/esm/components/headerstateful/HeaderStateful.js +3 -5
  54. package/lib/esm/components/livechatwidget/common/ActivityStreamHandler.js +32 -19
  55. package/lib/esm/components/livechatwidget/common/ActivitySubscriber/BotAuthActivitySubscriber.js +71 -56
  56. package/lib/esm/components/livechatwidget/common/ActivitySubscriber/DefaultActivitySubscriber.js +17 -8
  57. package/lib/esm/components/livechatwidget/common/ActivitySubscriber/HiddenAdaptiveCardActivitySubscriber.js +65 -52
  58. package/lib/esm/components/livechatwidget/common/ActivitySubscriber/PauseActivitySubscriber.js +31 -18
  59. package/lib/esm/components/livechatwidget/common/ChatAdapterShim.js +14 -6
  60. package/lib/esm/components/livechatwidget/common/Deferred.js +14 -6
  61. package/lib/esm/components/livechatwidget/common/chatDisconnectHelper.js +3 -1
  62. package/lib/esm/components/livechatwidget/common/endChat.js +4 -18
  63. package/lib/esm/components/livechatwidget/common/reconnectChatHelper.js +1 -6
  64. package/lib/esm/components/livechatwidget/common/startChat.js +3 -24
  65. package/lib/esm/components/livechatwidget/livechatwidgetstateful/LiveChatWidgetStateful.js +3 -3
  66. package/lib/esm/components/ooohpanestateful/OOOHPaneStateful.js +6 -4
  67. package/lib/esm/components/postchatsurveypanestateful/PostChatSurveyPaneStateful.js +7 -6
  68. package/lib/esm/components/webchatcontainerstateful/WebChatContainerStateful.js +4 -3
  69. package/lib/esm/components/webchatcontainerstateful/common/DemoChatAdapter.js +88 -65
  70. package/lib/esm/components/webchatcontainerstateful/common/DemoChatSDK.js +26 -7
  71. package/lib/esm/components/webchatcontainerstateful/common/DesignerChatAdapter.js +49 -26
  72. package/lib/esm/components/webchatcontainerstateful/common/DesignerChatSDK.js +57 -35
  73. package/lib/esm/components/webchatcontainerstateful/common/mockadapter.js +63 -52
  74. package/lib/esm/components/webchatcontainerstateful/common/mockchatsdk.js +95 -61
  75. package/lib/esm/components/webchatcontainerstateful/webchatcontroller/BotMagicCodeStore.js +6 -1
  76. package/lib/esm/components/webchatcontainerstateful/webchatcontroller/WebChatStoreLoader.js +6 -1
  77. package/lib/esm/components/webchatcontainerstateful/webchatcontroller/markdownrenderers/HyperlinkTextOverrideRenderer.js +53 -39
  78. package/lib/esm/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/timestamps/DeliveredTimestamp.js +4 -0
  79. package/lib/esm/components/webchatcontainerstateful/webchatcontroller/notification/NotificationHandler.js +88 -60
  80. package/lib/esm/contexts/common/LiveChatWidgetContextInitialState.js +18 -6
  81. package/lib/esm/firstresponselatency/Constants.js +6 -0
  82. package/lib/esm/firstresponselatency/FirstMessageTrackerFromBot.js +112 -0
  83. package/lib/esm/firstresponselatency/FirstResponseLatencyTracker.js +194 -0
  84. package/lib/esm/firstresponselatency/util.js +87 -0
  85. package/lib/esm/plugins/createChatTranscript.js +88 -58
  86. package/lib/esm/plugins/newMessageEventHandler.js +100 -86
  87. package/lib/types/common/facades/FacadeChatSDK.d.ts +1 -0
  88. package/lib/types/common/telemetry/TelemetryConstants.d.ts +8 -1
  89. package/lib/types/components/webchatcontainerstateful/interfaces/IWebChatProps.d.ts +2 -2
  90. package/lib/types/contexts/common/ILiveChatWidgetContext.d.ts +1 -1
  91. package/lib/types/firstresponselatency/Constants.d.ts +30 -0
  92. package/lib/types/firstresponselatency/FirstMessageTrackerFromBot.d.ts +1 -0
  93. package/lib/types/firstresponselatency/FirstResponseLatencyTracker.d.ts +22 -0
  94. package/lib/types/firstresponselatency/util.d.ts +7 -0
  95. package/package.json +18 -6
@@ -1,11 +1,15 @@
1
- import { ConversationState } from "./ConversationState";
2
- import { defaultMiddlewareLocalizedTexts } from "../../components/webchatcontainerstateful/common/defaultProps/defaultMiddlewareLocalizedTexts";
3
- import { getWidgetCacheIdfromProps, isNullOrUndefined } from "../../common/utils";
4
- import { defaultClientDataStoreProvider } from "../../common/storage/default/defaultClientDataStoreProvider";
5
1
  import { ConfirmationState, Constants, ConversationEndEntity, StorageType } from "../../common/Constants";
2
+ import { getWidgetCacheIdfromProps, isNullOrUndefined } from "../../common/utils";
3
+ import { ConversationState } from "./ConversationState";
6
4
  import { StartChatFailureType } from "./StartChatFailureType";
5
+ import { defaultClientDataStoreProvider } from "../../common/storage/default/defaultClientDataStoreProvider";
6
+ import { defaultMiddlewareLocalizedTexts } from "../../components/webchatcontainerstateful/common/defaultProps/defaultMiddlewareLocalizedTexts";
7
7
  export const getLiveChatWidgetContextInitialState = props => {
8
8
  var _props$controlProps, _props$webChatContain;
9
+ const isOutsideOperatingHours = () => {
10
+ var _props$chatConfig, _props$chatConfig$Liv, _props$chatConfig$Liv2;
11
+ return ((_props$chatConfig = props.chatConfig) === null || _props$chatConfig === void 0 ? void 0 : (_props$chatConfig$Liv = _props$chatConfig.LiveWSAndLiveChatEngJoin) === null || _props$chatConfig$Liv === void 0 ? void 0 : (_props$chatConfig$Liv2 = _props$chatConfig$Liv.OutOfOperatingHours) === null || _props$chatConfig$Liv2 === void 0 ? void 0 : _props$chatConfig$Liv2.toString().toLowerCase()) === "true";
12
+ };
9
13
  const widgetCacheId = getWidgetCacheIdfromProps(props);
10
14
  const cacheTtlInMins = (props === null || props === void 0 ? void 0 : (_props$controlProps = props.controlProps) === null || _props$controlProps === void 0 ? void 0 : _props$controlProps.cacheTtlInMins) ?? Constants.CacheTtlInMinutes;
11
15
  const storageType = (props === null || props === void 0 ? void 0 : props.useSessionStorage) === true ? StorageType.sessionStorage : StorageType.localStorage;
@@ -22,6 +26,14 @@ export const getLiveChatWidgetContextInitialState = props => {
22
26
  if (initialStateFromCache.appStates.conversationState === ConversationState.Prechat) {
23
27
  initialStateFromCache.appStates.conversationState = ConversationState.Closed;
24
28
  }
29
+
30
+ // we are always setting the chatConfig from the props to avoid any issues with the cache
31
+ initialStateFromCache.domainStates.liveChatConfig = props.chatConfig;
32
+
33
+ // Cache the result of isOutsideOperatingHours() to ensure consistency
34
+ const outsideOperatingHours = isOutsideOperatingHours();
35
+ initialStateFromCache.appStates.outsideOperatingHours = outsideOperatingHours;
36
+ initialStateFromCache.appStates.conversationState = outsideOperatingHours ? ConversationState.OutOfOffice : initialStateFromCache.appStates.conversationState;
25
37
  return initialStateFromCache;
26
38
  }
27
39
  const LiveChatWidgetContextInitialState = {
@@ -46,11 +58,11 @@ export const getLiveChatWidgetContextInitialState = props => {
46
58
  startChatFailureType: StartChatFailureType.Generic
47
59
  },
48
60
  appStates: {
49
- conversationState: ConversationState.Closed,
61
+ conversationState: isOutsideOperatingHours() ? ConversationState.OutOfOffice : ConversationState.Closed,
50
62
  isMinimized: undefined,
51
63
  previousElementIdOnFocusBeforeModalOpen: null,
52
64
  startChatFailed: false,
53
- outsideOperatingHours: false,
65
+ outsideOperatingHours: isOutsideOperatingHours(),
54
66
  preChatResponseEmail: "",
55
67
  isAudioMuted: null,
56
68
  newMessage: false,
@@ -0,0 +1,6 @@
1
+ export let ScenarioType;
2
+ (function (ScenarioType) {
3
+ ScenarioType["UserSendMessageStrategy"] = "UserSendMessageStrategy";
4
+ ScenarioType["SystemMessageStrategy"] = "SystemMessageStrategy";
5
+ ScenarioType["ReceivedMessageStrategy"] = "ReceivedMessageStrategy";
6
+ })(ScenarioType || (ScenarioType = {}));
@@ -0,0 +1,112 @@
1
+ import { BroadcastEvent, LogLevel, TelemetryEvent } from "../common/telemetry/TelemetryConstants";
2
+ import { BroadcastService } from "@microsoft/omnichannel-chat-components";
3
+ import { TelemetryHelper } from "../common/telemetry/TelemetryHelper";
4
+ import { createTrackingMessage } from "./util";
5
+
6
+ // This tracker is event based, this is since we are tracking events coming from different sources
7
+ // with different timeline, therefore this is a functional approach to track the events, instead of a class based approach
8
+ export const createTrackingForFirstMessage = () => {
9
+ // Reset the tracking variables
10
+ let startTracking = false;
11
+ let stopTracking = false;
12
+ let startTime = 0;
13
+ let stopTime = 0;
14
+ let stopTrackingMessage;
15
+ let flag = false;
16
+ const isMessageFromValidSender = payload => {
17
+ var _payload$tags;
18
+ // agent scenario
19
+ if (payload !== null && payload !== void 0 && (_payload$tags = payload.tags) !== null && _payload$tags !== void 0 && _payload$tags.includes("public")) {
20
+ return false;
21
+ }
22
+ return true;
23
+ };
24
+ const widgetLoadListener = BroadcastService.getMessageByEventName(TelemetryEvent.WidgetLoadComplete).subscribe(() => {
25
+ if (startTracking) return;
26
+ startTracking = true;
27
+ startTime = new Date().getTime();
28
+ });
29
+ const newMessageListener = BroadcastService.getMessageByEventName(BroadcastEvent.NewMessageReceived).subscribe(message => {
30
+ const payload = message.payload;
31
+
32
+ // we only care for bot, so we need to check if the message is from the bot
33
+ // pending to add typing message indicator signal detection
34
+
35
+ if (isMessageFromValidSender(payload)) {
36
+ if (startTracking && !stopTracking) {
37
+ stopTime = new Date().getTime();
38
+ const elapsedTime = stopTime - startTime;
39
+ stopTracking = true;
40
+ stopTrackingMessage = createTrackingMessage(payload, "botMessage");
41
+ notifyFMLTrackingCompleted();
42
+ TelemetryHelper.logActionEvent(LogLevel.INFO, {
43
+ Event: TelemetryEvent.BotFirstMessageLapTrack,
44
+ Description: "First Message from Bot latency tracking",
45
+ CustomProperties: {
46
+ elapsedTime,
47
+ widgetLoadedAt: startTime,
48
+ botMessage: stopTrackingMessage
49
+ }
50
+ });
51
+ }
52
+ }
53
+
54
+ // this track only first message, if coming from the bot or not
55
+ // the only difference is that it logs only those from bot
56
+ disconnectListener();
57
+ });
58
+ const notifyFMLTrackingCompleted = () => {
59
+ ackListener();
60
+ // Retry sending until flag is true, but do not block the main thread
61
+ const interval = setInterval(() => {
62
+ if (flag) {
63
+ clearInterval(interval);
64
+ } else {
65
+ BroadcastService.postMessage({
66
+ eventName: BroadcastEvent.FMLTrackingCompleted,
67
+ payload: null
68
+ });
69
+ }
70
+ }, 100);
71
+ };
72
+ const ackListener = () => {
73
+ const listen = BroadcastService.getMessageByEventName(BroadcastEvent.FMLTrackingCompletedAck).subscribe(() => {
74
+ flag = true;
75
+ listen.unsubscribe();
76
+ });
77
+ };
78
+
79
+ // Rehydrate message is received when the widget is reloaded, this is to ensure that we are not tracking messages that are not part of the current conversation
80
+ // No need to keep listerning for tracking, enforcing disconnection for the listners
81
+ const rehydrateListener = BroadcastService.getMessageByEventName(TelemetryEvent.RehydrateMessageReceived).subscribe(() => {
82
+ startTracking = false;
83
+ stopTracking = false;
84
+ disconnectListener();
85
+ });
86
+
87
+ // Rehydrate message is received when the widget is reloaded, this is to ensure that we are not tracking messages that are not part of the current conversation
88
+ // No need to keep listerning for tracking, enforcing disconnection for the listners
89
+ const historyListener = BroadcastService.getMessageByEventName(BroadcastEvent.HistoryMessageReceived).subscribe(() => {
90
+ startTracking = false;
91
+ stopTracking = false;
92
+ disconnectListener();
93
+ });
94
+ const offlineNetworkListener = BroadcastService.getMessageByEventName(TelemetryEvent.NetworkDisconnected).subscribe(() => {
95
+ startTracking = false;
96
+ stopTracking = false;
97
+ disconnectListener();
98
+ TelemetryHelper.logActionEvent(LogLevel.INFO, {
99
+ Event: TelemetryEvent.BotFirstMessageLapTrackError,
100
+ Description: "Tracker Stopped due to network disconnection"
101
+ });
102
+ });
103
+
104
+ // this is to ensure that we are not tracking messages that are not part of the current conversation
105
+ const disconnectListener = () => {
106
+ historyListener.unsubscribe();
107
+ rehydrateListener.unsubscribe();
108
+ newMessageListener.unsubscribe();
109
+ widgetLoadListener.unsubscribe();
110
+ offlineNetworkListener.unsubscribe();
111
+ };
112
+ };
@@ -0,0 +1,194 @@
1
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
3
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
4
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
5
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
6
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
7
+ import { BroadcastEvent, LogLevel, TelemetryEvent } from "../common/telemetry/TelemetryConstants";
8
+ import { BroadcastService } from "@microsoft/omnichannel-chat-components";
9
+ import { TelemetryHelper } from "../common/telemetry/TelemetryHelper";
10
+ export let FirstResponseLatencyTracker = /*#__PURE__*/function () {
11
+ function FirstResponseLatencyTracker() {
12
+ _classCallCheck(this, FirstResponseLatencyTracker);
13
+ _defineProperty(this, "isABotConversation", false);
14
+ _defineProperty(this, "isStarted", false);
15
+ _defineProperty(this, "isEnded", false);
16
+ _defineProperty(this, "startTrackingMessage", void 0);
17
+ _defineProperty(this, "stopTrackingMessage", void 0);
18
+ _defineProperty(this, "isReady", false);
19
+ _defineProperty(this, "offlineNetworkListener", BroadcastService.getMessageByEventName(TelemetryEvent.NetworkDisconnected).subscribe(() => {
20
+ this.isStarted = false;
21
+ this.isEnded = false;
22
+ TelemetryHelper.logActionEvent(LogLevel.INFO, {
23
+ Event: TelemetryEvent.MessageStopLapTrackError,
24
+ Description: "Tracker Stopped due to network disconnection"
25
+ });
26
+ }));
27
+ _defineProperty(this, "fmltrackingListener", BroadcastService.getMessageByEventName(BroadcastEvent.FMLTrackingCompleted).subscribe(() => {
28
+ this.isReady = true;
29
+ BroadcastService.postMessage({
30
+ eventName: BroadcastEvent.FMLTrackingCompletedAck,
31
+ payload: null
32
+ });
33
+ }));
34
+ // Rehydrate message is received when the widget is reloaded, this is to ensure that we are not tracking messages that are not part of the current conversation
35
+ // No need to keep listerning for tracking, enforcing disconnection for the listners
36
+ _defineProperty(this, "rehydrateListener", BroadcastService.getMessageByEventName(TelemetryEvent.RehydrateMessageReceived).subscribe(() => {
37
+ this.isReady = true;
38
+ }));
39
+ // Rehydrate message is received when the widget is reloaded, this is to ensure that we are not tracking messages that are not part of the current conversation
40
+ // No need to keep listerning for tracking, enforcing disconnection for the listners
41
+ _defineProperty(this, "historyListener", BroadcastService.getMessageByEventName(BroadcastEvent.HistoryMessageReceived).subscribe(() => {
42
+ this.isReady = true;
43
+ }));
44
+ // this is a workaround to ensure in reload we track effectively the messages
45
+ // we do have a mechanism in place to prevent log agent messages.
46
+ this.isABotConversation = true;
47
+ }
48
+ _createClass(FirstResponseLatencyTracker, [{
49
+ key: "createTrackingMessage",
50
+ value: function createTrackingMessage(payload, type) {
51
+ return {
52
+ Id: payload.Id,
53
+ role: payload.role,
54
+ timestamp: payload === null || payload === void 0 ? void 0 : payload.timestamp,
55
+ tags: payload.tags,
56
+ messageType: payload.messageType,
57
+ text: payload.text,
58
+ type: type,
59
+ checkTime: new Date().getTime()
60
+ };
61
+ }
62
+
63
+ // Tracking Functions
64
+ }, {
65
+ key: "startTracking",
66
+ value: function startTracking(payload) {
67
+ if (!this.isReady) return;
68
+ // this prevents to initiate tracking for multiple incoming messages
69
+ if (this.isStarted) {
70
+ return;
71
+ }
72
+ // this is to ensure we track only messages where bot is engaged
73
+ if (!this.isABotConversation) {
74
+ return;
75
+ }
76
+ // control of states to prevent clashing of messages
77
+ this.isStarted = true;
78
+ this.isEnded = false;
79
+ // The idea of using types is to enrich telemetry data
80
+ this.startTrackingMessage = this.createTrackingMessage(payload, "userMessage");
81
+ }
82
+ }, {
83
+ key: "handleAgentMessage",
84
+ value: function handleAgentMessage(payload) {
85
+ var _payload$tags;
86
+ // this tag so far is only present in agent messages
87
+ if (payload !== null && payload !== void 0 && (_payload$tags = payload.tags) !== null && _payload$tags !== void 0 && _payload$tags.includes("public")) {
88
+ this.deregister();
89
+ }
90
+ }
91
+ }, {
92
+ key: "stopTracking",
93
+ value: function stopTracking(payload) {
94
+ var _this$stopTrackingMes, _this$startTrackingMe;
95
+ // this prevents execution for multiple incoming messages from the bot.
96
+ if (this.isEnded && !this.isStarted) {
97
+ return;
98
+ }
99
+
100
+ // control of states to prevent clashing of messages
101
+ this.isEnded = true;
102
+ this.isStarted = false;
103
+
104
+ // The idea of using types is to enrich telemetry data
105
+ this.stopTrackingMessage = this.createTrackingMessage(payload, "botMessage");
106
+ // calculating elapsed time
107
+ const elapsedTime = (((_this$stopTrackingMes = this.stopTrackingMessage) === null || _this$stopTrackingMes === void 0 ? void 0 : _this$stopTrackingMes.checkTime) ?? 0) - (((_this$startTrackingMe = this.startTrackingMessage) === null || _this$startTrackingMe === void 0 ? void 0 : _this$startTrackingMe.checkTime) ?? 0);
108
+ TelemetryHelper.logActionEvent(LogLevel.INFO, {
109
+ Event: TelemetryEvent.MessageLapTrack,
110
+ Description: "First response latency tracking",
111
+ CustomProperties: {
112
+ elapsedTime,
113
+ userMessage: this.startTrackingMessage,
114
+ botMessage: this.stopTrackingMessage
115
+ }
116
+ });
117
+ }
118
+
119
+ // mechanism to ensure we track only allowed conversations
120
+ }, {
121
+ key: "isMessageFromValidSender",
122
+ value: function isMessageFromValidSender(payload) {
123
+ var _payload$tags2;
124
+ // agent scenario
125
+ if (payload !== null && payload !== void 0 && (_payload$tags2 = payload.tags) !== null && _payload$tags2 !== void 0 && _payload$tags2.includes("public")) {
126
+ this.handleAgentMessage(payload);
127
+ return false;
128
+ }
129
+ return true;
130
+ }
131
+ }, {
132
+ key: "startClock",
133
+ value: function startClock(payload) {
134
+ try {
135
+ if (!payload || !payload.Id) {
136
+ throw new Error("Invalid payload");
137
+ }
138
+ this.startTracking(payload);
139
+ } catch (e) {
140
+ TelemetryHelper.logActionEvent(LogLevel.ERROR, {
141
+ Event: TelemetryEvent.MessageStartLapTrackError,
142
+ Description: "Error while starting the clock",
143
+ ExceptionDetails: e,
144
+ CustomProperties: {
145
+ payload: payload
146
+ }
147
+ });
148
+ }
149
+ }
150
+ }, {
151
+ key: "stopClock",
152
+ value: function stopClock(payload) {
153
+ try {
154
+ if (!payload || !payload.Id) {
155
+ throw new Error("Invalid payload");
156
+ }
157
+ if (!this.isMessageFromValidSender(payload)) return;
158
+ if (this.isABotConversation && this.isStarted) {
159
+ this.stopTracking(payload);
160
+ }
161
+ } catch (e) {
162
+ console.error("FRL : error while trying to stop the tracker", e);
163
+ TelemetryHelper.logActionEvent(LogLevel.ERROR, {
164
+ Event: TelemetryEvent.MessageStopLapTrackError,
165
+ Description: "Error while stopping the clock",
166
+ ExceptionDetails: e,
167
+ CustomProperties: {
168
+ payload: payload
169
+ }
170
+ });
171
+ //reset state
172
+ this.startTrackingMessage = undefined;
173
+ this.stopTrackingMessage = undefined;
174
+ this.isStarted = false;
175
+ this.isEnded = false;
176
+ }
177
+ }
178
+ }, {
179
+ key: "deregister",
180
+ value: function deregister() {
181
+ // Reset State
182
+ this.isABotConversation = false;
183
+ this.isStarted = false;
184
+ this.isEnded = false;
185
+ this.startTrackingMessage = undefined;
186
+ this.stopTrackingMessage = undefined;
187
+ this.offlineNetworkListener.unsubscribe();
188
+ this.fmltrackingListener.unsubscribe();
189
+ this.rehydrateListener.unsubscribe();
190
+ this.historyListener.unsubscribe();
191
+ }
192
+ }]);
193
+ return FirstResponseLatencyTracker;
194
+ }();
@@ -0,0 +1,87 @@
1
+ import { ScenarioType } from "./Constants";
2
+ import { Constants } from "../common/Constants";
3
+ export const isHistoryMessage = (activity, startTime) => {
4
+ try {
5
+ if ((activity === null || activity === void 0 ? void 0 : activity.type) === Constants.message) {
6
+ var _activity$channelData, _activity$channelData2;
7
+ // this is an old piece of code, probably no longer relevant
8
+ if (activity !== null && activity !== void 0 && (_activity$channelData = activity.channelData) !== null && _activity$channelData !== void 0 && (_activity$channelData2 = _activity$channelData.tags) !== null && _activity$channelData2 !== void 0 && _activity$channelData2.includes(Constants.historyMessageTag)) return true;
9
+
10
+ // Id is an epoch time in milliseconds , in utc format, for some reason is in a string format
11
+ if (activity !== null && activity !== void 0 && activity.id) {
12
+ /// activity.id is an string that contains epoch time in milliseconds
13
+ const activityId = parseInt(activity === null || activity === void 0 ? void 0 : activity.id);
14
+
15
+ // if the activity id is not a number, we default to new message
16
+ if (isNaN(activityId)) {
17
+ return false;
18
+ }
19
+
20
+ // if the activity id is less than the start time, it means that the message is a history message
21
+ if (activityId < startTime) {
22
+ return true;
23
+ }
24
+ }
25
+ // anything else will be considered a new message
26
+ return false;
27
+ }
28
+ } catch (e) {
29
+ // if there is an error in parsing the activity id, we will consider it a new message
30
+ console.error("Error in parsing activity id: ", e);
31
+ }
32
+ return false;
33
+ };
34
+ export const buildMessagePayload = (activity, userId) => {
35
+ var _text, _text2, _activity$channelData3, _activity$from;
36
+ return {
37
+ // To identify hidden contents vs empty content
38
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
+ text: (activity === null || activity === void 0 ? void 0 : (_text = activity.text) === null || _text === void 0 ? void 0 : _text.length) >= 1 ? `*contents hidden (${activity === null || activity === void 0 ? void 0 : (_text2 = activity.text) === null || _text2 === void 0 ? void 0 : _text2.length} chars)*` : "",
40
+ type: activity === null || activity === void 0 ? void 0 : activity.type,
41
+ timestamp: activity === null || activity === void 0 ? void 0 : activity.timestamp,
42
+ userId: userId,
43
+ tags: activity === null || activity === void 0 ? void 0 : (_activity$channelData3 = activity.channelData) === null || _activity$channelData3 === void 0 ? void 0 : _activity$channelData3.tags,
44
+ messageType: "",
45
+ Id: activity === null || activity === void 0 ? void 0 : activity.id,
46
+ role: activity === null || activity === void 0 ? void 0 : (_activity$from = activity.from) === null || _activity$from === void 0 ? void 0 : _activity$from.role,
47
+ isChatComplete: false
48
+ };
49
+ };
50
+ export const polyfillMessagePayloadForEvent = (activity, payload, conversationId) => {
51
+ var _activity$conversatio, _attachments, _activity$from2;
52
+ return {
53
+ ...payload,
54
+ channelData: activity === null || activity === void 0 ? void 0 : activity.channelData,
55
+ chatId: activity === null || activity === void 0 ? void 0 : (_activity$conversatio = activity.conversation) === null || _activity$conversatio === void 0 ? void 0 : _activity$conversatio.id,
56
+ conversationId: conversationId,
57
+ Id: activity === null || activity === void 0 ? void 0 : activity.id,
58
+ isChatComplete: false,
59
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
+ text: activity === null || activity === void 0 ? void 0 : activity.text,
61
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
62
+ attachment: (activity === null || activity === void 0 ? void 0 : (_attachments = activity.attachments) === null || _attachments === void 0 ? void 0 : _attachments.length) >= 1 ? activity === null || activity === void 0 ? void 0 : activity.attachments : [],
63
+ role: activity === null || activity === void 0 ? void 0 : (_activity$from2 = activity.from) === null || _activity$from2 === void 0 ? void 0 : _activity$from2.role
64
+ };
65
+ };
66
+ export const getScenarioType = activity => {
67
+ var _activity$from3, _activity$channelData4, _activity$channelData5;
68
+ if ((activity === null || activity === void 0 ? void 0 : (_activity$from3 = activity.from) === null || _activity$from3 === void 0 ? void 0 : _activity$from3.role) === Constants.userMessageTag) {
69
+ return ScenarioType.UserSendMessageStrategy;
70
+ }
71
+ if (activity !== null && activity !== void 0 && (_activity$channelData4 = activity.channelData) !== null && _activity$channelData4 !== void 0 && (_activity$channelData5 = _activity$channelData4.tags) !== null && _activity$channelData5 !== void 0 && _activity$channelData5.includes(Constants.systemMessageTag)) {
72
+ return ScenarioType.SystemMessageStrategy;
73
+ }
74
+ return ScenarioType.ReceivedMessageStrategy;
75
+ };
76
+ export const createTrackingMessage = (payload, type) => {
77
+ return {
78
+ Id: payload.Id,
79
+ role: payload.role,
80
+ timestamp: payload === null || payload === void 0 ? void 0 : payload.timestamp,
81
+ tags: payload.tags,
82
+ messageType: payload.messageType,
83
+ text: payload.text,
84
+ type: type,
85
+ checkTime: new Date().getTime()
86
+ };
87
+ };