| @@ -60,10 +60,10 @@ | ||
| 60 | 60 | return Object.keys(this.instances); |
| 61 | 61 | }, |
| 62 | 62 | |
| 63 | 63 | // Session management per bot |
| 64 | - // Returns existing session ID from cookie or localStorage (with in-memory fallback), | |
| 65 | - // or null if none exists. Does NOT create a new session — use ensureSession() for that. | |
| 64 | + // Returns existing session ID from cookie or localStorage, or null if none exists. | |
| 65 | + // Does NOT create a new session — use ensureSession() for that. | |
| 66 | 66 | getChatSession: function(botId) { |
| 67 | 67 | var cookieName = 'mxchat_session_id_' + botId; |
| 68 | 68 | var storageKey = 'mxchat_session_id_' + botId; |
| 69 | 69 | var sessionId = getCookie(cookieName); |
| @@ -72,21 +72,8 @@ | ||
| 72 | 72 | if (!sessionId) { |
| 73 | 73 | try { sessionId = localStorage.getItem(storageKey); } catch (e) {} |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - // Fallback to in-memory instance when cookie AND localStorage are both blocked | |
| 77 | - // (Safari ITP, strict tracking prevention, cross-origin iframes with partitioned | |
| 78 | - // storage). Without this, ensureSession() can generate and store an ID that | |
| 79 | - // getChatSession() then can't read back, causing null session_ids on send. | |
| 80 | - if (!sessionId && this.instances[botId] && this.instances[botId].sessionId) { | |
| 81 | - sessionId = this.instances[botId].sessionId; | |
| 82 | - } | |
| 83 | - | |
| 84 | - // Guard against stored sentinel values that indicate earlier broken writes. | |
| 85 | - if (sessionId === 'null' || sessionId === 'undefined') { | |
| 86 | - sessionId = null; | |
| 87 | - } | |
| 88 | - | |
| 89 | 76 | // Re-sync cookie from localStorage if cookie was lost |
| 90 | 77 | if (sessionId && !getCookie(cookieName)) { |
| 91 | 78 | document.cookie = cookieName + "=" + sessionId + "; path=/; max-age=86400; SameSite=Lax"; |
| 92 | 79 | } |
| @@ -612,23 +599,13 @@ | ||
| 612 | 599 | |
| 613 | 600 | // Get instance for session start timestamp (used when persistence is OFF) |
| 614 | 601 | var instance = MxChatInstances.get(botId); |
| 615 | 602 | |
| 616 | - // Guarantee a non-null session_id before the AJAX leaves. ensureSession() is idempotent | |
| 617 | - // and returns the guaranteed-present session id from the in-memory instance even when | |
| 618 | - // cookie/localStorage writes are silently blocked by the browser. | |
| 619 | - var sessionId = MxChatInstances.ensureSession(botId); | |
| 620 | - if (!sessionId || sessionId === 'null' || sessionId === 'undefined') { | |
| 621 | - // Last-resort generation to ensure we never POST a null marker. | |
| 622 | - sessionId = generateSessionId(); | |
| 623 | - MxChatInstances.setChatSession(botId, sessionId); | |
| 624 | - } | |
| 625 | - | |
| 626 | 603 | // Prepare AJAX data |
| 627 | 604 | const ajaxData = { |
| 628 | 605 | action: 'mxchat_handle_chat_request', |
| 629 | 606 | message: message, |
| 630 | - session_id: sessionId, | |
| 607 | + session_id: getChatSession(botId), | |
| 631 | 608 | nonce: mxchatChat.nonce, |
| 632 | 609 | current_page_url: window.location.href, |
| 633 | 610 | current_page_title: document.title, |
| 634 | 611 | bot_id: botId, |
| @@ -861,22 +838,12 @@ | ||
| 861 | 838 | |
| 862 | 839 | // Get instance for session start timestamp (used when persistence is OFF) |
| 863 | 840 | var instance = MxChatInstances.get(botId); |
| 864 | 841 | |
| 865 | - // Guarantee a non-null session_id before the fetch. FormData.append() stringifies any | |
| 866 | - // non-string value via String(), so passing `null` would POST the literal string "null" | |
| 867 | - // and land in the transcripts table as a ghost session. ensureSession() always returns | |
| 868 | - // a real string even when cookies/localStorage are blocked. | |
| 869 | - var streamSessionId = MxChatInstances.ensureSession(botId); | |
| 870 | - if (!streamSessionId || streamSessionId === 'null' || streamSessionId === 'undefined') { | |
| 871 | - streamSessionId = generateSessionId(); | |
| 872 | - MxChatInstances.setChatSession(botId, streamSessionId); | |
| 873 | - } | |
| 874 | - | |
| 875 | 842 | const formData = new FormData(); |
| 876 | 843 | formData.append('action', 'mxchat_stream_chat'); |
| 877 | 844 | formData.append('message', message); |
| 878 | - formData.append('session_id', streamSessionId); | |
| 845 | + formData.append('session_id', getChatSession(botId)); | |
| 879 | 846 | formData.append('nonce', mxchatChat.nonce); |
| 880 | 847 | formData.append('current_page_url', window.location.href); |
| 881 | 848 | formData.append('current_page_title', document.title); |
| 882 | 849 | formData.append('bot_id', botId); |