#2467 Fix min activations for non-recursable entries

df67a7cdc49f11ea682013ccf0e185e8f8c3a181

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +33 -10Ignore whitespace
public/scripts/world-info.js+33 -10
@@ -50,6 +50,24 @@ const world_info_logic = {
50 AND_ALL: 3,50 AND_ALL: 3,
51};51};
5252
53/**
54 * @enum {number} Possible states of the WI evaluation
55 */
56const scan_state = {
57 /**
58 * The scan will be stopped.
59 */
60 NONE: 0,
61 /**
62 * The scan is triggered by a recursion step. Initial state.
63 */
64 RECURSION: 1,
65 /**
66 * The scan is triggered by a min activations depth skew.
67 */
68 MIN_ACTIVATIONS: 2,
69};
70
53const WI_ENTRY_EDIT_TEMPLATE = $('#entry_edit_template .world_entry');71const WI_ENTRY_EDIT_TEMPLATE = $('#entry_edit_template .world_entry');
5472
55let world_info = {};73let world_info = {};
@@ -3508,7 +3526,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
3508 }3526 }
3509 }3527 }
35103528
3511 let needsToScan = true;3529 let scanState = scan_state.RECURSION;
3512 let token_budget_overflowed = false;3530 let token_budget_overflowed = false;
3513 let count = 0;3531 let count = 0;
3514 let allActivatedEntries = new Set();3532 let allActivatedEntries = new Set();
@@ -3532,7 +3550,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
3532 return { worldInfoBefore: '', worldInfoAfter: '', WIDepthEntries: [], EMEntries: [], allActivatedEntries: new Set() };3550 return { worldInfoBefore: '', worldInfoAfter: '', WIDepthEntries: [], EMEntries: [], allActivatedEntries: new Set() };
3533 }3551 }
35343552
3535 while (needsToScan) {3553 while (scanState) {
3536 // Track how many times the loop has run3554 // Track how many times the loop has run
3537 count++;3555 count++;
35383556
@@ -3587,7 +3605,12 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
3587 continue;3605 continue;
3588 }3606 }
35893607
3590 if (allActivatedEntries.has(entry) || entry.disable == true || (count > 1 && world_info_recursive && entry.excludeRecursion) || (count == 1 && entry.delayUntilRecursion)) {3608 if (allActivatedEntries.has(entry) || entry.disable == true) {
3609 continue;
3610 }
3611
3612 // Only use checks for recursion flags if the scan step was activated by recursion
3613 if (scanState === scan_state.RECURSION && ((count > 1 && world_info_recursive && entry.excludeRecursion) || (count == 1 && entry.delayUntilRecursion))) {
3591 continue;3614 continue;
3592 }3615 }
35933616
@@ -3665,7 +3688,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
3665 }3688 }
3666 }3689 }
36673690
3668 needsToScan = world_info_recursive && activatedNow.size > 0;3691 scanState = world_info_recursive && activatedNow.size > 0 ? scan_state.RECURSION : scan_state.NONE;
3669 const newEntries = [...activatedNow]3692 const newEntries = [...activatedNow]
3670 .sort((a, b) => sortedEntries.indexOf(a) - sortedEntries.indexOf(b));3693 .sort((a, b) => sortedEntries.indexOf(a) - sortedEntries.indexOf(b));
3671 let newContent = '';3694 let newContent = '';
@@ -3697,7 +3720,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
3697 console.log('Alerting');3720 console.log('Alerting');
3698 toastr.warning(`World info budget reached after ${allActivatedEntries.size} entries.`, 'World Info');3721 toastr.warning(`World info budget reached after ${allActivatedEntries.size} entries.`, 'World Info');
3699 }3722 }
3700 needsToScan = false;3723 scanState = scan_state.NONE;
3701 token_budget_overflowed = true;3724 token_budget_overflowed = true;
3702 break;3725 break;
3703 }3726 }
@@ -3710,15 +3733,15 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
37103733
3711 if ((probabilityChecksAfter - probabilityChecksBefore) === activatedNow.size) {3734 if ((probabilityChecksAfter - probabilityChecksBefore) === activatedNow.size) {
3712 console.debug('WI probability checks failed for all activated entries, stopping');3735 console.debug('WI probability checks failed for all activated entries, stopping');
3713 needsToScan = false;3736 scanState = scan_state.NONE;
3714 }3737 }
37153738
3716 if (newEntries.length === 0) {3739 if (newEntries.length === 0) {
3717 console.debug('No new entries activated, stopping');3740 console.debug('No new entries activated, stopping');
3718 needsToScan = false;3741 scanState = scan_state.NONE;
3719 }3742 }
37203743
3721 if (needsToScan) {3744 if (scanState) {
3722 const text = newEntries3745 const text = newEntries
3723 .filter(x => !failedProbabilityChecks.has(x))3746 .filter(x => !failedProbabilityChecks.has(x))
3724 .filter(x => !x.preventRecursion)3747 .filter(x => !x.preventRecursion)
@@ -3728,7 +3751,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
3728 }3751 }
37293752
3730 // world_info_min_activations3753 // world_info_min_activations
3731 if (!needsToScan && !token_budget_overflowed) {3754 if (!scanState && !token_budget_overflowed) {
3732 if (world_info_min_activations > 0 && (allActivatedEntries.size < world_info_min_activations)) {3755 if (world_info_min_activations > 0 && (allActivatedEntries.size < world_info_min_activations)) {
3733 let over_max = (3756 let over_max = (
3734 world_info_min_activations_depth_max > 0 &&3757 world_info_min_activations_depth_max > 0 &&
@@ -3736,7 +3759,7 @@ async function checkWorldInfo(chat, maxContext, isDryRun) {
3736 ) || (buffer.getDepth() > chat.length);3759 ) || (buffer.getDepth() > chat.length);
37373760
3738 if (!over_max) {3761 if (!over_max) {
3739 needsToScan = true; // loop3762 scanState = scan_state.MIN_ACTIVATIONS; // loop
3740 buffer.advanceScanPosition();3763 buffer.advanceScanPosition();
3741 }3764 }
3742 }3765 }