Merge pull request #3423 from SillyTavern/wi-dry-run-delays

WI dry run checks delay + logging
This commit is contained in:
Cohee 2025-02-04 17:44:28 +02:00 committed by GitHub
commit 7aa9b857c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -399,6 +399,12 @@ class WorldInfoTimedEffects {
*/
#entries = [];
/**
* Is this a dry run?
* @type {boolean}
*/
#isDryRun = false;
/**
* Buffer for active timed effects.
* @type {Record<TimedEffectType, WIScanEntry[]>}
@ -448,10 +454,12 @@ class WorldInfoTimedEffects {
* Initialize the timed effects with the given messages.
* @param {string[]} chat Array of chat messages
* @param {WIScanEntry[]} entries Array of entries
* @param {boolean} isDryRun Whether the operation is a dry run
*/
constructor(chat, entries) {
constructor(chat, entries, isDryRun = false) {
this.#chat = chat;
this.#entries = entries;
this.#isDryRun = isDryRun;
this.#ensureChatMetadata();
}
@ -583,8 +591,10 @@ class WorldInfoTimedEffects {
* Checks for timed effects on chat messages.
*/
checkTimedEffects() {
this.#checkTimedEffectOfType('sticky', this.#buffer.sticky, this.#onEnded.sticky.bind(this));
this.#checkTimedEffectOfType('cooldown', this.#buffer.cooldown, this.#onEnded.cooldown.bind(this));
if (!this.#isDryRun) {
this.#checkTimedEffectOfType('sticky', this.#buffer.sticky, this.#onEnded.sticky.bind(this));
this.#checkTimedEffectOfType('cooldown', this.#buffer.cooldown, this.#onEnded.cooldown.bind(this));
}
this.#checkDelayEffect(this.#buffer.delay);
}
@ -629,6 +639,7 @@ class WorldInfoTimedEffects {
* @param {WIScanEntry[]} activatedEntries Entries that were activated
*/
setTimedEffects(activatedEntries) {
if (this.#isDryRun) return;
for (const entry of activatedEntries) {
this.#setTimedEffectOfType('sticky', entry);
this.#setTimedEffectOfType('cooldown', entry);
@ -645,6 +656,9 @@ class WorldInfoTimedEffects {
if (!this.isValidEffectType(type)) {
return;
}
if (this.#isDryRun && type !== 'delay') {
return;
}
const key = this.#getEntryKey(entry);
delete chat_metadata.timedWorldInfo[type][key];
@ -3847,7 +3861,7 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) {
const context = getContext();
const buffer = new WorldInfoBuffer(chat);
console.debug(`[WI] --- START WI SCAN (on ${chat.length} messages) ---`);
console.debug(`[WI] --- START WI SCAN (on ${chat.length} messages)${isDryRun ? ' (DRY RUN)' : ''} ---`);
// Combine the chat
@ -3879,9 +3893,9 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) {
console.debug(`[WI] Context size: ${maxContext}; WI budget: ${budget} (max% = ${world_info_budget}%, cap = ${world_info_budget_cap})`);
const sortedEntries = await getSortedEntries();
const timedEffects = new WorldInfoTimedEffects(chat, sortedEntries);
const timedEffects = new WorldInfoTimedEffects(chat, sortedEntries, isDryRun);
!isDryRun && timedEffects.checkTimedEffects();
timedEffects.checkTimedEffects();
if (sortedEntries.length === 0) {
return { worldInfoBefore: '', worldInfoAfter: '', WIDepthEntries: [], EMEntries: [], allActivatedEntries: new Set() };
@ -4324,12 +4338,12 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) {
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]);
}
!isDryRun && timedEffects.setTimedEffects(Array.from(allActivatedEntries.values()));
timedEffects.setTimedEffects(Array.from(allActivatedEntries.values()));
buffer.resetExternalEffects();
timedEffects.cleanUp();
console.log(`[WI] Adding ${allActivatedEntries.size} entries to prompt`, Array.from(allActivatedEntries.values()));
console.debug('[WI] --- DONE ---');
console.log(`[WI] ${isDryRun ? 'Hypothetically adding' : 'Adding'} ${allActivatedEntries.size} entries to prompt`, Array.from(allActivatedEntries.values()));
console.debug(`[WI] --- DONE${isDryRun ? ' (DRY RUN)' : ''} ---`);
return { worldInfoBefore, worldInfoAfter, EMEntries, WIDepthEntries, allActivatedEntries: new Set(allActivatedEntries.values()) };
}