trySendInitPayload()

in src/components/Widget/index.js [617:660]


  trySendInitPayload() {
    const {
      customData,
      socket,
      initialized,
      isChatOpen,
      isChatVisible,
      embedded,
      connected,
      dispatch
    } = this.props;

    // Send initial payload when chat is opened or widget is shown
    if (!initialized && connected && ((isChatOpen && isChatVisible) || embedded)) {
      // Only send initial payload if the widget is connected to the server but not yet initialized

      const sessionId = this.getSessionId();
      // check that session_id is confirmed
      if (!sessionId) return;

      // DIAGNOSTIC: Check token expiration before /session_start
      if (customData?.auth_header) {
        try {
          const tokenPayload = customData.auth_header.split('.')[1];
          const decoded = JSON.parse(atob(tokenPayload.replace(/-/g, '+').replace(/_/g, '/')));
          const now = Date.now() / 1000;
          const timeLeft = decoded.exp - now;
          logger.info('🔍 INIT_PAYLOAD (/session_start): Access token expires in:', Math.round(timeLeft / 60), 'minutes');
          if (timeLeft < 0) {
            logger.error('❌ INIT_PAYLOAD: Sending /session_start with EXPIRED access token! Expired', Math.round(-timeLeft / 60), 'minutes ago');
          }
        } catch (e) {
          logger.error('❌ INIT_PAYLOAD: Failed to decode access token:', e);
        }
      }

      socket.emit('user_uttered', { message: '/session_start', customData, session_id: sessionId });
      dispatch(initialize());
      // Show WIP bubble while waiting for bot's response to /session_start
      dispatch(setBotProcessing(true));
      // Start 30-second timeout to reset bot processing if backend hangs
      startBotProcessingTimeout(dispatch);
    }
  }