Merge pull request #2731 from SillyTavern/fix-pipe-types Add type conversion for /setvar commands with index
Signed| @@ -36,6 +36,8 @@ export const enumIcons = { | ||
| 36 | 36 | |
| 37 | 37 | true: '✔️', |
| 38 | 38 | false: '❌', |
| 39 | + null: '🚫', | |
| 40 | + undefined: '❓', | |
| 39 | 41 | |
| 40 | 42 | // Value types |
| 41 | 43 | boolean: '🔲', |
| @@ -230,4 +232,19 @@ export const commonEnumProviders = { | ||
| 230 | 232 | enumTypes.enum, '💉'); |
| 231 | 233 | }); |
| 232 | 234 | }, |
| 235 | + | |
| 236 | + /** | |
| 237 | + * Gets somewhat recognizable STscript types. | |
| 238 | + * | |
| 239 | + * @returns {SlashCommandEnumValue[]} | |
| 240 | + */ | |
| 241 | + types: () => [ | |
| 242 | + new SlashCommandEnumValue('string', null, enumTypes.type, enumIcons.string), | |
| 243 | + new SlashCommandEnumValue('number', null, enumTypes.type, enumIcons.number), | |
| 244 | + new SlashCommandEnumValue('boolean', null, enumTypes.type, enumIcons.boolean), | |
| 245 | + new SlashCommandEnumValue('array', null, enumTypes.type, enumIcons.array), | |
| 246 | + new SlashCommandEnumValue('object', null, enumTypes.type, enumIcons.dictionary), | |
| 247 | + new SlashCommandEnumValue('null', null, enumTypes.type, enumIcons.null), | |
| 248 | + new SlashCommandEnumValue('undefined', null, enumTypes.type, enumIcons.undefined), | |
| 249 | + ], | |
| 233 | 250 | }; |
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | import { SlashCommandClosure } from './SlashCommandClosure.js'; |
| 2 | +import { convertValueType } from '../utils.js'; | |
| 2 | 3 | |
| 3 | 4 | export class SlashCommandScope { |
| 4 | 5 | /**@type {string[]}*/ variableNames = []; |
| @@ -55,7 +56,7 @@ export class SlashCommandScope { | ||
| 55 | 56 | if (this.existsVariableInScope(key)) throw new SlashCommandScopeVariableExistsError(`Variable named "${key}" already exists.`); |
| 56 | 57 | this.variables[key] = value; |
| 57 | 58 | } |
| 58 | 59 | setVariable(key, value, index = null, type = null) { |
| 59 | 60 | if (this.existsVariableInScope(key)) { |
| 60 | 61 | if (index !== null && index !== undefined) { |
| 61 | 62 | let v = this.variables[key]; |
| @@ -63,13 +64,13 @@ export class SlashCommandScope { | ||
| 63 | 64 | v = JSON.parse(v); |
| 64 | 65 | const numIndex = Number(index); |
| 65 | 66 | if (Number.isNaN(numIndex)) { |
| 66 | 67 | v[index] = convertValueType(value, type); |
| 67 | 68 | } else { |
| 68 | 69 | v[numIndex] = convertValueType(value, type); |
| 69 | 70 | } |
| 70 | 71 | v = JSON.stringify(v); |
| 71 | 72 | } catch { |
| 72 | 73 | v[index] = convertValueType(value, type); |
| 73 | 74 | } |
| 74 | 75 | this.variables[key] = v; |
| 75 | 76 | } else { |
| @@ -78,7 +79,7 @@ export class SlashCommandScope { | ||
| 78 | 79 | return value; |
| 79 | 80 | } |
| 80 | 81 | if (this.parent) { |
| 81 | 82 | return this.parent.setVariable(key, value, index, type); |
| 82 | 83 | } |
| 83 | 84 | throw new SlashCommandScopeVariableNotFoundError(`No such variable: "${key}"`); |
| 84 | 85 | } |
| @@ -4,6 +4,7 @@ import { isMobile } from './RossAscends-mods.js'; | ||
| 4 | 4 | import { collapseNewlines } from './power-user.js'; |
| 5 | 5 | import { debounce_timeout } from './constants.js'; |
| 6 | 6 | import { Popup, POPUP_RESULT, POPUP_TYPE } from './popup.js'; |
| 7 | +import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js'; | |
| 7 | 8 | |
| 8 | 9 | /** |
| 9 | 10 | * Pagination status string template. |
| @@ -34,6 +35,74 @@ export function isValidUrl(value) { | ||
| 34 | 35 | } |
| 35 | 36 | |
| 36 | 37 | /** |
| 38 | + * Converts string to a value of a given type. Includes pythonista-friendly aliases. | |
| 39 | + * @param {string|SlashCommandClosure} value String value | |
| 40 | + * @param {string} type Type to convert to | |
| 41 | + * @returns {any} Converted value | |
| 42 | + */ | |
| 43 | +export function convertValueType(value, type) { | |
| 44 | + if (value instanceof SlashCommandClosure || typeof type !== 'string') { | |
| 45 | + return value; | |
| 46 | + } | |
| 47 | + | |
| 48 | + switch (type.trim().toLowerCase()) { | |
| 49 | + case 'string': | |
| 50 | + case 'str': | |
| 51 | + return String(value); | |
| 52 | + | |
| 53 | + case 'null': | |
| 54 | + return null; | |
| 55 | + | |
| 56 | + case 'undefined': | |
| 57 | + case 'none': | |
| 58 | + return undefined; | |
| 59 | + | |
| 60 | + case 'number': | |
| 61 | + return Number(value); | |
| 62 | + | |
| 63 | + case 'int': | |
| 64 | + return parseInt(value, 10); | |
| 65 | + | |
| 66 | + case 'float': | |
| 67 | + return parseFloat(value); | |
| 68 | + | |
| 69 | + case 'boolean': | |
| 70 | + case 'bool': | |
| 71 | + return isTrueBoolean(value); | |
| 72 | + | |
| 73 | + case 'list': | |
| 74 | + case 'array': | |
| 75 | + try { | |
| 76 | + const parsedArray = JSON.parse(value); | |
| 77 | + if (Array.isArray(parsedArray)) { | |
| 78 | + return parsedArray; | |
| 79 | + } | |
| 80 | + // The value is not an array | |
| 81 | + return []; | |
| 82 | + } catch { | |
| 83 | + return []; | |
| 84 | + } | |
| 85 | + | |
| 86 | + case 'object': | |
| 87 | + case 'dict': | |
| 88 | + case 'dictionary': | |
| 89 | + try { | |
| 90 | + const parsedObject = JSON.parse(value); | |
| 91 | + if (typeof parsedObject === 'object') { | |
| 92 | + return parsedObject; | |
| 93 | + } | |
| 94 | + // The value is not an object | |
| 95 | + return {}; | |
| 96 | + } catch { | |
| 97 | + return {}; | |
| 98 | + } | |
| 99 | + | |
| 100 | + default: | |
| 101 | + return value; | |
| 102 | + } | |
| 103 | +} | |
| 104 | + | |
| 105 | +/** | |
| 37 | 106 | * Parses ranges like 10-20 or 10. |
| 38 | 107 | * Range is inclusive. Start must be less than end. |
| 39 | 108 | * Returns null if invalid. |
| @@ -11,7 +11,7 @@ import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCom | ||
| 11 | 11 | import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js'; |
| 12 | 12 | import { PARSER_FLAG, SlashCommandParser } from './slash-commands/SlashCommandParser.js'; |
| 13 | 13 | import { SlashCommandScope } from './slash-commands/SlashCommandScope.js'; |
| 14 | 14 | import { isFalseBoolean, convertValueType } from './utils.js'; |
| 15 | 15 | |
| 16 | 16 | /** @typedef {import('./slash-commands/SlashCommandParser.js').NamedArguments} NamedArguments */ |
| 17 | 17 | /** @typedef {import('./slash-commands/SlashCommand.js').UnnamedArguments} UnnamedArguments */ |
| @@ -57,12 +57,12 @@ function setLocalVariable(name, value, args = {}) { | ||
| 57 | 57 | if (localVariable === null) { |
| 58 | 58 | localVariable = {}; |
| 59 | 59 | } |
| 60 | 60 | localVariable[args.index] = convertValueType(value, args.as); |
| 61 | 61 | } else { |
| 62 | 62 | if (localVariable === null) { |
| 63 | 63 | localVariable = []; |
| 64 | 64 | } |
| 65 | 65 | localVariable[numIndex] = convertValueType(value, args.as); |
| 66 | 66 | } |
| 67 | 67 | chat_metadata.variables[name] = JSON.stringify(localVariable); |
| 68 | 68 | } catch { |
| @@ -106,12 +106,12 @@ function setGlobalVariable(name, value, args = {}) { | ||
| 106 | 106 | if (globalVariable === null) { |
| 107 | 107 | globalVariable = {}; |
| 108 | 108 | } |
| 109 | 109 | globalVariable[args.index] = convertValueType(value, args.as); |
| 110 | 110 | } else { |
| 111 | 111 | if (globalVariable === null) { |
| 112 | 112 | globalVariable = []; |
| 113 | 113 | } |
| 114 | 114 | globalVariable[numIndex] = convertValueType(value, args.as); |
| 115 | 115 | } |
| 116 | 116 | extension_settings.variables.global[name] = JSON.stringify(globalVariable); |
| 117 | 117 | } catch { |
| @@ -667,23 +667,28 @@ function parseNumericSeries(value, scope = null) { | ||
| 667 | 667 | } |
| 668 | 668 | |
| 669 | 669 | function performOperation(value, operation, singleOperand = false, scope = null) { |
| 670 | 670 | iffunction getResult(!value) { |
| 671 | - return 0; | |
| 671 | + if (!value) { | |
| 672 | - } | |
| 672 | + return 0; | |
| 673 | + } | |
| 673 | 674 | |
| 674 | 675 | const array = parseNumericSeries(value, scope); |
| 675 | 676 | |
| 676 | 677 | if (array.length === 0) { |
| 677 | 678 | return 0; |
| 678 | 679 | } |
| 679 | 680 | |
| 680 | 681 | const result = singleOperand ? operation(array[0]) : operation(array); |
| 681 | 682 | |
| 682 | 683 | if (isNaN(result) || !isFinite(result)) { |
| 683 | 684 | return 0; |
| 685 | + } | |
| 686 | + | |
| 687 | + return result; | |
| 684 | 688 | } |
| 685 | 689 | |
| 686 | 690 | returnconst result = getResult(); |
| 691 | + return String(result); | |
| 687 | 692 | } |
| 688 | 693 | |
| 689 | 694 | function addValuesCallback(args, value) { |
| @@ -836,7 +841,7 @@ function varCallback(args, value) { | ||
| 836 | 841 | if (typeof key != 'string') throw new Error('Key must be a string'); |
| 837 | 842 | if (args._hasUnnamedArgument) { |
| 838 | 843 | const val = typeof value[0] == 'string' ? value.join(' ') : value[0]; |
| 839 | 844 | args._scope.setVariable(key, val, args.index, args.as); |
| 840 | 845 | return val; |
| 841 | 846 | } else { |
| 842 | 847 | return args._scope.getVariable(key, args.index); |
| @@ -846,7 +851,7 @@ function varCallback(args, value) { | ||
| 846 | 851 | if (typeof key != 'string') throw new Error('Key must be a string'); |
| 847 | 852 | if (value.length > 0) { |
| 848 | 853 | const val = typeof value[0] == 'string' ? value.join(' ') : value[0]; |
| 849 | 854 | args._scope.setVariable(key, val, args.index, args.as); |
| 850 | 855 | return val; |
| 851 | 856 | } else { |
| 852 | 857 | return args._scope.getVariable(key, args.index); |
| @@ -901,6 +906,14 @@ export function registerVariableCommands() { | ||
| 901 | 906 | new SlashCommandNamedArgument( |
| 902 | 907 | 'index', 'list index', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.STRING], false, |
| 903 | 908 | ), |
| 909 | + SlashCommandNamedArgument.fromProps({ | |
| 910 | + name: 'as', | |
| 911 | + description: 'change the type of the value when used with index', | |
| 912 | + forceEnum: true, | |
| 913 | + enumProvider: commonEnumProviders.types, | |
| 914 | + isRequired: false, | |
| 915 | + defaultValue: 'string', | |
| 916 | + }), | |
| 904 | 917 | ], |
| 905 | 918 | unnamedArgumentList: [ |
| 906 | 919 | new SlashCommandArgument( |
| @@ -910,6 +923,7 @@ export function registerVariableCommands() { | ||
| 910 | 923 | helpString: ` |
| 911 | 924 | <div> |
| 912 | 925 | Set a local variable value and pass it down the pipe. The <code>index</code> argument is optional. |
| 926 | + To convert the value to a specific JSON type when using <code>index</code>, use the <code>as</code> argument. | |
| 913 | 927 | </div> |
| 914 | 928 | <div> |
| 915 | 929 | <strong>Example:</strong> |
| @@ -917,6 +931,9 @@ export function registerVariableCommands() { | ||
| 917 | 931 | <li> |
| 918 | 932 | <pre><code class="language-stscript">/setvar key=color green</code></pre> |
| 919 | 933 | </li> |
| 934 | + <li> | |
| 935 | + <pre><code class="language-stscript">/setvar key=ages index=John as=number 21</code></pre> | |
| 936 | + </li> | |
| 920 | 937 | </ul> |
| 921 | 938 | </div> |
| 922 | 939 | `, |
| @@ -1015,6 +1032,14 @@ export function registerVariableCommands() { | ||
| 1015 | 1032 | new SlashCommandNamedArgument( |
| 1016 | 1033 | 'index', 'list index', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.STRING], false, |
| 1017 | 1034 | ), |
| 1035 | + SlashCommandNamedArgument.fromProps({ | |
| 1036 | + name: 'as', | |
| 1037 | + description: 'change the type of the value when used with index', | |
| 1038 | + forceEnum: true, | |
| 1039 | + enumProvider: commonEnumProviders.types, | |
| 1040 | + isRequired: false, | |
| 1041 | + defaultValue: 'string', | |
| 1042 | + }), | |
| 1018 | 1043 | ], |
| 1019 | 1044 | unnamedArgumentList: [ |
| 1020 | 1045 | new SlashCommandArgument( |
| @@ -1024,6 +1049,7 @@ export function registerVariableCommands() { | ||
| 1024 | 1049 | helpString: ` |
| 1025 | 1050 | <div> |
| 1026 | 1051 | Set a global variable value and pass it down the pipe. The <code>index</code> argument is optional. |
| 1052 | + To convert the value to a specific JSON type when using <code>index</code>, use the <code>as</code> argument. | |
| 1027 | 1053 | </div> |
| 1028 | 1054 | <div> |
| 1029 | 1055 | <strong>Example:</strong> |
| @@ -1031,6 +1057,9 @@ export function registerVariableCommands() { | ||
| 1031 | 1057 | <li> |
| 1032 | 1058 | <pre><code class="language-stscript">/setglobalvar key=color green</code></pre> |
| 1033 | 1059 | </li> |
| 1060 | + <li> | |
| 1061 | + <pre><code class="language-stscript">/setglobalvar key=ages index=John as=number 21</code></pre> | |
| 1062 | + </li> | |
| 1034 | 1063 | </ul> |
| 1035 | 1064 | </div> |
| 1036 | 1065 | `, |
| @@ -2030,6 +2059,14 @@ export function registerVariableCommands() { | ||
| 2030 | 2059 | false, // isRequired |
| 2031 | 2060 | false, // acceptsMultiple |
| 2032 | 2061 | ), |
| 2062 | + SlashCommandNamedArgument.fromProps({ | |
| 2063 | + name: 'as', | |
| 2064 | + description: 'change the type of the value when used with index', | |
| 2065 | + forceEnum: true, | |
| 2066 | + enumProvider: commonEnumProviders.types, | |
| 2067 | + isRequired: false, | |
| 2068 | + defaultValue: 'string', | |
| 2069 | + }), | |
| 2033 | 2070 | ], |
| 2034 | 2071 | unnamedArgumentList: [ |
| 2035 | 2072 | SlashCommandArgument.fromProps({ |
| @@ -2049,7 +2086,8 @@ export function registerVariableCommands() { | ||
| 2049 | 2086 | splitUnnamedArgumentCount: 1, |
| 2050 | 2087 | helpString: ` |
| 2051 | 2088 | <div> |
| 2052 | - Get or set a variable. | |
| 2089 | + Get or set a variable. Use <code>index</code> to access elements of a JSON-serialized list or dictionary. | |
| 2090 | + To convert the value to a specific JSON type when using with <code>index</code>, use the <code>as</code> argument. | |
| 2053 | 2091 | </div> |
| 2054 | 2092 | <div> |
| 2055 | 2093 | <strong>Examples:</strong> |
| @@ -2060,6 +2098,9 @@ export function registerVariableCommands() { | ||
| 2060 | 2098 | <li> |
| 2061 | 2099 | <pre><code class="language-stscript">/let x foo | /var key=x foo bar | /var x | /echo</code></pre> |
| 2062 | 2100 | </li> |
| 2101 | + <li> | |
| 2102 | + <pre><code class="language-stscript">/let x {} | /var index=cool as=number x 1337 | /echo {{var::x}}</code></pre> | |
| 2103 | + </li> | |
| 2063 | 2104 | </ul> |
| 2064 | 2105 | </div> |
| 2065 | 2106 | `, |