Add new WORLDINFO_SCAN_DONE event with mutable state for extensions (#4797) * chore: Add WORLDINFO_SCAN_DONE event with mutable state for extensions * Fix incorrect scan state reference in world info recursion logic * Fix typo in world info scan state property name: 'sucessful' → 'successful' (am i stupid)

a11abc42737015b27be87cb2dfc5c9f63bc272ee

Wolfsblvt <wolfsblvt@gmail.com>

Signed
2 files changed, +41 -0Showing whitespace changes
public/scripts/events.js+1 -0
@@ -91,6 +91,7 @@ export const event_types = {
9191 PRESET_RENAMED_BEFORE: 'preset_renamed_before',
9292 MAIN_API_CHANGED: 'main_api_changed',
9393 WORLDINFO_ENTRIES_LOADED: 'worldinfo_entries_loaded',
94+ WORLDINFO_SCAN_DONE: 'worldinfo_scan_done',
9495 MEDIA_ATTACHMENT_DELETED: 'media_attachment_deleted',
9596};
9697
public/scripts/world-info.js+40 -0
@@ -4845,6 +4845,7 @@ export async function checkWorldInfo(chat, maxContext, isDryRun, globalScanData
48454845 }
48464846
48474847 // Final check if we should really continue scan, and extend the current WI recurse buffer
4848+ const curScanState = scanState;
48484849 scanState = nextScanState;
48494850 if (scanState) {
48504851 const text = successfulNewEntriesForRecursion
@@ -4856,6 +4857,45 @@ export async function checkWorldInfo(chat, maxContext, isDryRun, globalScanData
48564857 } else {
48574858 logNextState('[WI] Scan done. No new entries to prompt. Stopping.');
48584859 }
4860+
4861+ // Fire an event after each scan loop, so extensions can hook into the current scanning state
4862+ const args = {
4863+ state: {
4864+ current: curScanState,
4865+ next: scanState,
4866+ loopCount: count,
4867+ },
4868+ new: {
4869+ all: newEntries,
4870+ successful: successfulNewEntries,
4871+ },
4872+ activated: {
4873+ entries: allActivatedEntries,
4874+ text: allActivatedText,
4875+ },
4876+ sortedEntries,
4877+ recursionDelay: {
4878+ availableLevels: availableRecursionDelayLevels,
4879+ currentLevel: currentRecursionDelayLevel,
4880+ },
4881+ budget: {
4882+ current: budget,
4883+ overflowed: token_budget_overflowed,
4884+ },
4885+ timedEffects,
4886+ };
4887+ await eventSource.emit(event_types.WORLDINFO_SCAN_DONE, args);
4888+
4889+ // Some fields are allowed to be changed by listeners, those will be handled here manually. They can be updated via changed the args from the listeners.
4890+ // Any array provided directly can be modified by updating it's elements, adding or removing elements. This has to be done consistently.
4891+ if (args.state.next !== scanState) {
4892+ logNextState('[WI] Scan state changed from', scanState, 'to', args.state.next);
4893+ scanState = args.state.next;
4894+ }
4895+ allActivatedText = args.activated.text;
4896+ currentRecursionDelayLevel = args.recursionDelay.currentLevel;
4897+ budget = args.budget.current;
4898+ token_budget_overflowed = args.budget.overflowed;
48594899 }
48604900
48614901 console.debug('[WI] --- BUILDING PROMPT ---');