From 91266c831c3ae9fcfcdc0aa774045b4bf7c8c912 Mon Sep 17 00:00:00 2001 From: smirgol <14124899+smirgol@users.noreply.github.com> Date: Wed, 8 Nov 2023 16:40:47 +0100 Subject: [PATCH 1/2] fix tts re-trigger for complete/autocomplete/delete fix re-trigger tts from start if text was added to chat by auto-continue fix re-trigger tts from start if text was added by continue button fix re-trigger of tts when message(s) got deleted --- public/scripts/extensions/tts/index.js | 42 ++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/public/scripts/extensions/tts/index.js b/public/scripts/extensions/tts/index.js index 30386c7c6..89dbf8496 100644 --- a/public/scripts/extensions/tts/index.js +++ b/public/scripts/extensions/tts/index.js @@ -76,6 +76,8 @@ let ttsProviders = { let ttsProvider let ttsProviderName +let ttsLastMessage = null; + async function onNarrateOneMessage() { audioElement.src = '/sounds/silence.mp3'; const context = getContext(); @@ -132,11 +134,27 @@ async function moduleWorker() { let diff = lastMessageNumber - currentMessageNumber let hashNew = getStringHash((chat.length && chat[chat.length - 1].mes) ?? '') + // if messages got deleted, diff will be < 0 + if (diff < 0) { + // necessary actions will be taken by the onChatDeleted() handler + return + } + if (diff == 0 && hashNew === lastMessageHash) { return } - const message = chat[chat.length - 1] + // clone message object, as things go haywire if message object is altered below (it's passed by reference) + const message = JSON.parse(JSON.stringify(chat[chat.length - 1])) + + // if last message within current message, message got extended. only send diff to TTS. + if (ttsLastMessage !== null && message.mes.indexOf(ttsLastMessage) !== -1) { + let tmp = message.mes + message.mes = message.mes.replace(ttsLastMessage, '') + ttsLastMessage = tmp + } else { + ttsLastMessage = message.mes + } // We're currently swiping or streaming. Don't generate voice if ( @@ -630,6 +648,25 @@ async function onChatChanged() { await initVoiceMap() } +async function onChatDeleted() { + const context = getContext() + + // update internal references to new last message + lastChatId = context.chatId + currentMessageNumber = context.chat.length ? context.chat.length : 0 + + // compare against lastMessageHash. If it's the same, we did not delete the last chat item, so no need to reset tts queue + let messageHash = getStringHash((context.chat.length && context.chat[context.chat.length - 1].mes) ?? '') + if (messageHash === lastMessageHash) { + return + } + lastMessageHash = messageHash + ttsLastMessage = (context.chat.length && context.chat[context.chat.length - 1].mes) ?? ''; + + // stop any tts playback since message might not exist anymore + await resetTtsPlayback() +} + function getCharacters(){ const context = getContext() let characters = [] @@ -771,7 +808,7 @@ export async function initVoiceMap(){ // Clear existing voiceMap state $('#tts_voicemap_block').empty() voiceMapEntries = [] - + // Get characters in current chat const characters = getCharacters() @@ -898,4 +935,5 @@ $(document).ready(function () { eventSource.on(event_types.MESSAGE_SWIPED, resetTtsPlayback); eventSource.on(event_types.CHAT_CHANGED, onChatChanged) eventSource.on(event_types.GROUP_UPDATED, onChatChanged) + eventSource.on(event_types.MESSAGE_DELETED, onChatDeleted); }) From 4f0935c4948cbb35618732782545d244382424c2 Mon Sep 17 00:00:00 2001 From: smirgol <14124899+smirgol@users.noreply.github.com> Date: Wed, 8 Nov 2023 19:08:42 +0100 Subject: [PATCH 2/2] better deep clone of message object. re-initalize ttsLastMessage after changing chats --- public/scripts/extensions/tts/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/public/scripts/extensions/tts/index.js b/public/scripts/extensions/tts/index.js index 89dbf8496..674c7a54f 100644 --- a/public/scripts/extensions/tts/index.js +++ b/public/scripts/extensions/tts/index.js @@ -145,7 +145,7 @@ async function moduleWorker() { } // clone message object, as things go haywire if message object is altered below (it's passed by reference) - const message = JSON.parse(JSON.stringify(chat[chat.length - 1])) + const message = structuredClone(chat[chat.length - 1]) // if last message within current message, message got extended. only send diff to TTS. if (ttsLastMessage !== null && message.mes.indexOf(ttsLastMessage) !== -1) { @@ -646,6 +646,7 @@ export function saveTtsProviderSettings() { async function onChatChanged() { await resetTtsPlayback() await initVoiceMap() + ttsLastMessage = null } async function onChatDeleted() {