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