diff --git a/public/script.js b/public/script.js index f3414a364..8fd62410b 100644 --- a/public/script.js +++ b/public/script.js @@ -38,6 +38,7 @@ import { resetSelectedGroup, select_group_chats, regenerateGroup, + group_generation_id, } from "./scripts/group-chats.js"; import { @@ -2232,6 +2233,7 @@ function saveReply(type, getMessage, this_mes_is_name) { } else { console.log('entering chat update routine for non-swipe post'); chat[chat.length] = {}; + chat[chat.length - 1]['extra'] = {}; chat[chat.length - 1]['name'] = name2; chat[chat.length - 1]['is_user'] = false; chat[chat.length - 1]['is_name'] = this_mes_is_name; @@ -2247,6 +2249,7 @@ function saveReply(type, getMessage, this_mes_is_name) { } chat[chat.length - 1]['is_name'] = true; chat[chat.length - 1]['force_avatar'] = avatarImg; + chat[chat.length - 1]['extra']['gen_id'] = group_generation_id; } saveImageToMessage(img, chat[chat.length - 1]); diff --git a/public/scripts/group-chats.js b/public/scripts/group-chats.js index 1b0b9e276..12752d1e5 100644 --- a/public/scripts/group-chats.js +++ b/public/scripts/group-chats.js @@ -48,6 +48,7 @@ export { selected_group, is_group_automode_enabled, is_group_generating, + group_generation_id, groups, saveGroupChat, generateGroupWrapper, @@ -64,6 +65,7 @@ let is_group_generating = false; // Group generation flag let is_group_automode_enabled = false; let groups = []; let selected_group = null; +let group_generation_id = null; const group_activation_strategy = { NATURAL: 0, @@ -88,10 +90,18 @@ async function _save(group) { // Group chats async function regenerateGroup() { + let generationId = getLastMessageGenerationId(); + while (chat.length > 0) { const lastMes = chat[chat.length - 1]; + const this_generationId = lastMes.extra?.gen_id; - if (lastMes.is_user || lastMes.is_system) { + // for new generations after the update + if ((generationId && this_generationId) && generationId !== this_generationId) { + break; + } + // legacy for generations before the update + else if (lastMes.is_user || lastMes.is_system) { break; } @@ -130,19 +140,7 @@ async function getGroupChat(id) { continue; } - const mes = {}; - mes["is_user"] = false; - mes["is_system"] = false; - mes["name"] = character.name; - mes["is_name"] = true; - mes["send_date"] = humanizedDateTime(); - mes["mes"] = character.first_mes - ? substituteParams(character.first_mes.trim(), name1, character.name) - : default_ch_mes; - mes["force_avatar"] = - character.avatar != "none" - ? getThumbnailUrl('avatar', character.avatar) - : default_avatar; + const mes = getFirstCharacterMessage(character); chat.push(mes); addOneMessage(mes); } @@ -154,16 +152,33 @@ async function getGroupChat(id) { updateChatMetadata(metadata, true); } - await saveGroupChat(id); + await saveGroupChat(id, true); } } +function getFirstCharacterMessage(character) { + const mes = {}; + mes["is_user"] = false; + mes["is_system"] = false; + mes["name"] = character.name; + mes["is_name"] = true; + mes["send_date"] = humanizedDateTime(); + mes["mes"] = character.first_mes + ? substituteParams(character.first_mes.trim(), name1, character.name) + : default_ch_mes; + mes["force_avatar"] = + character.avatar != "none" + ? getThumbnailUrl('avatar', character.avatar) + : default_avatar; + return mes; +} + function resetSelectedGroup() { selected_group = null; is_group_generating = false; } -async function saveGroupChat(id) { +async function saveGroupChat(id, shouldSaveGroup) { const response = await fetch("/savegroupchat", { method: "POST", headers: { @@ -173,7 +188,7 @@ async function saveGroupChat(id) { body: JSON.stringify({ id: id, chat: [...chat] }), }); - if (response.ok) { + if (shouldSaveGroup && response.ok) { await editGroup(id); } } @@ -303,6 +318,8 @@ async function generateGroupWrapper(by_auto_mode, type = null) { $("#chat").append(typingIndicator); } + // id of this specific batch for regeneration purposes + group_generation_id = Date.now(); const lastMessage = chat[chat.length - 1]; let messagesBefore = chat.length; let lastMessageText = lastMessage.mes; @@ -422,6 +439,17 @@ async function generateGroupWrapper(by_auto_mode, type = null) { } } +function getLastMessageGenerationId() { + let generationId = null; + if (chat.length > 0) { + const lastMes = chat[chat.length - 1]; + if (!lastMes.is_user && !lastMes.is_system && lastMes.extra) { + generationId = lastMes.extra.gen_id; + } + } + return generationId; +} + function activateImpersonate(members) { const randomIndex = Math.floor(Math.random() * members.length); const activatedNames = [members[randomIndex]];