Merge pull request #2840 from SillyTavern/wi-preserve-hash Calculate entry hash before replacing macros
Signed| @@ -108,6 +108,7 @@ const KNOWN_DECORATORS = ['@@activate', '@@dont_activate']; | |||
| 108 | * @property {number} [cooldown] The cooldown of the entry | 108 | * @property {number} [cooldown] The cooldown of the entry |
| 109 | * @property {number} [delay] The delay of the entry | 109 | * @property {number} [delay] The delay of the entry |
| 110 | * @property {string[]} [decorators] Array of decorators for the entry | 110 | * @property {string[]} [decorators] Array of decorators for the entry |
| 111 | * @property {number} [hash] The hash of the entry | ||
| 111 | */ | 112 | */ |
| 112 | 113 | ||
| 113 | /** | 114 | /** |
| @@ -383,12 +384,6 @@ class WorldInfoBuffer { | |||
| 383 | */ | 384 | */ |
| 384 | class WorldInfoTimedEffects { | 385 | class WorldInfoTimedEffects { |
| 385 | /** | 386 | /** |
| 386 | * Cache for entry hashes. Uses weak map to avoid memory leaks. | ||
| 387 | * @type {WeakMap<WIScanEntry, number>} | ||
| 388 | */ | ||
| 389 | #entryHashCache = new WeakMap(); | ||
| 390 | |||
| 391 | /** | ||
| 392 | * Array of chat messages. | 387 | * Array of chat messages. |
| 393 | * @type {string[]} | 388 | * @type {string[]} |
| 394 | */ | 389 | */ |
| @@ -485,13 +480,7 @@ class WorldInfoTimedEffects { | |||
| 485 | * @returns {number} String hash | 480 | * @returns {number} String hash |
| 486 | */ | 481 | */ |
| 487 | #getEntryHash(entry) { | 482 | #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; | ||
| 495 | } | 484 | } |
| 496 | 485 | ||
| 497 | /** | 486 | /** |
| @@ -3603,10 +3592,13 @@ export async function getSortedEntries() { | |||
| 3603 | // Chat lore always goes first | 3592 | // Chat lore always goes first |
| 3604 | entries = [...chatLore.sort(sortFn), ...entries]; | 3593 | entries = [...chatLore.sort(sortFn), ...entries]; |
| 3605 | 3594 | ||
| 3606 | // Parse decorators | 3595 | // Calculate hash and parse decorators. Split maps to preserve old hashes. |
| 3607 | entries = entries.map((entry) => { | 3596 | entries = entries.map((entry) => { |
| 3608 | const [decorators, content] = parseDecorators(entry.content || ''); | 3597 | const [decorators, content] = parseDecorators(entry.content || ''); |
| 3609 | return { ...entry, decorators, content }; | 3598 | return { ...entry, decorators, content }; |
| 3599 | }).map((entry) => { | ||
| 3600 | const hash = getStringHash(JSON.stringify(entry)); | ||
| 3601 | return { ...entry, hash }; | ||
| 3610 | }); | 3602 | }); |
| 3611 | 3603 | ||
| 3612 | 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)); | 3604 | 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)); |