Merge pull request #3423 from SillyTavern/wi-dry-run-delays WI dry run checks delay + logging

7aa9b857c21f6cc38d8cd793390f54377be88c47

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

Signed
1 files changed, +23 -9Ignore whitespace
public/scripts/world-info.js+23 -9
@@ -400,6 +400,12 @@ class WorldInfoTimedEffects {
400400 #entries = [];
401401
402402 /**
403+ * Is this a dry run?
404+ * @type {boolean}
405+ */
406+ #isDryRun = false;
407+
408+ /**
403409 * Buffer for active timed effects.
404410 * @type {Record<TimedEffectType, WIScanEntry[]>}
405411 */
@@ -448,10 +454,12 @@ class WorldInfoTimedEffects {
448454 * Initialize the timed effects with the given messages.
449455 * @param {string[]} chat Array of chat messages
450456 * @param {WIScanEntry[]} entries Array of entries
457+ * @param {boolean} isDryRun Whether the operation is a dry run
451458 */
452459 constructor(chat, entries, isDryRun = false) {
453460 this.#chat = chat;
454461 this.#entries = entries;
462+ this.#isDryRun = isDryRun;
455463 this.#ensureChatMetadata();
456464 }
457465
@@ -583,8 +591,10 @@ class WorldInfoTimedEffects {
583591 * Checks for timed effects on chat messages.
584592 */
585593 checkTimedEffects() {
586- this.#checkTimedEffectOfType('sticky', this.#buffer.sticky, this.#onEnded.sticky.bind(this));
594+ if (!this.#isDryRun) {
587595 this.#checkTimedEffectOfType('cooldownsticky', this.#buffer.cooldownsticky, this.#onEnded.cooldownsticky.bind(this));
596+ this.#checkTimedEffectOfType('cooldown', this.#buffer.cooldown, this.#onEnded.cooldown.bind(this));
597+ }
588598 this.#checkDelayEffect(this.#buffer.delay);
589599 }
590600
@@ -629,6 +639,7 @@ class WorldInfoTimedEffects {
629639 * @param {WIScanEntry[]} activatedEntries Entries that were activated
630640 */
631641 setTimedEffects(activatedEntries) {
642+ if (this.#isDryRun) return;
632643 for (const entry of activatedEntries) {
633644 this.#setTimedEffectOfType('sticky', entry);
634645 this.#setTimedEffectOfType('cooldown', entry);
@@ -645,6 +656,9 @@ class WorldInfoTimedEffects {
645656 if (!this.isValidEffectType(type)) {
646657 return;
647658 }
659+ if (this.#isDryRun && type !== 'delay') {
660+ return;
661+ }
648662
649663 const key = this.#getEntryKey(entry);
650664 delete chat_metadata.timedWorldInfo[type][key];
@@ -3847,7 +3861,7 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) {
38473861 const context = getContext();
38483862 const buffer = new WorldInfoBuffer(chat);
38493863
38503864 console.debug(`[WI] --- START WI SCAN (on ${chat.length} messages)${isDryRun ? ' (DRY RUN)' : ''} ---`);
38513865
38523866 // Combine the chat
38533867
@@ -3879,9 +3893,9 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) {
38793893
38803894 console.debug(`[WI] Context size: ${maxContext}; WI budget: ${budget} (max% = ${world_info_budget}%, cap = ${world_info_budget_cap})`);
38813895 const sortedEntries = await getSortedEntries();
38823896 const timedEffects = new WorldInfoTimedEffects(chat, sortedEntries, isDryRun);
38833897
38843898 !isDryRun && timedEffects.checkTimedEffects();
38853899
38863900 if (sortedEntries.length === 0) {
38873901 return { worldInfoBefore: '', worldInfoAfter: '', WIDepthEntries: [], EMEntries: [], allActivatedEntries: new Set() };
@@ -4324,12 +4338,12 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) {
43244338 context.setExtensionPrompt(NOTE_MODULE_NAME, ANWithWI, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth], extension_settings.note.allowWIScan, chat_metadata[metadata_keys.role]);
43254339 }
43264340
43274341 !isDryRun && timedEffects.setTimedEffects(Array.from(allActivatedEntries.values()));
43284342 buffer.resetExternalEffects();
43294343 timedEffects.cleanUp();
43304344
43314345 console.log(`[WI] ${isDryRun ? 'Hypothetically adding' : 'Adding'} ${allActivatedEntries.size} entries to prompt`, Array.from(allActivatedEntries.values()));
43324346 console.debug('`[WI] --- DONE${isDryRun ? ' (DRY RUN)' : ''} ---'`);
43334347
43344348 return { worldInfoBefore, worldInfoAfter, EMEntries, WIDepthEntries, allActivatedEntries: new Set(allActivatedEntries.values()) };
43354349}