Merge pull request #3701 from bmen25124/exports_for_chat_rebuild New exported methods

f245c48b170367606c1ed2063315dcb89c1e347f

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

Signed
5 files changed, +53 -23Ignore whitespace
public/scripts/instruct-mode.js+9 -6
@@ -398,23 +398,26 @@ export function formatInstructModeChat(name, mes, isUser, isNarrator, forceAvata
398398/**
399399 * Formats instruct mode system prompt.
400400 * @param {string} systemPrompt System prompt string.
401+ * @param {InstructSettings} customInstruct Custom instruct mode settings.
401402 * @returns {string} Formatted instruct mode system prompt.
402403 */
403404export function formatInstructModeSystemPrompt(systemPrompt, customInstruct = null) {
404405 if (!systemPrompt) {
405406 return '';
406407 }
407408
408- const separator = power_user.instruct.wrap ? '\n' : '';
409+ const instruct = structuredClone(customInstruct ?? power_user.instruct);
410+
411+ const separator = instruct.wrap ? '\n' : '';
409412
410413 if (power_user.instruct.system_sequence_prefix) {
411414 // TODO: Replace with a proper 'System' prompt entity name input
412415 const prefix = power_user.instruct.system_sequence_prefix.replace(/{{name}}/gi, 'System');
413416 systemPrompt = prefix + separator + systemPrompt;
414417 }
415418
416419 if (power_user.instruct.system_sequence_suffix) {
417420 systemPrompt = systemPrompt + separator + power_user.instruct.system_sequence_suffix;
418421 }
419422
420423 return systemPrompt;
public/scripts/openai.js+7 -5
@@ -705,16 +705,18 @@ export function parseExampleIntoIndividual(messageExampleString, appendNamesForG
705705 return result;
706706}
707707
708-function formatWorldInfo(value) {
708+export function formatWorldInfo(value, { wiFormat = null } = {}) {
709709 if (!value) {
710710 return '';
711711 }
712712
713- if (!oai_settings.wi_format.trim()) {
713+ const format = wiFormat ?? oai_settings.wi_format;
714+
715+ if (!format.trim()) {
714716 return value;
715717 }
716718
717719 return stringFormat(oai_settings.wi_formatformat, value);
718720}
719721
720722/**
@@ -952,7 +954,7 @@ async function populateDialogueExamples(prompts, chatCompletion, messageExamples
952954 * @param {number} position - Prompt position in the extensions object.
953955 * @returns {string|false} - The prompt position for prompt collection.
954956 */
955957export function getPromptPosition(position) {
956958 if (position == extension_prompt_types.BEFORE_PROMPT) {
957959 return 'start';
958960 }
@@ -969,7 +971,7 @@ function getPromptPosition(position) {
969971 * @param {number} role Role of the prompt.
970972 * @returns {string} Mapped role.
971973 */
972974export function getPromptRole(role) {
973975 switch (role) {
974976 case extension_prompt_roles.SYSTEM:
975977 return 'system';
public/scripts/power-user.js+10 -4
@@ -1985,15 +1985,21 @@ export function fuzzySearchGroups(searchValue, fuzzySearchCaches = null) {
19851985/**
19861986 * Renders a story string template with the given parameters.
19871987 * @param {object} params Template parameters.
1988+ * @param {object} [options] Additional options.
1989+ * @param {string} [options.customStoryString] Custom story string template.
1990+ * @param {InstructSettings} [options.customInstructSettings] Custom instruct settings.
19881991 * @returns {string} The rendered story string.
19891992 */
1990-export function renderStoryString(params) {
1993+export function renderStoryString(params, { customStoryString = null, customInstructSettings = null } = {}) {
19911994 try {
1995+ const storyString = customStoryString ?? power_user.context.story_string;
1996+ const instructSettings = structuredClone(customInstructSettings ?? power_user.instruct);
1997+
19921998 // Validate and log possible warnings/errors
19931999 validateStoryString(power_user.context.story_stringstoryString, params);
19942000
19952001 // compile the story string template into a function, with no HTML escaping
19962002 const compiledTemplate = Handlebars.compile(power_user.context.story_stringstoryString, { noEscape: true });
19972003
19982004 // render the story string template with the given params
19992005 let output = compiledTemplate(params);
@@ -2006,7 +2012,7 @@ export function renderStoryString(params) {
20062012
20072013 // add a newline to the end of the story string if it doesn't have one
20082014 if (output.length > 0 && !output.endsWith('\n')) {
20092015 if (!power_user.instructinstructSettings.enabled || power_user.instructinstructSettings.wrap) {
20102016 output += '\n';
20112017 }
20122018 }
public/scripts/st-context.js+4 -1
@@ -49,6 +49,7 @@ import {
4949 clearChat,
5050 unshallowCharacter,
5151 deleteLastMessage,
52+ getCharacterCardFields,
5253} from '../script.js';
5354import {
5455 extension_settings,
@@ -78,7 +79,7 @@ import { ToolManager } from './tool-calling.js';
7879import { accountStorage } from './util/AccountStorage.js';
7980import { timestampToMoment, uuidv4 } from './utils.js';
8081import { getGlobalVariable, getLocalVariable, setGlobalVariable, setLocalVariable } from './variables.js';
8182import { convertCharacterBook, getWorldInfoPrompt, loadWorldInfo, saveWorldInfo, updateWorldInfoList } from './world-info.js';
8283import { ChatCompletionService, TextCompletionService } from './custom-request.js';
8384import { ConnectionManagerRequestService } from './extensions/shared.js';
8485import { updateReasoningUI, parseReasoningFromString } from './reasoning.js';
@@ -189,6 +190,7 @@ export function getContext() {
189190 textCompletionSettings: textgenerationwebui_settings,
190191 powerUserSettings: power_user,
191192 getCharacters,
193+ getCharacterCardFields,
192194 uuidv4,
193195 humanizedDateTime,
194196 updateMessageBlock,
@@ -207,6 +209,7 @@ export function getContext() {
207209 saveWorldInfo,
208210 updateWorldInfoList,
209211 convertCharacterBook,
212+ getWorldInfoPrompt,
210213 CONNECT_API_MAP,
211214 getTextGenServer,
212215 extractMessageFromData,
public/scripts/world-info.js+23 -7
@@ -753,10 +753,17 @@ export const worldInfoCache = new StructuredCloneMap({ cloneOnGet: true, cloneOn
753753
754754/**
755755 * Gets the world info based on chat messages.
756756 * @param {string[]} chat - The chat messages to scan, in reverse order.
757757 * @param {number} maxContext - The maximum context size of the generation.
758758 * @param {boolean} isDryRun - If true, the function will not emit any events.
759- * @typedef {{worldInfoString: string, worldInfoBefore: string, worldInfoAfter: string, worldInfoExamples: any[], worldInfoDepth: any[]}} WIPromptResult
759+ * @typedef {object} WIPromptResult
760+ * @property {string} worldInfoString - Complete world info string
761+ * @property {string} worldInfoBefore - World info that goes before the prompt
762+ * @property {string} worldInfoAfter - World info that goes after the prompt
763+ * @property {Array} worldInfoExamples - Array of example entries
764+ * @property {Array} worldInfoDepth - Array of depth entries
765+ * @property {Array} anBefore - Array of entries before Author's Note
766+ * @property {Array} anAfter - Array of entries after Author's Note
760767 * @returns {Promise<WIPromptResult>} The world info string and depth.
761768 */
762769export async function getWorldInfoPrompt(chat, maxContext, isDryRun) {
@@ -778,6 +785,8 @@ export async function getWorldInfoPrompt(chat, maxContext, isDryRun) {
778785 worldInfoAfter,
779786 worldInfoExamples: activatedWorldInfo.EMEntries ?? [],
780787 worldInfoDepth: activatedWorldInfo.WIDepthEntries ?? [],
788+ anBefore: activatedWorldInfo.ANBeforeEntries ?? [],
789+ anAfter: activatedWorldInfo.ANAfterEntries ?? [],
781790 };
782791}
783792
@@ -3862,7 +3871,14 @@ function parseDecorators(content) {
38623871 * @param {string[]} chat The chat messages to scan, in reverse order.
38633872 * @param {number} maxContext The maximum context size of the generation.
38643873 * @param {boolean} isDryRun Whether to perform a dry run.
3865- * @typedef {{ worldInfoBefore: string, worldInfoAfter: string, EMEntries: any[], WIDepthEntries: any[], allActivatedEntries: Set<any> }} WIActivated
3874+ * @typedef {object} WIActivated
3875+ * @property {string} worldInfoBefore The world info before the chat.
3876+ * @property {string} worldInfoAfter The world info after the chat.
3877+ * @property {any[]} EMEntries The entries for examples.
3878+ * @property {any[]} WIDepthEntries The depth entries.
3879+ * @property {any[]} ANBeforeEntries The entries before Author's Note.
3880+ * @property {any[]} ANAfterEntries The entries after Author's Note.
3881+ * @property {Set<any>} allActivatedEntries All entries.
38663882 * @returns {Promise<WIActivated>} The world info activated.
38673883 */
38683884export async function checkWorldInfo(chat, maxContext, isDryRun) {
@@ -3906,7 +3922,7 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) {
39063922 timedEffects.checkTimedEffects();
39073923
39083924 if (sortedEntries.length === 0) {
39093925 return { worldInfoBefore: '', worldInfoAfter: '', WIDepthEntries: [], EMEntries: [], ANBeforeEntries: [], ANAfterEntries: [], allActivatedEntries: new Set() };
39103926 }
39113927
39123928 /** @type {number[]} Represents the delay levels for entries that are delayed until recursion */
@@ -4355,7 +4371,7 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) {
43554371 console.log(`[WI] ${isDryRun ? 'Hypothetically adding' : 'Adding'} ${allActivatedEntries.size} entries to prompt`, Array.from(allActivatedEntries.values()));
43564372 console.debug(`[WI] --- DONE${isDryRun ? ' (DRY RUN)' : ''} ---`);
43574373
43584374 return { worldInfoBefore, worldInfoAfter, EMEntries, WIDepthEntries, ANBeforeEntries: ANTopEntries, ANAfterEntries: ANBottomEntries, allActivatedEntries: new Set(allActivatedEntries.values()) };
43594375}
43604376
43614377/**