PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.8.0
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.8.0
8.8.0 8.7.9 8.7.8 8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 8.5.5 All 536 releases
← All changes | js/qcld-wp-chatbot-plugin.js +587 -240 8.6.0 → 8.8.0 View file →
@@ -77,8 +77,72 @@
77 77 //wpwMsg.single(serviceOffer);
78 78 }
79 79 }
80 80 };
81 + function qcldPushAiContext(role, content) {
82 + if (typeof globalwpw === 'undefined') return;
83 + if (typeof globalwpw.aiContext === 'undefined') {
84 + globalwpw.aiContext = [];
85 + }
86 + if (!content) return;
87 +
88 + var cleanContent = content;
89 + if (role === 'assistant') {
90 + cleanContent = cleanContent.replace(/<[^>]*>?/gm, '').trim();
91 + } else if (role === 'user') {
92 + var lower = content.toLowerCase().trim();
93 + var titles = (typeof qcld_chatbot_obj !== 'undefined' && qcld_chatbot_obj.ai_form_titles) ? qcld_chatbot_obj.ai_form_titles : ((typeof wp_chatbot_obj !== 'undefined' && wp_chatbot_obj.ai_form_titles) ? wp_chatbot_obj.ai_form_titles : []);
94 + if (titles && titles.length) {
95 + for (var t = 0; t < titles.length; t++) {
96 + var title = titles[t];
97 + if (lower === title || lower.indexOf(title) !== -1 || title.indexOf(lower) !== -1) {
98 + globalwpw.active_ai_action = title;
99 + globalwpw.qcld_ai_form_persisted_state = true;
100 + break;
101 + }
102 + }
103 + }
104 + }
105 +
106 + globalwpw.aiContext.push({role: role, content: cleanContent});
107 + if (globalwpw.aiContext.length > 30) {
108 + globalwpw.aiContext.shift();
109 + }
110 + }
111 +
112 + function qcldIsAiFormInProgress() {
113 + if (typeof globalwpw !== 'undefined' && (globalwpw.active_ai_action || globalwpw.qcld_ai_form_persisted_state)) {
114 + return true;
115 + }
116 + var titles = (typeof qcld_chatbot_obj !== 'undefined' && qcld_chatbot_obj.ai_form_titles) ? qcld_chatbot_obj.ai_form_titles : ((typeof wp_chatbot_obj !== 'undefined' && wp_chatbot_obj.ai_form_titles) ? wp_chatbot_obj.ai_form_titles : []);
117 + if (!titles || !titles.length || typeof globalwpw === 'undefined' || !globalwpw.aiContext) {
118 + return false;
119 + }
120 +
121 + var hasFormTrigger = false;
122 + for (var i = 0; i < globalwpw.aiContext.length; i++) {
123 + var msg = globalwpw.aiContext[i];
124 + if (msg.role === 'user') {
125 + var content = (msg.content || '').toLowerCase().trim();
126 + for (var t = 0; t < titles.length; t++) {
127 + var title = titles[t];
128 + if (content === title || content.indexOf(title) !== -1 || title.indexOf(content) !== -1) {
129 + hasFormTrigger = true;
130 + globalwpw.active_ai_action = title;
131 + }
132 + }
133 + }
134 + if (msg.role === 'assistant') {
135 + var assistantContent = (msg.content || '').toLowerCase();
136 + if (msg.content && (msg.content.indexOf('__AI_FORM_DATA__') !== -1 || msg.content.indexOf('ai-form-summary-card') !== -1 || assistantContent.indexOf('thank you for providing the information') !== -1 || assistantContent.indexOf('successfully submitted') !== -1)) {
137 + hasFormTrigger = false;
138 + globalwpw.active_ai_action = '';
139 + }
140 + }
141 + }
142 +
143 + return hasFormTrigger || !!(globalwpw.active_ai_action);
144 + }
81 145 //Append the message to the message container based on the requirement.
82 146 var wpwMsg={
83 147 oncommand_filter:function(msg){
84 148 var str = msg;
@@ -83,8 +147,61 @@
83 147 oncommand_filter:function(msg){
84 148 var str = msg;
85 149 if(typeof(str) === 'string'){
86 150 str = str.replace(/on[a-zA-Z]+\s*=/g, "");
151 + str = str.replace(/__AI_FORM_IN_PROGRESS__/g, "");
152 +
153 + // Matches __AI_FORM_DATA__{...}__AI_FORM_DATA_END__ or html-wrapped variations (e.g. from Markdown Parsedown)
154 + var formRegex = /(?:<[^>]+>)*\s*(?:__|<strong>|<b>)?AI_FORM_DATA(?:__|<\/strong>|<\/b>)?[\s\S]*?(?:__|<strong>|<b>)?AI_FORM_DATA_END(?:__|<\/strong>|<\/b>)?\s*(?:<\/[^>]+>)*/gi;
155 +
156 + if (formRegex.test(str)) {
157 + str = str.replace(formRegex, function(match) {
158 + try {
159 + var jsonMatch = match.match(/\{[\s\S]*\}/);
160 + if (jsonMatch) {
161 + var jsonStr = jsonMatch[0];
162 + var data = JSON.parse(jsonStr.trim());
163 +
164 + // Save form entry via AJAX
165 + var postData = {
166 + 'action': 'qcld_save_ai_form',
167 + 'data': jsonStr,
168 + 'nonce': (typeof qcld_chatbot_obj !== 'undefined' ? qcld_chatbot_obj.nonce : '')
169 + };
170 + var ajaxUrl = (typeof qcld_chatbot_obj !== 'undefined' && qcld_chatbot_obj.ajax_url) ? qcld_chatbot_obj.ajax_url : (typeof wpbot_ajax !== 'undefined' ? wpbot_ajax.ajax_url : (typeof ajaxurl !== 'undefined' ? ajaxurl : ''));
171 + if (ajaxUrl) {
172 + jQuery.post(ajaxUrl, postData);
173 + }
174 +
175 + if (typeof globalwpw !== 'undefined') {
176 + globalwpw.aiContext = [];
177 + globalwpw.active_ai_action = '';
178 + globalwpw.qcld_ai_form_persisted_state = false;
179 + }
180 +
181 + var html = '<div class="ai-form-summary-card" style="background: #f4f6f9; border-left: 4px solid #0073aa; padding: 12px 14px; margin: 10px 0; border-radius: 4px; font-size: 13px; line-height: 1.5; color: #333;">';
182 + if (data.form_title) {
183 + html += '<div style="font-weight: 600; color: #0073aa; margin-bottom: 8px; text-transform: capitalize; font-size: 14px;">' + data.form_title + '</div>';
184 + }
185 + if (data.data && typeof data.data === 'object') {
186 + html += '<table style="width: 100%; border-collapse: collapse; margin-top: 4px;">';
187 + for (var key in data.data) {
188 + if (data.data.hasOwnProperty(key)) {
189 + var cleanKey = key.replace(/[-_]/g, ' ');
190 + html += '<tr><td style="padding: 3px 6px 3px 0; color: #555; font-weight: 600; width: 42%; vertical-align: top;">' + cleanKey + ':</td><td style="padding: 3px 0; color: #222; vertical-align: top;">' + data.data[key] + '</td></tr>';
191 + }
192 + }
193 + html += '</table>';
194 + }
195 + html += '</div>';
196 + return html;
197 + }
198 + } catch(e) {
199 + console.error('AI Form parse error:', e);
200 + }
201 + return '';
202 + });
203 + }
87 204 }
88 205 return str;
89 206 },
90 207 open_new_tab: function(msg){
@@ -535,9 +652,31 @@
535 652 return html.replace(/(?:https:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g, '<iframe width="250" height="180" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>');
536 653 });
537 654 },
538 655 scrollTo:function () {
539 - $(globalwpw.settings.botContainer).animate({ scrollTop: $(globalwpw.settings.messageWrapper).prop("scrollHeight")}, 'slow').parent().find('.slimScrollBar').css({'top':$(globalwpw.settings.botContainer).height()+'px'});;
656 + var $bot = $(globalwpw.settings.botContainer);
657 + var scrollTarget = $(globalwpw.settings.messageWrapper).prop("scrollHeight");
658 + $bot.animate({ scrollTop: scrollTarget }, 'slow', function () {
659 + if (typeof window.wpbotSyncSlimScrollBar === 'function') {
660 + window.wpbotSyncSlimScrollBar($bot);
661 + } else {
662 + var el = $bot[0];
663 + var $wrap = $bot.parent();
664 + var $bar = $wrap.children('.slimScrollBar');
665 + if (!el || !$bar.length) {
666 + return;
667 + }
668 + if (el.scrollHeight <= el.clientHeight + 2) {
669 + $wrap.addClass('wpbot-no-scroll');
670 + $bar.stop(true, true).hide();
671 + $wrap.children('.slimScrollRail').stop(true, true).hide();
672 + } else {
673 + $wrap.removeClass('wpbot-no-scroll');
674 + var barH = $bar.outerHeight() || 30;
675 + $bar.css({ top: Math.max(0, $bot.outerHeight() - barH) + 'px', display: 'block' });
676 + }
677 + }
678 + });
540 679 },
541 680 botPreloader:function () {
542 681 var enableleReactions = globalwpw.settings.obj.skip_chat_reactions_menu;
543 682 var eanleeReport = globalwpw.settings.obj.enable_chat_report_menu;
@@ -553,17 +692,17 @@
553 692 '</div>'+
554 693 '<div class="wp-chatbot-agent">'+ wpwMsg.oncommand_filter(globalwpw.settings.obj.agent)+'</div>'
555 694 +'<div class="chat-container"><div class="wp-chatbot-paragraph"><img class="wp-chatbot-comment-loader" src="'+globalwpw.settings.obj.image_path+'comment.gif" alt="Typing..." /></div>'+
556 695 '<div class="qcld-like-dislike-icon">' +
557 - (enableleReactions == 1
696 + (enableleReactions == 1 && !qcldIsAiFormInProgress()
558 697 ? '<a href="#" title="' + (disLikeTxt?.en_US || 'Dislike') + '"><i class="dashicons dashicons-thumbs-down" aria-hidden="true"></i></a>' + '<a href="#" title="' + (likeTxt?.en_US || 'Like') + '"><i class="dashicons dashicons-thumbs-up" aria-hidden="true"></i></a>'
559 698 : '') +
560 699
561 - (eanleeReport == 1
700 + (eanleeReport == 1 && !qcldIsAiFormInProgress()
562 701 ? '<a href="#" title="' + (reportTxt?.en_US || 'Report') + '"><i class="dashicons dashicons-admin-comments"></i></a>'
563 702 : '') +
564 703
565 - (enableshare == 1
704 + (enableshare == 1 && !qcldIsAiFormInProgress()
566 705 ? '<div class="qcld-share">' +
567 706 '<a href="#" class="share-toggle" title="' + (sharetTxt?.en_US || 'Share') + '"><i class="dashicons dashicons-share"></i></a>' +
568 707 '<div class="share-menu" style="display:none;">' +
569 708 '<a href="#" class="share-fb"><i class="dashicons dashicons-facebook"></i></a>' +
@@ -1412,8 +1551,10 @@
1412 1551
1413 1552 var customAappend = globalwpw.settings.obj.qcld_openai_append_content;
1414 1553 var customMessage = customAappend ? msg + ' ' + customAappend : msg;
1415 1554
1555 + qcldPushAiContext('user', customMessage);
1556 +
1416 1557 if(wp_chatbot_obj.is_stream_enabled == '1'){
1417 1558 // Callers already append botPreloader before calling openai_reply
1418 1559 var streamData = {
1419 1560 action: 'qcld_stream_openai',
@@ -1418,13 +1559,15 @@
1418 1559 var streamData = {
1419 1560 action: 'qcld_stream_openai',
1420 1561 name: globalwpw.hasNameCookie,
1421 1562 keyword: customMessage,
1563 + active_ai_action: globalwpw.active_ai_action || "",
1564 + ai_history: JSON.stringify(globalwpw.aiContext),
1422 1565 nonce: qcld_chatbot_obj.nonce
1423 1566 };
1424 1567 qcldStreamOpenAI(streamData);
1425 1568 } else {
1426 - var data = {'action':'qcld_openai_response','name':globalwpw.hasNameCookie,'keyword':customMessage,nonce: qcld_chatbot_obj.nonce};
1569 + var data = {'action':'qcld_openai_response','name':globalwpw.hasNameCookie,'keyword':customMessage,'active_ai_action':(globalwpw.active_ai_action || ""),'ai_history':JSON.stringify(globalwpw.aiContext),nonce: qcld_chatbot_obj.nonce};
1427 1570
1428 1571 wpwKits.ajax(data).done(function (res) {
1429 1572 if( res == '' || typeof res === 'object' ){
1430 1573 var json = res;
@@ -1432,14 +1575,30 @@
1432 1575 var json = $.parseJSON(res);
1433 1576 }
1434 1577
1435 1578 if(json.status=='success'){
1579 + qcldPushAiContext('assistant', json.message);
1580 +
1436 1581 var serviceOffer=wpwKits.randomMsg(globalwpw.settings.obj.support_option_again);
1437 1582
1438 1583 setTimeout(function(){
1584 + var isFormInProgress = qcldIsAiFormInProgress();
1585 + if (json.message && json.message.indexOf('__AI_FORM_IN_PROGRESS__') !== -1) {
1586 + isFormInProgress = true;
1587 + json.message = json.message.replace(/__AI_FORM_IN_PROGRESS__/g, '').trim();
1588 + }
1589 + if (json.message && (json.message.indexOf('AI_FORM_DATA') !== -1 || json.message.indexOf('ai-form-summary-card') !== -1)) {
1590 + isFormInProgress = false;
1591 + if (typeof globalwpw !== 'undefined') {
1592 + globalwpw.aiContext = [];
1593 + globalwpw.active_ai_action = '';
1594 + globalwpw.qcld_ai_form_persisted_state = false;
1595 + }
1596 + }
1597 +
1439 1598 wpwMsg.single(json.message);
1440 1599
1441 - if((globalwpw.settings.obj.qcld_disable_repited_startmenu != "1")){
1600 + if(!isFormInProgress && (globalwpw.settings.obj.qcld_disable_repited_startmenu != "1")){
1442 1601 if(globalwpw.settings.obj.disable_repeatative!=1){
1443 1602 setTimeout(function(){
1444 1603 var serviceOffer=wpwKits.randomMsg(globalwpw.settings.obj.support_option_again);
1445 1604 if((globalwpw.settings.obj.qcld_disable_repited_startmenu != "1") && ((globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1))){
@@ -1453,9 +1612,9 @@
1453 1612 }
1454 1613 }, globalwpw.settings.preLoadingTime*2);
1455 1614 }
1456 1615 }
1457 - $(globalwpw.settings.messageContainer).find('.wp-chatbot-msg:last .qcld-like-dislike-icon').css('display', 'block');
1616 + if(!isFormInProgress) { $(globalwpw.settings.messageContainer).find('.wp-chatbot-msg:last .qcld-like-dislike-icon').css('display', 'block'); }
1458 1617
1459 1618 },globalwpw.settings.preLoadingTime)
1460 1619 }
1461 1620 })
@@ -1467,9 +1626,11 @@
1467 1626 var customprepend = globalwpw.settings.obj.qcld_openrouter_prepend_content;
1468 1627 var customMessage = customprepend ? customprepend + ' ' + msg : msg;
1469 1628 customMessage = customAappend ? customMessage + ' ' + customAappend : customMessage;
1470 1629
1471 - var data = {'action':'openrouter_response','name':globalwpw.hasNameCookie,'keyword':customMessage};
1630 + qcldPushAiContext('user', customMessage);
1631 +
1632 + var data = {'action':'openrouter_response','name':globalwpw.hasNameCookie,'keyword':customMessage, 'active_ai_action':(globalwpw.active_ai_action || ""), nonce: qcld_chatbot_obj.nonce, ai_history: JSON.stringify(globalwpw.aiContext)};
1472 1633 wpwKits.ajax(data).done(function (res) {
1473 1634 if( res == '' || typeof res === 'object' ){
1474 1635 var json = res;
1475 1636 }if(typeof res === 'string'){
@@ -1475,14 +1636,29 @@
1475 1636 }if(typeof res === 'string'){
1476 1637 var json = $.parseJSON(res);
1477 1638 }
1478 1639 if(json.status=='success'){
1640 + qcldPushAiContext('assistant', json.message);
1479 1641 var serviceOffer=wpwKits.randomMsg(globalwpw.settings.obj.support_option_again);
1480 1642
1481 1643 setTimeout(function(){
1644 + var isFormInProgress = qcldIsAiFormInProgress();
1645 + if (json.message && json.message.indexOf('__AI_FORM_IN_PROGRESS__') !== -1) {
1646 + isFormInProgress = true;
1647 + json.message = json.message.replace(/__AI_FORM_IN_PROGRESS__/g, '').trim();
1648 + }
1649 + if (json.message && (json.message.indexOf('AI_FORM_DATA') !== -1 || json.message.indexOf('ai-form-summary-card') !== -1)) {
1650 + isFormInProgress = false;
1651 + if (typeof globalwpw !== 'undefined') {
1652 + globalwpw.aiContext = [];
1653 + globalwpw.active_ai_action = '';
1654 + globalwpw.qcld_ai_form_persisted_state = false;
1655 + }
1656 + }
1657 +
1482 1658 wpwMsg.single(json.message);
1483 1659
1484 - if(globalwpw.settings.obj.qcld_disable_repited_startmenu != "1" ){
1660 + if(!isFormInProgress && globalwpw.settings.obj.qcld_disable_repited_startmenu != "1" ){
1485 1661 if(globalwpw.settings.obj.disable_repeatative!=1){
1486 1662 setTimeout(function(){
1487 1663 var serviceOffer=wpwKits.randomMsg(globalwpw.settings.obj.support_option_again);
1488 1664 if((globalwpw.settings.obj.qcld_disable_repited_startmenu != "1") && ((globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1))){
@@ -1496,8 +1672,9 @@
1496 1672 }
1497 1673 }, globalwpw.settings.preLoadingTime*2);
1498 1674 }
1499 1675 }
1676 + if(!isFormInProgress) { $(globalwpw.settings.messageContainer).find('.wp-chatbot-msg:last .qcld-like-dislike-icon').css('display', 'block'); }
1500 1677 },globalwpw.settings.preLoadingTime)
1501 1678 }else{
1502 1679 wpwMsg.single_nobg('<span class="qcld-chatbot-wildcard qcld_back_to_start" data-wildcart="back">' + 'Sorry, I encountered an error processing your AI request. Please check api key and try again later.' + '</span>');
1503 1680 }
@@ -1502,8 +1679,79 @@
1502 1679 wpwMsg.single_nobg('<span class="qcld-chatbot-wildcard qcld_back_to_start" data-wildcart="back">' + 'Sorry, I encountered an error processing your AI request. Please check api key and try again later.' + '</span>');
1503 1680 }
1504 1681 })
1505 1682 }
1683 +
1684 + if(globalwpw.settings.obj.claude_enabled == 1){
1685 + var customAappend = globalwpw.settings.obj.qcld_claude_append_content;
1686 + var customprepend = globalwpw.settings.obj.qcld_claude_prepend_content;
1687 + var customMessage = customprepend ? customprepend + ' ' + msg : msg;
1688 + customMessage = customAappend ? customMessage + ' ' + customAappend : customMessage;
1689 +
1690 + qcldPushAiContext('user', customMessage);
1691 +
1692 + var claudeStreamEnabled = globalwpw.settings.obj.qcld_claude_stream_enabled;
1693 + if (claudeStreamEnabled == "1") {
1694 + const claudeStreamData = {
1695 + action: "qcld_stream_claude",
1696 + name: globalwpw.hasNameCookie,
1697 + keyword: customMessage,
1698 + active_ai_action: globalwpw.active_ai_action || "",
1699 + ai_history: JSON.stringify(globalwpw.aiContext)
1700 + };
1701 + streamClaude(claudeStreamData);
1702 + return;
1703 + } else {
1704 + var data = {'action':'claude_response','name':globalwpw.hasNameCookie,'keyword':customMessage, 'active_ai_action':(globalwpw.active_ai_action || ""), nonce: qcld_chatbot_obj.nonce, ai_history: JSON.stringify(globalwpw.aiContext)};
1705 + wpwKits.ajax(data).done(function (res) {
1706 + if( res == '' || typeof res === 'object' ){
1707 + var json = res;
1708 + }if(typeof res === 'string'){
1709 + var json = $.parseJSON(res);
1710 + }
1711 + if(json.status=='success'){
1712 + qcldPushAiContext('assistant', json.message);
1713 + var serviceOffer=wpwKits.randomMsg(globalwpw.settings.obj.support_option_again);
1714 + setTimeout(function(){
1715 + var isFormInProgress = qcldIsAiFormInProgress();
1716 + if (json.message && json.message.indexOf('__AI_FORM_IN_PROGRESS__') !== -1) {
1717 + isFormInProgress = true;
1718 + json.message = json.message.replace(/__AI_FORM_IN_PROGRESS__/g, '').trim();
1719 + }
1720 + if (json.message && (json.message.indexOf('AI_FORM_DATA') !== -1 || json.message.indexOf('ai-form-summary-card') !== -1)) {
1721 + isFormInProgress = false;
1722 + if (typeof globalwpw !== 'undefined') {
1723 + globalwpw.aiContext = [];
1724 + globalwpw.active_ai_action = '';
1725 + globalwpw.qcld_ai_form_persisted_state = false;
1726 + }
1727 + }
1728 +
1729 + wpwMsg.single(json.message);
1730 + if(!isFormInProgress && globalwpw.settings.obj.qcld_disable_repited_startmenu != "1" ){
1731 + if(globalwpw.settings.obj.disable_repeatative!=1){
1732 + setTimeout(function(){
1733 + var serviceOffer=wpwKits.randomMsg(globalwpw.settings.obj.support_option_again);
1734 + if((globalwpw.settings.obj.qcld_disable_repited_startmenu != "1") && ((globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1))){
1735 + wpwMsg.single_nobg('<span class="qcld-chatbot-wildcard qcld_back_to_start" data-wildcart="back">' + wpwKits.randomMsg(globalwpw.settings.obj.back_to_start) + '</span>');
1736 + }
1737 + },globalwpw.settings.preLoadingTime)
1738 + }else{
1739 + setTimeout(function(){
1740 + if((globalwpw.settings.obj.qcld_disable_repited_startmenu != "1") && ((globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1))){
1741 + wpwMsg.single_nobg('<span class="qcld-chatbot-wildcard qcld_back_to_start" data-wildcart="back">' + wpwKits.randomMsg(globalwpw.settings.obj.back_to_start) + '</span>');
1742 + }
1743 + }, globalwpw.settings.preLoadingTime*2);
1744 + }
1745 + }
1746 + if(!isFormInProgress) { $(globalwpw.settings.messageContainer).find('.wp-chatbot-msg:last .qcld-like-dislike-icon').css('display', 'block'); }
1747 + },globalwpw.settings.preLoadingTime)
1748 + }else{
1749 + wpwMsg.single_nobg('<span class="qcld-chatbot-wildcard qcld_back_to_start" data-wildcart="back">' + 'Sorry, I encountered an error processing your AI request. Please check api key and try again later.' + '</span>');
1750 + }
1751 + });
1752 + }
1753 + }
1506 1754 if(globalwpw.settings.obj.gemini_enabled == 1){
1507 1755 var customAappend = globalwpw.settings.obj.qcld_openrouter_append_content;
1508 1756 var customprepend = globalwpw.settings.obj.qcld_openrouter_prepend_content;
1509 1757 var customMessage = customprepend ? customprepend + ' ' + msg : msg;
@@ -1508,9 +1756,11 @@
1508 1756 var customprepend = globalwpw.settings.obj.qcld_openrouter_prepend_content;
1509 1757 var customMessage = customprepend ? customprepend + ' ' + msg : msg;
1510 1758 customMessage = customAappend ? customMessage + ' ' + customAappend : customMessage;
1511 1759
1512 - var data = {'action':'qcld_gemini_response','name':globalwpw.hasNameCookie,'keyword':customMessage, nonce: qcld_chatbot_obj.nonce};
1760 + qcldPushAiContext('user', customMessage);
1761 +
1762 + var data = {'action':'qcld_gemini_response','name':globalwpw.hasNameCookie,'keyword':customMessage, 'active_ai_action':(globalwpw.active_ai_action || ""), nonce: qcld_chatbot_obj.nonce, ai_history: JSON.stringify(globalwpw.aiContext)};
1513 1763 wpwKits.ajax(data).done(function (res) {
1514 1764 if( res == '' || typeof res === 'object' ){
1515 1765 var json = res;
1516 1766 }if(typeof res === 'string'){
@@ -1517,14 +1767,29 @@
1517 1767 var json = $.parseJSON(res);
1518 1768 }
1519 1769
1520 1770 if(json.status=='success'){
1771 + qcldPushAiContext('assistant', json.message);
1521 1772 var serviceOffer=wpwKits.randomMsg(globalwpw.settings.obj.support_option_again);
1522 1773
1523 1774 setTimeout(function(){
1775 + var isFormInProgress = qcldIsAiFormInProgress();
1776 + if (json.message && json.message.indexOf('__AI_FORM_IN_PROGRESS__') !== -1) {
1777 + isFormInProgress = true;
1778 + json.message = json.message.replace(/__AI_FORM_IN_PROGRESS__/g, '').trim();
1779 + }
1780 + if (json.message && (json.message.indexOf('AI_FORM_DATA') !== -1 || json.message.indexOf('ai-form-summary-card') !== -1)) {
1781 + isFormInProgress = false;
1782 + if (typeof globalwpw !== 'undefined') {
1783 + globalwpw.aiContext = [];
1784 + globalwpw.active_ai_action = '';
1785 + globalwpw.qcld_ai_form_persisted_state = false;
1786 + }
1787 + }
1788 +
1524 1789 wpwMsg.single(json.message);
1525 1790
1526 - if((globalwpw.settings.obj.qcld_disable_repited_startmenu != "1")){
1791 + if(!isFormInProgress && (globalwpw.settings.obj.qcld_disable_repited_startmenu != "1")){
1527 1792 if((globalwpw.settings.obj.qcld_disable_repited_startmenu != "1") && ((globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1))){
1528 1793 setTimeout(function(){
1529 1794 var serviceOffer=wpwKits.randomMsg(globalwpw.settings.obj.support_option_again);
1530 1795 if((globalwpw.settings.obj.qcld_disable_start_menu != "1" && (globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1))){
@@ -1547,9 +1812,12 @@
1547 1812 }
1548 1813 if(globalwpw.settings.obj.grok_enabled == 1){
1549 1814 var customMessage = msg;
1550 1815 customMessage = customAappend ? customMessage + ' ' + customAappend : customMessage;
1551 - var data = {'action':'qcld_grok_response','name':globalwpw.hasNameCookie,'keyword':customMessage};
1816 +
1817 + qcldPushAiContext('user', customMessage);
1818 +
1819 + var data = {'action':'qcld_grok_response','name':globalwpw.hasNameCookie,'keyword':customMessage, 'active_ai_action':(globalwpw.active_ai_action || ""), nonce: qcld_chatbot_obj.nonce, ai_history: JSON.stringify(globalwpw.aiContext)};
1552 1820 wpwKits.ajax(data).done(function (res) {
1553 1821 if( res == '' || typeof res === 'object' ){
1554 1822 var json = res;
1555 1823 }if(typeof res === 'string'){
@@ -1556,14 +1824,29 @@
1556 1824 var json = $.parseJSON(res);
1557 1825 }
1558 1826
1559 1827 if(json.status=='success'){
1828 + qcldPushAiContext('assistant', json.message);
1560 1829 var serviceOffer=wpwKits.randomMsg(globalwpw.settings.obj.support_option_again);
1561 1830
1562 1831 setTimeout(function(){
1832 + var isFormInProgress = qcldIsAiFormInProgress();
1833 + if (json.message && json.message.indexOf('__AI_FORM_IN_PROGRESS__') !== -1) {
1834 + isFormInProgress = true;
1835 + json.message = json.message.replace(/__AI_FORM_IN_PROGRESS__/g, '').trim();
1836 + }
1837 + if (json.message && (json.message.indexOf('AI_FORM_DATA') !== -1 || json.message.indexOf('ai-form-summary-card') !== -1)) {
1838 + isFormInProgress = false;
1839 + if (typeof globalwpw !== 'undefined') {
1840 + globalwpw.aiContext = [];
1841 + globalwpw.active_ai_action = '';
1842 + globalwpw.qcld_ai_form_persisted_state = false;
1843 + }
1844 + }
1845 +
1563 1846 wpwMsg.single(json.message);
1564 1847
1565 - if((globalwpw.settings.obj.qcld_disable_repited_startmenu != "1")){
1848 + if(!isFormInProgress && (globalwpw.settings.obj.qcld_disable_repited_startmenu != "1")){
1566 1849 if((globalwpw.settings.obj.qcld_disable_repited_startmenu != "1") && ((globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1))){
1567 1850 setTimeout(function(){
1568 1851 var serviceOffer=wpwKits.randomMsg(globalwpw.settings.obj.support_option_again);
1569 1852 if((globalwpw.settings.obj.qcld_disable_start_menu != "1" && (globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1))){
@@ -1589,9 +1872,9 @@
1589 1872 msg1 = wpwKits.filterStopWords(msg);
1590 1873 var data = {'action':'wpbo_search_site','name':globalwpw.hasNameCookie,'keyword':msg1, 'security':wp_chatbot_obj.ajax_nonce};
1591 1874 wpwKits.ajax(data).done(function (res) {
1592 1875 var json=$.parseJSON(res);
1593 - $(globalwpw.settings.messageContainer).find('.wp-chatbot-msg:last .qcld-like-dislike-icon').css('display', 'block');
1876 + if(!qcldIsAiFormInProgress()) { $(globalwpw.settings.messageContainer).find('.wp-chatbot-msg:last .qcld-like-dislike-icon').css('display', 'block'); }
1594 1877
1595 1878 if(json.status=='success'){
1596 1879 if((globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1)){
1597 1880 wpwMsg.triple_nobg( wp_chatbot_obj.found_result_message,json.html,'<span class="qcld-chatbot-wildcard" data-wildcart="back">' + wpwKits.randomMsg(globalwpw.settings.obj.back_to_start) + '</span>' );
@@ -1597,9 +1880,9 @@
1597 1880 wpwMsg.triple_nobg( wp_chatbot_obj.found_result_message,json.html,'<span class="qcld-chatbot-wildcard" data-wildcart="back">' + wpwKits.randomMsg(globalwpw.settings.obj.back_to_start) + '</span>' );
1598 1881 }else{
1599 1882 wpwMsg.double_nobg( wp_chatbot_obj.found_result_message,json.html);
1600 1883 }
1601 - }else if( (globalwpw.settings.obj.openai_enabled == 1) || (wp_chatbot_obj.openai_enabled == 1) || (globalwpw.settings.obj.openrouter_enabled == 1) || (wp_chatbot_obj.openrouter_enabled == 1) || (globalwpw.settings.obj.gemini_enabled == 1) || (wp_chatbot_obj.gemini_enabled == 1) || (globalwpw.settings.obj.grok_enabled == 1) || (wp_chatbot_obj.grok_enabled == 1) ){
1884 + }else if( (globalwpw.settings.obj.openai_enabled == 1) || (wp_chatbot_obj.openai_enabled == 1) || (globalwpw.settings.obj.openrouter_enabled == 1) || (wp_chatbot_obj.openrouter_enabled == 1) || (globalwpw.settings.obj.gemini_enabled == 1) || (wp_chatbot_obj.gemini_enabled == 1) || (globalwpw.settings.obj.grok_enabled == 1) || (wp_chatbot_obj.grok_enabled == 1) || (wp_chatbot_obj.claude_enabled == 1) || (globalwpw.settings.obj.claude_enabled == 1) ){
1602 1885 if($(globalwpw.settings.messageLastChild+' .wp-chatbot-comment-loader').length==0){
1603 1886 $(globalwpw.settings.messageContainer).append(wpwKits.botPreloader());
1604 1887 }
1605 1888 wpwTree.openai_reply(msg);
@@ -1760,9 +2043,9 @@
1760 2043 }, globalwpw.settings.preLoadingTime*2);
1761 2044 }else{
1762 2045
1763 2046 setTimeout(function(){
1764 - $(globalwpw.settings.messageContainer).find('.wp-chatbot-msg:last .qcld-like-dislike-icon').css('display', 'block');
2047 + if(!qcldIsAiFormInProgress()) { $(globalwpw.settings.messageContainer).find('.wp-chatbot-msg:last .qcld-like-dislike-icon').css('display', 'block'); }
1765 2048 if((globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1)){
1766 2049 wpwMsg.single_nobg('<span class="qcld-chatbot-wildcard qcld_back_to_start" data-wildcart="back">' + wpwKits.randomMsg(globalwpw.settings.obj.back_to_start) + '</span>');
1767 2050 }
1768 2051 }, globalwpw.settings.preLoadingTime*2);
@@ -1800,9 +2083,9 @@
1800 2083
1801 2084 if(wp_chatbot_obj.disable_site_search != 1){
1802 2085 wpwTree.site_search(msg)
1803 2086 }
1804 - else if( (globalwpw.settings.obj.openai_enabled == 1) || (wp_chatbot_obj.openai_enabled == 1) || (globalwpw.settings.obj.openrouter_enabled == 1) || (wp_chatbot_obj.openrouter_enabled == 1) || (globalwpw.settings.obj.gemini_enabled == 1) || (wp_chatbot_obj.gemini_enabled == 1) || (globalwpw.settings.obj.grok_enabled == 1) || (wp_chatbot_obj.grok_enabled == 1) ){
2087 + else if( (globalwpw.settings.obj.openai_enabled == 1) || (wp_chatbot_obj.openai_enabled == 1) || (globalwpw.settings.obj.openrouter_enabled == 1) || (wp_chatbot_obj.openrouter_enabled == 1) || (globalwpw.settings.obj.gemini_enabled == 1) || (wp_chatbot_obj.gemini_enabled == 1) || (globalwpw.settings.obj.grok_enabled == 1) || (wp_chatbot_obj.grok_enabled == 1) || (wp_chatbot_obj.claude_enabled == 1) || (globalwpw.settings.obj.claude_enabled == 1) ){
1805 2088 wpwTree.openai_reply(msg)
1806 2089 }else{
1807 2090 wpwMsg.single(globalwpw.settings.obj.empty_filter_msg);
1808 2091 }
@@ -1833,9 +2116,9 @@
1833 2116 // wpwMsg.double_nobg(serviceOffer, globalwpw.wildcards);
1834 2117 // }, globalwpw.settings.preLoadingTime*2);
1835 2118 // }else{
1836 2119 setTimeout(function(){
1837 - $(globalwpw.settings.messageContainer).find('.wp-chatbot-msg:last .qcld-like-dislike-icon').css('display', 'block');
2120 + if(!qcldIsAiFormInProgress()) { $(globalwpw.settings.messageContainer).find('.wp-chatbot-msg:last .qcld-like-dislike-icon').css('display', 'block'); }
1838 2121 if( (globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1)){
1839 2122 wpwMsg.single_nobg('<span class="qcld-chatbot-wildcard qcld_back_to_start" data-wildcart="back">' + wpwKits.randomMsg(globalwpw.settings.obj.back_to_start) + '</span>');
1840 2123 }
1841 2124 }, globalwpw.settings.preLoadingTime*2);
@@ -1899,9 +2182,9 @@
1899 2182 // wpwMsg.double_nobg(serviceOffer,globalwpw.wildcards);
1900 2183 // },globalwpw.settings.preLoadingTime)
1901 2184 // }else{
1902 2185 setTimeout(function(){
1903 - $(globalwpw.settings.messageContainer).find('.wp-chatbot-msg:last .qcld-like-dislike-icon').css('display', 'block');
2186 + if(!qcldIsAiFormInProgress()) { $(globalwpw.settings.messageContainer).find('.wp-chatbot-msg:last .qcld-like-dislike-icon').css('display', 'block'); }
1904 2187 if((globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1)){
1905 2188 wpwMsg.single_nobg('<span class="qcld-chatbot-wildcard qcld_back_to_start" data-wildcart="back">' + wpwKits.randomMsg(globalwpw.settings.obj.back_to_start) + '</span>');
1906 2189 }
1907 2190 }, globalwpw.settings.preLoadingTime*2);
@@ -1970,9 +2253,9 @@
1970 2253 // wpwMsg.double_nobg(serviceOffer,globalwpw.wildcards);
1971 2254 // },globalwpw.settings.preLoadingTime)
1972 2255 // }else{
1973 2256 setTimeout(function(){
1974 - $(globalwpw.settings.messageContainer).find('.wp-chatbot-msg:last .qcld-like-dislike-icon').css('display', 'block');
2257 + if(!qcldIsAiFormInProgress()) { $(globalwpw.settings.messageContainer).find('.wp-chatbot-msg:last .qcld-like-dislike-icon').css('display', 'block'); }
1975 2258 if((globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1)){
1976 2259 wpwMsg.single_nobg('<span class="qcld-chatbot-wildcard qcld_back_to_start" data-wildcart="back">' + wpwKits.randomMsg(globalwpw.settings.obj.back_to_start) + '</span>');
1977 2260 }
1978 2261 }, globalwpw.settings.preLoadingTime*2);
@@ -2301,8 +2584,15 @@
2301 2584 wpwAction.bot(wp_chatbot_obj.sys_key_help.toLowerCase());
2302 2585 //keeping value in localstorage
2303 2586 localStorage.setItem("wildCard", globalwpw.wildCard);
2304 2587 }
2588 + if(wildcardData=='ai_form'){
2589 + globalwpw.wildCard=0;
2590 + globalwpw.active_ai_action = shooperChoice;
2591 + globalwpw.qcld_ai_form_persisted_state = true;
2592 + wpwAction.bot(shooperChoice);
2593 + localStorage.setItem("wildCard", globalwpw.wildCard);
2594 + }
2305 2595 if(wildcardData=='messenger'){
2306 2596 var url='https://www.messenger.com/t/'+globalwpw.settings.obj.fb_page_id;
2307 2597 var win = window.open(url, '_blank');
2308 2598 win.focus();
@@ -2351,8 +2641,20 @@
2351 2641 var number = Math.random() // 0.9394456857981651
2352 2642 number.toString(36); // '0.xtis06h6'
2353 2643 var id = number.toString(36).substr(2); // 'xtis06h6'
2354 2644 localStorage.setItem('botsessionid', id);
2645 + // Keep mobile/responsive chat full-width after reset (do not shrink to content)
2646 + if ($(window).width() <= 480 && globalwpw.settings.obj.mobile_full_screen == 1) {
2647 + $('#wp-chatbot-chat-container').addClass('wp-chatbot-mobile-fs-open').css({
2648 + 'bottom': '0',
2649 + 'left': '0',
2650 + 'right': '0',
2651 + 'width': '100%',
2652 + 'max-width': '100%'
2653 + });
2654 + $('#wp-chatbot-board-container').css({'width': '100%', 'max-width': '100%'});
2655 + $('.slimScrollDiv').css({'width': '100%', 'max-width': '100%'});
2656 + }
2355 2657 wpwWelcome.greeting();
2356 2658 });
2357 2659 $(document).on('click','.qcld-chatbot-formanswer',function(e){
2358 2660 e.preventDefault();
@@ -2892,256 +3194,256 @@
2892 3194 wildcardsShowTime:5000,
2893 3195 }
2894 3196
2895 3197 document.addEventListener("click", function(e) {
2896 - // Toggle share menu
2897 - if (e.target.closest(".share-toggle")) {
2898 - e.preventDefault();
2899 - let menu = e.target.closest(".qcld-share").querySelector(".share-menu");
2900 - menu.style.display = menu.style.display === "none" ? "block" : "none";
2901 - }
3198 + // Toggle share menu
3199 + if (e.target.closest(".share-toggle")) {
3200 + e.preventDefault();
3201 + let menu = e.target.closest(".qcld-share").querySelector(".share-menu");
3202 + menu.style.display = menu.style.display === "none" ? "block" : "none";
3203 + }
2902 3204
2903 - // Find message content dynamically
2904 - let msgItem = e.target.closest(".wp-chatbot-msg");
2905 - if (!msgItem) return;
3205 + // Find message content dynamically
3206 + let msgItem = e.target.closest(".wp-chatbot-msg");
3207 + if (!msgItem) return;
2906 3208
2907 - let msgContent = msgItem.querySelector(".wp-chatbot-paragraph");
2908 - if (!msgContent) return;
3209 + let msgContent = msgItem.querySelector(".wp-chatbot-paragraph");
3210 + if (!msgContent) return;
2909 3211
2910 - // Extract plain text
2911 - let msgText = msgContent.innerText.trim();
2912 - let shareSite = window.location.origin; // Get the site URL dynamically (e.g., "https://www.example.com")
2913 - // Ensure a trailing slash if it's meant to represent the root of the site
2914 - if (!shareSite.endsWith('/')) {
2915 - shareSite += '/';
2916 - }
2917 - let fullMsg = `${msgText}\n-- ${shareSite}`;
2918 - let encodedMsg = encodeURIComponent(fullMsg);
3212 + // Extract plain text
3213 + let msgText = msgContent.innerText.trim();
3214 + let shareSite = window.location.origin; // Get the site URL dynamically (e.g., "https://www.example.com")
3215 + // Ensure a trailing slash if it's meant to represent the root of the site
3216 + if (!shareSite.endsWith('/')) {
3217 + shareSite += '/';
3218 + }
3219 + let fullMsg = `${msgText}\n-- ${shareSite}`;
3220 + let encodedMsg = encodeURIComponent(fullMsg);
2919 3221
2920 - // Facebook (requires App ID)
2921 - if (e.target.closest(".share-fb")) {
2922 - e.preventDefault();
3222 + // Facebook (requires App ID)
3223 + if (e.target.closest(".share-fb")) {
3224 + e.preventDefault();
2923 3225
2924 - // You must include a URL even if you're just sharing text
2925 - const shareURL = encodeURIComponent(window.location.href); // or your chatbot page URL
2926 - const fbShareURL = "https://www.facebook.com/sharer/sharer.php?u=" + shareURL + "&quote=" + encodedMsg;
3226 + // You must include a URL even if you're just sharing text
3227 + const shareURL = encodeURIComponent(window.location.href); // or your chatbot page URL
3228 + const fbShareURL = "https://www.facebook.com/sharer/sharer.php?u=" + shareURL + "&quote=" + encodedMsg;
2927 3229
2928 - window.open(fbShareURL, "_blank", "width=600,height=400");
2929 - }
3230 + window.open(fbShareURL, "_blank", "width=600,height=400");
3231 + }
2930 3232
2931 - // WhatsApp
2932 - if (e.target.closest(".share-wa")) {
2933 - e.preventDefault();
2934 - window.open("https://wa.me/?text=" + encodedMsg, "_blank");
2935 - }
3233 + // WhatsApp
3234 + if (e.target.closest(".share-wa")) {
3235 + e.preventDefault();
3236 + window.open("https://wa.me/?text=" + encodedMsg, "_blank");
3237 + }
2936 3238
2937 - // X (Twitter)
2938 - if (e.target.closest(".share-x")) {
2939 - e.preventDefault();
2940 - window.open("https://twitter.com/intent/tweet?text=" + encodedMsg, "_blank");
2941 - }
3239 + // X (Twitter)
3240 + if (e.target.closest(".share-x")) {
3241 + e.preventDefault();
3242 + window.open("https://twitter.com/intent/tweet?text=" + encodedMsg, "_blank");
3243 + }
2942 3244
2943 - // Email
2944 - if (e.target.closest(".share-email")) {
2945 - e.preventDefault();
2946 - window.location.href = "mailto:?subject=Chatbot Message&body=" + encodedMsg;
2947 - }
2948 -});
2949 -jQuery(document).ready(function($) {
3245 + // Email
3246 + if (e.target.closest(".share-email")) {
3247 + e.preventDefault();
3248 + window.location.href = "mailto:?subject=Chatbot Message&body=" + encodedMsg;
3249 + }
3250 + });
3251 + jQuery(document).ready(function($) {
2950 3252
2951 - let reportedMessage = "";
2952 - let modalElements = {}; // Store references to modal elements
2953 - let isModalInitialized = false; // Flag to prevent re-attaching listeners
3253 + let reportedMessage = "";
3254 + let modalElements = {}; // Store references to modal elements
3255 + let isModalInitialized = false; // Flag to prevent re-attaching listeners
2954 3256
2955 - /**
2956 - * Initializes the report modal by finding its elements and attaching event listeners.
2957 - * This function is designed to be called only once, the first time the report icon is clicked,
2958 - * to ensure all DOM elements are available.
2959 - * @returns {boolean} True if initialization was successful, false otherwise.
2960 - */
2961 - function setupReportModal() {
2962 - if (isModalInitialized) {
2963 - return true; // Already initialized
2964 - }
3257 + /**
3258 + * Initializes the report modal by finding its elements and attaching event listeners.
3259 + * This function is designed to be called only once, the first time the report icon is clicked,
3260 + * to ensure all DOM elements are available.
3261 + * @returns {boolean} True if initialization was successful, false otherwise.
3262 + */
3263 + function setupReportModal() {
3264 + if (isModalInitialized) {
3265 + return true; // Already initialized
3266 + }
2965 3267
2966 - const modal = document.getElementById("wpbot-report-modal");
2967 - const emailInput = document.getElementById("wpbot-report-email");
2968 - const textInput = document.getElementById("wpbot-report-text");
2969 - const cancelBtn = document.getElementById("wpbot-report-cancel");
2970 - const reportForm = document.getElementById("wpbot-report-form");
3268 + const modal = document.getElementById("wpbot-report-modal");
3269 + const emailInput = document.getElementById("wpbot-report-email");
3270 + const textInput = document.getElementById("wpbot-report-text");
3271 + const cancelBtn = document.getElementById("wpbot-report-cancel");
3272 + const reportForm = document.getElementById("wpbot-report-form");
2971 3273
2972 - if (!modal || !emailInput || !textInput || !cancelBtn || !reportForm) {
2973 - console.error("❌ One or more modal elements missing in DOM. Cannot initialize report modal.");
2974 - return false; // Initialization failed
2975 - }
3274 + if (!modal || !emailInput || !textInput || !cancelBtn || !reportForm) {
3275 + console.error("❌ One or more modal elements missing in DOM. Cannot initialize report modal.");
3276 + return false; // Initialization failed
3277 + }
2976 3278
2977 - // Store references to the elements
2978 - modalElements = { modal, emailInput, textInput, cancelBtn, reportForm };
3279 + // Store references to the elements
3280 + modalElements = { modal, emailInput, textInput, cancelBtn, reportForm };
2979 3281
2980 - // Attach event listener for the cancel button
2981 - modalElements.cancelBtn.addEventListener("click", function() {
2982 - modalElements.modal.style.display = "none";
2983 - });
3282 + // Attach event listener for the cancel button
3283 + modalElements.cancelBtn.addEventListener("click", function() {
3284 + modalElements.modal.style.display = "none";
3285 + });
2984 3286
2985 - // Attach event listener for the form submission
2986 - modalElements.reportForm.addEventListener("submit", function(e) {
2987 - e.preventDefault();
3287 + // Attach event listener for the form submission
3288 + modalElements.reportForm.addEventListener("submit", function(e) {
3289 + e.preventDefault();
2988 3290
2989 - let email = modalElements.emailInput.value.trim();
2990 - let reportText = modalElements.textInput.value.trim();
3291 + let email = modalElements.emailInput.value.trim();
3292 + let reportText = modalElements.textInput.value.trim();
2991 3293
2992 - if (!email || !reportText) {
2993 - alert("Please fill in both fields.");
2994 - return;
2995 - }
3294 + if (!email || !reportText) {
3295 + alert("Please fill in both fields.");
3296 + return;
3297 + }
2996 3298
2997 - // Save email to localStorage
2998 - localStorage.setItem("shopperemail", email);
3299 + // Save email to localStorage
3300 + localStorage.setItem("shopperemail", email);
2999 3301
3000 - // Build meta info for the report
3001 - let meta = "Source URL: " + window.location.href + " | User Agent: " + navigator.userAgent;
3302 + // Build meta info for the report
3303 + let meta = "Source URL: " + window.location.href + " | User Agent: " + navigator.userAgent;
3002 3304
3003 - // Send report via AJAX
3004 - $.post(wp_chatbot_obj.ajax_url, {
3005 - action: "wpbot_save_report",
3006 - user_id: globalwpw.settings.obj.current_user_id,
3007 - conversation_id: 3,
3008 - message: reportedMessage, // Use the message captured when the icon was clicked
3009 - report_text: reportText,
3010 - email: email,
3011 - meta_info: meta
3012 - }, function(response) {
3013 - if (response.success) {
3014 - console.log("Report sent successfully.");
3015 - // The original line `jQuery(e.target.closest(".fa fa-commenting-o")).addClass("submitted");`
3016 - // was incorrect as `e.target` here refers to the form, not the original icon.
3017 - // To mark the icon, a reference to it would need to be stored when it was clicked.
3018 - } else {
3019 - console.log("Failed to send report.");
3020 - }
3021 - modalElements.modal.style.display = "none"; // Hide modal after submission
3022 - });
3023 - });
3305 + // Send report via AJAX
3306 + $.post(wp_chatbot_obj.ajax_url, {
3307 + action: "wpbot_save_report",
3308 + user_id: globalwpw.settings.obj.current_user_id,
3309 + conversation_id: 3,
3310 + message: reportedMessage, // Use the message captured when the icon was clicked
3311 + report_text: reportText,
3312 + email: email,
3313 + meta_info: meta
3314 + }, function(response) {
3315 + if (response.success) {
3316 + console.log("Report sent successfully.");
3317 + // The original line `jQuery(e.target.closest(".fa fa-commenting-o")).addClass("submitted");`
3318 + // was incorrect as `e.target` here refers to the form, not the original icon.
3319 + // To mark the icon, a reference to it would need to be stored when it was clicked.
3320 + } else {
3321 + console.log("Failed to send report.");
3322 + }
3323 + modalElements.modal.style.display = "none"; // Hide modal after submission
3324 + });
3325 + });
3024 3326
3025 - isModalInitialized = true; // Mark as initialized
3026 - return true; // Initialization successful
3027 - }
3327 + isModalInitialized = true; // Mark as initialized
3328 + return true; // Initialization successful
3329 + }
3028 3330
3029 - // Event listener for clicks on the document to detect report icon clicks
3030 - document.addEventListener("click", function(e) {
3031 - if (e.target.closest(".fa-commenting-o")) {
3032 - e.preventDefault();
3033 - console.log("Report icon clicked");
3331 + // Event listener for clicks on the document to detect report icon clicks
3332 + document.addEventListener("click", function(e) {
3333 + if (e.target.closest(".fa-commenting-o")) {
3334 + e.preventDefault();
3335 + console.log("Report icon clicked");
3034 3336
3035 - // Attempt to initialize the modal elements and listeners if not already done.
3036 - // This ensures elements are looked up only when needed, making it more robust
3037 - // against timing issues if the modal HTML is loaded asynchronously or late.
3038 - if (!setupReportModal()) {
3039 - return; // If initialization failed (elements not found), stop here.
3040 - }
3337 + // Attempt to initialize the modal elements and listeners if not already done.
3338 + // This ensures elements are looked up only when needed, making it more robust
3339 + // against timing issues if the modal HTML is loaded asynchronously or late.
3340 + if (!setupReportModal()) {
3341 + return; // If initialization failed (elements not found), stop here.
3342 + }
3041 3343
3042 - // Now that we are sure modal elements are available, proceed to open the modal.
3043 - let msgItem = e.target.closest(".wp-chatbot-msg");
3044 - if (msgItem) {
3045 - reportedMessage = msgItem.querySelector(".wp-chatbot-paragraph").innerText.trim();
3046 - } else {
3047 - reportedMessage = ""; // Fallback if message item not found
3048 - }
3344 + // Now that we are sure modal elements are available, proceed to open the modal.
3345 + let msgItem = e.target.closest(".wp-chatbot-msg");
3346 + if (msgItem) {
3347 + reportedMessage = msgItem.querySelector(".wp-chatbot-paragraph").innerText.trim();
3348 + } else {
3349 + reportedMessage = ""; // Fallback if message item not found
3350 + }
3049 3351
3050 - // Prefill email from localStorage if available
3051 - let shopperEmail = localStorage.getItem("shopperemail");
3052 - modalElements.emailInput.value = shopperEmail ? shopperEmail : "";
3352 + // Prefill email from localStorage if available
3353 + let shopperEmail = localStorage.getItem("shopperemail");
3354 + modalElements.emailInput.value = shopperEmail ? shopperEmail : "";
3053 3355
3054 - // Clear previous report text
3055 - modalElements.textInput.value = "";
3056 -
3057 - // Display the modal
3058 - modalElements.modal.style.display = "block";
3059 - }
3060 - });
3061 -});
3062 -jQuery(document).on('click', '.qc_wpbot_chat_link', function (e) {
3063 - console.log("Chat link clicked");
3064 - e.preventDefault();
3065 - jQuery("#wp-chatbot-ball").trigger("click");
3066 - jQuery("#wp-chatbot-chat-container").show();
3067 -});
3068 -document.addEventListener("click", function (e) {
3069 - const likeIcon = e.target.closest(".dashicons-thumbs-up");
3070 - const dislikeIcon = e.target.closest(".dashicons-thumbs-down");
3356 + // Clear previous report text
3357 + modalElements.textInput.value = "";
3358 +
3359 + // Display the modal
3360 + modalElements.modal.style.display = "block";
3361 + }
3362 + });
3363 + });
3364 + jQuery(document).on('click', '.qc_wpbot_chat_link', function (e) {
3365 + console.log("Chat link clicked");
3366 + e.preventDefault();
3367 + jQuery("#wp-chatbot-ball").trigger("click");
3368 + jQuery("#wp-chatbot-chat-container").show();
3369 + });
3370 + document.addEventListener("click", function (e) {
3371 + const likeIcon = e.target.closest(".dashicons-thumbs-up");
3372 + const dislikeIcon = e.target.closest(".dashicons-thumbs-down");
3071 3373
3072 - if (!likeIcon && !dislikeIcon) return; // only handle like/dislike clicks
3073 - e.preventDefault();
3374 + if (!likeIcon && !dislikeIcon) return; // only handle like/dislike clicks
3375 + e.preventDefault();
3074 3376
3075 - const msgItem = e.target.closest(".wp-chatbot-msg");
3076 - const feedbackContainer = jQuery(msgItem).find(".qcld-like-dislike-icon");
3077 - const likeAnchor = jQuery(msgItem).find(".dashicons-thumbs-up").parent("a");
3078 - const dislikeAnchor = jQuery(msgItem).find(".dashicons-thumbs-down").parent("a");
3377 + const msgItem = e.target.closest(".wp-chatbot-msg");
3378 + const feedbackContainer = jQuery(msgItem).find(".qcld-like-dislike-icon");
3379 + const likeAnchor = jQuery(msgItem).find(".dashicons-thumbs-up").parent("a");
3380 + const dislikeAnchor = jQuery(msgItem).find(".dashicons-thumbs-down").parent("a");
3079 3381
3080 - let feedbackType = "";
3081 - let isToggleOff = false;
3382 + let feedbackType = "";
3383 + let isToggleOff = false;
3082 3384
3083 - if (likeIcon) {
3084 - // If like is already active → toggle it off
3085 - if (likeAnchor.hasClass("liked")) {
3086 - likeAnchor.removeClass("liked");
3087 - likeIcon.classList.remove("liked");
3088 - isToggleOff = true;
3089 - } else {
3090 - // Activate like, deactivate dislike
3091 - likeAnchor.addClass("liked");
3092 - likeIcon.classList.add("liked");
3093 - dislikeAnchor.removeClass("disliked");
3094 - jQuery(msgItem).find(".dashicons-thumbs-down").removeClass("disliked");
3095 - feedbackType = "like";
3096 - }
3097 - } else if (dislikeIcon) {
3098 - // If dislike is already active → toggle it off
3099 - if (dislikeAnchor.hasClass("disliked")) {
3100 - dislikeAnchor.removeClass("disliked");
3101 - dislikeIcon.classList.remove("disliked");
3102 - isToggleOff = true;
3103 - } else {
3104 - // Activate dislike, deactivate like
3105 - dislikeAnchor.addClass("disliked");
3106 - dislikeIcon.classList.add("disliked");
3107 - likeAnchor.removeClass("liked");
3108 - jQuery(msgItem).find(".fa-thumbs-o-up").removeClass("liked");
3109 - feedbackType = "dislike";
3110 - }
3111 - }
3385 + if (likeIcon) {
3386 + // If like is already active → toggle it off
3387 + if (likeAnchor.hasClass("liked")) {
3388 + likeAnchor.removeClass("liked");
3389 + likeIcon.classList.remove("liked");
3390 + isToggleOff = true;
3391 + } else {
3392 + // Activate like, deactivate dislike
3393 + likeAnchor.addClass("liked");
3394 + likeIcon.classList.add("liked");
3395 + dislikeAnchor.removeClass("disliked");
3396 + jQuery(msgItem).find(".dashicons-thumbs-down").removeClass("disliked");
3397 + feedbackType = "like";
3398 + }
3399 + } else if (dislikeIcon) {
3400 + // If dislike is already active → toggle it off
3401 + if (dislikeAnchor.hasClass("disliked")) {
3402 + dislikeAnchor.removeClass("disliked");
3403 + dislikeIcon.classList.remove("disliked");
3404 + isToggleOff = true;
3405 + } else {
3406 + // Activate dislike, deactivate like
3407 + dislikeAnchor.addClass("disliked");
3408 + dislikeIcon.classList.add("disliked");
3409 + likeAnchor.removeClass("liked");
3410 + jQuery(msgItem).find(".fa-thumbs-o-up").removeClass("liked");
3411 + feedbackType = "dislike";
3412 + }
3413 + }
3112 3414
3113 - // 🔹 Only send feedback if toggled ON
3114 - if (!isToggleOff && feedbackType) {
3115 - let message = msgItem.querySelector(".wp-chatbot-paragraph").innerText.trim();
3116 - let meta =
3117 - "Source URL: " +
3118 - window.location.href +
3119 - " | User Agent: " +
3120 - navigator.userAgent;
3415 + // 🔹 Only send feedback if toggled ON
3416 + if (!isToggleOff && feedbackType) {
3417 + let message = msgItem.querySelector(".wp-chatbot-paragraph").innerText.trim();
3418 + let meta =
3419 + "Source URL: " +
3420 + window.location.href +
3421 + " | User Agent: " +
3422 + navigator.userAgent;
3121 3423
3122 - jQuery.post(
3123 - wp_chatbot_obj.ajax_url,
3124 - {
3125 - action: "wpbot_save_feedback",
3126 - user_id: globalwpw.settings.obj.current_user_id,
3127 - conversation_id: 3,
3128 - message: message,
3129 - feedback: feedbackType,
3130 - meta_info: meta,
3131 - },
3132 - function (response) {
3133 - if (response.success) {
3134 - console.log("Feedback saved:", feedbackType);
3424 + jQuery.post(
3425 + wp_chatbot_obj.ajax_url,
3426 + {
3427 + action: "wpbot_save_feedback",
3428 + user_id: globalwpw.settings.obj.current_user_id,
3429 + conversation_id: 3,
3430 + message: message,
3431 + feedback: feedbackType,
3432 + meta_info: meta,
3433 + },
3434 + function (response) {
3435 + if (response.success) {
3436 + console.log("Feedback saved:", feedbackType);
3437 + } else {
3438 + console.log("Failed to save feedback.");
3439 + }
3440 + }
3441 + );
3135 3442 } else {
3136 - console.log("Failed to save feedback.");
3443 + console.log("Feedback toggled off, not saving.");
3137 3444 }
3138 - }
3139 - );
3140 - } else {
3141 - console.log("Feedback toggled off, not saving.");
3142 - }
3143 -});
3445 + });
3144 3446
3145 3447 // ---------------------------------------------------------------------------
3146 3448 // Streaming helpers
3147 3449 // ---------------------------------------------------------------------------
@@ -3275,17 +3577,54 @@
3275 3577 var streamEnded = false;
3276 3578 var msgBuffer = '';
3277 3579
3278 3580 function finalize() {
3279 - // Replace raw typed text with markdown-formatted HTML
3581 + var finalMsg = msgBuffer;
3582 + var isFormInProgress = qcldIsAiFormInProgress();
3583 +
3584 + // The streaming endpoint never receives __AI_FORM_IN_PROGRESS__ markers
3585 + // (those are only added by the non-streaming PHP handler). So we also
3586 + // check whether the original request was sent with an active AI action.
3587 + if (dataObj.active_ai_action && dataObj.active_ai_action !== '') {
3588 + isFormInProgress = true;
3589 + }
3590 +
3591 + if (finalMsg.indexOf('__AI_FORM_IN_PROGRESS__') !== -1) {
3592 + isFormInProgress = true;
3593 + finalMsg = finalMsg.replace(/__AI_FORM_IN_PROGRESS__/g, '').trim();
3594 + }
3595 +
3596 + // qcldIsAiFormInProgress() above only reflects context from BEFORE this
3597 + // response. If this response itself carries the AI_FORM_DATA completion
3598 + // block, the form is done as of now regardless of that stale history.
3599 + var isFormCompletion = finalMsg.indexOf('AI_FORM_DATA') !== -1;
3600 + if (isFormCompletion) {
3601 + isFormInProgress = false;
3602 + }
3603 +
3604 + finalMsg = wpwMsg.oncommand_filter(finalMsg);
3605 +
3606 + if (typeof globalwpw !== 'undefined' && typeof globalwpw.aiContext !== 'undefined') {
3607 + globalwpw.aiContext.push({role: 'assistant', content: finalMsg});
3608 + if (globalwpw.aiContext.length > 10) {
3609 + globalwpw.aiContext.shift();
3610 + }
3611 + }
3612 +
3613 + // Replace raw typed text with formatted HTML
3280 3614 var $para = jQuery('.wp-chatbot-msg').last().find('.wp-chatbot-paragraph');
3281 - if (msgBuffer.trim()) {
3282 - $para.html(qcldParseMarkdown(msgBuffer));
3615 + if (finalMsg.trim()) {
3616 + $para.html(qcldParseMarkdown(finalMsg));
3283 3617 }
3284 - jQuery(globalwpw.settings.messageContainer)
3285 - .find('.wp-chatbot-msg:last .qcld-like-dislike-icon')
3286 - .css('display', 'block');
3287 - if((globalwpw.settings.obj.qcld_disable_repited_startmenu != "1") && ((globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1))){
3618 + // Reactions (like/dislike/report/share) don't apply to the form
3619 + // completion summary card itself, only to normal AI chat replies.
3620 + if (!isFormInProgress && !isFormCompletion) {
3621 + jQuery(globalwpw.settings.messageContainer)
3622 + .find('.wp-chatbot-msg:last .qcld-like-dislike-icon')
3623 + .css('display', 'block');
3624 + }
3625 +
3626 + if(!isFormInProgress && (globalwpw.settings.obj.qcld_disable_repited_startmenu != "1") && ((globalwpw.settings.obj.disable_back_to_start != '1' && globalwpw.settings.obj.skip_greetings_and_menu != 1))){
3288 3627 wpwMsg.single_nobg(
3289 3628 '<span class="qcld-chatbot-wildcard qcld_back_to_start" data-wildcart="back">' +
3290 3629 wpwKits.randomMsg(globalwpw.settings.obj.back_to_start) +
3291 3630 '</span>'
@@ -3309,8 +3648,16 @@
3309 3648
3310 3649 isTyping = true;
3311 3650 var nextChunk = queue.shift();
3312 3651 msgBuffer += nextChunk;
3652 +
3653 + // If we've started receiving the data block or internal flags, hide it from the UI!
3654 + // We use '__AI_' as the trigger so it stops rendering even partial tags like '__AI_FORM_DATA__' or '__AI_FORM_IN_PROGRESS__'.
3655 + if (msgBuffer.indexOf('__AI_') !== -1) {
3656 + isTyping = false;
3657 + processQueue();
3658 + return;
3659 + }
3313 3660
3314 3661 var $para = jQuery('.wp-chatbot-msg').last().find('.wp-chatbot-paragraph');
3315 3662 if ($para.length) {
3316 3663 $para.find('.wp-chatbot-comment-loader').remove();