Utility func to sync all mes data to swipe

4ab54016ad73af005e71ac019931bff3033d7ccd

Wolfsblvt <wolfsblvt@gmail.com>

2 files changed, +47 -12Ignore whitespace
public/script.js+45 -10
@@ -6086,17 +6086,52 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
6086 return { type, getMessage };6086 return { type, getMessage };
6087}6087}
60886088
6089export function syncCurrentSwipeInfoExtras() {6089/**
6090 * Syncs the current message and all its data into the swipe data at the given message ID (or the last message if no ID is given).
6091 *
6092 * If the swipe data is invalid in some way, this function will exit out without doing anything.
6093 * @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.
6094 * @returns {boolean} Whether the message was successfully synced
6095 */
6096export function syncMesToSwipe(messageId = null) {
6090 if (!chat.length) {6097 if (!chat.length) {
6091 return;6098 return false;
6092 }6099 }
6093 const currentMessage = chat[chat.length - 1];6100
6094 if (currentMessage && Array.isArray(currentMessage.swipe_info) && typeof currentMessage.swipe_id === 'number') {6101 const targetMessageId = messageId ?? chat.length - 1;
6095 const swipeInfo = currentMessage.swipe_info[currentMessage.swipe_id];6102 if (chat.length > targetMessageId) {
6096 if (swipeInfo && typeof swipeInfo === 'object') {6103 console.warn(`[syncMesToSwipe] Invalid message ID: ${messageId}`);
6097 swipeInfo.extra = structuredClone(currentMessage.extra);6104 return false;
6098 }6105 }
6106
6107 const targetMessage = chat[targetMessageId];
6108
6109 // No swipe data there yet, exit out
6110 if (typeof targetMessage.swipe_id !== 'number') {
6111 return false;
6099 }6112 }
6113 // If swipes structure is invalid, exit out (for now?)
6114 if (!Array.isArray(targetMessage.swipe_info) || !Array.isArray(targetMessage.swipes)) {
6115 return false;
6116 }
6117 // If the swipe is not present yet, exit out (will likely be copied later)
6118 if (!targetMessage.swipes[targetMessage.swipe_id] || !targetMessage.swipe_info[targetMessage.swipe_id]) {
6119 return false;
6120 }
6121
6122 const targetSwipeInfo = targetMessage.swipe_info[targetMessage.swipe_id];
6123 if (typeof targetSwipeInfo !== 'object') {
6124 return false;
6125 }
6126
6127 targetMessage.swipes[targetMessage.swipe_id] = targetMessage.mes;
6128
6129 targetSwipeInfo.send_date = targetMessage.send_date;
6130 targetSwipeInfo.gen_started = targetMessage.gen_started;
6131 targetSwipeInfo.gen_finished = targetMessage.gen_finished;
6132 targetSwipeInfo.extra = structuredClone(targetMessage.extra);
6133
6134 return true;
6100}6135}
61016136
6102function saveImageToMessage(img, mes) {6137function saveImageToMessage(img, mes) {
@@ -8568,7 +8603,7 @@ function swipe_left() { // when we swipe left..but no generation.
8568 }8603 }
85698604
8570 // Make sure ad-hoc changes to extras are saved before swiping away8605 // Make sure ad-hoc changes to extras are saved before swiping away
8571 syncCurrentSwipeInfoExtras();8606 syncMesToSwipe();
85728607
8573 const swipe_duration = 120;8608 const swipe_duration = 120;
8574 const swipe_range = '700px';8609 const swipe_range = '700px';
@@ -8706,7 +8741,7 @@ const swipe_right = () => {
8706 }8741 }
87078742
8708 // Make sure ad-hoc changes to extras are saved before swiping away8743 // Make sure ad-hoc changes to extras are saved before swiping away
8709 syncCurrentSwipeInfoExtras();8744 syncMesToSwipe();
87108745
8711 const swipe_duration = 200;8746 const swipe_duration = 200;
8712 const swipe_range = 700;8747 const swipe_range = 700;
public/scripts/slash-commands.js+2 -2
@@ -42,7 +42,7 @@ import {
42 showMoreMessages,42 showMoreMessages,
43 stopGeneration,43 stopGeneration,
44 substituteParams,44 substituteParams,
45 syncCurrentSwipeInfoExtras,45 syncMesToSwipe,
46 system_avatar,46 system_avatar,
47 system_message_types,47 system_message_types,
48 this_chid,48 this_chid,
@@ -2921,7 +2921,7 @@ async function addSwipeCallback(args, value) {
29212921
2922 if (isTrueBoolean(args.switch)) {2922 if (isTrueBoolean(args.switch)) {
2923 // Make sure ad-hoc changes to extras are saved before swiping away2923 // Make sure ad-hoc changes to extras are saved before swiping away
2924 syncCurrentSwipeInfoExtras();2924 syncMesToSwipe();
2925 lastMessage.swipe_id = newSwipeId;2925 lastMessage.swipe_id = newSwipeId;
2926 lastMessage.mes = lastMessage.swipes[newSwipeId];2926 lastMessage.mes = lastMessage.swipes[newSwipeId];
2927 lastMessage.extra = structuredClone(lastMessage.swipe_info?.[newSwipeId]?.extra ?? lastMessage.extra ?? {});2927 lastMessage.extra = structuredClone(lastMessage.swipe_info?.[newSwipeId]?.extra ?? lastMessage.extra ?? {});