1
0
mirror of https://github.com/coalaura/whiskr.git synced 2025-12-02 20:22:52 +00:00

prevent multi-run issues

This commit is contained in:
Laura
2025-10-05 00:14:56 +02:00
parent d3d487d314
commit e37b9e31e1

View File

@@ -1073,21 +1073,21 @@
} }
} }
let chatController; let abortCallback;
function abortNow() { function abortNow() {
chatController?.abort(); if (!abortCallback) {
return false;
}
abortCallback();
return true;
} }
function generate(cancel = false, noPush = false) { function generate(cancel = false, noPush = false) {
if (chatController) { if (abortNow() && cancel) {
chatController.abort(); return;
if (cancel) {
$chat.classList.remove("completing");
return;
}
} }
if (autoScrolling) { if (autoScrolling) {
@@ -1127,7 +1127,7 @@
pushMessage(); pushMessage();
} }
chatController = new AbortController(); const controller = new AbortController();
$chat.classList.add("completing"); $chat.classList.add("completing");
@@ -1151,20 +1151,27 @@
messages: messages.map(message => message.getData()).filter(Boolean), messages: messages.map(message => message.getData()).filter(Boolean),
}; };
let message, generationID, timeout; let message, generationID, stopTimeout;
function stopLoadingTimeout() {
clearTimeout(timeout);
message?.setLoading(false);
}
function startLoadingTimeout() { function startLoadingTimeout() {
clearTimeout(timeout); stopTimeout?.();
timeout = setTimeout(() => { if (!message) {
message?.setLoading(true); return;
}, 1500); }
const msg = message,
timeout = setTimeout(() => {
msg.setLoading(true);
}, 1500);
stopTimeout = () => {
stopTimeout = null;
clearTimeout(timeout);
msg?.setLoading(false);
};
} }
function finish() { function finish() {
@@ -1199,6 +1206,25 @@
} }
} }
let aborted;
abortCallback = () => {
abortCallback = null;
aborted = true;
controller.abort();
stopTimeout?.();
finish();
$chat.classList.remove("completing");
if (!chatTitle && !titleController) {
refreshTitle();
}
};
start(); start();
stream( stream(
@@ -1209,27 +1235,21 @@
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify(body), body: JSON.stringify(body),
signal: chatController.signal, signal: controller.signal,
}, },
chunk => { chunk => {
stopLoadingTimeout(); if (aborted) {
return;
}
if (chunk === "aborted" || chunk === "done") { if (chunk === "done") {
chatController = null; abortCallback();
finish();
if (chunk === "done") {
$chat.classList.remove("completing");
if (!chatTitle && !titleController) {
refreshTitle();
}
}
return; return;
} }
stopTimeout?.();
if (!message && chunk.type !== "end") { if (!message && chunk.type !== "end") {
start(); start();
} }