From d663be53acb7e41b034f3d8a8bde100295e8d14c Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Sun, 1 Sep 2024 23:12:33 +0200 Subject: [PATCH] Refactor deleteSwipe from callback into func --- public/script.js | 69 +++++++++++++++++++++++++------- public/scripts/slash-commands.js | 34 ++-------------- 2 files changed, 59 insertions(+), 44 deletions(-) diff --git a/public/script.js b/public/script.js index 57aa7643f..b973c8fe8 100644 --- a/public/script.js +++ b/public/script.js @@ -78,8 +78,6 @@ import { renderStoryString, sortEntitiesList, registerDebugFunction, - ui_mode, - switchSimpleMode, flushEphemeralStoppingStrings, context_presets, resetMovableStyles, @@ -6323,15 +6321,11 @@ export function setUserName(value) { } async function doOnboarding(avatarId) { - let simpleUiMode = false; const template = $('#onboarding_template .onboarding'); - template.find('input[name="enable_simple_mode"]').on('input', function () { - simpleUiMode = $(this).is(':checked'); - }); let userName = await callGenericPopup(template, POPUP_TYPE.INPUT, currentUser?.name || name1, { rows: 2, wide: true, large: true }); if (userName) { - userName = userName.replace('\n', ' '); + userName = String(userName).replace('\n', ' '); setUserName(userName); console.log(`Binding persona ${avatarId} to name ${userName}`); power_user.personas[avatarId] = userName; @@ -6340,12 +6334,6 @@ async function doOnboarding(avatarId) { position: persona_description_positions.IN_PROMPT, }; } - - if (simpleUiMode) { - power_user.ui_mode = ui_mode.SIMPLE; - $('#ui_mode_select').val(power_user.ui_mode); - switchSimpleMode(); - } } function reloadLoop() { @@ -7465,6 +7453,53 @@ export function hideSwipeButtons() { $('#chat').find('.swipe_left').css('display', 'none'); } +/** + * Deletes a swipe from the chat. + * + * @param {number?} swipeId - The ID of the swipe to delete. If not provided, the current swipe will be deleted. + * @returns {Promise|undefined} - The ID of the new swipe after deletion. + */ +export async function deleteSwipe(swipeId = null) { + if (swipeId && (isNaN(swipeId) || swipeId < 0)) { + toastr.warning('Invalid swipe ID: ' + swipeId); + return; + } + + const lastMessage = chat[chat.length - 1]; + if (!lastMessage || !Array.isArray(lastMessage.swipes) || !lastMessage.swipes.length) { + toastr.warning('No messages to delete swipes from.'); + return; + } + + if (lastMessage.swipes.length <= 1) { + toastr.warning('Can\'t delete the last swipe.'); + return; + } + + swipeId = swipeId ?? lastMessage.swipe_id; + + if (swipeId < 0 || swipeId >= lastMessage.swipes.length) { + toastr.warning(`Invalid swipe ID: ${swipeId + 1}`); + return; + } + + lastMessage.swipes.splice(swipeId, 1); + + if (Array.isArray(lastMessage.swipe_info) && lastMessage.swipe_info.length) { + lastMessage.swipe_info.splice(swipeId, 1); + } + + // Select the next swip, or the one before if it was the last one + const newSwipeId = Math.min(swipeId, lastMessage.swipes.length - 1); + lastMessage.swipe_id = newSwipeId; + lastMessage.mes = lastMessage.swipes[newSwipeId]; + + await saveChatConditional(); + await reloadCurrentChat(); + + return newSwipeId; +} + export async function saveMetadata() { if (selected_group) { await editGroup(selected_group, true, false); @@ -8008,7 +8043,7 @@ window['SillyTavern'].getContext = function () { registerHelper: () => { }, registerMacro: MacrosParser.registerMacro.bind(MacrosParser), unregisterMacro: MacrosParser.unregisterMacro.bind(MacrosParser), - registedDebugFunction: registerDebugFunction, + registerDebugFunction: registerDebugFunction, /** @deprecated Use renderExtensionTemplateAsync instead. */ renderExtensionTemplate: renderExtensionTemplate, renderExtensionTemplateAsync: renderExtensionTemplateAsync, @@ -8943,6 +8978,12 @@ function addDebugFunctions() { await reloadCurrentChat(); }; + registerDebugFunction('forceOnboarding', 'Force onboarding', 'Forces the onboarding process to restart.', async () => { + firstRun = true; + await saveSettings(); + location.reload(); + }); + registerDebugFunction('backfillTokenCounts', 'Backfill token counters', `Recalculates token counts of all messages in the current chat to refresh the counters. Useful when you switch between models that have different tokenizers. diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index f854dbd02..749578de8 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -11,6 +11,7 @@ import { comment_avatar, deactivateSendButtons, default_avatar, + deleteSwipe, eventSource, event_types, extension_prompt_roles, @@ -2309,37 +2310,10 @@ async function addSwipeCallback(args, value) { } async function deleteSwipeCallback(_, arg) { - const lastMessage = chat[chat.length - 1]; + // Take the provided argument. Null if none provided, which will target the current swipe. + const swipeId = arg && !isNaN(Number(arg)) ? (Number(arg) - 1) : null; - if (!lastMessage || !Array.isArray(lastMessage.swipes) || !lastMessage.swipes.length) { - toastr.warning('No messages to delete swipes from.'); - return ''; - } - - if (lastMessage.swipes.length <= 1) { - toastr.warning('Can\'t delete the last swipe.'); - return ''; - } - - const swipeId = arg && !isNaN(Number(arg)) ? (Number(arg) - 1) : lastMessage.swipe_id; - - if (swipeId < 0 || swipeId >= lastMessage.swipes.length) { - toastr.warning(`Invalid swipe ID: ${swipeId + 1}`); - return ''; - } - - lastMessage.swipes.splice(swipeId, 1); - - if (Array.isArray(lastMessage.swipe_info) && lastMessage.swipe_info.length) { - lastMessage.swipe_info.splice(swipeId, 1); - } - - const newSwipeId = Math.min(swipeId, lastMessage.swipes.length - 1); - lastMessage.swipe_id = newSwipeId; - lastMessage.mes = lastMessage.swipes[newSwipeId]; - - await saveChatConditional(); - await reloadCurrentChat(); + const newSwipeId = await deleteSwipe(swipeId); return String(newSwipeId); }