Fix TTS issues with streaming

This commit is contained in:
Cohee 2023-11-19 16:56:12 +02:00
parent b0b19edf31
commit 1dc1b926c4
1 changed files with 20 additions and 19 deletions

View File

@ -167,42 +167,43 @@ async function moduleWorker() {
} }
// take the count of messages // take the count of messages
let lastMessageNumber = context.chat.length ? context.chat.length : 0 let lastMessageNumber = context.chat.length ? context.chat.length : 0;
// There's no new messages // There's no new messages
let diff = lastMessageNumber - currentMessageNumber let diff = lastMessageNumber - currentMessageNumber;
let hashNew = getStringHash((chat.length && chat[chat.length - 1].mes) ?? '') let hashNew = getStringHash((chat.length && chat[chat.length - 1].mes) ?? '');
// if messages got deleted, diff will be < 0 // if messages got deleted, diff will be < 0
if (diff < 0) { if (diff < 0) {
// necessary actions will be taken by the onChatDeleted() handler // necessary actions will be taken by the onChatDeleted() handler
return return;
} }
// if no new messages, or same message, or same message hash, do nothing
if (diff == 0 && hashNew === lastMessageHash) { if (diff == 0 && hashNew === lastMessageHash) {
return return;
}
// If streaming, wait for streaming to finish before processing new messages
if (context.streamingProcessor && !context.streamingProcessor.isFinished) {
return;
} }
// clone message object, as things go haywire if message object is altered below (it's passed by reference) // clone message object, as things go haywire if message object is altered below (it's passed by reference)
const message = structuredClone(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 last message within current message, message got extended. only send diff to TTS.
if (ttsLastMessage !== null && message.mes.indexOf(ttsLastMessage) !== -1) { if (ttsLastMessage !== null && message.mes.indexOf(ttsLastMessage) !== -1) {
let tmp = message.mes let tmp = message.mes;
message.mes = message.mes.replace(ttsLastMessage, '') message.mes = message.mes.replace(ttsLastMessage, '');
ttsLastMessage = tmp ttsLastMessage = tmp;
} else { } else {
ttsLastMessage = message.mes ttsLastMessage = message.mes;
} }
// We're currently swiping or streaming. Don't generate voice // We're currently swiping. Don't generate voice
if ( if (!message || message.mes === '...' || message.mes === '') {
!message || return;
message.mes === '...' ||
message.mes === '' ||
(context.streamingProcessor && !context.streamingProcessor.isFinished)
) {
return
} }
// Don't generate if message doesn't have a display text // Don't generate if message doesn't have a display text
@ -454,7 +455,7 @@ let currentTtsJob // Null if nothing is currently being processed
let currentMessageNumber = 0 let currentMessageNumber = 0
function completeTtsJob() { function completeTtsJob() {
console.info(`Current TTS job for ${currentTtsJob.name} completed.`) console.info(`Current TTS job for ${currentTtsJob?.name} completed.`)
currentTtsJob = null currentTtsJob = null
} }