Merge branch 'release' into staging

This commit is contained in:
Cohee 2023-11-09 23:30:12 +02:00
commit 1f89e5a02a
2 changed files with 44 additions and 2 deletions

View File

@ -77,6 +77,8 @@ let ttsProviders = {
let ttsProvider
let ttsProviderName
let ttsLastMessage = null;
async function onNarrateOneMessage() {
audioElement.src = '/sounds/silence.mp3';
const context = getContext();
@ -169,11 +171,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 = 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) {
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 (
@ -665,6 +683,26 @@ export function saveTtsProviderSettings() {
async function onChatChanged() {
await resetTtsPlayback()
await initVoiceMap()
ttsLastMessage = null
}
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()
}
/**
@ -946,6 +984,7 @@ $(document).ready(function () {
setInterval(wrapper.update.bind(wrapper), UPDATE_INTERVAL) // Init depends on all the things
eventSource.on(event_types.MESSAGE_SWIPED, resetTtsPlayback);
eventSource.on(event_types.CHAT_CHANGED, onChatChanged)
eventSource.on(event_types.MESSAGE_DELETED, onChatDeleted);
eventSource.on(event_types.GROUP_UPDATED, onChatChanged)
registerSlashCommand('speak', onNarrateText, ['narrate', 'tts'], `<span class="monospace">(text)</span> narrate any text using currently selected character's voice. Use voice="Character Name" argument to set other voice from the voice map, example: <tt>/speak voice="Donald Duck" Quack!</tt>`, true, true);
})

View File

@ -203,7 +203,10 @@ function registerEndpoints(app, jsonParser) {
}
const text = request.body.text;
const lang = request.body.lang;
let lang = request.body.lang;
if (request.body.lang === 'zh-CN') {
lang = 'ZH'
}
if (!text || !lang) {
return response.sendStatus(400);