Merge pull request #3708 from SillyTavern/fix-swipes-reasoning Fix deleting swipes overwriting reasoning

058bddfe817894a5d7e95e1bef2ac30ded8fccfe

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +64 -4Ignore whitespace
public/script.js+64 -4
@@ -6276,7 +6276,6 @@ export function syncMesToSwipe(messageId = null) {
6276 }6276 }
62776277
6278 const targetMessage = chat[targetMessageId];6278 const targetMessage = chat[targetMessageId];
6279
6280 if (!targetMessage) {6279 if (!targetMessage) {
6281 return false;6280 return false;
6282 }6281 }
@@ -6310,6 +6309,68 @@ export function syncMesToSwipe(messageId = null) {
6310}6309}
63116310
6312/**6311/**
6312 * Syncs swipe data back to the message data at the given message ID (or the last message if no ID is given).
6313 * If the swipe ID is not provided, the current swipe ID in the message object is used.
6314 *
6315 * If the swipe data is invalid in some way, this function will exit out without doing anything.
6316 * @param {number?} [messageId=null] - The ID of the message to sync with the swipe data. If no ID is given, the last message is used.
6317 * @param {number?} [swipeId=null] - The ID of the swipe to sync. If no ID is given, the current swipe ID in the message object is used.
6318 * @returns {boolean} Whether the swipe data was successfully synced to the message
6319 */
6320export function syncSwipeToMes(messageId = null, swipeId = null) {
6321 if (!chat.length) {
6322 return false;
6323 }
6324
6325 const targetMessageId = messageId ?? chat.length - 1;
6326 if (targetMessageId >= chat.length || targetMessageId < 0) {
6327 console.warn(`[syncSwipeToMes] Invalid message ID: ${messageId}`);
6328 return false;
6329 }
6330
6331 const targetMessage = chat[targetMessageId];
6332 if (!targetMessage) {
6333 return false;
6334 }
6335
6336 if (swipeId !== null) {
6337 if (isNaN(swipeId) || swipeId < 0) {
6338 console.warn(`[syncSwipeToMes] Invalid swipe ID: ${swipeId}`);
6339 return false;
6340 }
6341 targetMessage.swipe_id = swipeId;
6342 }
6343
6344 // No swipe data there yet, exit out
6345 if (typeof targetMessage.swipe_id !== 'number') {
6346 return false;
6347 }
6348 // If swipes structure is invalid, exit out
6349 if (!Array.isArray(targetMessage.swipe_info) || !Array.isArray(targetMessage.swipes)) {
6350 return false;
6351 }
6352
6353 const targetSwipeId = targetMessage.swipe_id;
6354 if (!targetMessage.swipes[targetSwipeId] || !targetMessage.swipe_info[targetSwipeId]) {
6355 console.warn(`[syncSwipeToMes] Invalid swipe ID: ${targetSwipeId}`);
6356 return false;
6357 }
6358
6359 const targetSwipeInfo = targetMessage.swipe_info[targetSwipeId];
6360 if (typeof targetSwipeInfo !== 'object') {
6361 return false;
6362 }
6363
6364 targetMessage.mes = targetMessage.swipes[targetSwipeId];
6365 targetMessage.send_date = targetSwipeInfo.send_date;
6366 targetMessage.gen_started = targetSwipeInfo.gen_started;
6367 targetMessage.gen_finished = targetSwipeInfo.gen_finished;
6368 targetMessage.extra = structuredClone(targetSwipeInfo.extra);
6369
6370 return true;
6371}
6372
6373/**
6313 * Saves the image to the message object.6374 * Saves the image to the message object.
6314 * @param {ParsedImage} img Image object6375 * @param {ParsedImage} img Image object
6315 * @param {object} mes Chat message object6376 * @param {object} mes Chat message object
@@ -8317,10 +8378,9 @@ export async function deleteSwipe(swipeId = null) {
8317 lastMessage.swipe_info.splice(swipeId, 1);8378 lastMessage.swipe_info.splice(swipeId, 1);
8318 }8379 }
83198380
8320 // Select the next swip, or the one before if it was the last one8381 // Select the next swipe, or the one before if it was the last one
8321 const newSwipeId = Math.min(swipeId, lastMessage.swipes.length - 1);8382 const newSwipeId = Math.min(swipeId, lastMessage.swipes.length - 1);
8322 lastMessage.swipe_id = newSwipeId;8383 syncSwipeToMes(null, newSwipeId);
8323 lastMessage.mes = lastMessage.swipes[newSwipeId];
83248384
8325 await saveChatConditional();8385 await saveChatConditional();
8326 await reloadCurrentChat();8386 await reloadCurrentChat();