Merge branch 'staging' into qr-rewrite

This commit is contained in:
Cohee 2024-01-01 22:36:53 +02:00
commit 9a1103cf43
5 changed files with 62 additions and 31 deletions

View File

@ -4274,6 +4274,7 @@
<small class="textAlignCenter">Logic</small>
<select name="entryLogicType" class="widthFitContent margin0">
<option value="0">AND ANY</option>
<option value="3">AND ALL</option>
<option value="1">NOT ALL</option>
<option value="2">NOT ANY</option>
</select>

View File

@ -1,6 +1,6 @@
import { callPopup, cancelTtsPlay, eventSource, event_types, name2, saveSettingsDebounced } from '../../../script.js';
import { ModuleWorkerWrapper, doExtrasFetch, extension_settings, getApiUrl, getContext, modules } from '../../extensions.js';
import { delay, escapeRegex, getStringHash } from '../../utils.js';
import { delay, escapeRegex, getStringHash, onlyUnique } from '../../utils.js';
import { EdgeTtsProvider } from './edge.js';
import { ElevenLabsTtsProvider } from './elevenlabs.js';
import { SileroTtsProvider } from './silerotts.js';
@ -463,13 +463,25 @@ function saveLastValues() {
}
async function tts(text, voiceId, char) {
async function processResponse(response) {
// RVC injection
if (extension_settings.rvc.enabled && typeof window['rvcVoiceConversion'] === 'function')
response = await window['rvcVoiceConversion'](response, char, text);
await addAudioJob(response);
}
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);
// If async generator, process every chunk as it comes in
if (typeof response[Symbol.asyncIterator] === 'function') {
for await (const chunk of response) {
await processResponse(chunk);
}
} else {
await processResponse(response);
}
addAudioJob(response);
completeTtsJob();
}
@ -733,7 +745,7 @@ function getCharacters(unrestricted) {
if (unrestricted) {
const names = context.characters.map(char => char.name);
names.unshift(DEFAULT_VOICE_MARKER);
return names;
return names.filter(onlyUnique);
}
let characters = [];
@ -748,14 +760,13 @@ function getCharacters(unrestricted) {
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);
const character = context.characters.find(char => char.avatar == member);
if (character) {
characters.push(character.name);
}
characters.push(member);
}
}
return characters;
return characters.filter(onlyUnique);
}
function sanitizeId(input) {

View File

@ -1,4 +1,5 @@
import { getRequestHeaders, callPopup } from '../../../script.js';
import { splitRecursive } from '../../utils.js';
import { getPreviewString, saveTtsProviderSettings } from './index.js';
import { initVoiceMap } from './index.js';
@ -52,7 +53,7 @@ class NovelTtsProvider {
// Add a new Novel custom voice to provider
async addCustomVoice(){
async addCustomVoice() {
const voiceName = await callPopup('<h3>Custom Voice name:</h3>', 'input');
this.settings.customVoices.push(voiceName);
this.populateCustomVoices();
@ -74,7 +75,7 @@ class NovelTtsProvider {
}
// Create the UI dropdown list of voices in provider
populateCustomVoices(){
populateCustomVoices() {
let voiceSelect = $('#tts-novel-custom-voices-select');
voiceSelect.empty();
this.settings.customVoices.forEach(voice => {
@ -88,7 +89,7 @@ class NovelTtsProvider {
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-delete').on('click', () => (this.deleteCustomVoice()));
// Only accept keys defined in defaultSettings
this.settings = this.defaultSettings;
@ -108,7 +109,7 @@ class NovelTtsProvider {
// 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(){
async checkReady() {
await this.fetchTtsVoiceObjects();
}
@ -179,22 +180,26 @@ class NovelTtsProvider {
this.audioElement.play();
}
async fetchTtsGeneration(inputText, voiceId) {
async* fetchTtsGeneration(inputText, voiceId) {
const MAX_LENGTH = 1000;
console.info(`Generating new TTS for voice_id ${voiceId}`);
const response = await fetch('/api/novelai/generate-voice',
{
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({
'text': inputText,
'voice': voiceId,
}),
},
);
if (!response.ok) {
toastr.error(response.statusText, 'TTS Generation Failed');
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
const chunks = splitRecursive(inputText, MAX_LENGTH);
for (const chunk of chunks) {
const response = await fetch('/api/novelai/generate-voice',
{
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({
'text': chunk,
'voice': voiceId,
}),
},
);
if (!response.ok) {
toastr.error(response.statusText, 'TTS Generation Failed');
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
yield response;
}
return response;
}
}

View File

@ -39,6 +39,7 @@ const world_info_logic = {
AND_ANY: 0,
NOT_ALL: 1,
NOT_ANY: 2,
AND_ALL: 3,
};
let world_info = {};
@ -1789,6 +1790,7 @@ async function checkWorldInfo(chat, maxContext) {
) {
console.debug(`WI UID:${entry.uid} found. Checking logic: ${entry.selectiveLogic}`);
let hasAnyMatch = false;
let hasAllMatch = true;
secondary: for (let keysecondary of entry.keysecondary) {
const secondarySubstituted = substituteParams(keysecondary);
const hasSecondaryMatch = secondarySubstituted && matchKeys(textToScan, secondarySubstituted.trim());
@ -1798,6 +1800,10 @@ async function checkWorldInfo(chat, maxContext) {
hasAnyMatch = true;
}
if (!hasSecondaryMatch) {
hasAllMatch = false;
}
// Simplified AND ANY / NOT ALL if statement. (Proper fix for PR#1356 by Bronya)
// If AND ANY logic and the main checks pass OR if NOT ALL logic and the main checks do not pass
if ((selectiveLogic === world_info_logic.AND_ANY && hasSecondaryMatch) || (selectiveLogic === world_info_logic.NOT_ALL && !hasSecondaryMatch)) {
@ -1817,6 +1823,12 @@ async function checkWorldInfo(chat, maxContext) {
console.debug(`(NOT ANY Check) Activating WI Entry ${entry.uid}, no secondary keywords found.`);
activatedNow.add(entry);
}
// Handle AND ALL logic
if (selectiveLogic === world_info_logic.AND_ALL && hasAllMatch) {
console.debug(`(AND ALL Check) Activating WI Entry ${entry.uid}, all secondary keywords found.`);
activatedNow.add(entry);
}
} else {
// Handle cases where secondary is empty
console.debug(`WI UID ${entry.uid}: Activated without filter logic.`);

View File

@ -342,7 +342,9 @@ router.post('/generate-voice', jsonParser, async (request, response) => {
});
if (!result.ok) {
return response.sendStatus(result.status);
const errorText = await result.text();
console.log('NovelAI returned an error.', result.statusText, errorText);
return response.sendStatus(500);
}
const chunks = await readAllChunks(result.body);