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 {
2818 }2818 }
28192819
2820 #checkDomElements(messageId) {2820 #checkDomElements(messageId) {
2821 if (this.messageDom === null) {2821 if (this.messageDom === null || this.messageTextDom === null) {
2822 this.messageDom = document.querySelector(`#chat .mes[mesid="${messageId}"]`);2822 this.messageDom = document.querySelector(`#chat .mes[mesid="${messageId}"]`);
2823 this.messageTextDom = this.messageDom?.querySelector('.mes_text');2823 this.messageTextDom = this.messageDom?.querySelector('.mes_text');
2824 this.messageTimerDom = this.messageDom?.querySelector('.mes_timer');2824 this.messageTimerDom = this.messageDom?.querySelector('.mes_timer');
@@ -2890,6 +2890,7 @@ class StreamingProcessor {
2890 this.sendTextarea.dispatchEvent(new Event('input', { bubbles: true }));2890 this.sendTextarea.dispatchEvent(new Event('input', { bubbles: true }));
2891 }2891 }
2892 else {2892 else {
2893 this.#checkDomElements(messageId);
2893 const currentTime = new Date();2894 const currentTime = new Date();
2894 // Don't waste time calculating token count for streaming2895 // Don't waste time calculating token count for streaming
2895 const currentTokenCount = isFinal && power_user.message_token_count_enabled ? getTokenCount(processedText, 0) : 0;2896 const currentTokenCount = isFinal && power_user.message_token_count_enabled ? getTokenCount(processedText, 0) : 0;
@@ -2921,7 +2922,6 @@ class StreamingProcessor {
2921 chat[messageId].is_user,2922 chat[messageId].is_user,
2922 messageId,2923 messageId,
2923 );2924 );
2924 this.#checkDomElements(messageId);
2925 if (this.messageTextDom instanceof HTMLElement) {2925 if (this.messageTextDom instanceof HTMLElement) {
2926 this.messageTextDom.innerHTML = formattedText;2926 this.messageTextDom.innerHTML = formattedText;
2927 }2927 }
@@ -3215,6 +3215,23 @@ function restoreResponseLength(api, responseLength) {
3215}3215}
32163216
3217/**3217/**
3218 * Removes last message from the chat DOM.
3219 * @returns {Promise<void>} Resolves when the message is removed.
3220 */
3221function 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/**
3218 * Runs a generation using the current chat context.3235 * Runs a generation using the current chat context.
3219 * @param {string} type Generation type3236 * @param {string} type Generation type
3220 * @param {GenerateOptions} options Generation options3237 * @param {GenerateOptions} options Generation options
@@ -3346,9 +3363,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
3346 }3363 }
3347 else if (type !== 'quiet' && type !== 'swipe' && !isImpersonate && !dryRun && chat.length) {3364 else if (type !== 'quiet' && type !== 'swipe' && !isImpersonate && !dryRun && chat.length) {
3348 chat.length = chat.length - 1;3365 chat.length = chat.length - 1;
3349 $('#chat').children().last().hide(250, function () {3366 await removeLastMessage();
3350 $(this).remove();
3351 });
3352 await eventSource.emit(event_types.MESSAGE_DELETED, chat.length);3367 await eventSource.emit(event_types.MESSAGE_DELETED, chat.length);
3353 }3368 }
3354 }3369 }