diff --git a/public/script.js b/public/script.js index 8058ffa7b..8f12cfbd9 100644 --- a/public/script.js +++ b/public/script.js @@ -1738,12 +1738,16 @@ class StreamingProcessor { } } -async function Generate(type, { automatic_trigger, force_name2, resolve, reject, quiet_prompt, force_chid } = {}) { +async function Generate(type, { automatic_trigger, force_name2, resolve, reject, quiet_prompt, force_chid, signal } = {}) { //console.log('Generate entered'); setGenerationProgress(0); tokens_already_generated = 0; generation_started = new Date(); - abortController = new AbortController(); + + // Don't recreate abort controller if signal is passed + if (!(abortController && signal)) { + abortController = new AbortController(); + } const isImpersonate = type == "impersonate"; const isInstruct = power_user.instruct.enabled; @@ -1794,7 +1798,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, } if (selected_group && !is_group_generating) { - generateGroupWrapper(false, type, { resolve, reject, quiet_prompt, force_chid }); + generateGroupWrapper(false, type, { resolve, reject, quiet_prompt, force_chid, signal: abortController.signal }); return; } diff --git a/public/scripts/group-chats.js b/public/scripts/group-chats.js index db471c403..8a745e0b7 100644 --- a/public/scripts/group-chats.js +++ b/public/scripts/group-chats.js @@ -404,6 +404,7 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) { } const group = groups.find((x) => x.id === selected_group); + let typingIndicator = $("#chat .typing_indicator"); if (!group || !Array.isArray(group.members) || !group.members.length) { sendSystemMessage(system_message_types.EMPTY); @@ -417,8 +418,6 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) { setCharacterId(undefined); const userInput = $("#send_textarea").val(); - let typingIndicator = $("#chat .typing_indicator"); - if (typingIndicator.length === 0 && !isStreamingEnabled()) { typingIndicator = $( "#typing_indicator_template .typing_indicator" @@ -435,6 +434,7 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) { let activationText = ""; let isUserInput = false; let isGenerationDone = false; + let isGenerationAborted = false; if (userInput && userInput.length && !by_auto_mode) { isUserInput = true; @@ -449,6 +449,16 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) { const resolveOriginal = params.resolve; const rejectOriginal = params.reject; + if (params.signal instanceof AbortSignal) { + if (params.signal.aborted) { + throw new Error('Already aborted signal passed. Group generation stopped'); + } + + params.signal.onabort = () => { + isGenerationAborted = true; + }; + } + if (typeof params.resolve === 'function') { params.resolve = function () { isGenerationDone = true; @@ -517,6 +527,10 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) { // TODO: This is awful. Refactor this while (true) { + if (isGenerationAborted) { + throw new Error('Group generation aborted'); + } + // if not swipe - check if message generated already if (type !== "swipe" && !isMultigenEnabled() && chat.length == messagesBefore) { await delay(100); @@ -593,11 +607,12 @@ async function generateGroupWrapper(by_auto_mode, type = null, params = {}) { } } - // hide and reapply the indicator to the bottom of the list - typingIndicator.hide(250); - $("#chat").append(typingIndicator); } } finally { + // hide and reapply the indicator to the bottom of the list + typingIndicator.hide(250); + $("#chat").append(typingIndicator); + is_group_generating = false; $("#send_textarea").attr("disabled", false); setSendButtonState(false);