Reduce nesting and simply WI check loop

02989a9a787c64de5644b0d5635b790245020365

Wolfsblvt <wolfsblvt@gmail.com>

1 files changed, +34 -33Ignore whitespace
public/scripts/world-info.js+34 -33
@@ -3591,6 +3591,10 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
35913591
3592 console.debug(`[WI] Loop #${count}. Search state`, Object.entries(scan_state).find(x => x[1] === scanState));3592 console.debug(`[WI] Loop #${count}. Search state`, Object.entries(scan_state).find(x => x[1] === scanState));
35933593
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 let activatedNow = new Set();3598 let activatedNow = new Set();
35953599
3596 for (let entry of sortedEntries) {3600 for (let entry of sortedEntries) {
@@ -3775,8 +3779,6 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
3775 }3779 }
37763780
3777 console.debug(`[WI] Search done. Found ${activatedNow.size} possible entries.`);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 const newEntries = [...activatedNow]3782 const newEntries = [...activatedNow]
3781 .sort((a, b) => sortedEntries.indexOf(a) - sortedEntries.indexOf(b));3783 .sort((a, b) => sortedEntries.indexOf(a) - sortedEntries.indexOf(b));
3782 let newContent = '';3784 let newContent = '';
@@ -3826,7 +3828,6 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
3826 } else {3828 } else {
3827 console.debug(`[WI] budget of ${budget} reached, stopping after ${allActivatedEntries.size} entries`);3829 console.debug(`[WI] budget of ${budget} reached, stopping after ${allActivatedEntries.size} entries`);
3828 }3830 }
3829 scanState = scan_state.NONE;
3830 token_budget_overflowed = true;3831 token_budget_overflowed = true;
3831 break;3832 break;
3832 }3833 }
@@ -3836,48 +3837,48 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
3836 }3837 }
38373838
3838 const successfulNewEntries = newEntries.filter(x => !failedProbabilityChecks.has(x));3839 const successfulNewEntries = newEntries.filter(x => !failedProbabilityChecks.has(x));
3840 const successfulNewEntriesForRecursion = successfulNewEntries.filter(x => !x.preventRecursion);
38393841
3840 if (!newEntries.length) {3842 if (!newEntries.length) {
3841 console.debug('[WI] No new entries activated, stopping');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 console.debug('[WI] Probability checks failed for all activated entries, stopping');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 }
38573849
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 }
38613854
3862 // world_info_min_activations3855 // 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 if (world_info_min_activations > 0 && (allActivatedEntries.size < world_info_min_activations)) {3857 if (!nextScanState && !token_budget_overflowed && minActivationsNotSatisfied) {
3865 console.debug('[WI] --- MIN ACTIVATIONS CHECK ---');3858 console.debug('[WI] --- MIN ACTIVATIONS CHECK ---');
38663859
3867 let over_max = (3860 let over_max = (
3868 world_info_min_activations_depth_max > 0 &&3861 world_info_min_activations_depth_max > 0 &&
3869 buffer.getDepth() > world_info_min_activations_depth_max3862 buffer.getDepth() > world_info_min_activations_depth_max
3870 ) || (buffer.getDepth() > chat.length);3863 ) || (buffer.getDepth() > chat.length);
38713864
3872 if (!over_max) {3865 if (!over_max) {
3873 console.debug(`[WI] Min activations not reached (${allActivatedEntries.size}/${world_info_min_activations}), advancing depth to ${buffer.getDepth() + 1} and checking again`);3866 console.debug(`[WI] Min activations not reached (${allActivatedEntries.size}/${world_info_min_activations}), advancing depth to ${buffer.getDepth() + 1} and checking again`);
3874 scanState = scan_state.MIN_ACTIVATIONS; // loop3867 nextScanState = scan_state.MIN_ACTIVATIONS; // loop
3875 buffer.advanceScanPosition();3868 buffer.advanceScanPosition();
3876 } else {3869 } else {
3877 console.debug(`[WI] Min activations not reached (${allActivatedEntries.size}/${world_info_min_activations}), but reached on of depth. Stopping`);3870 console.debug(`[WI] Min activations not reached (${allActivatedEntries.size}/${world_info_min_activations}), but reached on of depth. Stopping`);
3878 }
3879 }3871 }
3880 }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);
3881 }
3881 }3882 }
38823883
3883 console.debug('[WI] --- BUILDING PROMPT ---');3884 console.debug('[WI] --- BUILDING PROMPT ---');