Fix performance in macro substitution

This commit is contained in:
Cohee 2024-03-16 01:38:23 +02:00
parent 3032f47b29
commit d311780328
1 changed files with 22 additions and 17 deletions

View File

@ -210,11 +210,22 @@ export function evaluateMacros(content, env) {
return '';
}
// Legacy non-macro substitutions
content = content.replace(/<USER>/gi, typeof env.user === 'function' ? env.user() : env.user);
content = content.replace(/<BOT>/gi, typeof env.char === 'function' ? env.char() : env.char);
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);
// Short circuit if there are no macros
if (!content.includes('{{')) {
return content;
}
content = diceRollReplace(content);
content = replaceInstructMacros(content);
content = replaceVariableMacros(content);
content = content.replace(/{{newline}}/gi, '\n');
content = content.replace(/{{input}}/gi, String($('#send_textarea').val()));
content = content.replace(/{{input}}/gi, () => String($('#send_textarea').val()));
// Substitute passed-in variables
for (const varName in env) {
@ -225,25 +236,19 @@ export function evaluateMacros(content, env) {
}
content = content.replace(/{{maxPrompt}}/gi, () => String(getMaxContextSize()));
content = content.replace(/{{lastMessage}}/gi, getLastMessage());
content = content.replace(/{{lastMessageId}}/gi, getLastMessageId());
content = content.replace(/{{firstIncludedMessageId}}/gi, getFirstIncludedMessageId());
content = content.replace(/{{lastSwipeId}}/gi, getLastSwipeId());
content = content.replace(/{{currentSwipeId}}/gi, getCurrentSwipeId());
// Legacy non-macro substitutions
content = content.replace(/<USER>/gi, typeof env.user === 'function' ? env.user() : env.user);
content = content.replace(/<BOT>/gi, typeof env.char === 'function' ? env.char() : env.char);
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(/{{lastMessage}}/gi, () => getLastMessage());
content = content.replace(/{{lastMessageId}}/gi, () => getLastMessageId());
content = content.replace(/{{firstIncludedMessageId}}/gi, () => getFirstIncludedMessageId());
content = content.replace(/{{lastSwipeId}}/gi, () => getLastSwipeId());
content = content.replace(/{{currentSwipeId}}/gi, () => getCurrentSwipeId());
content = content.replace(/\{\{\/\/([\s\S]*?)\}\}/gm, '');
content = content.replace(/{{time}}/gi, moment().format('LT'));
content = content.replace(/{{date}}/gi, moment().format('LL'));
content = content.replace(/{{weekday}}/gi, moment().format('dddd'));
content = content.replace(/{{isotime}}/gi, moment().format('HH:mm'));
content = content.replace(/{{isodate}}/gi, moment().format('YYYY-MM-DD'));
content = content.replace(/{{time}}/gi, () => moment().format('LT'));
content = content.replace(/{{date}}/gi, () => moment().format('LL'));
content = content.replace(/{{weekday}}/gi, () => moment().format('dddd'));
content = content.replace(/{{isotime}}/gi, () => moment().format('HH:mm'));
content = content.replace(/{{isodate}}/gi, () => moment().format('YYYY-MM-DD'));
content = content.replace(/{{datetimeformat +([^}]*)}}/gi, (_, format) => {
const formattedTime = moment().format(format);