| 1 | import { MacroRegistry, MacroCategory, MacroValueType } from '../engine/MacroRegistry.js'; |
| 2 | |
| 3 | /** |
| 4 | * Registers variable-related {{...}} macros that operate on local and global |
| 5 | * variables (e.g. {{setvar}}, {{getvar}}, {{incvar}}, etc.). |
| 6 | */ |
| 7 | export function registerVariableMacros() { |
| 8 | const ctx = SillyTavern.getContext(); |
| 9 | |
| 10 | // {{setvar::name::value}} -> '' (side-effect on local variable) |
| 11 | MacroRegistry.registerMacro('setvar', { |
| 12 | category: MacroCategory.VARIABLE, |
| 13 | unnamedArgs: [ |
| 14 | { |
| 15 | name: 'name', |
| 16 | type: MacroValueType.STRING, |
| 17 | description: 'The name of the local variable to set.', |
| 18 | }, |
| 19 | { |
| 20 | name: 'value', |
| 21 | type: [MacroValueType.STRING, MacroValueType.NUMBER], |
| 22 | description: 'The value to set the local variable to.', |
| 23 | }, |
| 24 | ], |
| 25 | description: 'Sets a local variable to the given value.', |
| 26 | returns: '', |
| 27 | exampleUsage: ['{{setvar::myvar::foo}}', '{{setvar::myintvar::3}}'], |
| 28 | handler: ({ unnamedArgs: [name, value] }) => { |
| 29 | ctx.variables.local.set(name, value); |
| 30 | return ''; |
| 31 | }, |
| 32 | }); |
| 33 | |
| 34 | // {{addvar::name::value}} -> '' (side-effect via addLocalVariable) |
| 35 | MacroRegistry.registerMacro('addvar', { |
| 36 | category: MacroCategory.VARIABLE, |
| 37 | unnamedArgs: [ |
| 38 | { |
| 39 | name: 'name', |
| 40 | type: MacroValueType.STRING, |
| 41 | description: 'The name of the local variable to add to.', |
| 42 | }, |
| 43 | { |
| 44 | name: 'value', |
| 45 | type: [MacroValueType.STRING, MacroValueType.NUMBER], |
| 46 | description: 'The value to add to the local variable.', |
| 47 | }, |
| 48 | ], |
| 49 | description: 'Adds a value to an existing local variable (numeric or string append). If the variable does not exist, it will be created.', |
| 50 | returns: '', |
| 51 | exampleUsage: ['{{addvar::mystrvar::foo}}', '{{addvar::myintvar::3}}'], |
| 52 | handler: ({ unnamedArgs: [name, value] }) => { |
| 53 | ctx.variables.local.add(name, value); |
| 54 | return ''; |
| 55 | }, |
| 56 | }); |
| 57 | |
| 58 | // {{incvar::name}} -> returns new value |
| 59 | MacroRegistry.registerMacro('incvar', { |
| 60 | category: MacroCategory.VARIABLE, |
| 61 | unnamedArgs: [ |
| 62 | { |
| 63 | name: 'name', |
| 64 | type: MacroValueType.STRING, |
| 65 | description: 'The name of the local variable to increment.', |
| 66 | }, |
| 67 | ], |
| 68 | description: 'Increments a local variable by 1 and returns the new value. If the variable does not exist, it will be created.', |
| 69 | returns: 'The new value of the local variable.', |
| 70 | returnType: MacroValueType.NUMBER, |
| 71 | exampleUsage: ['{{incvar::myintvar}}', '{{incvar some-local-int-var}}'], |
| 72 | handler: ({ unnamedArgs: [name], normalize }) => { |
| 73 | const result = ctx.variables.local.inc(name); |
| 74 | return normalize(result); |
| 75 | }, |
| 76 | }); |
| 77 | |
| 78 | // {{decvar::name}} -> returns new value |
| 79 | MacroRegistry.registerMacro('decvar', { |
| 80 | category: MacroCategory.VARIABLE, |
| 81 | unnamedArgs: [ |
| 82 | { |
| 83 | name: 'name', |
| 84 | type: MacroValueType.STRING, |
| 85 | description: 'The name of the local variable to decrement.', |
| 86 | }, |
| 87 | ], |
| 88 | description: 'Decrements a local variable by 1 and returns the new value. If the variable does not exist, it will be created.', |
| 89 | returns: 'The new value of the local variable.', |
| 90 | returnType: MacroValueType.NUMBER, |
| 91 | exampleUsage: ['{{decvar::myintvar}}', '{{decvar some-local-int-var}}'], |
| 92 | handler: ({ unnamedArgs: [name], normalize }) => { |
| 93 | const result = ctx.variables.local.dec(name); |
| 94 | return normalize(result); |
| 95 | }, |
| 96 | }); |
| 97 | |
| 98 | // {{getvar::name}} -> returns current value |
| 99 | MacroRegistry.registerMacro('getvar', { |
| 100 | category: MacroCategory.VARIABLE, |
| 101 | unnamedArgs: [ |
| 102 | { |
| 103 | name: 'name', |
| 104 | type: MacroValueType.STRING, |
| 105 | description: 'The name of the local variable to get.', |
| 106 | }, |
| 107 | ], |
| 108 | description: 'Gets the value of a local variable.', |
| 109 | returns: 'The value of the local variable.', |
| 110 | returnType: [MacroValueType.STRING, MacroValueType.NUMBER], |
| 111 | exampleUsage: ['{{getvar::myvar}}', '{{getvar myintvar}}'], |
| 112 | handler: ({ unnamedArgs: [name], normalize }) => { |
| 113 | const result = ctx.variables.local.get(name); |
| 114 | return normalize(result); |
| 115 | }, |
| 116 | }); |
| 117 | |
| 118 | // {{hasvar::name}} -> returns 'true' or 'false' |
| 119 | MacroRegistry.registerMacro('hasvar', { |
| 120 | aliases: [{ alias: 'varexists' }], |
| 121 | category: MacroCategory.VARIABLE, |
| 122 | unnamedArgs: [ |
| 123 | { |
| 124 | name: 'name', |
| 125 | type: MacroValueType.STRING, |
| 126 | description: 'The name of the local variable to check.', |
| 127 | }, |
| 128 | ], |
| 129 | description: 'Checks if a local variable exists.', |
| 130 | returns: '"true" if the variable exists, "false" otherwise.', |
| 131 | returnType: MacroValueType.STRING, |
| 132 | exampleUsage: ['{{hasvar::myvar}}', '{{hasvar some-local-var}}'], |
| 133 | handler: ({ unnamedArgs: [name] }) => { |
| 134 | return ctx.variables.local.has(name) ? 'true' : 'false'; |
| 135 | }, |
| 136 | }); |
| 137 | |
| 138 | // {{deletevar::name}} -> returns '' |
| 139 | MacroRegistry.registerMacro('deletevar', { |
| 140 | aliases: [{ alias: 'flushvar' }], |
| 141 | category: MacroCategory.VARIABLE, |
| 142 | unnamedArgs: [ |
| 143 | { |
| 144 | name: 'name', |
| 145 | type: MacroValueType.STRING, |
| 146 | description: 'The name of the local variable to delete.', |
| 147 | }, |
| 148 | ], |
| 149 | description: 'Deletes a local variable.', |
| 150 | returns: '', |
| 151 | exampleUsage: ['{{deletevar::myvar}}', '{{deletevar some-local-var}}'], |
| 152 | handler: ({ unnamedArgs: [name] }) => { |
| 153 | ctx.variables.local.del(name); |
| 154 | return ''; |
| 155 | }, |
| 156 | }); |
| 157 | |
| 158 | // {{setglobalvar::name::value}} -> '' |
| 159 | MacroRegistry.registerMacro('setglobalvar', { |
| 160 | category: MacroCategory.VARIABLE, |
| 161 | unnamedArgs: [ |
| 162 | { |
| 163 | name: 'name', |
| 164 | type: MacroValueType.STRING, |
| 165 | description: 'The name of the global variable to set.', |
| 166 | }, |
| 167 | { |
| 168 | name: 'value', |
| 169 | type: [MacroValueType.STRING, MacroValueType.NUMBER], |
| 170 | description: 'The value to set the global variable to.', |
| 171 | }, |
| 172 | ], |
| 173 | description: 'Sets a global variable to the given value.', |
| 174 | returns: '', |
| 175 | exampleUsage: ['{{setglobalvar::myvar::foo}}', '{{setglobalvar::myintvar::3}}'], |
| 176 | handler: ({ unnamedArgs: [name, value] }) => { |
| 177 | ctx.variables.global.set(name, value); |
| 178 | return ''; |
| 179 | }, |
| 180 | }); |
| 181 | |
| 182 | // {{addglobalvar::name::value}} -> '' |
| 183 | MacroRegistry.registerMacro('addglobalvar', { |
| 184 | category: MacroCategory.VARIABLE, |
| 185 | unnamedArgs: [ |
| 186 | { |
| 187 | name: 'name', |
| 188 | type: MacroValueType.STRING, |
| 189 | description: 'The name of the global variable to add to.', |
| 190 | }, |
| 191 | { |
| 192 | name: 'value', |
| 193 | type: [MacroValueType.STRING, MacroValueType.NUMBER], |
| 194 | description: 'The value to add to the global variable.', |
| 195 | }, |
| 196 | ], |
| 197 | description: 'Adds a value to an existing global variable (numeric or string append). If the variable does not exist, it will be created.', |
| 198 | returns: '', |
| 199 | exampleUsage: ['{{addglobalvar::mystrvar::foo}}', '{{addglobalvar::myintvar::3}}'], |
| 200 | handler: ({ unnamedArgs: [name, value] }) => { |
| 201 | ctx.variables.global.add(name, value); |
| 202 | return ''; |
| 203 | }, |
| 204 | }); |
| 205 | |
| 206 | // {{incglobalvar::name}} -> returns new value |
| 207 | MacroRegistry.registerMacro('incglobalvar', { |
| 208 | category: MacroCategory.VARIABLE, |
| 209 | unnamedArgs: [ |
| 210 | { |
| 211 | name: 'name', |
| 212 | type: MacroValueType.STRING, |
| 213 | description: 'The name of the global variable to increment.', |
| 214 | }, |
| 215 | ], |
| 216 | description: 'Increments a global variable by 1 and returns the new value. If the variable does not exist, it will be created.', |
| 217 | returns: 'The new value of the global variable.', |
| 218 | returnType: MacroValueType.NUMBER, |
| 219 | exampleUsage: ['{{incglobalvar::myintvar}}', '{{incglobalvar some-global-int-var}}'], |
| 220 | handler: ({ unnamedArgs: [name], normalize }) => { |
| 221 | const result = ctx.variables.global.inc(name); |
| 222 | return normalize(result); |
| 223 | }, |
| 224 | }); |
| 225 | |
| 226 | // {{decglobalvar::name}} -> returns new value |
| 227 | MacroRegistry.registerMacro('decglobalvar', { |
| 228 | category: MacroCategory.VARIABLE, |
| 229 | unnamedArgs: [ |
| 230 | { |
| 231 | name: 'name', |
| 232 | type: MacroValueType.STRING, |
| 233 | description: 'The name of the global variable to decrement.', |
| 234 | }, |
| 235 | ], |
| 236 | description: 'Decrements a global variable by 1 and returns the new value. If the variable does not exist, it will be created.', |
| 237 | returns: 'The new value of the global variable.', |
| 238 | returnType: MacroValueType.NUMBER, |
| 239 | exampleUsage: ['{{decglobalvar::myintvar}}', '{{decglobalvar some-global-int-var}}'], |
| 240 | handler: ({ unnamedArgs: [name], normalize }) => { |
| 241 | const result = ctx.variables.global.dec(name); |
| 242 | return normalize(result); |
| 243 | }, |
| 244 | }); |
| 245 | |
| 246 | // {{getglobalvar::name}} -> returns current value |
| 247 | MacroRegistry.registerMacro('getglobalvar', { |
| 248 | category: MacroCategory.VARIABLE, |
| 249 | unnamedArgs: [ |
| 250 | { |
| 251 | name: 'name', |
| 252 | type: MacroValueType.STRING, |
| 253 | description: 'The name of the global variable to get.', |
| 254 | }, |
| 255 | ], |
| 256 | description: 'Gets the value of a global variable.', |
| 257 | returns: 'The value of the global variable.', |
| 258 | returnType: [MacroValueType.STRING, MacroValueType.NUMBER], |
| 259 | exampleUsage: ['{{getglobalvar::myvar}}', '{{getglobalvar myintvar}}'], |
| 260 | handler: ({ unnamedArgs: [name], normalize }) => { |
| 261 | const result = ctx.variables.global.get(name); |
| 262 | return normalize(result); |
| 263 | }, |
| 264 | }); |
| 265 | |
| 266 | // {{hasglobalvar::name}} -> returns 'true' or 'false' |
| 267 | MacroRegistry.registerMacro('hasglobalvar', { |
| 268 | aliases: [{ alias: 'globalvarexists' }], |
| 269 | category: MacroCategory.VARIABLE, |
| 270 | unnamedArgs: [ |
| 271 | { |
| 272 | name: 'name', |
| 273 | type: MacroValueType.STRING, |
| 274 | description: 'The name of the global variable to check.', |
| 275 | }, |
| 276 | ], |
| 277 | description: 'Checks if a global variable exists.', |
| 278 | returns: '"true" if the variable exists, "false" otherwise.', |
| 279 | returnType: MacroValueType.STRING, |
| 280 | exampleUsage: ['{{hasglobalvar::myvar}}', '{{hasglobalvar some-global-var}}'], |
| 281 | handler: ({ unnamedArgs: [name] }) => { |
| 282 | return ctx.variables.global.has(name) ? 'true' : 'false'; |
| 283 | }, |
| 284 | }); |
| 285 | |
| 286 | // {{deleteglobalvar::name}} -> returns '' |
| 287 | MacroRegistry.registerMacro('deleteglobalvar', { |
| 288 | aliases: [{ alias: 'flushglobalvar' }], |
| 289 | category: MacroCategory.VARIABLE, |
| 290 | unnamedArgs: [ |
| 291 | { |
| 292 | name: 'name', |
| 293 | type: MacroValueType.STRING, |
| 294 | description: 'The name of the global variable to delete.', |
| 295 | }, |
| 296 | ], |
| 297 | description: 'Deletes a global variable.', |
| 298 | returns: '', |
| 299 | exampleUsage: ['{{deleteglobalvar::myvar}}', '{{deleteglobalvar some-global-var}}'], |
| 300 | handler: ({ unnamedArgs: [name] }) => { |
| 301 | ctx.variables.global.del(name); |
| 302 | return ''; |
| 303 | }, |
| 304 | }); |
| 305 | } |