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