Merge pull request #2840 from SillyTavern/wi-preserve-hash Calculate entry hash before replacing macros

11549ed7373bc708a8f53ebc724561b47171ab2b

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

Signed
1 files changed, +6 -14Showing whitespace changes
public/scripts/world-info.js+6 -14
@@ -108,6 +108,7 @@ const KNOWN_DECORATORS = ['@@activate', '@@dont_activate'];
108108 * @property {number} [cooldown] The cooldown of the entry
109109 * @property {number} [delay] The delay of the entry
110110 * @property {string[]} [decorators] Array of decorators for the entry
111+ * @property {number} [hash] The hash of the entry
111112 */
112113
113114/**
@@ -383,12 +384,6 @@ class WorldInfoBuffer {
383384 */
384385class WorldInfoTimedEffects {
385386 /**
386- * Cache for entry hashes. Uses weak map to avoid memory leaks.
387- * @type {WeakMap<WIScanEntry, number>}
388- */
389- #entryHashCache = new WeakMap();
390-
391- /**
392387 * Array of chat messages.
393388 * @type {string[]}
394389 */
@@ -485,13 +480,7 @@ class WorldInfoTimedEffects {
485480 * @returns {number} String hash
486481 */
487482 #getEntryHash(entry) {
488- if (this.#entryHashCache.has(entry)) {
483+ return entry.hash;
489- return this.#entryHashCache.get(entry);
490- }
491-
492- const hash = getStringHash(JSON.stringify(entry));
493- this.#entryHashCache.set(entry, hash);
494- return hash;
495484 }
496485
497486 /**
@@ -3603,10 +3592,13 @@ export async function getSortedEntries() {
36033592 // Chat lore always goes first
36043593 entries = [...chatLore.sort(sortFn), ...entries];
36053594
3606- // Parse decorators
3595+ // Calculate hash and parse decorators. Split maps to preserve old hashes.
36073596 entries = entries.map((entry) => {
36083597 const [decorators, content] = parseDecorators(entry.content || '');
36093598 return { ...entry, decorators, content };
3599+ }).map((entry) => {
3600+ const hash = getStringHash(JSON.stringify(entry));
3601+ return { ...entry, hash };
36103602 });
36113603
36123604 console.debug(`[WI] Found ${entries.length} world lore entries. Sorted by strategy`, Object.entries(world_info_insertion_strategy).find((x) => x[1] === world_info_character_strategy));