Add `clearData` option to `clearChat` function (#5091) * Centralized `await clearChat();` and `chat.length = 0;` into `wipeChat()` * Added `clearData` argument to `clearChat`. --------- Co-authored-by: user <user@exmaple.com>
Signed| @@ -837,13 +837,12 @@ export async function selectCharacterById(id, { switchMenu = true } = {}) { | |||
| 837 | if (selected_group || String(this_chid) !== String(id)) { | 837 | if (selected_group || String(this_chid) !== String(id)) { |
| 838 | //if clicked on a different character from what was currently selected | 838 | //if clicked on a different character from what was currently selected |
| 839 | if (!is_send_press) { | 839 | if (!is_send_press) { |
| 840 | await clearChat(); | 840 | await clearChat({ clearData: true }); |
| 841 | cancelTtsPlay(); | 841 | cancelTtsPlay(); |
| 842 | resetSelectedGroup(); | 842 | resetSelectedGroup(); |
| 843 | this_edit_mes_id = undefined; | 843 | this_edit_mes_id = undefined; |
| 844 | selected_button = 'character_edit'; | 844 | selected_button = 'character_edit'; |
| 845 | setCharacterId(id); | 845 | setCharacterId(id); |
| 846 | chat.length = 0; | ||
| 847 | chat_metadata = {}; | 846 | chat_metadata = {}; |
| 848 | await getChat(); | 847 | await getChat(); |
| 849 | } | 848 | } |
| @@ -1351,8 +1350,7 @@ export async function deleteCharacterChatByName(characterId, fileName) { | |||
| 1351 | } | 1350 | } |
| 1352 | 1351 | ||
| 1353 | export async function replaceCurrentChat() { | 1352 | export async function replaceCurrentChat() { |
| 1354 | await clearChat(); | 1353 | await clearChat({ clearData: true }); |
| 1355 | chat.length = 0; | ||
| 1356 | 1354 | ||
| 1357 | const chatsResponse = await fetch('/api/characters/chats', { | 1355 | const chatsResponse = await fetch('/api/characters/chats', { |
| 1358 | method: 'POST', | 1356 | method: 'POST', |
| @@ -1530,7 +1528,12 @@ export function cancelDebouncedChatSave() { | |||
| 1530 | } | 1528 | } |
| 1531 | } | 1529 | } |
| 1532 | 1530 | ||
| 1533 | export async function clearChat() { | 1531 | /** |
| 1532 | * Visually removes all chat message elements. | ||
| 1533 | * @param {object} [options] Options | ||
| 1534 | * @param {boolean} [options.clearData=false] Optionally clear the chat array's contents. | ||
| 1535 | */ | ||
| 1536 | export async function clearChat({ clearData = false } = {}) { | ||
| 1534 | cancelDebouncedChatSave(); | 1537 | cancelDebouncedChatSave(); |
| 1535 | cancelDebouncedMetadataSave(); | 1538 | cancelDebouncedMetadataSave(); |
| 1536 | closeMessageEditor(); | 1539 | closeMessageEditor(); |
| @@ -1547,6 +1550,8 @@ export async function clearChat() { | |||
| 1547 | 1550 | ||
| 1548 | await saveItemizedPrompts(getCurrentChatId()); | 1551 | await saveItemizedPrompts(getCurrentChatId()); |
| 1549 | itemizedPrompts.length = 0; | 1552 | itemizedPrompts.length = 0; |
| 1553 | |||
| 1554 | if (clearData) chat.length = 0; | ||
| 1550 | } | 1555 | } |
| 1551 | 1556 | ||
| 1552 | export async function deleteLastMessage() { | 1557 | export async function deleteLastMessage() { |
| @@ -1629,8 +1634,7 @@ export const reloadCurrentChat = reloadChatMutex.update.bind(reloadChatMutex); | |||
| 1629 | */ | 1634 | */ |
| 1630 | export async function reloadCurrentChatUnsafe() { | 1635 | export async function reloadCurrentChatUnsafe() { |
| 1631 | preserveNeutralChat(); | 1636 | preserveNeutralChat(); |
| 1632 | await clearChat(); | 1637 | await clearChat({ clearData: true }); |
| 1633 | chat.length = 0; | ||
| 1634 | 1638 | ||
| 1635 | if (selected_group) { | 1639 | if (selected_group) { |
| 1636 | await getGroupChat(selected_group, true); | 1640 | await getGroupChat(selected_group, true); |
| @@ -7512,9 +7516,8 @@ function getFirstMessage() { | |||
| 7512 | 7516 | ||
| 7513 | export async function openCharacterChat(file_name) { | 7517 | export async function openCharacterChat(file_name) { |
| 7514 | await waitUntilCondition(() => !isChatSaving, debounce_timeout.extended, 10); | 7518 | await waitUntilCondition(() => !isChatSaving, debounce_timeout.extended, 10); |
| 7515 | await clearChat(); | 7519 | await clearChat({ clearData: true }); |
| 7516 | characters[this_chid].chat = file_name; | 7520 | characters[this_chid].chat = file_name; |
| 7517 | chat.length = 0; | ||
| 7518 | chat_metadata = {}; | 7521 | chat_metadata = {}; |
| 7519 | await getChat(); | 7522 | await getChat(); |
| 7520 | $('#selected_chat_pole').val(file_name); | 7523 | $('#selected_chat_pole').val(file_name); |
| @@ -10352,8 +10355,7 @@ export async function doNewChat({ deleteCurrentChat = false } = {}) { | |||
| 10352 | 10355 | ||
| 10353 | //Fix it; New chat doesn't create while open create character menu | 10356 | //Fix it; New chat doesn't create while open create character menu |
| 10354 | await waitUntilCondition(() => !isChatSaving, debounce_timeout.extended, 10); | 10357 | await waitUntilCondition(() => !isChatSaving, debounce_timeout.extended, 10); |
| 10355 | await clearChat(); | 10358 | await clearChat({ clearData: true }); |
| 10356 | chat.length = 0; | ||
| 10357 | 10359 | ||
| 10358 | chat_file_for_del = getCurrentChatDetails()?.sessionName; | 10360 | chat_file_for_del = getCurrentChatDetails()?.sessionName; |
| 10359 | 10361 | ||
| @@ -10472,8 +10474,7 @@ export async function renameChat(oldFileName, newName) { | |||
| 10472 | export async function closeCurrentChat() { | 10474 | export async function closeCurrentChat() { |
| 10473 | if (is_send_press == false) { | 10475 | if (is_send_press == false) { |
| 10474 | await waitUntilCondition(() => !isChatSaving, debounce_timeout.extended, 10); | 10476 | await waitUntilCondition(() => !isChatSaving, debounce_timeout.extended, 10); |
| 10475 | await clearChat(); | 10477 | await clearChat({ clearData: true }); |
| 10476 | chat.length = 0; | ||
| 10477 | resetSelectedGroup(); | 10478 | resetSelectedGroup(); |
| 10478 | setCharacterId(undefined); | 10479 | setCharacterId(undefined); |
| 10479 | setCharacterName(''); | 10480 | setCharacterName(''); |
| @@ -2039,14 +2039,13 @@ export async function openGroupById(groupId) { | |||
| 2039 | 2039 | ||
| 2040 | if (selected_group !== groupId) { | 2040 | if (selected_group !== groupId) { |
| 2041 | groupChatQueueOrder = new Map(); | 2041 | groupChatQueueOrder = new Map(); |
| 2042 | await clearChat(); | 2042 | await clearChat({ clearData: true }); |
| 2043 | cancelTtsPlay(); | 2043 | cancelTtsPlay(); |
| 2044 | selected_group = groupId; | 2044 | selected_group = groupId; |
| 2045 | setCharacterId(undefined); | 2045 | setCharacterId(undefined); |
| 2046 | setCharacterName(''); | 2046 | setCharacterName(''); |
| 2047 | setEditedMessageId(undefined); | 2047 | setEditedMessageId(undefined); |
| 2048 | updateChatMetadata({}, true); | 2048 | updateChatMetadata({}, true); |
| 2049 | chat.length = 0; | ||
| 2050 | await getGroupChat(groupId); | 2049 | await getGroupChat(groupId); |
| 2051 | return true; | 2050 | return true; |
| 2052 | } | 2051 | } |
| @@ -2152,8 +2151,7 @@ export async function createNewGroupChat(groupId) { | |||
| 2152 | return; | 2151 | return; |
| 2153 | } | 2152 | } |
| 2154 | 2153 | ||
| 2155 | await clearChat(); | 2154 | await clearChat({ clearData: true }); |
| 2156 | chat.length = 0; | ||
| 2157 | const newChatName = humanizedDateTime(); | 2155 | const newChatName = humanizedDateTime(); |
| 2158 | group.chats.push(newChatName); | 2156 | group.chats.push(newChatName); |
| 2159 | group.chat_id = newChatName; | 2157 | group.chat_id = newChatName; |
| @@ -2209,8 +2207,7 @@ export async function openGroupChat(groupId, chatId) { | |||
| 2209 | return; | 2207 | return; |
| 2210 | } | 2208 | } |
| 2211 | 2209 | ||
| 2212 | await clearChat(); | 2210 | await clearChat({ clearData: true }); |
| 2213 | chat.length = 0; | ||
| 2214 | group.chat_id = chatId; | 2211 | group.chat_id = chatId; |
| 2215 | group.date_last_chat = Date.now(); | 2212 | group.date_last_chat = Date.now(); |
| 2216 | updateChatMetadata({}, true); | 2213 | updateChatMetadata({}, true); |