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 {
78 renderStoryString,78 renderStoryString,
79 sortEntitiesList,79 sortEntitiesList,
80 registerDebugFunction,80 registerDebugFunction,
81 ui_mode,
82 switchSimpleMode,
83 flushEphemeralStoppingStrings,81 flushEphemeralStoppingStrings,
84 context_presets,82 context_presets,
85 resetMovableStyles,83 resetMovableStyles,
@@ -6323,15 +6321,11 @@ export function setUserName(value) {
6323}6321}
63246322
6325async function doOnboarding(avatarId) {6323async function doOnboarding(avatarId) {
6326 let simpleUiMode = false;
6327 const template = $('#onboarding_template .onboarding');6324 const template = $('#onboarding_template .onboarding');
6328 template.find('input[name="enable_simple_mode"]').on('input', function () {
6329 simpleUiMode = $(this).is(':checked');
6330 });
6331 let userName = await callGenericPopup(template, POPUP_TYPE.INPUT, currentUser?.name || name1, { rows: 2, wide: true, large: true });6325 let userName = await callGenericPopup(template, POPUP_TYPE.INPUT, currentUser?.name || name1, { rows: 2, wide: true, large: true });
63326326
6333 if (userName) {6327 if (userName) {
6334 userName = userName.replace('\n', ' ');6328 userName = String(userName).replace('\n', ' ');
6335 setUserName(userName);6329 setUserName(userName);
6336 console.log(`Binding persona ${avatarId} to name ${userName}`);6330 console.log(`Binding persona ${avatarId} to name ${userName}`);
6337 power_user.personas[avatarId] = userName;6331 power_user.personas[avatarId] = userName;
@@ -6340,12 +6334,6 @@ async function doOnboarding(avatarId) {
6340 position: persona_description_positions.IN_PROMPT,6334 position: persona_description_positions.IN_PROMPT,
6341 };6335 };
6342 }6336 }
6343
6344 if (simpleUiMode) {
6345 power_user.ui_mode = ui_mode.SIMPLE;
6346 $('#ui_mode_select').val(power_user.ui_mode);
6347 switchSimpleMode();
6348 }
6349}6337}
63506338
6351function reloadLoop() {6339function reloadLoop() {
@@ -7465,6 +7453,53 @@ export function hideSwipeButtons() {
7465 $('#chat').find('.swipe_left').css('display', 'none');7453 $('#chat').find('.swipe_left').css('display', 'none');
7466}7454}
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 */
7462export 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
7468export async function saveMetadata() {7503export async function saveMetadata() {
7469 if (selected_group) {7504 if (selected_group) {
7470 await editGroup(selected_group, true, false);7505 await editGroup(selected_group, true, false);
@@ -8008,7 +8043,7 @@ window['SillyTavern'].getContext = function () {
8008 registerHelper: () => { },8043 registerHelper: () => { },
8009 registerMacro: MacrosParser.registerMacro.bind(MacrosParser),8044 registerMacro: MacrosParser.registerMacro.bind(MacrosParser),
8010 unregisterMacro: MacrosParser.unregisterMacro.bind(MacrosParser),8045 unregisterMacro: MacrosParser.unregisterMacro.bind(MacrosParser),
8011 registedDebugFunction: registerDebugFunction,8046 registerDebugFunction: registerDebugFunction,
8012 /** @deprecated Use renderExtensionTemplateAsync instead. */8047 /** @deprecated Use renderExtensionTemplateAsync instead. */
8013 renderExtensionTemplate: renderExtensionTemplate,8048 renderExtensionTemplate: renderExtensionTemplate,
8014 renderExtensionTemplateAsync: renderExtensionTemplateAsync,8049 renderExtensionTemplateAsync: renderExtensionTemplateAsync,
@@ -8943,6 +8978,12 @@ function addDebugFunctions() {
8943 await reloadCurrentChat();8978 await reloadCurrentChat();
8944 };8979 };
89458980
8981 registerDebugFunction('forceOnboarding', 'Force onboarding', 'Forces the onboarding process to restart.', async () => {
8982 firstRun = true;
8983 await saveSettings();
8984 location.reload();
8985 });
8986
8946 registerDebugFunction('backfillTokenCounts', 'Backfill token counters',8987 registerDebugFunction('backfillTokenCounts', 'Backfill token counters',
8947 `Recalculates token counts of all messages in the current chat to refresh the counters.8988 `Recalculates token counts of all messages in the current chat to refresh the counters.
8948 Useful when you switch between models that have different tokenizers.8989 Useful when you switch between models that have different tokenizers.
public/scripts/slash-commands.js+4 -30
@@ -11,6 +11,7 @@ import {
11 comment_avatar,11 comment_avatar,
12 deactivateSendButtons,12 deactivateSendButtons,
13 default_avatar,13 default_avatar,
14 deleteSwipe,
14 eventSource,15 eventSource,
15 event_types,16 event_types,
16 extension_prompt_roles,17 extension_prompt_roles,
@@ -2309,37 +2310,10 @@ async function addSwipeCallback(args, value) {
2309}2310}
23102311
2311async function deleteSwipeCallback(_, arg) {2312async 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.
23132314 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
2344 return String(newSwipeId);2318 return String(newSwipeId);
2345}2319}