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 {
400 #entries = [];400 #entries = [];
401401
402 /**402 /**
403 * Is this a dry run?
404 * @type {boolean}
405 */
406 #isDryRun = false;
407
408 /**
403 * Buffer for active timed effects.409 * Buffer for active timed effects.
404 * @type {Record<TimedEffectType, WIScanEntry[]>}410 * @type {Record<TimedEffectType, WIScanEntry[]>}
405 */411 */
@@ -448,10 +454,12 @@ class WorldInfoTimedEffects {
448 * Initialize the timed effects with the given messages.454 * Initialize the timed effects with the given messages.
449 * @param {string[]} chat Array of chat messages455 * @param {string[]} chat Array of chat messages
450 * @param {WIScanEntry[]} entries Array of entries456 * @param {WIScanEntry[]} entries Array of entries
457 * @param {boolean} isDryRun Whether the operation is a dry run
451 */458 */
452 constructor(chat, entries) {459 constructor(chat, entries, isDryRun = false) {
453 this.#chat = chat;460 this.#chat = chat;
454 this.#entries = entries;461 this.#entries = entries;
462 this.#isDryRun = isDryRun;
455 this.#ensureChatMetadata();463 this.#ensureChatMetadata();
456 }464 }
457465
@@ -583,8 +591,10 @@ class WorldInfoTimedEffects {
583 * Checks for timed effects on chat messages.591 * Checks for timed effects on chat messages.
584 */592 */
585 checkTimedEffects() {593 checkTimedEffects() {
586 this.#checkTimedEffectOfType('sticky', this.#buffer.sticky, this.#onEnded.sticky.bind(this));594 if (!this.#isDryRun) {
587 this.#checkTimedEffectOfType('cooldown', this.#buffer.cooldown, this.#onEnded.cooldown.bind(this));595 this.#checkTimedEffectOfType('sticky', this.#buffer.sticky, this.#onEnded.sticky.bind(this));
596 this.#checkTimedEffectOfType('cooldown', this.#buffer.cooldown, this.#onEnded.cooldown.bind(this));
597 }
588 this.#checkDelayEffect(this.#buffer.delay);598 this.#checkDelayEffect(this.#buffer.delay);
589 }599 }
590600
@@ -629,6 +639,7 @@ class WorldInfoTimedEffects {
629 * @param {WIScanEntry[]} activatedEntries Entries that were activated639 * @param {WIScanEntry[]} activatedEntries Entries that were activated
630 */640 */
631 setTimedEffects(activatedEntries) {641 setTimedEffects(activatedEntries) {
642 if (this.#isDryRun) return;
632 for (const entry of activatedEntries) {643 for (const entry of activatedEntries) {
633 this.#setTimedEffectOfType('sticky', entry);644 this.#setTimedEffectOfType('sticky', entry);
634 this.#setTimedEffectOfType('cooldown', entry);645 this.#setTimedEffectOfType('cooldown', entry);
@@ -645,6 +656,9 @@ class WorldInfoTimedEffects {
645 if (!this.isValidEffectType(type)) {656 if (!this.isValidEffectType(type)) {
646 return;657 return;
647 }658 }
659 if (this.#isDryRun && type !== 'delay') {
660 return;
661 }
648662
649 const key = this.#getEntryKey(entry);663 const key = this.#getEntryKey(entry);
650 delete chat_metadata.timedWorldInfo[type][key];664 delete chat_metadata.timedWorldInfo[type][key];
@@ -3847,7 +3861,7 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) {
3847 const context = getContext();3861 const context = getContext();
3848 const buffer = new WorldInfoBuffer(chat);3862 const buffer = new WorldInfoBuffer(chat);
38493863
3850 console.debug(`[WI] --- START WI SCAN (on ${chat.length} messages) ---`);3864 console.debug(`[WI] --- START WI SCAN (on ${chat.length} messages)${isDryRun ? ' (DRY RUN)' : ''} ---`);
38513865
3852 // Combine the chat3866 // Combine the chat
38533867
@@ -3879,9 +3893,9 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) {
38793893
3880 console.debug(`[WI] Context size: ${maxContext}; WI budget: ${budget} (max% = ${world_info_budget}%, cap = ${world_info_budget_cap})`);3894 console.debug(`[WI] Context size: ${maxContext}; WI budget: ${budget} (max% = ${world_info_budget}%, cap = ${world_info_budget_cap})`);
3881 const sortedEntries = await getSortedEntries();3895 const sortedEntries = await getSortedEntries();
3882 const timedEffects = new WorldInfoTimedEffects(chat, sortedEntries);3896 const timedEffects = new WorldInfoTimedEffects(chat, sortedEntries, isDryRun);
38833897
3884 !isDryRun && timedEffects.checkTimedEffects();3898 timedEffects.checkTimedEffects();
38853899
3886 if (sortedEntries.length === 0) {3900 if (sortedEntries.length === 0) {
3887 return { worldInfoBefore: '', worldInfoAfter: '', WIDepthEntries: [], EMEntries: [], allActivatedEntries: new Set() };3901 return { worldInfoBefore: '', worldInfoAfter: '', WIDepthEntries: [], EMEntries: [], allActivatedEntries: new Set() };
@@ -4324,12 +4338,12 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) {
4324 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]);4338 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]);
4325 }4339 }
43264340
4327 !isDryRun && timedEffects.setTimedEffects(Array.from(allActivatedEntries.values()));4341 timedEffects.setTimedEffects(Array.from(allActivatedEntries.values()));
4328 buffer.resetExternalEffects();4342 buffer.resetExternalEffects();
4329 timedEffects.cleanUp();4343 timedEffects.cleanUp();
43304344
4331 console.log(`[WI] Adding ${allActivatedEntries.size} entries to prompt`, Array.from(allActivatedEntries.values()));4345 console.log(`[WI] ${isDryRun ? 'Hypothetically adding' : 'Adding'} ${allActivatedEntries.size} entries to prompt`, Array.from(allActivatedEntries.values()));
4332 console.debug('[WI] --- DONE ---');4346 console.debug(`[WI] --- DONE${isDryRun ? ' (DRY RUN)' : ''} ---`);
43334347
4334 return { worldInfoBefore, worldInfoAfter, EMEntries, WIDepthEntries, allActivatedEntries: new Set(allActivatedEntries.values()) };4348 return { worldInfoBefore, worldInfoAfter, EMEntries, WIDepthEntries, allActivatedEntries: new Set(allActivatedEntries.values()) };
4335}4349}