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>

b64c279473be4b17be16b52caf3ce9dbf65b8eeb

DeclineThyself <235079501+DeclineThyself@users.noreply.github.com>

Signed
2 files changed, +17 -19Ignore whitespace
public/script.js+14 -13
@@ -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 selected838 //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}
13521351
1353export async function replaceCurrentChat() {1352export async function replaceCurrentChat() {
1354 await clearChat();1353 await clearChat({ clearData: true });
1355 chat.length = 0;
13561354
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}
15321530
1533export 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 */
1536export 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() {
15471550
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}
15511556
1552export async function deleteLastMessage() {1557export async function deleteLastMessage() {
@@ -1629,8 +1634,7 @@ export const reloadCurrentChat = reloadChatMutex.update.bind(reloadChatMutex);
1629 */1634 */
1630export async function reloadCurrentChatUnsafe() {1635export async function reloadCurrentChatUnsafe() {
1631 preserveNeutralChat();1636 preserveNeutralChat();
1632 await clearChat();1637 await clearChat({ clearData: true });
1633 chat.length = 0;
16341638
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() {
75127516
7513export async function openCharacterChat(file_name) {7517export 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 } = {}) {
1035210355
10353 //Fix it; New chat doesn't create while open create character menu10356 //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;
1035710359
10358 chat_file_for_del = getCurrentChatDetails()?.sessionName;10360 chat_file_for_del = getCurrentChatDetails()?.sessionName;
1035910361
@@ -10472,8 +10474,7 @@ export async function renameChat(oldFileName, newName) {
10472export async function closeCurrentChat() {10474export 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('');
public/scripts/group-chats.js+3 -6
@@ -2039,14 +2039,13 @@ export async function openGroupById(groupId) {
20392039
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 }
21542153
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 }
22112209
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);