Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
51ad27fb86 |
@@ -809,21 +809,18 @@
|
||||
<div class="inline-drawer-content">
|
||||
<div class="range-block m-t-1">
|
||||
<div class="justifyLeft" data-i18n="Main">Main</div>
|
||||
<i class="editor_maximize fa-solid fa-maximize right_menu_button" data-for="main_prompt_quick_edit_textarea" data-i18n="[title]Expand the editor"></i>
|
||||
<div class="wide100p">
|
||||
<textarea id="main_prompt_quick_edit_textarea" class="text_pole textarea_compact autoSetHeight" rows="6" placeholder="—" data-pm-prompt="main"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="range-block m-t-1">
|
||||
<div class="justifyLeft" data-i18n="Auxiliary">Auxiliary</div>
|
||||
<i class="editor_maximize fa-solid fa-maximize right_menu_button" data-for="nsfw_prompt_quick_edit_textarea" data-i18n="[title]Expand the editor"></i>
|
||||
<div class="wide100p">
|
||||
<textarea id="nsfw_prompt_quick_edit_textarea" class="text_pole textarea_compact autoSetHeight" rows="6" placeholder="—" data-pm-prompt="nsfw"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="range-block m-t-1">
|
||||
<div class="justifyLeft" data-i18n="Post-History Instructions">Post-History Instructions</div>
|
||||
<i class="editor_maximize fa-solid fa-maximize right_menu_button" data-for="jailbreak_prompt_quick_edit_textarea" data-i18n="[title]Expand the editor"></i>
|
||||
<div class="wide100p">
|
||||
<textarea id="jailbreak_prompt_quick_edit_textarea" class="text_pole textarea_compact autoSetHeight" rows="6" placeholder="—" data-pm-prompt="jailbreak"></textarea>
|
||||
</div>
|
||||
@@ -2147,9 +2144,6 @@
|
||||
<span data-i18n="Allows the model to return its thinking process.">
|
||||
Allows the model to return its thinking process.
|
||||
</span>
|
||||
<strong data-i18n="This setting enables or disables reasoning generation by the model." data-source="deepseek,zai,moonshot">
|
||||
This setting enables or disables reasoning generation by the model.
|
||||
</strong>
|
||||
<strong data-i18n="This setting affects visibility only." data-source-mode="except" data-source="zai,moonshot,openrouter,deepseek">
|
||||
This setting affects visibility only.
|
||||
</strong>
|
||||
|
||||
+1
-7
@@ -215,7 +215,7 @@ import {
|
||||
} from './scripts/tags.js';
|
||||
import { checkOpenRouterAuth, initSecrets, readSecretState } from './scripts/secrets.js';
|
||||
import { markdownExclusionExt } from './scripts/showdown-exclusion.js';
|
||||
import { markdownUnderscoreExt, fixNestedEmphasis } from './scripts/showdown-emphasis.js';
|
||||
import { markdownUnderscoreExt } from './scripts/showdown-underscore.js';
|
||||
import { NOTE_MODULE_NAME, initAuthorsNote, metadata_keys, setFloatingPrompt, shouldWIAddPrompt } from './scripts/authors-note.js';
|
||||
import { registerPromptManagerMigration } from './scripts/PromptManager.js';
|
||||
import { getRegexedString, regex_placement } from './scripts/extensions/regex/engine.js';
|
||||
@@ -1813,12 +1813,6 @@ export function messageFormatting(mes, ch_name, isSystem, isUser, messageId, san
|
||||
});
|
||||
}
|
||||
|
||||
// Convert nested same-delimiter emphasis to bold before any markdown fixing.
|
||||
// Must run before fixMarkdown which would strip the spaces that indicate nesting.
|
||||
if (!isSystem) {
|
||||
mes = fixNestedEmphasis(mes);
|
||||
}
|
||||
|
||||
if (power_user.auto_fix_generated_markdown) {
|
||||
mes = fixMarkdown(mes, true);
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ function setNotePositionCommand(_, text) {
|
||||
return;
|
||||
}
|
||||
|
||||
$(`input[name="extension_floating_position"][value="${position}"]`).prop('checked', true).trigger('input').trigger('change');
|
||||
$(`input[name="extension_floating_position"][value="${position}"]`).prop('checked', true).trigger('input');
|
||||
toastr.info(t`Author's Note position updated`);
|
||||
}
|
||||
return Object.keys(validPositions).find(key => validPositions[key] == chat_metadata[metadata_keys.position]);
|
||||
|
||||
@@ -126,17 +126,9 @@ export class AutoComplete {
|
||||
});
|
||||
textarea.addEventListener('blur', () => this.hide());
|
||||
if (isFloating) {
|
||||
textarea.addEventListener('scroll', () => {
|
||||
if (this.isActive) {
|
||||
this.updateFloatingPositionDebounced();
|
||||
}
|
||||
});
|
||||
textarea.addEventListener('scroll', () => this.updateFloatingPositionDebounced());
|
||||
}
|
||||
window.addEventListener('resize', () => {
|
||||
if (this.isActive) {
|
||||
this.updatePositionDebounced();
|
||||
}
|
||||
});
|
||||
window.addEventListener('resize', () => this.updatePositionDebounced());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -94,7 +94,7 @@ async function getBookmarkName({ isReplace = false, forceName = null } = {}) {
|
||||
return `${cleanName} - ${bookmarkNameToken}${i}`;
|
||||
}
|
||||
const existingChats = await getExistingChatNames();
|
||||
const suggestedName = getUniqueName(mainChatName, (x) => existingChats.includes(x), { nameBuilder: buildCheckpointName, startIndex: 1 });
|
||||
const suggestedName = getUniqueName(mainChatName, (x) => existingChats.includes(x), { nameBuilder: buildCheckpointName });
|
||||
|
||||
const body = await renderTemplateAsync('createCheckpoint', { isReplace: isReplace, suggestedName: suggestedName });
|
||||
let name = forceName ?? await Popup.show.input('Create Checkpoint', body, suggestedName);
|
||||
@@ -212,7 +212,7 @@ export async function createBranch(mesId, { swipeId = null } = {}) {
|
||||
return `${cleanName} - Branch #${i}`;
|
||||
}
|
||||
const existingChats = await getExistingChatNames();
|
||||
const name = getUniqueName(mainChatName, (x) => existingChats.includes(x), { nameBuilder: buildBranchName, startIndex: 1 });
|
||||
const name = getUniqueName(mainChatName, (x) => existingChats.includes(x), { nameBuilder: buildBranchName });
|
||||
if (!name) {
|
||||
console.error('Could not generate a unique branch name.');
|
||||
toastr.error('Could not generate a unique branch name.', 'Branch creation failed');
|
||||
|
||||
@@ -18,8 +18,6 @@ import { generateWebLlmChatPrompt, isWebLlmSupported } from '../shared.js';
|
||||
import { Popup, POPUP_RESULT } from '../../popup.js';
|
||||
import { t } from '../../i18n.js';
|
||||
import { removeReasoningFromString } from '../../reasoning.js';
|
||||
import { macros } from '../../macros/macro-system.js';
|
||||
import { MacrosParser } from '/scripts/macros.js';
|
||||
export { MODULE_NAME };
|
||||
|
||||
/**
|
||||
@@ -105,19 +103,6 @@ let lastServerResponseTime = 0;
|
||||
/** @type {{[characterName: string]: string}} */
|
||||
export let lastExpression = {};
|
||||
|
||||
/**
|
||||
* Gets the current fallback expression, including the labels for when it is not set
|
||||
* @returns {string|null}
|
||||
*/
|
||||
function getCurrentFallbackExpression() {
|
||||
const expression = extension_settings?.expressions.fallback_expression;
|
||||
const showEmojis = extension_settings?.expressions?.showDefault;
|
||||
|
||||
if (!expression && showEmojis) return OPTION_EMOJI_FALLBACK;
|
||||
if (!expression) return OPTION_NO_FALLBACK;
|
||||
|
||||
return expression;
|
||||
}
|
||||
/**
|
||||
* Returns a placeholder image object for a given expression
|
||||
* @param {string} expression - The expression label
|
||||
@@ -801,48 +786,13 @@ async function setSpriteSlashCommand({ type }, searchTerm) {
|
||||
return label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the currently set expression labels.
|
||||
* @param {Object} args
|
||||
* @param {'true'|'false'|'only'} [args.custom] - Whether to filter out or return only custom expressions
|
||||
* @param {'true'|'false'} [args.filter] - Filter the list to only include expressions that have available sprites for the current character
|
||||
* @param {import('../../slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} [args.return] - In which format must the expressions be returned
|
||||
* @param {string} characterName
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
async function getExpressionListSlashCommand(args, characterName) {
|
||||
const { custom, filter = 'true', return: returnType = 'pipe' } = args;
|
||||
|
||||
const expressions = await getExpressionsList({ filterAvailable: !isFalseBoolean(filter) });
|
||||
const customExpressions = extension_settings?.expressions?.custom || [];
|
||||
const expressionsMap = {
|
||||
default: expressions.filter(expression => !customExpressions.includes(expression)),
|
||||
custom: customExpressions,
|
||||
all: expressions,
|
||||
};
|
||||
|
||||
let expressionIndex = 'all';
|
||||
|
||||
if (custom === 'false') expressionIndex = 'default';
|
||||
if (custom === 'only') expressionIndex = 'custom';
|
||||
|
||||
try {
|
||||
return await slashCommandReturnHelper.doReturn(returnType, expressionsMap[expressionIndex], {
|
||||
objectToStringFunc: list => list.join(', '),
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} expressionName - Label of the expression to set as fallback
|
||||
*/
|
||||
function setFallBackExpressionSlashCommand(args, expressionName) {
|
||||
expressionName = expressionName.trim().toLowerCase();
|
||||
|
||||
if (!expressionName) return getCurrentFallbackExpression();
|
||||
if (!expressionName) return extension_settings?.expressions?.fallback_expression || '';
|
||||
|
||||
const select = /** @type {HTMLSelectElement} */(document.getElementById('expression_fallback'));
|
||||
const fallbackExpressions = Array
|
||||
@@ -1201,24 +1151,15 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} [options]
|
||||
* @param {string} [options.characterName] Filters last message to the one of the target character
|
||||
*/
|
||||
function getLastCharacterMessage({ characterName = '' } = {}) {
|
||||
function getLastCharacterMessage() {
|
||||
const context = getContext();
|
||||
const reversedChat = context.chat.slice().reverse();
|
||||
const ignoreCharName = !characterName;
|
||||
|
||||
for (let mes of reversedChat) {
|
||||
if (mes.is_user || mes.is_system || mes.extra?.type === system_message_types.NARRATOR) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!ignoreCharName && mes.name !== characterName) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return { mes: mes.mes, name: mes.name, original_avatar: mes.original_avatar, force_avatar: mes.force_avatar };
|
||||
}
|
||||
|
||||
@@ -1522,30 +1463,6 @@ export async function getExpressionsList({ filterAvailable = false } = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the last expression used in the chat by the active character.
|
||||
* @param {Object} [options]
|
||||
* @param {string} [options.characterName] Filters last expression to the one of the target character instead
|
||||
* @returns {string}
|
||||
*/
|
||||
function getLastExpression({ characterName = '' } = {}) {
|
||||
if (typeof characterName !== 'string') throw new Error('Character name must be a string');
|
||||
|
||||
if (!characterName) {
|
||||
characterName = characters[this_chid]?.avatar || '';
|
||||
}
|
||||
|
||||
const char = findChar({ name: characterName, quiet: true });
|
||||
|
||||
const currentLastMessage = getLastCharacterMessage({ characterName });
|
||||
const finalCharacterName = currentLastMessage?.name ?? char?.name ?? characterName;
|
||||
|
||||
const spriteFolderName = getSpriteFolderName(currentLastMessage, finalCharacterName);
|
||||
const sprite = lastExpression[spriteFolderName.split('/')[0]] ?? '';
|
||||
|
||||
return sprite;
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects a sprite from the given sprite folder for the given expression.
|
||||
*
|
||||
@@ -2434,15 +2351,15 @@ export async function init() {
|
||||
typeList: [ARGUMENT_TYPE.STRING],
|
||||
isRequired: false,
|
||||
enumProvider: () => [
|
||||
new SlashCommandEnumValue(OPTION_NO_FALLBACK, 'Sets the fallback expression to no image'),
|
||||
new SlashCommandEnumValue(OPTION_EMOJI_FALLBACK, 'Sets the fallback expression to emojis'),
|
||||
new SlashCommandEnumValue('#none', 'Sets the fallback expression to no image'),
|
||||
new SlashCommandEnumValue('#emoji', 'Sets the fallback expression to emojis'),
|
||||
...localEnumProviders.expressions(),
|
||||
],
|
||||
}),
|
||||
],
|
||||
helpString: `
|
||||
<div>
|
||||
Gets the currently selected global fallback expression.<br />
|
||||
Gets the currently selected expression fallback for all characters.<br />
|
||||
If a valid expression label is sent, it will be set as the new fallback.
|
||||
</div>
|
||||
<div>
|
||||
@@ -2493,8 +2410,23 @@ export async function init() {
|
||||
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
||||
name: 'expression-last',
|
||||
aliases: ['lastsprite'],
|
||||
/** @type {(args: object, name: string) => string} */
|
||||
callback: (_, name) => getLastExpression({ characterName: (name || '') }),
|
||||
/** @type {(args: object, name: string) => Promise<string>} */
|
||||
callback: async (_, name) => {
|
||||
if (typeof name !== 'string') throw new Error('name must be a string');
|
||||
if (!name) {
|
||||
if (selected_group) {
|
||||
toastr.error(t`In group chats, you must specify a character name.`, t`No character name specified`);
|
||||
return '';
|
||||
}
|
||||
name = characters[this_chid]?.avatar;
|
||||
}
|
||||
|
||||
const char = findChar({ name: name });
|
||||
if (!char) toastr.warning(t`Couldn't find character ${name}.`, t`Character not found`);
|
||||
|
||||
const sprite = lastExpression[char?.name ?? name] ?? '';
|
||||
return sprite;
|
||||
},
|
||||
returns: 'the last set expression for the named character.',
|
||||
unnamedArgumentList: [
|
||||
SlashCommandArgument.fromProps({
|
||||
@@ -2508,14 +2440,23 @@ export async function init() {
|
||||
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
||||
name: 'expression-list',
|
||||
aliases: ['expressions'],
|
||||
callback: getExpressionListSlashCommand,
|
||||
/** @type {(args: {return: string, filter: string}) => Promise<string>} */
|
||||
callback: async (args) => {
|
||||
let returnType =
|
||||
/** @type {import('../../slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} */
|
||||
(args.return);
|
||||
|
||||
const list = await getExpressionsList({ filterAvailable: !isFalseBoolean(args.filter) });
|
||||
|
||||
return await slashCommandReturnHelper.doReturn(returnType ?? 'pipe', list, { objectToStringFunc: list => list.join(', ') });
|
||||
},
|
||||
namedArgumentList: [
|
||||
SlashCommandNamedArgument.fromProps({
|
||||
name: 'return',
|
||||
description: 'The way how you want the return value to be provided',
|
||||
typeList: [ARGUMENT_TYPE.STRING],
|
||||
defaultValue: 'pipe',
|
||||
enumList: slashCommandReturnHelper.enumList({ allowObject: true, allowPopup: true }),
|
||||
enumList: slashCommandReturnHelper.enumList({ allowObject: true }),
|
||||
forceEnum: true,
|
||||
}),
|
||||
SlashCommandNamedArgument.fromProps({
|
||||
@@ -2525,21 +2466,6 @@ export async function init() {
|
||||
enumList: commonEnumProviders.boolean('trueFalse')(),
|
||||
defaultValue: 'true',
|
||||
}),
|
||||
SlashCommandNamedArgument.fromProps({
|
||||
name: 'custom',
|
||||
description: t`Whether to include, filter out or return only custom expressions`,
|
||||
typeList: [
|
||||
ARGUMENT_TYPE.STRING,
|
||||
ARGUMENT_TYPE.BOOLEAN,
|
||||
],
|
||||
isRequired: false,
|
||||
defaultValue: 'true',
|
||||
enumList: [
|
||||
new SlashCommandEnumValue('true', '(default) Custom expressions will be included in the result'),
|
||||
new SlashCommandEnumValue('false', 'Custom expressions will not be included in the result'),
|
||||
new SlashCommandEnumValue('only', 'Only custom expressions will be included in the result'),
|
||||
],
|
||||
}),
|
||||
],
|
||||
returns: 'The comma-separated list of available expressions, including custom expressions.',
|
||||
helpString: 'Returns a list of available expressions, including custom expressions.',
|
||||
@@ -2647,73 +2573,4 @@ export async function init() {
|
||||
</div>
|
||||
`,
|
||||
}));
|
||||
|
||||
if (power_user.experimental_macro_engine) {
|
||||
macros.register('defaultExpression', {
|
||||
handler: getCurrentFallbackExpression,
|
||||
category: macros.category.MISC,
|
||||
description: 'Returns the global fallback expression.',
|
||||
returns: 'Expression label',
|
||||
exampleUsage: '{{defaultExpression}}',
|
||||
});
|
||||
|
||||
macros.register('lastExpression', {
|
||||
handler: function ({ args: [name = '{{char}}'], resolve }) {
|
||||
try {
|
||||
return getLastExpression({ characterName: resolve(name || '') });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return '';
|
||||
}
|
||||
},
|
||||
unnamedArgs: [{
|
||||
name: 'name',
|
||||
description: 'The name of the target character',
|
||||
defaultValue: '{{char}}',
|
||||
optional: true,
|
||||
type: macros.valueType.STRING,
|
||||
}],
|
||||
delayArgResolution: true,
|
||||
category: macros.category.MISC,
|
||||
description: 'Returns the last expression used by the selected character. The currently active character is used if no character name is provided.',
|
||||
returns: 'Expression label',
|
||||
exampleUsage: [
|
||||
'{{lastExpression}}',
|
||||
'{{lastExpression::John}}',
|
||||
'{{lastExpression::{{char}}}}',
|
||||
],
|
||||
});
|
||||
|
||||
macros.register('availableExpressions', {
|
||||
handler: function () {
|
||||
return getCachedExpressions().join(', ');
|
||||
},
|
||||
category: macros.category.MISC,
|
||||
description: 'Returns a list with all the available expressions provided by the Classifier API.',
|
||||
returns: 'Expression label list',
|
||||
exampleUsage: '{{availableExpressions}}',
|
||||
});
|
||||
} else {
|
||||
MacrosParser.registerMacro('defaultExpression',
|
||||
getCurrentFallbackExpression,
|
||||
t`Returns the global fallback expression.`,
|
||||
);
|
||||
|
||||
MacrosParser.registerMacro('lastExpression',
|
||||
() => {
|
||||
try {
|
||||
return getLastExpression();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return '';
|
||||
}
|
||||
},
|
||||
t`Returns the last expression used.`,
|
||||
);
|
||||
|
||||
MacrosParser.registerMacro('availableExpressions',
|
||||
() => getCachedExpressions().join(', '),
|
||||
t`Returns a list with all the available expressions provided by the Classifier API.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { CONNECT_API_MAP, createModelIcon, getRequestHeaders, substituteParams } from '../../script.js';
|
||||
import { CONNECT_API_MAP, createModelIcon, getRequestHeaders } from '../../script.js';
|
||||
import { extension_settings, openThirdPartyExtensionMenu } from '../extensions.js';
|
||||
import { t } from '../i18n.js';
|
||||
import { oai_settings, proxies, ZAI_ENDPOINT } from '../openai.js';
|
||||
@@ -117,9 +117,9 @@ export async function getMultimodalCaption(base64Img, prompt) {
|
||||
}
|
||||
|
||||
requestBody.server_url = oai_settings.custom_url;
|
||||
requestBody.custom_include_headers = substituteParams(oai_settings.custom_include_headers);
|
||||
requestBody.custom_include_body = substituteParams(oai_settings.custom_include_body);
|
||||
requestBody.custom_exclude_body = substituteParams(oai_settings.custom_exclude_body);
|
||||
requestBody.custom_include_headers = oai_settings.custom_include_headers;
|
||||
requestBody.custom_include_body = oai_settings.custom_include_body;
|
||||
requestBody.custom_exclude_body = oai_settings.custom_exclude_body;
|
||||
}
|
||||
|
||||
if (extension_settings.caption.multimodal_api === 'zai') {
|
||||
|
||||
@@ -52,7 +52,6 @@ export const macros = {
|
||||
|
||||
// enums
|
||||
category: MacroCategory,
|
||||
valueType: MacroValueType,
|
||||
|
||||
// shorthand functions
|
||||
register: MacroRegistry.registerMacro.bind(MacroRegistry),
|
||||
|
||||
@@ -2856,9 +2856,9 @@ export async function createGenerationParameters(settings, model, type, messages
|
||||
|
||||
if (settings.chat_completion_source === chat_completion_sources.CUSTOM) {
|
||||
generate_data.custom_url = settings.custom_url;
|
||||
generate_data.custom_include_body = substituteParams(settings.custom_include_body);
|
||||
generate_data.custom_exclude_body = substituteParams(settings.custom_exclude_body);
|
||||
generate_data.custom_include_headers = substituteParams(settings.custom_include_headers);
|
||||
generate_data.custom_include_body = settings.custom_include_body;
|
||||
generate_data.custom_exclude_body = settings.custom_exclude_body;
|
||||
generate_data.custom_include_headers = settings.custom_include_headers;
|
||||
}
|
||||
|
||||
if (settings.chat_completion_source === chat_completion_sources.COHERE) {
|
||||
@@ -4407,7 +4407,7 @@ async function getStatusOpen() {
|
||||
if (oai_settings.chat_completion_source === chat_completion_sources.CUSTOM) {
|
||||
$('.model_custom_select').empty();
|
||||
data.custom_url = oai_settings.custom_url;
|
||||
data.custom_include_headers = substituteParams(oai_settings.custom_include_headers);
|
||||
data.custom_include_headers = oai_settings.custom_include_headers;
|
||||
}
|
||||
|
||||
if (oai_settings.chat_completion_source === chat_completion_sources.AZURE_OPENAI) {
|
||||
|
||||
@@ -1,237 +0,0 @@
|
||||
/**
|
||||
* Showdown emphasis extensions and pre-processing utilities.
|
||||
*
|
||||
* Contains:
|
||||
* - markdownUnderscoreExt: Showdown output extension for _word_ emphasis
|
||||
* - fixNestedEmphasis: Pre-processor to convert nested same-delimiter emphasis to bold
|
||||
*/
|
||||
|
||||
// ===== Underscore Emphasis Extension =====
|
||||
|
||||
/**
|
||||
* Showdown extension that replaces words surrounded by singular underscores with <em> tags.
|
||||
* @returns {import('showdown').ShowdownExtension[]} An array of Showdown extensions
|
||||
*/
|
||||
export const markdownUnderscoreExt = () => {
|
||||
try {
|
||||
if (!canUseNegativeLookbehind()) {
|
||||
console.log('Showdown-underscore extension: Negative lookbehind not supported. Skipping.');
|
||||
return [];
|
||||
}
|
||||
|
||||
return [{
|
||||
type: 'output',
|
||||
regex: new RegExp('(<code(?:\\s+[^>]*)?>[\\s\\S]*?<\\/code>|<style(?:\\s+[^>]*)?>[\\s\\S]*?<\\/style>)|\\b(?<!_)_(?!_)(.*?)(?<!_)_(?!_)\\b', 'gi'),
|
||||
replace: function (match, tagContent, italicContent) {
|
||||
if (tagContent) {
|
||||
// If it's inside <code> or <style> tags, return unchanged
|
||||
return match;
|
||||
} else if (italicContent) {
|
||||
// If it's an italic group, apply the replacement
|
||||
return '<em>' + italicContent + '</em>';
|
||||
}
|
||||
// If none of the conditions are met, return the original match
|
||||
return match;
|
||||
},
|
||||
}];
|
||||
} catch (e) {
|
||||
console.error('Error in Showdown-underscore extension:', e);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
function canUseNegativeLookbehind() {
|
||||
try {
|
||||
new RegExp('(?<!_)');
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Nested Emphasis Pre-Processor =====
|
||||
|
||||
/**
|
||||
* Fixes nested emphasis that showdown cannot handle.
|
||||
*
|
||||
* Showdown cannot handle nested emphasis with the same delimiter character.
|
||||
* This function handles two cases:
|
||||
* 1. `*outer *inner* outer*` — inner `*` promoted to `**` (renders as bold-inside-italic)
|
||||
* 2. `**outer **inner** outer**` — inner `**` stripped (bold-inside-bold is redundant)
|
||||
*
|
||||
* Uses CommonMark-like flanking rules and stack-based pairing to detect nesting.
|
||||
*
|
||||
* @param {string} text The message text to process
|
||||
* @returns {string} The processed text with nested emphasis fixed
|
||||
* @example
|
||||
* fixNestedEmphasis("*She sighs. *I can't do this,* she says.*")
|
||||
* // → "*She sighs. **I can't do this,** she says.*"
|
||||
* fixNestedEmphasis("**This is **bold**, isn't it?**")
|
||||
* // → "**This is bold, isn't it?**"
|
||||
*/
|
||||
export function fixNestedEmphasis(text) {
|
||||
// Temporarily replace code blocks and inline code to protect them from modification
|
||||
const codeBlocks = [];
|
||||
let processed = text.replace(/```[\s\S]*?```|~~~[\s\S]*?~~~|``[\s\S]*?``|`[^`\n]*`/g, (match) => {
|
||||
codeBlocks.push(match);
|
||||
return `\x00CODE_BLOCK_${codeBlocks.length - 1}\x00`;
|
||||
});
|
||||
|
||||
processed = processed.split('\n').map(line => {
|
||||
// Fix nested ** inside ** first (strip inner), then nested * inside * (promote inner)
|
||||
line = fixNestedBoldLine(line);
|
||||
line = fixNestedItalicLine(line);
|
||||
return line;
|
||||
}).join('\n');
|
||||
|
||||
// Restore code blocks
|
||||
processed = processed.replace(/\x00CODE_BLOCK_(\d+)\x00/g, (_, idx) => codeBlocks[parseInt(idx)]);
|
||||
|
||||
return processed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds nested `**...**` pairs inside other `**...**` pairs and strips the inner delimiters.
|
||||
* Bold inside bold is redundant — like GitHub, the whole block renders as bold.
|
||||
* @param {string} line A single line of text
|
||||
* @returns {string} The processed line
|
||||
*/
|
||||
function fixNestedBoldLine(line) {
|
||||
// Find all ** positions (exactly two consecutive asterisks, not part of ***)
|
||||
const positions = [];
|
||||
for (let i = 0; i < line.length - 1; i++) {
|
||||
if (line[i] === '*' && line[i + 1] === '*') {
|
||||
const before = i > 0 ? line[i - 1] : '';
|
||||
const after = i + 2 < line.length ? line[i + 2] : '';
|
||||
// Must be exactly ** (not *** or more)
|
||||
if (before !== '*' && after !== '*') {
|
||||
positions.push(i);
|
||||
i++; // skip the second *
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Need at least 4 ** delimiters for nesting to be possible
|
||||
if (positions.length < 4) return line;
|
||||
|
||||
// Stack-based pairing with flanking rules
|
||||
const stack = [];
|
||||
const pairs = [];
|
||||
|
||||
for (let i = 0; i < positions.length; i++) {
|
||||
const pos = positions[i];
|
||||
// For **, check the char before the first * and after the second *
|
||||
const charBefore = pos > 0 ? line[pos - 1] : '';
|
||||
const charAfter = pos + 2 < line.length ? line[pos + 2] : '';
|
||||
|
||||
const canClose = charBefore !== '' && !/\s/.test(charBefore);
|
||||
const canOpen = charAfter !== '' && !/\s/.test(charAfter);
|
||||
|
||||
if (canClose && stack.length > 0) {
|
||||
const openIdx = stack.pop();
|
||||
pairs.push([openIdx, i]);
|
||||
} else if (canOpen) {
|
||||
stack.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Determine which pairs are nested inside others
|
||||
const nestedPositions = new Set();
|
||||
for (let i = 0; i < pairs.length; i++) {
|
||||
const [outerOpen, outerClose] = [positions[pairs[i][0]], positions[pairs[i][1]]];
|
||||
for (let j = 0; j < pairs.length; j++) {
|
||||
if (i === j) continue;
|
||||
const [innerOpen, innerClose] = [positions[pairs[j][0]], positions[pairs[j][1]]];
|
||||
if (innerOpen > outerOpen && innerClose < outerClose) {
|
||||
// Inner pair is nested — mark both its ** delimiters for removal
|
||||
nestedPositions.add(innerOpen);
|
||||
nestedPositions.add(innerClose);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (nestedPositions.size === 0) return line;
|
||||
|
||||
// Build result, skipping the nested ** delimiters
|
||||
let result = '';
|
||||
for (let i = 0; i < line.length; i++) {
|
||||
if (nestedPositions.has(i)) {
|
||||
i++; // skip both characters of **
|
||||
continue;
|
||||
}
|
||||
result += line[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds nested `*...*` pairs inside other `*...*` pairs and promotes them to `**...**`.
|
||||
* This converts inner emphasis to bold so showdown renders bold-inside-italic.
|
||||
* @param {string} line A single line of text
|
||||
* @returns {string} The processed line
|
||||
*/
|
||||
function fixNestedItalicLine(line) {
|
||||
// Find all single-* positions (not part of ** or ***)
|
||||
const positions = [];
|
||||
for (let i = 0; i < line.length; i++) {
|
||||
if (line[i] === '*') {
|
||||
const before = i > 0 ? line[i - 1] : '';
|
||||
const after = i < line.length - 1 ? line[i + 1] : '';
|
||||
if (before !== '*' && after !== '*') {
|
||||
positions.push(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Need at least 4 single asterisks for nesting to be possible
|
||||
if (positions.length < 4) return line;
|
||||
|
||||
// Use stack-based pairing with CommonMark-like flanking rules:
|
||||
// - An asterisk can OPEN emphasis if followed by a non-whitespace character
|
||||
// - An asterisk can CLOSE emphasis if preceded by a non-whitespace character
|
||||
const stack = [];
|
||||
const pairs = [];
|
||||
|
||||
for (let i = 0; i < positions.length; i++) {
|
||||
const pos = positions[i];
|
||||
const charBefore = pos > 0 ? line[pos - 1] : '';
|
||||
const charAfter = pos < line.length - 1 ? line[pos + 1] : '';
|
||||
|
||||
const canClose = charBefore !== '' && !/\s/.test(charBefore);
|
||||
const canOpen = charAfter !== '' && !/\s/.test(charAfter);
|
||||
|
||||
// Prefer closing over opening (matches CommonMark behavior)
|
||||
if (canClose && stack.length > 0) {
|
||||
const openIdx = stack.pop();
|
||||
pairs.push([openIdx, i]);
|
||||
} else if (canOpen) {
|
||||
stack.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Determine nesting: a pair is nested if fully contained within another pair
|
||||
const nestedIndices = new Set();
|
||||
for (let i = 0; i < pairs.length; i++) {
|
||||
const [outerOpen, outerClose] = [positions[pairs[i][0]], positions[pairs[i][1]]];
|
||||
for (let j = 0; j < pairs.length; j++) {
|
||||
if (i === j) continue;
|
||||
const [innerOpen, innerClose] = [positions[pairs[j][0]], positions[pairs[j][1]]];
|
||||
if (innerOpen > outerOpen && innerClose < outerClose) {
|
||||
nestedIndices.add(innerOpen);
|
||||
nestedIndices.add(innerClose);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (nestedIndices.size === 0) return line;
|
||||
|
||||
// Build result with doubled asterisks at nested positions
|
||||
let result = '';
|
||||
for (let i = 0; i < line.length; i++) {
|
||||
result += line[i];
|
||||
if (nestedIndices.has(i)) {
|
||||
result += '*';
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -2970,9 +2970,9 @@ export function initDefaultSlashCommands() {
|
||||
}));
|
||||
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
||||
name: 'tokens',
|
||||
callback: async (_, text) => {
|
||||
callback: (_, text) => {
|
||||
if (text instanceof SlashCommandClosure || Array.isArray(text)) throw new Error(t`Unnamed argument cannot be a closure for command /tokens`);
|
||||
return String(await getTokenCountAsync(text));
|
||||
return getTokenCountAsync(text).then(count => String(count));
|
||||
},
|
||||
returns: t`number of tokens`,
|
||||
unnamedArgumentList: [
|
||||
|
||||
@@ -552,7 +552,7 @@ export function getTokenCount(str, padding = undefined) {
|
||||
* @deprecated Use counterWrapperOpenAIAsync instead.
|
||||
*/
|
||||
function counterWrapperOpenAI(text) {
|
||||
const message = { content: text };
|
||||
const message = { role: 'system', content: text };
|
||||
return countTokensOpenAI(message, true);
|
||||
}
|
||||
|
||||
@@ -562,7 +562,7 @@ function counterWrapperOpenAI(text) {
|
||||
* @returns {Promise<number>} Token count.
|
||||
*/
|
||||
function counterWrapperOpenAIAsync(text) {
|
||||
const message = { content: text };
|
||||
const message = { role: 'system', content: text };
|
||||
return countTokensOpenAIAsync(message, true);
|
||||
}
|
||||
|
||||
@@ -801,7 +801,7 @@ export function countTokensOpenAI(messages, full = false) {
|
||||
messages = [messages];
|
||||
}
|
||||
|
||||
let token_count = 0;
|
||||
let token_count = -1;
|
||||
|
||||
for (const message of messages) {
|
||||
const model = getTokenizerModel();
|
||||
@@ -851,7 +851,7 @@ export async function countTokensOpenAIAsync(messages, full = false) {
|
||||
messages = [messages];
|
||||
}
|
||||
|
||||
let token_count = 0;
|
||||
let token_count = -1;
|
||||
|
||||
for (const message of messages) {
|
||||
const model = getTokenizerModel();
|
||||
|
||||
@@ -692,12 +692,13 @@ export function isElementInViewport(el) {
|
||||
* @param {{ (name: string): boolean; }} exists Function to check if name exists.
|
||||
* @param {Object} [options] The options.
|
||||
* @param {((baseName: string, i: number) => string)|null} [options.nameBuilder=null] Function to build the name.
|
||||
* Starts with the index provided by `startIndex` (default is 0). If not provided, uses `baseName` for index 0 and "${baseName} (${i})" for higher indices.
|
||||
* Starts with the index provided by `startIndex` (default is 1). If not provided, uses "${baseName} (${i})".
|
||||
* @param {number} [options.maxTries=1000] The maximum number of tries to find a unique name. Default is 1000.
|
||||
* @param {number} [options.startIndex=0] The index to start with when building the name. Default is 0.
|
||||
* @param {number} [options.startIndex=1] The index to start with when building the name. Default is 1.
|
||||
* When set to 0, the intention is to also check if the basename (without applied index) is free.
|
||||
* @returns {string|null} A unique name. Null if no unique name could be found in `maxTries`.
|
||||
*/
|
||||
export function getUniqueName(baseName, exists, { nameBuilder = null, maxTries = 1000, startIndex = 0 } = {}) {
|
||||
export function getUniqueName(baseName, exists, { nameBuilder = null, maxTries = 1000, startIndex = 1 } = {}) {
|
||||
nameBuilder ??= (baseName, i) => i === 0 ? baseName : `${baseName} (${i})`;
|
||||
let i = startIndex;
|
||||
let name;
|
||||
|
||||
@@ -4123,12 +4123,11 @@ async function renameWorldInfo(name, data) {
|
||||
}
|
||||
|
||||
const entryPreviouslySelected = selected_world_info.findIndex((e) => e === oldName);
|
||||
const retargetPersonaLore = power_user.persona_description_lorebook === oldName;
|
||||
|
||||
await saveWorldInfo(newName, data, true);
|
||||
await deleteWorldInfo(oldName);
|
||||
|
||||
await updateWorldInfoLinks(oldName, newName, { retargetPersonaLore });
|
||||
await updateWorldInfoLinks(oldName, newName);
|
||||
|
||||
if (entryPreviouslySelected !== -1) {
|
||||
const wiElement = getWIElement(newName);
|
||||
@@ -4146,10 +4145,9 @@ async function renameWorldInfo(name, data) {
|
||||
* Retargets all character lore links from an old world info name to a new one, with an optional confirmation for primary lorebook links
|
||||
* @param {string} oldName Previous WI file name
|
||||
* @param {string} newName New WI file name
|
||||
* @param {{ retargetPersonaLore?: boolean }} [options] Additional relink options
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function updateWorldInfoLinks(oldName, newName, { retargetPersonaLore } = {}) {
|
||||
async function updateWorldInfoLinks(oldName, newName) {
|
||||
const existingCharLores = world_info.charLore?.filter((e) => e.extraBooks.includes(oldName));
|
||||
if (existingCharLores && existingCharLores.length > 0) {
|
||||
existingCharLores.forEach((charLore) => {
|
||||
@@ -4160,36 +4158,6 @@ async function updateWorldInfoLinks(oldName, newName, { retargetPersonaLore } =
|
||||
saveSettingsDebounced();
|
||||
}
|
||||
|
||||
// Update link for active persona
|
||||
if (retargetPersonaLore) {
|
||||
power_user.persona_description_lorebook = newName;
|
||||
const object = getOrCreatePersonaDescriptor();
|
||||
object.lorebook = newName;
|
||||
setPersonaDescription();
|
||||
saveSettingsDebounced();
|
||||
}
|
||||
|
||||
// Update links for other personas
|
||||
Object.keys(power_user.personas).forEach((persona) => {
|
||||
if (user_avatar === persona) {
|
||||
return;
|
||||
}
|
||||
const descriptor = power_user.persona_descriptions[persona];
|
||||
if (!descriptor) {
|
||||
return;
|
||||
}
|
||||
if (descriptor.lorebook === oldName) {
|
||||
descriptor.lorebook = newName;
|
||||
saveSettingsDebounced();
|
||||
}
|
||||
});
|
||||
|
||||
// update the world info key to the new name if it's still set to the old one
|
||||
if (chat_metadata[METADATA_KEY] === oldName) {
|
||||
chat_metadata[METADATA_KEY] = newName;
|
||||
await saveMetadata();
|
||||
}
|
||||
|
||||
// find all characters using the old lorebook name as their primary world
|
||||
const linkedChIDs = [];
|
||||
characters.forEach((character, chid) => {
|
||||
|
||||
@@ -556,6 +556,11 @@ async function downloadPygmalionCharacter(id) {
|
||||
*/
|
||||
function parseChubUrl(str) {
|
||||
const splitStr = str.split('/');
|
||||
const length = splitStr.length;
|
||||
|
||||
if (length < 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let domainIndex = -1;
|
||||
|
||||
@@ -565,25 +570,20 @@ function parseChubUrl(str) {
|
||||
}
|
||||
});
|
||||
|
||||
const cleanUrl = domainIndex !== -1 ? splitStr.slice(domainIndex + 1) : splitStr;
|
||||
const length = cleanUrl.length;
|
||||
const lastTwo = domainIndex !== -1 ? splitStr.slice(domainIndex + 1) : splitStr;
|
||||
|
||||
if (length < 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const firstPart = cleanUrl[0].toLowerCase();
|
||||
const firstPart = lastTwo[0].toLowerCase();
|
||||
|
||||
if (firstPart === 'characters' || firstPart === 'lorebooks') {
|
||||
const type = firstPart === 'characters' ? 'character' : 'lorebook';
|
||||
const id = type === 'character' ? cleanUrl.slice(1).join('/') : cleanUrl.join('/');
|
||||
const id = type === 'character' ? lastTwo.slice(1).join('/') : lastTwo.join('/');
|
||||
return {
|
||||
id: id,
|
||||
type: type,
|
||||
};
|
||||
} else if (length === 2) {
|
||||
return {
|
||||
id: cleanUrl.join('/'),
|
||||
id: lastTwo.join('/'),
|
||||
type: 'character',
|
||||
};
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import { Tokenizer } from '@agnai/web-tokenizers';
|
||||
import { SentencePieceProcessor } from '@agnai/sentencepiece-js';
|
||||
import tiktoken from 'tiktoken';
|
||||
|
||||
import { convertClaudePrompt } from '../prompt-converters.js';
|
||||
import { TEXTGEN_TYPES } from '../constants.js';
|
||||
import { setAdditionalHeaders } from '../additional-headers.js';
|
||||
import { getConfigValue, isValidUrl } from '../util.js';
|
||||
@@ -544,14 +545,15 @@ export function getTiktokenTokenizer(model) {
|
||||
* @returns {number} Number of tokens
|
||||
*/
|
||||
export function countWebTokenizerTokens(tokenizer, messages) {
|
||||
const jsonBody = messages.flatMap(x => Object.values(x)).join('\n\n');
|
||||
// Should be fine if we use the old conversion method instead of the messages API one i think?
|
||||
const convertedPrompt = convertClaudePrompt(messages, false, '', false, false, '', false);
|
||||
|
||||
// Fallback to strlen estimation
|
||||
if (!tokenizer) {
|
||||
return guesstimate(jsonBody);
|
||||
return guesstimate(convertedPrompt);
|
||||
}
|
||||
|
||||
const count = tokenizer.encode(jsonBody).length;
|
||||
const count = tokenizer.encode(convertedPrompt).length;
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,8 +12,6 @@ module.exports = {
|
||||
env: {
|
||||
es6: true,
|
||||
node: true,
|
||||
browser: true,
|
||||
jquery: true,
|
||||
'jest/globals': true,
|
||||
},
|
||||
parserOptions: {
|
||||
|
||||
@@ -1,246 +0,0 @@
|
||||
import { test, expect } from '@playwright/test';
|
||||
import { testSetup } from './frontent-test-utils.js';
|
||||
|
||||
test.describe('fixNestedEmphasis', () => {
|
||||
test.beforeEach(testSetup.goST);
|
||||
|
||||
test.describe('Basic nested emphasis', () => {
|
||||
test('should convert inner emphasis to bold within outer emphasis', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*She looks at you. *Are you okay?* she asks.*');
|
||||
expect(result).toBe('*She looks at you. **Are you okay?** she asks.*');
|
||||
});
|
||||
|
||||
test('should handle the issue example case', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*Some Text *Nested Emphasis Text* More text*');
|
||||
expect(result).toBe('*Some Text **Nested Emphasis Text** More text*');
|
||||
});
|
||||
|
||||
test('should handle short nested action/speech', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*action *speech* action*');
|
||||
expect(result).toBe('*action **speech** action*');
|
||||
});
|
||||
|
||||
test('should handle nested emphasis with punctuation in inner text', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*She sighs. *I never asked for this,* she mutters, turning away.*');
|
||||
expect(result).toBe('*She sighs. **I never asked for this,** she mutters, turning away.*');
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('Multiple nested pairs', () => {
|
||||
test('should handle multiple nested pairs in same outer emphasis', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*action *speech1* middle *speech2* end*');
|
||||
expect(result).toBe('*action **speech1** middle **speech2** end*');
|
||||
});
|
||||
|
||||
test('should handle multiple nested pairs with longer text', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*She said *hello* and *goodbye* to him.*');
|
||||
expect(result).toBe('*She said **hello** and **goodbye** to him.*');
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('Text that should NOT be modified', () => {
|
||||
test('should not modify simple emphasis', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*normal emphasis*');
|
||||
expect(result).toBe('*normal emphasis*');
|
||||
});
|
||||
|
||||
test('should not modify bold text', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '**bold text**');
|
||||
expect(result).toBe('**bold text**');
|
||||
});
|
||||
|
||||
test('should not modify sequential (non-nested) emphasis', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*first emphasis* and *second emphasis*');
|
||||
expect(result).toBe('*first emphasis* and *second emphasis*');
|
||||
});
|
||||
|
||||
test('should not modify already-correct bold inside italic', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*text **already bold** text*');
|
||||
expect(result).toBe('*text **already bold** text*');
|
||||
});
|
||||
|
||||
test('should not modify triple emphasis', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '***triple emphasis***');
|
||||
expect(result).toBe('***triple emphasis***');
|
||||
});
|
||||
|
||||
test('should not modify plain text', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, 'no formatting at all');
|
||||
expect(result).toBe('no formatting at all');
|
||||
});
|
||||
|
||||
test('should not modify adjacent emphasis without spaces (non-nested pattern)', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*hello*world*bye*');
|
||||
expect(result).toBe('*hello*world*bye*');
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('Nested bold (** inside **)', () => {
|
||||
test('should strip inner ** when nested inside outer **', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '**This is **bold**, is it?**');
|
||||
expect(result).toBe('**This is bold, is it?**');
|
||||
});
|
||||
|
||||
test('should strip inner ** for simple nesting', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '**text **nested** text**');
|
||||
expect(result).toBe('**text nested text**');
|
||||
});
|
||||
|
||||
test('should strip inner ** for short content', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '**a **b** c**');
|
||||
expect(result).toBe('**a b c**');
|
||||
});
|
||||
|
||||
test('should strip inner ** with longer word', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '**She said **hello** to him**');
|
||||
expect(result).toBe('**She said hello to him**');
|
||||
});
|
||||
|
||||
test('should handle multiple nested ** pairs', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '**She **whispered** and then **shouted** at him**');
|
||||
expect(result).toBe('**She whispered and then shouted at him**');
|
||||
});
|
||||
|
||||
test('should not modify normal bold (no nesting)', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '**normal bold**');
|
||||
expect(result).toBe('**normal bold**');
|
||||
});
|
||||
|
||||
test('should not modify sequential bold', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '**first** and **second**');
|
||||
expect(result).toBe('**first** and **second**');
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('Edge cases', () => {
|
||||
test('should handle nested emphasis next to sequential emphasis', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*start *nested* end* and *another*');
|
||||
expect(result).toBe('*start **nested** end* and *another*');
|
||||
});
|
||||
|
||||
test('should not modify asterisk preceded by space (non-flanking)', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*single asterisk at end *');
|
||||
expect(result).toBe('*single asterisk at end *');
|
||||
});
|
||||
|
||||
test('should handle nested emphasis adjacent to punctuation', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*She said: *"Hello!"* quietly.*');
|
||||
expect(result).toBe('*She said: **"Hello!"** quietly.*');
|
||||
});
|
||||
|
||||
test('should handle tabs as whitespace around nested emphasis', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*She looks.\t*Are you okay?*\tshe asks.*');
|
||||
expect(result).toBe('*She looks.\t**Are you okay?**\tshe asks.*');
|
||||
});
|
||||
|
||||
test('should process each line independently for multi-line text', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '*first line\n*nested* second line*');
|
||||
// Lines are processed independently - first line has only 1 asterisk, second has 3
|
||||
expect(result).toBe('*first line\n*nested* second line*');
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('Code block protection', () => {
|
||||
test('should not modify content inside inline code', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '`*code *nested* code*`');
|
||||
expect(result).toBe('`*code *nested* code*`');
|
||||
});
|
||||
|
||||
test('should not modify content inside fenced code blocks', async ({ page }) => {
|
||||
const result = await runFixNestedEmphasis(page, '```\n*code *nested* code*\n```');
|
||||
expect(result).toBe('```\n*code *nested* code*\n```');
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('Realistic roleplay examples', () => {
|
||||
test('should handle dialogue within action narration', async ({ page }) => {
|
||||
const input = '*She reaches out, her hand trembling. *Please... don\'t leave me,* she whispers, tears streaming down her face.*';
|
||||
const expected = '*She reaches out, her hand trembling. **Please... don\'t leave me,** she whispers, tears streaming down her face.*';
|
||||
const result = await runFixNestedEmphasis(page, input);
|
||||
expect(result).toBe(expected);
|
||||
});
|
||||
|
||||
test('should handle multiple dialogue lines within narration', async ({ page }) => {
|
||||
const input = '*The door creaks open. *Who\'s there?* a voice calls out from the darkness. *Show yourself!* it demands.*';
|
||||
const expected = '*The door creaks open. **Who\'s there?** a voice calls out from the darkness. **Show yourself!** it demands.*';
|
||||
const result = await runFixNestedEmphasis(page, input);
|
||||
expect(result).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('Full pipeline (fixNestedEmphasis + showdown)', () => {
|
||||
test('should render nested emphasis as italic with bold inside', async ({ page }) => {
|
||||
const html = await runFullPipeline(page, '*action *speech* action*');
|
||||
expect(html).toContain('<em>');
|
||||
expect(html).toContain('<strong>');
|
||||
expect(html).toContain('speech');
|
||||
});
|
||||
|
||||
test('should render basic nested example correctly', async ({ page }) => {
|
||||
const html = await runFullPipeline(page, '*Some Text *Nested Emphasis Text* More text*');
|
||||
expect(html).toMatch(/<em>.*<strong>Nested Emphasis Text<\/strong>.*<\/em>/);
|
||||
});
|
||||
|
||||
test('should not break normal emphasis rendering', async ({ page }) => {
|
||||
const html = await runFullPipeline(page, '*normal emphasis*');
|
||||
expect(html).toContain('<em>normal emphasis</em>');
|
||||
});
|
||||
|
||||
test('should not break bold rendering', async ({ page }) => {
|
||||
const html = await runFullPipeline(page, '**bold text**');
|
||||
expect(html).toContain('<strong>bold text</strong>');
|
||||
});
|
||||
|
||||
test('should not break sequential emphasis rendering', async ({ page }) => {
|
||||
const html = await runFullPipeline(page, '*first* and *second*');
|
||||
expect(html).toContain('<em>first</em>');
|
||||
expect(html).toContain('<em>second</em>');
|
||||
});
|
||||
|
||||
test('should render nested bold as single bold block', async ({ page }) => {
|
||||
const html = await runFullPipeline(page, '**This is **bold**, is it?**');
|
||||
expect(html).toContain('<strong>');
|
||||
expect(html).toContain('This is bold');
|
||||
expect(html).not.toContain('**');
|
||||
});
|
||||
|
||||
test('should render stripped nested bold correctly', async ({ page }) => {
|
||||
const html = await runFullPipeline(page, '**text **nested** text**');
|
||||
expect(html).toMatch(/<strong>text nested text<\/strong>/);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Run fixNestedEmphasis in the browser context.
|
||||
* @param {import('@playwright/test').Page} page
|
||||
* @param {string} input
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
async function runFixNestedEmphasis(page, input) {
|
||||
return await page.evaluate(async (text) => {
|
||||
/** @type {import('../../public/scripts/showdown-emphasis.js')} */
|
||||
const { fixNestedEmphasis } = await import('./scripts/showdown-emphasis.js');
|
||||
return fixNestedEmphasis(text);
|
||||
}, input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run fixNestedEmphasis followed by showdown conversion in the browser context.
|
||||
* @param {import('@playwright/test').Page} page
|
||||
* @param {string} input
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
async function runFullPipeline(page, input) {
|
||||
return await page.evaluate(async (text) => {
|
||||
/** @type {import('../../public/scripts/showdown-emphasis.js')} */
|
||||
const { fixNestedEmphasis } = await import('./scripts/showdown-emphasis.js');
|
||||
const { showdown } = await import('./lib.js');
|
||||
const converter = new showdown.Converter({
|
||||
literalMidWordUnderscores: true,
|
||||
simpleLineBreaks: true,
|
||||
});
|
||||
const fixed = fixNestedEmphasis(text);
|
||||
return converter.makeHtml(fixed);
|
||||
}, input);
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
import { test, expect } from '@playwright/test';
|
||||
import { testSetup } from './frontent-test-utils.js';
|
||||
|
||||
test.describe('World Info Rename Chat Lore', () => {
|
||||
test.beforeEach(testSetup.awaitST);
|
||||
|
||||
test('should retarget chat lore metadata after lorebook rename', async ({ page }) => {
|
||||
const oldName = 'STAGE_REPRO_ALPHA';
|
||||
const newName = 'STAGE_REPRO_BETA';
|
||||
|
||||
const result = await page.evaluate(async ({ oldName, newName }) => {
|
||||
const { createNewWorldInfo, deleteWorldInfo, openWorldInfoEditor, METADATA_KEY, world_names } = await import('./scripts/world-info.js');
|
||||
const { chat_metadata } = await import('./script.js');
|
||||
const { Popup, POPUP_RESULT } = await import('./scripts/popup.js');
|
||||
|
||||
async function waitFor(condition, timeoutMs = 5000, intervalMs = 50) {
|
||||
const start = Date.now();
|
||||
while (Date.now() - start < timeoutMs) {
|
||||
if (condition()) return true;
|
||||
await new Promise(resolve => setTimeout(resolve, intervalMs));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const originalInput = Popup.show.input;
|
||||
const originalConfirm = Popup.show.confirm;
|
||||
|
||||
try {
|
||||
if (world_names.includes(oldName)) {
|
||||
await deleteWorldInfo(oldName);
|
||||
}
|
||||
if (world_names.includes(newName)) {
|
||||
await deleteWorldInfo(newName);
|
||||
}
|
||||
|
||||
const created = await createNewWorldInfo(oldName, { interactive: false });
|
||||
if (!created) {
|
||||
throw new Error(`Failed to create world info '${oldName}'`);
|
||||
}
|
||||
|
||||
chat_metadata[METADATA_KEY] = oldName;
|
||||
|
||||
await openWorldInfoEditor(oldName);
|
||||
|
||||
Popup.show.input = async () => newName;
|
||||
Popup.show.confirm = async () => POPUP_RESULT.NEGATIVE;
|
||||
|
||||
const renameButton = document.querySelector('#world_popup_name_button');
|
||||
if (!renameButton) {
|
||||
throw new Error('Could not find #world_popup_name_button');
|
||||
}
|
||||
|
||||
renameButton.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true }));
|
||||
|
||||
const renameApplied = await waitFor(() => {
|
||||
const editorValue = String($('#world_editor_select').find(':selected').text());
|
||||
const optionTexts = $('#world_editor_select option').map((_, option) => String($(option).text())).get();
|
||||
return editorValue === newName && optionTexts.includes(newName) && !optionTexts.includes(oldName);
|
||||
});
|
||||
if (!renameApplied) {
|
||||
throw new Error('Rename operation did not complete in time');
|
||||
}
|
||||
|
||||
return {
|
||||
metadataAfter: chat_metadata[METADATA_KEY],
|
||||
hasOldWorld: $('#world_editor_select option').toArray().some(option => String($(option).text()) === oldName),
|
||||
hasNewWorld: $('#world_editor_select option').toArray().some(option => String($(option).text()) === newName),
|
||||
};
|
||||
} finally {
|
||||
Popup.show.input = originalInput;
|
||||
Popup.show.confirm = originalConfirm;
|
||||
}
|
||||
}, { oldName, newName });
|
||||
|
||||
expect(result.hasOldWorld).toBe(false);
|
||||
expect(result.hasNewWorld).toBe(true);
|
||||
expect(result.metadataAfter).toBe(newName);
|
||||
});
|
||||
});
|
||||
@@ -1,85 +0,0 @@
|
||||
import { test, expect } from '@playwright/test';
|
||||
import { testSetup } from './frontent-test-utils.js';
|
||||
|
||||
test.describe('World Info Rename Persona Lore', () => {
|
||||
test.beforeEach(testSetup.awaitST);
|
||||
|
||||
test('should retarget persona lore metadata after lorebook rename', async ({ page }) => {
|
||||
const oldName = 'STAGE_REPRO_PERSONA_ALPHA';
|
||||
const newName = 'STAGE_REPRO_PERSONA_BETA';
|
||||
|
||||
const result = await page.evaluate(async ({ oldName, newName }) => {
|
||||
const { createNewWorldInfo, deleteWorldInfo, openWorldInfoEditor, world_names } = await import('./scripts/world-info.js');
|
||||
const { Popup, POPUP_RESULT } = await import('./scripts/popup.js');
|
||||
const { power_user } = await import('./scripts/power-user.js');
|
||||
const { getOrCreatePersonaDescriptor, user_avatar } = await import('./scripts/personas.js');
|
||||
|
||||
async function waitFor(condition, timeoutMs = 5000, intervalMs = 50) {
|
||||
const start = Date.now();
|
||||
while (Date.now() - start < timeoutMs) {
|
||||
if (condition()) return true;
|
||||
await new Promise(resolve => setTimeout(resolve, intervalMs));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const originalInput = Popup.show.input;
|
||||
const originalConfirm = Popup.show.confirm;
|
||||
|
||||
try {
|
||||
if (world_names.includes(oldName)) {
|
||||
await deleteWorldInfo(oldName);
|
||||
}
|
||||
if (world_names.includes(newName)) {
|
||||
await deleteWorldInfo(newName);
|
||||
}
|
||||
|
||||
const created = await createNewWorldInfo(oldName, { interactive: false });
|
||||
if (!created) {
|
||||
throw new Error(`Failed to create world info '${oldName}'`);
|
||||
}
|
||||
|
||||
power_user.personas[user_avatar] = power_user.personas[user_avatar] || 'STAGE_REPRO_PERSONA';
|
||||
power_user.persona_description_lorebook = oldName;
|
||||
const descriptor = getOrCreatePersonaDescriptor();
|
||||
descriptor.lorebook = oldName;
|
||||
|
||||
await openWorldInfoEditor(oldName);
|
||||
|
||||
Popup.show.input = async () => newName;
|
||||
Popup.show.confirm = async () => POPUP_RESULT.NEGATIVE;
|
||||
|
||||
const renameButton = document.querySelector('#world_popup_name_button');
|
||||
if (!renameButton) {
|
||||
throw new Error('Could not find #world_popup_name_button');
|
||||
}
|
||||
|
||||
renameButton.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true }));
|
||||
|
||||
const renameApplied = await waitFor(() => {
|
||||
const editorValue = String($('#world_editor_select').find(':selected').text());
|
||||
const optionTexts = $('#world_editor_select option').map((_, option) => String($(option).text())).get();
|
||||
return editorValue === newName && optionTexts.includes(newName) && !optionTexts.includes(oldName);
|
||||
});
|
||||
if (!renameApplied) {
|
||||
throw new Error('Rename operation did not complete in time');
|
||||
}
|
||||
|
||||
return {
|
||||
personaLoreAfter: power_user.persona_description_lorebook,
|
||||
descriptorLoreAfter: descriptor.lorebook,
|
||||
hasOldWorld: $('#world_editor_select option').toArray().some(option => String($(option).text()) === oldName),
|
||||
hasNewWorld: $('#world_editor_select option').toArray().some(option => String($(option).text()) === newName),
|
||||
};
|
||||
} finally {
|
||||
Popup.show.input = originalInput;
|
||||
Popup.show.confirm = originalConfirm;
|
||||
}
|
||||
}, { oldName, newName });
|
||||
|
||||
expect(result.hasOldWorld).toBe(false);
|
||||
expect(result.hasNewWorld).toBe(true);
|
||||
expect(result.personaLoreAfter).toBe(newName);
|
||||
expect(result.descriptorLoreAfter).toBe(newName);
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,3 @@
|
||||
const baseURL = process.env.ST_BASE_URL || process.env.PLAYWRIGHT_BASE_URL || 'http://127.0.0.1:8000';
|
||||
|
||||
export const testSetup = {
|
||||
/**
|
||||
* Navigates to the home page without waiting for SillyTavern to load.
|
||||
@@ -17,24 +15,8 @@ export const testSetup = {
|
||||
*/
|
||||
awaitST: async ({ page }) => {
|
||||
await page.goto('/');
|
||||
if (await testSetup.isLoginPage({ page })) {
|
||||
// eslint-disable-next-line playwright/no-networkidle
|
||||
await page.waitForLoadState('networkidle');
|
||||
const userSelect = page.locator('#userList .userSelect').last();
|
||||
if (await userSelect.count()) {
|
||||
await userSelect.click();
|
||||
}
|
||||
await page.waitForURL(url => url.toString().startsWith(baseURL) && url.pathname !== '/login');
|
||||
}
|
||||
await page.click('#userList .userSelect:last-child');
|
||||
await page.waitForURL('http://127.0.0.1:8000');
|
||||
await page.waitForFunction('document.getElementById("preloader") === null', { timeout: 0 });
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks if the current page is the login page by looking for a body element with the class 'login'.
|
||||
* @param {Object} params
|
||||
* @param {import('@playwright/test').Page} params.page
|
||||
*/
|
||||
isLoginPage: async ({ page }) => {
|
||||
return await page.locator('body.login').count() > 0;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import { defineConfig } from '@playwright/test';
|
||||
|
||||
const baseURL = process.env.ST_BASE_URL || process.env.PLAYWRIGHT_BASE_URL || 'http://127.0.0.1:8000';
|
||||
|
||||
export default defineConfig({
|
||||
testMatch: '*.e2e.js',
|
||||
use: {
|
||||
baseURL,
|
||||
baseURL: 'http://127.0.0.1:8000',
|
||||
video: 'only-on-failure',
|
||||
screenshot: 'only-on-failure',
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user