Export variable manipulation functions to getContext

1df209c284b97e1b482bdfa892ea17af782338f9

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +15 -4Showing whitespace changes
public/scripts/st-context.js+11 -0
@@ -69,6 +69,7 @@ import { textgenerationwebui_settings } from './textgen-settings.js';
6969import { tokenizers, getTextTokens, getTokenCount, getTokenCountAsync, getTokenizerModel } from './tokenizers.js';
7070import { ToolManager } from './tool-calling.js';
7171import { timestampToMoment, uuidv4 } from './utils.js';
72+import { getGlobalVariable, getLocalVariable, setGlobalVariable, setLocalVariable } from './variables.js';
7273
7374export function getContext() {
7475 return {
@@ -175,6 +176,16 @@ export function getContext() {
175176 humanizedDateTime,
176177 updateMessageBlock,
177178 appendMediaToMessage,
179+ variables: {
180+ local: {
181+ get: getLocalVariable,
182+ set: setLocalVariable,
183+ },
184+ global: {
185+ get: getGlobalVariable,
186+ set: setGlobalVariable,
187+ },
188+ },
178189 };
179190}
180191
public/scripts/variables.js+4 -4
@@ -19,7 +19,7 @@ import { isFalseBoolean, convertValueType, isTrueBoolean } from './utils.js';
1919
2020const MAX_LOOPS = 100;
2121
2222export function getLocalVariable(name, args = {}) {
2323 if (!chat_metadata.variables) {
2424 chat_metadata.variables = {};
2525 }
@@ -45,7 +45,7 @@ function getLocalVariable(name, args = {}) {
4545 return (localVariable?.trim?.() === '' || isNaN(Number(localVariable))) ? (localVariable || '') : Number(localVariable);
4646}
4747
4848export function setLocalVariable(name, value, args = {}) {
4949 if (!name) {
5050 throw new Error('Variable name cannot be empty or undefined.');
5151 }
@@ -80,7 +80,7 @@ function setLocalVariable(name, value, args = {}) {
8080 return value;
8181}
8282
8383export function getGlobalVariable(name, args = {}) {
8484 let globalVariable = extension_settings.variables.global[args.key ?? name];
8585 if (args.index !== undefined) {
8686 try {
@@ -102,7 +102,7 @@ function getGlobalVariable(name, args = {}) {
102102 return (globalVariable?.trim?.() === '' || isNaN(Number(globalVariable))) ? (globalVariable || '') : Number(globalVariable);
103103}
104104
105105export function setGlobalVariable(name, value, args = {}) {
106106 if (!name) {
107107 throw new Error('Variable name cannot be empty or undefined.');
108108 }