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
35923592 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
35943598 let activatedNow = new Set();
35953599
35963600 for (let entry of sortedEntries) {
@@ -3775,8 +3779,6 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
37753779 }
37763780
37773781 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;
37803782 const newEntries = [...activatedNow]
37813783 .sort((a, b) => sortedEntries.indexOf(a) - sortedEntries.indexOf(b));
37823784 let newContent = '';
@@ -3826,7 +3828,6 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
38263828 } else {
38273829 console.debug(`[WI] budget of ${budget} reached, stopping after ${allActivatedEntries.size} entries`);
38283830 }
3829- scanState = scan_state.NONE;
38303831 token_budget_overflowed = true;
38313832 break;
38323833 }
@@ -3836,48 +3837,48 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
38363837 }
38373838
38383839 const successfulNewEntries = newEntries.filter(x => !failedProbabilityChecks.has(x));
3840+ const successfulNewEntriesForRecursion = successfulNewEntries.filter(x => !x.preventRecursion);
38393841
38403842 if (!newEntries.length) {
38413843 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) {
38463845 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);
38563848 }
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;
38603853 }
38613854
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);
38643857 if (world_info_min_activations > 0!nextScanState && (allActivatedEntries.size!token_budget_overflowed <&& world_info_min_activations)minActivationsNotSatisfied) {
38653858 console.debug('[WI] --- MIN ACTIVATIONS CHECK ---');
38663859
38673860 let over_max = (
38683861 world_info_min_activations_depth_max > 0 &&
38693862 buffer.getDepth() > world_info_min_activations_depth_max
38703863 ) || (buffer.getDepth() > chat.length);
38713864
38723865 if (!over_max) {
38733866 console.debug(`[WI] Min activations not reached (${allActivatedEntries.size}/${world_info_min_activations}), advancing depth to ${buffer.getDepth() + 1} and checking again`);
38743867 scanState nextScanState = scan_state.MIN_ACTIVATIONS; // loop
38753868 buffer.advanceScanPosition();
38763869 } else {
38773870 console.debug(`[WI] Min activations not reached (${allActivatedEntries.size}/${world_info_min_activations}), but reached on of depth. Stopping`);
3878- }
38793871 }
38803872 }
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+ }
38813882 }
38823883
38833884 console.debug('[WI] --- BUILDING PROMPT ---');