Fix performance in macro substitution
This commit is contained in:
parent
3032f47b29
commit
d311780328
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue