Reduce nesting and simply WI check loop
| @@ -3591,6 +3591,10 @@ async function checkWorldInfo(chat, maxContext, isDryRun) { | ||
| 3591 | 3591 | |
| 3592 | 3592 | console.debug(`[WI] Loop #${count}. Search state`, Object.entries(scan_state).find(x => x[1] === scanState)); |
| 3593 | 3593 | |
| 3594 | + // Until decided otherwise, we set the loop to stop scanning after this | |
| 3595 | + let nextScanState = scan_state.NONE; | |
| 3596 | + | |
| 3597 | + // Loop and find all entries that can activate here | |
| 3594 | 3598 | let activatedNow = new Set(); |
| 3595 | 3599 | |
| 3596 | 3600 | for (let entry of sortedEntries) { |
| @@ -3775,8 +3779,6 @@ async function checkWorldInfo(chat, maxContext, isDryRun) { | ||
| 3775 | 3779 | } |
| 3776 | 3780 | |
| 3777 | 3781 | console.debug(`[WI] Search done. Found ${activatedNow.size} possible entries.`); |
| 3778 | - | |
| 3779 | - scanState = world_info_recursive && activatedNow.size > 0 ? scan_state.RECURSION : scan_state.NONE; | |
| 3780 | 3782 | const newEntries = [...activatedNow] |
| 3781 | 3783 | .sort((a, b) => sortedEntries.indexOf(a) - sortedEntries.indexOf(b)); |
| 3782 | 3784 | let newContent = ''; |
| @@ -3826,7 +3828,6 @@ async function checkWorldInfo(chat, maxContext, isDryRun) { | ||
| 3826 | 3828 | } else { |
| 3827 | 3829 | console.debug(`[WI] budget of ${budget} reached, stopping after ${allActivatedEntries.size} entries`); |
| 3828 | 3830 | } |
| 3829 | - scanState = scan_state.NONE; | |
| 3830 | 3831 | token_budget_overflowed = true; |
| 3831 | 3832 | break; |
| 3832 | 3833 | } |
| @@ -3836,32 +3837,24 @@ async function checkWorldInfo(chat, maxContext, isDryRun) { | ||
| 3836 | 3837 | } |
| 3837 | 3838 | |
| 3838 | 3839 | const successfulNewEntries = newEntries.filter(x => !failedProbabilityChecks.has(x)); |
| 3840 | + const successfulNewEntriesForRecursion = successfulNewEntries.filter(x => !x.preventRecursion); | |
| 3839 | 3841 | |
| 3840 | 3842 | if (!newEntries.length) { |
| 3841 | 3843 | console.debug('[WI] No new entries activated, stopping'); |
| 3842 | - scanState = scan_state.NONE; | |
| 3844 | + } else if (!successfulNewEntries.length) { | |
| 3843 | - } | |
| 3844 | - | |
| 3845 | - if (newEntries.length && !successfulNewEntries.length) { | |
| 3846 | 3845 | console.debug('[WI] Probability checks failed for all activated entries, stopping'); |
| 3847 | - scanState = scan_state.NONE; | |
| 3846 | + } else { | |
| 3848 | - } | |
| 3847 | + console.debug(`[WI] Sucessfully activated ${successfulNewEntries.length} new entries to prompt. ${allActivatedEntries.size} total entries activated.`, successfulNewEntries); | |
| 3849 | - | |
| 3850 | - if (scanState) { | |
| 3851 | - const text = successfulNewEntries | |
| 3852 | - .filter(x => !x.preventRecursion) | |
| 3853 | - .map(x => x.content).join('\n'); | |
| 3854 | - buffer.addRecurse(text); | |
| 3855 | - allActivatedText = (text + '\n' + allActivatedText); | |
| 3856 | 3848 | } |
| 3857 | 3849 | |
| 3858 | - if (successfulNewEntries.length) { | |
| 3850 | + // After processing and rolling entries is done, see if we should continue with recursion | |
| 3859 | - console.debug(`[WI] Sucessfully activated ${successfulNewEntries.length} new entries to prompt. ${allActivatedEntries.size} total entries activated.`, successfulNewEntries); | |
| 3851 | + if (world_info_recursive && !token_budget_overflowed && successfulNewEntriesForRecursion.length) { | |
| 3852 | + nextScanState = scan_state.RECURSION; | |
| 3860 | 3853 | } |
| 3861 | 3854 | |
| 3862 | - // world_info_min_activations | |
| 3855 | + // If scanning is planned to stop, but min activations is set and not satisfied, check if we should continue | |
| 3863 | - if (!scanState && !token_budget_overflowed) { | |
| 3856 | + const minActivationsNotSatisfied = world_info_min_activations > 0 && (allActivatedEntries.size < world_info_min_activations); | |
| 3864 | 3857 | if (world_info_min_activations > 0!nextScanState && (allActivatedEntries.size!token_budget_overflowed <&& world_info_min_activations)minActivationsNotSatisfied) { |
| 3865 | 3858 | console.debug('[WI] --- MIN ACTIVATIONS CHECK ---'); |
| 3866 | 3859 | |
| 3867 | 3860 | let over_max = ( |
| @@ -3871,12 +3864,20 @@ async function checkWorldInfo(chat, maxContext, isDryRun) { | ||
| 3871 | 3864 | |
| 3872 | 3865 | if (!over_max) { |
| 3873 | 3866 | console.debug(`[WI] Min activations not reached (${allActivatedEntries.size}/${world_info_min_activations}), advancing depth to ${buffer.getDepth() + 1} and checking again`); |
| 3874 | 3867 | scanState nextScanState = scan_state.MIN_ACTIVATIONS; // loop |
| 3875 | 3868 | buffer.advanceScanPosition(); |
| 3876 | 3869 | } else { |
| 3877 | 3870 | console.debug(`[WI] Min activations not reached (${allActivatedEntries.size}/${world_info_min_activations}), but reached on of depth. Stopping`); |
| 3878 | 3871 | } |
| 3879 | 3872 | } |
| 3873 | + | |
| 3874 | + // Final check if we should really continue scan, and extend the current WI recurse buffer | |
| 3875 | + scanState = nextScanState; | |
| 3876 | + if (scanState) { | |
| 3877 | + const text = successfulNewEntriesForRecursion | |
| 3878 | + .map(x => x.content).join('\n'); | |
| 3879 | + buffer.addRecurse(text); | |
| 3880 | + allActivatedText = (text + '\n' + allActivatedText); | |
| 3880 | 3881 | } |
| 3881 | 3882 | } |
| 3882 | 3883 | |