From 08f2b73ab8f785455898f832d80a4cc95f8ddbf5 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Mon, 16 Sep 2024 00:38:41 +0300 Subject: [PATCH] Preserve neutral chat on reloading with commands (#2848) * Preserve neutral chat on reloading * Restore neutral on deleting character with bulk --- public/script.js | 19 +++++++++---------- public/scripts/chats.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/public/script.js b/public/script.js index a5ad50717..85cb7ccd3 100644 --- a/public/script.js +++ b/public/script.js @@ -225,7 +225,7 @@ import { getBackgrounds, initBackgrounds, loadBackgroundSettings, background_set import { hideLoader, showLoader } from './scripts/loader.js'; import { BulkEditOverlay, CharacterContextMenu } from './scripts/BulkEditOverlay.js'; import { loadFeatherlessModels, loadMancerModels, loadOllamaModels, loadTogetherAIModels, loadInfermaticAIModels, loadOpenRouterModels, loadVllmModels, loadAphroditeModels, loadDreamGenModels, initTextGenModels, loadTabbyModels } from './scripts/textgen-models.js'; -import { appendFileContent, hasPendingFileAttachment, populateFileAttachment, decodeStyleTags, encodeStyleTags, isExternalMediaAllowed, getCurrentEntityId } from './scripts/chats.js'; +import { appendFileContent, hasPendingFileAttachment, populateFileAttachment, decodeStyleTags, encodeStyleTags, isExternalMediaAllowed, getCurrentEntityId, preserveNeutralChat, restoreNeutralChat } from './scripts/chats.js'; import { initPresetManager } from './scripts/preset-manager.js'; import { MacrosParser, evaluateMacros, getLastMessageId } from './scripts/macros.js'; import { currentUser, setUserControls } from './scripts/user.js'; @@ -1846,6 +1846,7 @@ export async function deleteLastMessage() { } export async function reloadCurrentChat() { + preserveNeutralChat(); await clearChat(); chat.length = 0; @@ -1857,6 +1858,7 @@ export async function reloadCurrentChat() { } else { resetChatState(); + restoreNeutralChat(); await getCharacters(); await printMessages(); await eventSource.emit(event_types.CHAT_CHANGED, getCurrentChatId()); @@ -5700,10 +5702,10 @@ export function deactivateSendButtons() { } export function resetChatState() { + // replaces deleted charcter name with system user since it will be displayed next. + name2 = (this_chid === undefined && neutralCharacterName) ? neutralCharacterName : systemUserName; //unsets expected chid before reloading (related to getCharacters/printCharacters from using old arrays) this_chid = undefined; - // replaces deleted charcter name with system user since it will be displayed next. - name2 = systemUserName; // sets up system user to tell user about having deleted a character chat.splice(0, chat.length, ...SAFETY_CHAT); // resets chat metadata @@ -8947,15 +8949,12 @@ export async function deleteCharacter(characterKey, { deleteChats = true } = {}) * It also ensures to save the settings after all the operations. */ async function removeCharacterFromUI() { + preserveNeutralChat(); await clearChat(); - $('#character_cross').click(); - this_chid = undefined; - characters.length = 0; - name2 = systemUserName; - chat.splice(0, chat.length, ...SAFETY_CHAT); - chat_metadata = {}; + $('#character_cross').trigger('click'); + resetChatState(); $(document.getElementById('rm_button_selected_ch')).children('h2').text(''); - this_chid = undefined; + restoreNeutralChat(); await getCharacters(); await printMessages(); saveSettingsDebounced(); diff --git a/public/scripts/chats.js b/public/scripts/chats.js index 941b70212..cd6b45808 100644 --- a/public/scripts/chats.js +++ b/public/scripts/chats.js @@ -19,6 +19,8 @@ import { this_chid, saveChatConditional, chat_metadata, + neutralCharacterName, + updateChatMetadata, } from '../script.js'; import { selected_group } from './group-chats.js'; import { power_user } from './power-user.js'; @@ -1352,6 +1354,32 @@ async function verifyAttachmentsForSource(source) { } } +const NEUTRAL_CHAT_KEY = 'neutralChat'; + +export function preserveNeutralChat() { + if (this_chid !== undefined || selected_group || name2 !== neutralCharacterName) { + return; + } + + sessionStorage.setItem(NEUTRAL_CHAT_KEY, JSON.stringify({ chat, chat_metadata })); +} + +export function restoreNeutralChat() { + if (this_chid !== undefined || selected_group || name2 !== neutralCharacterName) { + return; + } + + const neutralChat = sessionStorage.getItem(NEUTRAL_CHAT_KEY); + if (!neutralChat) { + return; + } + + const { chat: neutralChatData, chat_metadata: neutralChatMetadata } = JSON.parse(neutralChat); + chat.splice(0, chat.length, ...neutralChatData); + updateChatMetadata(neutralChatMetadata, true); + sessionStorage.removeItem(NEUTRAL_CHAT_KEY); +} + /** * Registers a file converter function. * @param {string} mimeType MIME type