Merge pull request #3388 from SillyTavern/autoswipe-fix Autoswipe: Fix endless loop if blacklist empty
Signed| @@ -95,6 +95,7 @@ import { | ||
| 95 | 95 | resetMovableStyles, |
| 96 | 96 | forceCharacterEditorTokenize, |
| 97 | 97 | applyPowerUserSettings, |
| 98 | + generatedTextFiltered, | |
| 98 | 99 | } from './scripts/power-user.js'; |
| 99 | 100 | |
| 100 | 101 | import { |
| @@ -3290,39 +3291,11 @@ class StreamingProcessor { | ||
| 3290 | 3291 | unblockGeneration(); |
| 3291 | 3292 | generatedPromptCache = ''; |
| 3292 | 3293 | |
| 3293 | - //console.log("Generated text size:", text.length, text) | |
| 3294 | - | |
| 3295 | 3294 | const isAborted = this.abortController.signal.aborted; |
| 3296 | 3295 | 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 | - } | |
| 3310 | 3297 | } |
| 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 | - }; | |
| 3320 | 3298 | |
| 3321 | - if (generatedTextFiltered(text)) { | |
| 3322 | - swipe_right(); | |
| 3323 | - return; | |
| 3324 | - } | |
| 3325 | - } | |
| 3326 | 3299 | playMessageSound(); |
| 3327 | 3300 | } |
| 3328 | 3301 | |
| @@ -4862,32 +4835,9 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | ||
| 4862 | 4835 | } |
| 4863 | 4836 | |
| 4864 | 4837 | const isAborted = abortController && abortController.signal.aborted; |
| 4865 | 4838 | 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'); | |
| 4886 | 4839 | is_send_press = false; |
| 4887 | 4840 | return swipe_right(); |
| 4888 | - // TODO: do we want to resolve after an auto-swipe? | |
| 4889 | - return; | |
| 4890 | - } | |
| 4891 | 4841 | } |
| 4892 | 4842 | |
| 4893 | 4843 | console.debug('/api/chats/save called by /Generate'); |
| @@ -2917,6 +2917,46 @@ export function flushEphemeralStoppingStrings() { | ||
| 2917 | 2917 | } |
| 2918 | 2918 | |
| 2919 | 2919 | /** |
| 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 | +/** | |
| 2920 | 2960 | * Gets the custom stopping strings from the power user settings. |
| 2921 | 2961 | * @param {number | undefined} limit Number of strings to return. If 0 or undefined, returns all strings. |
| 2922 | 2962 | * @returns {string[]} An array of custom stopping strings |