Preserve neutral chat on reloading with commands (#2848) * Preserve neutral chat on reloading * Restore neutral on deleting character with bulk

08f2b73ab8f785455898f832d80a4cc95f8ddbf5

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
2 files changed, +37 -10Ignore whitespace
public/script.js+9 -10
@@ -225,7 +225,7 @@ import { getBackgrounds, initBackgrounds, loadBackgroundSettings, background_set
225import { hideLoader, showLoader } from './scripts/loader.js';225import { hideLoader, showLoader } from './scripts/loader.js';
226import { BulkEditOverlay, CharacterContextMenu } from './scripts/BulkEditOverlay.js';226import { BulkEditOverlay, CharacterContextMenu } from './scripts/BulkEditOverlay.js';
227import { loadFeatherlessModels, loadMancerModels, loadOllamaModels, loadTogetherAIModels, loadInfermaticAIModels, loadOpenRouterModels, loadVllmModels, loadAphroditeModels, loadDreamGenModels, initTextGenModels, loadTabbyModels } from './scripts/textgen-models.js';227import { loadFeatherlessModels, loadMancerModels, loadOllamaModels, loadTogetherAIModels, loadInfermaticAIModels, loadOpenRouterModels, loadVllmModels, loadAphroditeModels, loadDreamGenModels, initTextGenModels, loadTabbyModels } from './scripts/textgen-models.js';
228import { appendFileContent, hasPendingFileAttachment, populateFileAttachment, decodeStyleTags, encodeStyleTags, isExternalMediaAllowed, getCurrentEntityId } from './scripts/chats.js';228import { appendFileContent, hasPendingFileAttachment, populateFileAttachment, decodeStyleTags, encodeStyleTags, isExternalMediaAllowed, getCurrentEntityId, preserveNeutralChat, restoreNeutralChat } from './scripts/chats.js';
229import { initPresetManager } from './scripts/preset-manager.js';229import { initPresetManager } from './scripts/preset-manager.js';
230import { MacrosParser, evaluateMacros, getLastMessageId } from './scripts/macros.js';230import { MacrosParser, evaluateMacros, getLastMessageId } from './scripts/macros.js';
231import { currentUser, setUserControls } from './scripts/user.js';231import { currentUser, setUserControls } from './scripts/user.js';
@@ -1846,6 +1846,7 @@ export async function deleteLastMessage() {
1846}1846}
18471847
1848export async function reloadCurrentChat() {1848export async function reloadCurrentChat() {
1849 preserveNeutralChat();
1849 await clearChat();1850 await clearChat();
1850 chat.length = 0;1851 chat.length = 0;
18511852
@@ -1857,6 +1858,7 @@ export async function reloadCurrentChat() {
1857 }1858 }
1858 else {1859 else {
1859 resetChatState();1860 resetChatState();
1861 restoreNeutralChat();
1860 await getCharacters();1862 await getCharacters();
1861 await printMessages();1863 await printMessages();
1862 await eventSource.emit(event_types.CHAT_CHANGED, getCurrentChatId());1864 await eventSource.emit(event_types.CHAT_CHANGED, getCurrentChatId());
@@ -5700,10 +5702,10 @@ export function deactivateSendButtons() {
5700}5702}
57015703
5702export function resetChatState() {5704export function resetChatState() {
5705 // replaces deleted charcter name with system user since it will be displayed next.
5706 name2 = (this_chid === undefined && neutralCharacterName) ? neutralCharacterName : systemUserName;
5703 //unsets expected chid before reloading (related to getCharacters/printCharacters from using old arrays)5707 //unsets expected chid before reloading (related to getCharacters/printCharacters from using old arrays)
5704 this_chid = undefined;5708 this_chid = undefined;
5705 // replaces deleted charcter name with system user since it will be displayed next.
5706 name2 = systemUserName;
5707 // sets up system user to tell user about having deleted a character5709 // sets up system user to tell user about having deleted a character
5708 chat.splice(0, chat.length, ...SAFETY_CHAT);5710 chat.splice(0, chat.length, ...SAFETY_CHAT);
5709 // resets chat metadata5711 // resets chat metadata
@@ -8947,15 +8949,12 @@ export async function deleteCharacter(characterKey, { deleteChats = true } = {})
8947 * It also ensures to save the settings after all the operations.8949 * It also ensures to save the settings after all the operations.
8948 */8950 */
8949async function removeCharacterFromUI() {8951async function removeCharacterFromUI() {
8952 preserveNeutralChat();
8950 await clearChat();8953 await clearChat();
8951 $('#character_cross').click();8954 $('#character_cross').trigger('click');
8952 this_chid = undefined;8955 resetChatState();
8953 characters.length = 0;
8954 name2 = systemUserName;
8955 chat.splice(0, chat.length, ...SAFETY_CHAT);
8956 chat_metadata = {};
8957 $(document.getElementById('rm_button_selected_ch')).children('h2').text('');8956 $(document.getElementById('rm_button_selected_ch')).children('h2').text('');
8958 this_chid = undefined;8957 restoreNeutralChat();
8959 await getCharacters();8958 await getCharacters();
8960 await printMessages();8959 await printMessages();
8961 saveSettingsDebounced();8960 saveSettingsDebounced();
public/scripts/chats.js+28 -0
@@ -19,6 +19,8 @@ import {
19 this_chid,19 this_chid,
20 saveChatConditional,20 saveChatConditional,
21 chat_metadata,21 chat_metadata,
22 neutralCharacterName,
23 updateChatMetadata,
22} from '../script.js';24} from '../script.js';
23import { selected_group } from './group-chats.js';25import { selected_group } from './group-chats.js';
24import { power_user } from './power-user.js';26import { power_user } from './power-user.js';
@@ -1352,6 +1354,32 @@ async function verifyAttachmentsForSource(source) {
1352 }1354 }
1353}1355}
13541356
1357const NEUTRAL_CHAT_KEY = 'neutralChat';
1358
1359export function preserveNeutralChat() {
1360 if (this_chid !== undefined || selected_group || name2 !== neutralCharacterName) {
1361 return;
1362 }
1363
1364 sessionStorage.setItem(NEUTRAL_CHAT_KEY, JSON.stringify({ chat, chat_metadata }));
1365}
1366
1367export function restoreNeutralChat() {
1368 if (this_chid !== undefined || selected_group || name2 !== neutralCharacterName) {
1369 return;
1370 }
1371
1372 const neutralChat = sessionStorage.getItem(NEUTRAL_CHAT_KEY);
1373 if (!neutralChat) {
1374 return;
1375 }
1376
1377 const { chat: neutralChatData, chat_metadata: neutralChatMetadata } = JSON.parse(neutralChat);
1378 chat.splice(0, chat.length, ...neutralChatData);
1379 updateChatMetadata(neutralChatMetadata, true);
1380 sessionStorage.removeItem(NEUTRAL_CHAT_KEY);
1381}
1382
1355/**1383/**
1356 * Registers a file converter function.1384 * Registers a file converter function.
1357 * @param {string} mimeType MIME type1385 * @param {string} mimeType MIME type