MCPcopy Create free account
hub / github.com/The-Pocket/PocketFlow-Tutorial-Website-Chatbot / initializeChatbotLogic

Function initializeChatbotLogic

static/chatbot.js:884–1357  ·  view source on GitHub ↗
(currentUrl, extraUrls, prefixes, chatbotName, wsUrl, instruction = '', isOpenByDefault = false, wasCurrentUrlProvided = false)

Source from the content-addressed store, hash-verified

882 });
883
884 function initializeChatbotLogic(currentUrl, extraUrls, prefixes, chatbotName, wsUrl, instruction = '', isOpenByDefault = false, wasCurrentUrlProvided = false) {
885 // Get chat elements
886 const chatIcon = document.getElementById('pocketflow-chat-icon');
887 const chatWindow = document.getElementById('pocketflow-chat-window');
888 const chatMessages = document.getElementById('pocketflow-chat-messages');
889 const chatInput = document.getElementById('pocketflow-chat-input');
890 const sendButton = document.getElementById('pocketflow-chat-send-button');
891 const closeButton = document.getElementById('pocketflow-chat-close-button');
892 const maximizeButton = document.getElementById('pocketflow-chat-maximize-button');
893 const newConversationButton = document.getElementById('pocketflow-chat-new-conversation-button');
894 const typingIndicator = document.getElementById('pocketflow-typing-indicator');
895 const charCounter = document.getElementById('pocketflow-char-counter');
896 const connectionStatus = document.getElementById('pocketflow-connection-status');
897
898 // Chat state variables
899 let isChatOpen = isOpenByDefault; // Open by default if specified
900 let isChatMaximized = false; // Never start maximized, let user choose
901 let socket = null;
902 let isFirstMessage = true;
903 let hasPageChanged = false; // Track if page changed since last conversation
904
905
906
907 // Set initial state if opened by default
908 if (isOpenByDefault) {
909 isChatMaximized = true;
910 chatWindow.classList.add('open', 'maximized');
911
912 const [iconMax, iconMin] = ['icon-maximize', 'icon-minimize'].map(cls => maximizeButton.querySelector(`.${cls}`));
913 iconMax.style.display = 'none';
914 iconMin.style.display = 'block';
915 maximizeButton.setAttribute('aria-label', 'Restore chat');
916 }
917
918 // Auto-resize textarea and update character counter
919 chatInput.addEventListener('input', () => {
920 chatInput.style.height = 'auto';
921 const newHeight = Math.min(chatInput.scrollHeight, 120);
922 chatInput.style.height = newHeight + 'px';
923
924 // Ensure overflow is always hidden to prevent scrollbars
925 chatInput.style.overflow = 'hidden';
926
927 const currentLength = chatInput.value.length;
928 charCounter.textContent = `${currentLength}/1000`;
929
930 charCounter.classList.remove('warning', 'error');
931 if (currentLength > 900) {
932 charCounter.classList.add('error');
933 } else if (currentLength > 800) {
934 charCounter.classList.add('warning');
935 }
936
937 if (!chatInput.disabled) {
938 sendButton.disabled = currentLength > 1000 || currentLength === 0;
939 }
940 });
941

Callers 1

initializeChatbotFunction · 0.85

Calls 4

toggleChatFunction · 0.85
sendMessageFunction · 0.85
connectWebSocketFunction · 0.85

Tested by

no test coverage detected