zhlajiex
commited on
Commit
·
a058845
1
Parent(s):
24d0354
Fix: Instant Send Button reset and resolved 'Link Severed' false positives
Browse files- backend/controllers/ai.js +8 -1
- backend/public/chat.html +9 -1
backend/controllers/ai.js
CHANGED
|
@@ -176,7 +176,10 @@ exports.chat = asyncHandler(async (req, res, next) => {
|
|
| 176 |
if (msg.type === "data" && msg.data && typeof msg.data[0] === 'string') {
|
| 177 |
const newContent = msg.data[0].slice(fullAIResponse.length);
|
| 178 |
fullAIResponse = msg.data[0];
|
| 179 |
-
if (newContent)
|
|
|
|
|
|
|
|
|
|
| 180 |
}
|
| 181 |
}
|
| 182 |
} catch (e) { console.error("Gradio Error", e); }
|
|
@@ -258,6 +261,10 @@ exports.chat = asyncHandler(async (req, res, next) => {
|
|
| 258 |
const dataStr = line.slice(6).trim();
|
| 259 |
if (dataStr === '[DONE]') {
|
| 260 |
isStreamEnded = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
return;
|
| 262 |
}
|
| 263 |
try {
|
|
|
|
| 176 |
if (msg.type === "data" && msg.data && typeof msg.data[0] === 'string') {
|
| 177 |
const newContent = msg.data[0].slice(fullAIResponse.length);
|
| 178 |
fullAIResponse = msg.data[0];
|
| 179 |
+
if (newContent) {
|
| 180 |
+
// INSTANT RESET TRIGGER: We can't do it here easily, but we'll ensure done is sent
|
| 181 |
+
res.write(`data: ${JSON.stringify({ message: newContent })}\n\n`);
|
| 182 |
+
}
|
| 183 |
}
|
| 184 |
}
|
| 185 |
} catch (e) { console.error("Gradio Error", e); }
|
|
|
|
| 261 |
const dataStr = line.slice(6).trim();
|
| 262 |
if (dataStr === '[DONE]') {
|
| 263 |
isStreamEnded = true;
|
| 264 |
+
if (!res.writableEnded) {
|
| 265 |
+
res.write(`data: ${JSON.stringify({ done: true, sessionId: session._id })}\n\n`);
|
| 266 |
+
res.end();
|
| 267 |
+
}
|
| 268 |
return;
|
| 269 |
}
|
| 270 |
try {
|
backend/public/chat.html
CHANGED
|
@@ -412,6 +412,11 @@
|
|
| 412 |
return;
|
| 413 |
}
|
| 414 |
if (data.message) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
fullText += data.message;
|
| 416 |
aiNode.querySelector('.prose').innerHTML = marked.parse(fullText);
|
| 417 |
injectCopyBtns(aiNode);
|
|
@@ -420,6 +425,7 @@
|
|
| 420 |
if (data.done) {
|
| 421 |
clearTimeout(safetyTimeout);
|
| 422 |
resetUI();
|
|
|
|
| 423 |
}
|
| 424 |
if (data.sessionId && !currentSessionId) { currentSessionId = data.sessionId; loadHistory(); }
|
| 425 |
} catch (e) {}
|
|
@@ -428,7 +434,9 @@
|
|
| 428 |
}
|
| 429 |
clearTimeout(safetyTimeout);
|
| 430 |
} catch (err) {
|
| 431 |
-
|
|
|
|
|
|
|
| 432 |
resetUI();
|
| 433 |
} finally {
|
| 434 |
resetUI();
|
|
|
|
| 412 |
return;
|
| 413 |
}
|
| 414 |
if (data.message) {
|
| 415 |
+
// INSTANT RESET: As soon as we get the first packet, release the button
|
| 416 |
+
if (isProcessing) {
|
| 417 |
+
isProcessing = false;
|
| 418 |
+
document.getElementById('send-btn').innerHTML = '<i class="fas fa-arrow-up text-sm"></i>';
|
| 419 |
+
}
|
| 420 |
fullText += data.message;
|
| 421 |
aiNode.querySelector('.prose').innerHTML = marked.parse(fullText);
|
| 422 |
injectCopyBtns(aiNode);
|
|
|
|
| 425 |
if (data.done) {
|
| 426 |
clearTimeout(safetyTimeout);
|
| 427 |
resetUI();
|
| 428 |
+
return; // Exit loop on clean done
|
| 429 |
}
|
| 430 |
if (data.sessionId && !currentSessionId) { currentSessionId = data.sessionId; loadHistory(); }
|
| 431 |
} catch (e) {}
|
|
|
|
| 434 |
}
|
| 435 |
clearTimeout(safetyTimeout);
|
| 436 |
} catch (err) {
|
| 437 |
+
if (fullText.length < 5) {
|
| 438 |
+
appendMessage('ai', "!! LINK SEVERED");
|
| 439 |
+
}
|
| 440 |
resetUI();
|
| 441 |
} finally {
|
| 442 |
resetUI();
|