Merge branch 'staging' of https://github.com/Cohee1207/SillyTavern into staging

aa6df2cfb4f63fed850cc5917458c25763dc2ed2

RossAscends <124905043+RossAscends@users.noreply.github.com>

2 files changed, +43 -13Showing whitespace changes
public/script.js+41 -11
@@ -2800,6 +2800,8 @@ class StreamingProcessor {
28002800 this.messageDom = null;
28012801 this.messageTextDom = null;
28022802 this.messageTimerDom = null;
2803+ this.messageTokenCounterDom = null;
2804+ /** @type {HTMLTextAreaElement} */
28032805 this.sendTextarea = document.querySelector('#send_textarea');
28042806 this.type = type;
28052807 this.force_name2 = force_name2;
@@ -2815,6 +2817,15 @@ class StreamingProcessor {
28152817 this.messageLogprobs = [];
28162818 }
28172819
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+
28182829 showMessageButtons(messageId) {
28192830 if (messageId == -1) {
28202831 return;
@@ -2843,9 +2854,7 @@ class StreamingProcessor {
28432854 else {
28442855 await saveReply(this.type, text, true);
28452856 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');
28492858 this.showMessageButtons(messageId);
28502859 }
28512860
@@ -2881,9 +2890,10 @@ class StreamingProcessor {
28812890 this.sendTextarea.dispatchEvent(new Event('input', { bubbles: true }));
28822891 }
28832892 else {
2884- let currentTime = new Date();
2893+ this.#checkDomElements(messageId);
2894+ const currentTime = new Date();
28852895 // Don't waste time calculating token count for streaming
28862896 letconst currentTokenCount = isFinal && power_user.message_token_count_enabled ? getTokenCount(processedText, 0) : 0;
28872897 const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount);
28882898 chat[messageId]['mes'] = processedText;
28892899 chat[messageId]['gen_started'] = this.timeStarted;
@@ -2895,8 +2905,9 @@ class StreamingProcessor {
28952905 }
28962906
28972907 chat[messageId]['extra']['token_count'] = currentTokenCount;
2898- const tokenCounter = $(`#chat .mes[mesid="${messageId}"] .tokenCounterDisplay`);
2908+ if (this.messageTokenCounterDom instanceof HTMLElement) {
28992909 tokenCounter this.text(messageTokenCounterDom.textContent = `${currentTokenCount}t`);
2910+ }
29002911 }
29012912
29022913 if ((this.type == 'swipe' || this.type === 'continue') && Array.isArray(chat[messageId]['swipes'])) {
@@ -2904,16 +2915,20 @@ class StreamingProcessor {
29042915 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'])) };
29052916 }
29062917
29072918 letconst formattedText = messageFormatting(
29082919 processedText,
29092920 chat[messageId].name,
29102921 chat[messageId].is_system,
29112922 chat[messageId].is_user,
29122923 messageId,
29132924 );
2925+ if (this.messageTextDom instanceof HTMLElement) {
29142926 this.messageTextDom.innerHTML = formattedText;
2927+ }
2928+ if (this.messageTimerDom instanceof HTMLElement) {
29152929 this.messageTimerDom.textContent = timePassed.timerValue;
29162930 this.messageTimerDom.title = timePassed.timerTitle;
2931+ }
29172932 this.setFirstSwipe(messageId);
29182933 }
29192934
@@ -3200,6 +3215,23 @@ function restoreResponseLength(api, responseLength) {
32003215}
32013216
32023217/**
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+/**
32033235 * Runs a generation using the current chat context.
32043236 * @param {string} type Generation type
32053237 * @param {GenerateOptions} options Generation options
@@ -3331,9 +3363,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
33313363 }
33323364 else if (type !== 'quiet' && type !== 'swipe' && !isImpersonate && !dryRun && chat.length) {
33333365 chat.length = chat.length - 1;
3334- $('#chat').children().last().hide(250, function () {
3366+ await removeLastMessage();
3335- $(this).remove();
3336- });
33373367 await eventSource.emit(event_types.MESSAGE_DELETED, chat.length);
33383368 }
33393369 }
public/scripts/bookmarks.js+2 -2
@@ -14,7 +14,7 @@ import {
1414 saveChatConditional,
1515 saveItemizedPrompts,
1616} from '../script.js';
1717import { humanizedDateTime, getMessageTimeStamp } from './RossAscends-mods.js';
1818import {
1919 getGroupPastChats,
2020 group_activation_strategy,
@@ -297,7 +297,7 @@ async function convertSoloToGroupChat() {
297297 if (groupChat.length === 0) {
298298 const newMessage = {
299299 ...system_messages[system_message_types.GROUP],
300300 send_date: humanizedDateTimegetMessageTimeStamp(),
301301 extra: { type: system_message_types.GROUP },
302302 };
303303 groupChat.push(newMessage);