Implement WI matching on global data

d2ffefd24c869ff73cafab1ee76b0f5c8d371b5e

Crow <bismuthglass@protonmail.com>

2 files changed, +41 -13Showing whitespace changes
public/script.js+2 -1
@@ -4143,7 +4143,8 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
41434143 // Make quiet prompt available for WIAN
41444144 setExtensionPrompt('QUIET_PROMPT', quiet_prompt || '', extension_prompt_types.IN_PROMPT, 0, true);
41454145 const chatForWI = coreChat.map(x => world_info_include_names ? `${x.name}: ${x.mes}` : x.mes).reverse();
4146- const { worldInfoString, worldInfoBefore, worldInfoAfter, worldInfoExamples, worldInfoDepth } = await getWorldInfoPrompt(chatForWI, this_max_context, dryRun);
4146+ // TODO: Build globalScanData
4147+ const { worldInfoString, worldInfoBefore, worldInfoAfter, worldInfoExamples, worldInfoDepth } = await getWorldInfoPrompt(null, chatForWI, this_max_context, dryRun);
41474148 setExtensionPrompt('QUIET_PROMPT', '', extension_prompt_types.IN_PROMPT, 0, true);
41484149
41494150 // Add message example WI
public/scripts/world-info.js+39 -12
@@ -156,6 +156,11 @@ class WorldInfoBuffer {
156156 static externalActivations = new Map();
157157
158158 /**
159+ * @type {WIGlobalScanData} Chat independent data to be scanned, such as persona and character descriptions
160+ */
161+ #globalScanDataBuffer = null;
162+
163+ /**
159164 * @type {string[]} Array of messages sorted by ascending depth
160165 */
161166 #depthBuffer = [];
@@ -182,10 +187,12 @@ class WorldInfoBuffer {
182187
183188 /**
184189 * Initialize the buffer with the given messages.
190+ * @param {WIGlobalScanData} globalScanData Chat independent context to be scanned
185191 * @param {string[]} messages Array of messages to add to the buffer
186192 */
187193 constructor(globalScanData, messages) {
188194 this.#initDepthBuffer(messages);
195+ this.#globalScanDataBuffer = globalScanData;
189196 }
190197
191198 /**
@@ -242,6 +249,25 @@ class WorldInfoBuffer {
242249 const JOINER = '\n' + MATCHER;
243250 let result = MATCHER + this.#depthBuffer.slice(this.#startDepth, depth).join(JOINER);
244251
252+ if (entry.matchPersonaDescription) {
253+ result += JOINER + this.#globalScanDataBuffer.personaDescription;
254+ }
255+ if (entry.matchCharacterDescription) {
256+ result += JOINER + this.#globalScanDataBuffer.characterDescription;
257+ }
258+ if (entry.matchCharacterPersonality) {
259+ result += JOINER + this.#globalScanDataBuffer.characterPersonality;
260+ }
261+ if (entry.matchCharacterNote) {
262+ result += JOINER + this.#globalScanDataBuffer.characterNote;
263+ }
264+ if (entry.matchScenario) {
265+ result += JOINER + this.#globalScanDataBuffer.scenario;
266+ }
267+ if (entry.matchCreatorNotes) {
268+ result += JOINER + this.#globalScanDataBuffer.creatorNotes;
269+ }
270+
245271 if (this.#injectBuffer.length > 0) {
246272 result += JOINER + this.#injectBuffer.join(JOINER);
247273 }
@@ -770,6 +796,7 @@ export const worldInfoCache = new StructuredCloneMap({ cloneOnGet: true, cloneOn
770796
771797/**
772798 * Gets the world info based on chat messages.
799+ * @param {WIGlobalScanData} globalScanData Chat independent context to be scanned
773800 * @param {string[]} chat - The chat messages to scan, in reverse order.
774801 * @param {number} maxContext - The maximum context size of the generation.
775802 * @param {boolean} isDryRun - If true, the function will not emit any events.
@@ -783,10 +810,10 @@ export const worldInfoCache = new StructuredCloneMap({ cloneOnGet: true, cloneOn
783810 * @property {Array} anAfter - Array of entries after Author's Note
784811 * @returns {Promise<WIPromptResult>} The world info string and depth.
785812 */
786813export async function getWorldInfoPrompt(globalScanData, chat, maxContext, isDryRun) {
787814 let worldInfoString = '', worldInfoBefore = '', worldInfoAfter = '';
788815
789816 const activatedWorldInfo = await checkWorldInfo(globalScanData, chat, maxContext, isDryRun);
790817 worldInfoBefore = activatedWorldInfo.worldInfoBefore;
791818 worldInfoAfter = activatedWorldInfo.worldInfoAfter;
792819 worldInfoString = worldInfoBefore + worldInfoAfter;
@@ -3333,17 +3360,17 @@ export async function getWorldEntry(name, data, entry) {
33333360
33343361 function handleOptionalSelect(name) {
33353362 const key = originalWIDataKeyMap[name];
33363363 const selectElemcheckBoxElem = template.find(`selectinput[type="checkbox"][name="${name}"]`);
33373364 selectElemcheckBoxElem.data('uid', entry.uid);
33383365 selectElemcheckBoxElem.on('inputchange', async function () {
33393366 const uid = $(this).data('uid');
33403367 const valueisChecked = $(this).valis(':checked');
33413368
33423369 data.entries[uid][name] = value === 'null' ? null : value === 'true'isChecked;
33433370 setWIOriginalDataValue(data, uid, key, data.entries[uid][name]);
33443371 await saveWorldInfo(name, data);
33453372 });
3346- selectElem.val((entry[name] === null || entry[name] === undefined) ? 'null' : entry[name] ? 'true' : 'false').trigger('input');
3373+ checkBoxElem.prop('checked', !!entry[name]).trigger('change');
33473374 }
33483375
33493376 handleOptionalSelect("matchPersonaDescription");
@@ -4020,7 +4047,7 @@ function parseDecorators(content) {
40204047
40214048/**
40224049 * Performs a scan on the chat and returns the world info activated.
40234050 * @param {string[]WIGlobalScanData} chatglobalScanData TheChat chatindependent messagescontext to scan, in reversebe order.scanned
40244051 * @param {number} maxContext The maximum context size of the generation.
40254052 * @param {boolean} isDryRun Whether to perform a dry run.
40264053 * @typedef {object} WIActivated
@@ -4033,9 +4060,9 @@ function parseDecorators(content) {
40334060 * @property {Set<any>} allActivatedEntries All entries.
40344061 * @returns {Promise<WIActivated>} The world info activated.
40354062 */
40364063export async function checkWorldInfo(globalScanData, chat, maxContext, isDryRun) {
40374064 const context = getContext();
40384065 const buffer = new WorldInfoBuffer(globalScanData, chat);
40394066
40404067 console.debug(`[WI] --- START WI SCAN (on ${chat.length} messages)${isDryRun ? ' (DRY RUN)' : ''} ---`);
40414068