Merge branch 'staging' into quad-sample

This commit is contained in:
Alexander Abushady 2024-01-28 19:20:20 -05:00
commit d1f41a4191
2 changed files with 62 additions and 45 deletions

View File

@ -2035,18 +2035,19 @@ function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll = true
const messageId = forceId ?? chat.length - 1; const messageId = forceId ?? chat.length - 1;
$('#chat').find(`[mesid="${messageId}"]`).find('.mes_text').append(messageText); $('#chat').find(`[mesid="${messageId}"]`).find('.mes_text').append(messageText);
appendMediaToMessage(mes, newMessage); appendMediaToMessage(mes, newMessage);
hideSwipeButtons(newMessageId); hideSwipeButtons();
} }
addCopyToCodeBlocks(newMessage); addCopyToCodeBlocks(newMessage);
// Don't scroll if not inserting last
if (!insertAfter && !insertBefore && scroll) {
$('#chat .mes').last().addClass('last_mes'); $('#chat .mes').last().addClass('last_mes');
$('#chat .mes').eq(-2).removeClass('last_mes'); $('#chat .mes').eq(-2).removeClass('last_mes');
hideSwipeButtons(newMessageId); hideSwipeButtons();
showSwipeButtons(newMessageId); showSwipeButtons();
// Don't scroll if not inserting last
if (!insertAfter && !insertBefore && scroll) {
scrollChatToBottom(); scrollChatToBottom();
} }
} }
@ -2139,10 +2140,42 @@ function scrollChatToBottom() {
* @param {*} _name2 - The name of the character. Uses global name2 if not provided. * @param {*} _name2 - The name of the character. Uses global name2 if not provided.
* @param {*} _original - The original message for {{original}} substitution. * @param {*} _original - The original message for {{original}} substitution.
* @param {*} _group - The group members list for {{group}} substitution. * @param {*} _group - The group members list for {{group}} substitution.
* @param {boolean} _replaceCharacterCard - Whether to replace character card macros.
* @returns {string} The string with substituted parameters. * @returns {string} The string with substituted parameters.
*/ */
function substituteParams(content, _name1, _name2, _original, _group, _replaceCharacterCard = true) { function substituteParams(content, _name1, _name2, _original, _group, _replaceCharacterCard = true) {
return evaluateMacros(content, _name1 ?? name1, _name2 ?? name2, _original, _group ?? name2, _replaceCharacterCard); const environment = {};
if (typeof _original === 'string') {
let originalSubstituted = false;
environment.original = () => {
if (originalSubstituted) {
return '';
}
originalSubstituted = true;
return _original;
};
}
if (_replaceCharacterCard) {
const fields = getCharacterCardFields();
environment.charPrompt = fields.system || '';
environment.charJailbreak = fields.jailbreak || '';
environment.description = fields.description || '';
environment.personality = fields.personality || '';
environment.scenario = fields.scenario || '';
environment.persona = fields.persona || '';
environment.mesExamples = fields.mesExamples || '';
}
// Must be substituted last so that they're replaced inside {{description}}
// TODO: evaluate macros recursively so we don't need to rely on substitution order
environment.user = _name1 ?? name1;
environment.char = _name2 ?? name2;
environment.group = environment.charIfNotGroup = _group ?? name2;
return evaluateMacros(content, environment);
} }
@ -2442,7 +2475,7 @@ function showStopButton() {
function hideStopButton() { function hideStopButton() {
// prevent NOOP, because hideStopButton() gets called multiple times // prevent NOOP, because hideStopButton() gets called multiple times
if($('#mes_stop').css('display') !== 'none') { if ($('#mes_stop').css('display') !== 'none') {
$('#mes_stop').css({ 'display': 'none' }); $('#mes_stop').css({ 'display': 'none' });
eventSource.emit(event_types.GENERATION_ENDED, chat.length); eventSource.emit(event_types.GENERATION_ENDED, chat.length);
} }
@ -6475,7 +6508,7 @@ function callPopup(text, type, inputValue = '', { okButton, rows, wide, large }
}); });
} }
function showSwipeButtons(id = chat.length - 1) { function showSwipeButtons() {
if (chat.length === 0) { if (chat.length === 0) {
return; return;
} }
@ -6483,10 +6516,9 @@ function showSwipeButtons(id = chat.length - 1) {
if ( if (
chat[chat.length - 1].is_system || chat[chat.length - 1].is_system ||
!swipes || !swipes ||
$('.mes:last').attr('mesid') < 0 || Number($('.mes:last').attr('mesid')) < 0 ||
chat[chat.length - 1].is_user || chat[chat.length - 1].is_user ||
chat[chat.length - 1].extra?.image || chat[chat.length - 1].extra?.image ||
id < 0 ||
(selected_group && is_group_generating) (selected_group && is_group_generating)
) { return; } ) { return; }
@ -6505,7 +6537,7 @@ function showSwipeButtons(id = chat.length - 1) {
chat[chat.length - 1]['swipes'][0] = chat[chat.length - 1]['mes']; //assign swipe array with last message from chat chat[chat.length - 1]['swipes'][0] = chat[chat.length - 1]['mes']; //assign swipe array with last message from chat
} }
const currentMessage = $('#chat').children().filter(`[mesid="${id}"]`); const currentMessage = $('#chat').children().filter(`[mesid="${chat.length - 1}"]`);
const swipeId = chat[chat.length - 1].swipe_id; const swipeId = chat[chat.length - 1].swipe_id;
var swipesCounterHTML = (`${(swipeId + 1)}/${(chat[chat.length - 1].swipes.length)}`); var swipesCounterHTML = (`${(swipeId + 1)}/${(chat[chat.length - 1].swipes.length)}`);
@ -6530,10 +6562,10 @@ function showSwipeButtons(id = chat.length - 1) {
//console.log(chat[chat.length - 1].swipes.length); //console.log(chat[chat.length - 1].swipes.length);
} }
function hideSwipeButtons(id = chat.length - 1) { function hideSwipeButtons() {
//console.log('hideswipebuttons entered'); //console.log('hideswipebuttons entered');
$('#chat').children().filter(`[mesid="${id}"]`).children('.swipe_right').css('display', 'none'); $('#chat').find('.swipe_right').css('display', 'none');
$('#chat').children().filter(`[mesid="${id}"]`).children('.swipe_left').css('display', 'none'); $('#chat').find('.swipe_left').css('display', 'none');
} }
export async function saveMetadata() { export async function saveMetadata() {

View File

@ -1,4 +1,4 @@
import { chat, main_api, getMaxContextSize, getCharacterCardFields } from '../script.js'; import { chat, main_api, getMaxContextSize } from '../script.js';
import { timestampToMoment, isDigitsOnly } from './utils.js'; import { timestampToMoment, isDigitsOnly } from './utils.js';
import { textgenerationwebui_banned_in_macros } from './textgen-settings.js'; import { textgenerationwebui_banned_in_macros } from './textgen-settings.js';
import { replaceInstructMacros } from './instruct-mode.js'; import { replaceInstructMacros } from './instruct-mode.js';
@ -201,56 +201,41 @@ function diceRollReplace(input, invalidRollPlaceholder = '') {
/** /**
* Substitutes {{macro}} parameters in a string. * Substitutes {{macro}} parameters in a string.
* @param {string} content - The string to substitute parameters in. * @param {string} content - The string to substitute parameters in.
* @param {*} _name1 - The name of the user. * @param {Object<string, *>} env - Map of macro names to the values they'll be substituted with. If the param
* @param {*} _name2 - The name of the character. * values are functions, those functions will be called and their return values are used.
* @param {*} _original - The original message for {{original}} substitution.
* @param {*} _group - The group members list for {{group}} substitution.
* @param {boolean} _replaceCharacterCard - Whether to replace character card macros.
* @returns {string} The string with substituted parameters. * @returns {string} The string with substituted parameters.
*/ */
export function evaluateMacros(content, _name1, _name2, _original, _group, _replaceCharacterCard = true) { export function evaluateMacros(content, env) {
if (!content) { if (!content) {
return ''; return '';
} }
// Replace {{original}} with the original message
// Note: only replace the first instance of {{original}}
// This will hopefully prevent the abuse
if (typeof _original === 'string') {
content = content.replace(/{{original}}/i, _original);
}
content = diceRollReplace(content); content = diceRollReplace(content);
content = replaceInstructMacros(content); content = replaceInstructMacros(content);
content = replaceVariableMacros(content); content = replaceVariableMacros(content);
content = content.replace(/{{newline}}/gi, '\n'); content = content.replace(/{{newline}}/gi, '\n');
content = content.replace(/{{input}}/gi, String($('#send_textarea').val())); content = content.replace(/{{input}}/gi, String($('#send_textarea').val()));
if (_replaceCharacterCard) { // Substitute passed-in variables
const fields = getCharacterCardFields(); for (const varName in env) {
content = content.replace(/{{charPrompt}}/gi, fields.system || ''); if (!Object.hasOwn(env, varName)) continue;
content = content.replace(/{{charJailbreak}}/gi, fields.jailbreak || '');
content = content.replace(/{{description}}/gi, fields.description || ''); const param = env[varName];
content = content.replace(/{{personality}}/gi, fields.personality || ''); content = content.replace(new RegExp(`{{${varName}}}`, 'gi'), param);
content = content.replace(/{{scenario}}/gi, fields.scenario || '');
content = content.replace(/{{persona}}/gi, fields.persona || '');
content = content.replace(/{{mesExamples}}/gi, fields.mesExamples || '');
} }
content = content.replace(/{{maxPrompt}}/gi, () => String(getMaxContextSize())); content = content.replace(/{{maxPrompt}}/gi, () => String(getMaxContextSize()));
content = content.replace(/{{user}}/gi, _name1);
content = content.replace(/{{char}}/gi, _name2);
content = content.replace(/{{charIfNotGroup}}/gi, _group);
content = content.replace(/{{group}}/gi, _group);
content = content.replace(/{{lastMessage}}/gi, getLastMessage()); content = content.replace(/{{lastMessage}}/gi, getLastMessage());
content = content.replace(/{{lastMessageId}}/gi, getLastMessageId()); content = content.replace(/{{lastMessageId}}/gi, getLastMessageId());
content = content.replace(/{{firstIncludedMessageId}}/gi, getFirstIncludedMessageId()); content = content.replace(/{{firstIncludedMessageId}}/gi, getFirstIncludedMessageId());
content = content.replace(/{{lastSwipeId}}/gi, getLastSwipeId()); content = content.replace(/{{lastSwipeId}}/gi, getLastSwipeId());
content = content.replace(/{{currentSwipeId}}/gi, getCurrentSwipeId()); content = content.replace(/{{currentSwipeId}}/gi, getCurrentSwipeId());
content = content.replace(/<USER>/gi, _name1); // Legacy non-macro substitutions
content = content.replace(/<BOT>/gi, _name2); content = content.replace(/<USER>/gi, typeof env.user === 'function' ? env.user() : env.user);
content = content.replace(/<CHARIFNOTGROUP>/gi, _group); content = content.replace(/<BOT>/gi, typeof env.char === 'function' ? env.char() : env.char);
content = content.replace(/<GROUP>/gi, _group); content = content.replace(/<CHARIFNOTGROUP>/gi, typeof env.group === 'function' ? env.group() : env.group);
content = content.replace(/<GROUP>/gi, typeof env.group === 'function' ? env.group() : env.group);
content = content.replace(/\{\{\/\/([\s\S]*?)\}\}/gm, ''); content = content.replace(/\{\{\/\/([\s\S]*?)\}\}/gm, '');