New exports

This commit is contained in:
bmen25124
2025-03-16 23:44:02 +03:00
parent 8ec83fd5d9
commit 0e41db615e
5 changed files with 36 additions and 20 deletions

View File

@ -398,23 +398,26 @@ export function formatInstructModeChat(name, mes, isUser, isNarrator, forceAvata
/** /**
* Formats instruct mode system prompt. * Formats instruct mode system prompt.
* @param {string} systemPrompt System prompt string. * @param {string} systemPrompt System prompt string.
* @param {InstructSettings} customInstruct Custom instruct mode settings.
* @returns {string} Formatted instruct mode system prompt. * @returns {string} Formatted instruct mode system prompt.
*/ */
export function formatInstructModeSystemPrompt(systemPrompt) { export function formatInstructModeSystemPrompt(systemPrompt, customInstruct = null) {
if (!systemPrompt) { if (!systemPrompt) {
return ''; return '';
} }
const separator = power_user.instruct.wrap ? '\n' : ''; const instruct = customInstruct ?? power_user.instruct;
if (power_user.instruct.system_sequence_prefix) { const separator = instruct.wrap ? '\n' : '';
if (instruct.system_sequence_prefix) {
// TODO: Replace with a proper 'System' prompt entity name input // TODO: Replace with a proper 'System' prompt entity name input
const prefix = power_user.instruct.system_sequence_prefix.replace(/{{name}}/gi, 'System'); const prefix = instruct.system_sequence_prefix.replace(/{{name}}/gi, 'System');
systemPrompt = prefix + separator + systemPrompt; systemPrompt = prefix + separator + systemPrompt;
} }
if (power_user.instruct.system_sequence_suffix) { if (instruct.system_sequence_suffix) {
systemPrompt = systemPrompt + separator + power_user.instruct.system_sequence_suffix; systemPrompt = systemPrompt + separator + instruct.system_sequence_suffix;
} }
return systemPrompt; return systemPrompt;

View File

@ -705,16 +705,18 @@ export function parseExampleIntoIndividual(messageExampleString, appendNamesForG
return result; return result;
} }
function formatWorldInfo(value) { export function formatWorldInfo(value, { wiFormat = null } = {}) {
if (!value) { if (!value) {
return ''; return '';
} }
if (!oai_settings.wi_format.trim()) { const format = wiFormat || oai_settings.wi_format;
if (!format.trim()) {
return value; return value;
} }
return stringFormat(oai_settings.wi_format, value); return stringFormat(format, value);
} }
/** /**
@ -952,7 +954,7 @@ async function populateDialogueExamples(prompts, chatCompletion, messageExamples
* @param {number} position - Prompt position in the extensions object. * @param {number} position - Prompt position in the extensions object.
* @returns {string|false} - The prompt position for prompt collection. * @returns {string|false} - The prompt position for prompt collection.
*/ */
function getPromptPosition(position) { export function getPromptPosition(position) {
if (position == extension_prompt_types.BEFORE_PROMPT) { if (position == extension_prompt_types.BEFORE_PROMPT) {
return 'start'; return 'start';
} }
@ -969,7 +971,7 @@ function getPromptPosition(position) {
* @param {number} role Role of the prompt. * @param {number} role Role of the prompt.
* @returns {string} Mapped role. * @returns {string} Mapped role.
*/ */
function getPromptRole(role) { export function getPromptRole(role) {
switch (role) { switch (role) {
case extension_prompt_roles.SYSTEM: case extension_prompt_roles.SYSTEM:
return 'system'; return 'system';

View File

@ -1985,15 +1985,21 @@ export function fuzzySearchGroups(searchValue, fuzzySearchCaches = null) {
/** /**
* Renders a story string template with the given parameters. * Renders a story string template with the given parameters.
* @param {object} params Template parameters. * @param {object} params Template parameters.
* @param {object} [options] Additional options.
* @param {string} [options.customStoryString] Custom story string template.
* @param {InstructSettings} [options.customInstructSettings] Custom instruct settings.
* @returns {string} The rendered story string. * @returns {string} The rendered story string.
*/ */
export function renderStoryString(params) { export function renderStoryString(params, { customStoryString = null, customInstructSettings = null } = {}) {
try { try {
const storyString = customStoryString ?? power_user.context.story_string;
const instructSettings = customInstructSettings ?? power_user.instruct;
// Validate and log possible warnings/errors // Validate and log possible warnings/errors
validateStoryString(power_user.context.story_string, params); validateStoryString(storyString, params);
// compile the story string template into a function, with no HTML escaping // compile the story string template into a function, with no HTML escaping
const compiledTemplate = Handlebars.compile(power_user.context.story_string, { noEscape: true }); const compiledTemplate = Handlebars.compile(storyString, { noEscape: true });
// render the story string template with the given params // render the story string template with the given params
let output = compiledTemplate(params); let output = compiledTemplate(params);
@ -2006,7 +2012,7 @@ export function renderStoryString(params) {
// add a newline to the end of the story string if it doesn't have one // add a newline to the end of the story string if it doesn't have one
if (output.length > 0 && !output.endsWith('\n')) { if (output.length > 0 && !output.endsWith('\n')) {
if (!power_user.instruct.enabled || power_user.instruct.wrap) { if (!instructSettings.enabled || instructSettings.wrap) {
output += '\n'; output += '\n';
} }
} }

View File

@ -49,6 +49,7 @@ import {
clearChat, clearChat,
unshallowCharacter, unshallowCharacter,
deleteLastMessage, deleteLastMessage,
getCharacterCardFields,
} from '../script.js'; } from '../script.js';
import { import {
extension_settings, extension_settings,
@ -78,7 +79,7 @@ import { ToolManager } from './tool-calling.js';
import { accountStorage } from './util/AccountStorage.js'; import { accountStorage } from './util/AccountStorage.js';
import { timestampToMoment, uuidv4 } from './utils.js'; import { timestampToMoment, uuidv4 } from './utils.js';
import { getGlobalVariable, getLocalVariable, setGlobalVariable, setLocalVariable } from './variables.js'; import { getGlobalVariable, getLocalVariable, setGlobalVariable, setLocalVariable } from './variables.js';
import { convertCharacterBook, loadWorldInfo, saveWorldInfo, updateWorldInfoList } from './world-info.js'; import { convertCharacterBook, getWorldInfoPrompt, loadWorldInfo, saveWorldInfo, updateWorldInfoList } from './world-info.js';
import { ChatCompletionService, TextCompletionService } from './custom-request.js'; import { ChatCompletionService, TextCompletionService } from './custom-request.js';
import { ConnectionManagerRequestService } from './extensions/shared.js'; import { ConnectionManagerRequestService } from './extensions/shared.js';
import { updateReasoningUI, parseReasoningFromString } from './reasoning.js'; import { updateReasoningUI, parseReasoningFromString } from './reasoning.js';
@ -189,6 +190,7 @@ export function getContext() {
textCompletionSettings: textgenerationwebui_settings, textCompletionSettings: textgenerationwebui_settings,
powerUserSettings: power_user, powerUserSettings: power_user,
getCharacters, getCharacters,
getCharacterCardFields,
uuidv4, uuidv4,
humanizedDateTime, humanizedDateTime,
updateMessageBlock, updateMessageBlock,
@ -207,6 +209,7 @@ export function getContext() {
saveWorldInfo, saveWorldInfo,
updateWorldInfoList, updateWorldInfoList,
convertCharacterBook, convertCharacterBook,
getWorldInfoPrompt,
CONNECT_API_MAP, CONNECT_API_MAP,
getTextGenServer, getTextGenServer,
extractMessageFromData, extractMessageFromData,

View File

@ -756,7 +756,7 @@ export const worldInfoCache = new StructuredCloneMap({ cloneOnGet: true, cloneOn
* @param {string[]} chat The chat messages to scan, in reverse order. * @param {string[]} chat The chat messages to scan, in reverse order.
* @param {number} maxContext The maximum context size of the generation. * @param {number} maxContext The maximum context size of the generation.
* @param {boolean} isDryRun If true, the function will not emit any events. * @param {boolean} isDryRun If true, the function will not emit any events.
* @typedef {{worldInfoString: string, worldInfoBefore: string, worldInfoAfter: string, worldInfoExamples: any[], worldInfoDepth: any[]}} WIPromptResult * @typedef {{worldInfoString: string, worldInfoBefore: string, worldInfoAfter: string, worldInfoExamples: any[], worldInfoDepth: any[], anBefore: any[], anAfter: any[]}} WIPromptResult
* @returns {Promise<WIPromptResult>} The world info string and depth. * @returns {Promise<WIPromptResult>} The world info string and depth.
*/ */
export async function getWorldInfoPrompt(chat, maxContext, isDryRun) { export async function getWorldInfoPrompt(chat, maxContext, isDryRun) {
@ -778,6 +778,8 @@ export async function getWorldInfoPrompt(chat, maxContext, isDryRun) {
worldInfoAfter, worldInfoAfter,
worldInfoExamples: activatedWorldInfo.EMEntries ?? [], worldInfoExamples: activatedWorldInfo.EMEntries ?? [],
worldInfoDepth: activatedWorldInfo.WIDepthEntries ?? [], worldInfoDepth: activatedWorldInfo.WIDepthEntries ?? [],
anBefore: activatedWorldInfo.ANBeforeEntries ?? [],
anAfter: activatedWorldInfo.ANAfterEntries ?? [],
}; };
} }
@ -3862,7 +3864,7 @@ function parseDecorators(content) {
* @param {string[]} chat The chat messages to scan, in reverse order. * @param {string[]} chat The chat messages to scan, in reverse order.
* @param {number} maxContext The maximum context size of the generation. * @param {number} maxContext The maximum context size of the generation.
* @param {boolean} isDryRun Whether to perform a dry run. * @param {boolean} isDryRun Whether to perform a dry run.
* @typedef {{ worldInfoBefore: string, worldInfoAfter: string, EMEntries: any[], WIDepthEntries: any[], allActivatedEntries: Set<any> }} WIActivated * @typedef {{ worldInfoBefore: string, worldInfoAfter: string, EMEntries: any[], WIDepthEntries: any[], ANBeforeEntries: any[], ANAfterEntries: any[], allActivatedEntries: Set<any> }} WIActivated
* @returns {Promise<WIActivated>} The world info activated. * @returns {Promise<WIActivated>} The world info activated.
*/ */
export async function checkWorldInfo(chat, maxContext, isDryRun) { export async function checkWorldInfo(chat, maxContext, isDryRun) {
@ -3906,7 +3908,7 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) {
timedEffects.checkTimedEffects(); timedEffects.checkTimedEffects();
if (sortedEntries.length === 0) { if (sortedEntries.length === 0) {
return { worldInfoBefore: '', worldInfoAfter: '', WIDepthEntries: [], EMEntries: [], allActivatedEntries: new Set() }; return { worldInfoBefore: '', worldInfoAfter: '', WIDepthEntries: [], EMEntries: [], ANBeforeEntries: [], ANAfterEntries: [], allActivatedEntries: new Set() };
} }
/** @type {number[]} Represents the delay levels for entries that are delayed until recursion */ /** @type {number[]} Represents the delay levels for entries that are delayed until recursion */
@ -4355,7 +4357,7 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) {
console.log(`[WI] ${isDryRun ? 'Hypothetically adding' : 'Adding'} ${allActivatedEntries.size} entries to prompt`, Array.from(allActivatedEntries.values())); console.log(`[WI] ${isDryRun ? 'Hypothetically adding' : 'Adding'} ${allActivatedEntries.size} entries to prompt`, Array.from(allActivatedEntries.values()));
console.debug(`[WI] --- DONE${isDryRun ? ' (DRY RUN)' : ''} ---`); console.debug(`[WI] --- DONE${isDryRun ? ' (DRY RUN)' : ''} ---`);
return { worldInfoBefore, worldInfoAfter, EMEntries, WIDepthEntries, allActivatedEntries: new Set(allActivatedEntries.values()) }; return { worldInfoBefore, worldInfoAfter, EMEntries, WIDepthEntries, ANBeforeEntries: ANTopEntries, ANAfterEntries: ANBottomEntries, allActivatedEntries: new Set(allActivatedEntries.values()) };
} }
/** /**