Utility func to sync all mes data to swipe

This commit is contained in:
Wolfsblvt 2025-02-25 17:00:41 +01:00
parent f0c7ea062b
commit 4ab54016ad
2 changed files with 47 additions and 12 deletions

View File

@ -6086,17 +6086,52 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
return { type, getMessage };
}
export function syncCurrentSwipeInfoExtras() {
/**
* 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).
*
* If the swipe data is invalid in some way, this function will exit out without doing anything.
* @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.
* @returns {boolean} Whether the message was successfully synced
*/
export function syncMesToSwipe(messageId = null) {
if (!chat.length) {
return;
return false;
}
const currentMessage = chat[chat.length - 1];
if (currentMessage && Array.isArray(currentMessage.swipe_info) && typeof currentMessage.swipe_id === 'number') {
const swipeInfo = currentMessage.swipe_info[currentMessage.swipe_id];
if (swipeInfo && typeof swipeInfo === 'object') {
swipeInfo.extra = structuredClone(currentMessage.extra);
}
const targetMessageId = messageId ?? chat.length - 1;
if (chat.length > targetMessageId) {
console.warn(`[syncMesToSwipe] Invalid message ID: ${messageId}`);
return false;
}
const targetMessage = chat[targetMessageId];
// No swipe data there yet, exit out
if (typeof targetMessage.swipe_id !== 'number') {
return false;
}
// If swipes structure is invalid, exit out (for now?)
if (!Array.isArray(targetMessage.swipe_info) || !Array.isArray(targetMessage.swipes)) {
return false;
}
// If the swipe is not present yet, exit out (will likely be copied later)
if (!targetMessage.swipes[targetMessage.swipe_id] || !targetMessage.swipe_info[targetMessage.swipe_id]) {
return false;
}
const targetSwipeInfo = targetMessage.swipe_info[targetMessage.swipe_id];
if (typeof targetSwipeInfo !== 'object') {
return false;
}
targetMessage.swipes[targetMessage.swipe_id] = targetMessage.mes;
targetSwipeInfo.send_date = targetMessage.send_date;
targetSwipeInfo.gen_started = targetMessage.gen_started;
targetSwipeInfo.gen_finished = targetMessage.gen_finished;
targetSwipeInfo.extra = structuredClone(targetMessage.extra);
return true;
}
function saveImageToMessage(img, mes) {
@ -8568,7 +8603,7 @@ function swipe_left() { // when we swipe left..but no generation.
}
// Make sure ad-hoc changes to extras are saved before swiping away
syncCurrentSwipeInfoExtras();
syncMesToSwipe();
const swipe_duration = 120;
const swipe_range = '700px';
@ -8706,7 +8741,7 @@ const swipe_right = () => {
}
// Make sure ad-hoc changes to extras are saved before swiping away
syncCurrentSwipeInfoExtras();
syncMesToSwipe();
const swipe_duration = 200;
const swipe_range = 700;

View File

@ -42,7 +42,7 @@ import {
showMoreMessages,
stopGeneration,
substituteParams,
syncCurrentSwipeInfoExtras,
syncMesToSwipe,
system_avatar,
system_message_types,
this_chid,
@ -2921,7 +2921,7 @@ async function addSwipeCallback(args, value) {
if (isTrueBoolean(args.switch)) {
// Make sure ad-hoc changes to extras are saved before swiping away
syncCurrentSwipeInfoExtras();
syncMesToSwipe();
lastMessage.swipe_id = newSwipeId;
lastMessage.mes = lastMessage.swipes[newSwipeId];
lastMessage.extra = structuredClone(lastMessage.swipe_info?.[newSwipeId]?.extra ?? lastMessage.extra ?? {});