Reduce nesting and simply WI check loop

This commit is contained in:
Wolfsblvt 2024-07-06 02:35:41 +02:00
parent 0b9431cd9a
commit 02989a9a78

View File

@ -3591,6 +3591,10 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
console.debug(`[WI] Loop #${count}. Search state`, Object.entries(scan_state).find(x => x[1] === scanState)); console.debug(`[WI] Loop #${count}. Search state`, Object.entries(scan_state).find(x => x[1] === scanState));
// Until decided otherwise, we set the loop to stop scanning after this
let nextScanState = scan_state.NONE;
// Loop and find all entries that can activate here
let activatedNow = new Set(); let activatedNow = new Set();
for (let entry of sortedEntries) { for (let entry of sortedEntries) {
@ -3775,8 +3779,6 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
} }
console.debug(`[WI] Search done. Found ${activatedNow.size} possible entries.`); console.debug(`[WI] Search done. Found ${activatedNow.size} possible entries.`);
scanState = world_info_recursive && activatedNow.size > 0 ? scan_state.RECURSION : scan_state.NONE;
const newEntries = [...activatedNow] const newEntries = [...activatedNow]
.sort((a, b) => sortedEntries.indexOf(a) - sortedEntries.indexOf(b)); .sort((a, b) => sortedEntries.indexOf(a) - sortedEntries.indexOf(b));
let newContent = ''; let newContent = '';
@ -3826,7 +3828,6 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
} else { } else {
console.debug(`[WI] budget of ${budget} reached, stopping after ${allActivatedEntries.size} entries`); console.debug(`[WI] budget of ${budget} reached, stopping after ${allActivatedEntries.size} entries`);
} }
scanState = scan_state.NONE;
token_budget_overflowed = true; token_budget_overflowed = true;
break; break;
} }
@ -3836,32 +3837,24 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
} }
const successfulNewEntries = newEntries.filter(x => !failedProbabilityChecks.has(x)); const successfulNewEntries = newEntries.filter(x => !failedProbabilityChecks.has(x));
const successfulNewEntriesForRecursion = successfulNewEntries.filter(x => !x.preventRecursion);
if (!newEntries.length) { if (!newEntries.length) {
console.debug('[WI] No new entries activated, stopping'); console.debug('[WI] No new entries activated, stopping');
scanState = scan_state.NONE; } else if (!successfulNewEntries.length) {
}
if (newEntries.length && !successfulNewEntries.length) {
console.debug('[WI] Probability checks failed for all activated entries, stopping'); console.debug('[WI] Probability checks failed for all activated entries, stopping');
scanState = scan_state.NONE; } else {
}
if (scanState) {
const text = successfulNewEntries
.filter(x => !x.preventRecursion)
.map(x => x.content).join('\n');
buffer.addRecurse(text);
allActivatedText = (text + '\n' + allActivatedText);
}
if (successfulNewEntries.length) {
console.debug(`[WI] Sucessfully activated ${successfulNewEntries.length} new entries to prompt. ${allActivatedEntries.size} total entries activated.`, successfulNewEntries); console.debug(`[WI] Sucessfully activated ${successfulNewEntries.length} new entries to prompt. ${allActivatedEntries.size} total entries activated.`, successfulNewEntries);
} }
// world_info_min_activations // After processing and rolling entries is done, see if we should continue with recursion
if (!scanState && !token_budget_overflowed) { if (world_info_recursive && !token_budget_overflowed && successfulNewEntriesForRecursion.length) {
if (world_info_min_activations > 0 && (allActivatedEntries.size < world_info_min_activations)) { nextScanState = scan_state.RECURSION;
}
// If scanning is planned to stop, but min activations is set and not satisfied, check if we should continue
const minActivationsNotSatisfied = world_info_min_activations > 0 && (allActivatedEntries.size < world_info_min_activations);
if (!nextScanState && !token_budget_overflowed && minActivationsNotSatisfied) {
console.debug('[WI] --- MIN ACTIVATIONS CHECK ---'); console.debug('[WI] --- MIN ACTIVATIONS CHECK ---');
let over_max = ( let over_max = (
@ -3871,12 +3864,20 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
if (!over_max) { if (!over_max) {
console.debug(`[WI] Min activations not reached (${allActivatedEntries.size}/${world_info_min_activations}), advancing depth to ${buffer.getDepth() + 1} and checking again`); console.debug(`[WI] Min activations not reached (${allActivatedEntries.size}/${world_info_min_activations}), advancing depth to ${buffer.getDepth() + 1} and checking again`);
scanState = scan_state.MIN_ACTIVATIONS; // loop nextScanState = scan_state.MIN_ACTIVATIONS; // loop
buffer.advanceScanPosition(); buffer.advanceScanPosition();
} else { } else {
console.debug(`[WI] Min activations not reached (${allActivatedEntries.size}/${world_info_min_activations}), but reached on of depth. Stopping`); console.debug(`[WI] Min activations not reached (${allActivatedEntries.size}/${world_info_min_activations}), but reached on of depth. Stopping`);
} }
} }
// Final check if we should really continue scan, and extend the current WI recurse buffer
scanState = nextScanState;
if (scanState) {
const text = successfulNewEntriesForRecursion
.map(x => x.content).join('\n');
buffer.addRecurse(text);
allActivatedText = (text + '\n' + allActivatedText);
} }
} }