@robylon/web-react-sdk 1.1.28-staging.53 → 1.1.28-staging.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,7 +13,7 @@ npm install @robylon/web-react-sdk
13
13
  ### Vanilla JavaScript
14
14
 
15
15
  ```html
16
- <script src="https://staging-cdn.robylon.com/1.1.28-staging.52/robylon.min.js"></script>
16
+ <script src="https://staging-cdn.robylon.com/1.1.28-staging.54/robylon.min.js"></script>
17
17
  ```
18
18
 
19
19
  Or via npm:
package/dist/cjs/index.js CHANGED
@@ -226,12 +226,13 @@ var getCookies = function (isTeachmint) {
226
226
  * Core ChatbotEmbed class - provides the main functionality for both React and vanilla JS implementations
227
227
  */
228
228
  var ChatbotEmbed = /** @class */ (function () {
229
- function ChatbotEmbed(config) {
229
+ function ChatbotEmbed(config, options) {
230
230
  var _this = this;
231
- this.logoContainer = null;
232
231
  this.iframe = null;
233
- this.bubblePromptContainer = null;
232
+ this.logoContainer = null;
234
233
  this.chatBtnImage = null;
234
+ this.bubblePromptContainer = null;
235
+ this.bubblePromptTimeouts = [];
235
236
  this.eventHandlers = new Map();
236
237
  this.isInitialized = false;
237
238
  this.onMessageListener = function (event) {
@@ -282,6 +283,8 @@ var ChatbotEmbed = /** @class */ (function () {
282
283
  }
283
284
  };
284
285
  this.config = config;
286
+ // Use provided container or default to document.body
287
+ this.containerElement = (options === null || options === void 0 ? void 0 : options.containerElement) || document.body;
285
288
  // Normalize the config
286
289
  if (typeof this.config.userId === "number") {
287
290
  this.config.userId = String(this.config.userId);
@@ -423,7 +426,7 @@ var ChatbotEmbed = /** @class */ (function () {
423
426
  cursor: "pointer",
424
427
  overflow: "hidden",
425
428
  });
426
- document.body.appendChild(div);
429
+ this.containerElement.appendChild(div);
427
430
  this.logoContainer = div;
428
431
  this.loadImage(this.logoContainer);
429
432
  this.createChatBubblContainer();
@@ -481,7 +484,7 @@ var ChatbotEmbed = /** @class */ (function () {
481
484
  chatbaseContainer.appendChild(messageBubble);
482
485
  });
483
486
  chatbaseContainer.appendChild(closeButton);
484
- document.body.appendChild(chatbaseContainer);
487
+ this.containerElement.appendChild(chatbaseContainer);
485
488
  };
486
489
  ChatbotEmbed.prototype.createMessageBubble = function (message) {
487
490
  var bubble = document.createElement("div");
@@ -557,7 +560,8 @@ var ChatbotEmbed = /** @class */ (function () {
557
560
  display: "none",
558
561
  });
559
562
  iframe.src = "".concat(this.config.domain, "/chatbot-plugin?id=").concat(this.config.chatbotId);
560
- document.body.appendChild(iframe);
563
+ // CRITICAL CHANGE: Append to container instead of body
564
+ this.containerElement.appendChild(iframe);
561
565
  this.iframe = iframe;
562
566
  // Media Query Handling
563
567
  var applyStylesBasedOnScreenWidth = function () {
@@ -713,59 +717,24 @@ var ChatbotEmbed = /** @class */ (function () {
713
717
  };
714
718
  (_b = this.iframe.contentWindow) === null || _b === void 0 ? void 0 : _b.postMessage(data, this.config.domain || "*");
715
719
  };
720
+ // Add method to update container
721
+ ChatbotEmbed.prototype.setContainer = function (container) {
722
+ if (!container)
723
+ return;
724
+ // Move existing elements to new container if they exist
725
+ if (this.iframe && this.iframe.parentElement) {
726
+ this.iframe.parentElement.removeChild(this.iframe);
727
+ container.appendChild(this.iframe);
728
+ }
729
+ if (this.logoContainer && this.logoContainer.parentElement) {
730
+ this.logoContainer.parentElement.removeChild(this.logoContainer);
731
+ container.appendChild(this.logoContainer);
732
+ }
733
+ this.containerElement = container;
734
+ };
716
735
  return ChatbotEmbed;
717
736
  }());
718
737
 
719
- /**
720
- * Environment configuration for the SDK
721
- */
722
- // Get current environment from build-time
723
- var NODE_ENV = "staging" ;
724
- // Default configuration values based on environment
725
- var DEFAULT_CONFIG = {
726
- // Default URLs for each environment
727
- copilotUrl: {
728
- development: "https://dev.robylon.ai",
729
- staging: "https://staging.d2s3wsqyyond1h.amplifyapp.com",
730
- production: "https://app.robylon.ai",
731
- },
732
- environment: NODE_ENV,
733
- apiUrl: {
734
- development: "https://dev-api.robylon.ai",
735
- staging: "https://stage-api.robylon.ai",
736
- production: "https://api.robylon.ai",
737
- },
738
- };
739
- // Store custom config values
740
- var customConfig = {};
741
- /**
742
- * Set custom environment configuration
743
- */
744
- var setEnvironmentConfig = function (config) {
745
- customConfig = __assign(__assign({}, customConfig), config);
746
- };
747
- /**
748
- * Gets environment configuration with defaults
749
- */
750
- var getEnvironmentConfig = function () {
751
- // Determine current environment
752
- var environment = customConfig.environment || NODE_ENV;
753
- // Ensure we pick URLs appropriate for the current environment
754
- var envType = environment === "staging"
755
- ? "staging"
756
- : environment === "development"
757
- ? "development"
758
- : "production";
759
- // Get default values for current environment
760
- var defaultCopilotUrl = DEFAULT_CONFIG.copilotUrl[envType];
761
- var defaultApiUrl = DEFAULT_CONFIG.apiUrl[envType];
762
- return {
763
- copilotUrl: customConfig.copilotUrl || defaultCopilotUrl,
764
- environment: environment,
765
- apiUrl: customConfig.apiUrl || defaultApiUrl,
766
- };
767
- };
768
-
769
738
  /**
770
739
  * Simple logger class with configurable log level
771
740
  */
@@ -849,6 +818,126 @@ var Logger = /** @class */ (function () {
849
818
  */
850
819
  var logger = new Logger();
851
820
 
821
+ /**
822
+ * Environment configuration for the SDK
823
+ */
824
+ // Get current environment from build-time
825
+ var NODE_ENV = "staging" ;
826
+ // Default configuration values based on environment
827
+ var DEFAULT_CONFIG = {
828
+ // Default URLs for each environment
829
+ copilotUrl: {
830
+ development: "https://dev.robylon.ai",
831
+ staging: "https://staging.d2s3wsqyyond1h.amplifyapp.com",
832
+ production: "https://app.robylon.ai",
833
+ },
834
+ environment: NODE_ENV,
835
+ apiUrl: {
836
+ development: "https://dev-api.robylon.ai",
837
+ staging: "https://stage-api.robylon.ai",
838
+ production: "https://api.robylon.ai",
839
+ },
840
+ };
841
+ // Store custom config values
842
+ var customConfig = {};
843
+ /**
844
+ * Set custom environment configuration
845
+ */
846
+ var setEnvironmentConfig = function (config) {
847
+ customConfig = __assign(__assign({}, customConfig), config);
848
+ };
849
+ /**
850
+ * Gets environment configuration with defaults
851
+ */
852
+ var getEnvironmentConfig = function () {
853
+ // Determine current environment
854
+ var environment = customConfig.environment || NODE_ENV;
855
+ // Ensure we pick URLs appropriate for the current environment
856
+ var envType = environment === "staging"
857
+ ? "staging"
858
+ : environment === "development"
859
+ ? "development"
860
+ : "production";
861
+ // Get default values for current environment
862
+ var defaultCopilotUrl = DEFAULT_CONFIG.copilotUrl[envType];
863
+ var defaultApiUrl = DEFAULT_CONFIG.apiUrl[envType];
864
+ return {
865
+ copilotUrl: customConfig.copilotUrl || defaultCopilotUrl,
866
+ environment: environment,
867
+ apiUrl: customConfig.apiUrl || defaultApiUrl,
868
+ };
869
+ };
870
+
871
+ /**
872
+ * Initialize the SDK with custom environment settings
873
+ * @internal This is primarily for internal use and advanced debugging.
874
+ * Normal users should not need to call this function.
875
+ */
876
+ function init(config) {
877
+ try {
878
+ setEnvironmentConfig({
879
+ environment: config.environment || "",
880
+ apiUrl: config.apiUrl || "",
881
+ copilotUrl: config.copilotUrl || "",
882
+ });
883
+ logger.debug("Robylon SDK initialized with custom environment", config);
884
+ }
885
+ catch (error) {
886
+ logger.error("Error initializing Robylon SDK:", error);
887
+ }
888
+ }
889
+ /**
890
+ * Normalize the user configuration
891
+ */
892
+ function normalizeConfig(config) {
893
+ return {
894
+ chatbotId: config.api_key || "",
895
+ userId: config.user_id ? String(config.user_id) : null,
896
+ token: config.user_token,
897
+ userProfile: config.user_profile,
898
+ };
899
+ }
900
+ /**
901
+ * Create a new Robylon chatbot instance
902
+ */
903
+ function create(config) {
904
+ return new Promise(function (resolve) {
905
+ try {
906
+ var normalizedConfig = normalizeConfig(config);
907
+ // Create the chatbot with container if specified
908
+ var chatbotEmbed_1 = new ChatbotEmbed(normalizedConfig, {
909
+ containerElement: config.container || undefined,
910
+ });
911
+ // Set up event handling
912
+ if (config.onEvent) {
913
+ chatbotEmbed_1.on("*", function (event) {
914
+ var _a;
915
+ (_a = config.onEvent) === null || _a === void 0 ? void 0 : _a.call(config, event);
916
+ });
917
+ }
918
+ // Resolve with the public API
919
+ resolve({
920
+ show: function () { return chatbotEmbed_1.show(); },
921
+ hide: function () { return chatbotEmbed_1.hide(); },
922
+ toggle: function () { return chatbotEmbed_1.toggle(); },
923
+ update: function (newConfig) {
924
+ return chatbotEmbed_1.updateConfig(normalizeConfig(newConfig));
925
+ },
926
+ destroy: function () { return chatbotEmbed_1.destroy(); },
927
+ setContainer: function (container) {
928
+ if (container instanceof HTMLElement) {
929
+ chatbotEmbed_1.setContainer(container);
930
+ }
931
+ },
932
+ });
933
+ }
934
+ catch (error) {
935
+ logger.error("Error creating Robylon chatbot:", error);
936
+ throw error;
937
+ }
938
+ });
939
+ }
940
+
852
941
  /**
853
942
  * Error codes for API operations
854
943
  */
@@ -1035,132 +1124,6 @@ var getSystemInfo = function () {
1035
1124
  };
1036
1125
  };
1037
1126
 
1038
- /**
1039
- * Initialize the SDK with custom environment settings
1040
- * @internal This is primarily for internal use and advanced debugging.
1041
- * Normal users should not need to call this function.
1042
- */
1043
- function init(config) {
1044
- // For internal SDK initialization - not intended for general user use
1045
- // We prioritize built-in environment settings based on NODE_ENV
1046
- // Get current environment from build-time
1047
- var currentEnv = "staging" ;
1048
- // Default environment-specific API URLs
1049
- var defaultApiUrls = {
1050
- development: "https://dev-api.robylon.ai",
1051
- staging: "https://stage-api.robylon.ai",
1052
- production: "https://api.robylon.ai",
1053
- };
1054
- // Default environment-specific Copilot URLs
1055
- var defaultCopilotUrls = {
1056
- development: "https://dev.robylon.ai",
1057
- staging: "https://staging.d2s3wsqyyond1h.amplifyapp.com",
1058
- production: "https://app.robylon.ai",
1059
- };
1060
- // Only override with user values in non-production environments or if explicitly allowed
1061
- var isProduction = currentEnv === "production";
1062
- config.allowProductionOverride === "true" && isProduction;
1063
- {
1064
- // Use environment-specific defaults as fallbacks
1065
- var configWithDefaults = __assign(__assign({}, config), { apiUrl: config.apiUrl ||
1066
- defaultApiUrls[currentEnv], copilotUrl: config.copilotUrl ||
1067
- defaultCopilotUrls[currentEnv], environment: config.environment || currentEnv });
1068
- setEnvironmentConfig(configWithDefaults);
1069
- logger.log("Robylon SDK environment initialized with custom config", configWithDefaults);
1070
- }
1071
- }
1072
- /**
1073
- * Create a new Robylon chatbot instance
1074
- */
1075
- function create(config) {
1076
- return __awaiter(this, void 0, void 0, function () {
1077
- var userId, isAnonymous, systemInfo_1, apiResponse, userProfile, chatbotConfig, chatbotEmbed_1, error_1;
1078
- var _a, _b, _c, _d, _e, _f, _g;
1079
- return __generator(this, function (_h) {
1080
- switch (_h.label) {
1081
- case 0:
1082
- if (!config.api_key) {
1083
- throw new Error("api_key is required to create a chatbot instance");
1084
- }
1085
- _h.label = 1;
1086
- case 1:
1087
- _h.trys.push([1, 3, , 4]);
1088
- userId = config.user_id;
1089
- isAnonymous = false;
1090
- // Handle anonymous users
1091
- if (!userId) {
1092
- userId = getCookie("rblyn_anon") || generateUUID();
1093
- setCookie("rblyn_anon", String(userId), 365);
1094
- isAnonymous = true;
1095
- }
1096
- systemInfo_1 = getSystemInfo();
1097
- return [4 /*yield*/, fetchChatbotConfig(config.api_key, userId, config.user_token)];
1098
- case 2:
1099
- apiResponse = _h.sent();
1100
- userProfile = __assign(__assign({}, systemInfo_1), config.user_profile);
1101
- chatbotConfig = {
1102
- chatbotId: config.api_key,
1103
- userId: userId,
1104
- token: config.user_token,
1105
- isAnonymous: isAnonymous,
1106
- domain: getEnvironmentConfig().copilotUrl,
1107
- brand_colour: ((_b = (_a = apiResponse.user.org_info.brand_config) === null || _a === void 0 ? void 0 : _a.colors) === null || _b === void 0 ? void 0 : _b.brand_color) ||
1108
- "#6a26cd",
1109
- image_url: ((_d = (_c = apiResponse.user.org_info) === null || _c === void 0 ? void 0 : _c.brand_config) === null || _d === void 0 ? void 0 : _d.launcher_logo_url) || "",
1110
- userProfile: userProfile,
1111
- chat_interface_config: {
1112
- chat_bubble_prompts: [],
1113
- display_name: ((_e = apiResponse.user.org_info.brand_config) === null || _e === void 0 ? void 0 : _e.display_name) || "",
1114
- welcome_message: ((_f = apiResponse.user.org_info.brand_config) === null || _f === void 0 ? void 0 : _f.welcome_message) ||
1115
- "Hey! What can we help you with today?",
1116
- redirect_url: ((_g = apiResponse.user.org_info.brand_config) === null || _g === void 0 ? void 0 : _g.redirect_url) || "",
1117
- },
1118
- };
1119
- chatbotEmbed_1 = new ChatbotEmbed(chatbotConfig);
1120
- // Set up event handler
1121
- if (config.onEvent) {
1122
- chatbotEmbed_1.on("*", config.onEvent);
1123
- }
1124
- // Return public API
1125
- return [2 /*return*/, {
1126
- show: function () { return chatbotEmbed_1.show(); },
1127
- hide: function () { return chatbotEmbed_1.hide(); },
1128
- toggle: function () { return chatbotEmbed_1.toggle(); },
1129
- update: function (newConfig) {
1130
- // Handle profile updates
1131
- if (newConfig.user_profile) {
1132
- chatbotEmbed_1.updateConfig({
1133
- userProfile: __assign(__assign({}, systemInfo_1), newConfig.user_profile),
1134
- });
1135
- }
1136
- // Handle user_id updates
1137
- if (newConfig.user_id !== undefined) {
1138
- chatbotEmbed_1.updateConfig({
1139
- userId: newConfig.user_id ? String(newConfig.user_id) : null,
1140
- });
1141
- }
1142
- // Handle token updates
1143
- if (newConfig.user_token !== undefined) {
1144
- chatbotEmbed_1.updateConfig({
1145
- token: newConfig.user_token,
1146
- });
1147
- }
1148
- },
1149
- destroy: function () { return chatbotEmbed_1.destroy(); },
1150
- }];
1151
- case 3:
1152
- error_1 = _h.sent();
1153
- logger.error("Failed to create chatbot instance:", error_1);
1154
- if (error_1 instanceof ApiError) {
1155
- throw new Error("Robylon SDK initialization failed: ".concat(error_1.message, " (").concat(error_1.code, ")"));
1156
- }
1157
- throw new Error("Robylon SDK initialization failed: ".concat(error_1 instanceof Error ? error_1.message : String(error_1)));
1158
- case 4: return [2 /*return*/];
1159
- }
1160
- });
1161
- });
1162
- }
1163
-
1164
1127
  /**
1165
1128
  * Error Boundary component to catch and handle React rendering errors
1166
1129
  */
@@ -1196,10 +1159,12 @@ var RobylonChatbot = React.memo(function (_a) {
1196
1159
  var api_key = _a.api_key, user_id = _a.user_id, user_token = _a.user_token, user_profile = _a.user_profile, onEvent = _a.onEvent;
1197
1160
  // Reference to the ChatbotEmbed instance
1198
1161
  var chatbotRef = React.useRef(null);
1162
+ // Add container ref
1163
+ var containerRef = React.useRef(null);
1199
1164
  // Track initialization state
1200
1165
  var _b = React.useState(false), isInitializing = _b[0], setIsInitializing = _b[1];
1201
1166
  var _c = React.useState(false), isInitialized = _c[0], setIsInitialized = _c[1];
1202
- var _d = React.useState(null), error = _d[0], setError = _d[1];
1167
+ var _d = React.useState(null); _d[0]; var setError = _d[1];
1203
1168
  var _e = React.useState(null), chatbotConfig = _e[0], setChatbotConfig = _e[1];
1204
1169
  // Stabilize onEvent callback
1205
1170
  var stableOnEvent = React.useCallback(function (event) {
@@ -1255,8 +1220,16 @@ var RobylonChatbot = React.memo(function (_a) {
1255
1220
  };
1256
1221
  // Store the config
1257
1222
  setChatbotConfig(config);
1258
- // Create the ChatbotEmbed instance
1259
- chatbotRef.current = new ChatbotEmbed(config);
1223
+ // CRITICAL CHANGE: Pass container element to ChatbotEmbed
1224
+ if (containerRef.current) {
1225
+ chatbotRef.current = new ChatbotEmbed(config, {
1226
+ containerElement: containerRef.current,
1227
+ });
1228
+ }
1229
+ else {
1230
+ // Fallback to body if container not available
1231
+ chatbotRef.current = new ChatbotEmbed(config);
1232
+ }
1260
1233
  // Register event handler
1261
1234
  if (stableOnEvent) {
1262
1235
  chatbotRef.current.on("*", stableOnEvent);
@@ -1275,7 +1248,14 @@ var RobylonChatbot = React.memo(function (_a) {
1275
1248
  case 5: return [2 /*return*/];
1276
1249
  }
1277
1250
  });
1278
- }); }, [api_key, user_id, user_token, stableOnEvent]);
1251
+ }); }, [
1252
+ api_key,
1253
+ user_id,
1254
+ user_token,
1255
+ stableOnEvent,
1256
+ isInitialized,
1257
+ isInitializing,
1258
+ ]);
1279
1259
  // Initialize on component mount
1280
1260
  React.useEffect(function () {
1281
1261
  if (api_key) {
@@ -1289,6 +1269,12 @@ var RobylonChatbot = React.memo(function (_a) {
1289
1269
  }
1290
1270
  };
1291
1271
  }, [initializeChatbot]);
1272
+ // If container reference changes, update the ChatbotEmbed
1273
+ React.useEffect(function () {
1274
+ if (containerRef.current && chatbotRef.current && isInitialized) {
1275
+ chatbotRef.current.setContainer(containerRef.current);
1276
+ }
1277
+ }, [containerRef.current, isInitialized]);
1292
1278
  // Update chatbot when user profile changes
1293
1279
  React.useEffect(function () {
1294
1280
  if (chatbotRef.current && isInitialized && chatbotConfig) {
@@ -1297,14 +1283,9 @@ var RobylonChatbot = React.memo(function (_a) {
1297
1283
  userProfile: __assign(__assign({}, systemInfo), user_profile),
1298
1284
  });
1299
1285
  }
1300
- }, [user_profile, isInitialized]);
1301
- // Don't render anything if there are errors or not initialized
1302
- if (!api_key || error || !isInitialized) {
1303
- return null;
1304
- }
1305
- // The ChatbotEmbed handles all rendering via DOM manipulation,
1306
- // so we don't need to return any visible elements here
1307
- return null;
1286
+ }, [user_profile, isInitialized, chatbotConfig]);
1287
+ // CRITICAL CHANGE: Return a container div
1288
+ return (jsxRuntime.jsx("div", { ref: containerRef, className: "robylon-chatbot-container", style: { position: "relative", width: "100%", height: "100%" } }));
1308
1289
  }, function (prevProps, nextProps) {
1309
1290
  // Memoization comparison
1310
1291
  return (prevProps.api_key === nextProps.api_key &&