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) {
62766276 }
62776277
62786278 const targetMessage = chat[targetMessageId];
6279-
62806279 if (!targetMessage) {
62816280 return false;
62826281 }
@@ -6310,6 +6309,68 @@ export function syncMesToSwipe(messageId = null) {
63106309}
63116310
63126311/**
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+ */
6320+export 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+/**
63136374 * Saves the image to the message object.
63146375 * @param {ParsedImage} img Image object
63156376 * @param {object} mes Chat message object
@@ -8317,10 +8378,9 @@ export async function deleteSwipe(swipeId = null) {
83178378 lastMessage.swipe_info.splice(swipeId, 1);
83188379 }
83198380
83208381 // Select the next swipswipe, or the one before if it was the last one
83218382 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
83258385 await saveChatConditional();
83268386 await reloadCurrentChat();