| 1 |
jQuery(document).ready(function($) { |
| 2 |
$('#mxchat-test-streaming-btn').on('click', function() { |
| 3 |
const $button = $(this); |
| 4 |
const $result = $('#mxchat-test-streaming-result'); |
| 5 |
|
| 6 |
$button.prop('disabled', true).text('Testing...'); |
| 7 |
$result.text('Testing streaming environment...'); |
| 8 |
|
| 9 |
// Test both backend capability AND frontend environment |
| 10 |
testCompleteStreamingEnvironment() |
| 11 |
.then(result => { |
| 12 |
if (result.success) { |
| 13 |
$result.css('color', 'green').html(result.message); |
| 14 |
} else { |
| 15 |
$result.css('color', 'red').html(result.message); |
| 16 |
} |
| 17 |
}) |
| 18 |
.catch(error => { |
| 19 |
$result.css('color', 'red').html('❌ Test failed: ' + error.message); |
| 20 |
}) |
| 21 |
.finally(() => { |
| 22 |
$button.prop('disabled', false).text('Test Streaming Compatibility'); |
| 23 |
}); |
| 24 |
}); |
| 25 |
|
| 26 |
function testCompleteStreamingEnvironment() { |
| 27 |
return new Promise((resolve, reject) => { |
| 28 |
let results = { |
| 29 |
backendSupported: false, |
| 30 |
frontendWorking: false, |
| 31 |
chunks: 0, |
| 32 |
timing: null, |
| 33 |
issues: [] |
| 34 |
}; |
| 35 |
|
| 36 |
let startTime = Date.now(); |
| 37 |
let firstChunkTime = null; |
| 38 |
let lastChunkTime = null; |
| 39 |
let timeout; |
| 40 |
|
| 41 |
// Set overall timeout |
| 42 |
timeout = setTimeout(() => { |
| 43 |
resolve({ |
| 44 |
success: false, |
| 45 |
message: getFailureMessage(results) |
| 46 |
}); |
| 47 |
}, 15000); |
| 48 |
|
| 49 |
// Test the actual chat streaming endpoint (not the test endpoint) |
| 50 |
const formData = new FormData(); |
| 51 |
formData.append('action', 'mxchat_stream_chat'); |
| 52 |
formData.append('message', 'Say "test 1", then "test 2", then "test 3" - each on a separate line.'); |
| 53 |
formData.append('session_id', 'streaming_test_' + Date.now()); |
| 54 |
formData.append('nonce', mxchatTestStreamingAjax.nonce); |
| 55 |
|
| 56 |
fetch(mxchatTestStreamingAjax.ajax_url, { |
| 57 |
method: 'POST', |
| 58 |
body: formData, |
| 59 |
credentials: 'same-origin' |
| 60 |
}) |
| 61 |
.then(response => { |
| 62 |
const contentType = response.headers.get('content-type'); |
| 63 |
console.log('Content-Type:', contentType); |
| 64 |
|
| 65 |
// Check if we got JSON (streaming failed) |
| 66 |
if (contentType && contentType.includes('application/json')) { |
| 67 |
response.json().then(data => { |
| 68 |
clearTimeout(timeout); |
| 69 |
results.backendSupported = true; |
| 70 |
results.frontendWorking = false; |
| 71 |
results.issues.push('Server fell back to JSON response'); |
| 72 |
resolve({ |
| 73 |
success: false, |
| 74 |
message: getFailureMessage(results) |
| 75 |
}); |
| 76 |
}); |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
// Check for proper SSE content type |
| 81 |
if (!contentType || !contentType.includes('text/event-stream')) { |
| 82 |
clearTimeout(timeout); |
| 83 |
results.issues.push(`Wrong content-type: ${contentType || 'none'}`); |
| 84 |
resolve({ |
| 85 |
success: false, |
| 86 |
message: getFailureMessage(results) |
| 87 |
}); |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
results.backendSupported = true; |
| 92 |
|
| 93 |
// Process streaming response |
| 94 |
const reader = response.body.getReader(); |
| 95 |
const decoder = new TextDecoder(); |
| 96 |
let buffer = ''; |
| 97 |
|
| 98 |
function processStream() { |
| 99 |
reader.read().then(({ done, value }) => { |
| 100 |
if (done) { |
| 101 |
clearTimeout(timeout); |
| 102 |
results.timing = { |
| 103 |
total: Date.now() - startTime, |
| 104 |
firstChunk: firstChunkTime ? firstChunkTime - startTime : null, |
| 105 |
lastChunk: lastChunkTime ? lastChunkTime - startTime : null |
| 106 |
}; |
| 107 |
|
| 108 |
if (results.chunks > 0) { |
| 109 |
resolve({ |
| 110 |
success: true, |
| 111 |
message: getSuccessMessage(results) |
| 112 |
}); |
| 113 |
} else { |
| 114 |
results.issues.push('No chunks received'); |
| 115 |
resolve({ |
| 116 |
success: false, |
| 117 |
message: getFailureMessage(results) |
| 118 |
}); |
| 119 |
} |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
buffer += decoder.decode(value, { stream: true }); |
| 124 |
const lines = buffer.split('\n'); |
| 125 |
buffer = lines.pop() || ''; |
| 126 |
|
| 127 |
for (const line of lines) { |
| 128 |
if (line.startsWith('data: ')) { |
| 129 |
const data = line.substring(6); |
| 130 |
|
| 131 |
if (data === '[DONE]') { |
| 132 |
// Stream complete - will be handled in done section |
| 133 |
continue; |
| 134 |
} |
| 135 |
|
| 136 |
try { |
| 137 |
const json = JSON.parse(data); |
| 138 |
if (json.content && json.content.trim().length > 0) { |
| 139 |
results.chunks++; |
| 140 |
results.frontendWorking = true; |
| 141 |
|
| 142 |
if (!firstChunkTime) { |
| 143 |
firstChunkTime = Date.now(); |
| 144 |
} |
| 145 |
lastChunkTime = Date.now(); |
| 146 |
|
| 147 |
$result.text(`� |
| 148 |
Streaming working... (${results.chunks} chunks received)`); |
| 149 |
} |
| 150 |
} catch (e) { |
| 151 |
// Non-JSON data is fine for some SSE implementations |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
processStream(); |
| 157 |
}).catch(error => { |
| 158 |
clearTimeout(timeout); |
| 159 |
results.issues.push('Stream read error: ' + error.message); |
| 160 |
resolve({ |
| 161 |
success: false, |
| 162 |
message: getFailureMessage(results) |
| 163 |
}); |
| 164 |
}); |
| 165 |
} |
| 166 |
|
| 167 |
processStream(); |
| 168 |
}) |
| 169 |
.catch(error => { |
| 170 |
clearTimeout(timeout); |
| 171 |
results.issues.push('Fetch error: ' + error.message); |
| 172 |
resolve({ |
| 173 |
success: false, |
| 174 |
message: getFailureMessage(results) |
| 175 |
}); |
| 176 |
}); |
| 177 |
}); |
| 178 |
} |
| 179 |
|
| 180 |
function getSuccessMessage(results) { |
| 181 |
return `� |
| 182 |
<strong>Streaming is working!</strong><br> |
| 183 |
📊 Received ${results.chunks} chunks in ${results.timing.total}ms<br> |
| 184 |
⚡ First chunk: ${results.timing.firstChunk}ms<br> |
| 185 |
🎯 Your users will see real-time streaming responses.`; |
| 186 |
} |
| 187 |
|
| 188 |
function getFailureMessage(results) { |
| 189 |
let message = '❌ <strong>Streaming is not working in your environment. Please disable and use regular response.</strong><br><br>'; |
| 190 |
|
| 191 |
if (results.backendSupported) { |
| 192 |
message += '<strong>Likely causes:</strong><br>'; |
| 193 |
message += '• Caching plugin (WP Rocket, W3 Total Cache, etc.)<br>'; |
| 194 |
message += '• CDN buffering (Cloudflare, etc.)<br>'; |
| 195 |
message += '• Server-level buffering (nginx, Apache)<br>'; |
| 196 |
message += '• Hosting provider optimizations<br><br>'; |
| 197 |
|
| 198 |
message += '<strong>Solutions:</strong><br>'; |
| 199 |
message += '• Disable caching for chat endpoints<br>'; |
| 200 |
message += '• Add streaming exceptions in your CDN<br>'; |
| 201 |
message += '• Contact your hosting provider<br>'; |
| 202 |
} else { |
| 203 |
message += '❌ Backend streaming not supported<br>'; |
| 204 |
if (results.issues.length > 0) { |
| 205 |
message += '<br><strong>Issues detected:</strong><br>'; |
| 206 |
results.issues.forEach(issue => { |
| 207 |
message += `• ${issue}<br>`; |
| 208 |
}); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
return message; |
| 213 |
} |
| 214 |
}); |