diff --git a/.eslintrc.js b/.eslintrc.js
index f3efee9b7..6aef11686 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -59,6 +59,7 @@ module.exports = {
'eol-last': ['error', 'always'],
'no-trailing-spaces': 'error',
'object-curly-spacing': ['error', 'always'],
+ 'space-infix-ops': 'error',
// These rules should eventually be enabled.
'no-async-promise-executor': 'off',
diff --git a/public/scripts/variables.js b/public/scripts/variables.js
index 2db475abf..6612dd01b 100644
--- a/public/scripts/variables.js
+++ b/public/scripts/variables.js
@@ -2,12 +2,20 @@ import { chat_metadata, getCurrentChatId, saveSettingsDebounced, sendSystemMessa
import { extension_settings, saveMetadataDebounced } from './extensions.js';
import { executeSlashCommands, registerSlashCommand } from './slash-commands.js';
-function getLocalVariable(name) {
+function getLocalVariable(name, args = {}) {
if (!chat_metadata.variables) {
chat_metadata.variables = {};
}
- const localVariable = chat_metadata?.variables[name];
+ let localVariable = chat_metadata?.variables[name];
+ if (args.index !== undefined) {
+ try {
+ localVariable = JSON.parse(localVariable);
+ localVariable = localVariable[Number(args.index)];
+ } catch {
+ // that didn't work
+ }
+ }
return (localVariable === '' || isNaN(Number(localVariable))) ? (localVariable || '') : Number(localVariable);
}
@@ -22,8 +30,16 @@ function setLocalVariable(name, value) {
return value;
}
-function getGlobalVariable(name) {
- const globalVariable = extension_settings.variables.global[name];
+function getGlobalVariable(name, args = {}) {
+ let globalVariable = extension_settings.variables.global[name];
+ if (args.index !== undefined) {
+ try {
+ globalVariable = JSON.parse(globalVariable);
+ globalVariable = globalVariable[Number(args.index)];
+ } catch {
+ // that didn't work
+ }
+ }
return (globalVariable === '' || isNaN(Number(globalVariable))) ? (globalVariable || '') : Number(globalVariable);
}
@@ -533,13 +549,23 @@ function sqrtValuesCallback(value) {
return performOperation(value, Math.sqrt, true);
}
+function lenValuesCallback(value) {
+ let parsedValue = value;
+ try {
+ parsedValue = JSON.parse(value);
+ } catch {
+ // could not parse
+ }
+ return parsedValue.length;
+}
+
export function registerVariableCommands() {
registerSlashCommand('listvar', listVariablesCallback, [], ' – list registered chat variables', true, true);
registerSlashCommand('setvar', (args, value) => setLocalVariable(args.key || args.name, value), [], 'key=varname (value) – set a local variable value and pass it down the pipe, e.g. /setvar key=color green', true, true);
- registerSlashCommand('getvar', (_, value) => getLocalVariable(value), [], '(key) – get a local variable value and pass it down the pipe, e.g. /getvar height', true, true);
+ registerSlashCommand('getvar', (args, value) => getLocalVariable(value, args), [], 'index=listIndex (key) – get a local variable value and pass it down the pipe, index is optional, e.g. /getvar height or /getvar index=3 costumes', true, true);
registerSlashCommand('addvar', (args, value) => addLocalVariable(args.key || args.name, value), [], 'key=varname (increment) – add a value to a local variable and pass the result down the pipe, e.g. /addvar score 10', true, true);
registerSlashCommand('setglobalvar', (args, value) => setGlobalVariable(args.key || args.name, value), [], 'key=varname (value) – set a global variable value and pass it down the pipe, e.g. /setglobalvar key=color green', true, true);
- registerSlashCommand('getglobalvar', (_, value) => getGlobalVariable(value), [], '(key) – get a global variable value and pass it down the pipe, e.g. /getglobalvar height', true, true);
+ registerSlashCommand('getglobalvar', (args, value) => getGlobalVariable(value, args), [], 'index=listIndex (key) – get a global variable value and pass it down the pipe, index is optional, e.g. /getglobalvar height or /getglobalvar index=3 costumes', true, true);
registerSlashCommand('addglobalvar', (args, value) => addGlobalVariable(args.key || args.name, value), [], 'key=varname (increment) – add a value to a global variable and pass the result down the pipe, e.g. /addglobalvar score 10', true, true);
registerSlashCommand('incvar', (_, value) => incrementLocalVariable(value), [], '(key) – increment a local variable by 1 and pass the result down the pipe, e.g. /incvar score', true, true);
registerSlashCommand('decvar', (_, value) => decrementLocalVariable(value), [], '(key) – decrement a local variable by 1 and pass the result down the pipe, e.g. /decvar score', true, true);
@@ -563,4 +589,5 @@ export function registerVariableCommands() {
registerSlashCommand('abs', (_, value) => absValuesCallback(value), [], '(a) – performs an absolute value operation of a value and passes the result down the pipe, can use variable names, e.g. /abs i', true, true);
registerSlashCommand('sqrt', (_, value) => sqrtValuesCallback(value), [], '(a) – performs a square root operation of a value and passes the result down the pipe, can use variable names, e.g. /sqrt i', true, true);
registerSlashCommand('round', (_, value) => roundValuesCallback(value), [], '(a) – rounds a value and passes the result down the pipe, can use variable names, e.g. /round i', true, true);
+ registerSlashCommand('len', (_, value) => lenValuesCallback(value), [], '(a) – gets the length of a value and passes the result down the pipe, can use variable names, e.g. /len i', true, true);
}