Fix streams getting stuck on regen

807487ce853197bed409e4b058f4af7bdecf97bd

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +20 -5Ignore whitespace
public/script.js+20 -5
@@ -2818,7 +2818,7 @@ class StreamingProcessor {
28182818 }
28192819
28202820 #checkDomElements(messageId) {
28212821 if (this.messageDom === null || this.messageTextDom === null) {
28222822 this.messageDom = document.querySelector(`#chat .mes[mesid="${messageId}"]`);
28232823 this.messageTextDom = this.messageDom?.querySelector('.mes_text');
28242824 this.messageTimerDom = this.messageDom?.querySelector('.mes_timer');
@@ -2890,6 +2890,7 @@ class StreamingProcessor {
28902890 this.sendTextarea.dispatchEvent(new Event('input', { bubbles: true }));
28912891 }
28922892 else {
2893+ this.#checkDomElements(messageId);
28932894 const currentTime = new Date();
28942895 // Don't waste time calculating token count for streaming
28952896 const currentTokenCount = isFinal && power_user.message_token_count_enabled ? getTokenCount(processedText, 0) : 0;
@@ -2921,7 +2922,6 @@ class StreamingProcessor {
29212922 chat[messageId].is_user,
29222923 messageId,
29232924 );
2924- this.#checkDomElements(messageId);
29252925 if (this.messageTextDom instanceof HTMLElement) {
29262926 this.messageTextDom.innerHTML = formattedText;
29272927 }
@@ -3215,6 +3215,23 @@ function restoreResponseLength(api, responseLength) {
32153215}
32163216
32173217/**
3218+ * Removes last message from the chat DOM.
3219+ * @returns {Promise<void>} Resolves when the message is removed.
3220+ */
3221+function removeLastMessage() {
3222+ return new Promise((resolve) => {
3223+ const lastMes = $('#chat').children('.mes').last();
3224+ if (lastMes.length === 0) {
3225+ return resolve();
3226+ }
3227+ lastMes.hide(animation_duration, function () {
3228+ $(this).remove();
3229+ resolve();
3230+ });
3231+ });
3232+}
3233+
3234+/**
32183235 * Runs a generation using the current chat context.
32193236 * @param {string} type Generation type
32203237 * @param {GenerateOptions} options Generation options
@@ -3346,9 +3363,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
33463363 }
33473364 else if (type !== 'quiet' && type !== 'swipe' && !isImpersonate && !dryRun && chat.length) {
33483365 chat.length = chat.length - 1;
3349- $('#chat').children().last().hide(250, function () {
3366+ await removeLastMessage();
3350- $(this).remove();
3351- });
33523367 await eventSource.emit(event_types.MESSAGE_DELETED, chat.length);
33533368 }
33543369 }