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