File size: 8,784 Bytes
6efa67a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { MacroRegistry, MacroCategory, MacroValueType } from '../engine/MacroRegistry.js';

/**
 * Registers variable-related {{...}} macros that operate on local and global
 * variables (e.g. {{setvar}}, {{getvar}}, {{incvar}}, etc.).
 */
export function registerVariableMacros() {
    const ctx = SillyTavern.getContext();

    // {{setvar::name::value}} -> '' (side-effect on local variable)
    MacroRegistry.registerMacro('setvar', {
        category: MacroCategory.VARIABLE,
        unnamedArgs: [
            {
                name: 'name',
                type: MacroValueType.STRING,
                description: 'The name of the local variable to set.',
            },
            {
                name: 'value',
                type: [MacroValueType.STRING, MacroValueType.NUMBER],
                description: 'The value to set the local variable to.',
            },
        ],
        description: 'Sets a local variable to the given value.',
        returns: '',
        exampleUsage: ['{{setvar::myvar::foo}}', '{{setvar::myintvar::3}}'],
        handler: ({ unnamedArgs: [name, value] }) => {
            ctx.variables.local.set(name, value);
            return '';
        },
    });

    // {{addvar::name::value}} -> '' (side-effect via addLocalVariable)
    MacroRegistry.registerMacro('addvar', {
        category: MacroCategory.VARIABLE,
        unnamedArgs: [
            {
                name: 'name',
                type: MacroValueType.STRING,
                description: 'The name of the local variable to add to.',
            },
            {
                name: 'value',
                type: [MacroValueType.STRING, MacroValueType.NUMBER],
                description: 'The value to add to the local variable.',
            },
        ],
        description: 'Adds a value to an existing local variable (numeric or string append). If the variable does not exist, it will be created.',
        returns: '',
        exampleUsage: ['{{addvar::mystrvar::foo}}', '{{addvar::myintvar::3}}'],
        handler: ({ unnamedArgs: [name, value] }) => {
            ctx.variables.local.add(name, value);
            return '';
        },
    });

    // {{incvar::name}} -> returns new value
    MacroRegistry.registerMacro('incvar', {
        category: MacroCategory.VARIABLE,
        unnamedArgs: [
            {
                name: 'name',
                type: MacroValueType.STRING,
                description: 'The name of the local variable to increment.',
            },
        ],
        description: 'Increments a local variable by 1 and returns the new value. If the variable does not exist, it will be created.',
        returns: 'The new value of the local variable.',
        returnType: MacroValueType.NUMBER,
        exampleUsage: ['{{incvar::myintvar}}'],
        handler: ({ unnamedArgs: [name], normalize }) => {
            const result = ctx.variables.local.inc(name);
            return normalize(result);
        },
    });

    // {{decvar::name}} -> returns new value
    MacroRegistry.registerMacro('decvar', {
        category: MacroCategory.VARIABLE,
        unnamedArgs: [
            {
                name: 'name',
                type: MacroValueType.STRING,
                description: 'The name of the local variable to decrement.',
            },
        ],
        description: 'Decrements a local variable by 1 and returns the new value. If the variable does not exist, it will be created.',
        returns: 'The new value of the local variable.',
        returnType: MacroValueType.NUMBER,
        exampleUsage: ['{{decvar::myintvar}}'],
        handler: ({ unnamedArgs: [name], normalize }) => {
            const result = ctx.variables.local.dec(name);
            return normalize(result);
        },
    });

    // {{getvar::name}} -> returns current value
    MacroRegistry.registerMacro('getvar', {
        category: MacroCategory.VARIABLE,
        unnamedArgs: [
            {
                name: 'name',
                type: MacroValueType.STRING,
                description: 'The name of the local variable to get.',
            },
        ],
        description: 'Gets the value of a local variable.',
        returns: 'The value of the local variable.',
        returnType: [MacroValueType.STRING, MacroValueType.NUMBER],
        exampleUsage: ['{{getvar::myvar}}', '{{getvar::myintvar}}'],
        handler: ({ unnamedArgs: [name], normalize }) => {
            const result = ctx.variables.local.get(name);
            return normalize(result);
        },
    });

    // {{setglobalvar::name::value}} -> ''
    MacroRegistry.registerMacro('setglobalvar', {
        category: MacroCategory.VARIABLE,
        unnamedArgs: [
            {
                name: 'name',
                type: MacroValueType.STRING,
                description: 'The name of the global variable to set.',
            },
            {
                name: 'value',
                type: [MacroValueType.STRING, MacroValueType.NUMBER],
                description: 'The value to set the global variable to.',
            },
        ],
        description: 'Sets a global variable to the given value.',
        returns: '',
        exampleUsage: ['{{setglobalvar::myvar::foo}}', '{{setglobalvar::myintvar::3}}'],
        handler: ({ unnamedArgs: [name, value] }) => {
            ctx.variables.global.set(name, value);
            return '';
        },
    });

    // {{addglobalvar::name::value}} -> ''
    MacroRegistry.registerMacro('addglobalvar', {
        category: MacroCategory.VARIABLE,
        unnamedArgs: [
            {
                name: 'name',
                type: MacroValueType.STRING,
                description: 'The name of the global variable to add to.',
            },
            {
                name: 'value',
                type: [MacroValueType.STRING, MacroValueType.NUMBER],
                description: 'The value to add to the global variable.',
            },
        ],
        description: 'Adds a value to an existing global variable (numeric or string append). If the variable does not exist, it will be created.',
        returns: '',
        exampleUsage: ['{{addglobalvar::mystrvar::foo}}', '{{addglobalvar::myintvar::3}}'],
        handler: ({ unnamedArgs: [name, value] }) => {
            ctx.variables.global.add(name, value);
            return '';
        },
    });

    // {{incglobalvar::name}} -> returns new value
    MacroRegistry.registerMacro('incglobalvar', {
        category: MacroCategory.VARIABLE,
        unnamedArgs: [
            {
                name: 'name',
                type: MacroValueType.STRING,
                description: 'The name of the global variable to increment.',
            },
        ],
        description: 'Increments a global variable by 1 and returns the new value. If the variable does not exist, it will be created.',
        returns: 'The new value of the global variable.',
        returnType: MacroValueType.NUMBER,
        handler: ({ unnamedArgs: [name], normalize }) => {
            const result = ctx.variables.global.inc(name);
            return normalize(result);
        },
    });

    // {{decglobalvar::name}} -> returns new value
    MacroRegistry.registerMacro('decglobalvar', {
        category: MacroCategory.VARIABLE,
        unnamedArgs: [
            {
                name: 'name',
                type: MacroValueType.STRING,
                description: 'The name of the global variable to decrement.',
            },
        ],
        description: 'Decrements a global variable by 1 and returns the new value. If the variable does not exist, it will be created.',
        returns: 'The new value of the global variable.',
        returnType: MacroValueType.NUMBER,
        exampleUsage: ['{{decglobalvar::myintvar}}'],
        handler: ({ unnamedArgs: [name], normalize }) => {
            const result = ctx.variables.global.dec(name);
            return normalize(result);
        },
    });

    // {{getglobalvar::name}} -> returns current value
    MacroRegistry.registerMacro('getglobalvar', {
        category: MacroCategory.VARIABLE,
        unnamedArgs: [
            {
                name: 'name',
                type: MacroValueType.STRING,
                description: 'The name of the global variable to get.',
            },
        ],
        description: 'Gets the value of a global variable.',
        returns: 'The value of the global variable.',
        returnType: [MacroValueType.STRING, MacroValueType.NUMBER],
        exampleUsage: ['{{getglobalvar::myvar}}', '{{getglobalvar::myintvar}}'],
        handler: ({ unnamedArgs: [name], normalize }) => {
            const result = ctx.variables.global.get(name);
            return normalize(result);
        },
    });
}