From 935a3d6c3563a091aba28fe1eb698ad9432231a8 Mon Sep 17 00:00:00 2001 From: Cohee Date: Wed, 5 Jul 2023 20:46:30 +0300 Subject: [PATCH] Clone WI entries before returning them to evaluator --- public/script.js | 2 +- public/scripts/utils.js | 4 ++++ public/scripts/world-info.js | 5 +++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/public/script.js b/public/script.js index f0ff58d16..cf6ec849c 100644 --- a/public/script.js +++ b/public/script.js @@ -1501,7 +1501,7 @@ function substituteParams(content, _name1, _name2, _original) { } function randomReplace(input, emptyListPlaceholder = '') { - const randomPattern = /{{random:([^}]+)}}/g; + const randomPattern = /{{random:([^}]+)}}/gi; return input.replace(randomPattern, (match, listString) => { const list = listString.split(',').map(item => item.trim()).filter(item => item.length > 0); diff --git a/public/scripts/utils.js b/public/scripts/utils.js index e19b3f790..88a564b07 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -676,3 +676,7 @@ export function uuidv4() { return v.toString(16); }); } + +export function deepClone(obj) { + return JSON.parse(JSON.stringify(obj)); +} diff --git a/public/scripts/world-info.js b/public/scripts/world-info.js index c9ffca7df..42e4c2b07 100644 --- a/public/scripts/world-info.js +++ b/public/scripts/world-info.js @@ -1,5 +1,5 @@ import { saveSettings, callPopup, substituteParams, getTokenCount, getRequestHeaders, chat_metadata, this_chid, characters, saveCharacterDebounced, menu_type } from "../script.js"; -import { download, debounce, initScrollHeight, resetScrollHeight, parseJsonFile, extractDataFromPng, getFileBuffer, delay, getCharaFilename } from "./utils.js"; +import { download, debounce, initScrollHeight, resetScrollHeight, parseJsonFile, extractDataFromPng, getFileBuffer, delay, getCharaFilename, deepClone } from "./utils.js"; import { getContext } from "./extensions.js"; import { NOTE_MODULE_NAME, metadata_keys, shouldWIAddPrompt } from "./extensions/floating-prompt/index.js"; import { registerSlashCommand } from "./slash-commands.js"; @@ -898,7 +898,8 @@ async function getSortedEntries() { console.debug(`Sorted ${entries.length} world lore entries using strategy ${world_info_character_strategy}`); - return entries; + // Need to deep clone the entries to avoid modifying the cached data + return deepClone(entries); } catch (e) { console.error(e);