From df4ca46c02e2254c1463cb33989290fcfc4f74a0 Mon Sep 17 00:00:00 2001 From: Tony Ribeiro Date: Mon, 9 Oct 2023 01:26:57 +0200 Subject: [PATCH] Save chat id between received and rendered event for safety (blip extension allow multiple message received before one is rendered). Added export in translate extension for usage in blip extension. --- public/script.js | 48 ++++++++++++-------- public/scripts/extensions/translate/index.js | 4 +- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/public/script.js b/public/script.js index c24ba3be2..ecd7f4b39 100644 --- a/public/script.js +++ b/public/script.js @@ -251,6 +251,8 @@ export { extension_prompt_types, mesForShowdownParse, printCharacters, + isOdd, + countOccurrences } // Allow target="_blank" in links @@ -3497,9 +3499,10 @@ export async function sendMessageAsUser(textareaText, messageBias) { } statMesProcess(chat[chat.length - 1], 'user', characters, this_chid, ''); // Wait for all handlers to finish before continuing with the prompt - await eventSource.emit(event_types.MESSAGE_SENT, (chat.length - 1)); - addOneMessage(chat[chat.length - 1]); - await eventSource.emit(event_types.USER_MESSAGE_RENDERED, (chat.length - 1)); + const chat_id = (chat.length - 1); + await eventSource.emit(event_types.MESSAGE_SENT, chat_id); + addOneMessage(chat[chat_id]); + await eventSource.emit(event_types.USER_MESSAGE_RENDERED, chat_id); console.debug('message sent as user'); } @@ -4005,9 +4008,10 @@ async function saveReply(type, getMessage, _, title) { if (power_user.message_token_count_enabled) { chat[chat.length - 1]['extra']['token_count'] = getTokenCount(chat[chat.length - 1]['mes'], 0); } - await eventSource.emit(event_types.MESSAGE_RECEIVED, (chat.length - 1)); - addOneMessage(chat[chat.length - 1], { type: 'swipe' }); - await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, (chat.length - 1)); + const chat_id = (chat.length - 1); + await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id); + addOneMessage(chat[chat_id], { type: 'swipe' }); + await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id); } else { chat[chat.length - 1]['mes'] = getMessage; } @@ -4024,9 +4028,10 @@ async function saveReply(type, getMessage, _, title) { if (power_user.message_token_count_enabled) { chat[chat.length - 1]['extra']['token_count'] = getTokenCount(chat[chat.length - 1]['mes'], 0); } - await eventSource.emit(event_types.MESSAGE_RECEIVED, (chat.length - 1)); - addOneMessage(chat[chat.length - 1], { type: 'swipe' }); - await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, (chat.length - 1)); + const chat_id = (chat.length - 1); + await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id); + addOneMessage(chat[chat_id], { type: 'swipe' }); + await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id); } else if (type === 'appendFinal') { oldMessage = chat[chat.length - 1]['mes']; console.debug("Trying to appendFinal.") @@ -4040,9 +4045,10 @@ async function saveReply(type, getMessage, _, title) { if (power_user.message_token_count_enabled) { chat[chat.length - 1]['extra']['token_count'] = getTokenCount(chat[chat.length - 1]['mes'], 0); } - await eventSource.emit(event_types.MESSAGE_RECEIVED, (chat.length - 1)); - addOneMessage(chat[chat.length - 1], { type: 'swipe' }); - await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, (chat.length - 1)); + const chat_id = (chat.length - 1); + await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id); + addOneMessage(chat[chat_id], { type: 'swipe' }); + await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id); } else { console.debug('entering chat update routine for non-swipe post'); @@ -4077,9 +4083,10 @@ async function saveReply(type, getMessage, _, title) { } saveImageToMessage(img, chat[chat.length - 1]); - await eventSource.emit(event_types.MESSAGE_RECEIVED, (chat.length - 1)); - addOneMessage(chat[chat.length - 1]); - await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, (chat.length - 1)); + const chat_id = (chat.length - 1); + await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id); + addOneMessage(chat[chat_id]); + await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id); } const item = chat[chat.length - 1]; @@ -4545,8 +4552,10 @@ async function getChatResult() { await eventSource.emit(event_types.CHAT_CHANGED, (getCurrentChatId())); if (chat.length === 1) { - await eventSource.emit(event_types.MESSAGE_RECEIVED, (chat.length - 1)); - await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, (chat.length - 1)); + const chat_id = (chat.length - 1); + await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id); + addOneMessage(chat[chat_id]); + await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id); } } @@ -6409,10 +6418,11 @@ async function createOrEditCharacter(e) { const firstMessage = getFirstMessage(); chat[0] = firstMessage; - await eventSource.emit(event_types.MESSAGE_RECEIVED, (chat.length - 1)); + const chat_id = (chat.length - 1); + await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id); clearChat(); await printMessages(); - await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, (chat.length - 1)); + await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, chat_id); await saveChatConditional(); } }, diff --git a/public/scripts/extensions/translate/index.js b/public/scripts/extensions/translate/index.js index e01e4d117..dad4dcd40 100644 --- a/public/scripts/extensions/translate/index.js +++ b/public/scripts/extensions/translate/index.js @@ -1,3 +1,5 @@ +export {translate}; + import { callPopup, eventSource, @@ -11,7 +13,7 @@ import { import { extension_settings, getContext } from "../../extensions.js"; import { secret_state, writeSecret } from "../../secrets.js"; -const autoModeOptions = { +export const autoModeOptions = { NONE: 'none', RESPONSES: 'responses', INPUT: 'inputs',