PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.8.1
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.8.1
8.8.1 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 All 537 releases
← All changes | js/qcld-wp-chatbot-plugin.js +575 -240 8.6.6 → 8.8.1 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();
@@ -2904,256 +3194,256 @@
2904 3194 wildcardsShowTime:5000,
2905 3195 }
2906 3196
2907 3197 document.addEventListener("click", function(e) {
2908 - // Toggle share menu
2909 - if (e.target.closest(".share-toggle")) {
2910 - e.preventDefault();
2911 - let menu = e.target.closest(".qcld-share").querySelector(".share-menu");
2912 - menu.style.display = menu.style.display === "none" ? "block" : "none";
2913 - }
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 + }
2914 3204
2915 - // Find message content dynamically
2916 - let msgItem = e.target.closest(".wp-chatbot-msg");
2917 - if (!msgItem) return;
3205 + // Find message content dynamically
3206 + let msgItem = e.target.closest(".wp-chatbot-msg");
3207 + if (!msgItem) return;
2918 3208
2919 - let msgContent = msgItem.querySelector(".wp-chatbot-paragraph");
2920 - if (!msgContent) return;
3209 + let msgContent = msgItem.querySelector(".wp-chatbot-paragraph");
3210 + if (!msgContent) return;
2921 3211
2922 - // Extract plain text
2923 - let msgText = msgContent.innerText.trim();
2924 - let shareSite = window.location.origin; // Get the site URL dynamically (e.g., "https://www.example.com")
2925 - // Ensure a trailing slash if it's meant to represent the root of the site
2926 - if (!shareSite.endsWith('/')) {
2927 - shareSite += '/';
2928 - }
2929 - let fullMsg = `${msgText}\n-- ${shareSite}`;
2930 - 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);
2931 3221
2932 - // Facebook (requires App ID)
2933 - if (e.target.closest(".share-fb")) {
2934 - e.preventDefault();
3222 + // Facebook (requires App ID)
3223 + if (e.target.closest(".share-fb")) {
3224 + e.preventDefault();
2935 3225
2936 - // You must include a URL even if you're just sharing text
2937 - const shareURL = encodeURIComponent(window.location.href); // or your chatbot page URL
2938 - 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;
2939 3229
2940 - window.open(fbShareURL, "_blank", "width=600,height=400");
2941 - }
3230 + window.open(fbShareURL, "_blank", "width=600,height=400");
3231 + }
2942 3232
2943 - // WhatsApp
2944 - if (e.target.closest(".share-wa")) {
2945 - e.preventDefault();
2946 - window.open("https://wa.me/?text=" + encodedMsg, "_blank");
2947 - }
3233 + // WhatsApp
3234 + if (e.target.closest(".share-wa")) {
3235 + e.preventDefault();
3236 + window.open("https://wa.me/?text=" + encodedMsg, "_blank");
3237 + }
2948 3238
2949 - // X (Twitter)
2950 - if (e.target.closest(".share-x")) {
2951 - e.preventDefault();
2952 - window.open("https://twitter.com/intent/tweet?text=" + encodedMsg, "_blank");
2953 - }
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 + }
2954 3244
2955 - // Email
2956 - if (e.target.closest(".share-email")) {
2957 - e.preventDefault();
2958 - window.location.href = "mailto:?subject=Chatbot Message&body=" + encodedMsg;
2959 - }
2960 -});
2961 -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($) {
2962 3252
2963 - let reportedMessage = "";
2964 - let modalElements = {}; // Store references to modal elements
2965 - 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
2966 3256
2967 - /**
2968 - * Initializes the report modal by finding its elements and attaching event listeners.
2969 - * This function is designed to be called only once, the first time the report icon is clicked,
2970 - * to ensure all DOM elements are available.
2971 - * @returns {boolean} True if initialization was successful, false otherwise.
2972 - */
2973 - function setupReportModal() {
2974 - if (isModalInitialized) {
2975 - return true; // Already initialized
2976 - }
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 + }
2977 3267
2978 - const modal = document.getElementById("wpbot-report-modal");
2979 - const emailInput = document.getElementById("wpbot-report-email");
2980 - const textInput = document.getElementById("wpbot-report-text");
2981 - const cancelBtn = document.getElementById("wpbot-report-cancel");
2982 - 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");
2983 3273
2984 - if (!modal || !emailInput || !textInput || !cancelBtn || !reportForm) {
2985 - console.error("❌ One or more modal elements missing in DOM. Cannot initialize report modal.");
2986 - return false; // Initialization failed
2987 - }
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 + }
2988 3278
2989 - // Store references to the elements
2990 - modalElements = { modal, emailInput, textInput, cancelBtn, reportForm };
3279 + // Store references to the elements
3280 + modalElements = { modal, emailInput, textInput, cancelBtn, reportForm };
2991 3281
2992 - // Attach event listener for the cancel button
2993 - modalElements.cancelBtn.addEventListener("click", function() {
2994 - modalElements.modal.style.display = "none";
2995 - });
3282 + // Attach event listener for the cancel button
3283 + modalElements.cancelBtn.addEventListener("click", function() {
3284 + modalElements.modal.style.display = "none";
3285 + });
2996 3286
2997 - // Attach event listener for the form submission
2998 - modalElements.reportForm.addEventListener("submit", function(e) {
2999 - e.preventDefault();
3287 + // Attach event listener for the form submission
3288 + modalElements.reportForm.addEventListener("submit", function(e) {
3289 + e.preventDefault();
3000 3290
3001 - let email = modalElements.emailInput.value.trim();
3002 - let reportText = modalElements.textInput.value.trim();
3291 + let email = modalElements.emailInput.value.trim();
3292 + let reportText = modalElements.textInput.value.trim();
3003 3293
3004 - if (!email || !reportText) {
3005 - alert("Please fill in both fields.");
3006 - return;
3007 - }
3294 + if (!email || !reportText) {
3295 + alert("Please fill in both fields.");
3296 + return;
3297 + }
3008 3298
3009 - // Save email to localStorage
3010 - localStorage.setItem("shopperemail", email);
3299 + // Save email to localStorage
3300 + localStorage.setItem("shopperemail", email);
3011 3301
3012 - // Build meta info for the report
3013 - 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;
3014 3304
3015 - // Send report via AJAX
3016 - $.post(wp_chatbot_obj.ajax_url, {
3017 - action: "wpbot_save_report",
3018 - user_id: globalwpw.settings.obj.current_user_id,
3019 - conversation_id: 3,
3020 - message: reportedMessage, // Use the message captured when the icon was clicked
3021 - report_text: reportText,
3022 - email: email,
3023 - meta_info: meta
3024 - }, function(response) {
3025 - if (response.success) {
3026 - console.log("Report sent successfully.");
3027 - // The original line `jQuery(e.target.closest(".fa fa-commenting-o")).addClass("submitted");`
3028 - // was incorrect as `e.target` here refers to the form, not the original icon.
3029 - // To mark the icon, a reference to it would need to be stored when it was clicked.
3030 - } else {
3031 - console.log("Failed to send report.");
3032 - }
3033 - modalElements.modal.style.display = "none"; // Hide modal after submission
3034 - });
3035 - });
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 + });
3036 3326
3037 - isModalInitialized = true; // Mark as initialized
3038 - return true; // Initialization successful
3039 - }
3327 + isModalInitialized = true; // Mark as initialized
3328 + return true; // Initialization successful
3329 + }
3040 3330
3041 - // Event listener for clicks on the document to detect report icon clicks
3042 - document.addEventListener("click", function(e) {
3043 - if (e.target.closest(".fa-commenting-o")) {
3044 - e.preventDefault();
3045 - 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");
3046 3336
3047 - // Attempt to initialize the modal elements and listeners if not already done.
3048 - // This ensures elements are looked up only when needed, making it more robust
3049 - // against timing issues if the modal HTML is loaded asynchronously or late.
3050 - if (!setupReportModal()) {
3051 - return; // If initialization failed (elements not found), stop here.
3052 - }
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 + }
3053 3343
3054 - // Now that we are sure modal elements are available, proceed to open the modal.
3055 - let msgItem = e.target.closest(".wp-chatbot-msg");
3056 - if (msgItem) {
3057 - reportedMessage = msgItem.querySelector(".wp-chatbot-paragraph").innerText.trim();
3058 - } else {
3059 - reportedMessage = ""; // Fallback if message item not found
3060 - }
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 + }
3061 3351
3062 - // Prefill email from localStorage if available
3063 - let shopperEmail = localStorage.getItem("shopperemail");
3064 - modalElements.emailInput.value = shopperEmail ? shopperEmail : "";
3352 + // Prefill email from localStorage if available
3353 + let shopperEmail = localStorage.getItem("shopperemail");
3354 + modalElements.emailInput.value = shopperEmail ? shopperEmail : "";
3065 3355
3066 - // Clear previous report text
3067 - modalElements.textInput.value = "";
3068 -
3069 - // Display the modal
3070 - modalElements.modal.style.display = "block";
3071 - }
3072 - });
3073 -});
3074 -jQuery(document).on('click', '.qc_wpbot_chat_link', function (e) {
3075 - console.log("Chat link clicked");
3076 - e.preventDefault();
3077 - jQuery("#wp-chatbot-ball").trigger("click");
3078 - jQuery("#wp-chatbot-chat-container").show();
3079 -});
3080 -document.addEventListener("click", function (e) {
3081 - const likeIcon = e.target.closest(".dashicons-thumbs-up");
3082 - 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");
3083 3373
3084 - if (!likeIcon && !dislikeIcon) return; // only handle like/dislike clicks
3085 - e.preventDefault();
3374 + if (!likeIcon && !dislikeIcon) return; // only handle like/dislike clicks
3375 + e.preventDefault();
3086 3376
3087 - const msgItem = e.target.closest(".wp-chatbot-msg");
3088 - const feedbackContainer = jQuery(msgItem).find(".qcld-like-dislike-icon");
3089 - const likeAnchor = jQuery(msgItem).find(".dashicons-thumbs-up").parent("a");
3090 - 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");
3091 3381
3092 - let feedbackType = "";
3093 - let isToggleOff = false;
3382 + let feedbackType = "";
3383 + let isToggleOff = false;
3094 3384
3095 - if (likeIcon) {
3096 - // If like is already active → toggle it off
3097 - if (likeAnchor.hasClass("liked")) {
3098 - likeAnchor.removeClass("liked");
3099 - likeIcon.classList.remove("liked");
3100 - isToggleOff = true;
3101 - } else {
3102 - // Activate like, deactivate dislike
3103 - likeAnchor.addClass("liked");
3104 - likeIcon.classList.add("liked");
3105 - dislikeAnchor.removeClass("disliked");
3106 - jQuery(msgItem).find(".dashicons-thumbs-down").removeClass("disliked");
3107 - feedbackType = "like";
3108 - }
3109 - } else if (dislikeIcon) {
3110 - // If dislike is already active → toggle it off
3111 - if (dislikeAnchor.hasClass("disliked")) {
3112 - dislikeAnchor.removeClass("disliked");
3113 - dislikeIcon.classList.remove("disliked");
3114 - isToggleOff = true;
3115 - } else {
3116 - // Activate dislike, deactivate like
3117 - dislikeAnchor.addClass("disliked");
3118 - dislikeIcon.classList.add("disliked");
3119 - likeAnchor.removeClass("liked");
3120 - jQuery(msgItem).find(".fa-thumbs-o-up").removeClass("liked");
3121 - feedbackType = "dislike";
3122 - }
3123 - }
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 + }
3124 3414
3125 - // 🔹 Only send feedback if toggled ON
3126 - if (!isToggleOff && feedbackType) {
3127 - let message = msgItem.querySelector(".wp-chatbot-paragraph").innerText.trim();
3128 - let meta =
3129 - "Source URL: " +
3130 - window.location.href +
3131 - " | User Agent: " +
3132 - 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;
3133 3423
3134 - jQuery.post(
3135 - wp_chatbot_obj.ajax_url,
3136 - {
3137 - action: "wpbot_save_feedback",
3138 - user_id: globalwpw.settings.obj.current_user_id,
3139 - conversation_id: 3,
3140 - message: message,
3141 - feedback: feedbackType,
3142 - meta_info: meta,
3143 - },
3144 - function (response) {
3145 - if (response.success) {
3146 - 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 + );
3147 3442 } else {
3148 - console.log("Failed to save feedback.");
3443 + console.log("Feedback toggled off, not saving.");
3149 3444 }
3150 - }
3151 - );
3152 - } else {
3153 - console.log("Feedback toggled off, not saving.");
3154 - }
3155 -});
3445 + });
3156 3446
3157 3447 // ---------------------------------------------------------------------------
3158 3448 // Streaming helpers
3159 3449 // ---------------------------------------------------------------------------
@@ -3287,17 +3577,54 @@
3287 3577 var streamEnded = false;
3288 3578 var msgBuffer = '';
3289 3579
3290 3580 function finalize() {
3291 - // 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
3292 3614 var $para = jQuery('.wp-chatbot-msg').last().find('.wp-chatbot-paragraph');
3293 - if (msgBuffer.trim()) {
3294 - $para.html(qcldParseMarkdown(msgBuffer));
3615 + if (finalMsg.trim()) {
3616 + $para.html(qcldParseMarkdown(finalMsg));
3295 3617 }
3296 - jQuery(globalwpw.settings.messageContainer)
3297 - .find('.wp-chatbot-msg:last .qcld-like-dislike-icon')
3298 - .css('display', 'block');
3299 - 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))){
3300 3627 wpwMsg.single_nobg(
3301 3628 '<span class="qcld-chatbot-wildcard qcld_back_to_start" data-wildcart="back">' +
3302 3629 wpwKits.randomMsg(globalwpw.settings.obj.back_to_start) +
3303 3630 '</span>'
@@ -3321,8 +3648,16 @@
3321 3648
3322 3649 isTyping = true;
3323 3650 var nextChunk = queue.shift();
3324 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 + }
3325 3660
3326 3661 var $para = jQuery('.wp-chatbot-msg').last().find('.wp-chatbot-paragraph');
3327 3662 if ($para.length) {
3328 3663 $para.find('.wp-chatbot-comment-loader').remove();