Preserve neutral chat on reloading with commands (#2848) * Preserve neutral chat on reloading * Restore neutral on deleting character with bulk
Signed| @@ -225,7 +225,7 @@ import { getBackgrounds, initBackgrounds, loadBackgroundSettings, background_set | ||
| 225 | 225 | import { hideLoader, showLoader } from './scripts/loader.js'; |
| 226 | 226 | import { BulkEditOverlay, CharacterContextMenu } from './scripts/BulkEditOverlay.js'; |
| 227 | 227 | import { loadFeatherlessModels, loadMancerModels, loadOllamaModels, loadTogetherAIModels, loadInfermaticAIModels, loadOpenRouterModels, loadVllmModels, loadAphroditeModels, loadDreamGenModels, initTextGenModels, loadTabbyModels } from './scripts/textgen-models.js'; |
| 228 | 228 | import { appendFileContent, hasPendingFileAttachment, populateFileAttachment, decodeStyleTags, encodeStyleTags, isExternalMediaAllowed, getCurrentEntityId, preserveNeutralChat, restoreNeutralChat } from './scripts/chats.js'; |
| 229 | 229 | import { initPresetManager } from './scripts/preset-manager.js'; |
| 230 | 230 | import { MacrosParser, evaluateMacros, getLastMessageId } from './scripts/macros.js'; |
| 231 | 231 | import { currentUser, setUserControls } from './scripts/user.js'; |
| @@ -1846,6 +1846,7 @@ export async function deleteLastMessage() { | ||
| 1846 | 1846 | } |
| 1847 | 1847 | |
| 1848 | 1848 | export async function reloadCurrentChat() { |
| 1849 | + preserveNeutralChat(); | |
| 1849 | 1850 | await clearChat(); |
| 1850 | 1851 | chat.length = 0; |
| 1851 | 1852 | |
| @@ -1857,6 +1858,7 @@ export async function reloadCurrentChat() { | ||
| 1857 | 1858 | } |
| 1858 | 1859 | else { |
| 1859 | 1860 | resetChatState(); |
| 1861 | + restoreNeutralChat(); | |
| 1860 | 1862 | await getCharacters(); |
| 1861 | 1863 | await printMessages(); |
| 1862 | 1864 | await eventSource.emit(event_types.CHAT_CHANGED, getCurrentChatId()); |
| @@ -5700,10 +5702,10 @@ export function deactivateSendButtons() { | ||
| 5700 | 5702 | } |
| 5701 | 5703 | |
| 5702 | 5704 | export 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 | 5707 | //unsets expected chid before reloading (related to getCharacters/printCharacters from using old arrays) |
| 5704 | 5708 | this_chid = undefined; |
| 5705 | - // replaces deleted charcter name with system user since it will be displayed next. | |
| 5706 | - name2 = systemUserName; | |
| 5707 | 5709 | // sets up system user to tell user about having deleted a character |
| 5708 | 5710 | chat.splice(0, chat.length, ...SAFETY_CHAT); |
| 5709 | 5711 | // resets chat metadata |
| @@ -8947,15 +8949,12 @@ export async function deleteCharacter(characterKey, { deleteChats = true } = {}) | ||
| 8947 | 8949 | * It also ensures to save the settings after all the operations. |
| 8948 | 8950 | */ |
| 8949 | 8951 | async function removeCharacterFromUI() { |
| 8952 | + preserveNeutralChat(); | |
| 8950 | 8953 | await clearChat(); |
| 8951 | 8954 | $('#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 = {}; | |
| 8957 | 8956 | $(document.getElementById('rm_button_selected_ch')).children('h2').text(''); |
| 8958 | - this_chid = undefined; | |
| 8957 | + restoreNeutralChat(); | |
| 8959 | 8958 | await getCharacters(); |
| 8960 | 8959 | await printMessages(); |
| 8961 | 8960 | saveSettingsDebounced(); |
| @@ -19,6 +19,8 @@ import { | ||
| 19 | 19 | this_chid, |
| 20 | 20 | saveChatConditional, |
| 21 | 21 | chat_metadata, |
| 22 | + neutralCharacterName, | |
| 23 | + updateChatMetadata, | |
| 22 | 24 | } from '../script.js'; |
| 23 | 25 | import { selected_group } from './group-chats.js'; |
| 24 | 26 | import { power_user } from './power-user.js'; |
| @@ -1352,6 +1354,32 @@ async function verifyAttachmentsForSource(source) { | ||
| 1352 | 1354 | } |
| 1353 | 1355 | } |
| 1354 | 1356 | |
| 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 | + | |
| 1355 | 1383 | /** |
| 1356 | 1384 | * Registers a file converter function. |
| 1357 | 1385 | * @param {string} mimeType MIME type |