@crestapps/ai-chat-ui 1.0.0-rc.7 → 1.0.0

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.
@@ -6,7 +6,7 @@
6
6
  function clampWidgetValue(value, min, max) {
7
7
  return Math.min(Math.max(value, min), max);
8
8
  }
9
- window.openAIChatWidgetBehavior = window.openAIChatWidgetBehavior || function () {
9
+ window.coreAIChatWidgetBehavior = window.coreAIChatWidgetBehavior || function () {
10
10
  function parsePixelValue(value) {
11
11
  if (typeof value === 'number' && Number.isFinite(value)) {
12
12
  return value;
@@ -497,9 +497,6 @@ window.openAIChatWidgetBehavior = window.openAIChatWidgetBehavior || function ()
497
497
  window.addEventListener('resize', app.widgetViewportResizeHandler);
498
498
  }
499
499
  app.reloadCurrentSession();
500
- if (config.autoCreateSession && !app.getSessionId()) {
501
- app.startNewSession();
502
- }
503
500
  if (config.widget.showHistoryButton && app.chatHistorySection) {
504
501
  var showHistoryButton = document.querySelector(config.widget.showHistoryButton);
505
502
  if (showHistoryButton) {
@@ -582,7 +579,106 @@ window.openAIChatWidgetBehavior = window.openAIChatWidgetBehavior || function ()
582
579
  onBeforeUnmount: dispose
583
580
  };
584
581
  }();
585
- window.openAIChatWidgetManager = window.openAIChatWidgetManager || function () {
582
+ window.coreAIChatWidgetManager = window.coreAIChatWidgetManager || function () {
583
+ function getAttributeValue(element, attributeName) {
584
+ var value = element.getAttribute(attributeName);
585
+ return value === null || value === '' ? null : value;
586
+ }
587
+ function parseBooleanAttributeValue(value) {
588
+ if (value === null || value === undefined) {
589
+ return null;
590
+ }
591
+ if (typeof value === 'boolean') {
592
+ return value;
593
+ }
594
+ var normalized = String(value).trim().toLowerCase();
595
+ if (normalized === 'true') {
596
+ return true;
597
+ }
598
+ if (normalized === 'false') {
599
+ return false;
600
+ }
601
+ return null;
602
+ }
603
+ function parseJsonAttribute(element, attributeName, description) {
604
+ var rawValue = getAttributeValue(element, attributeName);
605
+ if (!rawValue) {
606
+ return null;
607
+ }
608
+ try {
609
+ return JSON.parse(rawValue);
610
+ } catch (error) {
611
+ console.error('Failed to parse ' + description + ' JSON.', error);
612
+ return null;
613
+ }
614
+ }
615
+ function buildOptionsFromDataAttributes(element) {
616
+ var widgetContainerSelector = getAttributeValue(element, 'data-coreai-chat-widget-container-selector');
617
+ if (!widgetContainerSelector) {
618
+ return null;
619
+ }
620
+ var chatConfig = null;
621
+ if (window.coreAIChatManager && typeof window.coreAIChatManager.buildConfigFromElement === 'function') {
622
+ chatConfig = window.coreAIChatManager.buildConfigFromElement(element, {
623
+ allowWidgetHost: true
624
+ });
625
+ }
626
+ var profileId = getAttributeValue(element, 'data-coreai-chat-widget-profile-id');
627
+ var profileName = getAttributeValue(element, 'data-coreai-chat-widget-profile-name');
628
+ var profileDisplayText = getAttributeValue(element, 'data-coreai-chat-widget-profile-display-text');
629
+ var profileWelcomeMessage = getAttributeValue(element, 'data-coreai-chat-widget-profile-welcome-message');
630
+ var profileInitialPrompt = getAttributeValue(element, 'data-coreai-chat-widget-profile-initial-prompt');
631
+ var profileChatMode = getAttributeValue(element, 'data-coreai-chat-widget-profile-chat-mode');
632
+ var profileTtsVoiceName = getAttributeValue(element, 'data-coreai-chat-widget-profile-tts-voice-name');
633
+ var profileMetricsEnabled = parseBooleanAttributeValue(getAttributeValue(element, 'data-coreai-chat-widget-profile-enable-session-metrics'));
634
+ var profileSessionDocuments = parseBooleanAttributeValue(getAttributeValue(element, 'data-coreai-chat-widget-profile-allow-session-documents'));
635
+ var profile = null;
636
+ if (profileId || profileName || profileDisplayText || profileWelcomeMessage || profileChatMode || profileTtsVoiceName || profileMetricsEnabled !== null || profileSessionDocuments !== null) {
637
+ profile = {
638
+ id: profileId,
639
+ name: profileName,
640
+ displayText: profileDisplayText,
641
+ welcomeMessage: profileWelcomeMessage,
642
+ initialPrompt: profileInitialPrompt,
643
+ chatMode: profileChatMode,
644
+ ttsVoiceName: profileTtsVoiceName,
645
+ enableSessionMetrics: profileMetricsEnabled === true,
646
+ allowSessionDocuments: profileSessionDocuments === true
647
+ };
648
+ }
649
+ var enableDragging = parseBooleanAttributeValue(getAttributeValue(element, 'data-coreai-chat-widget-enable-dragging'));
650
+ var enableResizing = parseBooleanAttributeValue(getAttributeValue(element, 'data-coreai-chat-widget-enable-resizing'));
651
+ var persistLayout = parseBooleanAttributeValue(getAttributeValue(element, 'data-coreai-chat-widget-persist-layout'));
652
+ return {
653
+ shellSelector: getAttributeValue(element, 'data-coreai-chat-widget-shell-selector'),
654
+ widgetContainerSelector: widgetContainerSelector,
655
+ toggleButtonSelector: getAttributeValue(element, 'data-coreai-chat-widget-toggle-button-selector'),
656
+ closeButtonSelector: getAttributeValue(element, 'data-coreai-chat-widget-close-button-selector'),
657
+ inputSelector: getAttributeValue(element, 'data-coreai-chat-widget-input-selector'),
658
+ messageTemplateSelector: getAttributeValue(element, 'data-coreai-chat-widget-message-template-selector'),
659
+ indicatorTemplateSelector: getAttributeValue(element, 'data-coreai-chat-widget-indicator-template-selector'),
660
+ profile: profile,
661
+ widgetStateName: getAttributeValue(element, 'data-coreai-chat-widget-state-name'),
662
+ openStateValue: getAttributeValue(element, 'data-coreai-chat-widget-open-state-value'),
663
+ closedStateValue: getAttributeValue(element, 'data-coreai-chat-widget-closed-state-value'),
664
+ openIconHtml: getAttributeValue(element, 'data-coreai-chat-widget-open-icon-html'),
665
+ closedIconHtml: getAttributeValue(element, 'data-coreai-chat-widget-closed-icon-html'),
666
+ chatConfig: chatConfig ? Object.assign({}, chatConfig, {
667
+ initialPrompt: profileInitialPrompt || chatConfig.initialPrompt,
668
+ widget: Object.assign({}, chatConfig.widget || {}, {
669
+ chatWidgetContainer: widgetContainerSelector,
670
+ chatWidgetStateName: getAttributeValue(element, 'data-coreai-chat-widget-state-name'),
671
+ newChatButton: getAttributeValue(element, 'data-coreai-chat-widget-new-chat-button-selector'),
672
+ toggleButtonSelector: getAttributeValue(element, 'data-coreai-chat-widget-toggle-button-selector'),
673
+ resetSizeButtonSelector: getAttributeValue(element, 'data-coreai-chat-widget-reset-size-button-selector'),
674
+ dragHandleSelector: getAttributeValue(element, 'data-coreai-chat-widget-drag-handle-selector'),
675
+ enableDragging: enableDragging === null ? undefined : enableDragging,
676
+ enableResizing: enableResizing === null ? undefined : enableResizing,
677
+ persistLayout: persistLayout === null ? undefined : persistLayout
678
+ })
679
+ }) : null
680
+ };
681
+ }
586
682
  function initialize(options) {
587
683
  if (!options) {
588
684
  return null;
@@ -590,7 +686,7 @@ window.openAIChatWidgetManager = window.openAIChatWidgetManager || function () {
590
686
  var chatConfig = options.chatConfig;
591
687
  if (!chatConfig && options.configElementSelector) {
592
688
  var configElement = document.querySelector(options.configElementSelector);
593
- chatConfig = configElement ? JSON.parse(configElement.getAttribute('data-config')) : null;
689
+ chatConfig = configElement ? parseJsonAttribute(configElement, 'data-config', 'AI chat widget config') : null;
594
690
  }
595
691
  if (!chatConfig) {
596
692
  return null;
@@ -676,7 +772,7 @@ window.openAIChatWidgetManager = window.openAIChatWidgetManager || function () {
676
772
  shell.style.visibility = '';
677
773
  }
678
774
  }
679
- var widgetApp = window.openAIChatManager.initialize(chatConfig);
775
+ var widgetApp = window.coreAIChatManager.initialize(chatConfig);
680
776
  if (options.applySessionProfilePatch !== false && window.crestAppsAIChat && window.crestAppsAIChat.patchSessionProfileSync) {
681
777
  window.crestAppsAIChat.patchSessionProfileSync(widgetApp);
682
778
  }
@@ -807,8 +903,62 @@ window.openAIChatWidgetManager = window.openAIChatWidgetManager || function () {
807
903
  }
808
904
  return widgetApp;
809
905
  }
906
+ function initializeFromElement(element) {
907
+ if (!element || element.dataset.coreAiChatWidgetInitialized === 'true') {
908
+ return element ? element.__coreAIChatWidgetApp || null : null;
909
+ }
910
+ var jsonOptions = parseJsonAttribute(element, 'data-coreai-chat-widget-config', 'CoreAI chat widget config');
911
+ var attributeOptions = buildOptionsFromDataAttributes(element);
912
+ if (!jsonOptions && !attributeOptions) {
913
+ return null;
914
+ }
915
+ var options = Object.assign({}, jsonOptions || {}, attributeOptions || {});
916
+ var widgetApp = initialize(options);
917
+ if (!widgetApp) {
918
+ return null;
919
+ }
920
+ element.dataset.coreAiChatWidgetInitialized = 'true';
921
+ element.__coreAIChatWidgetApp = widgetApp;
922
+ return widgetApp;
923
+ }
924
+ function scanForAutoInitialization(root) {
925
+ if (!root || typeof root.querySelectorAll !== 'function') {
926
+ return;
927
+ }
928
+ if (typeof root.matches === 'function' && root.matches('[data-coreai-chat-widget-config],[data-coreai-chat-widget-container-selector]')) {
929
+ initializeFromElement(root);
930
+ }
931
+ root.querySelectorAll('[data-coreai-chat-widget-config],[data-coreai-chat-widget-container-selector]').forEach(initializeFromElement);
932
+ }
933
+ function startAutoInitialization() {
934
+ scanForAutoInitialization(document);
935
+ if (typeof MutationObserver === 'undefined') {
936
+ return;
937
+ }
938
+ var observer = new MutationObserver(function (mutations) {
939
+ mutations.forEach(function (mutation) {
940
+ mutation.addedNodes.forEach(function (node) {
941
+ if (node && node.nodeType === 1) {
942
+ scanForAutoInitialization(node);
943
+ }
944
+ });
945
+ });
946
+ });
947
+ observer.observe(document.body, {
948
+ childList: true,
949
+ subtree: true
950
+ });
951
+ }
952
+ if (document.readyState === 'loading') {
953
+ document.addEventListener('DOMContentLoaded', startAutoInitialization, {
954
+ once: true
955
+ });
956
+ } else {
957
+ startAutoInitialization();
958
+ }
810
959
  return {
811
- initialize: initialize
960
+ initialize: initialize,
961
+ initializeFromElement: initializeFromElement
812
962
  };
813
963
  }();
814
964
  //# sourceMappingURL=ai-chat-widget.js.map