Merge branch 'staging' of https://github.com/Cohee1207/SillyTavern into staging
| @@ -2800,6 +2800,8 @@ class StreamingProcessor { | ||
| 2800 | 2800 | this.messageDom = null; |
| 2801 | 2801 | this.messageTextDom = null; |
| 2802 | 2802 | this.messageTimerDom = null; |
| 2803 | + this.messageTokenCounterDom = null; | |
| 2804 | + /** @type {HTMLTextAreaElement} */ | |
| 2803 | 2805 | this.sendTextarea = document.querySelector('#send_textarea'); |
| 2804 | 2806 | this.type = type; |
| 2805 | 2807 | this.force_name2 = force_name2; |
| @@ -2815,6 +2817,15 @@ class StreamingProcessor { | ||
| 2815 | 2817 | this.messageLogprobs = []; |
| 2816 | 2818 | } |
| 2817 | 2819 | |
| 2820 | + #checkDomElements(messageId) { | |
| 2821 | + if (this.messageDom === null || this.messageTextDom === null) { | |
| 2822 | + this.messageDom = document.querySelector(`#chat .mes[mesid="${messageId}"]`); | |
| 2823 | + this.messageTextDom = this.messageDom?.querySelector('.mes_text'); | |
| 2824 | + this.messageTimerDom = this.messageDom?.querySelector('.mes_timer'); | |
| 2825 | + this.messageTokenCounterDom = this.messageDom?.querySelector('.tokenCounterDisplay'); | |
| 2826 | + } | |
| 2827 | + } | |
| 2828 | + | |
| 2818 | 2829 | showMessageButtons(messageId) { |
| 2819 | 2830 | if (messageId == -1) { |
| 2820 | 2831 | return; |
| @@ -2843,9 +2854,7 @@ class StreamingProcessor { | ||
| 2843 | 2854 | else { |
| 2844 | 2855 | await saveReply(this.type, text, true); |
| 2845 | 2856 | messageId = chat.length - 1; |
| 2846 | - this.messageDom = document.querySelector(`#chat .mes[mesid="${messageId}"]`); | |
| 2857 | + this.#checkDomElements(messageId); | |
| 2847 | - this.messageTextDom = this.messageDom.querySelector('.mes_text'); | |
| 2848 | - this.messageTimerDom = this.messageDom.querySelector('.mes_timer'); | |
| 2849 | 2858 | this.showMessageButtons(messageId); |
| 2850 | 2859 | } |
| 2851 | 2860 | |
| @@ -2881,9 +2890,10 @@ class StreamingProcessor { | ||
| 2881 | 2890 | this.sendTextarea.dispatchEvent(new Event('input', { bubbles: true })); |
| 2882 | 2891 | } |
| 2883 | 2892 | else { |
| 2884 | - let currentTime = new Date(); | |
| 2893 | + this.#checkDomElements(messageId); | |
| 2894 | + const currentTime = new Date(); | |
| 2885 | 2895 | // Don't waste time calculating token count for streaming |
| 2886 | 2896 | letconst currentTokenCount = isFinal && power_user.message_token_count_enabled ? getTokenCount(processedText, 0) : 0; |
| 2887 | 2897 | const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount); |
| 2888 | 2898 | chat[messageId]['mes'] = processedText; |
| 2889 | 2899 | chat[messageId]['gen_started'] = this.timeStarted; |
| @@ -2895,8 +2905,9 @@ class StreamingProcessor { | ||
| 2895 | 2905 | } |
| 2896 | 2906 | |
| 2897 | 2907 | chat[messageId]['extra']['token_count'] = currentTokenCount; |
| 2898 | - const tokenCounter = $(`#chat .mes[mesid="${messageId}"] .tokenCounterDisplay`); | |
| 2908 | + if (this.messageTokenCounterDom instanceof HTMLElement) { | |
| 2899 | 2909 | tokenCounter this.text(messageTokenCounterDom.textContent = `${currentTokenCount}t`); |
| 2910 | + } | |
| 2900 | 2911 | } |
| 2901 | 2912 | |
| 2902 | 2913 | if ((this.type == 'swipe' || this.type === 'continue') && Array.isArray(chat[messageId]['swipes'])) { |
| @@ -2904,16 +2915,20 @@ class StreamingProcessor { | ||
| 2904 | 2915 | chat[messageId]['swipe_info'][chat[messageId]['swipe_id']] = { 'send_date': chat[messageId]['send_date'], 'gen_started': chat[messageId]['gen_started'], 'gen_finished': chat[messageId]['gen_finished'], 'extra': JSON.parse(JSON.stringify(chat[messageId]['extra'])) }; |
| 2905 | 2916 | } |
| 2906 | 2917 | |
| 2907 | 2918 | letconst formattedText = messageFormatting( |
| 2908 | 2919 | processedText, |
| 2909 | 2920 | chat[messageId].name, |
| 2910 | 2921 | chat[messageId].is_system, |
| 2911 | 2922 | chat[messageId].is_user, |
| 2912 | 2923 | messageId, |
| 2913 | 2924 | ); |
| 2914 | - this.messageTextDom.innerHTML = formattedText; | |
| 2925 | + if (this.messageTextDom instanceof HTMLElement) { | |
| 2915 | 2926 | this.messageTimerDommessageTextDom.textContentinnerHTML = timePassed.timerValueformattedText; |
| 2916 | - this.messageTimerDom.title = timePassed.timerTitle; | |
| 2927 | + } | |
| 2928 | + if (this.messageTimerDom instanceof HTMLElement) { | |
| 2929 | + this.messageTimerDom.textContent = timePassed.timerValue; | |
| 2930 | + this.messageTimerDom.title = timePassed.timerTitle; | |
| 2931 | + } | |
| 2917 | 2932 | this.setFirstSwipe(messageId); |
| 2918 | 2933 | } |
| 2919 | 2934 | |
| @@ -3200,6 +3215,23 @@ function restoreResponseLength(api, responseLength) { | ||
| 3200 | 3215 | } |
| 3201 | 3216 | |
| 3202 | 3217 | /** |
| 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 | +/** | |
| 3203 | 3235 | * Runs a generation using the current chat context. |
| 3204 | 3236 | * @param {string} type Generation type |
| 3205 | 3237 | * @param {GenerateOptions} options Generation options |
| @@ -3331,9 +3363,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | ||
| 3331 | 3363 | } |
| 3332 | 3364 | else if (type !== 'quiet' && type !== 'swipe' && !isImpersonate && !dryRun && chat.length) { |
| 3333 | 3365 | chat.length = chat.length - 1; |
| 3334 | - $('#chat').children().last().hide(250, function () { | |
| 3366 | + await removeLastMessage(); | |
| 3335 | - $(this).remove(); | |
| 3336 | - }); | |
| 3337 | 3367 | await eventSource.emit(event_types.MESSAGE_DELETED, chat.length); |
| 3338 | 3368 | } |
| 3339 | 3369 | } |
| @@ -14,7 +14,7 @@ import { | ||
| 14 | 14 | saveChatConditional, |
| 15 | 15 | saveItemizedPrompts, |
| 16 | 16 | } from '../script.js'; |
| 17 | 17 | import { humanizedDateTime, getMessageTimeStamp } from './RossAscends-mods.js'; |
| 18 | 18 | import { |
| 19 | 19 | getGroupPastChats, |
| 20 | 20 | group_activation_strategy, |
| @@ -297,7 +297,7 @@ async function convertSoloToGroupChat() { | ||
| 297 | 297 | if (groupChat.length === 0) { |
| 298 | 298 | const newMessage = { |
| 299 | 299 | ...system_messages[system_message_types.GROUP], |
| 300 | 300 | send_date: humanizedDateTimegetMessageTimeStamp(), |
| 301 | 301 | extra: { type: system_message_types.GROUP }, |
| 302 | 302 | }; |
| 303 | 303 | groupChat.push(newMessage); |