@microsoft/omnichannel-chat-widget 1.7.8-main.7a07fc5 → 1.7.8-main.ab4d3b4

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 (48) hide show
  1. package/lib/cjs/common/telemetry/TelemetryConstants.js +9 -0
  2. package/lib/cjs/components/chatbuttonstateful/ChatButtonStateful.js +4 -5
  3. package/lib/cjs/components/headerstateful/HeaderStateful.js +3 -5
  4. package/lib/cjs/components/livechatwidget/common/ActivitySubscriber/BotAuthActivitySubscriber.js +4 -1
  5. package/lib/cjs/components/livechatwidget/common/chatDisconnectHelper.js +3 -1
  6. package/lib/cjs/components/livechatwidget/common/endChat.js +4 -18
  7. package/lib/cjs/components/livechatwidget/common/startChat.js +2 -0
  8. package/lib/cjs/components/livechatwidget/livechatwidgetstateful/LiveChatWidgetStateful.js +17 -11
  9. package/lib/cjs/components/ooohpanestateful/OOOHPaneStateful.js +6 -4
  10. package/lib/cjs/components/postchatsurveypanestateful/PostChatSurveyPaneStateful.js +22 -6
  11. package/lib/cjs/components/postchatsurveypanestateful/common/isValidSurveyUrl.js +28 -0
  12. package/lib/cjs/components/webchatcontainerstateful/WebChatContainerStateful.js +4 -3
  13. package/lib/cjs/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/timestamps/DeliveredTimestamp.js +4 -0
  14. package/lib/cjs/contexts/common/LiveChatWidgetContextInitialState.js +18 -6
  15. package/lib/cjs/firstresponselatency/Constants.js +13 -0
  16. package/lib/cjs/firstresponselatency/FirstMessageTrackerFromBot.js +118 -0
  17. package/lib/cjs/firstresponselatency/FirstResponseLatencyTracker.js +179 -0
  18. package/lib/cjs/firstresponselatency/util.js +98 -0
  19. package/lib/cjs/plugins/createChatTranscript.js +4 -4
  20. package/lib/cjs/plugins/newMessageEventHandler.js +102 -88
  21. package/lib/esm/common/telemetry/TelemetryConstants.js +9 -0
  22. package/lib/esm/components/chatbuttonstateful/ChatButtonStateful.js +4 -5
  23. package/lib/esm/components/headerstateful/HeaderStateful.js +3 -5
  24. package/lib/esm/components/livechatwidget/common/ActivitySubscriber/BotAuthActivitySubscriber.js +4 -1
  25. package/lib/esm/components/livechatwidget/common/chatDisconnectHelper.js +3 -1
  26. package/lib/esm/components/livechatwidget/common/endChat.js +4 -18
  27. package/lib/esm/components/livechatwidget/common/startChat.js +2 -0
  28. package/lib/esm/components/livechatwidget/livechatwidgetstateful/LiveChatWidgetStateful.js +17 -11
  29. package/lib/esm/components/ooohpanestateful/OOOHPaneStateful.js +6 -4
  30. package/lib/esm/components/postchatsurveypanestateful/PostChatSurveyPaneStateful.js +22 -6
  31. package/lib/esm/components/postchatsurveypanestateful/common/isValidSurveyUrl.js +21 -0
  32. package/lib/esm/components/webchatcontainerstateful/WebChatContainerStateful.js +4 -3
  33. package/lib/esm/components/webchatcontainerstateful/webchatcontroller/middlewares/renderingmiddlewares/timestamps/DeliveredTimestamp.js +4 -0
  34. package/lib/esm/contexts/common/LiveChatWidgetContextInitialState.js +18 -6
  35. package/lib/esm/firstresponselatency/Constants.js +6 -0
  36. package/lib/esm/firstresponselatency/FirstMessageTrackerFromBot.js +112 -0
  37. package/lib/esm/firstresponselatency/FirstResponseLatencyTracker.js +172 -0
  38. package/lib/esm/firstresponselatency/util.js +87 -0
  39. package/lib/esm/plugins/createChatTranscript.js +4 -4
  40. package/lib/esm/plugins/newMessageEventHandler.js +100 -86
  41. package/lib/types/common/telemetry/TelemetryConstants.d.ts +10 -1
  42. package/lib/types/components/postchatsurveypanestateful/common/isValidSurveyUrl.d.ts +2 -0
  43. package/lib/types/contexts/common/ILiveChatWidgetContext.d.ts +1 -1
  44. package/lib/types/firstresponselatency/Constants.d.ts +30 -0
  45. package/lib/types/firstresponselatency/FirstMessageTrackerFromBot.d.ts +1 -0
  46. package/lib/types/firstresponselatency/FirstResponseLatencyTracker.d.ts +22 -0
  47. package/lib/types/firstresponselatency/util.d.ts +7 -0
  48. package/package.json +13 -3
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createTrackingForFirstMessage = void 0;
7
+ var _TelemetryConstants = require("../common/telemetry/TelemetryConstants");
8
+ var _omnichannelChatComponents = require("@microsoft/omnichannel-chat-components");
9
+ var _TelemetryHelper = require("../common/telemetry/TelemetryHelper");
10
+ var _util = require("./util");
11
+ // This tracker is event based, this is since we are tracking events coming from different sources
12
+ // with different timeline, therefore this is a functional approach to track the events, instead of a class based approach
13
+ const createTrackingForFirstMessage = () => {
14
+ // Reset the tracking variables
15
+ let startTracking = false;
16
+ let stopTracking = false;
17
+ let startTime = 0;
18
+ let stopTime = 0;
19
+ let stopTrackingMessage;
20
+ let flag = false;
21
+ const isMessageFromValidSender = payload => {
22
+ var _payload$tags;
23
+ // agent scenario
24
+ if (payload !== null && payload !== void 0 && (_payload$tags = payload.tags) !== null && _payload$tags !== void 0 && _payload$tags.includes("public")) {
25
+ return false;
26
+ }
27
+ return true;
28
+ };
29
+ const widgetLoadListener = _omnichannelChatComponents.BroadcastService.getMessageByEventName(_TelemetryConstants.TelemetryEvent.WidgetLoadComplete).subscribe(() => {
30
+ if (startTracking) return;
31
+ startTracking = true;
32
+ startTime = new Date().getTime();
33
+ });
34
+ const newMessageListener = _omnichannelChatComponents.BroadcastService.getMessageByEventName(_TelemetryConstants.BroadcastEvent.NewMessageReceived).subscribe(message => {
35
+ const payload = message.payload;
36
+
37
+ // we only care for bot, so we need to check if the message is from the bot
38
+ // pending to add typing message indicator signal detection
39
+
40
+ if (isMessageFromValidSender(payload)) {
41
+ if (startTracking && !stopTracking) {
42
+ stopTime = new Date().getTime();
43
+ const elapsedTime = stopTime - startTime;
44
+ stopTracking = true;
45
+ stopTrackingMessage = (0, _util.createTrackingMessage)(payload, "botMessage");
46
+ notifyFMLTrackingCompleted();
47
+ _TelemetryHelper.TelemetryHelper.logActionEvent(_TelemetryConstants.LogLevel.INFO, {
48
+ Event: _TelemetryConstants.TelemetryEvent.BotFirstMessageLapTrack,
49
+ Description: "First Message from Bot latency tracking",
50
+ CustomProperties: {
51
+ elapsedTime,
52
+ widgetLoadedAt: startTime,
53
+ botMessage: stopTrackingMessage
54
+ }
55
+ });
56
+ }
57
+ }
58
+
59
+ // this track only first message, if coming from the bot or not
60
+ // the only difference is that it logs only those from bot
61
+ disconnectListener();
62
+ });
63
+ const notifyFMLTrackingCompleted = () => {
64
+ ackListener();
65
+ // Retry sending until flag is true, but do not block the main thread
66
+ const interval = setInterval(() => {
67
+ if (flag) {
68
+ clearInterval(interval);
69
+ } else {
70
+ _omnichannelChatComponents.BroadcastService.postMessage({
71
+ eventName: _TelemetryConstants.BroadcastEvent.FMLTrackingCompleted,
72
+ payload: null
73
+ });
74
+ }
75
+ }, 100);
76
+ };
77
+ const ackListener = () => {
78
+ const listen = _omnichannelChatComponents.BroadcastService.getMessageByEventName(_TelemetryConstants.BroadcastEvent.FMLTrackingCompletedAck).subscribe(() => {
79
+ flag = true;
80
+ listen.unsubscribe();
81
+ });
82
+ };
83
+
84
+ // 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
85
+ // No need to keep listerning for tracking, enforcing disconnection for the listners
86
+ const rehydrateListener = _omnichannelChatComponents.BroadcastService.getMessageByEventName(_TelemetryConstants.TelemetryEvent.RehydrateMessageReceived).subscribe(() => {
87
+ startTracking = false;
88
+ stopTracking = false;
89
+ disconnectListener();
90
+ });
91
+
92
+ // 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
93
+ // No need to keep listerning for tracking, enforcing disconnection for the listners
94
+ const historyListener = _omnichannelChatComponents.BroadcastService.getMessageByEventName(_TelemetryConstants.BroadcastEvent.HistoryMessageReceived).subscribe(() => {
95
+ startTracking = false;
96
+ stopTracking = false;
97
+ disconnectListener();
98
+ });
99
+ const offlineNetworkListener = _omnichannelChatComponents.BroadcastService.getMessageByEventName(_TelemetryConstants.TelemetryEvent.NetworkDisconnected).subscribe(() => {
100
+ startTracking = false;
101
+ stopTracking = false;
102
+ disconnectListener();
103
+ _TelemetryHelper.TelemetryHelper.logActionEvent(_TelemetryConstants.LogLevel.INFO, {
104
+ Event: _TelemetryConstants.TelemetryEvent.BotFirstMessageLapTrackError,
105
+ Description: "Tracker Stopped due to network disconnection"
106
+ });
107
+ });
108
+
109
+ // this is to ensure that we are not tracking messages that are not part of the current conversation
110
+ const disconnectListener = () => {
111
+ historyListener.unsubscribe();
112
+ rehydrateListener.unsubscribe();
113
+ newMessageListener.unsubscribe();
114
+ widgetLoadListener.unsubscribe();
115
+ offlineNetworkListener.unsubscribe();
116
+ };
117
+ };
118
+ exports.createTrackingForFirstMessage = createTrackingForFirstMessage;
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.FirstResponseLatencyTracker = void 0;
7
+ var _TelemetryConstants = require("../common/telemetry/TelemetryConstants");
8
+ var _omnichannelChatComponents = require("@microsoft/omnichannel-chat-components");
9
+ var _TelemetryHelper = require("../common/telemetry/TelemetryHelper");
10
+ 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; }
11
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
12
+ 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); }
13
+ class FirstResponseLatencyTracker {
14
+ constructor() {
15
+ _defineProperty(this, "isABotConversation", false);
16
+ _defineProperty(this, "isStarted", false);
17
+ _defineProperty(this, "isEnded", false);
18
+ _defineProperty(this, "startTrackingMessage", void 0);
19
+ _defineProperty(this, "stopTrackingMessage", void 0);
20
+ _defineProperty(this, "isReady", false);
21
+ _defineProperty(this, "offlineNetworkListener", _omnichannelChatComponents.BroadcastService.getMessageByEventName(_TelemetryConstants.TelemetryEvent.NetworkDisconnected).subscribe(() => {
22
+ this.isStarted = false;
23
+ this.isEnded = false;
24
+ _TelemetryHelper.TelemetryHelper.logActionEvent(_TelemetryConstants.LogLevel.INFO, {
25
+ Event: _TelemetryConstants.TelemetryEvent.MessageStopLapTrackError,
26
+ Description: "Tracker Stopped due to network disconnection"
27
+ });
28
+ }));
29
+ _defineProperty(this, "fmltrackingListener", _omnichannelChatComponents.BroadcastService.getMessageByEventName(_TelemetryConstants.BroadcastEvent.FMLTrackingCompleted).subscribe(() => {
30
+ this.isReady = true;
31
+ _omnichannelChatComponents.BroadcastService.postMessage({
32
+ eventName: _TelemetryConstants.BroadcastEvent.FMLTrackingCompletedAck,
33
+ payload: null
34
+ });
35
+ }));
36
+ // 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
37
+ // No need to keep listerning for tracking, enforcing disconnection for the listners
38
+ _defineProperty(this, "rehydrateListener", _omnichannelChatComponents.BroadcastService.getMessageByEventName(_TelemetryConstants.TelemetryEvent.RehydrateMessageReceived).subscribe(() => {
39
+ this.isReady = true;
40
+ }));
41
+ // 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
42
+ // No need to keep listerning for tracking, enforcing disconnection for the listners
43
+ _defineProperty(this, "historyListener", _omnichannelChatComponents.BroadcastService.getMessageByEventName(_TelemetryConstants.BroadcastEvent.HistoryMessageReceived).subscribe(() => {
44
+ this.isReady = true;
45
+ }));
46
+ // this is a workaround to ensure in reload we track effectively the messages
47
+ // we do have a mechanism in place to prevent log agent messages.
48
+ this.isABotConversation = true;
49
+ }
50
+ 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
+ startTracking(payload) {
65
+ if (!this.isReady) return;
66
+ // this prevents to initiate tracking for multiple incoming messages
67
+ if (this.isStarted) {
68
+ return;
69
+ }
70
+ // this is to ensure we track only messages where bot is engaged
71
+ if (!this.isABotConversation) {
72
+ return;
73
+ }
74
+ // control of states to prevent clashing of messages
75
+ this.isStarted = true;
76
+ this.isEnded = false;
77
+ // The idea of using types is to enrich telemetry data
78
+ this.startTrackingMessage = this.createTrackingMessage(payload, "userMessage");
79
+ }
80
+ handleAgentMessage(payload) {
81
+ var _payload$tags;
82
+ // this tag so far is only present in agent messages
83
+ if (payload !== null && payload !== void 0 && (_payload$tags = payload.tags) !== null && _payload$tags !== void 0 && _payload$tags.includes("public")) {
84
+ this.deregister();
85
+ }
86
+ }
87
+ stopTracking(payload) {
88
+ var _this$stopTrackingMes, _this$startTrackingMe;
89
+ // this prevents execution for multiple incoming messages from the bot.
90
+ if (this.isEnded && !this.isStarted) {
91
+ return;
92
+ }
93
+
94
+ // control of states to prevent clashing of messages
95
+ this.isEnded = true;
96
+ this.isStarted = false;
97
+
98
+ // The idea of using types is to enrich telemetry data
99
+ this.stopTrackingMessage = this.createTrackingMessage(payload, "botMessage");
100
+ // calculating elapsed time
101
+ 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);
102
+ _TelemetryHelper.TelemetryHelper.logActionEvent(_TelemetryConstants.LogLevel.INFO, {
103
+ Event: _TelemetryConstants.TelemetryEvent.MessageLapTrack,
104
+ Description: "First response latency tracking",
105
+ CustomProperties: {
106
+ elapsedTime,
107
+ userMessage: this.startTrackingMessage,
108
+ botMessage: this.stopTrackingMessage
109
+ }
110
+ });
111
+ }
112
+
113
+ // mechanism to ensure we track only allowed conversations
114
+ isMessageFromValidSender(payload) {
115
+ var _payload$tags2;
116
+ // agent scenario
117
+ if (payload !== null && payload !== void 0 && (_payload$tags2 = payload.tags) !== null && _payload$tags2 !== void 0 && _payload$tags2.includes("public")) {
118
+ this.handleAgentMessage(payload);
119
+ return false;
120
+ }
121
+ return true;
122
+ }
123
+ startClock(payload) {
124
+ try {
125
+ if (!payload || !payload.Id) {
126
+ throw new Error("Invalid payload");
127
+ }
128
+ this.startTracking(payload);
129
+ } catch (e) {
130
+ _TelemetryHelper.TelemetryHelper.logActionEvent(_TelemetryConstants.LogLevel.ERROR, {
131
+ Event: _TelemetryConstants.TelemetryEvent.MessageStartLapTrackError,
132
+ Description: "Error while starting the clock",
133
+ ExceptionDetails: e,
134
+ CustomProperties: {
135
+ payload: payload
136
+ }
137
+ });
138
+ }
139
+ }
140
+ stopClock(payload) {
141
+ try {
142
+ if (!payload || !payload.Id) {
143
+ throw new Error("Invalid payload");
144
+ }
145
+ if (!this.isMessageFromValidSender(payload)) return;
146
+ if (this.isABotConversation && this.isStarted) {
147
+ this.stopTracking(payload);
148
+ }
149
+ } catch (e) {
150
+ console.error("FRL : error while trying to stop the tracker", e);
151
+ _TelemetryHelper.TelemetryHelper.logActionEvent(_TelemetryConstants.LogLevel.ERROR, {
152
+ Event: _TelemetryConstants.TelemetryEvent.MessageStopLapTrackError,
153
+ Description: "Error while stopping the clock",
154
+ ExceptionDetails: e,
155
+ CustomProperties: {
156
+ payload: payload
157
+ }
158
+ });
159
+ //reset state
160
+ this.startTrackingMessage = undefined;
161
+ this.stopTrackingMessage = undefined;
162
+ this.isStarted = false;
163
+ this.isEnded = false;
164
+ }
165
+ }
166
+ deregister() {
167
+ // Reset State
168
+ this.isABotConversation = false;
169
+ this.isStarted = false;
170
+ this.isEnded = false;
171
+ this.startTrackingMessage = undefined;
172
+ this.stopTrackingMessage = undefined;
173
+ this.offlineNetworkListener.unsubscribe();
174
+ this.fmltrackingListener.unsubscribe();
175
+ this.rehydrateListener.unsubscribe();
176
+ this.historyListener.unsubscribe();
177
+ }
178
+ }
179
+ exports.FirstResponseLatencyTracker = FirstResponseLatencyTracker;
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.polyfillMessagePayloadForEvent = exports.isHistoryMessage = exports.getScenarioType = exports.createTrackingMessage = exports.buildMessagePayload = void 0;
7
+ var _Constants = require("./Constants");
8
+ var _Constants2 = require("../common/Constants");
9
+ const isHistoryMessage = (activity, startTime) => {
10
+ try {
11
+ if ((activity === null || activity === void 0 ? void 0 : activity.type) === _Constants2.Constants.message) {
12
+ var _activity$channelData, _activity$channelData2;
13
+ // this is an old piece of code, probably no longer relevant
14
+ 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(_Constants2.Constants.historyMessageTag)) return true;
15
+
16
+ // Id is an epoch time in milliseconds , in utc format, for some reason is in a string format
17
+ if (activity !== null && activity !== void 0 && activity.id) {
18
+ /// activity.id is an string that contains epoch time in milliseconds
19
+ const activityId = parseInt(activity === null || activity === void 0 ? void 0 : activity.id);
20
+
21
+ // if the activity id is not a number, we default to new message
22
+ if (isNaN(activityId)) {
23
+ return false;
24
+ }
25
+
26
+ // if the activity id is less than the start time, it means that the message is a history message
27
+ if (activityId < startTime) {
28
+ return true;
29
+ }
30
+ }
31
+ // anything else will be considered a new message
32
+ return false;
33
+ }
34
+ } catch (e) {
35
+ // if there is an error in parsing the activity id, we will consider it a new message
36
+ console.error("Error in parsing activity id: ", e);
37
+ }
38
+ return false;
39
+ };
40
+ exports.isHistoryMessage = isHistoryMessage;
41
+ const buildMessagePayload = (activity, userId) => {
42
+ var _text, _text2, _activity$channelData3, _activity$from;
43
+ return {
44
+ // To identify hidden contents vs empty content
45
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
+ 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)*` : "",
47
+ type: activity === null || activity === void 0 ? void 0 : activity.type,
48
+ timestamp: activity === null || activity === void 0 ? void 0 : activity.timestamp,
49
+ userId: userId,
50
+ tags: activity === null || activity === void 0 ? void 0 : (_activity$channelData3 = activity.channelData) === null || _activity$channelData3 === void 0 ? void 0 : _activity$channelData3.tags,
51
+ messageType: "",
52
+ Id: activity === null || activity === void 0 ? void 0 : activity.id,
53
+ role: activity === null || activity === void 0 ? void 0 : (_activity$from = activity.from) === null || _activity$from === void 0 ? void 0 : _activity$from.role,
54
+ isChatComplete: false
55
+ };
56
+ };
57
+ exports.buildMessagePayload = buildMessagePayload;
58
+ const polyfillMessagePayloadForEvent = (activity, payload, conversationId) => {
59
+ var _activity$conversatio, _attachments, _activity$from2;
60
+ return {
61
+ ...payload,
62
+ channelData: activity === null || activity === void 0 ? void 0 : activity.channelData,
63
+ chatId: activity === null || activity === void 0 ? void 0 : (_activity$conversatio = activity.conversation) === null || _activity$conversatio === void 0 ? void 0 : _activity$conversatio.id,
64
+ conversationId: conversationId,
65
+ Id: activity === null || activity === void 0 ? void 0 : activity.id,
66
+ isChatComplete: false,
67
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
68
+ text: activity === null || activity === void 0 ? void 0 : activity.text,
69
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
70
+ 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 : [],
71
+ role: activity === null || activity === void 0 ? void 0 : (_activity$from2 = activity.from) === null || _activity$from2 === void 0 ? void 0 : _activity$from2.role
72
+ };
73
+ };
74
+ exports.polyfillMessagePayloadForEvent = polyfillMessagePayloadForEvent;
75
+ const getScenarioType = activity => {
76
+ var _activity$from3, _activity$channelData4, _activity$channelData5;
77
+ if ((activity === null || activity === void 0 ? void 0 : (_activity$from3 = activity.from) === null || _activity$from3 === void 0 ? void 0 : _activity$from3.role) === _Constants2.Constants.userMessageTag) {
78
+ return _Constants.ScenarioType.UserSendMessageStrategy;
79
+ }
80
+ 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(_Constants2.Constants.systemMessageTag)) {
81
+ return _Constants.ScenarioType.SystemMessageStrategy;
82
+ }
83
+ return _Constants.ScenarioType.ReceivedMessageStrategy;
84
+ };
85
+ exports.getScenarioType = getScenarioType;
86
+ const createTrackingMessage = (payload, type) => {
87
+ return {
88
+ Id: payload.Id,
89
+ role: payload.role,
90
+ timestamp: payload === null || payload === void 0 ? void 0 : payload.timestamp,
91
+ tags: payload.tags,
92
+ messageType: payload.messageType,
93
+ text: payload.text,
94
+ type: type,
95
+ checkTime: new Date().getTime()
96
+ };
97
+ };
98
+ exports.createTrackingMessage = createTrackingMessage;
@@ -173,7 +173,7 @@ class TranscriptHTMLBuilder {
173
173
  <script>
174
174
  class Translator {
175
175
  static convertTranscriptMessageToActivity(message) {
176
- const {created, OriginalMessageId, id, isControlMessage, content, tags, from, attachments, amsMetadata, amsReferences} = message;
176
+ const {created, OriginalMessageId, id, isControlMessage, content, tags, from, attachments, amsMetadata, amsReferences, amsreferences} = message;
177
177
 
178
178
  //it's required to convert the id to a number, otherwise the webchat will not render the messages in the correct order
179
179
  // if the OrginalMessageId is not present, we can use the id as the sequence id, which is always present.
@@ -220,7 +220,7 @@ class TranscriptHTMLBuilder {
220
220
  }
221
221
 
222
222
  // Attachments
223
- if (amsReferences && amsMetadata) {
223
+ if ((amsReferences || amsreferences) && amsMetadata) {
224
224
  const metadata = JSON.parse(amsMetadata);
225
225
  const { fileName } = metadata[0];
226
226
  const text = \`${this.attachmentMessage}\${fileName}\`;
@@ -244,7 +244,7 @@ class TranscriptHTMLBuilder {
244
244
  // Message
245
245
  if (content) {
246
246
  // Adaptive card formatting
247
- if (content.includes('"text"') && content.includes('"attachments"') && content.includes('"suggestedActions"')) {
247
+ if (content.includes('"attachments"') || content.includes('"suggestedActions"')) {
248
248
  try {
249
249
  const partialActivity = JSON.parse(content);
250
250
  return {
@@ -683,7 +683,7 @@ const createChatTranscript = async function (transcript, facadeChatSDK) {
683
683
  messages = await Promise.all(transcriptMessages.map(async message => {
684
684
  // eslint-disable-line @typescript-eslint/no-explicit-any
685
685
  const {
686
- amsReferences,
686
+ amsReferences = message.amsreferences,
687
687
  amsMetadata
688
688
  } = message;
689
689
  if (amsReferences && amsMetadata) {
@@ -5,102 +5,116 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.createOnNewAdapterActivityHandler = void 0;
7
7
  var _TelemetryConstants = require("../common/telemetry/TelemetryConstants");
8
+ var _Constants = require("../firstresponselatency/Constants");
9
+ var _util = require("../firstresponselatency/util");
8
10
  var _omnichannelChatComponents = require("@microsoft/omnichannel-chat-components");
9
- var _Constants = require("../common/Constants");
11
+ var _Constants2 = require("../common/Constants");
12
+ var _FirstResponseLatencyTracker = require("../firstresponselatency/FirstResponseLatencyTracker");
10
13
  var _TelemetryHelper = require("../common/telemetry/TelemetryHelper");
11
14
  var _TelemetryManager = require("../common/telemetry/TelemetryManager");
12
15
  const createOnNewAdapterActivityHandler = (chatId, userId) => {
16
+ // Hooking the message tracker in the listener, a bit invasive but easier to control.
17
+ const firstResponseLatencyTracker = new _FirstResponseLatencyTracker.FirstResponseLatencyTracker();
18
+ // epoch time in utc for when start to listen.
19
+ // We dont longer have a mechanism to know if a message is history or new, so any message older than the time we start listening will be considered a history message.
20
+ // this is a workaround for the fact that we dont have a way to identify if a message is history or new, and it will provide consistency across different scenarios
21
+ const startTime = new Date().getTime();
22
+ let isHistoryMessageReceivedEventRaised = false;
13
23
  const onNewAdapterActivityHandler = activity => {
14
- var _activity$channelData, _activity$channelData2, _activity$channelData3;
15
- const isActivityMessage = (activity === null || activity === void 0 ? void 0 : activity.type) === _Constants.Constants.message;
16
- const isHistoryMessage = isActivityMessage && ((activity === null || activity === void 0 ? void 0 : (_activity$channelData = activity.channelData) === null || _activity$channelData === void 0 ? void 0 : (_activity$channelData2 = _activity$channelData.tags) === null || _activity$channelData2 === void 0 ? void 0 : _activity$channelData2.includes(_Constants.Constants.historyMessageTag)) || (activity === null || activity === void 0 ? void 0 : (_activity$channelData3 = activity.channelData) === null || _activity$channelData3 === void 0 ? void 0 : _activity$channelData3.fromList));
17
- raiseMessageEvent(activity, isHistoryMessage);
24
+ raiseMessageEvent(activity);
18
25
  };
19
- let isHistoryMessageReceivedEventRasied = false;
20
- const raiseMessageEvent = (activity, isHistoryMessage) => {
26
+ const userSendMessageStrategy = activity => {
27
+ var _TelemetryManager$Int;
28
+ const payload = (0, _util.buildMessagePayload)(activity, userId);
21
29
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
- const polyfillMessagePayloadForEvent = payload => {
23
- var _activity$conversatio, _TelemetryManager$Int, _attachments;
24
- return {
25
- ...payload,
26
- channelData: activity === null || activity === void 0 ? void 0 : activity.channelData,
27
- chatId: activity === null || activity === void 0 ? void 0 : (_activity$conversatio = activity.conversation) === null || _activity$conversatio === void 0 ? void 0 : _activity$conversatio.id,
28
- conversationId: (_TelemetryManager$Int = _TelemetryManager.TelemetryManager.InternalTelemetryData) === null || _TelemetryManager$Int === void 0 ? void 0 : _TelemetryManager$Int.conversationId,
29
- id: activity === null || activity === void 0 ? void 0 : activity.id,
30
- isChatComplete: false,
31
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
32
- text: activity === null || activity === void 0 ? void 0 : activity.text,
33
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
- 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 : []
35
- };
30
+ payload.messageType = _Constants2.Constants.userMessageTag;
31
+ const newMessageSentEvent = {
32
+ eventName: _TelemetryConstants.BroadcastEvent.NewMessageSent,
33
+ payload: (0, _util.polyfillMessagePayloadForEvent)(activity, payload, (_TelemetryManager$Int = _TelemetryManager.TelemetryManager.InternalTelemetryData) === null || _TelemetryManager$Int === void 0 ? void 0 : _TelemetryManager$Int.conversationId)
36
34
  };
37
- if ((activity === null || activity === void 0 ? void 0 : activity.type) === _Constants.Constants.message) {
38
- var _text, _text2, _activity$channelData4, _activity$from;
39
- const payload = {
40
- // To identify hidden contents vs empty content
41
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
- 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)*` : "",
43
- type: activity === null || activity === void 0 ? void 0 : activity.type,
44
- timestamp: activity === null || activity === void 0 ? void 0 : activity.timestamp,
45
- userId: userId,
46
- tags: activity === null || activity === void 0 ? void 0 : (_activity$channelData4 = activity.channelData) === null || _activity$channelData4 === void 0 ? void 0 : _activity$channelData4.tags,
47
- messageType: ""
48
- };
49
- if ((activity === null || activity === void 0 ? void 0 : (_activity$from = activity.from) === null || _activity$from === void 0 ? void 0 : _activity$from.role) === _Constants.Constants.userMessageTag) {
50
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
51
- payload.messageType = _Constants.Constants.userMessageTag;
52
- const newMessageSentEvent = {
53
- eventName: _TelemetryConstants.BroadcastEvent.NewMessageSent,
54
- payload: polyfillMessagePayloadForEvent(payload)
55
- };
56
- _omnichannelChatComponents.BroadcastService.postMessage(newMessageSentEvent);
57
- _TelemetryHelper.TelemetryHelper.logActionEvent(_TelemetryConstants.LogLevel.INFO, {
58
- Event: _TelemetryConstants.TelemetryEvent.MessageSent,
59
- Description: "New message sent"
60
- });
61
- } else {
62
- var _activity$channelData5, _activity$channelData6;
63
- if (activity !== null && activity !== void 0 && (_activity$channelData5 = activity.channelData) !== null && _activity$channelData5 !== void 0 && (_activity$channelData6 = _activity$channelData5.tags) !== null && _activity$channelData6 !== void 0 && _activity$channelData6.includes(_Constants.Constants.systemMessageTag)) {
64
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
65
- payload.messageType = _Constants.Constants.systemMessageTag;
66
- _TelemetryHelper.TelemetryHelper.logActionEvent(_TelemetryConstants.LogLevel.INFO, {
67
- Event: _TelemetryConstants.TelemetryEvent.SystemMessageReceived,
68
- Description: "System message received"
69
- });
70
- } else {
71
- var _activity$channelData7, _activity$channelData8, _activity$channelData9;
72
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
73
- const messageHasNoText = !(activity !== null && activity !== void 0 && activity.text);
74
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
75
- const messageHasNoTags = !(activity !== null && activity !== void 0 && activity.channelData) || !(activity !== null && activity !== void 0 && (_activity$channelData7 = activity.channelData) !== null && _activity$channelData7 !== void 0 && _activity$channelData7.tags) || (activity === null || activity === void 0 ? void 0 : (_activity$channelData8 = activity.channelData) === null || _activity$channelData8 === void 0 ? void 0 : (_activity$channelData9 = _activity$channelData8.tags) === null || _activity$channelData9 === void 0 ? void 0 : _activity$channelData9.length) === 0;
76
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
77
- const messageHasNoAttachments = !(activity !== null && activity !== void 0 && activity.attachments) || (activity === null || activity === void 0 ? void 0 : activity.attachments.length) === 0;
78
- if (messageHasNoTags && messageHasNoText && messageHasNoAttachments) {
79
- return;
80
- }
81
- payload.messageType = _Constants.Constants.userMessageTag;
82
- }
83
- const newMessageReceivedEvent = {
84
- eventName: isHistoryMessage ? _TelemetryConstants.BroadcastEvent.HistoryMessageReceived : _TelemetryConstants.BroadcastEvent.NewMessageReceived,
85
- payload: polyfillMessagePayloadForEvent(payload)
86
- };
87
- _omnichannelChatComponents.BroadcastService.postMessage(newMessageReceivedEvent);
88
- if (!isHistoryMessage) {
89
- _TelemetryHelper.TelemetryHelper.logActionEvent(_TelemetryConstants.LogLevel.INFO, {
90
- Event: _TelemetryConstants.TelemetryEvent.MessageReceived,
91
- Description: "New message received",
92
- CustomProperties: payload
93
- });
94
- } else {
95
- if (!isHistoryMessageReceivedEventRasied) {
96
- isHistoryMessageReceivedEventRasied = true;
97
- _TelemetryHelper.TelemetryHelper.logActionEvent(_TelemetryConstants.LogLevel.INFO, {
98
- Event: _TelemetryConstants.TelemetryEvent.RehydrateMessageReceived,
99
- Description: "History message received",
100
- CustomProperties: payload
101
- });
102
- }
103
- }
35
+ _omnichannelChatComponents.BroadcastService.postMessage(newMessageSentEvent);
36
+ if (!(0, _util.isHistoryMessage)(activity, startTime)) {
37
+ firstResponseLatencyTracker.startClock(payload);
38
+ }
39
+ _TelemetryHelper.TelemetryHelper.logActionEvent(_TelemetryConstants.LogLevel.INFO, {
40
+ Event: _TelemetryConstants.TelemetryEvent.MessageSent,
41
+ Description: "New message sent"
42
+ });
43
+ };
44
+ const systemMessageStrategy = activity => {
45
+ const payload = (0, _util.buildMessagePayload)(activity, userId);
46
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
47
+ payload.messageType = _Constants2.Constants.systemMessageTag;
48
+ _TelemetryHelper.TelemetryHelper.logActionEvent(_TelemetryConstants.LogLevel.INFO, {
49
+ Event: _TelemetryConstants.TelemetryEvent.SystemMessageReceived,
50
+ Description: "System message received"
51
+ });
52
+ };
53
+ const historyMessageStrategy = payload => {
54
+ const newMessageReceivedEvent = {
55
+ eventName: _TelemetryConstants.BroadcastEvent.HistoryMessageReceived,
56
+ payload: payload
57
+ };
58
+ _omnichannelChatComponents.BroadcastService.postMessage(newMessageReceivedEvent);
59
+ if (!isHistoryMessageReceivedEventRaised) {
60
+ // this is needed for reload scenarios, it helps to identify the last message received before the reload
61
+ isHistoryMessageReceivedEventRaised = true;
62
+ _TelemetryHelper.TelemetryHelper.logActionEvent(_TelemetryConstants.LogLevel.INFO, {
63
+ Event: _TelemetryConstants.TelemetryEvent.RehydrateMessageReceived,
64
+ Description: "History message received",
65
+ CustomProperties: payload
66
+ });
67
+ }
68
+ };
69
+ const isValidMessage = activity => {
70
+ var _activity$channelData, _activity$channelData2, _activity$channelData3;
71
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
72
+ const messageHasNoText = !(activity !== null && activity !== void 0 && activity.text);
73
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
+ const messageHasNoTags = !(activity !== null && activity !== void 0 && activity.channelData) || !(activity !== null && activity !== void 0 && (_activity$channelData = activity.channelData) !== null && _activity$channelData !== void 0 && _activity$channelData.tags) || (activity === null || activity === void 0 ? void 0 : (_activity$channelData2 = activity.channelData) === null || _activity$channelData2 === void 0 ? void 0 : (_activity$channelData3 = _activity$channelData2.tags) === null || _activity$channelData3 === void 0 ? void 0 : _activity$channelData3.length) === 0;
75
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
76
+ const messageHasNoAttachments = !(activity !== null && activity !== void 0 && activity.attachments) || (activity === null || activity === void 0 ? void 0 : activity.attachments.length) === 0;
77
+ if (messageHasNoTags && messageHasNoText && messageHasNoAttachments) {
78
+ return false;
79
+ }
80
+ return true;
81
+ };
82
+ const receivedMessageStrategy = activity => {
83
+ var _TelemetryManager$Int3;
84
+ if (!isValidMessage(activity)) return;
85
+ const isHistoryMessageReceived = (0, _util.isHistoryMessage)(activity, startTime);
86
+ const payload = (0, _util.buildMessagePayload)(activity, userId);
87
+ payload.messageType = _Constants2.Constants.userMessageTag;
88
+ if (isHistoryMessageReceived) {
89
+ var _TelemetryManager$Int2;
90
+ historyMessageStrategy((0, _util.polyfillMessagePayloadForEvent)(activity, payload, (_TelemetryManager$Int2 = _TelemetryManager.TelemetryManager.InternalTelemetryData) === null || _TelemetryManager$Int2 === void 0 ? void 0 : _TelemetryManager$Int2.conversationId));
91
+ return;
92
+ }
93
+ firstResponseLatencyTracker.stopClock(payload);
94
+ const newMessageReceivedEvent = {
95
+ eventName: _TelemetryConstants.BroadcastEvent.NewMessageReceived,
96
+ payload: (0, _util.polyfillMessagePayloadForEvent)(activity, payload, (_TelemetryManager$Int3 = _TelemetryManager.TelemetryManager.InternalTelemetryData) === null || _TelemetryManager$Int3 === void 0 ? void 0 : _TelemetryManager$Int3.conversationId)
97
+ };
98
+ _omnichannelChatComponents.BroadcastService.postMessage(newMessageReceivedEvent);
99
+ _TelemetryHelper.TelemetryHelper.logActionEvent(_TelemetryConstants.LogLevel.INFO, {
100
+ Event: _TelemetryConstants.TelemetryEvent.MessageReceived,
101
+ Description: "New message received",
102
+ CustomProperties: payload
103
+ });
104
+ };
105
+ const raiseMessageEvent = activity => {
106
+ if ((activity === null || activity === void 0 ? void 0 : activity.type) === _Constants2.Constants.message) {
107
+ const scenarioType = (0, _util.getScenarioType)(activity);
108
+ switch (scenarioType) {
109
+ case _Constants.ScenarioType.UserSendMessageStrategy:
110
+ userSendMessageStrategy(activity);
111
+ break;
112
+ case _Constants.ScenarioType.SystemMessageStrategy:
113
+ systemMessageStrategy(activity);
114
+ break;
115
+ case _Constants.ScenarioType.ReceivedMessageStrategy:
116
+ receivedMessageStrategy(activity);
117
+ break;
104
118
  }
105
119
  }
106
120
  };