New exports
| @@ -398,23 +398,26 @@ export function formatInstructModeChat(name, mes, isUser, isNarrator, forceAvata | ||
| 398 | 398 | /** |
| 399 | 399 | * Formats instruct mode system prompt. |
| 400 | 400 | * @param {string} systemPrompt System prompt string. |
| 401 | + * @param {InstructSettings} customInstruct Custom instruct mode settings. | |
| 401 | 402 | * @returns {string} Formatted instruct mode system prompt. |
| 402 | 403 | */ |
| 403 | 404 | export function formatInstructModeSystemPrompt(systemPrompt, customInstruct = null) { |
| 404 | 405 | if (!systemPrompt) { |
| 405 | 406 | return ''; |
| 406 | 407 | } |
| 407 | 408 | |
| 408 | - const separator = power_user.instruct.wrap ? '\n' : ''; | |
| 409 | + const instruct = customInstruct ?? power_user.instruct; | |
| 410 | + | |
| 411 | + const separator = instruct.wrap ? '\n' : ''; | |
| 409 | 412 | |
| 410 | 413 | if (power_user.instruct.system_sequence_prefix) { |
| 411 | 414 | // TODO: Replace with a proper 'System' prompt entity name input |
| 412 | 415 | const prefix = power_user.instruct.system_sequence_prefix.replace(/{{name}}/gi, 'System'); |
| 413 | 416 | systemPrompt = prefix + separator + systemPrompt; |
| 414 | 417 | } |
| 415 | 418 | |
| 416 | 419 | if (power_user.instruct.system_sequence_suffix) { |
| 417 | 420 | systemPrompt = systemPrompt + separator + power_user.instruct.system_sequence_suffix; |
| 418 | 421 | } |
| 419 | 422 | |
| 420 | 423 | return systemPrompt; |
| @@ -705,16 +705,18 @@ export function parseExampleIntoIndividual(messageExampleString, appendNamesForG | ||
| 705 | 705 | return result; |
| 706 | 706 | } |
| 707 | 707 | |
| 708 | -function formatWorldInfo(value) { | |
| 708 | +export function formatWorldInfo(value, { wiFormat = null } = {}) { | |
| 709 | 709 | if (!value) { |
| 710 | 710 | return ''; |
| 711 | 711 | } |
| 712 | 712 | |
| 713 | - if (!oai_settings.wi_format.trim()) { | |
| 713 | + const format = wiFormat || oai_settings.wi_format; | |
| 714 | + | |
| 715 | + if (!format.trim()) { | |
| 714 | 716 | return value; |
| 715 | 717 | } |
| 716 | 718 | |
| 717 | 719 | return stringFormat(oai_settings.wi_formatformat, value); |
| 718 | 720 | } |
| 719 | 721 | |
| 720 | 722 | /** |
| @@ -952,7 +954,7 @@ async function populateDialogueExamples(prompts, chatCompletion, messageExamples | ||
| 952 | 954 | * @param {number} position - Prompt position in the extensions object. |
| 953 | 955 | * @returns {string|false} - The prompt position for prompt collection. |
| 954 | 956 | */ |
| 955 | 957 | export function getPromptPosition(position) { |
| 956 | 958 | if (position == extension_prompt_types.BEFORE_PROMPT) { |
| 957 | 959 | return 'start'; |
| 958 | 960 | } |
| @@ -969,7 +971,7 @@ function getPromptPosition(position) { | ||
| 969 | 971 | * @param {number} role Role of the prompt. |
| 970 | 972 | * @returns {string} Mapped role. |
| 971 | 973 | */ |
| 972 | 974 | export function getPromptRole(role) { |
| 973 | 975 | switch (role) { |
| 974 | 976 | case extension_prompt_roles.SYSTEM: |
| 975 | 977 | return 'system'; |
| @@ -1985,15 +1985,21 @@ export function fuzzySearchGroups(searchValue, fuzzySearchCaches = null) { | ||
| 1985 | 1985 | /** |
| 1986 | 1986 | * Renders a story string template with the given parameters. |
| 1987 | 1987 | * @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. | |
| 1988 | 1991 | * @returns {string} The rendered story string. |
| 1989 | 1992 | */ |
| 1990 | -export function renderStoryString(params) { | |
| 1993 | +export function renderStoryString(params, { customStoryString = null, customInstructSettings = null } = {}) { | |
| 1991 | 1994 | try { |
| 1995 | + const storyString = customStoryString ?? power_user.context.story_string; | |
| 1996 | + const instructSettings = customInstructSettings ?? power_user.instruct; | |
| 1997 | + | |
| 1992 | 1998 | // Validate and log possible warnings/errors |
| 1993 | 1999 | validateStoryString(power_user.context.story_stringstoryString, params); |
| 1994 | 2000 | |
| 1995 | 2001 | // compile the story string template into a function, with no HTML escaping |
| 1996 | 2002 | const compiledTemplate = Handlebars.compile(power_user.context.story_stringstoryString, { noEscape: true }); |
| 1997 | 2003 | |
| 1998 | 2004 | // render the story string template with the given params |
| 1999 | 2005 | let output = compiledTemplate(params); |
| @@ -2006,7 +2012,7 @@ export function renderStoryString(params) { | ||
| 2006 | 2012 | |
| 2007 | 2013 | // add a newline to the end of the story string if it doesn't have one |
| 2008 | 2014 | if (output.length > 0 && !output.endsWith('\n')) { |
| 2009 | 2015 | if (!power_user.instructinstructSettings.enabled || power_user.instructinstructSettings.wrap) { |
| 2010 | 2016 | output += '\n'; |
| 2011 | 2017 | } |
| 2012 | 2018 | } |
| @@ -49,6 +49,7 @@ import { | ||
| 49 | 49 | clearChat, |
| 50 | 50 | unshallowCharacter, |
| 51 | 51 | deleteLastMessage, |
| 52 | + getCharacterCardFields, | |
| 52 | 53 | } from '../script.js'; |
| 53 | 54 | import { |
| 54 | 55 | extension_settings, |
| @@ -78,7 +79,7 @@ import { ToolManager } from './tool-calling.js'; | ||
| 78 | 79 | import { accountStorage } from './util/AccountStorage.js'; |
| 79 | 80 | import { timestampToMoment, uuidv4 } from './utils.js'; |
| 80 | 81 | import { getGlobalVariable, getLocalVariable, setGlobalVariable, setLocalVariable } from './variables.js'; |
| 81 | 82 | import { convertCharacterBook, getWorldInfoPrompt, loadWorldInfo, saveWorldInfo, updateWorldInfoList } from './world-info.js'; |
| 82 | 83 | import { ChatCompletionService, TextCompletionService } from './custom-request.js'; |
| 83 | 84 | import { ConnectionManagerRequestService } from './extensions/shared.js'; |
| 84 | 85 | import { updateReasoningUI, parseReasoningFromString } from './reasoning.js'; |
| @@ -189,6 +190,7 @@ export function getContext() { | ||
| 189 | 190 | textCompletionSettings: textgenerationwebui_settings, |
| 190 | 191 | powerUserSettings: power_user, |
| 191 | 192 | getCharacters, |
| 193 | + getCharacterCardFields, | |
| 192 | 194 | uuidv4, |
| 193 | 195 | humanizedDateTime, |
| 194 | 196 | updateMessageBlock, |
| @@ -207,6 +209,7 @@ export function getContext() { | ||
| 207 | 209 | saveWorldInfo, |
| 208 | 210 | updateWorldInfoList, |
| 209 | 211 | convertCharacterBook, |
| 212 | + getWorldInfoPrompt, | |
| 210 | 213 | CONNECT_API_MAP, |
| 211 | 214 | getTextGenServer, |
| 212 | 215 | extractMessageFromData, |
| @@ -756,7 +756,7 @@ export const worldInfoCache = new StructuredCloneMap({ cloneOnGet: true, cloneOn | ||
| 756 | 756 | * @param {string[]} chat The chat messages to scan, in reverse order. |
| 757 | 757 | * @param {number} maxContext The maximum context size of the generation. |
| 758 | 758 | * @param {boolean} isDryRun If true, the function will not emit any events. |
| 759 | 759 | * @typedef {{worldInfoString: string, worldInfoBefore: string, worldInfoAfter: string, worldInfoExamples: any[], worldInfoDepth: any[], anBefore: any[], anAfter: any[]}} WIPromptResult |
| 760 | 760 | * @returns {Promise<WIPromptResult>} The world info string and depth. |
| 761 | 761 | */ |
| 762 | 762 | export async function getWorldInfoPrompt(chat, maxContext, isDryRun) { |
| @@ -778,6 +778,8 @@ export async function getWorldInfoPrompt(chat, maxContext, isDryRun) { | ||
| 778 | 778 | worldInfoAfter, |
| 779 | 779 | worldInfoExamples: activatedWorldInfo.EMEntries ?? [], |
| 780 | 780 | worldInfoDepth: activatedWorldInfo.WIDepthEntries ?? [], |
| 781 | + anBefore: activatedWorldInfo.ANBeforeEntries ?? [], | |
| 782 | + anAfter: activatedWorldInfo.ANAfterEntries ?? [], | |
| 781 | 783 | }; |
| 782 | 784 | } |
| 783 | 785 | |
| @@ -3862,7 +3864,7 @@ function parseDecorators(content) { | ||
| 3862 | 3864 | * @param {string[]} chat The chat messages to scan, in reverse order. |
| 3863 | 3865 | * @param {number} maxContext The maximum context size of the generation. |
| 3864 | 3866 | * @param {boolean} isDryRun Whether to perform a dry run. |
| 3865 | 3867 | * @typedef {{ worldInfoBefore: string, worldInfoAfter: string, EMEntries: any[], WIDepthEntries: any[], ANBeforeEntries: any[], ANAfterEntries: any[], allActivatedEntries: Set<any> }} WIActivated |
| 3866 | 3868 | * @returns {Promise<WIActivated>} The world info activated. |
| 3867 | 3869 | */ |
| 3868 | 3870 | export async function checkWorldInfo(chat, maxContext, isDryRun) { |
| @@ -3906,7 +3908,7 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) { | ||
| 3906 | 3908 | timedEffects.checkTimedEffects(); |
| 3907 | 3909 | |
| 3908 | 3910 | if (sortedEntries.length === 0) { |
| 3909 | 3911 | return { worldInfoBefore: '', worldInfoAfter: '', WIDepthEntries: [], EMEntries: [], ANBeforeEntries: [], ANAfterEntries: [], allActivatedEntries: new Set() }; |
| 3910 | 3912 | } |
| 3911 | 3913 | |
| 3912 | 3914 | /** @type {number[]} Represents the delay levels for entries that are delayed until recursion */ |
| @@ -4355,7 +4357,7 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) { | ||
| 4355 | 4357 | console.log(`[WI] ${isDryRun ? 'Hypothetically adding' : 'Adding'} ${allActivatedEntries.size} entries to prompt`, Array.from(allActivatedEntries.values())); |
| 4356 | 4358 | console.debug(`[WI] --- DONE${isDryRun ? ' (DRY RUN)' : ''} ---`); |
| 4357 | 4359 | |
| 4358 | 4360 | return { worldInfoBefore, worldInfoAfter, EMEntries, WIDepthEntries, ANBeforeEntries: ANTopEntries, ANAfterEntries: ANBottomEntries, allActivatedEntries: new Set(allActivatedEntries.values()) }; |
| 4359 | 4361 | } |
| 4360 | 4362 | |
| 4361 | 4363 | /** |