Allow message swipe gens with image attachments (#4184) * Allow message swipe gens with image attachments * Replace JSON.parse with structuredClone

5ea2c48bf88c11922f371d002d2a6d8dc4debf71

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

Signed
1 files changed, +28 -8Showing whitespace changes
public/script.js+28 -8
@@ -2496,6 +2496,11 @@ export function appendMediaToMessage(mes, messageElement, adjustScroll = true) {
24962496 eventSource.emit(event_types.IMAGE_SWIPED, { message: mes, element: messageElement, direction: 'right' });
24972497 });
24982498 }
2499+ } else {
2500+ const container = messageElement.find('.mes_img_container');
2501+ container.removeClass('img_extra img_swipes');
2502+ const text = messageElement.find('.mes_text');
2503+ text.removeClass('displayNone');
24992504 }
25002505
25012506 // Add video to message
@@ -2521,6 +2526,8 @@ export function appendMediaToMessage(mes, messageElement, adjustScroll = true) {
25212526 });
25222527
25232528 video.attr('src', mes.extra?.video);
2529+ } else {
2530+ messageElement.find('.mes_video').remove();
25242531 }
25252532
25262533 // Add file to message
@@ -3561,7 +3568,7 @@ class StreamingProcessor {
35613568 'send_date': chat[messageId]['send_date'],
35623569 'gen_started': chat[messageId]['gen_started'],
35633570 'gen_finished': chat[messageId]['gen_finished'],
35643571 'extra': JSON.parse(JSON.stringifystructuredClone(chat[messageId]['extra'])),
35653572 };
35663573 }
35673574
@@ -3666,7 +3673,7 @@ class StreamingProcessor {
36663673 'send_date': chat[messageId]['send_date'],
36673674 'gen_started': chat[messageId]['gen_started'],
36683675 'gen_finished': chat[messageId]['gen_finished'],
36693676 'extra': JSON.parse(JSON.stringifystructuredClone(chat[messageId]['extra'])),
36703677 };
36713678 }
36723679 }
@@ -6551,7 +6558,7 @@ export async function saveReply({ type, getMessage, fromStreaming = false, title
65516558 send_date: item['send_date'],
65526559 gen_started: item['gen_started'],
65536560 gen_finished: item['gen_finished'],
65546561 extra: JSON.parse(JSON.stringifystructuredClone(item['extra'])),
65556562 };
65566563 } else {
65576564 item['swipe_id'] = 0;
@@ -6561,7 +6568,7 @@ export async function saveReply({ type, getMessage, fromStreaming = false, title
65616568 send_date: chat[chat.length - 1]['send_date'],
65626569 gen_started: chat[chat.length - 1]['gen_started'],
65636570 gen_finished: chat[chat.length - 1]['gen_finished'],
65646571 extra: JSON.parse(JSON.stringifystructuredClone(chat[chat.length - 1]['extra'])),
65656572 };
65666573 }
65676574
@@ -8649,7 +8656,6 @@ export function showSwipeButtons() {
86498656 !swipes ||
86508657 Number($('.mes:last').attr('mesid')) < 0 ||
86518658 chat[chat.length - 1].is_user ||
8652- chat[chat.length - 1].extra?.image ||
86538659 (selected_group && is_group_generating)
86548660 ) { return; }
86558661
@@ -8665,6 +8671,13 @@ export function showSwipeButtons() {
86658671 chat[chat.length - 1]['swipe_id'] = 0; // set it to id 0
86668672 chat[chat.length - 1]['swipes'] = []; // empty the array
86678673 chat[chat.length - 1]['swipes'][0] = chat[chat.length - 1]['mes']; //assign swipe array with last message from chat
8674+ chat[chat.length - 1]['swipe_info'] = [];
8675+ chat[chat.length - 1]['swipe_info'][0] = {
8676+ 'send_date': chat[chat.length - 1]['send_date'],
8677+ 'gen_started': chat[chat.length - 1]['gen_started'],
8678+ 'gen_finished': chat[chat.length - 1]['gen_finished'],
8679+ 'extra': structuredClone(chat[chat.length - 1]['extra']),
8680+ };
86688681 }
86698682
86708683 const currentMessage = $('#chat').children().filter(`[mesid="${chat.length - 1}"]`);
@@ -9339,7 +9352,7 @@ export function swipe_left(_event, { source, repeated } = {}) {
93399352 const this_mes_block_height = this_mes_block[0].scrollHeight;
93409353 chat[chat.length - 1]['mes'] = chat[chat.length - 1]['swipes'][chat[chat.length - 1]['swipe_id']];
93419354 chat[chat.length - 1]['send_date'] = chat[chat.length - 1].swipe_info[chat[chat.length - 1]['swipe_id']]?.send_date || chat[chat.length - 1].send_date; //load the last mes box with the latest generation
93429355 chat[chat.length - 1]['extra'] = JSON.parse(JSON.stringifystructuredClone(chat[chat.length - 1].swipe_info[chat[chat.length - 1]['swipe_id']]?.extra || chat[chat.length - 1].extra));
93439356
93449357 if (chat[chat.length - 1].extra) {
93459358 // if message has memory attached - remove it to allow regen
@@ -9401,6 +9414,7 @@ export function swipe_left(_event, { source, repeated } = {}) {
94019414 easing: animation_easing,
94029415 queue: false,
94039416 complete: async function () {
9417+ appendMediaToMessage(chat[chat.length - 1], $(this).parent().children('.mes_block'));
94049418 await eventSource.emit(event_types.MESSAGE_SWIPED, (chat.length - 1));
94059419 saveChatDebounced();
94069420 },
@@ -9476,7 +9490,7 @@ export function swipe_right(_event = null, { source, repeated } = {}) {
94769490 'send_date': chat[chat.length - 1]['send_date'],
94779491 'gen_started': chat[chat.length - 1]['gen_started'],
94789492 'gen_finished': chat[chat.length - 1]['gen_finished'],
94799493 'extra': JSON.parse(JSON.stringifystructuredClone(chat[chat.length - 1]['extra'])),
94809494 };
94819495 //assign swipe info array with last message from chat
94829496 }
@@ -9500,6 +9514,11 @@ export function swipe_right(_event = null, { source, repeated } = {}) {
95009514 if (chat[chat.length - 1].extra.display_text) {
95019515 delete chat[chat.length - 1].extra.display_text;
95029516 }
9517+
9518+ delete chat[chat.length - 1].extra.image;
9519+ delete chat[chat.length - 1].extra.image_swipes;
9520+ delete chat[chat.length - 1].extra.video;
9521+ delete chat[chat.length - 1].extra.inline_image;
95039522 }
95049523 if (!Array.isArray(chat[chat.length - 1]['swipe_info'])) {
95059524 chat[chat.length - 1]['swipe_info'] = [];
@@ -9512,7 +9531,7 @@ export function swipe_right(_event = null, { source, repeated } = {}) {
95129531 } else if (parseInt(chat[chat.length - 1]['swipe_id']) < chat[chat.length - 1]['swipes'].length) { //otherwise, if the id is less than the number of swipes
95139532 chat[chat.length - 1]['mes'] = chat[chat.length - 1]['swipes'][chat[chat.length - 1]['swipe_id']]; //load the last mes box with the latest generation
95149533 chat[chat.length - 1]['send_date'] = chat[chat.length - 1]?.swipe_info[chat[chat.length - 1]['swipe_id']]?.send_date || chat[chat.length - 1]['send_date']; //update send date
95159534 chat[chat.length - 1]['extra'] = JSON.parse(JSON.stringifystructuredClone(chat[chat.length - 1].swipe_info[chat[chat.length - 1]['swipe_id']]?.extra || chat[chat.length - 1].extra || []));
95169535 run_swipe_right = true; //then prepare to do normal right swipe to show next message
95179536 }
95189537
@@ -9595,6 +9614,7 @@ export function swipe_right(_event = null, { source, repeated } = {}) {
95959614 easing: animation_easing,
95969615 queue: false,
95979616 complete: async function () {
9617+ appendMediaToMessage(chat[chat.length - 1], swipeMessage);
95989618 await eventSource.emit(event_types.MESSAGE_SWIPED, (chat.length - 1));
95999619 if (run_generate && !is_send_press && parseInt(chat[chat.length - 1]['swipe_id']) === chat[chat.length - 1]['swipes'].length) {
96009620 console.debug('caught here 2');