| @@ -257,9 +257,9 @@ | ||
| 257 | 257 | |
| 258 | 258 | // NEW: Update approved URLs |
| 259 | 259 | this.updateApprovedUrls(testingData.approved_urls || []); |
| 260 | 260 | |
| 261 | - this.updateTopMatches(testingData.top_matches || [], testingData.similarity_threshold || 0.75, testingData.sources_used || 0, testingData.total_chunks_used || 0); | |
| 261 | + this.updateTopMatches(testingData.top_matches || [], testingData.similarity_threshold || 0.75); | |
| 262 | 262 | |
| 263 | 263 | // NEW: Update action matches |
| 264 | 264 | this.updateActionMatches(testingData.action_matches || []); |
| 265 | 265 | |
| @@ -391,9 +391,9 @@ | ||
| 391 | 391 | |
| 392 | 392 | actionsEl.innerHTML = html; |
| 393 | 393 | } |
| 394 | 394 | |
| 395 | - updateTopMatches(topMatches, threshold, sourcesUsed = 0, totalChunksUsed = 0) { | |
| 395 | + updateTopMatches(topMatches, threshold) { | |
| 396 | 396 | const scoresEl = this.panel.querySelector('#similarity-scores'); |
| 397 | 397 | |
| 398 | 398 | if (!topMatches || topMatches.length === 0) { |
| 399 | 399 | scoresEl.innerHTML = '<div class="no-data-message">No similarity data available</div>'; |
| @@ -437,15 +437,14 @@ | ||
| 437 | 437 | |
| 438 | 438 | // Convert to array and sort by best score |
| 439 | 439 | const urlGroups = Object.values(groupedByUrl).sort((a, b) => b.bestScore - a.bestScore); |
| 440 | 440 | |
| 441 | - // Use backend counts if available, otherwise fall back to frontend calculation | |
| 442 | - const usedUrlCount = sourcesUsed > 0 ? sourcesUsed : urlGroups.filter(g => g.usedForContext).length; | |
| 443 | - const chunksInfo = totalChunksUsed > 0 ? `${totalChunksUsed} chunks sent to AI` : `${topMatches.length} chunk matches`; | |
| 441 | + // Count unique URLs used for context | |
| 442 | + const usedUrlCount = urlGroups.filter(g => g.usedForContext).length; | |
| 444 | 443 | |
| 445 | 444 | let html = `<div class="matches-header"> |
| 446 | - <strong>${usedUrlCount} source${usedUrlCount === 1 ? '' : 's'} used for AI context</strong> | |
| 447 | - <span class="matches-subheader">(${chunksInfo})</span> | |
| 445 | + <strong>${usedUrlCount} entr${usedUrlCount === 1 ? 'y' : 'ies'} used for AI context</strong> | |
| 446 | + <span class="matches-subheader">(from ${topMatches.length} chunk matches)</span> | |
| 448 | 447 | </div>`; |
| 449 | 448 | |
| 450 | 449 | urlGroups.forEach((group, groupIndex) => { |
| 451 | 450 | const cardClass = group.usedForContext ? 'above-threshold' : 'below-threshold'; |
| @@ -539,49 +538,28 @@ | ||
| 539 | 538 | }; |
| 540 | 539 | } |
| 541 | 540 | |
| 542 | 541 | clearChatSession() { |
| 543 | - // Determine the active bot ID from MxChatInstances | |
| 544 | - let botId = 'default'; | |
| 545 | - if (typeof MxChatInstances !== 'undefined' && typeof MxChatInstances.getAllBotIds === 'function') { | |
| 546 | - const botIds = MxChatInstances.getAllBotIds(); | |
| 547 | - if (botIds.length > 0) { | |
| 548 | - botId = botIds[0]; | |
| 549 | - } | |
| 550 | - } | |
| 551 | - | |
| 552 | - // Get current session ID using the correct bot-suffixed cookie name | |
| 553 | - const cookieName = 'mxchat_session_id_' + botId; | |
| 554 | - const sessionId = this.getCookie(cookieName) || this.getCookie('mxchat_session_id') || this.getCurrentSessionId(); | |
| 555 | - | |
| 542 | + // Get current session ID from cookie (most reliable) | |
| 543 | + const sessionId = this.getCookie('mxchat_session_id') || this.getCurrentSessionId(); | |
| 544 | + | |
| 556 | 545 | if (!sessionId) { |
| 557 | - this.log('No active session found'); | |
| 546 | + this.log('❌ No active session found'); | |
| 558 | 547 | return; |
| 559 | 548 | } |
| 560 | - | |
| 561 | - this.log('Current session ID: ' + sessionId); | |
| 562 | - this.log('Starting fresh session...'); | |
| 563 | - | |
| 564 | - // Use MxChatInstances.resetChatSession to properly reset in-memory state + cookie | |
| 565 | - if (typeof MxChatInstances !== 'undefined' && typeof MxChatInstances.resetChatSession === 'function') { | |
| 566 | - MxChatInstances.resetChatSession(botId); | |
| 567 | - } | |
| 568 | - | |
| 569 | - // Get the new session ID that was just set by resetChatSession | |
| 570 | - let newSessionId = ''; | |
| 571 | - if (typeof MxChatInstances !== 'undefined' && typeof MxChatInstances.getChatSession === 'function') { | |
| 572 | - newSessionId = MxChatInstances.getChatSession(botId); | |
| 573 | - } | |
| 574 | - | |
| 575 | - // Fallback if MxChatInstances wasn't available | |
| 576 | - if (!newSessionId) { | |
| 577 | - newSessionId = 'mxchat_chat_' + Math.random().toString(36).substr(2, 9); | |
| 578 | - this.clearMxChatCookie(botId); | |
| 579 | - this.setChatSession(newSessionId, botId); | |
| 580 | - } | |
| 581 | - | |
| 582 | - this.log('New session ID: ' + newSessionId); | |
| 583 | - | |
| 549 | + | |
| 550 | + this.log(`🔍 Current session ID: ${sessionId}`); | |
| 551 | + this.log('🧹 Starting fresh session...'); | |
| 552 | + | |
| 553 | + // Generate new session ID (using your existing format) | |
| 554 | + const newSessionId = 'mxchat_chat_' + Math.random().toString(36).substr(2, 9); | |
| 555 | + | |
| 556 | + // Clear the old cookie and set new one immediately | |
| 557 | + this.clearMxChatCookie(); | |
| 558 | + this.setChatSession(newSessionId); | |
| 559 | + | |
| 560 | + this.log(`🆕 New session ID: ${newSessionId}`); | |
| 561 | + | |
| 584 | 562 | // Call backend to clear old session data |
| 585 | 563 | fetch(mxchatTestData.ajaxUrl, { |
| 586 | 564 | method: 'POST', |
| 587 | 565 | headers: { |
| @@ -596,36 +574,36 @@ | ||
| 596 | 574 | }) |
| 597 | 575 | .then(response => response.json()) |
| 598 | 576 | .then(data => { |
| 599 | 577 | if (data.success) { |
| 600 | - this.log('Backend session cleared: ' + data.data.message); | |
| 601 | - | |
| 578 | + this.log('✅ Backend session cleared: ' + data.data.message); | |
| 579 | + | |
| 602 | 580 | // Update the session ID everywhere in the DOM |
| 603 | 581 | this.updateSessionIdEverywhere(newSessionId); |
| 604 | - | |
| 582 | + | |
| 605 | 583 | // Clear the chat UI |
| 606 | 584 | this.clearChatUI(); |
| 607 | - | |
| 585 | + | |
| 608 | 586 | // Show popular questions again |
| 609 | 587 | const popularQuestions = document.querySelector('#mxchat-popular-questions'); |
| 610 | 588 | if (popularQuestions) { |
| 611 | 589 | popularQuestions.style.display = 'block'; |
| 612 | 590 | } |
| 613 | - | |
| 591 | + | |
| 614 | 592 | // Clear testing data displays |
| 615 | 593 | this.updateLastQuery('New session started', []); |
| 616 | 594 | this.updateTopMatches([], 0); |
| 617 | 595 | this.updateApprovedUrls([]); |
| 618 | - | |
| 619 | - this.log('Fresh session started successfully'); | |
| 620 | - | |
| 596 | + | |
| 597 | + this.log('🎉 Fresh session started successfully'); | |
| 598 | + | |
| 621 | 599 | } else { |
| 622 | - this.log('Error clearing session: ' + (data.data?.message || 'Unknown error')); | |
| 600 | + this.log('❌ Error clearing session: ' + (data.data?.message || 'Unknown error')); | |
| 623 | 601 | } |
| 624 | 602 | }) |
| 625 | 603 | .catch(error => { |
| 626 | 604 | console.error('Error clearing chat session:', error); |
| 627 | - this.log('Connection error when clearing session'); | |
| 605 | + this.log('🔌 Connection error when clearing session'); | |
| 628 | 606 | }); |
| 629 | 607 | } |
| 630 | 608 | |
| 631 | 609 | // Helper function to get cookie (same as your existing one) |
| @@ -634,21 +612,19 @@ | ||
| 634 | 612 | let parts = value.split("; " + name + "="); |
| 635 | 613 | if (parts.length == 2) return parts.pop().split(";").shift(); |
| 636 | 614 | } |
| 637 | 615 | |
| 638 | - // Helper function to set session cookie (matches chat-script.js format) | |
| 639 | - setChatSession(sessionId, botId) { | |
| 640 | - botId = botId || 'default'; | |
| 641 | - document.cookie = 'mxchat_session_id_' + botId + '=' + sessionId + '; path=/; max-age=86400; SameSite=Lax'; | |
| 616 | + // Helper function to set session cookie (same as your existing one) | |
| 617 | + setChatSession(sessionId) { | |
| 618 | + // Set the cookie with a 24-hour expiration (86400 seconds) | |
| 619 | + document.cookie = "mxchat_session_id=" + sessionId + "; path=/; max-age=86400; SameSite=Lax"; | |
| 642 | 620 | } |
| 643 | 621 | |
| 644 | 622 | // Helper function to clear the MxChat session cookie |
| 645 | - clearMxChatCookie(botId) { | |
| 646 | - botId = botId || 'default'; | |
| 647 | - // Clear both bot-suffixed and legacy cookie formats | |
| 648 | - document.cookie = 'mxchat_session_id_' + botId + '=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=Lax'; | |
| 649 | - document.cookie = 'mxchat_session_id=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=Lax'; | |
| 650 | - this.log('Session cookie cleared'); | |
| 623 | + clearMxChatCookie() { | |
| 624 | + // Clear the cookie by setting it to expire in the past | |
| 625 | + document.cookie = "mxchat_session_id=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=Lax"; | |
| 626 | + this.log('🍪 Session cookie cleared'); | |
| 651 | 627 | } |
| 652 | 628 | |
| 653 | 629 | updateSessionIdEverywhere(newSessionId) { |
| 654 | 630 | // Update global session ID variable if it exists |
| @@ -707,23 +683,9 @@ | ||
| 707 | 683 | } |
| 708 | 684 | } |
| 709 | 685 | |
| 710 | 686 | getCurrentSessionId() { |
| 711 | - // Try MxChatInstances first (most reliable — matches chat-script.js) | |
| 712 | - if (typeof MxChatInstances !== 'undefined' && typeof MxChatInstances.getChatSession === 'function') { | |
| 713 | - const botIds = typeof MxChatInstances.getAllBotIds === 'function' ? MxChatInstances.getAllBotIds() : ['default']; | |
| 714 | - const botId = botIds.length > 0 ? botIds[0] : 'default'; | |
| 715 | - const instanceSession = MxChatInstances.getChatSession(botId); | |
| 716 | - if (instanceSession) { | |
| 717 | - return instanceSession; | |
| 718 | - } | |
| 719 | - } | |
| 720 | - | |
| 721 | - // Try bot-suffixed cookie, then legacy cookie | |
| 722 | - const botCookieId = this.getCookie('mxchat_session_id_default'); | |
| 723 | - if (botCookieId) { | |
| 724 | - return botCookieId; | |
| 725 | - } | |
| 687 | + // First try to get from cookie (most reliable) | |
| 726 | 688 | const cookieSessionId = this.getCookie('mxchat_session_id'); |
| 727 | 689 | if (cookieSessionId) { |
| 728 | 690 | return cookieSessionId; |
| 729 | 691 | } |