From a5c595d8ae8646123de128b4dfd9fbea0e492934 Mon Sep 17 00:00:00 2001 From: LenAnderson Date: Wed, 6 Dec 2023 19:03:10 +0000 Subject: [PATCH 1/3] add list accessors to /getvar and /getglobalvar --- public/scripts/variables.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/public/scripts/variables.js b/public/scripts/variables.js index 2db475abf..768c747a5 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); } @@ -536,10 +552,10 @@ function sqrtValuesCallback(value) { 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); From ea7720a7de964f7249f937dc0b5d1f8ecec73785 Mon Sep 17 00:00:00 2001 From: LenAnderson Date: Wed, 6 Dec 2023 19:03:20 +0000 Subject: [PATCH 2/3] add /len slash command --- public/scripts/variables.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/public/scripts/variables.js b/public/scripts/variables.js index 768c747a5..5ee27a6d3 100644 --- a/public/scripts/variables.js +++ b/public/scripts/variables.js @@ -549,6 +549,16 @@ 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); @@ -579,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); } From 5e282ac7b402b050c15dfb44e765ef80e904b9fd Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 6 Dec 2023 22:08:06 +0200 Subject: [PATCH 3/3] lint: infix spacing --- .eslintrc.js | 1 + public/scripts/variables.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index a7d0800a7..d8ff54937 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 5ee27a6d3..6612dd01b 100644 --- a/public/scripts/variables.js +++ b/public/scripts/variables.js @@ -2,7 +2,7 @@ import { chat_metadata, getCurrentChatId, saveSettingsDebounced, sendSystemMessa import { extension_settings, saveMetadataDebounced } from './extensions.js'; import { executeSlashCommands, registerSlashCommand } from './slash-commands.js'; -function getLocalVariable(name, args={}) { +function getLocalVariable(name, args = {}) { if (!chat_metadata.variables) { chat_metadata.variables = {}; } @@ -30,7 +30,7 @@ function setLocalVariable(name, value) { return value; } -function getGlobalVariable(name, args={}) { +function getGlobalVariable(name, args = {}) { let globalVariable = extension_settings.variables.global[name]; if (args.index !== undefined) { try {