mirror of
				https://github.com/SillyTavern/SillyTavern.git
				synced 2025-06-05 21:59:27 +02:00 
			
		
		
		
	Add slash command for dice rolls. Create class for worker wrappers
This commit is contained in:
		| @@ -8,12 +8,38 @@ export { | |||||||
|     defaultRequestArgs, |     defaultRequestArgs, | ||||||
|     modules, |     modules, | ||||||
|     extension_settings, |     extension_settings, | ||||||
|  |     ModuleWorkerWrapper, | ||||||
| }; | }; | ||||||
|  |  | ||||||
| let extensionNames = []; | let extensionNames = []; | ||||||
| let manifests = []; | let manifests = []; | ||||||
| const defaultUrl = "http://localhost:5100"; | const defaultUrl = "http://localhost:5100"; | ||||||
|  |  | ||||||
|  | // Disables parallel updates | ||||||
|  | class ModuleWorkerWrapper { | ||||||
|  |     constructor(callback) { | ||||||
|  |         this.isBusy = false; | ||||||
|  |         this.callback = callback; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // Called by the extension | ||||||
|  |     async update() { | ||||||
|  |         // Don't touch me I'm busy... | ||||||
|  |         if (this.isBusy) { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         // I'm free. Let's update! | ||||||
|  |         try { | ||||||
|  |             this.isBusy = true; | ||||||
|  |             await this.callback(); | ||||||
|  |         } | ||||||
|  |         finally { | ||||||
|  |             this.isBusy = false; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| const extension_settings = { | const extension_settings = { | ||||||
|     apiUrl: defaultUrl, |     apiUrl: defaultUrl, | ||||||
|     autoConnect: false, |     autoConnect: false, | ||||||
|   | |||||||
| @@ -6,11 +6,7 @@ const MODULE_NAME = 'caption'; | |||||||
| const UPDATE_INTERVAL = 1000; | const UPDATE_INTERVAL = 1000; | ||||||
|  |  | ||||||
| async function moduleWorker() { | async function moduleWorker() { | ||||||
|     const context = getContext(); |     $('#send_picture').toggle(getContext().onlineStatus !== 'no_connection'); | ||||||
|  |  | ||||||
|     context.onlineStatus === 'no_connection' |  | ||||||
|         ? $('#send_picture').hide(200) |  | ||||||
|         : $('#send_picture').show(200); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| async function setImageIcon() { | async function setImageIcon() { | ||||||
|   | |||||||
| @@ -1,21 +1,20 @@ | |||||||
| import { callPopup } from "../../../script.js"; | import { callPopup } from "../../../script.js"; | ||||||
| import { getContext } from "../../extensions.js"; | import { getContext } from "../../extensions.js"; | ||||||
|  | import { registerSlashCommand } from "../../slash-commands.js"; | ||||||
| export { MODULE_NAME }; | export { MODULE_NAME }; | ||||||
|  |  | ||||||
| const MODULE_NAME = 'dice'; | const MODULE_NAME = 'dice'; | ||||||
| const UPDATE_INTERVAL = 1000; | const UPDATE_INTERVAL = 1000; | ||||||
|  |  | ||||||
| function setDiceIcon() { | async function doDiceRoll(customDiceFormula) { | ||||||
|     const sendButton = document.getElementById('roll_dice'); |     let value = typeof customDiceFormula === 'string' ? customDiceFormula.trim() : $(this).data('value'); | ||||||
|     /* sendButton.style.backgroundImage = `url(/img/dice-solid.svg)`; */ |  | ||||||
|     //sendButton.classList.remove('spin'); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async function doDiceRoll() { |  | ||||||
|     let value = $(this).data('value'); |  | ||||||
|  |  | ||||||
|     if (value == 'custom') { |     if (value == 'custom') { | ||||||
|         value = await callPopup('Enter the dice formula:<br><i>(for example, <tt>2d6</tt>)</i>', 'input'); |         value = await callPopup('Enter the dice formula:<br><i>(for example, <tt>2d6</tt>)</i>', 'input');x | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if (!value) { | ||||||
|  |         return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const isValid = droll.validate(value); |     const isValid = droll.validate(value); | ||||||
| @@ -24,6 +23,8 @@ async function doDiceRoll() { | |||||||
|         const result = droll.roll(value); |         const result = droll.roll(value); | ||||||
|         const context = getContext(); |         const context = getContext(); | ||||||
|         context.sendSystemMessage('generic', `${context.name1} rolls a ${value}. The result is: ${result.total} (${result.rolls})`, { isSmallSys: true }); |         context.sendSystemMessage('generic', `${context.name1} rolls a ${value}. The result is: ${result.total} (${result.rolls})`, { isSmallSys: true }); | ||||||
|  |     } else { | ||||||
|  |         toastr.warning('Invalid dice formula'); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -84,17 +85,13 @@ function addDiceScript() { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function moduleWorker() { | async function moduleWorker() { | ||||||
|     const context = getContext(); |     $('#roll_dice').toggle(getContext().onlineStatus !== 'no_connection'); | ||||||
|  |  | ||||||
|     context.onlineStatus === 'no_connection' |  | ||||||
|         ? $('#roll_dice').hide(200) |  | ||||||
|         : $('#roll_dice').show(200); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| $(document).ready(function () { | jQuery(function () { | ||||||
|     addDiceScript(); |     addDiceScript(); | ||||||
|     addDiceRollButton(); |     addDiceRollButton(); | ||||||
|     setDiceIcon(); |  | ||||||
|     moduleWorker(); |     moduleWorker(); | ||||||
|     setInterval(moduleWorker, UPDATE_INTERVAL); |     setInterval(moduleWorker, UPDATE_INTERVAL); | ||||||
|  |     registerSlashCommand('roll', (_, value) => doDiceRoll(value), [], "<span class='monospace'>(dice formula)</span> – roll the dice. For example, /roll 2d6", false, true); | ||||||
| }); | }); | ||||||
| @@ -1,5 +1,5 @@ | |||||||
| import { callPopup, getRequestHeaders, saveSettingsDebounced } from "../../../script.js"; | import { callPopup, getRequestHeaders, saveSettingsDebounced } from "../../../script.js"; | ||||||
| import { getContext, getApiUrl, modules, extension_settings } from "../../extensions.js"; | import { getContext, getApiUrl, modules, extension_settings, ModuleWorkerWrapper } from "../../extensions.js"; | ||||||
| export { MODULE_NAME }; | export { MODULE_NAME }; | ||||||
|  |  | ||||||
| const MODULE_NAME = 'expressions'; | const MODULE_NAME = 'expressions'; | ||||||
| @@ -58,24 +58,6 @@ function onExpressionsShowDefaultInput() { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| let isWorkerBusy = false; |  | ||||||
|  |  | ||||||
| async function moduleWorkerWrapper() { |  | ||||||
|     // Don't touch me I'm busy... |  | ||||||
|     if (isWorkerBusy) { |  | ||||||
|         return; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     // I'm free. Let's update! |  | ||||||
|     try { |  | ||||||
|         isWorkerBusy = true; |  | ||||||
|         await moduleWorker(); |  | ||||||
|     } |  | ||||||
|     finally { |  | ||||||
|         isWorkerBusy = false; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async function moduleWorker() { | async function moduleWorker() { | ||||||
|     const context = getContext(); |     const context = getContext(); | ||||||
|  |  | ||||||
| @@ -509,6 +491,7 @@ async function onClickExpressionDelete(event) { | |||||||
|  |  | ||||||
|     addExpressionImage(); |     addExpressionImage(); | ||||||
|     addSettings(); |     addSettings(); | ||||||
|     setInterval(moduleWorkerWrapper, UPDATE_INTERVAL); |     const wrapper = new ModuleWorkerWrapper(moduleWorker); | ||||||
|     moduleWorkerWrapper(); |     setInterval(wrapper.update.bind(wrapper), UPDATE_INTERVAL); | ||||||
|  |     moduleWorker(); | ||||||
| })(); | })(); | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| import { chat_metadata, saveSettingsDebounced } from "../../../script.js"; | import { chat_metadata, saveSettingsDebounced } from "../../../script.js"; | ||||||
| import { extension_settings, getContext } from "../../extensions.js"; | import { ModuleWorkerWrapper, extension_settings, getContext } from "../../extensions.js"; | ||||||
| import { registerSlashCommand } from "../../slash-commands.js"; | import { registerSlashCommand } from "../../slash-commands.js"; | ||||||
| import { debounce } from "../../utils.js"; | import { debounce } from "../../utils.js"; | ||||||
| export { MODULE_NAME }; | export { MODULE_NAME }; | ||||||
| @@ -110,24 +110,6 @@ function loadSettings() { | |||||||
|     $('#extension_floating_default').val(extension_settings.note.default); |     $('#extension_floating_default').val(extension_settings.note.default); | ||||||
| } | } | ||||||
|  |  | ||||||
| let isWorkerBusy = false; |  | ||||||
|  |  | ||||||
| async function moduleWorkerWrapper() { |  | ||||||
|     // Don't touch me I'm busy... |  | ||||||
|     if (isWorkerBusy) { |  | ||||||
|         return; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     // I'm free. Let's update! |  | ||||||
|     try { |  | ||||||
|         isWorkerBusy = true; |  | ||||||
|         await moduleWorker(); |  | ||||||
|     } |  | ||||||
|     finally { |  | ||||||
|         isWorkerBusy = false; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async function moduleWorker() { | async function moduleWorker() { | ||||||
|     const context = getContext(); |     const context = getContext(); | ||||||
|  |  | ||||||
| @@ -226,7 +208,8 @@ async function moduleWorker() { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     addExtensionsSettings(); |     addExtensionsSettings(); | ||||||
|     setInterval(moduleWorkerWrapper, UPDATE_INTERVAL); |     const wrapper = new ModuleWorkerWrapper(moduleWorker); | ||||||
|  |     setInterval(wrapper.update.bind(wrapper), UPDATE_INTERVAL); | ||||||
|     registerSlashCommand('note', setNoteTextCommand, [], "<span class='monospace'>(text)</span> – sets an author's note for the currently selected chat", true, true); |     registerSlashCommand('note', setNoteTextCommand, [], "<span class='monospace'>(text)</span> – sets an author's note for the currently selected chat", true, true); | ||||||
|     registerSlashCommand('depth', setNoteDepthCommand, [], "<span class='monospace'>(number)</span> – sets an author's note depth for in-chat positioning", true, true); |     registerSlashCommand('depth', setNoteDepthCommand, [], "<span class='monospace'>(number)</span> – sets an author's note depth for in-chat positioning", true, true); | ||||||
|     registerSlashCommand('freq', setNoteIntervalCommand, ['interval'], "<span class='monospace'>(number)</span> – sets an author's note insertion frequency", true, true); |     registerSlashCommand('freq', setNoteIntervalCommand, ['interval'], "<span class='monospace'>(number)</span> – sets an author's note insertion frequency", true, true); | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| import { getStringHash, debounce } from "../../utils.js"; | import { getStringHash, debounce } from "../../utils.js"; | ||||||
| import { getContext, getApiUrl, extension_settings } from "../../extensions.js"; | import { getContext, getApiUrl, extension_settings, ModuleWorkerWrapper } from "../../extensions.js"; | ||||||
| import { extension_prompt_types, is_send_press, saveSettingsDebounced } from "../../../script.js"; | import { extension_prompt_types, is_send_press, saveSettingsDebounced } from "../../../script.js"; | ||||||
| export { MODULE_NAME }; | export { MODULE_NAME }; | ||||||
|  |  | ||||||
| @@ -129,24 +129,6 @@ function getLatestMemoryFromChat(chat) { | |||||||
|     return ''; |     return ''; | ||||||
| } | } | ||||||
|  |  | ||||||
| let isWorkerBusy = false; |  | ||||||
|  |  | ||||||
| async function moduleWorkerWrapper() { |  | ||||||
|     // Don't touch me I'm busy... |  | ||||||
|     if (isWorkerBusy) { |  | ||||||
|         return; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     // I'm free. Let's update! |  | ||||||
|     try { |  | ||||||
|         isWorkerBusy = true; |  | ||||||
|         await moduleWorker(); |  | ||||||
|     } |  | ||||||
|     finally { |  | ||||||
|         isWorkerBusy = false; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async function moduleWorker() { | async function moduleWorker() { | ||||||
|     const context = getContext(); |     const context = getContext(); | ||||||
|     const chat = context.chat; |     const chat = context.chat; | ||||||
| @@ -384,5 +366,6 @@ $(document).ready(function () { | |||||||
|  |  | ||||||
|     addExtensionControls(); |     addExtensionControls(); | ||||||
|     loadSettings(); |     loadSettings(); | ||||||
|     setInterval(moduleWorkerWrapper, UPDATE_INTERVAL); |     const wrapper = new ModuleWorkerWrapper(moduleWorker); | ||||||
|  |     setInterval(wrapper.update.bind(wrapper), UPDATE_INTERVAL); | ||||||
| }); | }); | ||||||
| @@ -1,5 +1,5 @@ | |||||||
| import { callPopup, cancelTtsPlay, eventSource, event_types, isMultigenEnabled, is_send_press, saveSettingsDebounced } from '../../../script.js' | import { callPopup, cancelTtsPlay, eventSource, event_types, isMultigenEnabled, is_send_press, saveSettingsDebounced } from '../../../script.js' | ||||||
| import { extension_settings, getContext } from '../../extensions.js' | import { ModuleWorkerWrapper, extension_settings, getContext } from '../../extensions.js' | ||||||
| import { getStringHash } from '../../utils.js' | import { getStringHash } from '../../utils.js' | ||||||
| import { ElevenLabsTtsProvider } from './elevenlabs.js' | import { ElevenLabsTtsProvider } from './elevenlabs.js' | ||||||
| import { SileroTtsProvider } from './silerotts.js' | import { SileroTtsProvider } from './silerotts.js' | ||||||
| @@ -38,24 +38,6 @@ async function onNarrateOneMessage() { | |||||||
|     moduleWorker(); |     moduleWorker(); | ||||||
| } | } | ||||||
|  |  | ||||||
| let isWorkerBusy = false; |  | ||||||
|  |  | ||||||
| async function moduleWorkerWrapper() { |  | ||||||
|     // Don't touch me I'm busy... |  | ||||||
|     if (isWorkerBusy) { |  | ||||||
|         return; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     // I'm free. Let's update! |  | ||||||
|     try { |  | ||||||
|         isWorkerBusy = true; |  | ||||||
|         await moduleWorker(); |  | ||||||
|     } |  | ||||||
|     finally { |  | ||||||
|         isWorkerBusy = false; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async function moduleWorker() { | async function moduleWorker() { | ||||||
|     // Primarily determining when to add new chat to the TTS queue |     // Primarily determining when to add new chat to the TTS queue | ||||||
|     const enabled = $('#tts_enabled').is(':checked') |     const enabled = $('#tts_enabled').is(':checked') | ||||||
| @@ -661,6 +643,7 @@ $(document).ready(function () { | |||||||
|     loadSettings() // Depends on Extension Controls and loadTtsProvider |     loadSettings() // Depends on Extension Controls and loadTtsProvider | ||||||
|     loadTtsProvider(extension_settings.tts.currentProvider) // No dependencies |     loadTtsProvider(extension_settings.tts.currentProvider) // No dependencies | ||||||
|     addAudioControl() // Depends on Extension Controls |     addAudioControl() // Depends on Extension Controls | ||||||
|     setInterval(moduleWorkerWrapper, UPDATE_INTERVAL) // Init depends on all the things |     const wrapper = new ModuleWorkerWrapper(moduleWorker); | ||||||
|  |     setInterval(wrapper.update.bind(wrapper), UPDATE_INTERVAL) // Init depends on all the things | ||||||
|     eventSource.on(event_types.MESSAGE_SWIPED, resetTtsPlayback); |     eventSource.on(event_types.MESSAGE_SWIPED, resetTtsPlayback); | ||||||
| }) | }) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user