@microsoft/omnichannel-chat-sdk 1.11.7 → 1.11.8-main.255365e

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.
@@ -85,6 +85,7 @@ declare class OmnichannelChatSDK {
85
85
  private debugACS;
86
86
  private detailedDebugEnabled;
87
87
  private regexCompiledForDataMasking;
88
+ private isEndingChat;
88
89
  constructor(omnichannelConfig: OmnichannelConfig, chatSDKConfig?: ChatSDKConfig);
89
90
  /**
90
91
  * Executes an operation with mutual exclusion to prevent race conditions
@@ -129,8 +130,28 @@ declare class OmnichannelChatSDK {
129
130
  startChat(optionalParams?: StartChatOptionalParams): Promise<void>;
130
131
  private internalStartChat;
131
132
  private closeChat;
133
+ /**
134
+ * Ends the chat by closing the session and disconnecting from the conversation.
135
+ *
136
+ * On React Native, automatically waits for conversational survey completion if enabled.
137
+ * On Web, disconnects immediately as the widget handles surveys independently.
138
+ *
139
+ * @param endChatOptionalParams - Optional parameters
140
+ * @param endChatOptionalParams.isSessionEnded - Skip survey wait if session already ended
141
+ * @example
142
+ * await chatSDK.endChat();
143
+ * await chatSDK.endChat({ isSessionEnded: true });
144
+ */
132
145
  endChat(endChatOptionalParams?: EndChatOptionalParams): Promise<void>;
133
146
  private internalEndChat;
147
+ /**
148
+ * Waits for a message with specific tags. Used for conversational survey detection.
149
+ * Automatically unregisters the listener after the promise resolves to prevent memory leaks.
150
+ * Includes timeout to prevent indefinite hanging if messages never arrive.
151
+ */
152
+ private waitForMessageTags;
153
+ private waitForSurveyStart;
154
+ private waitForConversationalSurveyEnd;
134
155
  getCurrentLiveChatContext(): Promise<GetCurrentLiveChatContextResponse>;
135
156
  getConversationDetails(optionalParams?: GetConversationDetailsOptionalParams): Promise<LiveWorkItemDetails>;
136
157
  /**
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
3
2
  var __assign = (this && this.__assign) || function () {
4
3
  __assign = Object.assign || function(t) {
5
4
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -150,6 +149,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
150
149
  this.debugACS = false;
151
150
  this.detailedDebugEnabled = false;
152
151
  this.regexCompiledForDataMasking = [];
152
+ this.isEndingChat = false;
153
153
  this.populateInitChatOptionalParam = function (requestOptionalParams, optionalParams, telemetryEvent) {
154
154
  requestOptionalParams.initContext.locale = (0, locale_1.getLocaleStringFromId)(_this.localeId);
155
155
  if (optionalParams === null || optionalParams === void 0 ? void 0 : optionalParams.customContext) {
@@ -850,6 +850,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
850
850
  return __generator(this, function (_o) {
851
851
  switch (_o.label) {
852
852
  case 0:
853
+ this.isEndingChat = false;
853
854
  this.scenarioMarker.startScenario(TelemetryEvent_1.default.StartChat, {
854
855
  RequestId: this.requestId
855
856
  });
@@ -1277,6 +1278,18 @@ var OmnichannelChatSDK = /** @class */ (function () {
1277
1278
  });
1278
1279
  });
1279
1280
  };
1281
+ /**
1282
+ * Ends the chat by closing the session and disconnecting from the conversation.
1283
+ *
1284
+ * On React Native, automatically waits for conversational survey completion if enabled.
1285
+ * On Web, disconnects immediately as the widget handles surveys independently.
1286
+ *
1287
+ * @param endChatOptionalParams - Optional parameters
1288
+ * @param endChatOptionalParams.isSessionEnded - Skip survey wait if session already ended
1289
+ * @example
1290
+ * await chatSDK.endChat();
1291
+ * await chatSDK.endChat({ isSessionEnded: true });
1292
+ */
1280
1293
  OmnichannelChatSDK.prototype.endChat = function () {
1281
1294
  return __awaiter(this, arguments, void 0, function (endChatOptionalParams) {
1282
1295
  var _this = this;
@@ -1288,11 +1301,11 @@ var OmnichannelChatSDK = /** @class */ (function () {
1288
1301
  };
1289
1302
  OmnichannelChatSDK.prototype.internalEndChat = function () {
1290
1303
  return __awaiter(this, arguments, void 0, function (endChatOptionalParams) {
1291
- var cleanupMetadata, error_15, telemetryData;
1292
- var _a;
1304
+ var cleanupMetadata, isReactNative, isConversationalSurveyEnabled, shouldWaitForSurvey, telemetryData, surveyStarted, error_15, error_16, telemetryData;
1305
+ var _a, _b, _c;
1293
1306
  if (endChatOptionalParams === void 0) { endChatOptionalParams = {}; }
1294
- return __generator(this, function (_b) {
1295
- switch (_b.label) {
1307
+ return __generator(this, function (_d) {
1308
+ switch (_d.label) {
1296
1309
  case 0:
1297
1310
  cleanupMetadata = {
1298
1311
  RequestId: this.requestId,
@@ -1302,18 +1315,54 @@ var OmnichannelChatSDK = /** @class */ (function () {
1302
1315
  if (!this.isInitialized) {
1303
1316
  exceptionThrowers_1.default.throwUninitializedChatSDK(this.scenarioMarker, TelemetryEvent_1.default.EndChat);
1304
1317
  }
1305
- _b.label = 1;
1318
+ this.isEndingChat = true;
1319
+ _d.label = 1;
1306
1320
  case 1:
1307
- _b.trys.push([1, 6, , 7]);
1308
- _b.label = 2;
1309
- case 2:
1310
- _b.trys.push([2, , 4, 5]);
1321
+ _d.trys.push([1, 9, 10, 11]);
1311
1322
  return [4 /*yield*/, this.closeChat(endChatOptionalParams)];
1323
+ case 2:
1324
+ _d.sent();
1325
+ isReactNative = platform_1.default.isReactNative();
1326
+ isConversationalSurveyEnabled = ((_b = (_a = this.liveChatConfig.LiveWSAndLiveChatEngJoin) === null || _a === void 0 ? void 0 : _a.msdyn_isConversationalPostChatSurveyEnabled) === null || _b === void 0 ? void 0 : _b.toString().toLowerCase()) === 'true';
1327
+ shouldWaitForSurvey = isReactNative && isConversationalSurveyEnabled && !endChatOptionalParams.isSessionEnded;
1328
+ telemetryData = __assign(__assign({}, cleanupMetadata), { IsReactNative: isReactNative, SurveyEnabled: isConversationalSurveyEnabled, IsSessionEnded: endChatOptionalParams.isSessionEnded || false, WaitingForSurvey: shouldWaitForSurvey });
1329
+ if (!shouldWaitForSurvey) return [3 /*break*/, 8];
1330
+ this.scenarioMarker.startScenario(TelemetryEvent_1.default.WaitForConversationalSurvey, telemetryData);
1331
+ _d.label = 3;
1312
1332
  case 3:
1313
- _b.sent();
1314
- return [3 /*break*/, 5];
1333
+ _d.trys.push([3, 7, , 8]);
1334
+ return [4 /*yield*/, this.waitForSurveyStart()];
1315
1335
  case 4:
1316
- (_a = this.conversation) === null || _a === void 0 ? void 0 : _a.disconnect();
1336
+ surveyStarted = _d.sent();
1337
+ if (!surveyStarted) return [3 /*break*/, 6];
1338
+ return [4 /*yield*/, this.waitForConversationalSurveyEnd()];
1339
+ case 5:
1340
+ _d.sent();
1341
+ _d.label = 6;
1342
+ case 6:
1343
+ this.scenarioMarker.completeScenario(TelemetryEvent_1.default.WaitForConversationalSurvey, telemetryData);
1344
+ return [3 /*break*/, 8];
1345
+ case 7:
1346
+ error_15 = _d.sent();
1347
+ return [3 /*break*/, 8];
1348
+ case 8:
1349
+ this.scenarioMarker.completeScenario(TelemetryEvent_1.default.EndChat, cleanupMetadata);
1350
+ return [3 /*break*/, 11];
1351
+ case 9:
1352
+ error_16 = _d.sent();
1353
+ telemetryData = {
1354
+ RequestId: this.requestId,
1355
+ ChatId: this.chatToken.chatId
1356
+ };
1357
+ if (error_16 instanceof ChatSDKError_1.ChatSDKError) {
1358
+ exceptionThrowers_1.default.throwConversationClosureFailure(new Error(JSON.stringify(error_16.exceptionDetails)), this.scenarioMarker, TelemetryEvent_1.default.EndChat, telemetryData);
1359
+ }
1360
+ exceptionThrowers_1.default.throwConversationClosureFailure(error_16, this.scenarioMarker, TelemetryEvent_1.default.EndChat, telemetryData);
1361
+ return [3 /*break*/, 11];
1362
+ case 10:
1363
+ // Cleanup always runs, regardless of success or failure
1364
+ // This ensures resources are released even if closeChat or survey waiting throws
1365
+ (_c = this.conversation) === null || _c === void 0 ? void 0 : _c.disconnect();
1317
1366
  this.conversation = null;
1318
1367
  this.requestId = (0, ocsdk_1.uuidv4)();
1319
1368
  this.chatToken = {};
@@ -1333,22 +1382,104 @@ var OmnichannelChatSDK = /** @class */ (function () {
1333
1382
  clearInterval(this.refreshTokenTimer);
1334
1383
  this.refreshTokenTimer = null;
1335
1384
  }
1385
+ this.isEndingChat = false;
1336
1386
  return [7 /*endfinally*/];
1337
- case 5:
1338
- this.scenarioMarker.completeScenario(TelemetryEvent_1.default.EndChat, cleanupMetadata);
1339
- return [3 /*break*/, 7];
1340
- case 6:
1341
- error_15 = _b.sent();
1342
- telemetryData = {
1343
- RequestId: this.requestId,
1344
- ChatId: this.chatToken.chatId
1345
- };
1346
- if (error_15 instanceof ChatSDKError_1.ChatSDKError) {
1347
- exceptionThrowers_1.default.throwConversationClosureFailure(new Error(JSON.stringify(error_15.exceptionDetails)), this.scenarioMarker, TelemetryEvent_1.default.EndChat, telemetryData);
1348
- }
1349
- exceptionThrowers_1.default.throwConversationClosureFailure(error_15, this.scenarioMarker, TelemetryEvent_1.default.EndChat, telemetryData);
1350
- return [3 /*break*/, 7];
1351
- case 7: return [2 /*return*/];
1387
+ case 11: return [2 /*return*/];
1388
+ }
1389
+ });
1390
+ });
1391
+ };
1392
+ /**
1393
+ * Waits for a message with specific tags. Used for conversational survey detection.
1394
+ * Automatically unregisters the listener after the promise resolves to prevent memory leaks.
1395
+ * Includes timeout to prevent indefinite hanging if messages never arrive.
1396
+ */
1397
+ OmnichannelChatSDK.prototype.waitForMessageTags = function (requiredTags, timeoutMs) {
1398
+ var _this = this;
1399
+ if (timeoutMs === void 0) { timeoutMs = 60000; }
1400
+ return new Promise(function (resolve, reject) {
1401
+ var resolved = false;
1402
+ var timeoutHandle = null;
1403
+ var checkForTags = function (event) {
1404
+ var _a;
1405
+ if (resolved)
1406
+ return;
1407
+ try {
1408
+ var tagsString = ((_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.tags) || '';
1409
+ var tags_1 = tagsString.replace(/\"/g, "").split(",").filter(function (tag) { return tag.length > 0; }); // eslint-disable-line no-useless-escape
1410
+ var hasTags = requiredTags.every(function (tag) { return tags_1.includes(tag); });
1411
+ if (hasTags) {
1412
+ resolved = true;
1413
+ cleanup();
1414
+ resolve(true);
1415
+ }
1416
+ }
1417
+ catch (error) {
1418
+ // Silently continue listening on message processing errors
1419
+ }
1420
+ };
1421
+ var cleanup = function (clearTimer) {
1422
+ if (clearTimer === void 0) { clearTimer = true; }
1423
+ // Clear timeout if still active
1424
+ if (clearTimer && timeoutHandle) {
1425
+ clearTimeout(timeoutHandle);
1426
+ timeoutHandle = null;
1427
+ }
1428
+ // Remove listener to prevent memory leak
1429
+ if (_this.conversation && _this.liveChatVersion === LiveChatVersion_1.default.V2) {
1430
+ var acsConversation = _this.conversation;
1431
+ acsConversation.removeListener("chatMessageReceived", checkForTags);
1432
+ acsConversation.removeListener("chatMessageEdited", checkForTags);
1433
+ }
1434
+ };
1435
+ // Set timeout to prevent indefinite hang
1436
+ timeoutHandle = setTimeout(function () {
1437
+ var _a, _b;
1438
+ if (!resolved) {
1439
+ resolved = true;
1440
+ cleanup(false); // Don't clear timeout since we're in the timeout handler
1441
+ // Log telemetry for timeout
1442
+ (_a = _this.scenarioMarker) === null || _a === void 0 ? void 0 : _a.failScenario(TelemetryEvent_1.default.WaitForConversationalSurvey, {
1443
+ RequestId: _this.requestId,
1444
+ ChatId: (_b = _this.chatToken) === null || _b === void 0 ? void 0 : _b.chatId,
1445
+ Reason: 'Timeout',
1446
+ ExpectedTags: requiredTags.join(','),
1447
+ TimeoutMs: timeoutMs
1448
+ });
1449
+ reject(new Error("Timeout waiting for message with tags: ".concat(requiredTags.join(', '))));
1450
+ }
1451
+ }, timeoutMs);
1452
+ try {
1453
+ if (_this.conversation && _this.liveChatVersion === LiveChatVersion_1.default.V2) {
1454
+ var acsConversation = _this.conversation;
1455
+ acsConversation.addListener("chatMessageReceived", checkForTags);
1456
+ acsConversation.addListener("chatMessageEdited", checkForTags);
1457
+ }
1458
+ }
1459
+ catch (error) {
1460
+ if (!resolved) {
1461
+ resolved = true;
1462
+ cleanup();
1463
+ reject(error);
1464
+ }
1465
+ }
1466
+ });
1467
+ };
1468
+ OmnichannelChatSDK.prototype.waitForSurveyStart = function () {
1469
+ return __awaiter(this, void 0, void 0, function () {
1470
+ return __generator(this, function (_a) {
1471
+ return [2 /*return*/, this.waitForMessageTags(['system', 'startconversationalsurvey'])];
1472
+ });
1473
+ });
1474
+ };
1475
+ OmnichannelChatSDK.prototype.waitForConversationalSurveyEnd = function () {
1476
+ return __awaiter(this, void 0, void 0, function () {
1477
+ return __generator(this, function (_a) {
1478
+ switch (_a.label) {
1479
+ case 0: return [4 /*yield*/, this.waitForMessageTags(['system', 'endconversationalsurvey'])];
1480
+ case 1:
1481
+ _a.sent();
1482
+ return [2 /*return*/];
1352
1483
  }
1353
1484
  });
1354
1485
  });
@@ -1393,7 +1524,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
1393
1524
  };
1394
1525
  OmnichannelChatSDK.prototype.getConversationDetails = function () {
1395
1526
  return __awaiter(this, arguments, void 0, function (optionalParams) {
1396
- var requestId, chatToken, chatId, reconnectId, sessionId, getLWIDetailsOptionalParams, lwiDetails, state, conversationId, agentAcceptedOn, canRenderPostChat, participantType, liveWorkItemDetails, error_16, telemetryData;
1527
+ var requestId, chatToken, chatId, reconnectId, sessionId, getLWIDetailsOptionalParams, lwiDetails, state, conversationId, agentAcceptedOn, canRenderPostChat, participantType, liveWorkItemDetails, error_17, telemetryData;
1397
1528
  var _a, _b, _c, _d;
1398
1529
  if (optionalParams === void 0) { optionalParams = {}; }
1399
1530
  return __generator(this, function (_e) {
@@ -1466,15 +1597,15 @@ var OmnichannelChatSDK = /** @class */ (function () {
1466
1597
  });
1467
1598
  return [2 /*return*/, liveWorkItemDetails];
1468
1599
  case 3:
1469
- error_16 = _e.sent();
1600
+ error_17 = _e.sent();
1470
1601
  telemetryData = {
1471
1602
  RequestId: requestId,
1472
1603
  ChatId: chatId
1473
1604
  };
1474
- if ((0, utilities_1.isClientIdNotFoundErrorMessage)(error_16)) {
1475
- exceptionThrowers_1.default.throwAuthContactIdNotFoundFailure(error_16, this.scenarioMarker, TelemetryEvent_1.default.GetConversationDetails, telemetryData);
1605
+ if ((0, utilities_1.isClientIdNotFoundErrorMessage)(error_17)) {
1606
+ exceptionThrowers_1.default.throwAuthContactIdNotFoundFailure(error_17, this.scenarioMarker, TelemetryEvent_1.default.GetConversationDetails, telemetryData);
1476
1607
  }
1477
- exceptionSuppressors_1.default.suppressConversationDetailsRetrievalFailure(error_16, this.scenarioMarker, TelemetryEvent_1.default.GetConversationDetails, telemetryData);
1608
+ exceptionSuppressors_1.default.suppressConversationDetailsRetrievalFailure(error_17, this.scenarioMarker, TelemetryEvent_1.default.GetConversationDetails, telemetryData);
1478
1609
  return [3 /*break*/, 4];
1479
1610
  case 4: return [2 /*return*/, {}];
1480
1611
  }
@@ -1515,7 +1646,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
1515
1646
  };
1516
1647
  OmnichannelChatSDK.prototype.getChatToken = function () {
1517
1648
  return __awaiter(this, arguments, void 0, function (cached, optionalParams) {
1518
- var getChatTokenOptionalParams, chatToken, error_17, telemetryData;
1649
+ var getChatTokenOptionalParams, chatToken, error_18, telemetryData;
1519
1650
  var _a, _b, _c;
1520
1651
  if (cached === void 0) { cached = true; }
1521
1652
  return __generator(this, function (_d) {
@@ -1557,16 +1688,16 @@ var OmnichannelChatSDK = /** @class */ (function () {
1557
1688
  });
1558
1689
  return [3 /*break*/, 4];
1559
1690
  case 3:
1560
- error_17 = _d.sent();
1691
+ error_18 = _d.sent();
1561
1692
  telemetryData = {
1562
1693
  RequestId: this.requestId,
1563
1694
  ChatId: (_c = this.chatToken) === null || _c === void 0 ? void 0 : _c.chatId,
1564
1695
  };
1565
- if ((0, utilities_1.isClientIdNotFoundErrorMessage)(error_17)) {
1566
- exceptionThrowers_1.default.throwAuthContactIdNotFoundFailure(error_17, this.scenarioMarker, TelemetryEvent_1.default.GetChatToken, telemetryData);
1696
+ if ((0, utilities_1.isClientIdNotFoundErrorMessage)(error_18)) {
1697
+ exceptionThrowers_1.default.throwAuthContactIdNotFoundFailure(error_18, this.scenarioMarker, TelemetryEvent_1.default.GetChatToken, telemetryData);
1567
1698
  }
1568
1699
  else {
1569
- exceptionThrowers_1.default.throwChatTokenRetrievalFailure(error_17, this.scenarioMarker, TelemetryEvent_1.default.GetChatToken, telemetryData);
1700
+ exceptionThrowers_1.default.throwChatTokenRetrievalFailure(error_18, this.scenarioMarker, TelemetryEvent_1.default.GetChatToken, telemetryData);
1570
1701
  }
1571
1702
  return [3 /*break*/, 4];
1572
1703
  case 4: return [3 /*break*/, 6];
@@ -1725,7 +1856,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
1725
1856
  };
1726
1857
  OmnichannelChatSDK.prototype.sendMessage = function (message) {
1727
1858
  return __awaiter(this, void 0, void 0, function () {
1728
- var sendMessageRequest, chatMessage, error_18, exceptionDetails;
1859
+ var sendMessageRequest, chatMessage, error_19, exceptionDetails;
1729
1860
  var _a;
1730
1861
  return __generator(this, function (_b) {
1731
1862
  switch (_b.label) {
@@ -1760,10 +1891,10 @@ var OmnichannelChatSDK = /** @class */ (function () {
1760
1891
  });
1761
1892
  return [2 /*return*/, chatMessage];
1762
1893
  case 3:
1763
- error_18 = _b.sent();
1894
+ error_19 = _b.sent();
1764
1895
  exceptionDetails = {
1765
1896
  response: ChatSDKError_1.ChatSDKErrorName.ChatSDKSendMessageFailed,
1766
- errorObject: "".concat(error_18)
1897
+ errorObject: "".concat(error_19)
1767
1898
  };
1768
1899
  this.scenarioMarker.failScenario(TelemetryEvent_1.default.SendMessages, {
1769
1900
  RequestId: this.requestId,
@@ -1772,7 +1903,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
1772
1903
  });
1773
1904
  throw new ChatSDKError_1.ChatSDKError(ChatSDKError_1.ChatSDKErrorName.ChatSDKSendMessageFailed, undefined, {
1774
1905
  response: ChatSDKError_1.ChatSDKErrorName.ChatSDKSendMessageFailed,
1775
- errorObject: "".concat(error_18)
1906
+ errorObject: "".concat(error_19)
1776
1907
  });
1777
1908
  case 4: return [2 /*return*/];
1778
1909
  }
@@ -1858,7 +1989,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
1858
1989
  };
1859
1990
  OmnichannelChatSDK.prototype.sendTypingEvent = function () {
1860
1991
  return __awaiter(this, void 0, void 0, function () {
1861
- var error_19;
1992
+ var error_20;
1862
1993
  return __generator(this, function (_a) {
1863
1994
  switch (_a.label) {
1864
1995
  case 0:
@@ -1887,7 +2018,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
1887
2018
  });
1888
2019
  return [3 /*break*/, 5];
1889
2020
  case 4:
1890
- error_19 = _a.sent();
2021
+ error_20 = _a.sent();
1891
2022
  this.scenarioMarker.failScenario(TelemetryEvent_1.default.SendTypingEvent, {
1892
2023
  RequestId: this.requestId,
1893
2024
  ChatId: this.chatToken.chatId
@@ -1954,6 +2085,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
1954
2085
  };
1955
2086
  OmnichannelChatSDK.prototype.onAgentEndSession = function (onAgentEndSessionCallback) {
1956
2087
  return __awaiter(this, void 0, void 0, function () {
2088
+ var agentEndSessionFired_1;
1957
2089
  var _this = this;
1958
2090
  return __generator(this, function (_a) {
1959
2091
  this.scenarioMarker.startScenario(TelemetryEvent_1.default.OnAgentEndSession, {
@@ -1964,18 +2096,62 @@ var OmnichannelChatSDK = /** @class */ (function () {
1964
2096
  exceptionThrowers_1.default.throwUninitializedChatSDK(this.scenarioMarker, TelemetryEvent_1.default.OnAgentEndSession);
1965
2097
  }
1966
2098
  try {
2099
+ agentEndSessionFired_1 = false;
1967
2100
  this.conversation.registerOnThreadUpdate(function (event) { return __awaiter(_this, void 0, void 0, function () {
1968
- var liveWorkItemDetails;
1969
- return __generator(this, function (_a) {
1970
- switch (_a.label) {
1971
- case 0: return [4 /*yield*/, this.getConversationDetails()];
2101
+ var maxRetries, retryDelayMs, attempt, liveWorkItemDetails, _a;
2102
+ return __generator(this, function (_b) {
2103
+ switch (_b.label) {
2104
+ case 0:
2105
+ if (this.isEndingChat) {
2106
+ return [2 /*return*/];
2107
+ }
2108
+ if (agentEndSessionFired_1) {
2109
+ return [2 /*return*/];
2110
+ }
2111
+ maxRetries = 3;
2112
+ retryDelayMs = 2000;
2113
+ attempt = 1;
2114
+ _b.label = 1;
1972
2115
  case 1:
1973
- liveWorkItemDetails = _a.sent();
2116
+ if (!(attempt <= maxRetries)) return [3 /*break*/, 10];
2117
+ liveWorkItemDetails = void 0;
2118
+ _b.label = 2;
2119
+ case 2:
2120
+ _b.trys.push([2, 4, , 7]);
2121
+ return [4 /*yield*/, this.getConversationDetails()];
2122
+ case 3:
2123
+ liveWorkItemDetails = _b.sent();
2124
+ return [3 /*break*/, 7];
2125
+ case 4:
2126
+ _a = _b.sent();
2127
+ if (!(attempt < maxRetries)) return [3 /*break*/, 6];
2128
+ return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, retryDelayMs); })];
2129
+ case 5:
2130
+ _b.sent();
2131
+ return [3 /*break*/, 9];
2132
+ case 6: return [2 /*return*/];
2133
+ case 7:
1974
2134
  if (Object.keys(liveWorkItemDetails).length === 0 || liveWorkItemDetails.state == LiveWorkItemState_1.default.WrapUp || liveWorkItemDetails.state == LiveWorkItemState_1.default.Closed) {
2135
+ if (agentEndSessionFired_1) {
2136
+ return [2 /*return*/];
2137
+ }
2138
+ agentEndSessionFired_1 = true;
1975
2139
  onAgentEndSessionCallback(event);
1976
2140
  this.stopPolling();
2141
+ return [2 /*return*/];
1977
2142
  }
1978
- return [2 /*return*/];
2143
+ if (!(attempt < maxRetries)) return [3 /*break*/, 9];
2144
+ return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, retryDelayMs); })];
2145
+ case 8:
2146
+ _b.sent();
2147
+ if (this.isEndingChat) {
2148
+ return [2 /*return*/];
2149
+ }
2150
+ _b.label = 9;
2151
+ case 9:
2152
+ attempt++;
2153
+ return [3 /*break*/, 1];
2154
+ case 10: return [2 /*return*/];
1979
2155
  }
1980
2156
  });
1981
2157
  }); });
@@ -1996,7 +2172,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
1996
2172
  };
1997
2173
  OmnichannelChatSDK.prototype.uploadFileAttachment = function (fileInfo) {
1998
2174
  return __awaiter(this, void 0, void 0, function () {
1999
- var amsClient, createObjectResponse, documentId, uploadDocumentResponse, fileIdsProperty, fileMetaProperty, sendMessageRequest, messageToSend, error_20, fileMetadata, messageToSend, error_21;
2175
+ var amsClient, createObjectResponse, documentId, uploadDocumentResponse, fileIdsProperty, fileMetaProperty, sendMessageRequest, messageToSend, error_21, fileMetadata, messageToSend, error_22;
2000
2176
  var _a, _b;
2001
2177
  return __generator(this, function (_c) {
2002
2178
  switch (_c.label) {
@@ -2065,7 +2241,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
2065
2241
  });
2066
2242
  return [2 /*return*/, messageToSend];
2067
2243
  case 6:
2068
- error_20 = _c.sent();
2244
+ error_21 = _c.sent();
2069
2245
  console.error("OmnichannelChatSDK/uploadFileAttachment/sendMessage/error");
2070
2246
  this.scenarioMarker.failScenario(TelemetryEvent_1.default.UploadFileAttachment, {
2071
2247
  RequestId: this.requestId,
@@ -2111,8 +2287,8 @@ var OmnichannelChatSDK = /** @class */ (function () {
2111
2287
  });
2112
2288
  return [2 /*return*/, messageToSend];
2113
2289
  case 15:
2114
- error_21 = _c.sent();
2115
- console.error("OmnichannelChatSDK/uploadFileAttachment/error: ".concat(error_21));
2290
+ error_22 = _c.sent();
2291
+ console.error("OmnichannelChatSDK/uploadFileAttachment/error: ".concat(error_22));
2116
2292
  this.scenarioMarker.failScenario(TelemetryEvent_1.default.UploadFileAttachment, {
2117
2293
  RequestId: this.requestId,
2118
2294
  ChatId: this.chatToken.chatId
@@ -2125,7 +2301,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
2125
2301
  };
2126
2302
  OmnichannelChatSDK.prototype.downloadFileAttachment = function (fileMetadata) {
2127
2303
  return __awaiter(this, void 0, void 0, function () {
2128
- var amsClient, response, view_location, viewResponse, ex_1, downloadedFile, error_22;
2304
+ var amsClient, response, view_location, viewResponse, ex_1, downloadedFile, error_23;
2129
2305
  return __generator(this, function (_a) {
2130
2306
  switch (_a.label) {
2131
2307
  case 0:
@@ -2186,8 +2362,8 @@ var OmnichannelChatSDK = /** @class */ (function () {
2186
2362
  });
2187
2363
  return [2 /*return*/, downloadedFile];
2188
2364
  case 9:
2189
- error_22 = _a.sent();
2190
- console.error("OmnichannelChatSDK/downloadFileAttachment/error: ".concat(error_22));
2365
+ error_23 = _a.sent();
2366
+ console.error("OmnichannelChatSDK/downloadFileAttachment/error: ".concat(error_23));
2191
2367
  this.scenarioMarker.failScenario(TelemetryEvent_1.default.DownloadFileAttachment, {
2192
2368
  RequestId: this.requestId,
2193
2369
  ChatId: this.chatToken.chatId
@@ -2200,7 +2376,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
2200
2376
  };
2201
2377
  OmnichannelChatSDK.prototype.emailLiveChatTranscript = function (body_1) {
2202
2378
  return __awaiter(this, arguments, void 0, function (body, optionalParams) {
2203
- var emailTranscriptOptionalParams, requestId, chatToken, chatId, sessionId, emailRequestBody, error_23;
2379
+ var emailTranscriptOptionalParams, requestId, chatToken, chatId, sessionId, emailRequestBody, error_24;
2204
2380
  var _a;
2205
2381
  if (optionalParams === void 0) { optionalParams = {}; }
2206
2382
  return __generator(this, function (_b) {
@@ -2251,8 +2427,8 @@ var OmnichannelChatSDK = /** @class */ (function () {
2251
2427
  });
2252
2428
  return [3 /*break*/, 4];
2253
2429
  case 3:
2254
- error_23 = _b.sent();
2255
- console.error("OmnichannelChatSDK/emailLiveChatTranscript/error: ".concat(error_23));
2430
+ error_24 = _b.sent();
2431
+ console.error("OmnichannelChatSDK/emailLiveChatTranscript/error: ".concat(error_24));
2256
2432
  this.scenarioMarker.failScenario(TelemetryEvent_1.default.EmailLiveChatTranscript, {
2257
2433
  RequestId: requestId,
2258
2434
  ChatId: chatId
@@ -2265,7 +2441,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
2265
2441
  };
2266
2442
  OmnichannelChatSDK.prototype.getLiveChatTranscript = function () {
2267
2443
  return __awaiter(this, arguments, void 0, function (optionalParams) {
2268
- var getChatTranscriptOptionalParams, requestId, chatToken, chatId, sessionId, transcriptResponse, error_24, telemetryData;
2444
+ var getChatTranscriptOptionalParams, requestId, chatToken, chatId, sessionId, transcriptResponse, error_25, telemetryData;
2269
2445
  var _a, _b;
2270
2446
  if (optionalParams === void 0) { optionalParams = {}; }
2271
2447
  return __generator(this, function (_c) {
@@ -2322,12 +2498,12 @@ var OmnichannelChatSDK = /** @class */ (function () {
2322
2498
  });
2323
2499
  return [2 /*return*/, transcriptResponse];
2324
2500
  case 3:
2325
- error_24 = _c.sent();
2501
+ error_25 = _c.sent();
2326
2502
  telemetryData = {
2327
2503
  RequestId: requestId,
2328
2504
  ChatId: chatId
2329
2505
  };
2330
- exceptionThrowers_1.default.throwLiveChatTranscriptRetrievalFailure(error_24, this.scenarioMarker, TelemetryEvent_1.default.GetLiveChatTranscript, telemetryData);
2506
+ exceptionThrowers_1.default.throwLiveChatTranscriptRetrievalFailure(error_25, this.scenarioMarker, TelemetryEvent_1.default.GetLiveChatTranscript, telemetryData);
2331
2507
  return [3 /*break*/, 4];
2332
2508
  case 4: return [2 /*return*/];
2333
2509
  }
@@ -2877,7 +3053,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
2877
3053
  };
2878
3054
  OmnichannelChatSDK.prototype.getChatConfig = function () {
2879
3055
  return __awaiter(this, arguments, void 0, function (optionalParams) {
2880
- var sendCacheHeaders, bypassCache, liveChatConfig, error_25, _a;
3056
+ var sendCacheHeaders, bypassCache, liveChatConfig, error_26, _a;
2881
3057
  var _b, _c;
2882
3058
  if (optionalParams === void 0) { optionalParams = {}; }
2883
3059
  return __generator(this, function (_d) {
@@ -2898,8 +3074,8 @@ var OmnichannelChatSDK = /** @class */ (function () {
2898
3074
  this.debug && console.log("[OmnichannelChatSDK][getChatConfig][liveChatVersion] ".concat(this.liveChatVersion));
2899
3075
  return [2 /*return*/, this.liveChatConfig];
2900
3076
  case 3:
2901
- error_25 = _d.sent();
2902
- if (!(0, internalUtils_1.isCoreServicesOrgUrlDNSError)(error_25, this.coreServicesOrgUrl, this.dynamicsLocationCode)) return [3 /*break*/, 6];
3077
+ error_26 = _d.sent();
3078
+ if (!(0, internalUtils_1.isCoreServicesOrgUrlDNSError)(error_26, this.coreServicesOrgUrl, this.dynamicsLocationCode)) return [3 /*break*/, 6];
2903
3079
  this.omnichannelConfig.orgUrl = this.unqServicesOrgUrl;
2904
3080
  _a = this;
2905
3081
  return [4 /*yield*/, ocsdk_1.SDKProvider.getSDK(this.omnichannelConfig, (0, createOcSDKConfiguration_1.default)(false), this.ocSdkLogger)];
@@ -2911,10 +3087,10 @@ var OmnichannelChatSDK = /** @class */ (function () {
2911
3087
  return [3 /*break*/, 7];
2912
3088
  case 6:
2913
3089
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
2914
- if (((_c = (_b = error_25.response) === null || _b === void 0 ? void 0 : _b.headers) === null || _c === void 0 ? void 0 : _c.errorcode) && parseInt(error_25.response.headers.errorcode) === OmnichannelErrorCodes_1.default.WidgetNotFound) {
3090
+ if (((_c = (_b = error_26.response) === null || _b === void 0 ? void 0 : _b.headers) === null || _c === void 0 ? void 0 : _c.errorcode) && parseInt(error_26.response.headers.errorcode) === OmnichannelErrorCodes_1.default.WidgetNotFound) {
2915
3091
  console.warn("No widget with the given app id is present in the system.");
2916
3092
  }
2917
- throw error_25; // Bubble up error by default to throw ChatConfigRetrievalFailure
3093
+ throw error_26; // Bubble up error by default to throw ChatConfigRetrievalFailure
2918
3094
  case 7: return [3 /*break*/, 8];
2919
3095
  case 8: return [2 /*return*/, this.liveChatConfig];
2920
3096
  }
@@ -2949,7 +3125,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
2949
3125
  };
2950
3126
  OmnichannelChatSDK.prototype.updateChatToken = function (newToken, newRegionGTMS) {
2951
3127
  return __awaiter(this, void 0, void 0, function () {
2952
- var sessionInfo, error_26, exceptionDetails;
3128
+ var sessionInfo, error_27, exceptionDetails;
2953
3129
  return __generator(this, function (_a) {
2954
3130
  switch (_a.label) {
2955
3131
  case 0:
@@ -2977,7 +3153,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
2977
3153
  });
2978
3154
  return [3 /*break*/, 5];
2979
3155
  case 4:
2980
- error_26 = _a.sent();
3156
+ error_27 = _a.sent();
2981
3157
  exceptionDetails = {
2982
3158
  response: "UpdateChatTokenFailed"
2983
3159
  };
@@ -2994,7 +3170,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
2994
3170
  };
2995
3171
  OmnichannelChatSDK.prototype.setAuthTokenProvider = function (provider_1) {
2996
3172
  return __awaiter(this, arguments, void 0, function (provider, optionalParams) {
2997
- var token, exceptionDetails, error_27, exceptionDetails, exceptionDetails;
3173
+ var token, exceptionDetails, error_28, exceptionDetails, exceptionDetails;
2998
3174
  if (optionalParams === void 0) { optionalParams = {}; }
2999
3175
  return __generator(this, function (_a) {
3000
3176
  switch (_a.label) {
@@ -3026,12 +3202,12 @@ var OmnichannelChatSDK = /** @class */ (function () {
3026
3202
  }
3027
3203
  return [3 /*break*/, 4];
3028
3204
  case 3:
3029
- error_27 = _a.sent();
3205
+ error_28 = _a.sent();
3030
3206
  exceptionDetails = {
3031
3207
  response: ChatSDKError_1.ChatSDKErrorName.GetAuthTokenFailed
3032
3208
  };
3033
- if (error_27.message == ChatSDKError_1.ChatSDKErrorName.UndefinedAuthToken) {
3034
- exceptionDetails.response = error_27.message;
3209
+ if (error_28.message == ChatSDKError_1.ChatSDKErrorName.UndefinedAuthToken) {
3210
+ exceptionDetails.response = error_28.message;
3035
3211
  }
3036
3212
  this.scenarioMarker.failScenario(TelemetryEvent_1.default.GetAuthToken, {
3037
3213
  ExceptionDetails: JSON.stringify(exceptionDetails)
@@ -3130,7 +3306,7 @@ var OmnichannelChatSDK = /** @class */ (function () {
3130
3306
  */
3131
3307
  OmnichannelChatSDK.prototype.getPersistentChatHistory = function () {
3132
3308
  return __awaiter(this, arguments, void 0, function (getPersistentChatHistoryOptionalParams) {
3133
- var params, result, error_28, telemetryData;
3309
+ var params, result, error_29, telemetryData;
3134
3310
  var _a, _b, _c, _d, _e;
3135
3311
  if (getPersistentChatHistoryOptionalParams === void 0) { getPersistentChatHistoryOptionalParams = {}; }
3136
3312
  return __generator(this, function (_f) {
@@ -3172,13 +3348,13 @@ var OmnichannelChatSDK = /** @class */ (function () {
3172
3348
  });
3173
3349
  return [2 /*return*/, result];
3174
3350
  case 3:
3175
- error_28 = _f.sent();
3351
+ error_29 = _f.sent();
3176
3352
  telemetryData = {
3177
3353
  RequestId: this.requestId,
3178
3354
  ChatId: (_e = this.chatToken) === null || _e === void 0 ? void 0 : _e.chatId,
3179
- ErrorMessage: (error_28 === null || error_28 === void 0 ? void 0 : error_28.message) || 'Unknown error' // Added error message for better debugging
3355
+ ErrorMessage: (error_29 === null || error_29 === void 0 ? void 0 : error_29.message) || 'Unknown error' // Added error message for better debugging
3180
3356
  };
3181
- exceptionThrowers_1.default.throwPersistentChatConversationRetrievalFailure(error_28, this.scenarioMarker, TelemetryEvent_1.default.GetPersistentChatHistory, telemetryData);
3357
+ exceptionThrowers_1.default.throwPersistentChatConversationRetrievalFailure(error_29, this.scenarioMarker, TelemetryEvent_1.default.GetPersistentChatHistory, telemetryData);
3182
3358
  return [3 /*break*/, 4];
3183
3359
  case 4: return [2 /*return*/];
3184
3360
  }