Refactor deleteSwipe from callback into func

d663be53acb7e41b034f3d8a8bde100295e8d14c

Wolfsblvt <wolfsblvt@gmail.com>

2 files changed, +59 -44Showing whitespace changes
public/script.js+55 -14
@@ -78,8 +78,6 @@ import {
7878 renderStoryString,
7979 sortEntitiesList,
8080 registerDebugFunction,
81- ui_mode,
82- switchSimpleMode,
8381 flushEphemeralStoppingStrings,
8482 context_presets,
8583 resetMovableStyles,
@@ -6323,15 +6321,11 @@ export function setUserName(value) {
63236321}
63246322
63256323async function doOnboarding(avatarId) {
6326- let simpleUiMode = false;
63276324 const template = $('#onboarding_template .onboarding');
6328- template.find('input[name="enable_simple_mode"]').on('input', function () {
6329- simpleUiMode = $(this).is(':checked');
6330- });
63316325 let userName = await callGenericPopup(template, POPUP_TYPE.INPUT, currentUser?.name || name1, { rows: 2, wide: true, large: true });
63326326
63336327 if (userName) {
63346328 userName = String(userName).replace('\n', ' ');
63356329 setUserName(userName);
63366330 console.log(`Binding persona ${avatarId} to name ${userName}`);
63376331 power_user.personas[avatarId] = userName;
@@ -6340,12 +6334,6 @@ async function doOnboarding(avatarId) {
63406334 position: persona_description_positions.IN_PROMPT,
63416335 };
63426336 }
6343-
6344- if (simpleUiMode) {
6345- power_user.ui_mode = ui_mode.SIMPLE;
6346- $('#ui_mode_select').val(power_user.ui_mode);
6347- switchSimpleMode();
6348- }
63496337}
63506338
63516339function reloadLoop() {
@@ -7465,6 +7453,53 @@ export function hideSwipeButtons() {
74657453 $('#chat').find('.swipe_left').css('display', 'none');
74667454}
74677455
7456+/**
7457+ * Deletes a swipe from the chat.
7458+ *
7459+ * @param {number?} swipeId - The ID of the swipe to delete. If not provided, the current swipe will be deleted.
7460+ * @returns {Promise<number>|undefined} - The ID of the new swipe after deletion.
7461+ */
7462+export async function deleteSwipe(swipeId = null) {
7463+ if (swipeId && (isNaN(swipeId) || swipeId < 0)) {
7464+ toastr.warning('Invalid swipe ID: ' + swipeId);
7465+ return;
7466+ }
7467+
7468+ const lastMessage = chat[chat.length - 1];
7469+ if (!lastMessage || !Array.isArray(lastMessage.swipes) || !lastMessage.swipes.length) {
7470+ toastr.warning('No messages to delete swipes from.');
7471+ return;
7472+ }
7473+
7474+ if (lastMessage.swipes.length <= 1) {
7475+ toastr.warning('Can\'t delete the last swipe.');
7476+ return;
7477+ }
7478+
7479+ swipeId = swipeId ?? lastMessage.swipe_id;
7480+
7481+ if (swipeId < 0 || swipeId >= lastMessage.swipes.length) {
7482+ toastr.warning(`Invalid swipe ID: ${swipeId + 1}`);
7483+ return;
7484+ }
7485+
7486+ lastMessage.swipes.splice(swipeId, 1);
7487+
7488+ if (Array.isArray(lastMessage.swipe_info) && lastMessage.swipe_info.length) {
7489+ lastMessage.swipe_info.splice(swipeId, 1);
7490+ }
7491+
7492+ // Select the next swip, or the one before if it was the last one
7493+ const newSwipeId = Math.min(swipeId, lastMessage.swipes.length - 1);
7494+ lastMessage.swipe_id = newSwipeId;
7495+ lastMessage.mes = lastMessage.swipes[newSwipeId];
7496+
7497+ await saveChatConditional();
7498+ await reloadCurrentChat();
7499+
7500+ return newSwipeId;
7501+}
7502+
74687503export async function saveMetadata() {
74697504 if (selected_group) {
74707505 await editGroup(selected_group, true, false);
@@ -8008,7 +8043,7 @@ window['SillyTavern'].getContext = function () {
80088043 registerHelper: () => { },
80098044 registerMacro: MacrosParser.registerMacro.bind(MacrosParser),
80108045 unregisterMacro: MacrosParser.unregisterMacro.bind(MacrosParser),
80118046 registedDebugFunctionregisterDebugFunction: registerDebugFunction,
80128047 /** @deprecated Use renderExtensionTemplateAsync instead. */
80138048 renderExtensionTemplate: renderExtensionTemplate,
80148049 renderExtensionTemplateAsync: renderExtensionTemplateAsync,
@@ -8943,6 +8978,12 @@ function addDebugFunctions() {
89438978 await reloadCurrentChat();
89448979 };
89458980
8981+ registerDebugFunction('forceOnboarding', 'Force onboarding', 'Forces the onboarding process to restart.', async () => {
8982+ firstRun = true;
8983+ await saveSettings();
8984+ location.reload();
8985+ });
8986+
89468987 registerDebugFunction('backfillTokenCounts', 'Backfill token counters',
89478988 `Recalculates token counts of all messages in the current chat to refresh the counters.
89488989 Useful when you switch between models that have different tokenizers.
public/scripts/slash-commands.js+4 -30
@@ -11,6 +11,7 @@ import {
1111 comment_avatar,
1212 deactivateSendButtons,
1313 default_avatar,
14+ deleteSwipe,
1415 eventSource,
1516 event_types,
1617 extension_prompt_roles,
@@ -2309,37 +2310,10 @@ async function addSwipeCallback(args, value) {
23092310}
23102311
23112312async function deleteSwipeCallback(_, arg) {
2312- const lastMessage = chat[chat.length - 1];
2313+ // Take the provided argument. Null if none provided, which will target the current swipe.
2313-
2314+ const swipeId = arg && !isNaN(Number(arg)) ? (Number(arg) - 1) : null;
2314- if (!lastMessage || !Array.isArray(lastMessage.swipes) || !lastMessage.swipes.length) {
2315- toastr.warning('No messages to delete swipes from.');
2316- return '';
2317- }
2318-
2319- if (lastMessage.swipes.length <= 1) {
2320- toastr.warning('Can\'t delete the last swipe.');
2321- return '';
2322- }
2323-
2324- const swipeId = arg && !isNaN(Number(arg)) ? (Number(arg) - 1) : lastMessage.swipe_id;
23252315
2326- if (swipeId < 0 || swipeId >= lastMessage.swipes.length) {
2316+ const newSwipeId = await deleteSwipe(swipeId);
2327- toastr.warning(`Invalid swipe ID: ${swipeId + 1}`);
2328- return '';
2329- }
2330-
2331- lastMessage.swipes.splice(swipeId, 1);
2332-
2333- if (Array.isArray(lastMessage.swipe_info) && lastMessage.swipe_info.length) {
2334- lastMessage.swipe_info.splice(swipeId, 1);
2335- }
2336-
2337- const newSwipeId = Math.min(swipeId, lastMessage.swipes.length - 1);
2338- lastMessage.swipe_id = newSwipeId;
2339- lastMessage.mes = lastMessage.swipes[newSwipeId];
2340-
2341- await saveChatConditional();
2342- await reloadCurrentChat();
23432317
23442318 return String(newSwipeId);
23452319}