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