feat: replaceRandom slash command
This commit is contained in:
@@ -16,6 +16,14 @@ Register [macros](https://docs.sillytavern.app/usage/core-concepts/macros/) that
|
||||
|
||||
Placeholders are strings that are replaced with a value at generation time.
|
||||
|
||||
## Slash Commands
|
||||
|
||||
Register [slash commands](https://docs.sillytavern.app/usage/core-concepts/slashcommands/) that are used for [scripting](https://docs.sillytavern.app/usage/st-script/).
|
||||
|
||||
### /replaceRandom (alias: /randomReplace)
|
||||
|
||||
Replace placeholders in the provided text with random values. This command processes all supported placeholder types in a single operation. Example: `/replaceRandom I have a %%RandomWord:PetSpecies%% myself`
|
||||
|
||||
## Services
|
||||
|
||||
### RandomWord
|
||||
|
||||
Vendored
+113
-97
File diff suppressed because one or more lines are too long
@@ -1,6 +1,7 @@
|
||||
import renderSettingsGui from '@/gui'
|
||||
import initializeMacros from '@/macros/initializeMacros'
|
||||
import initializePlaceholders from '@/placeholders/initializePlaceholders'
|
||||
import initializeSlashCommands from '@/slashCommands/initializeSlashCommands'
|
||||
|
||||
// Initialize the React GUI
|
||||
renderSettingsGui()
|
||||
@@ -8,3 +9,5 @@ renderSettingsGui()
|
||||
initializeMacros()
|
||||
// Setup the placeholders
|
||||
initializePlaceholders()
|
||||
// Setup slash commands
|
||||
initializeSlashCommands()
|
||||
|
||||
+12
-20
@@ -1,5 +1,15 @@
|
||||
const pickPlaceholderRegex = /%%pick::([\w\W]*?)%%/g
|
||||
|
||||
export const replacePickPlaceholders = (text: string) => {
|
||||
return text.replaceAll(pickPlaceholderRegex, (_, optionsString: string) => {
|
||||
const options = optionsString.split('::')
|
||||
if (options.length === 0) {
|
||||
return ''
|
||||
}
|
||||
return options[Math.floor(Math.random() * options.length)] ?? ''
|
||||
})
|
||||
}
|
||||
|
||||
export const setupPickPlaceholders = () => {
|
||||
const { eventTypes, eventSource } = SillyTavern.getContext()
|
||||
eventSource.on(
|
||||
@@ -8,28 +18,10 @@ export const setupPickPlaceholders = () => {
|
||||
chat: { prompt: string } | { prompt: { role: string; content: string }[] }
|
||||
) => {
|
||||
if (typeof chat.prompt === 'string') {
|
||||
chat.prompt = chat.prompt.replaceAll(
|
||||
pickPlaceholderRegex,
|
||||
(_, optionsString: string) => {
|
||||
const options = optionsString.split('::')
|
||||
if (options.length === 0) {
|
||||
return ''
|
||||
}
|
||||
return options[Math.floor(Math.random() * options.length)] ?? ''
|
||||
}
|
||||
)
|
||||
chat.prompt = replacePickPlaceholders(chat.prompt)
|
||||
} else if (Array.isArray(chat.prompt)) {
|
||||
chat.prompt = chat.prompt.map((prompt) => {
|
||||
prompt.content = prompt.content.replaceAll(
|
||||
pickPlaceholderRegex,
|
||||
(_, optionsString: string) => {
|
||||
const options = optionsString.split('::')
|
||||
if (options.length === 0) {
|
||||
return ''
|
||||
}
|
||||
return options[Math.floor(Math.random() * options.length)] ?? ''
|
||||
}
|
||||
)
|
||||
prompt.content = replacePickPlaceholders(prompt.content)
|
||||
return prompt
|
||||
})
|
||||
}
|
||||
|
||||
+18
-32
@@ -110,6 +110,22 @@ export const setupRandomWordMacros = () => {
|
||||
|
||||
const randomWordPlaceholderRegex = /%%randomWord::([\w\W]*?)%%/g
|
||||
|
||||
export const replaceRandomWordPlaceholders = (text: string) => {
|
||||
return text.replaceAll(randomWordPlaceholderRegex, (_, wordList: string) => {
|
||||
const words = new Map(Object.entries(useStore.getState().wordLists)).get(
|
||||
wordList
|
||||
)
|
||||
if (words === undefined || words.length === 0) {
|
||||
console.error(
|
||||
`[st-randomness-helpers] randomWordPlaceholders: No words found for ${wordList}`
|
||||
)
|
||||
return ''
|
||||
}
|
||||
const word = words[Math.floor(Math.random() * words.length)]
|
||||
return word ?? ''
|
||||
})
|
||||
}
|
||||
|
||||
export const setupRandomWordPlaceholders = () => {
|
||||
const { eventTypes, eventSource } = SillyTavern.getContext()
|
||||
eventSource.on(
|
||||
@@ -118,43 +134,13 @@ export const setupRandomWordPlaceholders = () => {
|
||||
chat: { prompt: string } | { prompt: { role: string; content: string }[] }
|
||||
) => {
|
||||
if (typeof chat.prompt === 'string') {
|
||||
chat.prompt = chat.prompt.replaceAll(
|
||||
randomWordPlaceholderRegex,
|
||||
(_, wordList: string) => {
|
||||
const words = new Map(
|
||||
Object.entries(useStore.getState().wordLists)
|
||||
).get(wordList)
|
||||
if (words === undefined || words.length === 0) {
|
||||
console.error(
|
||||
`[st-randomness-helpers] randomWordPlaceholders: No words found for ${wordList}`
|
||||
)
|
||||
return ''
|
||||
}
|
||||
const word = words[Math.floor(Math.random() * words.length)]
|
||||
return word ?? ''
|
||||
}
|
||||
)
|
||||
chat.prompt = replaceRandomWordPlaceholders(chat.prompt)
|
||||
} else if (Array.isArray(chat.prompt)) {
|
||||
const newPrompt = chat.prompt.map(
|
||||
({ role, content }: { role: string; content: string }) => {
|
||||
return {
|
||||
role,
|
||||
content: content.replaceAll(
|
||||
randomWordPlaceholderRegex,
|
||||
(_, wordList: string) => {
|
||||
const words = new Map(
|
||||
Object.entries(useStore.getState().wordLists)
|
||||
).get(wordList)
|
||||
if (words === undefined || words.length === 0) {
|
||||
console.error(
|
||||
`[st-randomness-helpers] randomWordPlaceholders: No words found for ${wordList}`
|
||||
)
|
||||
return ''
|
||||
}
|
||||
const word = words[Math.floor(Math.random() * words.length)]
|
||||
return word ?? ''
|
||||
}
|
||||
)
|
||||
content: replaceRandomWordPlaceholders(content)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
+14
-18
@@ -49,6 +49,18 @@ export const setupShuffleMacro = () => {
|
||||
|
||||
const shufflePlaceholderRegex = /%%shuffle::([\w\W]*?)(;;[\w\W]*?)?%%/g
|
||||
|
||||
export const replaceShufflePlaceholders = (text: string) => {
|
||||
return text.replaceAll(
|
||||
shufflePlaceholderRegex,
|
||||
(_, toBeShuffled: string, separator?: string) => {
|
||||
const options = toBeShuffled.split('::')
|
||||
return fishersYatesShuffle(options).join(
|
||||
separator !== undefined ? separator.substring(2) : ''
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export const setupShufflePlaceholders = () => {
|
||||
const { eventTypes, eventSource } = SillyTavern.getContext()
|
||||
eventSource.on(
|
||||
@@ -57,26 +69,10 @@ export const setupShufflePlaceholders = () => {
|
||||
chat: { prompt: string } | { prompt: { role: string; content: string }[] }
|
||||
) => {
|
||||
if (typeof chat.prompt === 'string') {
|
||||
chat.prompt = chat.prompt.replaceAll(
|
||||
shufflePlaceholderRegex,
|
||||
(_, toBeshuffleed: string, separator?: string) => {
|
||||
const options = toBeshuffleed.split('::')
|
||||
return fishersYatesShuffle(options).join(
|
||||
separator !== undefined ? separator.substring(2) : ''
|
||||
)
|
||||
}
|
||||
)
|
||||
chat.prompt = replaceShufflePlaceholders(chat.prompt)
|
||||
} else if (Array.isArray(chat.prompt)) {
|
||||
chat.prompt = chat.prompt.map((prompt) => {
|
||||
prompt.content = prompt.content.replaceAll(
|
||||
shufflePlaceholderRegex,
|
||||
(_, toBeshuffleed: string, separator?: string) => {
|
||||
const options = toBeshuffleed.split('::')
|
||||
return fishersYatesShuffle(options).join(
|
||||
separator !== undefined ? separator.substring(2) : ''
|
||||
)
|
||||
}
|
||||
)
|
||||
prompt.content = replaceShufflePlaceholders(prompt.content)
|
||||
return prompt
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import { replacePickPlaceholders } from '@/services/pick'
|
||||
import { replaceRandomWordPlaceholders } from '@/services/randomWord'
|
||||
import { replaceShufflePlaceholders } from '@/services/shuffle'
|
||||
|
||||
const replacers = [
|
||||
replaceRandomWordPlaceholders,
|
||||
replacePickPlaceholders,
|
||||
replaceShufflePlaceholders
|
||||
]
|
||||
|
||||
const helpString = `<div>
|
||||
<div>Replace the placeholders in the provided text.</div>
|
||||
<div>
|
||||
<strong>Example:</strong>
|
||||
<ul>
|
||||
<li>
|
||||
<pre><code class="language-stscript">/replaceRandom I have a %%RandomWord:PetSpecies%% myself</code></pre>
|
||||
</li>
|
||||
<li>
|
||||
<pre><code class="language-stscript">/replaceRandom I looked for him in %%Shuffle::the infirmary::the classroom::the gym;; ,%%, and his room but I couldn't find him.</code></pre>
|
||||
</li>
|
||||
<li>
|
||||
<pre><code class="language-stscript">/replaceRandom I have a %%Pick::Red::Blue::Green%% hoodie.</code></pre>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>`
|
||||
const initializeSlashCommands = () => {
|
||||
const { SlashCommandParser, SlashCommand, SlashCommandArgument } =
|
||||
SillyTavern.getContext()
|
||||
SlashCommandParser.addCommandObject(
|
||||
SlashCommand.fromProps({
|
||||
name: 'randomReplace',
|
||||
aliases: ['replaceRandom'],
|
||||
unnamedArgumentList: [
|
||||
SlashCommandArgument.fromProps({
|
||||
description: 'The text to replace in placeholder format.',
|
||||
acceptsMultiple: false,
|
||||
typeList: ['string'],
|
||||
isRequired: true
|
||||
})
|
||||
],
|
||||
returns: 'The text with the placeholders replaced.',
|
||||
helpString,
|
||||
callback: (_, unnamedArgument) => {
|
||||
if (typeof unnamedArgument !== 'string') {
|
||||
return ''
|
||||
}
|
||||
|
||||
let text = unnamedArgument
|
||||
|
||||
for (const replacer of replacers) {
|
||||
text = replacer(text)
|
||||
}
|
||||
|
||||
return text
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export default initializeSlashCommands
|
||||
Reference in New Issue
Block a user