mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Utility func to sync all mes data to swipe
This commit is contained in:
@ -6086,17 +6086,52 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
|
|||||||
return { type, getMessage };
|
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) {
|
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 targetMessageId = messageId ?? chat.length - 1;
|
||||||
const swipeInfo = currentMessage.swipe_info[currentMessage.swipe_id];
|
if (chat.length > targetMessageId) {
|
||||||
if (swipeInfo && typeof swipeInfo === 'object') {
|
console.warn(`[syncMesToSwipe] Invalid message ID: ${messageId}`);
|
||||||
swipeInfo.extra = structuredClone(currentMessage.extra);
|
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) {
|
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
|
// Make sure ad-hoc changes to extras are saved before swiping away
|
||||||
syncCurrentSwipeInfoExtras();
|
syncMesToSwipe();
|
||||||
|
|
||||||
const swipe_duration = 120;
|
const swipe_duration = 120;
|
||||||
const swipe_range = '700px';
|
const swipe_range = '700px';
|
||||||
@ -8706,7 +8741,7 @@ const swipe_right = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Make sure ad-hoc changes to extras are saved before swiping away
|
// Make sure ad-hoc changes to extras are saved before swiping away
|
||||||
syncCurrentSwipeInfoExtras();
|
syncMesToSwipe();
|
||||||
|
|
||||||
const swipe_duration = 200;
|
const swipe_duration = 200;
|
||||||
const swipe_range = 700;
|
const swipe_range = 700;
|
||||||
|
@ -42,7 +42,7 @@ import {
|
|||||||
showMoreMessages,
|
showMoreMessages,
|
||||||
stopGeneration,
|
stopGeneration,
|
||||||
substituteParams,
|
substituteParams,
|
||||||
syncCurrentSwipeInfoExtras,
|
syncMesToSwipe,
|
||||||
system_avatar,
|
system_avatar,
|
||||||
system_message_types,
|
system_message_types,
|
||||||
this_chid,
|
this_chid,
|
||||||
@ -2921,7 +2921,7 @@ async function addSwipeCallback(args, value) {
|
|||||||
|
|
||||||
if (isTrueBoolean(args.switch)) {
|
if (isTrueBoolean(args.switch)) {
|
||||||
// Make sure ad-hoc changes to extras are saved before swiping away
|
// Make sure ad-hoc changes to extras are saved before swiping away
|
||||||
syncCurrentSwipeInfoExtras();
|
syncMesToSwipe();
|
||||||
lastMessage.swipe_id = newSwipeId;
|
lastMessage.swipe_id = newSwipeId;
|
||||||
lastMessage.mes = lastMessage.swipes[newSwipeId];
|
lastMessage.mes = lastMessage.swipes[newSwipeId];
|
||||||
lastMessage.extra = structuredClone(lastMessage.swipe_info?.[newSwipeId]?.extra ?? lastMessage.extra ?? {});
|
lastMessage.extra = structuredClone(lastMessage.swipe_info?.[newSwipeId]?.extra ?? lastMessage.extra ?? {});
|
||||||
|
Reference in New Issue
Block a user