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 -19Showing whitespace changes
public/script.js+14 -13
@@ -837,13 +837,12 @@ export async function selectCharacterById(id, { switchMenu = true } = {}) {
837837 if (selected_group || String(this_chid) !== String(id)) {
838838 //if clicked on a different character from what was currently selected
839839 if (!is_send_press) {
840840 await clearChat({ clearData: true });
841841 cancelTtsPlay();
842842 resetSelectedGroup();
843843 this_edit_mes_id = undefined;
844844 selected_button = 'character_edit';
845845 setCharacterId(id);
846- chat.length = 0;
847846 chat_metadata = {};
848847 await getChat();
849848 }
@@ -1351,8 +1350,7 @@ export async function deleteCharacterChatByName(characterId, fileName) {
13511350}
13521351
13531352export async function replaceCurrentChat() {
13541353 await clearChat({ clearData: true });
1355- chat.length = 0;
13561354
13571355 const chatsResponse = await fetch('/api/characters/chats', {
13581356 method: 'POST',
@@ -1530,7 +1528,12 @@ export function cancelDebouncedChatSave() {
15301528 }
15311529}
15321530
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 } = {}) {
15341537 cancelDebouncedChatSave();
15351538 cancelDebouncedMetadataSave();
15361539 closeMessageEditor();
@@ -1547,6 +1550,8 @@ export async function clearChat() {
15471550
15481551 await saveItemizedPrompts(getCurrentChatId());
15491552 itemizedPrompts.length = 0;
1553+
1554+ if (clearData) chat.length = 0;
15501555}
15511556
15521557export async function deleteLastMessage() {
@@ -1629,8 +1634,7 @@ export const reloadCurrentChat = reloadChatMutex.update.bind(reloadChatMutex);
16291634 */
16301635export async function reloadCurrentChatUnsafe() {
16311636 preserveNeutralChat();
16321637 await clearChat({ clearData: true });
1633- chat.length = 0;
16341638
16351639 if (selected_group) {
16361640 await getGroupChat(selected_group, true);
@@ -7512,9 +7516,8 @@ function getFirstMessage() {
75127516
75137517export async function openCharacterChat(file_name) {
75147518 await waitUntilCondition(() => !isChatSaving, debounce_timeout.extended, 10);
75157519 await clearChat({ clearData: true });
75167520 characters[this_chid].chat = file_name;
7517- chat.length = 0;
75187521 chat_metadata = {};
75197522 await getChat();
75207523 $('#selected_chat_pole').val(file_name);
@@ -10352,8 +10355,7 @@ export async function doNewChat({ deleteCurrentChat = false } = {}) {
1035210355
1035310356 //Fix it; New chat doesn't create while open create character menu
1035410357 await waitUntilCondition(() => !isChatSaving, debounce_timeout.extended, 10);
1035510358 await clearChat({ clearData: true });
10356- chat.length = 0;
1035710359
1035810360 chat_file_for_del = getCurrentChatDetails()?.sessionName;
1035910361
@@ -10472,8 +10474,7 @@ export async function renameChat(oldFileName, newName) {
1047210474export async function closeCurrentChat() {
1047310475 if (is_send_press == false) {
1047410476 await waitUntilCondition(() => !isChatSaving, debounce_timeout.extended, 10);
1047510477 await clearChat({ clearData: true });
10476- chat.length = 0;
1047710478 resetSelectedGroup();
1047810479 setCharacterId(undefined);
1047910480 setCharacterName('');
public/scripts/group-chats.js+3 -6
@@ -2039,14 +2039,13 @@ export async function openGroupById(groupId) {
20392039
20402040 if (selected_group !== groupId) {
20412041 groupChatQueueOrder = new Map();
20422042 await clearChat({ clearData: true });
20432043 cancelTtsPlay();
20442044 selected_group = groupId;
20452045 setCharacterId(undefined);
20462046 setCharacterName('');
20472047 setEditedMessageId(undefined);
20482048 updateChatMetadata({}, true);
2049- chat.length = 0;
20502049 await getGroupChat(groupId);
20512050 return true;
20522051 }
@@ -2152,8 +2151,7 @@ export async function createNewGroupChat(groupId) {
21522151 return;
21532152 }
21542153
21552154 await clearChat({ clearData: true });
2156- chat.length = 0;
21572155 const newChatName = humanizedDateTime();
21582156 group.chats.push(newChatName);
21592157 group.chat_id = newChatName;
@@ -2209,8 +2207,7 @@ export async function openGroupChat(groupId, chatId) {
22092207 return;
22102208 }
22112209
22122210 await clearChat({ clearData: true });
2213- chat.length = 0;
22142211 group.chat_id = chatId;
22152212 group.date_last_chat = Date.now();
22162213 updateChatMetadata({}, true);