mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Evaluate macro functions for every instance
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
import { chat, chat_metadata, main_api, getMaxContextSize, getCurrentChatId, substituteParams } from '../script.js';
|
import { chat, chat_metadata, main_api, getMaxContextSize, getCurrentChatId, substituteParams } from '../script.js';
|
||||||
import { timestampToMoment, isDigitsOnly, getStringHash, escapeRegex } from './utils.js';
|
import { timestampToMoment, isDigitsOnly, getStringHash, escapeRegex, uuidv4 } 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';
|
||||||
import { replaceVariableMacros } from './variables.js';
|
import { replaceVariableMacros } from './variables.js';
|
||||||
@ -16,14 +16,14 @@ Handlebars.registerHelper('helperMissing', function () {
|
|||||||
export class MacrosParser {
|
export class MacrosParser {
|
||||||
/**
|
/**
|
||||||
* A map of registered macros.
|
* A map of registered macros.
|
||||||
* @type {Map<string, string|(() => string)>}
|
* @type {Map<string, string|((nonce: string) => string)>}
|
||||||
*/
|
*/
|
||||||
static #macros = new Map();
|
static #macros = new Map();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a global macro that can be used anywhere where substitution is allowed.
|
* Registers a global macro that can be used anywhere where substitution is allowed.
|
||||||
* @param {string} key Macro name (key)
|
* @param {string} key Macro name (key)
|
||||||
* @param {string|(() => string)} value A string or a function that returns a string
|
* @param {string|((nonce: string) => string)} value A string or a function that returns a string
|
||||||
*/
|
*/
|
||||||
static registerMacro(key, value) {
|
static registerMacro(key, value) {
|
||||||
if (typeof key !== 'string') {
|
if (typeof key !== 'string') {
|
||||||
@ -399,15 +399,18 @@ export function evaluateMacros(content, env) {
|
|||||||
content = content.replace(/{{input}}/gi, () => String($('#send_textarea').val()));
|
content = content.replace(/{{input}}/gi, () => String($('#send_textarea').val()));
|
||||||
|
|
||||||
// Add all registered macros to the env object
|
// Add all registered macros to the env object
|
||||||
|
const nonce = uuidv4();
|
||||||
MacrosParser.populateEnv(env);
|
MacrosParser.populateEnv(env);
|
||||||
|
|
||||||
// Substitute passed-in variables
|
// Substitute passed-in variables
|
||||||
for (const varName in env) {
|
for (const varName in env) {
|
||||||
if (!Object.hasOwn(env, varName)) continue;
|
if (!Object.hasOwn(env, varName)) continue;
|
||||||
|
|
||||||
const param = env[varName];
|
content = content.replace(new RegExp(`{{${escapeRegex(varName)}}}`, 'gi'), () => {
|
||||||
const value = MacrosParser.sanitizeMacroValue(typeof param === 'function' ? param() : param);
|
const param = env[varName];
|
||||||
content = content.replace(new RegExp(`{{${escapeRegex(varName)}}}`, 'gi'), value);
|
const value = MacrosParser.sanitizeMacroValue(typeof param === 'function' ? param(nonce) : param);
|
||||||
|
return value;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
content = content.replace(/{{maxPrompt}}/gi, () => String(getMaxContextSize()));
|
content = content.replace(/{{maxPrompt}}/gi, () => String(getMaxContextSize()));
|
||||||
|
Reference in New Issue
Block a user