Merge pull request #3205 from SillyTavern/forbid-empty-varnames

Do not allow empty or undefined variable names.
This commit is contained in:
Cohee
2024-12-19 19:16:20 +02:00
committed by GitHub
2 changed files with 13 additions and 1 deletions

View File

@@ -516,7 +516,11 @@ export function evaluateMacros(content, env, postProcessFn) {
break;
}
try {
content = content.replace(macro.regex, (...args) => postProcessFn(macro.replace(...args)));
} catch (e) {
console.warn(`Macro content can't be replaced: ${macro.regex} in ${content}`, e);
}
}
return content;

View File

@@ -46,6 +46,10 @@ function getLocalVariable(name, args = {}) {
}
function setLocalVariable(name, value, args = {}) {
if (!name) {
throw new Error('Variable name cannot be empty or undefined.');
}
if (!chat_metadata.variables) {
chat_metadata.variables = {};
}
@@ -99,6 +103,10 @@ function getGlobalVariable(name, args = {}) {
}
function setGlobalVariable(name, value, args = {}) {
if (!name) {
throw new Error('Variable name cannot be empty or undefined.');
}
if (args.index !== undefined) {
try {
let globalVariable = JSON.parse(extension_settings.variables.global[name] ?? 'null');