| @@ -76,9 +76,9 @@ | ||
| 76 | 76 | interceptChatResponses: function() { |
| 77 | 77 | var self = this; |
| 78 | 78 | |
| 79 | 79 | // Store reference globally so interception can find us |
| 80 | - window.mxchatTestPanelInstance = self; | |
| 80 | + window.mxchatAdminTestPanel = self; | |
| 81 | 81 | |
| 82 | 82 | // Intercept jQuery AJAX calls |
| 83 | 83 | if (window.jQuery) { |
| 84 | 84 | var originalAjax = jQuery.ajax; |
| @@ -107,9 +107,9 @@ | ||
| 107 | 107 | return originalAjax.call(this, options); |
| 108 | 108 | }; |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - // Intercept fetch API calls (for both JSON and SSE streaming) | |
| 111 | + // Intercept fetch API calls (for streaming) | |
| 112 | 112 | var originalFetch = window.fetch; |
| 113 | 113 | |
| 114 | 114 | window.fetch = function() { |
| 115 | 115 | var args = arguments; |
| @@ -115,12 +115,10 @@ | ||
| 115 | 115 | var args = arguments; |
| 116 | 116 | return originalFetch.apply(this, args).then(function(response) { |
| 117 | 117 | if (args[0] && typeof args[0] === 'string' && |
| 118 | 118 | (args[0].includes('admin-ajax.php') || args[0].includes('mxchat'))) { |
| 119 | - var contentType = response.headers.get('content-type') || ''; | |
| 120 | - | |
| 121 | - if (contentType.includes('application/json')) { | |
| 122 | - // Non-streaming: parse the full JSON response | |
| 119 | + var contentType = response.headers.get('content-type'); | |
| 120 | + if (contentType && contentType.includes('application/json')) { | |
| 123 | 121 | response.clone().json().then(function(data) { |
| 124 | 122 | if (self && data && data.testing_data) { |
| 125 | 123 | self.handleTestingData(data.testing_data); |
| 126 | 124 | } else if (self && data && data.data && data.data.testing_data) { |
| @@ -126,57 +124,8 @@ | ||
| 126 | 124 | } else if (self && data && data.data && data.data.testing_data) { |
| 127 | 125 | self.handleTestingData(data.data.testing_data); |
| 128 | 126 | } |
| 129 | 127 | }).catch(function() {}); |
| 130 | - } else if (contentType.includes('text/event-stream') || contentType.includes('text/plain')) { | |
| 131 | - // Streaming (SSE): read just enough to find the testing_data event | |
| 132 | - // Using clone() so the original stream is unaffected for chat-script.js | |
| 133 | - try { | |
| 134 | - var clonedResponse = response.clone(); | |
| 135 | - var reader = clonedResponse.body.getReader(); | |
| 136 | - var decoder = new TextDecoder(); | |
| 137 | - var sseBuffer = ''; | |
| 138 | - var found = false; | |
| 139 | - | |
| 140 | - function readChunk() { | |
| 141 | - reader.read().then(function(result) { | |
| 142 | - if (result.done || found) { | |
| 143 | - reader.cancel().catch(function() {}); | |
| 144 | - return; | |
| 145 | - } | |
| 146 | - | |
| 147 | - sseBuffer += decoder.decode(result.value, { stream: true }); | |
| 148 | - var sseLines = sseBuffer.split('\n'); | |
| 149 | - | |
| 150 | - for (var i = 0; i < sseLines.length; i++) { | |
| 151 | - var sseLine = sseLines[i]; | |
| 152 | - if (sseLine.indexOf('data: ') === 0) { | |
| 153 | - var payload = sseLine.substring(6); | |
| 154 | - try { | |
| 155 | - var parsed = JSON.parse(payload); | |
| 156 | - if (parsed && parsed.testing_data) { | |
| 157 | - self.handleTestingData(parsed.testing_data); | |
| 158 | - found = true; | |
| 159 | - reader.cancel().catch(function() {}); | |
| 160 | - return; | |
| 161 | - } | |
| 162 | - } catch (e) {} | |
| 163 | - } | |
| 164 | - } | |
| 165 | - | |
| 166 | - // Keep reading until we find testing_data or hit a limit | |
| 167 | - if (sseBuffer.length < 50000) { | |
| 168 | - readChunk(); | |
| 169 | - } else { | |
| 170 | - reader.cancel().catch(function() {}); | |
| 171 | - } | |
| 172 | - }).catch(function() { | |
| 173 | - // Stream read error — safe to ignore | |
| 174 | - }); | |
| 175 | - } | |
| 176 | - | |
| 177 | - readChunk(); | |
| 178 | - } catch (e) {} | |
| 179 | 128 | } |
| 180 | 129 | } |
| 181 | 130 | return response; |
| 182 | 131 | }); |