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
225225import { hideLoader, showLoader } from './scripts/loader.js';
226226import { BulkEditOverlay, CharacterContextMenu } from './scripts/BulkEditOverlay.js';
227227import { loadFeatherlessModels, loadMancerModels, loadOllamaModels, loadTogetherAIModels, loadInfermaticAIModels, loadOpenRouterModels, loadVllmModels, loadAphroditeModels, loadDreamGenModels, initTextGenModels, loadTabbyModels } from './scripts/textgen-models.js';
228228import { appendFileContent, hasPendingFileAttachment, populateFileAttachment, decodeStyleTags, encodeStyleTags, isExternalMediaAllowed, getCurrentEntityId, preserveNeutralChat, restoreNeutralChat } from './scripts/chats.js';
229229import { initPresetManager } from './scripts/preset-manager.js';
230230import { MacrosParser, evaluateMacros, getLastMessageId } from './scripts/macros.js';
231231import { currentUser, setUserControls } from './scripts/user.js';
@@ -1846,6 +1846,7 @@ export async function deleteLastMessage() {
18461846}
18471847
18481848export async function reloadCurrentChat() {
1849+ preserveNeutralChat();
18491850 await clearChat();
18501851 chat.length = 0;
18511852
@@ -1857,6 +1858,7 @@ export async function reloadCurrentChat() {
18571858 }
18581859 else {
18591860 resetChatState();
1861+ restoreNeutralChat();
18601862 await getCharacters();
18611863 await printMessages();
18621864 await eventSource.emit(event_types.CHAT_CHANGED, getCurrentChatId());
@@ -5700,10 +5702,10 @@ export function deactivateSendButtons() {
57005702}
57015703
57025704export function resetChatState() {
5705+ // replaces deleted charcter name with system user since it will be displayed next.
5706+ name2 = (this_chid === undefined && neutralCharacterName) ? neutralCharacterName : systemUserName;
57035707 //unsets expected chid before reloading (related to getCharacters/printCharacters from using old arrays)
57045708 this_chid = undefined;
5705- // replaces deleted charcter name with system user since it will be displayed next.
5706- name2 = systemUserName;
57075709 // sets up system user to tell user about having deleted a character
57085710 chat.splice(0, chat.length, ...SAFETY_CHAT);
57095711 // resets chat metadata
@@ -8947,15 +8949,12 @@ export async function deleteCharacter(characterKey, { deleteChats = true } = {})
89478949 * It also ensures to save the settings after all the operations.
89488950 */
89498951async function removeCharacterFromUI() {
8952+ preserveNeutralChat();
89508953 await clearChat();
89518954 $('#character_cross').clicktrigger('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 = {};
89578956 $(document.getElementById('rm_button_selected_ch')).children('h2').text('');
8958- this_chid = undefined;
8957+ restoreNeutralChat();
89598958 await getCharacters();
89608959 await printMessages();
89618960 saveSettingsDebounced();
public/scripts/chats.js+28 -0
@@ -19,6 +19,8 @@ import {
1919 this_chid,
2020 saveChatConditional,
2121 chat_metadata,
22+ neutralCharacterName,
23+ updateChatMetadata,
2224} from '../script.js';
2325import { selected_group } from './group-chats.js';
2426import { power_user } from './power-user.js';
@@ -1352,6 +1354,32 @@ async function verifyAttachmentsForSource(source) {
13521354 }
13531355}
13541356
1357+const NEUTRAL_CHAT_KEY = 'neutralChat';
1358+
1359+export 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+
1367+export 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+
13551383/**
13561384 * Registers a file converter function.
13571385 * @param {string} mimeType MIME type