lint: Require semicolons

This commit is contained in:
Cohee
2023-12-02 21:11:06 +02:00
parent 2ec14a59ee
commit c63cd87cc0
67 changed files with 1554 additions and 1552 deletions

View File

@ -11,7 +11,7 @@ export { MODULE_NAME };
const MODULE_NAME = 'assets';
const DEBUG_PREFIX = '<Assets module> ';
let previewAudio = null;
let ASSETS_JSON_URL = 'https://raw.githubusercontent.com/SillyTavern/SillyTavern-Content/main/index.json'
let ASSETS_JSON_URL = 'https://raw.githubusercontent.com/SillyTavern/SillyTavern-Content/main/index.json';
// DBG
@ -49,7 +49,7 @@ function downloadAssetsList(url) {
for (const assetType of assetTypes) {
let assetTypeMenu = $('<div />', { id: 'assets_audio_ambient_div', class: 'assets-list-div' });
assetTypeMenu.append(`<h3>${assetType}</h3>`)
assetTypeMenu.append(`<h3>${assetType}</h3>`);
if (assetType == 'extension') {
assetTypeMenu.append(`
@ -61,7 +61,7 @@ function downloadAssetsList(url) {
for (const i in availableAssets[assetType]) {
const asset = availableAssets[assetType][i];
const elemId = `assets_install_${assetType}_${i}`;
let element = $('<button />', { id: elemId, type: 'button', class: 'asset-download-button menu_button' })
let element = $('<button />', { id: elemId, type: 'button', class: 'asset-download-button menu_button' });
const label = $('<i class="fa-fw fa-solid fa-download fa-xl"></i>');
element.append(label);
@ -98,7 +98,7 @@ function downloadAssetsList(url) {
label.addClass('fa-download');
element.off('mouseenter').off('mouseleave');
element.on('click', assetInstall);
}
};
if (isAssetInstalled(assetType, asset['id'])) {
console.debug(DEBUG_PREFIX, 'installed, checked');
@ -116,12 +116,12 @@ function downloadAssetsList(url) {
});
}
else {
console.debug(DEBUG_PREFIX, 'not installed, unchecked')
console.debug(DEBUG_PREFIX, 'not installed, unchecked');
element.prop('checked', false);
element.on('click', assetInstall);
}
console.debug(DEBUG_PREFIX, 'Created element for ', asset['id'])
console.debug(DEBUG_PREFIX, 'Created element for ', asset['id']);
const displayName = DOMPurify.sanitize(asset['name'] || asset['id']);
const description = DOMPurify.sanitize(asset['description'] || '');
@ -200,9 +200,9 @@ async function installAsset(url, assetType, filename) {
const category = assetType;
try {
if (category === 'extension') {
console.debug(DEBUG_PREFIX, 'Installing extension ', url)
console.debug(DEBUG_PREFIX, 'Installing extension ', url);
await installExtension(url);
console.debug(DEBUG_PREFIX, 'Extension installed.')
console.debug(DEBUG_PREFIX, 'Extension installed.');
return;
}
@ -214,7 +214,7 @@ async function installAsset(url, assetType, filename) {
cache: 'no-cache',
});
if (result.ok) {
console.debug(DEBUG_PREFIX, 'Download success.')
console.debug(DEBUG_PREFIX, 'Download success.');
}
}
catch (err) {
@ -228,9 +228,9 @@ async function deleteAsset(assetType, filename) {
const category = assetType;
try {
if (category === 'extension') {
console.debug(DEBUG_PREFIX, 'Deleting extension ', filename)
console.debug(DEBUG_PREFIX, 'Deleting extension ', filename);
await deleteExtension(filename);
console.debug(DEBUG_PREFIX, 'Extension deleted.')
console.debug(DEBUG_PREFIX, 'Extension deleted.');
}
const body = { category, filename };
@ -241,7 +241,7 @@ async function deleteAsset(assetType, filename) {
cache: 'no-cache',
});
if (result.ok) {
console.debug(DEBUG_PREFIX, 'Deletion success.')
console.debug(DEBUG_PREFIX, 'Deletion success.');
}
}
catch (err) {
@ -255,7 +255,7 @@ async function deleteAsset(assetType, filename) {
//#############################//
async function updateCurrentAssets() {
console.debug(DEBUG_PREFIX, 'Checking installed assets...')
console.debug(DEBUG_PREFIX, 'Checking installed assets...');
try {
const result = await fetch('/api/assets/get', {
method: 'POST',
@ -266,7 +266,7 @@ async function updateCurrentAssets() {
catch (err) {
console.log(err);
}
console.debug(DEBUG_PREFIX, 'Current assets found:', currentAssets)
console.debug(DEBUG_PREFIX, 'Current assets found:', currentAssets);
}

View File

@ -87,7 +87,7 @@ async function sendCaptionedMessage(caption, image) {
let template = extension_settings.caption.template || TEMPLATE_DEFAULT;
if (!/{{caption}}/i.test(template)) {
console.warn('Poka-yoke: Caption template does not contain {{caption}}. Appending it.')
console.warn('Poka-yoke: Caption template does not contain {{caption}}. Appending it.');
template += ' {{caption}}';
}

View File

@ -229,14 +229,14 @@ async function visualNovelUpdateLayers(container) {
images.sort(sortFunction).each((index, current) => {
const element = $(current);
const elementID = element.attr('id')
const elementID = element.attr('id');
// skip repositioning of dragged elements
if (element.data('dragged')
|| (power_user.movingUIState[elementID]
&& (typeof power_user.movingUIState[elementID] === 'object')
&& Object.keys(power_user.movingUIState[elementID]).length > 0)) {
loadMovingUIState()
loadMovingUIState();
//currentPosition += imagesWidth[index];
return;
}
@ -298,7 +298,7 @@ async function setImage(img, path) {
//only swap expressions when necessary
if (prevExpressionSrc !== path && !img.hasClass('expression-animating')) {
//clone expression
expressionClone.addClass('expression-clone')
expressionClone.addClass('expression-clone');
//make invisible and remove id to prevent double ids
//must be made invisible to start because they share the same Z-index
expressionClone.attr('id', '').css({ opacity: 0 });
@ -855,7 +855,7 @@ async function validateImages(character, forceRedrawCached) {
if (spriteCache[character]) {
if (forceRedrawCached && $('#image_list').data('name') !== character) {
console.debug('force redrawing character sprites list')
console.debug('force redrawing character sprites list');
drawSpritesList(character, labels, spriteCache[character]);
}
@ -1002,7 +1002,7 @@ async function setExpression(character, expression, force) {
await validateImages(character);
const img = $('img.expression');
const prevExpressionSrc = img.attr('src');
const expressionClone = img.clone()
const expressionClone = img.clone();
const sprite = (spriteCache[character] && spriteCache[character].find(x => x.label === expression));
console.debug('checking for expression images to show..');
@ -1030,14 +1030,14 @@ async function setExpression(character, expression, force) {
if (prevExpressionSrc !== sprite.path
&& !img.hasClass('expression-animating')) {
//clone expression
expressionClone.addClass('expression-clone')
expressionClone.addClass('expression-clone');
//make invisible and remove id to prevent double ids
//must be made invisible to start because they share the same Z-index
expressionClone.attr('id', '').css({ opacity: 0 });
//add new sprite path to clone src
expressionClone.attr('src', sprite.path);
//add invisible clone to html
expressionClone.appendTo($('#expression-holder'))
expressionClone.appendTo($('#expression-holder'));
const duration = 200;
@ -1426,7 +1426,7 @@ function setExpressionOverrideHtml(forceClear = false) {
function addVisualNovelMode() {
const html = `
<div id="visual-novel-wrapper">
</div>`
</div>`;
const element = $(html);
element.hide();
$('body').append(element);
@ -1444,9 +1444,9 @@ function setExpressionOverrideHtml(forceClear = false) {
});
$('#expression_override_cleanup_button').on('click', onClickExpressionOverrideRemoveAllButton);
$(document).on('dragstart', '.expression', (e) => {
e.preventDefault()
return false
})
e.preventDefault();
return false;
});
$(document).on('click', '.expression_list_item', onClickExpressionImage);
$(document).on('click', '.expression_list_upload', onClickExpressionUpload);
$(document).on('click', '.expression_list_delete', onClickExpressionDelete);
@ -1472,7 +1472,7 @@ function setExpressionOverrideHtml(forceClear = false) {
const updateFunction = wrapper.update.bind(wrapper);
setInterval(updateFunction, UPDATE_INTERVAL);
moduleWorker();
dragElement($('#expression-holder'))
dragElement($('#expression-holder'));
eventSource.on(event_types.CHAT_CHANGED, () => {
// character changed
removeExpression();

View File

@ -111,7 +111,7 @@ async function initGallery(items, url) {
});
//let images populate first
await delay(100)
await delay(100);
//unset the height (which must be getting set by the gallery library at some point)
$('#dragGallery').css('height', 'unset');
//force a resize to make images display correctly
@ -227,7 +227,7 @@ async function uploadFile(file, url) {
// Replacing alert with toastr error notification
toastr.error('Failed to upload the file.');
}
}
};
reader.readAsDataURL(file);
}
@ -256,14 +256,14 @@ $(document).ready(function () {
*/
function makeMovable(id = 'gallery') {
console.debug('making new container from template')
console.debug('making new container from template');
const template = $('#generic_draggable_template').html();
const newElement = $(template);
newElement.css('background-color', 'var(--SmartThemeBlurTintColor)');
newElement.attr('forChar', id);
newElement.attr('id', `${id}`);
newElement.find('.drag-grabber').attr('id', `${id}header`);
newElement.find('.dragTitle').text('Image Gallery')
newElement.find('.dragTitle').text('Image Gallery');
//add a div for the gallery
newElement.append('<div id="dragGallery"></div>');
// add no-scrollbar class to this element

View File

@ -29,7 +29,7 @@ const formatMemoryValue = function (value) {
} else {
return `Summary: ${value}`;
}
}
};
const saveChatDebounced = debounce(() => getContext().saveChat(), 2000);
@ -319,8 +319,8 @@ async function onChatEvent() {
async function forceSummarizeChat() {
const context = getContext();
const skipWIAN = extension_settings.memory.SkipWIAN
console.log(`Skipping WIAN? ${skipWIAN}`)
const skipWIAN = extension_settings.memory.SkipWIAN;
console.log(`Skipping WIAN? ${skipWIAN}`);
if (!context.chatId) {
toastr.warning('No chat selected');
return;
@ -336,7 +336,7 @@ async function forceSummarizeChat() {
}
async function summarizeChat(context) {
const skipWIAN = extension_settings.memory.SkipWIAN
const skipWIAN = extension_settings.memory.SkipWIAN;
switch (extension_settings.memory.source) {
case summary_sources.extras:
await summarizeChatExtras(context);
@ -409,7 +409,7 @@ async function summarizeChatMain(context, force, skipWIAN) {
console.debug('Summarization prompt is empty. Skipping summarization.');
return;
}
console.log('sending summary prompt')
console.log('sending summary prompt');
const summary = await generateQuietPrompt(prompt, false, skipWIAN);
const newContext = getContext();
@ -565,25 +565,25 @@ function doPopout(e) {
const target = e.target;
//repurposes the zoomed avatar template to server as a floating div
if ($('#summaryExtensionPopout').length === 0) {
console.debug('did not see popout yet, creating')
const originalHTMLClone = $(target).parent().parent().parent().find('.inline-drawer-content').html()
const originalElement = $(target).parent().parent().parent().find('.inline-drawer-content')
console.debug('did not see popout yet, creating');
const originalHTMLClone = $(target).parent().parent().parent().find('.inline-drawer-content').html();
const originalElement = $(target).parent().parent().parent().find('.inline-drawer-content');
const template = $('#zoomed_avatar_template').html();
const controlBarHtml = `<div class="panelControlBar flex-container">
<div id="summaryExtensionPopoutheader" class="fa-solid fa-grip drag-grabber hoverglow"></div>
<div id="summaryExtensionPopoutClose" class="fa-solid fa-circle-xmark hoverglow dragClose"></div>
</div>`
</div>`;
const newElement = $(template);
newElement.attr('id', 'summaryExtensionPopout')
.removeClass('zoomed_avatar')
.addClass('draggable')
.empty()
.empty();
const prevSummaryBoxContents = $('#memory_contents').val(); //copy summary box before emptying
originalElement.empty();
originalElement.html('<div class="flex-container alignitemscenter justifyCenter wide100p"><small>Currently popped out</small></div>')
newElement.append(controlBarHtml).append(originalHTMLClone)
originalElement.html('<div class="flex-container alignitemscenter justifyCenter wide100p"><small>Currently popped out</small></div>');
newElement.append(controlBarHtml).append(originalHTMLClone);
$('body').append(newElement);
$('#summaryExtensionDrawerContents').addClass('scrollableInnerFull')
$('#summaryExtensionDrawerContents').addClass('scrollableInnerFull');
setMemoryContext(prevSummaryBoxContents, false); //paste prev summary box contents into popout box
setupListeners();
loadSettings();
@ -594,18 +594,18 @@ function doPopout(e) {
//setup listener for close button to restore extensions menu
$('#summaryExtensionPopoutClose').off('click').on('click', function () {
$('#summaryExtensionDrawerContents').removeClass('scrollableInnerFull')
const summaryPopoutHTML = $('#summaryExtensionDrawerContents')
$('#summaryExtensionDrawerContents').removeClass('scrollableInnerFull');
const summaryPopoutHTML = $('#summaryExtensionDrawerContents');
$('#summaryExtensionPopout').fadeOut(250, () => {
originalElement.empty();
originalElement.html(summaryPopoutHTML);
$('#summaryExtensionPopout').remove()
})
$('#summaryExtensionPopout').remove();
});
loadSettings();
})
});
} else {
console.debug('saw existing popout, removing')
$('#summaryExtensionPopout').fadeOut(250, () => { $('#summaryExtensionPopoutClose').trigger('click') });
console.debug('saw existing popout, removing');
$('#summaryExtensionPopout').fadeOut(250, () => { $('#summaryExtensionPopoutClose').trigger('click'); });
}
}
@ -630,7 +630,7 @@ function setupListeners() {
$('input[name="memory_position"]').off('click').on('change', onMemoryPositionChange);
$('#memory_prompt_words_force').off('click').on('input', onMemoryPromptWordsForceInput);
$('#summarySettingsBlockToggle').off('click').on('click', function () {
console.log('saw settings button click')
console.log('saw settings button click');
$('#summarySettingsBlock').slideToggle(200, 'swing'); //toggleClass("hidden");
});
}

View File

@ -22,7 +22,7 @@ const defaultSettings = {
placeBeforeInputEnabled: false,
quickActionEnabled: false,
AutoInputInject: true,
}
};
//method from worldinfo
async function updateQuickReplyPresetList() {
@ -53,7 +53,7 @@ async function updateQuickReplyPresetList() {
async function loadSettings(type) {
if (type === 'init') {
await updateQuickReplyPresetList()
await updateQuickReplyPresetList();
}
if (Object.keys(extension_settings.quickReply).length === 0) {
Object.assign(extension_settings.quickReply, defaultSettings);
@ -109,7 +109,7 @@ function onQuickReplyLabelInput(id) {
}
async function onQuickReplyContextMenuChange(id) {
extension_settings.quickReply.quickReplySlots[id - 1].contextMenu = JSON.parse($(`#quickReplyContainer > [data-order="${id}"]`).attr('data-contextMenu'))
extension_settings.quickReply.quickReplySlots[id - 1].contextMenu = JSON.parse($(`#quickReplyContainer > [data-order="${id}"]`).attr('data-contextMenu'));
saveSettingsDebounced();
}
@ -141,9 +141,9 @@ async function onQuickReplyCtxButtonClick(id) {
dom.querySelector('.quickReply_contextMenuEditor_chaining').checked = item.chain;
$('.quickReply_contextMenuEditor_remove', ctxItem).on('click', () => ctxItem.remove());
document.querySelector('#quickReply_contextMenuEditor_content').append(ctxItem);
}
};
[...qr.contextMenu, {}].forEach((item, idx) => {
addCtxItem(item, idx)
addCtxItem(item, idx);
});
$('#quickReply_contextMenuEditor_addPreset').on('click', () => {
addCtxItem({}, document.querySelector('#quickReply_contextMenuEditor_content').children.length);
@ -204,7 +204,7 @@ async function onQuickReplyCtxButtonClick(id) {
}
async function onQuickReplyEnabledInput() {
let isEnabled = $(this).prop('checked')
let isEnabled = $(this).prop('checked');
extension_settings.quickReply.quickReplyEnabled = !!isEnabled;
if (isEnabled === true) {
$('#quickReplyBar').show();
@ -327,29 +327,29 @@ function buildContextMenu(qr, chainMes = null, hierarchy = [], labelHierarchy =
async function doQuickReplyBarPopout() {
//shared elements
const newQuickRepliesDiv = '<div id="quickReplies"></div>'
const popoutButtonClone = $('#quickReplyPopoutButton')
const newQuickRepliesDiv = '<div id="quickReplies"></div>';
const popoutButtonClone = $('#quickReplyPopoutButton');
if ($('#quickReplyBarPopout').length === 0) {
console.debug('did not see popout yet, creating')
console.debug('did not see popout yet, creating');
const template = $('#zoomed_avatar_template').html();
const controlBarHtml = `<div class="panelControlBar flex-container">
<div id="quickReplyBarPopoutheader" class="fa-solid fa-grip drag-grabber hoverglow"></div>
<div id="quickReplyBarPopoutClose" class="fa-solid fa-circle-xmark hoverglow"></div>
</div>`
</div>`;
const newElement = $(template);
let quickRepliesClone = $('#quickReplies').html()
let quickRepliesClone = $('#quickReplies').html();
newElement.attr('id', 'quickReplyBarPopout')
.removeClass('zoomed_avatar')
.addClass('draggable scrollY')
.empty()
.append(controlBarHtml)
.append(newQuickRepliesDiv)
.append(newQuickRepliesDiv);
//empty original bar
$('#quickReplyBar').empty()
$('#quickReplyBar').empty();
//add clone in popout
$('body').append(newElement);
$('#quickReplies').append(quickRepliesClone).css('margin-top', '1em')
$('#quickReplies').append(quickRepliesClone).css('margin-top', '1em');
$('.quickReplyButton').on('click', function () {
let index = $(this).data('index');
sendQuickReply(index);
@ -364,7 +364,7 @@ async function doQuickReplyBarPopout() {
const menu = new ContextMenu(tree.children);
menu.show(evt);
}
})
});
$('.quickReplyButton').on('contextmenu', function (evt) {
let index = $(this).data('index');
const qr = extension_settings.quickReply.quickReplySlots[index];
@ -377,16 +377,16 @@ async function doQuickReplyBarPopout() {
});
loadMovingUIState();
$('#quickReplyBarPopout').fadeIn(250)
dragElement(newElement)
$('#quickReplyBarPopout').fadeIn(250);
dragElement(newElement);
$('#quickReplyBarPopoutClose').off('click').on('click', function () {
console.debug('saw existing popout, removing')
let quickRepliesClone = $('#quickReplies').html()
$('#quickReplyBar').append(newQuickRepliesDiv)
$('#quickReplies').prepend(quickRepliesClone)
$('#quickReplyBar').append(popoutButtonClone).fadeIn(250)
$('#quickReplyBarPopout').fadeOut(250, () => { $('#quickReplyBarPopout').remove() });
console.debug('saw existing popout, removing');
let quickRepliesClone = $('#quickReplies').html();
$('#quickReplyBar').append(newQuickRepliesDiv);
$('#quickReplies').prepend(quickRepliesClone);
$('#quickReplyBar').append(popoutButtonClone).fadeIn(250);
$('#quickReplyBarPopout').fadeOut(250, () => { $('#quickReplyBarPopout').remove(); });
$('.quickReplyButton').on('click', function () {
let index = $(this).data('index');
sendQuickReply(index);
@ -401,7 +401,7 @@ async function doQuickReplyBarPopout() {
const menu = new ContextMenu(tree.children);
menu.show(evt);
}
})
});
$('.quickReplyButton').on('contextmenu', function (evt) {
let index = $(this).data('index');
const qr = extension_settings.quickReply.quickReplySlots[index];
@ -412,8 +412,8 @@ async function doQuickReplyBarPopout() {
menu.show(evt);
}
});
$('#quickReplyPopoutButton').off('click').on('click', doQuickReplyBarPopout)
})
$('#quickReplyPopoutButton').off('click').on('click', doQuickReplyBarPopout);
});
}
}
@ -422,9 +422,9 @@ function addQuickReplyBar() {
let quickReplyButtonHtml = '';
var targetContainer;
if ($('#quickReplyBarPopout').length !== 0) {
targetContainer = 'popout'
targetContainer = 'popout';
} else {
targetContainer = 'bar'
targetContainer = 'bar';
$('#quickReplyBar').remove();
}
@ -452,7 +452,7 @@ function addQuickReplyBar() {
if (targetContainer === 'bar') {
$('#send_form').prepend(quickReplyBarFullHtml);
} else {
$('#quickReplies').empty().append(quickReplyButtonHtml)
$('#quickReplies').empty().append(quickReplyButtonHtml);
}
@ -460,7 +460,7 @@ function addQuickReplyBar() {
let index = $(this).data('index');
sendQuickReply(index);
});
$('#quickReplyPopoutButton').off('click').on('click', doQuickReplyBarPopout)
$('#quickReplyPopoutButton').off('click').on('click', doQuickReplyBarPopout);
$('.quickReplyButton > .ctx-expander').on('click', function (evt) {
evt.stopPropagation();
let index = $(this.closest('.quickReplyButton')).data('index');
@ -471,7 +471,7 @@ function addQuickReplyBar() {
const menu = new ContextMenu(tree.children);
menu.show(evt);
}
})
});
$('.quickReplyButton').on('contextmenu', function (evt) {
let index = $(this).data('index');
const qr = extension_settings.quickReply.quickReplySlots[index];
@ -507,7 +507,7 @@ async function saveQuickReplyPreset() {
numberOfSlots: extension_settings.quickReply.numberOfSlots,
AutoInputInject: extension_settings.quickReply.AutoInputInject,
selectedPreset: name,
}
};
const response = await fetch('/savequickreply', {
method: 'POST',
@ -532,13 +532,13 @@ async function saveQuickReplyPreset() {
}
saveSettingsDebounced();
} else {
toastr.warning('Failed to save Quick Reply Preset.')
toastr.warning('Failed to save Quick Reply Preset.');
}
}
//just a copy of save function with the name hardcoded to currently selected preset
async function updateQuickReplyPreset() {
const name = $('#quickReplyPresets').val()
const name = $('#quickReplyPresets').val();
if (!name) {
return;
@ -551,7 +551,7 @@ async function updateQuickReplyPreset() {
numberOfSlots: extension_settings.quickReply.numberOfSlots,
AutoInputInject: extension_settings.quickReply.AutoInputInject,
selectedPreset: name,
}
};
const response = await fetch('/savequickreply', {
method: 'POST',
@ -576,7 +576,7 @@ async function updateQuickReplyPreset() {
}
saveSettingsDebounced();
} else {
toastr.warning('Failed to save Quick Reply Preset.')
toastr.warning('Failed to save Quick Reply Preset.');
}
}
@ -653,14 +653,14 @@ async function applyQuickReplyPreset(name) {
const quickReplyPreset = presets.find(x => x.name == name);
if (!quickReplyPreset) {
toastr.warning(`error, QR preset '${name}' not found. Confirm you are using proper case sensitivity!`)
toastr.warning(`error, QR preset '${name}' not found. Confirm you are using proper case sensitivity!`);
return;
}
extension_settings.quickReply = quickReplyPreset;
extension_settings.quickReply.selectedPreset = name;
saveSettingsDebounced()
loadSettings('init')
saveSettingsDebounced();
loadSettings('init');
addQuickReplyBar();
moduleWorker();
@ -669,42 +669,42 @@ async function applyQuickReplyPreset(name) {
}
async function doQRPresetSwitch(_, text) {
text = String(text)
applyQuickReplyPreset(text)
text = String(text);
applyQuickReplyPreset(text);
}
async function doQR(_, text) {
if (!text) {
toastr.warning('must specify which QR # to use')
return
toastr.warning('must specify which QR # to use');
return;
}
text = Number(text)
text = Number(text);
//use scale starting with 0
//ex: user inputs "/qr 2" >> qr with data-index 1 (but 2nd item displayed) gets triggered
let QRnum = Number(text - 1)
if (QRnum <= 0) { QRnum = 0 }
let QRnum = Number(text - 1);
if (QRnum <= 0) { QRnum = 0; }
const whichQR = $('#quickReplies').find(`[data-index='${QRnum}']`);
whichQR.trigger('click')
whichQR.trigger('click');
}
function saveQROrder() {
//update html-level order data to match new sort
let i = 1
let i = 1;
$('#quickReplyContainer').children().each(function () {
$(this).attr('data-order', i)
$(this).find('input').attr('id', `quickReply${i}Label`)
$(this).find('textarea').attr('id', `quickReply${i}Mes`)
i++
$(this).attr('data-order', i);
$(this).find('input').attr('id', `quickReply${i}Label`);
$(this).find('textarea').attr('id', `quickReply${i}Mes`);
i++;
});
//rebuild the extension_Settings array based on new order
i = 1
i = 1;
$('#quickReplyContainer').children().each(function () {
onQuickReplyContextMenuChange(i)
onQuickReplyLabelInput(i)
onQuickReplyInput(i)
i++
onQuickReplyContextMenuChange(i);
onQuickReplyLabelInput(i);
onQuickReplyInput(i);
i++;
});
}
@ -892,4 +892,4 @@ jQuery(async () => {
jQuery(() => {
registerSlashCommand('qr', doQR, [], '<span class="monospace">(number)</span> activates the specified Quick Reply', true, true);
registerSlashCommand('qrset', doQRPresetSwitch, [], '<span class="monospace">(name)</span> swaps to the specified Quick Reply Preset', true, true);
})
});

View File

@ -4,7 +4,7 @@ export {
regex_placement,
getRegexedString,
runRegexScript
}
};
const regex_placement = {
// MD Display is deprecated. Do not use.
@ -12,12 +12,12 @@ const regex_placement = {
USER_INPUT: 1,
AI_OUTPUT: 2,
SLASH_COMMAND: 3
}
};
const regex_replace_strategy = {
REPLACE: 0,
OVERLAY: 1
}
};
// Originally from: https://github.com/IonicaBizau/regex-parser.js/blob/master/lib/index.js
function regexFromString(input) {

View File

@ -119,7 +119,7 @@ async function onRegexEditorOpenClick(existingId) {
if (existingScript.scriptName) {
editorHtml.find('.regex_script_name').val(existingScript.scriptName);
} else {
toastr.error('This script doesn\'t have a name! Please delete it.')
toastr.error('This script doesn\'t have a name! Please delete it.');
return;
}
@ -176,7 +176,7 @@ async function onRegexEditorOpenClick(existingId) {
editorHtml
.find('input[name="replace_position"]')
.filter(':checked')
.map(function () { return parseInt($(this).val()) })
.map(function () { return parseInt($(this).val()); })
.get()
.filter((e) => !isNaN(e)) || [],
disabled:
@ -222,8 +222,8 @@ function migrateSettings() {
Object.values(regex_placement).filter((e) => e !== regex_placement.MD_DISPLAY) :
script.placement = script.placement.filter((e) => e !== regex_placement.MD_DISPLAY);
script.markdownOnly = true
script.promptOnly = true
script.markdownOnly = true;
script.promptOnly = true;
performSave = true;
}

View File

@ -31,7 +31,7 @@ const m = x => `<span class="monospace">${x}</span>`;
// Joins an array of strings with ' / '
const j = a => a.join(' / ');
// Wraps a string into paragraph block
const p = a => `<p>${a}</p>`
const p = a => `<p>${a}</p>`;
const MODULE_NAME = 'sd';
const UPDATE_INTERVAL = 1000;
@ -44,7 +44,7 @@ const sources = {
vlad: 'vlad',
openai: 'openai',
comfy: 'comfy',
}
};
const generationMode = {
CHARACTER: 0,
@ -58,13 +58,13 @@ const generationMode = {
CHARACTER_MULTIMODAL: 8,
USER_MULTIMODAL: 9,
FACE_MULTIMODAL: 10,
}
};
const multimodalMap = {
[generationMode.CHARACTER]: generationMode.CHARACTER_MULTIMODAL,
[generationMode.USER]: generationMode.USER_MULTIMODAL,
[generationMode.FACE]: generationMode.FACE_MULTIMODAL,
}
};
const modeLabels = {
[generationMode.CHARACTER]: 'Character ("Yourself")',
@ -77,7 +77,7 @@ const modeLabels = {
[generationMode.CHARACTER_MULTIMODAL]: 'Character (Multimodal Mode)',
[generationMode.FACE_MULTIMODAL]: 'Portrait (Multimodal Mode)',
[generationMode.USER_MULTIMODAL]: 'User (Multimodal Mode)',
}
};
const triggerWords = {
[generationMode.CHARACTER]: ['you'],
@ -87,7 +87,7 @@ const triggerWords = {
[generationMode.NOW]: ['last'],
[generationMode.FACE]: ['face'],
[generationMode.BACKGROUND]: ['background'],
}
};
const messageTrigger = {
activationRegex: /\b(send|mail|imagine|generate|make|create|draw|paint|render)\b.*\b(pic|picture|image|drawing|painting|photo|photograph)\b(?:\s+of)?(?:\s+(?:a|an|the|this|that|those)?)?(.+)/i,
@ -99,7 +99,7 @@ const messageTrigger = {
[generationMode.FACE]: ['your face', 'your portrait', 'your selfie'],
[generationMode.BACKGROUND]: ['background', 'scene background', 'scene', 'scenery', 'surroundings', 'environment'],
},
}
};
const promptTemplates = {
/*OLD: [generationMode.CHARACTER]: "Pause your roleplay and provide comma-delimited list of phrases and keywords which describe {{char}}'s physical appearance and clothing. Ignore {{char}}'s personality traits, and chat history when crafting this description. End your response once the comma-delimited list is complete. Do not roleplay when writing this description, and do not attempt to continue the story.", */
@ -139,7 +139,7 @@ const promptTemplates = {
[generationMode.FACE_MULTIMODAL]: 'Provide an exhaustive comma-separated list of tags describing the appearance of the character on this image in great detail. Start with "close-up portrait".',
[generationMode.CHARACTER_MULTIMODAL]: 'Provide an exhaustive comma-separated list of tags describing the appearance of the character on this image in great detail. Start with "full body portrait".',
[generationMode.USER_MULTIMODAL]: 'Provide an exhaustive comma-separated list of tags describing the appearance of the character on this image in great detail. Start with "full body portrait".',
}
};
const helpString = [
`${m('(argument)')} requests to generate an image. Supported arguments: ${m(j(Object.values(triggerWords).flat()))}.`,
@ -244,7 +244,7 @@ const defaultSettings = {
// ComyUI settings
comfy_url: 'http://127.0.0.1:8188',
comfy_workflow: 'Default_Comfy_Workflow.json',
}
};
function processTriggers(chat, _, abort) {
if (!extension_settings.sd.interactive_mode) {
@ -452,7 +452,7 @@ function addPromptTemplates() {
const container = $('<div></div>')
.addClass('title_restorable')
.append(label)
.append(button)
.append(button);
$('#sd_prompt_templates').append(container);
$('#sd_prompt_templates').append(textarea);
}
@ -610,7 +610,7 @@ function combinePrefixes(str1, str2, macro = '') {
return str1;
}
str1 = process(str1)
str1 = process(str1);
str2 = process(str2);
// Combine the strings with a comma between them)
@ -1579,11 +1579,11 @@ function processReply(str) {
return '';
}
str = str.replaceAll('"', '')
str = str.replaceAll('“', '')
str = str.replaceAll('.', ',')
str = str.replaceAll('\n', ', ')
str = str.replace(/[^a-zA-Z0-9,:()]+/g, ' ') // Replace everything except alphanumeric characters and commas with spaces
str = str.replaceAll('"', '');
str = str.replaceAll('“', '');
str = str.replaceAll('.', ',');
str = str.replaceAll('\n', ', ');
str = str.replace(/[^a-zA-Z0-9,:()]+/g, ' '); // Replace everything except alphanumeric characters and commas with spaces
str = str.replace(/\s+/g, ' '); // Collapse multiple whitespaces into one
str = str.trim();
@ -1608,13 +1608,13 @@ function getRawLastMessage() {
toastr.warning('No usable messages found.', 'Image Generation');
throw new Error('No usable messages found.');
}
};
const context = getContext();
const lastMessage = getLastUsableMessage(),
characterDescription = context.characters[context.characterId].description,
situation = context.characters[context.characterId].scenario;
return `((${processReply(lastMessage)})), (${processReply(situation)}:0.7), (${processReply(characterDescription)}:0.5)`
return `((${processReply(lastMessage)})), (${processReply(situation)}:0.7), (${processReply(characterDescription)}:0.5)`;
}
async function generatePicture(_, trigger, message, callback) {
@ -1652,7 +1652,7 @@ async function generatePicture(_, trigger, message, callback) {
} else {
sendMessage(prompt, imagePath, generationType);
}
}
};
}
const dimensions = setTypeSpecificDimensions(generationType);
@ -1667,7 +1667,7 @@ async function generatePicture(_, trigger, message, callback) {
await sendGenerationRequest(generationType, prompt, characterName, callback);
} catch (err) {
console.trace(err);
throw new Error('SD prompt text generation failed.')
throw new Error('SD prompt text generation failed.');
}
finally {
restoreOriginalDimensions(dimensions);
@ -2269,7 +2269,7 @@ function addSDGenButtons() {
const waitButtonHtml = `
<div id="sd_gen_wait" class="fa-solid fa-hourglass-half" /></div>
`
`;
const dropdownHtml = `
<div id="sd_dropdown">
<ul class="list-group">
@ -2432,7 +2432,7 @@ $('#sd_dropdown [id]').on('click', function () {
const param = idParamMap[id];
if (param) {
console.log('doing /sd ' + param)
console.log('doing /sd ' + param);
generatePicture('sd', param);
}
});
@ -2492,7 +2492,7 @@ jQuery(async () => {
initScrollHeight($('#sd_prompt_prefix'));
initScrollHeight($('#sd_negative_prompt'));
initScrollHeight($('#sd_character_prompt'));
})
});
for (const [key, value] of Object.entries(resolutionOptions)) {
const option = document.createElement('option');

View File

@ -4,11 +4,11 @@ TODO:
- Delete useless call
*/
import { doExtrasFetch, extension_settings, getApiUrl, modules } from '../../extensions.js'
import { callPopup } from '../../../script.js'
import { initVoiceMap } from './index.js'
import { doExtrasFetch, extension_settings, getApiUrl, modules } from '../../extensions.js';
import { callPopup } from '../../../script.js';
import { initVoiceMap } from './index.js';
export { CoquiTtsProvider }
export { CoquiTtsProvider };
const DEBUG_PREFIX = '<Coqui TTS module> ';
@ -38,11 +38,11 @@ const languageLabels = {
'fr': 'French',
'es': 'Spanish',
'ja': 'Japanese'
}
};
function throwIfModuleMissing() {
if (!modules.includes('coqui-tts')) {
const message = 'Coqui TTS module not loaded. Add coqui-tts to enable-modules and restart the Extras API.'
const message = 'Coqui TTS module not loaded. Add coqui-tts to enable-modules and restart the Extras API.';
// toastr.error(message, { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
throw new Error(DEBUG_PREFIX, message);
}
@ -58,14 +58,14 @@ class CoquiTtsProvider {
// Extension UI and Settings //
//#############################//
settings
settings;
defaultSettings = {
voiceMap: {},
customVoices: {},
voiceIds: [],
voiceMapDict: {}
}
};
get settingsHtml() {
let html = `
@ -119,19 +119,19 @@ class CoquiTtsProvider {
</div>
</div>
</div>
`
return html
`;
return html;
}
async loadSettings(settings) {
// Only accept keys defined in defaultSettings
this.settings = this.defaultSettings
this.settings = this.defaultSettings;
for (const key in settings) {
if (key in this.settings) {
this.settings[key] = settings[key]
this.settings[key] = settings[key];
} else {
throw DEBUG_PREFIX + `Invalid setting passed to extension: ${key}`
throw DEBUG_PREFIX + `Invalid setting passed to extension: ${key}`;
}
}
@ -147,13 +147,13 @@ class CoquiTtsProvider {
$('#coqui_api_model_install_status').hide();
$('#coqui_api_model_install_button').hide();
let that = this
$('#coqui_model_origin').on('change', function () { that.onModelOriginChange() });
$('#coqui_api_language').on('change', function () { that.onModelLanguageChange() });
$('#coqui_api_model_name').on('change', function () { that.onModelNameChange() });
let that = this;
$('#coqui_model_origin').on('change', function () { that.onModelOriginChange(); });
$('#coqui_api_language').on('change', function () { that.onModelLanguageChange(); });
$('#coqui_api_model_name').on('change', function () { that.onModelNameChange(); });
$('#coqui_remove_voiceId_mapping').on('click', function () { that.onRemoveClick() });
$('#coqui_add_voiceId_mapping').on('click', function () { that.onAddClick() });
$('#coqui_remove_voiceId_mapping').on('click', function () { that.onRemoveClick(); });
$('#coqui_add_voiceId_mapping').on('click', function () { that.onAddClick(); });
// Load coqui-api settings from json file
await fetch('/scripts/extensions/tts/coqui_api_models_settings.json')
@ -198,8 +198,8 @@ class CoquiTtsProvider {
// Perform a simple readiness check by trying to fetch voiceIds
async checkReady(){
throwIfModuleMissing()
await this.fetchTtsVoiceObjects()
throwIfModuleMissing();
await this.fetchTtsVoiceObjects();
}
updateCustomVoices() {
@ -217,18 +217,18 @@ class CoquiTtsProvider {
}
// Update UI select list with voices
$('#coqui_voicename_select').empty()
$('#coqui_voicename_select').empty();
$('#coqui_voicename_select')
.find('option')
.remove()
.end()
.append('<option value="none">Select Voice</option>')
.val('none')
.val('none');
for (const voiceName in this.settings.voiceMapDict) {
$('#coqui_voicename_select').append(new Option(voiceName, voiceName));
}
this.onSettingsChange()
this.onSettingsChange();
}
onSettingsChange() {
@ -237,7 +237,7 @@ class CoquiTtsProvider {
}
async onRefreshClick() {
this.checkReady()
this.checkReady();
}
async onAddClick() {
@ -246,7 +246,7 @@ class CoquiTtsProvider {
}
// Ask user for voiceId name to save voice
const voiceName = await callPopup('<h3>Name of Coqui voice to add to voice select dropdown:</h3>', 'input')
const voiceName = await callPopup('<h3>Name of Coqui voice to add to voice select dropdown:</h3>', 'input');
const model_origin = $('#coqui_model_origin').val();
const model_language = $('#coqui_api_language').val();
@ -303,11 +303,11 @@ class CoquiTtsProvider {
const tokens = $('#coqui_api_model_name').val().split('/');
const model_dataset = tokens[0];
const model_label = tokens[1];
const model_id = 'tts_models/' + model_language + '/' + model_dataset + '/' + model_label
const model_id = 'tts_models/' + model_language + '/' + model_dataset + '/' + model_label;
let modelDict = coquiApiModels
let modelDict = coquiApiModels;
if (model_origin == 'coqui-api-full')
modelDict = coquiApiModelsFull
modelDict = coquiApiModelsFull;
if (model_setting_language == null & 'languages' in modelDict[model_language][model_dataset][model_label]) {
toastr.error('Model language not selected, please select one.', DEBUG_PREFIX+' voice mapping model language', { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
@ -326,7 +326,7 @@ class CoquiTtsProvider {
console.debug(DEBUG_PREFIX, 'Registered new voice map: ', voiceName, ':', this.settings.voiceMapDict[voiceName]);
this.updateCustomVoices();
initVoiceMap() // Update TTS extension voiceMap
initVoiceMap(); // Update TTS extension voiceMap
let successMsg = voiceName + ':' + model_id;
if (model_setting_language != null)
@ -335,16 +335,16 @@ class CoquiTtsProvider {
successMsg += '[' + model_setting_speaker + ']';
toastr.info(successMsg, DEBUG_PREFIX + ' voice map updated', { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
return
return;
}
async getVoice(voiceName) {
let match = await this.fetchTtsVoiceObjects()
let match = await this.fetchTtsVoiceObjects();
match = match.filter(
voice => voice.name == voiceName
)[0]
)[0];
if (!match) {
throw `TTS Voice name ${voiceName} not found in CoquiTTS Provider voice list`
throw `TTS Voice name ${voiceName} not found in CoquiTTS Provider voice list`;
}
return match;
}
@ -360,11 +360,11 @@ class CoquiTtsProvider {
// Todo erase from voicemap
delete (this.settings.voiceMapDict[voiceName]);
this.updateCustomVoices();
initVoiceMap() // Update TTS extension voiceMap
initVoiceMap(); // Update TTS extension voiceMap
}
async onModelOriginChange() {
throwIfModuleMissing()
throwIfModuleMissing();
resetModelSettings();
const model_origin = $('#coqui_model_origin').val();
@ -385,9 +385,9 @@ class CoquiTtsProvider {
.val('none');
for(let language in coquiApiModels) {
let languageLabel = language
let languageLabel = language;
if (language in languageLabels)
languageLabel = languageLabels[language]
languageLabel = languageLabels[language];
$('#coqui_api_language').append(new Option(languageLabel,language));
console.log(DEBUG_PREFIX,'added language',languageLabel,'(',language,')');
}
@ -407,9 +407,9 @@ class CoquiTtsProvider {
.val('none');
for(let language in coquiApiModelsFull) {
let languageLabel = language
let languageLabel = language;
if (language in languageLabels)
languageLabel = languageLabels[language]
languageLabel = languageLabels[language];
$('#coqui_api_language').append(new Option(languageLabel,language));
console.log(DEBUG_PREFIX,'added language',languageLabel,'(',language,')');
}
@ -446,14 +446,14 @@ class CoquiTtsProvider {
.append('<option value="none">Select model</option>')
.val('none');
let modelDict = coquiApiModels
let modelDict = coquiApiModels;
if (model_origin == 'coqui-api-full')
modelDict = coquiApiModelsFull
modelDict = coquiApiModelsFull;
for(let model_dataset in modelDict[model_language])
for(let model_name in modelDict[model_language][model_dataset]) {
const model_id = model_dataset + '/' + model_name
const model_label = model_name + ' (' + model_dataset + ' dataset)'
const model_id = model_dataset + '/' + model_name;
const model_label = model_name + ' (' + model_dataset + ' dataset)';
$('#coqui_api_model_name').append(new Option(model_label, model_id));
}
}
@ -477,11 +477,11 @@ class CoquiTtsProvider {
const model_dataset = tokens[0];
const model_name = tokens[1];
let modelDict = coquiApiModels
let modelDict = coquiApiModels;
if (model_origin == 'coqui-api-full')
modelDict = coquiApiModelsFull
modelDict = coquiApiModelsFull;
const model_settings = modelDict[model_language][model_dataset][model_name]
const model_settings = modelDict[model_language][model_dataset][model_name];
if ('languages' in model_settings) {
$('#coqui_api_model_settings').show();
@ -525,22 +525,22 @@ class CoquiTtsProvider {
$('#coqui_api_model_install_status').show();
// Check if already installed and propose to do it otherwise
const model_id = modelDict[model_language][model_dataset][model_name]['id']
const model_id = modelDict[model_language][model_dataset][model_name]['id'];
console.debug(DEBUG_PREFIX,'Check if model is already installed',model_id);
let result = await CoquiTtsProvider.checkmodel_state(model_id);
result = await result.json();
const model_state = result['model_state'];
console.debug(DEBUG_PREFIX, ' Model state:', model_state)
console.debug(DEBUG_PREFIX, ' Model state:', model_state);
if (model_state == 'installed') {
$('#coqui_api_model_install_status').text('Model already installed on extras server');
$('#coqui_api_model_install_button').hide();
}
else {
let action = 'download'
let action = 'download';
if (model_state == 'corrupted') {
action = 'repare'
action = 'repare';
//toastr.error("Click install button to reinstall the model "+$("#coqui_api_model_name").find(":selected").text(), DEBUG_PREFIX+" corrupted model install", { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
$('#coqui_api_model_install_status').text('Model found but incomplete try install again (maybe still downloading)'); // (remove and download again)
}
@ -573,7 +573,7 @@ class CoquiTtsProvider {
$('#coqui_api_model_install_button').show();
}
} catch (error) {
console.error(error)
console.error(error);
toastr.error(error, DEBUG_PREFIX + ' error with model download', { timeOut: 10000, extendedTimeOut: 20000, preventDuplicates: true });
onModelNameChange_pointer();
}
@ -595,7 +595,7 @@ class CoquiTtsProvider {
Check model installation state, return one of ["installed", "corrupted", "absent"]
*/
static async checkmodel_state(model_id) {
throwIfModuleMissing()
throwIfModuleMissing();
const url = new URL(getApiUrl());
url.pathname = '/api/text-to-speech/coqui/coqui-api/check-model-state';
@ -615,11 +615,11 @@ class CoquiTtsProvider {
throw new Error(`HTTP ${apiResult.status}: ${await apiResult.text()}`);
}
return apiResult
return apiResult;
}
static async installModel(model_id, action) {
throwIfModuleMissing()
throwIfModuleMissing();
const url = new URL(getApiUrl());
url.pathname = '/api/text-to-speech/coqui/coqui-api/install-model';
@ -640,14 +640,14 @@ class CoquiTtsProvider {
throw new Error(`HTTP ${apiResult.status}: ${await apiResult.text()}`);
}
return apiResult
return apiResult;
}
/*
Retrieve user custom models
*/
static async getLocalModelList() {
throwIfModuleMissing()
throwIfModuleMissing();
const url = new URL(getApiUrl());
url.pathname = '/api/text-to-speech/coqui/local/get-models';
@ -661,14 +661,14 @@ class CoquiTtsProvider {
'model_id': 'model_id',
'action': 'action'
})
})
});
if (!apiResult.ok) {
toastr.error(apiResult.statusText, DEBUG_PREFIX + ' Get local model list request failed');
throw new Error(`HTTP ${apiResult.status}: ${await apiResult.text()}`);
}
return apiResult
return apiResult;
}
@ -677,27 +677,27 @@ class CoquiTtsProvider {
// tts_models/en/ljspeech/glow-tts
// ts_models/ja/kokoro/tacotron2-DDC
async generateTts(text, voiceId) {
throwIfModuleMissing()
voiceId = this.settings.customVoices[voiceId]
throwIfModuleMissing();
voiceId = this.settings.customVoices[voiceId];
const url = new URL(getApiUrl());
url.pathname = '/api/text-to-speech/coqui/generate-tts';
let language = 'none'
let speaker = 'none'
let language = 'none';
let speaker = 'none';
const tokens = voiceId.replaceAll(']', '').replaceAll('"', '').split('[');
const model_id = tokens[0]
const model_id = tokens[0];
console.debug(DEBUG_PREFIX, 'Preparing TTS request for', tokens)
console.debug(DEBUG_PREFIX, 'Preparing TTS request for', tokens);
// First option
if (tokens.length > 1) {
const option1 = tokens[1]
const option1 = tokens[1];
if (model_id.includes('multilingual'))
language = option1
language = option1;
else
speaker = option1
speaker = option1;
}
// Second option
@ -723,7 +723,7 @@ class CoquiTtsProvider {
throw new Error(`HTTP ${apiResult.status}: ${await apiResult.text()}`);
}
return apiResult
return apiResult;
}
// Dirty hack to say not implemented
@ -731,12 +731,12 @@ class CoquiTtsProvider {
const voiceIds = Object
.keys(this.settings.voiceMapDict)
.map(voice => ({ name: voice, voice_id: voice, preview_url: false }));
return voiceIds
return voiceIds;
}
// Do nothing
previewTtsVoice(id) {
return
return;
}
async fetchTtsFromHistory(history_item_id) {
@ -746,7 +746,7 @@ class CoquiTtsProvider {
async function initLocalModels() {
if (!modules.includes('coqui-tts'))
return
return;
// Initialized local model once
if (!coquiLocalModelsReceived) {

View File

@ -1,73 +1,73 @@
import { getRequestHeaders } from '../../../script.js'
import { getApiUrl } from '../../extensions.js'
import { doExtrasFetch, modules } from '../../extensions.js'
import { getPreviewString } from './index.js'
import { saveTtsProviderSettings } from './index.js'
import { getRequestHeaders } from '../../../script.js';
import { getApiUrl } from '../../extensions.js';
import { doExtrasFetch, modules } from '../../extensions.js';
import { getPreviewString } from './index.js';
import { saveTtsProviderSettings } from './index.js';
export { EdgeTtsProvider }
export { EdgeTtsProvider };
class EdgeTtsProvider {
//########//
// Config //
//########//
settings
voices = []
separator = ' . '
audioElement = document.createElement('audio')
settings;
voices = [];
separator = ' . ';
audioElement = document.createElement('audio');
defaultSettings = {
voiceMap: {},
rate: 0,
}
};
get settingsHtml() {
let html = `Microsoft Edge TTS Provider<br>
<label for="edge_tts_rate">Rate: <span id="edge_tts_rate_output"></span></label>
<input id="edge_tts_rate" type="range" value="${this.defaultSettings.rate}" min="-100" max="100" step="1" />`
return html
<input id="edge_tts_rate" type="range" value="${this.defaultSettings.rate}" min="-100" max="100" step="1" />`;
return html;
}
onSettingsChange() {
this.settings.rate = Number($('#edge_tts_rate').val());
$('#edge_tts_rate_output').text(this.settings.rate);
saveTtsProviderSettings()
saveTtsProviderSettings();
}
async loadSettings(settings) {
// Pupulate Provider UI given input settings
if (Object.keys(settings).length == 0) {
console.info('Using default TTS Provider settings')
console.info('Using default TTS Provider settings');
}
// Only accept keys defined in defaultSettings
this.settings = this.defaultSettings
this.settings = this.defaultSettings;
for (const key in settings) {
if (key in this.settings) {
this.settings[key] = settings[key]
this.settings[key] = settings[key];
} else {
throw `Invalid setting passed to TTS Provider: ${key}`
throw `Invalid setting passed to TTS Provider: ${key}`;
}
}
$('#edge_tts_rate').val(this.settings.rate || 0);
$('#edge_tts_rate_output').text(this.settings.rate || 0);
$('#edge_tts_rate').on('input', () => {this.onSettingsChange()})
await this.checkReady()
$('#edge_tts_rate').on('input', () => {this.onSettingsChange();});
await this.checkReady();
console.debug('EdgeTTS: Settings loaded')
console.debug('EdgeTTS: Settings loaded');
}
// Perform a simple readiness check by trying to fetch voiceIds
async checkReady(){
throwIfModuleMissing()
await this.fetchTtsVoiceObjects()
throwIfModuleMissing();
await this.fetchTtsVoiceObjects();
}
async onRefreshClick() {
return
return;
}
//#################//
@ -76,39 +76,39 @@ class EdgeTtsProvider {
async getVoice(voiceName) {
if (this.voices.length == 0) {
this.voices = await this.fetchTtsVoiceObjects()
this.voices = await this.fetchTtsVoiceObjects();
}
const match = this.voices.filter(
voice => voice.name == voiceName
)[0]
)[0];
if (!match) {
throw `TTS Voice name ${voiceName} not found`
throw `TTS Voice name ${voiceName} not found`;
}
return match
return match;
}
async generateTts(text, voiceId) {
const response = await this.fetchTtsGeneration(text, voiceId)
return response
const response = await this.fetchTtsGeneration(text, voiceId);
return response;
}
//###########//
// API CALLS //
//###########//
async fetchTtsVoiceObjects() {
throwIfModuleMissing()
throwIfModuleMissing();
const url = new URL(getApiUrl());
url.pathname = '/api/edge-tts/list'
const response = await doExtrasFetch(url)
url.pathname = '/api/edge-tts/list';
const response = await doExtrasFetch(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`)
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
let responseJson = await response.json()
let responseJson = await response.json();
responseJson = responseJson
.sort((a, b) => a.Locale.localeCompare(b.Locale) || a.ShortName.localeCompare(b.ShortName))
.map(x => ({ name: x.ShortName, voice_id: x.ShortName, preview_url: false, lang: x.Locale }));
return responseJson
return responseJson;
}
@ -117,9 +117,9 @@ class EdgeTtsProvider {
this.audioElement.currentTime = 0;
const voice = await this.getVoice(id);
const text = getPreviewString(voice.lang);
const response = await this.fetchTtsGeneration(text, id)
const response = await this.fetchTtsGeneration(text, id);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`)
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
const audio = await response.blob();
@ -129,9 +129,9 @@ class EdgeTtsProvider {
}
async fetchTtsGeneration(inputText, voiceId) {
throwIfModuleMissing()
throwIfModuleMissing();
console.info(`Generating new TTS for voice_id ${voiceId}`)
console.info(`Generating new TTS for voice_id ${voiceId}`);
const url = new URL(getApiUrl());
url.pathname = '/api/edge-tts/generate';
const response = await doExtrasFetch(url,
@ -144,19 +144,19 @@ class EdgeTtsProvider {
'rate': Number(this.settings.rate),
})
}
)
);
if (!response.ok) {
toastr.error(response.statusText, 'TTS Generation Failed');
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
return response
return response;
}
}
function throwIfModuleMissing() {
if (!modules.includes('edge-tts')) {
const message = 'Edge TTS module not loaded. Add edge-tts to enable-modules and restart the Extras API.'
const message = 'Edge TTS module not loaded. Add edge-tts to enable-modules and restart the Extras API.';
// toastr.error(message)
throw new Error(message)
throw new Error(message);
}
}

View File

@ -1,14 +1,14 @@
import { saveTtsProviderSettings } from './index.js'
export { ElevenLabsTtsProvider }
import { saveTtsProviderSettings } from './index.js';
export { ElevenLabsTtsProvider };
class ElevenLabsTtsProvider {
//########//
// Config //
//########//
settings
voices = []
separator = ' ... ... ... '
settings;
voices = [];
separator = ' ... ... ... ';
defaultSettings = {
@ -17,7 +17,7 @@ class ElevenLabsTtsProvider {
apiKey: '',
model: 'eleven_monolingual_v1',
voiceMap: {}
}
};
get settingsHtml() {
let html = `
@ -36,28 +36,28 @@ class ElevenLabsTtsProvider {
<label for="elevenlabs_tts_similarity_boost">Similarity Boost: <span id="elevenlabs_tts_similarity_boost_output"></span></label>
<input id="elevenlabs_tts_similarity_boost" type="range" value="${this.defaultSettings.similarity_boost}" min="0" max="1" step="0.05" />
</div>
`
return html
`;
return html;
}
onSettingsChange() {
// Update dynamically
this.settings.stability = $('#elevenlabs_tts_stability').val()
this.settings.similarity_boost = $('#elevenlabs_tts_similarity_boost').val()
this.settings.model = $('#elevenlabs_tts_model').find(':selected').val()
this.settings.stability = $('#elevenlabs_tts_stability').val();
this.settings.similarity_boost = $('#elevenlabs_tts_similarity_boost').val();
this.settings.model = $('#elevenlabs_tts_model').find(':selected').val();
$('#elevenlabs_tts_stability_output').text(this.settings.stability);
$('#elevenlabs_tts_similarity_boost_output').text(this.settings.similarity_boost);
saveTtsProviderSettings()
saveTtsProviderSettings();
}
async loadSettings(settings) {
// Pupulate Provider UI given input settings
if (Object.keys(settings).length == 0) {
console.info('Using default TTS Provider settings')
console.info('Using default TTS Provider settings');
}
// Only accept keys defined in defaultSettings
this.settings = this.defaultSettings
this.settings = this.defaultSettings;
// Migrate old settings
if (settings['multilingual'] !== undefined) {
@ -67,34 +67,34 @@ class ElevenLabsTtsProvider {
for (const key in settings) {
if (key in this.settings) {
this.settings[key] = settings[key]
this.settings[key] = settings[key];
} else {
throw `Invalid setting passed to TTS Provider: ${key}`
throw `Invalid setting passed to TTS Provider: ${key}`;
}
}
$('#elevenlabs_tts_stability').val(this.settings.stability)
$('#elevenlabs_tts_similarity_boost').val(this.settings.similarity_boost)
$('#elevenlabs_tts_api_key').val(this.settings.apiKey)
$('#elevenlabs_tts_stability').val(this.settings.stability);
$('#elevenlabs_tts_similarity_boost').val(this.settings.similarity_boost);
$('#elevenlabs_tts_api_key').val(this.settings.apiKey);
$('#elevenlabs_tts_model').val(this.settings.model);
$('#eleven_labs_connect').on('click', () => { this.onConnectClick() })
$('#elevenlabs_tts_similarity_boost').on('input', this.onSettingsChange.bind(this))
$('#elevenlabs_tts_stability').on('input', this.onSettingsChange.bind(this))
$('#elevenlabs_tts_model').on('change', this.onSettingsChange.bind(this))
$('#eleven_labs_connect').on('click', () => { this.onConnectClick(); });
$('#elevenlabs_tts_similarity_boost').on('input', this.onSettingsChange.bind(this));
$('#elevenlabs_tts_stability').on('input', this.onSettingsChange.bind(this));
$('#elevenlabs_tts_model').on('change', this.onSettingsChange.bind(this));
$('#elevenlabs_tts_stability_output').text(this.settings.stability);
$('#elevenlabs_tts_similarity_boost_output').text(this.settings.similarity_boost);
try {
await this.checkReady()
console.debug('ElevenLabs: Settings loaded')
await this.checkReady();
console.debug('ElevenLabs: Settings loaded');
} catch {
console.debug('ElevenLabs: Settings loaded, but not ready')
console.debug('ElevenLabs: Settings loaded, but not ready');
}
}
// Perform a simple readiness check by trying to fetch voiceIds
async checkReady() {
await this.fetchTtsVoiceObjects()
await this.fetchTtsVoiceObjects();
}
async onRefreshClick() {
@ -103,21 +103,21 @@ class ElevenLabsTtsProvider {
async onConnectClick() {
// Update on Apply click
return await this.updateApiKey().catch((error) => {
toastr.error(`ElevenLabs: ${error}`)
})
toastr.error(`ElevenLabs: ${error}`);
});
}
async updateApiKey() {
// Using this call to validate API key
this.settings.apiKey = $('#elevenlabs_tts_api_key').val()
this.settings.apiKey = $('#elevenlabs_tts_api_key').val();
await this.fetchTtsVoiceObjects().catch(error => {
throw 'TTS API key validation failed'
})
console.debug(`Saved new API_KEY: ${this.settings.apiKey}`)
$('#tts_status').text('')
this.onSettingsChange()
throw 'TTS API key validation failed';
});
console.debug(`Saved new API_KEY: ${this.settings.apiKey}`);
$('#tts_status').text('');
this.onSettingsChange();
}
//#################//
@ -126,30 +126,30 @@ class ElevenLabsTtsProvider {
async getVoice(voiceName) {
if (this.voices.length == 0) {
this.voices = await this.fetchTtsVoiceObjects()
this.voices = await this.fetchTtsVoiceObjects();
}
const match = this.voices.filter(
elevenVoice => elevenVoice.name == voiceName
)[0]
)[0];
if (!match) {
throw `TTS Voice name ${voiceName} not found in ElevenLabs account`
throw `TTS Voice name ${voiceName} not found in ElevenLabs account`;
}
return match
return match;
}
async generateTts(text, voiceId) {
const historyId = await this.findTtsGenerationInHistory(text, voiceId)
const historyId = await this.findTtsGenerationInHistory(text, voiceId);
let response
let response;
if (historyId) {
console.debug(`Found existing TTS generation with id ${historyId}`)
response = await this.fetchTtsFromHistory(historyId)
console.debug(`Found existing TTS generation with id ${historyId}`);
response = await this.fetchTtsFromHistory(historyId);
} else {
console.debug('No existing TTS generation found, requesting new generation')
response = await this.fetchTtsGeneration(text, voiceId)
console.debug('No existing TTS generation found, requesting new generation');
response = await this.fetchTtsGeneration(text, voiceId);
}
return response
return response;
}
//###################//
@ -157,16 +157,16 @@ class ElevenLabsTtsProvider {
//###################//
async findTtsGenerationInHistory(message, voiceId) {
const ttsHistory = await this.fetchTtsHistory()
const ttsHistory = await this.fetchTtsHistory();
for (const history of ttsHistory) {
const text = history.text
const itemId = history.history_item_id
const text = history.text;
const itemId = history.history_item_id;
if (message === text && history.voice_id == voiceId) {
console.info(`Existing TTS history item ${itemId} found: ${text} `)
return itemId
console.info(`Existing TTS history item ${itemId} found: ${text} `);
return itemId;
}
}
return ''
return '';
}
@ -176,36 +176,36 @@ class ElevenLabsTtsProvider {
async fetchTtsVoiceObjects() {
const headers = {
'xi-api-key': this.settings.apiKey
}
};
const response = await fetch('https://api.elevenlabs.io/v1/voices', {
headers: headers
})
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`)
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
const responseJson = await response.json()
return responseJson.voices
const responseJson = await response.json();
return responseJson.voices;
}
async fetchTtsVoiceSettings() {
const headers = {
'xi-api-key': this.settings.apiKey
}
};
const response = await fetch(
'https://api.elevenlabs.io/v1/voices/settings/default',
{
headers: headers
}
)
);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`)
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
return response.json()
return response.json();
}
async fetchTtsGeneration(text, voiceId) {
let model = this.settings.model ?? 'eleven_monolingual_v1';
console.info(`Generating new TTS for voice_id ${voiceId}, model ${model}`)
console.info(`Generating new TTS for voice_id ${voiceId}, model ${model}`);
const response = await fetch(
`https://api.elevenlabs.io/v1/text-to-speech/${voiceId}`,
{
@ -223,16 +223,16 @@ class ElevenLabsTtsProvider {
},
})
}
)
);
if (!response.ok) {
toastr.error(response.statusText, 'TTS Generation Failed');
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
return response
return response;
}
async fetchTtsFromHistory(history_item_id) {
console.info(`Fetched existing TTS with history_item_id ${history_item_id}`)
console.info(`Fetched existing TTS with history_item_id ${history_item_id}`);
const response = await fetch(
`https://api.elevenlabs.io/v1/history/${history_item_id}/audio`,
{
@ -240,24 +240,24 @@ class ElevenLabsTtsProvider {
'xi-api-key': this.settings.apiKey
}
}
)
);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`)
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
return response
return response;
}
async fetchTtsHistory() {
const headers = {
'xi-api-key': this.settings.apiKey
}
};
const response = await fetch('https://api.elevenlabs.io/v1/history', {
headers: headers
})
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`)
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
const responseJson = await response.json()
return responseJson.history
const responseJson = await response.json();
return responseJson.history;
}
}

View File

@ -1,25 +1,25 @@
import { callPopup, cancelTtsPlay, eventSource, event_types, name2, saveSettingsDebounced } from '../../../script.js'
import { ModuleWorkerWrapper, doExtrasFetch, extension_settings, getApiUrl, getContext, modules } from '../../extensions.js'
import { escapeRegex, getStringHash } from '../../utils.js'
import { EdgeTtsProvider } from './edge.js'
import { ElevenLabsTtsProvider } from './elevenlabs.js'
import { SileroTtsProvider } from './silerotts.js'
import { CoquiTtsProvider } from './coqui.js'
import { SystemTtsProvider } from './system.js'
import { NovelTtsProvider } from './novel.js'
import { power_user } from '../../power-user.js'
import { registerSlashCommand } from '../../slash-commands.js'
import { OpenAITtsProvider } from './openai.js'
import {XTTSTtsProvider} from './xtts.js'
import { callPopup, cancelTtsPlay, eventSource, event_types, name2, saveSettingsDebounced } from '../../../script.js';
import { ModuleWorkerWrapper, doExtrasFetch, extension_settings, getApiUrl, getContext, modules } from '../../extensions.js';
import { escapeRegex, getStringHash } from '../../utils.js';
import { EdgeTtsProvider } from './edge.js';
import { ElevenLabsTtsProvider } from './elevenlabs.js';
import { SileroTtsProvider } from './silerotts.js';
import { CoquiTtsProvider } from './coqui.js';
import { SystemTtsProvider } from './system.js';
import { NovelTtsProvider } from './novel.js';
import { power_user } from '../../power-user.js';
import { registerSlashCommand } from '../../slash-commands.js';
import { OpenAITtsProvider } from './openai.js';
import {XTTSTtsProvider} from './xtts.js';
export { talkingAnimation };
const UPDATE_INTERVAL = 1000
const UPDATE_INTERVAL = 1000;
let voiceMapEntries = []
let voiceMap = {} // {charName:voiceid, charName2:voiceid2}
let voiceMapEntries = [];
let voiceMap = {}; // {charName:voiceid, charName2:voiceid2}
let storedvalue = false;
let lastChatId = null
let lastMessageHash = null
let lastChatId = null;
let lastMessageHash = null;
const DEFAULT_VOICE_MARKER = '[Default Voice]';
const DISABLED_VOICE_MARKER = 'disabled';
@ -59,8 +59,8 @@ export function getPreviewString(lang) {
'vi-VN': 'Cô bé quàng khăn đỏ đang ngồi trên bãi cỏ xanh',
'ar-SA': 'أَبْجَدِيَّة عَرَبِيَّة',
'hi-IN': 'श्वेता ने श्वेता के श्वेते हाथों में श्वेता का श्वेता चावल पकड़ा',
}
const fallbackPreview = 'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet'
};
const fallbackPreview = 'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet';
return previewStrings[lang] ?? fallbackPreview;
}
@ -74,9 +74,9 @@ let ttsProviders = {
Edge: EdgeTtsProvider,
Novel: NovelTtsProvider,
OpenAI: OpenAITtsProvider,
}
let ttsProvider
let ttsProviderName
};
let ttsProvider;
let ttsProviderName;
let ttsLastMessage = null;
@ -90,7 +90,7 @@ async function onNarrateOneMessage() {
return;
}
resetTtsPlayback()
resetTtsPlayback();
ttsJobQueue.push(message);
moduleWorker();
}
@ -117,7 +117,7 @@ async function onNarrateText(args, text) {
return;
}
resetTtsPlayback()
resetTtsPlayback();
ttsJobQueue.push({ mes: text, name: name });
await moduleWorker();
@ -127,42 +127,42 @@ async function onNarrateText(args, text) {
async function moduleWorker() {
// Primarily determining when to add new chat to the TTS queue
const enabled = $('#tts_enabled').is(':checked')
const enabled = $('#tts_enabled').is(':checked');
$('body').toggleClass('tts', enabled);
if (!enabled) {
return
return;
}
const context = getContext()
const chat = context.chat
const context = getContext();
const chat = context.chat;
processTtsQueue()
processAudioJobQueue()
updateUiAudioPlayState()
processTtsQueue();
processAudioJobQueue();
updateUiAudioPlayState();
// Auto generation is disabled
if (extension_settings.tts.auto_generation == false) {
return
return;
}
// no characters or group selected
if (!context.groupId && context.characterId === undefined) {
return
return;
}
// Chat changed
if (
context.chatId !== lastChatId
) {
currentMessageNumber = context.chat.length ? context.chat.length : 0
saveLastValues()
currentMessageNumber = context.chat.length ? context.chat.length : 0;
saveLastValues();
// Force to speak on the first message in the new chat
if (context.chat.length === 1) {
lastMessageHash = -1;
}
return
return;
}
// take the count of messages
@ -216,13 +216,13 @@ async function moduleWorker() {
}
// New messages, add new chat to history
lastMessageHash = hashNew
currentMessageNumber = lastMessageNumber
lastMessageHash = hashNew;
currentMessageNumber = lastMessageNumber;
console.debug(
`Adding message from ${message.name} for TTS processing: "${message.mes}"`
)
ttsJobQueue.push(message)
);
ttsJobQueue.push(message);
}
function talkingAnimation(switchValue) {
@ -243,7 +243,7 @@ function talkingAnimation(switchValue) {
// Handle the error here or simply ignore it to prevent logging
}
}
updateUiAudioPlayState()
updateUiAudioPlayState();
}
function resetTtsPlayback() {
@ -267,17 +267,17 @@ function resetTtsPlayback() {
}
function isTtsProcessing() {
let processing = false
let processing = false;
// Check job queues
if (ttsJobQueue.length > 0 || audioJobQueue.length > 0) {
processing = true
processing = true;
}
// Check current jobs
if (currentTtsJob != null || currentAudioJob != null) {
processing = true
processing = true;
}
return processing
return processing;
}
function debugTtsPlayback() {
@ -294,57 +294,57 @@ function debugTtsPlayback() {
'currentTtsJob': currentTtsJob,
'ttsConfig': extension_settings.tts
}
))
));
}
window.debugTtsPlayback = debugTtsPlayback
window.debugTtsPlayback = debugTtsPlayback;
//##################//
// Audio Control //
//##################//
let audioElement = new Audio()
audioElement.id = 'tts_audio'
audioElement.autoplay = true
let audioElement = new Audio();
audioElement.id = 'tts_audio';
audioElement.autoplay = true;
let audioJobQueue = []
let currentAudioJob
let audioPaused = false
let audioQueueProcessorReady = true
let audioJobQueue = [];
let currentAudioJob;
let audioPaused = false;
let audioQueueProcessorReady = true;
async function playAudioData(audioBlob) {
// Since current audio job can be cancelled, don't playback if it is null
if (currentAudioJob == null) {
console.log('Cancelled TTS playback because currentAudioJob was null')
console.log('Cancelled TTS playback because currentAudioJob was null');
}
const reader = new FileReader()
const reader = new FileReader();
reader.onload = function (e) {
const srcUrl = e.target.result
audioElement.src = srcUrl
}
reader.readAsDataURL(audioBlob)
audioElement.addEventListener('ended', completeCurrentAudioJob)
const srcUrl = e.target.result;
audioElement.src = srcUrl;
};
reader.readAsDataURL(audioBlob);
audioElement.addEventListener('ended', completeCurrentAudioJob);
audioElement.addEventListener('canplay', () => {
console.debug('Starting TTS playback')
audioElement.play()
})
console.debug('Starting TTS playback');
audioElement.play();
});
}
window['tts_preview'] = function (id) {
const audio = document.getElementById(id)
const audio = document.getElementById(id);
if (audio && !$(audio).data('disabled')) {
audio.play()
audio.play();
}
else {
ttsProvider.previewTtsVoice(id)
ttsProvider.previewTtsVoice(id);
}
}
};
async function onTtsVoicesClick() {
let popupText = ''
let popupText = '';
try {
const voiceIds = await ttsProvider.fetchTtsVoiceObjects()
const voiceIds = await ttsProvider.fetchTtsVoiceObjects();
for (const voice of voiceIds) {
popupText += `
@ -352,27 +352,27 @@ async function onTtsVoicesClick() {
<span class="voice_lang">${voice.lang || ''}</span>
<b class="voice_name">${voice.name}</b>
<i onclick="tts_preview('${voice.voice_id}')" class="fa-solid fa-play"></i>
</div>`
</div>`;
if (voice.preview_url) {
popupText += `<audio id="${voice.voice_id}" src="${voice.preview_url}" data-disabled="${voice.preview_url == false}"></audio>`
popupText += `<audio id="${voice.voice_id}" src="${voice.preview_url}" data-disabled="${voice.preview_url == false}"></audio>`;
}
}
} catch {
popupText = 'Could not load voices list. Check your API key.'
popupText = 'Could not load voices list. Check your API key.';
}
callPopup(popupText, 'text')
callPopup(popupText, 'text');
}
function updateUiAudioPlayState() {
if (extension_settings.tts.enabled == true) {
$('#ttsExtensionMenuItem').show();
let img
let img;
// Give user feedback that TTS is active by setting the stop icon if processing or playing
if (!audioElement.paused || isTtsProcessing()) {
img = 'fa-solid fa-stop-circle extensionsMenuExtensionButton'
img = 'fa-solid fa-stop-circle extensionsMenuExtensionButton';
} else {
img = 'fa-solid fa-circle-play extensionsMenuExtensionButton'
img = 'fa-solid fa-circle-play extensionsMenuExtensionButton';
}
$('#tts_media_control').attr('class', img);
} else {
@ -382,16 +382,16 @@ function updateUiAudioPlayState() {
function onAudioControlClicked() {
audioElement.src = '/sounds/silence.mp3';
let context = getContext()
let context = getContext();
// Not pausing, doing a full stop to anything TTS is doing. Better UX as pause is not as useful
if (!audioElement.paused || isTtsProcessing()) {
resetTtsPlayback()
resetTtsPlayback();
talkingAnimation(false);
} else {
// Default play behavior if not processing or playing is to play the last message.
ttsJobQueue.push(context.chat[context.chat.length - 1])
ttsJobQueue.push(context.chat[context.chat.length - 1]);
}
updateUiAudioPlayState()
updateUiAudioPlayState();
}
function addAudioControl() {
@ -400,15 +400,15 @@ function addAudioControl() {
<div id="ttsExtensionMenuItem" class="list-group-item flex-container flexGap5">
<div id="tts_media_control" class="extensionsMenuExtensionButton "/></div>
TTS Playback
</div>`)
$('#ttsExtensionMenuItem').attr('title', 'TTS play/pause').on('click', onAudioControlClicked)
updateUiAudioPlayState()
</div>`);
$('#ttsExtensionMenuItem').attr('title', 'TTS play/pause').on('click', onAudioControlClicked);
updateUiAudioPlayState();
}
function completeCurrentAudioJob() {
audioQueueProcessorReady = true
currentAudioJob = null
talkingAnimation(false) //stop lip animation
audioQueueProcessorReady = true;
currentAudioJob = null;
talkingAnimation(false); //stop lip animation
// updateUiPlayState();
}
@ -417,27 +417,27 @@ function completeCurrentAudioJob() {
* @param {Response} response
*/
async function addAudioJob(response) {
const audioData = await response.blob()
const audioData = await response.blob();
if (!audioData.type.startsWith('audio/')) {
throw `TTS received HTTP response with invalid data format. Expecting audio/*, got ${audioData.type}`
throw `TTS received HTTP response with invalid data format. Expecting audio/*, got ${audioData.type}`;
}
audioJobQueue.push(audioData)
console.debug('Pushed audio job to queue.')
audioJobQueue.push(audioData);
console.debug('Pushed audio job to queue.');
}
async function processAudioJobQueue() {
// Nothing to do, audio not completed, or audio paused - stop processing.
if (audioJobQueue.length == 0 || !audioQueueProcessorReady || audioPaused) {
return
return;
}
try {
audioQueueProcessorReady = false
currentAudioJob = audioJobQueue.pop()
playAudioData(currentAudioJob)
talkingAnimation(true)
audioQueueProcessorReady = false;
currentAudioJob = audioJobQueue.pop();
playAudioData(currentAudioJob);
talkingAnimation(true);
} catch (error) {
console.error(error)
audioQueueProcessorReady = true
console.error(error);
audioQueueProcessorReady = true;
}
}
@ -445,46 +445,46 @@ async function processAudioJobQueue() {
// TTS Control //
//################//
let ttsJobQueue = []
let currentTtsJob // Null if nothing is currently being processed
let currentMessageNumber = 0
let ttsJobQueue = [];
let currentTtsJob; // Null if nothing is currently being processed
let currentMessageNumber = 0;
function completeTtsJob() {
console.info(`Current TTS job for ${currentTtsJob?.name} completed.`)
currentTtsJob = null
console.info(`Current TTS job for ${currentTtsJob?.name} completed.`);
currentTtsJob = null;
}
function saveLastValues() {
const context = getContext()
lastChatId = context.chatId
const context = getContext();
lastChatId = context.chatId;
lastMessageHash = getStringHash(
(context.chat.length && context.chat[context.chat.length - 1].mes) ?? ''
)
);
}
async function tts(text, voiceId, char) {
let response = await ttsProvider.generateTts(text, voiceId)
let response = await ttsProvider.generateTts(text, voiceId);
// RVC injection
if (extension_settings.rvc.enabled && typeof window['rvcVoiceConversion'] === 'function')
response = await window['rvcVoiceConversion'](response, char, text)
response = await window['rvcVoiceConversion'](response, char, text);
addAudioJob(response)
completeTtsJob()
addAudioJob(response);
completeTtsJob();
}
async function processTtsQueue() {
// Called each moduleWorker iteration to pull chat messages from queue
if (currentTtsJob || ttsJobQueue.length <= 0 || audioPaused) {
return
return;
}
console.debug('New message found, running TTS')
currentTtsJob = ttsJobQueue.shift()
let text = extension_settings.tts.narrate_translated_only ? (currentTtsJob?.extra?.display_text || currentTtsJob.mes) : currentTtsJob.mes
console.debug('New message found, running TTS');
currentTtsJob = ttsJobQueue.shift();
let text = extension_settings.tts.narrate_translated_only ? (currentTtsJob?.extra?.display_text || currentTtsJob.mes) : currentTtsJob.mes;
text = extension_settings.tts.narrate_dialogues_only
? text.replace(/\*[^*]*?(\*|$)/g, '').trim() // remove asterisks content
: text.replaceAll('*', '').trim() // remove just the asterisks
: text.replaceAll('*', '').trim(); // remove just the asterisks
if (extension_settings.tts.narrate_quoted_only) {
const special_quotes = /[“”]/g; // Extend this regex to include other special quotes
@ -501,8 +501,8 @@ async function processTtsQueue() {
// Collapse newlines and spaces into single space
text = text.replace(/\s+/g, ' ').trim();
console.log(`TTS: ${text}`)
const char = currentTtsJob.name
console.log(`TTS: ${text}`);
const char = currentTtsJob.name;
// Remove character name from start of the line if power user setting is disabled
if (char && !power_user.allow_name2_display) {
@ -513,35 +513,35 @@ async function processTtsQueue() {
try {
if (!text) {
console.warn('Got empty text in TTS queue job.');
completeTtsJob()
completeTtsJob();
return;
}
const voiceMapEntry = voiceMap[char] === DEFAULT_VOICE_MARKER ? voiceMap[DEFAULT_VOICE_MARKER] : voiceMap[char]
const voiceMapEntry = voiceMap[char] === DEFAULT_VOICE_MARKER ? voiceMap[DEFAULT_VOICE_MARKER] : voiceMap[char];
if (!voiceMapEntry || voiceMapEntry === DISABLED_VOICE_MARKER) {
throw `${char} not in voicemap. Configure character in extension settings voice map`
throw `${char} not in voicemap. Configure character in extension settings voice map`;
}
const voice = await ttsProvider.getVoice(voiceMapEntry)
const voiceId = voice.voice_id
const voice = await ttsProvider.getVoice(voiceMapEntry);
const voiceId = voice.voice_id;
if (voiceId == null) {
toastr.error(`Specified voice for ${char} was not found. Check the TTS extension settings.`)
throw `Unable to attain voiceId for ${char}`
toastr.error(`Specified voice for ${char} was not found. Check the TTS extension settings.`);
throw `Unable to attain voiceId for ${char}`;
}
tts(text, voiceId, char)
tts(text, voiceId, char);
} catch (error) {
console.error(error)
currentTtsJob = null
console.error(error);
currentTtsJob = null;
}
}
// Secret function for now
async function playFullConversation() {
const context = getContext()
const chat = context.chat
ttsJobQueue = chat
const context = getContext();
const chat = context.chat;
ttsJobQueue = chat;
}
window.playFullConversation = playFullConversation
window.playFullConversation = playFullConversation;
//#############################//
// Extension UI and Settings //
@ -549,21 +549,21 @@ window.playFullConversation = playFullConversation
function loadSettings() {
if (Object.keys(extension_settings.tts).length === 0) {
Object.assign(extension_settings.tts, defaultSettings)
Object.assign(extension_settings.tts, defaultSettings);
}
for (const key in defaultSettings) {
if (!(key in extension_settings.tts)) {
extension_settings.tts[key] = defaultSettings[key]
extension_settings.tts[key] = defaultSettings[key];
}
}
$('#tts_provider').val(extension_settings.tts.currentProvider)
$('#tts_provider').val(extension_settings.tts.currentProvider);
$('#tts_enabled').prop(
'checked',
extension_settings.tts.enabled
)
$('#tts_narrate_dialogues').prop('checked', extension_settings.tts.narrate_dialogues_only)
$('#tts_narrate_quoted').prop('checked', extension_settings.tts.narrate_quoted_only)
$('#tts_auto_generation').prop('checked', extension_settings.tts.auto_generation)
);
$('#tts_narrate_dialogues').prop('checked', extension_settings.tts.narrate_dialogues_only);
$('#tts_narrate_quoted').prop('checked', extension_settings.tts.narrate_quoted_only);
$('#tts_auto_generation').prop('checked', extension_settings.tts.auto_generation);
$('#tts_narrate_translated_only').prop('checked', extension_settings.tts.narrate_translated_only);
$('#tts_narrate_user').prop('checked', extension_settings.tts.narrate_user);
$('body').toggleClass('tts', extension_settings.tts.enabled);
@ -575,14 +575,14 @@ const defaultSettings = {
currentProvider: 'ElevenLabs',
auto_generation: true,
narrate_user: false,
}
};
function setTtsStatus(status, success) {
$('#tts_status').text(status)
$('#tts_status').text(status);
if (success) {
$('#tts_status').removeAttr('style')
$('#tts_status').removeAttr('style');
} else {
$('#tts_status').css('color', 'red')
$('#tts_status').css('color', 'red');
}
}
@ -591,36 +591,36 @@ function onRefreshClick() {
ttsProvider.onRefreshClick(),
// updateVoiceMap()
]).then(() => {
extension_settings.tts[ttsProviderName] = ttsProvider.settings
saveSettingsDebounced()
setTtsStatus('Successfully applied settings', true)
console.info(`Saved settings ${ttsProviderName} ${JSON.stringify(ttsProvider.settings)}`)
initVoiceMap()
updateVoiceMap()
extension_settings.tts[ttsProviderName] = ttsProvider.settings;
saveSettingsDebounced();
setTtsStatus('Successfully applied settings', true);
console.info(`Saved settings ${ttsProviderName} ${JSON.stringify(ttsProvider.settings)}`);
initVoiceMap();
updateVoiceMap();
}).catch(error => {
console.error(error)
setTtsStatus(error, false)
})
console.error(error);
setTtsStatus(error, false);
});
}
function onEnableClick() {
extension_settings.tts.enabled = $('#tts_enabled').is(
':checked'
)
updateUiAudioPlayState()
saveSettingsDebounced()
);
updateUiAudioPlayState();
saveSettingsDebounced();
}
function onAutoGenerationClick() {
extension_settings.tts.auto_generation = !!$('#tts_auto_generation').prop('checked');
saveSettingsDebounced()
saveSettingsDebounced();
}
function onNarrateDialoguesClick() {
extension_settings.tts.narrate_dialogues_only = !!$('#tts_narrate_dialogues').prop('checked');
saveSettingsDebounced()
saveSettingsDebounced();
}
function onNarrateUserClick() {
@ -630,7 +630,7 @@ function onNarrateUserClick() {
function onNarrateQuotedClick() {
extension_settings.tts.narrate_quoted_only = !!$('#tts_narrate_quoted').prop('checked');
saveSettingsDebounced()
saveSettingsDebounced();
}
@ -645,39 +645,39 @@ function onNarrateTranslatedOnlyClick() {
async function loadTtsProvider(provider) {
//Clear the current config and add new config
$('#tts_provider_settings').html('')
$('#tts_provider_settings').html('');
if (!provider) {
return
return;
}
// Init provider references
extension_settings.tts.currentProvider = provider
ttsProviderName = provider
ttsProvider = new ttsProviders[provider]
extension_settings.tts.currentProvider = provider;
ttsProviderName = provider;
ttsProvider = new ttsProviders[provider];
// Init provider settings
$('#tts_provider_settings').append(ttsProvider.settingsHtml)
$('#tts_provider_settings').append(ttsProvider.settingsHtml);
if (!(ttsProviderName in extension_settings.tts)) {
console.warn(`Provider ${ttsProviderName} not in Extension Settings, initiatilizing provider in settings`)
extension_settings.tts[ttsProviderName] = {}
console.warn(`Provider ${ttsProviderName} not in Extension Settings, initiatilizing provider in settings`);
extension_settings.tts[ttsProviderName] = {};
}
await ttsProvider.loadSettings(extension_settings.tts[ttsProviderName])
await initVoiceMap()
await ttsProvider.loadSettings(extension_settings.tts[ttsProviderName]);
await initVoiceMap();
}
function onTtsProviderChange() {
const ttsProviderSelection = $('#tts_provider').val()
extension_settings.tts.currentProvider = ttsProviderSelection
loadTtsProvider(ttsProviderSelection)
const ttsProviderSelection = $('#tts_provider').val();
extension_settings.tts.currentProvider = ttsProviderSelection;
loadTtsProvider(ttsProviderSelection);
}
// Ensure that TTS provider settings are saved to extension settings.
export function saveTtsProviderSettings() {
extension_settings.tts[ttsProviderName] = ttsProvider.settings
updateVoiceMap()
saveSettingsDebounced()
console.info(`Saved settings ${ttsProviderName} ${JSON.stringify(ttsProvider.settings)}`)
extension_settings.tts[ttsProviderName] = ttsProvider.settings;
updateVoiceMap();
saveSettingsDebounced();
console.info(`Saved settings ${ttsProviderName} ${JSON.stringify(ttsProvider.settings)}`);
}
@ -686,28 +686,28 @@ export function saveTtsProviderSettings() {
//###################//
async function onChatChanged() {
await resetTtsPlayback()
await initVoiceMap()
ttsLastMessage = null
await resetTtsPlayback();
await initVoiceMap();
ttsLastMessage = null;
}
async function onChatDeleted() {
const context = getContext()
const context = getContext();
// update internal references to new last message
lastChatId = context.chatId
currentMessageNumber = context.chat.length ? context.chat.length : 0
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) ?? '')
let messageHash = getStringHash((context.chat.length && context.chat[context.chat.length - 1].mes) ?? '');
if (messageHash === lastMessageHash) {
return
return;
}
lastMessageHash = messageHash
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()
await resetTtsPlayback();
}
/**
@ -716,7 +716,7 @@ async function onChatDeleted() {
* @returns {string[]} - Array of character names
*/
function getCharacters(unrestricted) {
const context = getContext()
const context = getContext();
if (unrestricted) {
const names = context.characters.map(char => char.name);
@ -724,26 +724,26 @@ function getCharacters(unrestricted) {
return names;
}
let characters = []
let characters = [];
if (context.groupId === null) {
// Single char chat
characters.push(DEFAULT_VOICE_MARKER)
characters.push(context.name1)
characters.push(context.name2)
characters.push(DEFAULT_VOICE_MARKER);
characters.push(context.name1);
characters.push(context.name2);
} else {
// Group chat
characters.push(DEFAULT_VOICE_MARKER)
characters.push(context.name1)
const group = context.groups.find(group => context.groupId == group.id)
characters.push(DEFAULT_VOICE_MARKER);
characters.push(context.name1);
const group = context.groups.find(group => context.groupId == group.id);
for (let member of group.members) {
// Remove suffix
if (member.endsWith('.png')) {
member = member.slice(0, -4)
member = member.slice(0, -4);
}
characters.push(member)
characters.push(member);
}
}
return characters
return characters;
}
function sanitizeId(input) {
@ -759,15 +759,15 @@ function sanitizeId(input) {
}
function parseVoiceMap(voiceMapString) {
let parsedVoiceMap = {}
let parsedVoiceMap = {};
for (const [charName, voiceId] of voiceMapString
.split(',')
.map(s => s.split(':'))) {
if (charName && voiceId) {
parsedVoiceMap[charName.trim()] = voiceId.trim()
parsedVoiceMap[charName.trim()] = voiceId.trim();
}
}
return parsedVoiceMap
return parsedVoiceMap;
}
@ -776,39 +776,39 @@ function parseVoiceMap(voiceMapString) {
* Apply voiceMap based on current voiceMapEntries
*/
function updateVoiceMap() {
const tempVoiceMap = {}
const tempVoiceMap = {};
for (const voice of voiceMapEntries) {
if (voice.voiceId === null) {
continue
continue;
}
tempVoiceMap[voice.name] = voice.voiceId
tempVoiceMap[voice.name] = voice.voiceId;
}
if (Object.keys(tempVoiceMap).length !== 0) {
voiceMap = tempVoiceMap
console.log(`Voicemap updated to ${JSON.stringify(voiceMap)}`)
voiceMap = tempVoiceMap;
console.log(`Voicemap updated to ${JSON.stringify(voiceMap)}`);
}
if (!extension_settings.tts[ttsProviderName].voiceMap) {
extension_settings.tts[ttsProviderName].voiceMap = {}
extension_settings.tts[ttsProviderName].voiceMap = {};
}
Object.assign(extension_settings.tts[ttsProviderName].voiceMap, voiceMap)
saveSettingsDebounced()
Object.assign(extension_settings.tts[ttsProviderName].voiceMap, voiceMap);
saveSettingsDebounced();
}
class VoiceMapEntry {
name
voiceId
selectElement
name;
voiceId;
selectElement;
constructor(name, voiceId = DEFAULT_VOICE_MARKER) {
this.name = name
this.voiceId = voiceId
this.selectElement = null
this.name = name;
this.voiceId = voiceId;
this.selectElement = null;
}
addUI(voiceIds) {
let sanitizedName = sanitizeId(this.name)
let sanitizedName = sanitizeId(this.name);
let defaultOption = this.name === DEFAULT_VOICE_MARKER ?
`<option>${DISABLED_VOICE_MARKER}</option>` :
`<option>${DEFAULT_VOICE_MARKER}</option><option>${DISABLED_VOICE_MARKER}</option>`
`<option>${DEFAULT_VOICE_MARKER}</option><option>${DISABLED_VOICE_MARKER}</option>`;
let template = `
<div class='tts_voicemap_block_char flex-container flexGap5'>
<span id='tts_voicemap_char_${sanitizedName}'>${this.name}</span>
@ -816,25 +816,25 @@ class VoiceMapEntry {
${defaultOption}
</select>
</div>
`
$('#tts_voicemap_block').append(template)
`;
$('#tts_voicemap_block').append(template);
// Populate voice ID select list
for (const voiceId of voiceIds) {
const option = document.createElement('option');
option.innerText = voiceId.name;
option.value = voiceId.name;
$(`#tts_voicemap_char_${sanitizedName}_voice`).append(option)
$(`#tts_voicemap_char_${sanitizedName}_voice`).append(option);
}
this.selectElement = $(`#tts_voicemap_char_${sanitizedName}_voice`)
this.selectElement.on('change', args => this.onSelectChange(args))
this.selectElement.val(this.voiceId)
this.selectElement = $(`#tts_voicemap_char_${sanitizedName}_voice`);
this.selectElement.on('change', args => this.onSelectChange(args));
this.selectElement.val(this.voiceId);
}
onSelectChange(args) {
this.voiceId = this.selectElement.find(':selected').val()
updateVoiceMap()
this.voiceId = this.selectElement.find(':selected').val();
updateVoiceMap();
}
}
@ -844,69 +844,69 @@ class VoiceMapEntry {
*/
export async function initVoiceMap(unrestricted = false) {
// Gate initialization if not enabled or TTS Provider not ready. Prevents error popups.
const enabled = $('#tts_enabled').is(':checked')
const enabled = $('#tts_enabled').is(':checked');
if (!enabled) {
return
return;
}
// Keep errors inside extension UI rather than toastr. Toastr errors for TTS are annoying.
try {
await ttsProvider.checkReady()
await ttsProvider.checkReady();
} catch (error) {
const message = `TTS Provider not ready. ${error}`
setTtsStatus(message, false)
return
const message = `TTS Provider not ready. ${error}`;
setTtsStatus(message, false);
return;
}
setTtsStatus('TTS Provider Loaded', true)
setTtsStatus('TTS Provider Loaded', true);
// Clear existing voiceMap state
$('#tts_voicemap_block').empty()
voiceMapEntries = []
$('#tts_voicemap_block').empty();
voiceMapEntries = [];
// Get characters in current chat
const characters = getCharacters(unrestricted);
// Get saved voicemap from provider settings, handling new and old representations
let voiceMapFromSettings = {}
let voiceMapFromSettings = {};
if ('voiceMap' in extension_settings.tts[ttsProviderName]) {
// Handle previous representation
if (typeof extension_settings.tts[ttsProviderName].voiceMap === 'string') {
voiceMapFromSettings = parseVoiceMap(extension_settings.tts[ttsProviderName].voiceMap)
voiceMapFromSettings = parseVoiceMap(extension_settings.tts[ttsProviderName].voiceMap);
// Handle new representation
} else if (typeof extension_settings.tts[ttsProviderName].voiceMap === 'object') {
voiceMapFromSettings = extension_settings.tts[ttsProviderName].voiceMap
voiceMapFromSettings = extension_settings.tts[ttsProviderName].voiceMap;
}
}
// Get voiceIds from provider
let voiceIdsFromProvider
let voiceIdsFromProvider;
try {
voiceIdsFromProvider = await ttsProvider.fetchTtsVoiceObjects()
voiceIdsFromProvider = await ttsProvider.fetchTtsVoiceObjects();
}
catch {
toastr.error('TTS Provider failed to return voice ids.')
toastr.error('TTS Provider failed to return voice ids.');
}
// Build UI using VoiceMapEntry objects
for (const character of characters) {
if (character === 'SillyTavern System') {
continue
continue;
}
// Check provider settings for voiceIds
let voiceId
let voiceId;
if (character in voiceMapFromSettings) {
voiceId = voiceMapFromSettings[character]
voiceId = voiceMapFromSettings[character];
} else if (character === DEFAULT_VOICE_MARKER) {
voiceId = DISABLED_VOICE_MARKER
voiceId = DISABLED_VOICE_MARKER;
} else {
voiceId = DEFAULT_VOICE_MARKER
voiceId = DEFAULT_VOICE_MARKER;
}
const voiceMapEntry = new VoiceMapEntry(character, voiceId)
voiceMapEntry.addUI(voiceIdsFromProvider)
voiceMapEntries.push(voiceMapEntry)
const voiceMapEntry = new VoiceMapEntry(character, voiceId);
voiceMapEntry.addUI(voiceIdsFromProvider);
voiceMapEntries.push(voiceMapEntry);
}
updateVoiceMap()
updateVoiceMap();
}
$(document).ready(function () {
@ -965,32 +965,32 @@ $(document).ready(function () {
</div>
</div>
</div>
`
$('#extensions_settings').append(settingsHtml)
$('#tts_refresh').on('click', onRefreshClick)
$('#tts_enabled').on('click', onEnableClick)
`;
$('#extensions_settings').append(settingsHtml);
$('#tts_refresh').on('click', onRefreshClick);
$('#tts_enabled').on('click', onEnableClick);
$('#tts_narrate_dialogues').on('click', onNarrateDialoguesClick);
$('#tts_narrate_quoted').on('click', onNarrateQuotedClick);
$('#tts_narrate_translated_only').on('click', onNarrateTranslatedOnlyClick);
$('#tts_auto_generation').on('click', onAutoGenerationClick);
$('#tts_narrate_user').on('click', onNarrateUserClick);
$('#tts_voices').on('click', onTtsVoicesClick)
$('#tts_voices').on('click', onTtsVoicesClick);
for (const provider in ttsProviders) {
$('#tts_provider').append($('<option />').val(provider).text(provider))
$('#tts_provider').append($('<option />').val(provider).text(provider));
}
$('#tts_provider').on('change', onTtsProviderChange)
$('#tts_provider').on('change', onTtsProviderChange);
$(document).on('click', '.mes_narrate', onNarrateOneMessage);
}
addExtensionControls() // No init dependencies
loadSettings() // Depends on Extension Controls and loadTtsProvider
loadTtsProvider(extension_settings.tts.currentProvider) // No dependencies
addAudioControl() // Depends on Extension Controls
addExtensionControls(); // No init dependencies
loadSettings(); // Depends on Extension Controls and loadTtsProvider
loadTtsProvider(extension_settings.tts.currentProvider); // No dependencies
addAudioControl(); // Depends on Extension Controls
const wrapper = new ModuleWorkerWrapper(moduleWorker);
setInterval(wrapper.update.bind(wrapper), UPDATE_INTERVAL) // Init depends on all the things
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.CHAT_CHANGED, onChatChanged);
eventSource.on(event_types.MESSAGE_DELETED, onChatDeleted);
eventSource.on(event_types.GROUP_UPDATED, onChatChanged)
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);
document.body.appendChild(audioElement);
})
});

View File

@ -1,23 +1,23 @@
import { getRequestHeaders, callPopup } from '../../../script.js'
import { getPreviewString, saveTtsProviderSettings } from './index.js'
import { initVoiceMap } from './index.js'
import { getRequestHeaders, callPopup } from '../../../script.js';
import { getPreviewString, saveTtsProviderSettings } from './index.js';
import { initVoiceMap } from './index.js';
export { NovelTtsProvider }
export { NovelTtsProvider };
class NovelTtsProvider {
//########//
// Config //
//########//
settings
voices = []
separator = ' . '
audioElement = document.createElement('audio')
settings;
voices = [];
separator = ' . ';
audioElement = document.createElement('audio');
defaultSettings = {
voiceMap: {},
customVoices: []
}
};
/**
* Perform any text processing before passing to TTS engine.
@ -53,11 +53,11 @@ class NovelTtsProvider {
// Add a new Novel custom voice to provider
async addCustomVoice(){
const voiceName = await callPopup('<h3>Custom Voice name:</h3>', 'input')
this.settings.customVoices.push(voiceName)
this.populateCustomVoices()
initVoiceMap() // Update TTS extension voiceMap
saveTtsProviderSettings()
const voiceName = await callPopup('<h3>Custom Voice name:</h3>', 'input');
this.settings.customVoices.push(voiceName);
this.populateCustomVoices();
initVoiceMap(); // Update TTS extension voiceMap
saveTtsProviderSettings();
}
// Delete selected custom voice from provider
@ -68,52 +68,52 @@ class NovelTtsProvider {
if (voiceIndex !== -1) {
this.settings.customVoices.splice(voiceIndex, 1);
}
this.populateCustomVoices()
initVoiceMap() // Update TTS extension voiceMap
saveTtsProviderSettings()
this.populateCustomVoices();
initVoiceMap(); // Update TTS extension voiceMap
saveTtsProviderSettings();
}
// Create the UI dropdown list of voices in provider
populateCustomVoices(){
let voiceSelect = $('#tts-novel-custom-voices-select')
voiceSelect.empty()
let voiceSelect = $('#tts-novel-custom-voices-select');
voiceSelect.empty();
this.settings.customVoices.forEach(voice => {
voiceSelect.append(`<option>${voice}</option>`)
})
voiceSelect.append(`<option>${voice}</option>`);
});
}
async loadSettings(settings) {
// Populate Provider UI given input settings
if (Object.keys(settings).length == 0) {
console.info('Using default TTS Provider settings')
console.info('Using default TTS Provider settings');
}
$('#tts-novel-custom-voices-add').on('click', () => (this.addCustomVoice()))
$('#tts-novel-custom-voices-delete').on('click',() => (this.deleteCustomVoice()))
$('#tts-novel-custom-voices-add').on('click', () => (this.addCustomVoice()));
$('#tts-novel-custom-voices-delete').on('click',() => (this.deleteCustomVoice()));
// Only accept keys defined in defaultSettings
this.settings = this.defaultSettings
this.settings = this.defaultSettings;
for (const key in settings) {
if (key in this.settings) {
this.settings[key] = settings[key]
this.settings[key] = settings[key];
} else {
throw `Invalid setting passed to TTS Provider: ${key}`
throw `Invalid setting passed to TTS Provider: ${key}`;
}
}
this.populateCustomVoices()
await this.checkReady()
console.debug('NovelTTS: Settings loaded')
this.populateCustomVoices();
await this.checkReady();
console.debug('NovelTTS: Settings loaded');
}
// Perform a simple readiness check by trying to fetch voiceIds
// Doesnt really do much for Novel, not seeing a good way to test this at the moment.
async checkReady(){
await this.fetchTtsVoiceObjects()
await this.fetchTtsVoiceObjects();
}
async onRefreshClick() {
return
return;
}
//#################//
@ -122,15 +122,15 @@ class NovelTtsProvider {
async getVoice(voiceName) {
if (!voiceName) {
throw 'TTS Voice name not provided'
throw 'TTS Voice name not provided';
}
return { name: voiceName, voice_id: voiceName, lang: 'en-US', preview_url: false}
return { name: voiceName, voice_id: voiceName, lang: 'en-US', preview_url: false};
}
async generateTts(text, voiceId) {
const response = await this.fetchTtsGeneration(text, voiceId)
return response
const response = await this.fetchTtsGeneration(text, voiceId);
return response;
}
//###########//
@ -156,8 +156,8 @@ class NovelTtsProvider {
// Add in custom voices to the map
let addVoices = this.settings.customVoices.map(voice =>
({ name: voice, voice_id: voice, lang: 'en-US', preview_url: false })
)
voices = voices.concat(addVoices)
);
voices = voices.concat(addVoices);
return voices;
}
@ -167,10 +167,10 @@ class NovelTtsProvider {
this.audioElement.pause();
this.audioElement.currentTime = 0;
const text = getPreviewString('en-US')
const response = await this.fetchTtsGeneration(text, id)
const text = getPreviewString('en-US');
const response = await this.fetchTtsGeneration(text, id);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`)
throw new Error(`HTTP ${response.status}`);
}
const audio = await response.blob();
@ -180,7 +180,7 @@ class NovelTtsProvider {
}
async fetchTtsGeneration(inputText, voiceId) {
console.info(`Generating new TTS for voice_id ${voiceId}`)
console.info(`Generating new TTS for voice_id ${voiceId}`);
const response = await fetch('/api/novelai/generate-voice',
{
method: 'POST',
@ -190,11 +190,11 @@ class NovelTtsProvider {
'voice': voiceId,
})
}
)
);
if (!response.ok) {
toastr.error(response.statusText, 'TTS Generation Failed');
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
return response
return response;
}
}

View File

@ -1,7 +1,7 @@
import { getRequestHeaders } from '../../../script.js'
import { getRequestHeaders } from '../../../script.js';
import { saveTtsProviderSettings } from './index.js';
export { OpenAITtsProvider }
export { OpenAITtsProvider };
class OpenAITtsProvider {
static voices = [
@ -13,17 +13,17 @@ class OpenAITtsProvider {
{ name: 'Shimmer', voice_id: 'shimmer', lang: 'en-US', preview_url: 'https://cdn.openai.com/API/docs/audio/shimmer.wav' },
];
settings
voices = []
separator = ' . '
audioElement = document.createElement('audio')
settings;
voices = [];
separator = ' . ';
audioElement = document.createElement('audio');
defaultSettings = {
voiceMap: {},
customVoices: [],
model: 'tts-1',
speed: 1,
}
};
get settingsHtml() {
let html = `
@ -52,7 +52,7 @@ class OpenAITtsProvider {
async loadSettings(settings) {
// Populate Provider UI given input settings
if (Object.keys(settings).length == 0) {
console.info('Using default TTS Provider settings')
console.info('Using default TTS Provider settings');
}
// Only accept keys defined in defaultSettings
@ -100,21 +100,21 @@ class OpenAITtsProvider {
async getVoice(voiceName) {
if (!voiceName) {
throw 'TTS Voice name not provided'
throw 'TTS Voice name not provided';
}
const voice = OpenAITtsProvider.voices.find(voice => voice.voice_id === voiceName || voice.name === voiceName);
if (!voice) {
throw `TTS Voice not found: ${voiceName}`
throw `TTS Voice not found: ${voiceName}`;
}
return voice;
}
async generateTts(text, voiceId) {
const response = await this.fetchTtsGeneration(text, voiceId)
return response
const response = await this.fetchTtsGeneration(text, voiceId);
return response;
}
async fetchTtsVoiceObjects() {
@ -126,7 +126,7 @@ class OpenAITtsProvider {
}
async fetchTtsGeneration(inputText, voiceId) {
console.info(`Generating new TTS for voice_id ${voiceId}`)
console.info(`Generating new TTS for voice_id ${voiceId}`);
const response = await fetch('/api/openai/generate-voice', {
method: 'POST',
headers: getRequestHeaders(),

View File

@ -1,22 +1,22 @@
import { doExtrasFetch, getApiUrl, modules } from '../../extensions.js'
import { saveTtsProviderSettings } from './index.js'
import { doExtrasFetch, getApiUrl, modules } from '../../extensions.js';
import { saveTtsProviderSettings } from './index.js';
export { SileroTtsProvider }
export { SileroTtsProvider };
class SileroTtsProvider {
//########//
// Config //
//########//
settings
ready = false
voices = []
separator = ' .. '
settings;
ready = false;
voices = [];
separator = ' .. ';
defaultSettings = {
provider_endpoint: 'http://localhost:8001/tts',
voiceMap: {}
}
};
get settingsHtml() {
let html = `
@ -24,31 +24,31 @@ class SileroTtsProvider {
<input id="silero_tts_endpoint" type="text" class="text_pole" maxlength="250" value="${this.defaultSettings.provider_endpoint}"/>
<span>
<span>Use <a target="_blank" href="https://github.com/SillyTavern/SillyTavern-extras">SillyTavern Extras API</a> or <a target="_blank" href="https://github.com/ouoertheo/silero-api-server">Silero TTS Server</a>.</span>
`
return html
`;
return html;
}
onSettingsChange() {
// Used when provider settings are updated from UI
this.settings.provider_endpoint = $('#silero_tts_endpoint').val()
saveTtsProviderSettings()
this.refreshSession()
this.settings.provider_endpoint = $('#silero_tts_endpoint').val();
saveTtsProviderSettings();
this.refreshSession();
}
async loadSettings(settings) {
// Pupulate Provider UI given input settings
if (Object.keys(settings).length == 0) {
console.info('Using default TTS Provider settings')
console.info('Using default TTS Provider settings');
}
// Only accept keys defined in defaultSettings
this.settings = this.defaultSettings
this.settings = this.defaultSettings;
for (const key in settings) {
if (key in this.settings) {
this.settings[key] = settings[key]
this.settings[key] = settings[key];
} else {
throw `Invalid setting passed to TTS Provider: ${key}`
throw `Invalid setting passed to TTS Provider: ${key}`;
}
}
@ -63,26 +63,26 @@ class SileroTtsProvider {
}
}, 2000);
$('#silero_tts_endpoint').val(this.settings.provider_endpoint)
$('#silero_tts_endpoint').on('input', () => { this.onSettingsChange() })
this.refreshSession()
$('#silero_tts_endpoint').val(this.settings.provider_endpoint);
$('#silero_tts_endpoint').on('input', () => { this.onSettingsChange(); });
this.refreshSession();
await this.checkReady()
await this.checkReady();
console.debug('SileroTTS: Settings loaded')
console.debug('SileroTTS: Settings loaded');
}
// Perform a simple readiness check by trying to fetch voiceIds
async checkReady() {
await this.fetchTtsVoiceObjects()
await this.fetchTtsVoiceObjects();
}
async onRefreshClick() {
return
return;
}
async refreshSession() {
await this.initSession()
await this.initSession();
}
//#################//
@ -91,36 +91,36 @@ class SileroTtsProvider {
async getVoice(voiceName) {
if (this.voices.length == 0) {
this.voices = await this.fetchTtsVoiceObjects()
this.voices = await this.fetchTtsVoiceObjects();
}
const match = this.voices.filter(
sileroVoice => sileroVoice.name == voiceName
)[0]
)[0];
if (!match) {
throw `TTS Voice name ${voiceName} not found`
throw `TTS Voice name ${voiceName} not found`;
}
return match
return match;
}
async generateTts(text, voiceId) {
const response = await this.fetchTtsGeneration(text, voiceId)
return response
const response = await this.fetchTtsGeneration(text, voiceId);
return response;
}
//###########//
// API CALLS //
//###########//
async fetchTtsVoiceObjects() {
const response = await doExtrasFetch(`${this.settings.provider_endpoint}/speakers`)
const response = await doExtrasFetch(`${this.settings.provider_endpoint}/speakers`);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.json()}`)
throw new Error(`HTTP ${response.status}: ${await response.json()}`);
}
const responseJson = await response.json()
return responseJson
const responseJson = await response.json();
return responseJson;
}
async fetchTtsGeneration(inputText, voiceId) {
console.info(`Generating new TTS for voice_id ${voiceId}`)
console.info(`Generating new TTS for voice_id ${voiceId}`);
const response = await doExtrasFetch(
`${this.settings.provider_endpoint}/generate`,
{
@ -135,12 +135,12 @@ class SileroTtsProvider {
'session': 'sillytavern'
})
}
)
);
if (!response.ok) {
toastr.error(response.statusText, 'TTS Generation Failed');
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
return response
return response;
}
async initSession() {
@ -158,7 +158,7 @@ class SileroTtsProvider {
'path': 'sillytavern',
}),
}
)
);
if (!response.ok && response.status !== 404) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`);

View File

@ -1,8 +1,8 @@
import { isMobile } from '../../RossAscends-mods.js';
import { getPreviewString } from './index.js';
import { talkingAnimation } from './index.js';
import { saveTtsProviderSettings } from './index.js'
export { SystemTtsProvider }
import { saveTtsProviderSettings } from './index.js';
export { SystemTtsProvider };
/**
* Chunkify
@ -79,16 +79,16 @@ class SystemTtsProvider {
// Config //
//########//
settings
ready = false
voices = []
separator = ' ... '
settings;
ready = false;
voices = [];
separator = ' ... ';
defaultSettings = {
voiceMap: {},
rate: 1,
pitch: 1,
}
};
get settingsHtml() {
if (!('speechSynthesis' in window)) {
@ -107,7 +107,7 @@ class SystemTtsProvider {
this.settings.pitch = Number($('#system_tts_pitch').val());
$('#system_tts_pitch_output').text(this.settings.pitch);
$('#system_tts_rate_output').text(this.settings.rate);
saveTtsProviderSettings()
saveTtsProviderSettings();
}
async loadSettings(settings) {
@ -146,8 +146,8 @@ class SystemTtsProvider {
$('#system_tts_pitch').val(this.settings.pitch || this.defaultSettings.pitch);
// Trigger updates
$('#system_tts_rate').on('input', () => { this.onSettingsChange() })
$('#system_tts_rate').on('input', () => { this.onSettingsChange() })
$('#system_tts_rate').on('input', () => { this.onSettingsChange(); });
$('#system_tts_rate').on('input', () => { this.onSettingsChange(); });
$('#system_tts_pitch_output').text(this.settings.pitch);
$('#system_tts_rate_output').text(this.settings.rate);
@ -156,11 +156,11 @@ class SystemTtsProvider {
// Perform a simple readiness check by trying to fetch voiceIds
async checkReady() {
await this.fetchTtsVoiceObjects()
await this.fetchTtsVoiceObjects();
}
async onRefreshClick() {
return
return;
}
//#################//
@ -191,7 +191,7 @@ class SystemTtsProvider {
const voice = speechSynthesis.getVoices().find(x => x.voiceURI === voiceId);
if (!voice) {
throw `TTS Voice id ${voiceId} not found`
throw `TTS Voice id ${voiceId} not found`;
}
speechSynthesis.cancel();
@ -205,14 +205,14 @@ class SystemTtsProvider {
async getVoice(voiceName) {
if (!('speechSynthesis' in window)) {
return { voice_id: null }
return { voice_id: null };
}
const voices = speechSynthesis.getVoices();
const match = voices.find(x => x.name == voiceName);
if (!match) {
throw `TTS Voice name ${voiceName} not found`
throw `TTS Voice name ${voiceName} not found`;
}
return { voice_id: match.voiceURI, name: match.name };

View File

@ -1,17 +1,17 @@
import { doExtrasFetch, getApiUrl, modules } from '../../extensions.js'
import { saveTtsProviderSettings } from './index.js'
import { doExtrasFetch, getApiUrl, modules } from '../../extensions.js';
import { saveTtsProviderSettings } from './index.js';
export { XTTSTtsProvider }
export { XTTSTtsProvider };
class XTTSTtsProvider {
//########//
// Config //
//########//
settings
ready = false
voices = []
separator = '. '
settings;
ready = false;
voices = [];
separator = '. ';
/**
* Perform any text processing before passing to TTS engine.
@ -46,13 +46,13 @@ class XTTSTtsProvider {
'Korean': 'ko',
'Hungarian': 'hu',
'Hindi': 'hi',
}
};
defaultSettings = {
provider_endpoint: 'http://localhost:8020',
language: 'en',
voiceMap: {}
}
};
get settingsHtml() {
let html = `
@ -64,7 +64,7 @@ class XTTSTtsProvider {
if (this.languageLabels[language] == this.settings?.language) {
html += `<option value="${this.languageLabels[language]}" selected="selected">${language}</option>`;
continue
continue;
}
html += `<option value="${this.languageLabels[language]}">${language}</option>`;
@ -88,25 +88,25 @@ class XTTSTtsProvider {
}
onSettingsChange() {
// Used when provider settings are updated from UI
this.settings.provider_endpoint = $('#xtts_tts_endpoint').val()
this.settings.language = $('#xtts_api_language').val()
saveTtsProviderSettings()
this.settings.provider_endpoint = $('#xtts_tts_endpoint').val();
this.settings.language = $('#xtts_api_language').val();
saveTtsProviderSettings();
}
async loadSettings(settings) {
// Pupulate Provider UI given input settings
if (Object.keys(settings).length == 0) {
console.info('Using default TTS Provider settings')
console.info('Using default TTS Provider settings');
}
// Only accept keys defined in defaultSettings
this.settings = this.defaultSettings
this.settings = this.defaultSettings;
for (const key in settings) {
if (key in this.settings) {
this.settings[key] = settings[key]
this.settings[key] = settings[key];
} else {
throw `Invalid setting passed to TTS Provider: ${key}`
throw `Invalid setting passed to TTS Provider: ${key}`;
}
}
@ -121,23 +121,23 @@ class XTTSTtsProvider {
}
}, 2000);
$('#xtts_tts_endpoint').val(this.settings.provider_endpoint)
$('#xtts_tts_endpoint').on('input', () => { this.onSettingsChange() })
$('#xtts_api_language').val(this.settings.language)
$('#xtts_api_language').on('change', () => { this.onSettingsChange() })
$('#xtts_tts_endpoint').val(this.settings.provider_endpoint);
$('#xtts_tts_endpoint').on('input', () => { this.onSettingsChange(); });
$('#xtts_api_language').val(this.settings.language);
$('#xtts_api_language').on('change', () => { this.onSettingsChange(); });
await this.checkReady()
await this.checkReady();
console.debug('XTTS: Settings loaded')
console.debug('XTTS: Settings loaded');
}
// Perform a simple readiness check by trying to fetch voiceIds
async checkReady() {
await this.fetchTtsVoiceObjects()
await this.fetchTtsVoiceObjects();
}
async onRefreshClick() {
return
return;
}
//#################//
@ -146,36 +146,36 @@ class XTTSTtsProvider {
async getVoice(voiceName) {
if (this.voices.length == 0) {
this.voices = await this.fetchTtsVoiceObjects()
this.voices = await this.fetchTtsVoiceObjects();
}
const match = this.voices.filter(
XTTSVoice => XTTSVoice.name == voiceName
)[0]
)[0];
if (!match) {
throw `TTS Voice name ${voiceName} not found`
throw `TTS Voice name ${voiceName} not found`;
}
return match
return match;
}
async generateTts(text, voiceId) {
const response = await this.fetchTtsGeneration(text, voiceId)
return response
const response = await this.fetchTtsGeneration(text, voiceId);
return response;
}
//###########//
// API CALLS //
//###########//
async fetchTtsVoiceObjects() {
const response = await doExtrasFetch(`${this.settings.provider_endpoint}/speakers`)
const response = await doExtrasFetch(`${this.settings.provider_endpoint}/speakers`);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.json()}`)
throw new Error(`HTTP ${response.status}: ${await response.json()}`);
}
const responseJson = await response.json()
return responseJson
const responseJson = await response.json();
return responseJson;
}
async fetchTtsGeneration(inputText, voiceId) {
console.info(`Generating new TTS for voice_id ${voiceId}`)
console.info(`Generating new TTS for voice_id ${voiceId}`);
const response = await doExtrasFetch(
`${this.settings.provider_endpoint}/tts_to_audio/`,
{
@ -190,12 +190,12 @@ class XTTSTtsProvider {
'language': this.settings.language
})
}
)
);
if (!response.ok) {
toastr.error(response.statusText, 'TTS Generation Failed');
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
return response
return response;
}
// Interface not used by XTTS TTS