Autoswipe: Fix endless loop if blacklist empty

c32e0bdde74838ae84b071e380d6fb448cd14be4

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +45 -55Showing whitespace changes
public/script.js+5 -55
@@ -95,6 +95,7 @@ import {
9595 resetMovableStyles,
9696 forceCharacterEditorTokenize,
9797 applyPowerUserSettings,
98+ generatedTextFiltered,
9899} from './scripts/power-user.js';
99100
100101import {
@@ -3290,39 +3291,11 @@ class StreamingProcessor {
32903291 unblockGeneration();
32913292 generatedPromptCache = '';
32923293
3293- //console.log("Generated text size:", text.length, text)
3294-
32953294 const isAborted = this.abortController.signal.aborted;
32963295 if (!isAborted && power_user.auto_swipe && !isAbortedgeneratedTextFiltered(text)) {
3297- function containsBlacklistedWords(str, blacklist, threshold) {
3296+ return swipe_right();
3298- const regex = new RegExp(`\\b(${blacklist.join('|')})\\b`, 'gi');
3299- const matches = str.match(regex) || [];
3300- return matches.length >= threshold;
3301- }
3302-
3303- const generatedTextFiltered = (text) => {
3304- if (text) {
3305- if (power_user.auto_swipe_minimum_length) {
3306- if (text.length < power_user.auto_swipe_minimum_length && text.length !== 0) {
3307- console.log('Generated text size too small');
3308- return true;
3309- }
33103297 }
3311- if (power_user.auto_swipe_blacklist_threshold) {
3312- if (containsBlacklistedWords(text, power_user.auto_swipe_blacklist, power_user.auto_swipe_blacklist_threshold)) {
3313- console.log('Generated text has blacklisted words');
3314- return true;
3315- }
3316- }
3317- }
3318- return false;
3319- };
33203298
3321- if (generatedTextFiltered(text)) {
3322- swipe_right();
3323- return;
3324- }
3325- }
33263299 playMessageSound();
33273300 }
33283301
@@ -4862,32 +4835,9 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
48624835 }
48634836
48644837 const isAborted = abortController && abortController.signal.aborted;
48654838 if (!isAborted && power_user.auto_swipe && !isAbortedgeneratedTextFiltered(getMessage)) {
4866- console.debug('checking for autoswipeblacklist on non-streaming message');
4867- function containsBlacklistedWords(getMessage, blacklist, threshold) {
4868- console.debug('checking blacklisted words');
4869- const regex = new RegExp(`\\b(${blacklist.join('|')})\\b`, 'gi');
4870- const matches = getMessage.match(regex) || [];
4871- return matches.length >= threshold;
4872- }
4873-
4874- const generatedTextFiltered = (getMessage) => {
4875- if (power_user.auto_swipe_blacklist_threshold) {
4876- if (containsBlacklistedWords(getMessage, power_user.auto_swipe_blacklist, power_user.auto_swipe_blacklist_threshold)) {
4877- console.debug('Generated text has blacklisted words');
4878- return true;
4879- }
4880- }
4881-
4882- return false;
4883- };
4884- if (generatedTextFiltered(getMessage)) {
4885- console.debug('swiping right automatically');
48864839 is_send_press = false;
48874840 return swipe_right();
4888- // TODO: do we want to resolve after an auto-swipe?
4889- return;
4890- }
48914841 }
48924842
48934843 console.debug('/api/chats/save called by /Generate');
public/scripts/power-user.js+40 -0
@@ -2917,6 +2917,46 @@ export function flushEphemeralStoppingStrings() {
29172917}
29182918
29192919/**
2920+ * Checks if the generated text should be filtered based on the auto-swipe settings.
2921+ * @param {string} text The text to check
2922+ * @returns {boolean} If the generated text should be filtered
2923+ */
2924+export function generatedTextFiltered(text) {
2925+ /**
2926+ * Checks if the given text contains any of the blacklisted words.
2927+ * @param {string} text The text to check
2928+ * @param {string[]} blacklist The list of blacklisted words
2929+ * @param {number} threshold The number of blacklisted words that need to be present to trigger the check
2930+ * @returns {boolean} Whether the text contains blacklisted words
2931+ */
2932+ function containsBlacklistedWords(text, blacklist, threshold) {
2933+ const regex = new RegExp(`\\b(${blacklist.join('|')})\\b`, 'gi');
2934+ const matches = text.match(regex) || [];
2935+ return matches.length >= threshold;
2936+ }
2937+
2938+ // Make sure a generated text is non-empty
2939+ // Otherwise we might get in a loop with a broken API
2940+ text = text.trim();
2941+ if (text.length > 0) {
2942+ if (power_user.auto_swipe_minimum_length) {
2943+ if (text.length < power_user.auto_swipe_minimum_length) {
2944+ console.log('Generated text size too small');
2945+ return true;
2946+ }
2947+ }
2948+ if (power_user.auto_swipe_blacklist.length && power_user.auto_swipe_blacklist_threshold) {
2949+ if (containsBlacklistedWords(text, power_user.auto_swipe_blacklist, power_user.auto_swipe_blacklist_threshold)) {
2950+ console.log('Generated text has blacklisted words');
2951+ return true;
2952+ }
2953+ }
2954+ }
2955+
2956+ return false;
2957+}
2958+
2959+/**
29202960 * Gets the custom stopping strings from the power user settings.
29212961 * @param {number | undefined} limit Number of strings to return. If 0 or undefined, returns all strings.
29222962 * @returns {string[]} An array of custom stopping strings