From 1df209c284b97e1b482bdfa892ea17af782338f9 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 30 Jan 2025 01:58:55 +0200 Subject: [PATCH] Export variable manipulation functions to getContext --- public/scripts/st-context.js | 11 +++++++++++ public/scripts/variables.js | 8 ++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/public/scripts/st-context.js b/public/scripts/st-context.js index 1c7a46199..f69a1bd8c 100644 --- a/public/scripts/st-context.js +++ b/public/scripts/st-context.js @@ -69,6 +69,7 @@ import { textgenerationwebui_settings } from './textgen-settings.js'; import { tokenizers, getTextTokens, getTokenCount, getTokenCountAsync, getTokenizerModel } from './tokenizers.js'; import { ToolManager } from './tool-calling.js'; import { timestampToMoment, uuidv4 } from './utils.js'; +import { getGlobalVariable, getLocalVariable, setGlobalVariable, setLocalVariable } from './variables.js'; export function getContext() { return { @@ -175,6 +176,16 @@ export function getContext() { humanizedDateTime, updateMessageBlock, appendMediaToMessage, + variables: { + local: { + get: getLocalVariable, + set: setLocalVariable, + }, + global: { + get: getGlobalVariable, + set: setGlobalVariable, + }, + }, }; } diff --git a/public/scripts/variables.js b/public/scripts/variables.js index fd533f656..0a8412ffd 100644 --- a/public/scripts/variables.js +++ b/public/scripts/variables.js @@ -19,7 +19,7 @@ import { isFalseBoolean, convertValueType, isTrueBoolean } from './utils.js'; const MAX_LOOPS = 100; -function getLocalVariable(name, args = {}) { +export function getLocalVariable(name, args = {}) { if (!chat_metadata.variables) { chat_metadata.variables = {}; } @@ -45,7 +45,7 @@ function getLocalVariable(name, args = {}) { return (localVariable?.trim?.() === '' || isNaN(Number(localVariable))) ? (localVariable || '') : Number(localVariable); } -function setLocalVariable(name, value, args = {}) { +export function setLocalVariable(name, value, args = {}) { if (!name) { throw new Error('Variable name cannot be empty or undefined.'); } @@ -80,7 +80,7 @@ function setLocalVariable(name, value, args = {}) { return value; } -function getGlobalVariable(name, args = {}) { +export function getGlobalVariable(name, args = {}) { let globalVariable = extension_settings.variables.global[args.key ?? name]; if (args.index !== undefined) { try { @@ -102,7 +102,7 @@ function getGlobalVariable(name, args = {}) { return (globalVariable?.trim?.() === '' || isNaN(Number(globalVariable))) ? (globalVariable || '') : Number(globalVariable); } -function setGlobalVariable(name, value, args = {}) { +export function setGlobalVariable(name, value, args = {}) { if (!name) { throw new Error('Variable name cannot be empty or undefined.'); }