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) {
2496 eventSource.emit(event_types.IMAGE_SWIPED, { message: mes, element: messageElement, direction: 'right' });2496 eventSource.emit(event_types.IMAGE_SWIPED, { message: mes, element: messageElement, direction: 'right' });
2497 });2497 });
2498 }2498 }
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');
2499 }2504 }
25002505
2501 // Add video to message2506 // Add video to message
@@ -2521,6 +2526,8 @@ export function appendMediaToMessage(mes, messageElement, adjustScroll = true) {
2521 });2526 });
25222527
2523 video.attr('src', mes.extra?.video);2528 video.attr('src', mes.extra?.video);
2529 } else {
2530 messageElement.find('.mes_video').remove();
2524 }2531 }
25252532
2526 // Add file to message2533 // Add file to message
@@ -3561,7 +3568,7 @@ class StreamingProcessor {
3561 'send_date': chat[messageId]['send_date'],3568 'send_date': chat[messageId]['send_date'],
3562 'gen_started': chat[messageId]['gen_started'],3569 'gen_started': chat[messageId]['gen_started'],
3563 'gen_finished': chat[messageId]['gen_finished'],3570 'gen_finished': chat[messageId]['gen_finished'],
3564 'extra': JSON.parse(JSON.stringify(chat[messageId]['extra'])),3571 'extra': structuredClone(chat[messageId]['extra']),
3565 };3572 };
3566 }3573 }
35673574
@@ -3666,7 +3673,7 @@ class StreamingProcessor {
3666 'send_date': chat[messageId]['send_date'],3673 'send_date': chat[messageId]['send_date'],
3667 'gen_started': chat[messageId]['gen_started'],3674 'gen_started': chat[messageId]['gen_started'],
3668 'gen_finished': chat[messageId]['gen_finished'],3675 'gen_finished': chat[messageId]['gen_finished'],
3669 'extra': JSON.parse(JSON.stringify(chat[messageId]['extra'])),3676 'extra': structuredClone(chat[messageId]['extra']),
3670 };3677 };
3671 }3678 }
3672 }3679 }
@@ -6551,7 +6558,7 @@ export async function saveReply({ type, getMessage, fromStreaming = false, title
6551 send_date: item['send_date'],6558 send_date: item['send_date'],
6552 gen_started: item['gen_started'],6559 gen_started: item['gen_started'],
6553 gen_finished: item['gen_finished'],6560 gen_finished: item['gen_finished'],
6554 extra: JSON.parse(JSON.stringify(item['extra'])),6561 extra: structuredClone(item['extra']),
6555 };6562 };
6556 } else {6563 } else {
6557 item['swipe_id'] = 0;6564 item['swipe_id'] = 0;
@@ -6561,7 +6568,7 @@ export async function saveReply({ type, getMessage, fromStreaming = false, title
6561 send_date: chat[chat.length - 1]['send_date'],6568 send_date: chat[chat.length - 1]['send_date'],
6562 gen_started: chat[chat.length - 1]['gen_started'],6569 gen_started: chat[chat.length - 1]['gen_started'],
6563 gen_finished: chat[chat.length - 1]['gen_finished'],6570 gen_finished: chat[chat.length - 1]['gen_finished'],
6564 extra: JSON.parse(JSON.stringify(chat[chat.length - 1]['extra'])),6571 extra: structuredClone(chat[chat.length - 1]['extra']),
6565 };6572 };
6566 }6573 }
65676574
@@ -8649,7 +8656,6 @@ export function showSwipeButtons() {
8649 !swipes ||8656 !swipes ||
8650 Number($('.mes:last').attr('mesid')) < 0 ||8657 Number($('.mes:last').attr('mesid')) < 0 ||
8651 chat[chat.length - 1].is_user ||8658 chat[chat.length - 1].is_user ||
8652 chat[chat.length - 1].extra?.image ||
8653 (selected_group && is_group_generating)8659 (selected_group && is_group_generating)
8654 ) { return; }8660 ) { return; }
86558661
@@ -8665,6 +8671,13 @@ export function showSwipeButtons() {
8665 chat[chat.length - 1]['swipe_id'] = 0; // set it to id 08671 chat[chat.length - 1]['swipe_id'] = 0; // set it to id 0
8666 chat[chat.length - 1]['swipes'] = []; // empty the array8672 chat[chat.length - 1]['swipes'] = []; // empty the array
8667 chat[chat.length - 1]['swipes'][0] = chat[chat.length - 1]['mes']; //assign swipe array with last message from chat8673 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 };
8668 }8681 }
86698682
8670 const currentMessage = $('#chat').children().filter(`[mesid="${chat.length - 1}"]`);8683 const currentMessage = $('#chat').children().filter(`[mesid="${chat.length - 1}"]`);
@@ -9339,7 +9352,7 @@ export function swipe_left(_event, { source, repeated } = {}) {
9339 const this_mes_block_height = this_mes_block[0].scrollHeight;9352 const this_mes_block_height = this_mes_block[0].scrollHeight;
9340 chat[chat.length - 1]['mes'] = chat[chat.length - 1]['swipes'][chat[chat.length - 1]['swipe_id']];9353 chat[chat.length - 1]['mes'] = chat[chat.length - 1]['swipes'][chat[chat.length - 1]['swipe_id']];
9341 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 generation9354 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
9342 chat[chat.length - 1]['extra'] = JSON.parse(JSON.stringify(chat[chat.length - 1].swipe_info[chat[chat.length - 1]['swipe_id']]?.extra || chat[chat.length - 1].extra));9355 chat[chat.length - 1]['extra'] = structuredClone(chat[chat.length - 1].swipe_info[chat[chat.length - 1]['swipe_id']]?.extra || chat[chat.length - 1].extra);
93439356
9344 if (chat[chat.length - 1].extra) {9357 if (chat[chat.length - 1].extra) {
9345 // if message has memory attached - remove it to allow regen9358 // if message has memory attached - remove it to allow regen
@@ -9401,6 +9414,7 @@ export function swipe_left(_event, { source, repeated } = {}) {
9401 easing: animation_easing,9414 easing: animation_easing,
9402 queue: false,9415 queue: false,
9403 complete: async function () {9416 complete: async function () {
9417 appendMediaToMessage(chat[chat.length - 1], $(this).parent().children('.mes_block'));
9404 await eventSource.emit(event_types.MESSAGE_SWIPED, (chat.length - 1));9418 await eventSource.emit(event_types.MESSAGE_SWIPED, (chat.length - 1));
9405 saveChatDebounced();9419 saveChatDebounced();
9406 },9420 },
@@ -9476,7 +9490,7 @@ export function swipe_right(_event = null, { source, repeated } = {}) {
9476 'send_date': chat[chat.length - 1]['send_date'],9490 'send_date': chat[chat.length - 1]['send_date'],
9477 'gen_started': chat[chat.length - 1]['gen_started'],9491 'gen_started': chat[chat.length - 1]['gen_started'],
9478 'gen_finished': chat[chat.length - 1]['gen_finished'],9492 'gen_finished': chat[chat.length - 1]['gen_finished'],
9479 'extra': JSON.parse(JSON.stringify(chat[chat.length - 1]['extra'])),9493 'extra': structuredClone(chat[chat.length - 1]['extra']),
9480 };9494 };
9481 //assign swipe info array with last message from chat9495 //assign swipe info array with last message from chat
9482 }9496 }
@@ -9500,6 +9514,11 @@ export function swipe_right(_event = null, { source, repeated } = {}) {
9500 if (chat[chat.length - 1].extra.display_text) {9514 if (chat[chat.length - 1].extra.display_text) {
9501 delete chat[chat.length - 1].extra.display_text;9515 delete chat[chat.length - 1].extra.display_text;
9502 }9516 }
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;
9503 }9522 }
9504 if (!Array.isArray(chat[chat.length - 1]['swipe_info'])) {9523 if (!Array.isArray(chat[chat.length - 1]['swipe_info'])) {
9505 chat[chat.length - 1]['swipe_info'] = [];9524 chat[chat.length - 1]['swipe_info'] = [];
@@ -9512,7 +9531,7 @@ export function swipe_right(_event = null, { source, repeated } = {}) {
9512 } 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 swipes9531 } 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
9513 chat[chat.length - 1]['mes'] = chat[chat.length - 1]['swipes'][chat[chat.length - 1]['swipe_id']]; //load the last mes box with the latest generation9532 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
9514 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 date9533 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
9515 chat[chat.length - 1]['extra'] = JSON.parse(JSON.stringify(chat[chat.length - 1].swipe_info[chat[chat.length - 1]['swipe_id']]?.extra || chat[chat.length - 1].extra || []));9534 chat[chat.length - 1]['extra'] = structuredClone(chat[chat.length - 1].swipe_info[chat[chat.length - 1]['swipe_id']]?.extra || chat[chat.length - 1].extra || []);
9516 run_swipe_right = true; //then prepare to do normal right swipe to show next message9535 run_swipe_right = true; //then prepare to do normal right swipe to show next message
9517 }9536 }
95189537
@@ -9595,6 +9614,7 @@ export function swipe_right(_event = null, { source, repeated } = {}) {
9595 easing: animation_easing,9614 easing: animation_easing,
9596 queue: false,9615 queue: false,
9597 complete: async function () {9616 complete: async function () {
9617 appendMediaToMessage(chat[chat.length - 1], swipeMessage);
9598 await eventSource.emit(event_types.MESSAGE_SWIPED, (chat.length - 1));9618 await eventSource.emit(event_types.MESSAGE_SWIPED, (chat.length - 1));
9599 if (run_generate && !is_send_press && parseInt(chat[chat.length - 1]['swipe_id']) === chat[chat.length - 1]['swipes'].length) {9619 if (run_generate && !is_send_press && parseInt(chat[chat.length - 1]['swipe_id']) === chat[chat.length - 1]['swipes'].length) {
9600 console.debug('caught here 2');9620 console.debug('caught here 2');