Refactor deleteSwipe from callback into func

This commit is contained in:
Wolfsblvt 2024-09-01 23:12:33 +02:00
parent 7b576c8067
commit d663be53ac
2 changed files with 59 additions and 44 deletions

View File

@ -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<number>|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.

View File

@ -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);
}