Preserve neutral chat on reloading with commands (#2848)

* Preserve neutral chat on reloading

* Restore neutral on deleting character with bulk
This commit is contained in:
Cohee 2024-09-16 00:38:41 +03:00 committed by GitHub
parent 5bbc3ea758
commit 08f2b73ab8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 10 deletions

View File

@ -225,7 +225,7 @@ import { getBackgrounds, initBackgrounds, loadBackgroundSettings, background_set
import { hideLoader, showLoader } from './scripts/loader.js'; import { hideLoader, showLoader } from './scripts/loader.js';
import { BulkEditOverlay, CharacterContextMenu } from './scripts/BulkEditOverlay.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 { 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 { initPresetManager } from './scripts/preset-manager.js';
import { MacrosParser, evaluateMacros, getLastMessageId } from './scripts/macros.js'; import { MacrosParser, evaluateMacros, getLastMessageId } from './scripts/macros.js';
import { currentUser, setUserControls } from './scripts/user.js'; import { currentUser, setUserControls } from './scripts/user.js';
@ -1846,6 +1846,7 @@ export async function deleteLastMessage() {
} }
export async function reloadCurrentChat() { export async function reloadCurrentChat() {
preserveNeutralChat();
await clearChat(); await clearChat();
chat.length = 0; chat.length = 0;
@ -1857,6 +1858,7 @@ export async function reloadCurrentChat() {
} }
else { else {
resetChatState(); resetChatState();
restoreNeutralChat();
await getCharacters(); await getCharacters();
await printMessages(); await printMessages();
await eventSource.emit(event_types.CHAT_CHANGED, getCurrentChatId()); await eventSource.emit(event_types.CHAT_CHANGED, getCurrentChatId());
@ -5700,10 +5702,10 @@ export function deactivateSendButtons() {
} }
export function resetChatState() { 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) //unsets expected chid before reloading (related to getCharacters/printCharacters from using old arrays)
this_chid = undefined; 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 // sets up system user to tell user about having deleted a character
chat.splice(0, chat.length, ...SAFETY_CHAT); chat.splice(0, chat.length, ...SAFETY_CHAT);
// resets chat metadata // 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. * It also ensures to save the settings after all the operations.
*/ */
async function removeCharacterFromUI() { async function removeCharacterFromUI() {
preserveNeutralChat();
await clearChat(); await clearChat();
$('#character_cross').click(); $('#character_cross').trigger('click');
this_chid = undefined; resetChatState();
characters.length = 0;
name2 = systemUserName;
chat.splice(0, chat.length, ...SAFETY_CHAT);
chat_metadata = {};
$(document.getElementById('rm_button_selected_ch')).children('h2').text(''); $(document.getElementById('rm_button_selected_ch')).children('h2').text('');
this_chid = undefined; restoreNeutralChat();
await getCharacters(); await getCharacters();
await printMessages(); await printMessages();
saveSettingsDebounced(); saveSettingsDebounced();

View File

@ -19,6 +19,8 @@ import {
this_chid, this_chid,
saveChatConditional, saveChatConditional,
chat_metadata, chat_metadata,
neutralCharacterName,
updateChatMetadata,
} from '../script.js'; } from '../script.js';
import { selected_group } from './group-chats.js'; import { selected_group } from './group-chats.js';
import { power_user } from './power-user.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. * Registers a file converter function.
* @param {string} mimeType MIME type * @param {string} mimeType MIME type