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.
|
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
|
## Services
|
||||||
|
|
||||||
### RandomWord
|
### RandomWord
|
||||||
|
|||||||
Vendored
+113
-97
File diff suppressed because one or more lines are too long
@@ -1,6 +1,7 @@
|
|||||||
import renderSettingsGui from '@/gui'
|
import renderSettingsGui from '@/gui'
|
||||||
import initializeMacros from '@/macros/initializeMacros'
|
import initializeMacros from '@/macros/initializeMacros'
|
||||||
import initializePlaceholders from '@/placeholders/initializePlaceholders'
|
import initializePlaceholders from '@/placeholders/initializePlaceholders'
|
||||||
|
import initializeSlashCommands from '@/slashCommands/initializeSlashCommands'
|
||||||
|
|
||||||
// Initialize the React GUI
|
// Initialize the React GUI
|
||||||
renderSettingsGui()
|
renderSettingsGui()
|
||||||
@@ -8,3 +9,5 @@ renderSettingsGui()
|
|||||||
initializeMacros()
|
initializeMacros()
|
||||||
// Setup the placeholders
|
// Setup the placeholders
|
||||||
initializePlaceholders()
|
initializePlaceholders()
|
||||||
|
// Setup slash commands
|
||||||
|
initializeSlashCommands()
|
||||||
|
|||||||
+12
-20
@@ -1,5 +1,15 @@
|
|||||||
const pickPlaceholderRegex = /%%pick::([\w\W]*?)%%/g
|
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 = () => {
|
export const setupPickPlaceholders = () => {
|
||||||
const { eventTypes, eventSource } = SillyTavern.getContext()
|
const { eventTypes, eventSource } = SillyTavern.getContext()
|
||||||
eventSource.on(
|
eventSource.on(
|
||||||
@@ -8,28 +18,10 @@ export const setupPickPlaceholders = () => {
|
|||||||
chat: { prompt: string } | { prompt: { role: string; content: string }[] }
|
chat: { prompt: string } | { prompt: { role: string; content: string }[] }
|
||||||
) => {
|
) => {
|
||||||
if (typeof chat.prompt === 'string') {
|
if (typeof chat.prompt === 'string') {
|
||||||
chat.prompt = chat.prompt.replaceAll(
|
chat.prompt = replacePickPlaceholders(chat.prompt)
|
||||||
pickPlaceholderRegex,
|
|
||||||
(_, optionsString: string) => {
|
|
||||||
const options = optionsString.split('::')
|
|
||||||
if (options.length === 0) {
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
return options[Math.floor(Math.random() * options.length)] ?? ''
|
|
||||||
}
|
|
||||||
)
|
|
||||||
} else if (Array.isArray(chat.prompt)) {
|
} else if (Array.isArray(chat.prompt)) {
|
||||||
chat.prompt = chat.prompt.map((prompt) => {
|
chat.prompt = chat.prompt.map((prompt) => {
|
||||||
prompt.content = prompt.content.replaceAll(
|
prompt.content = replacePickPlaceholders(prompt.content)
|
||||||
pickPlaceholderRegex,
|
|
||||||
(_, optionsString: string) => {
|
|
||||||
const options = optionsString.split('::')
|
|
||||||
if (options.length === 0) {
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
return options[Math.floor(Math.random() * options.length)] ?? ''
|
|
||||||
}
|
|
||||||
)
|
|
||||||
return prompt
|
return prompt
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-32
@@ -110,6 +110,22 @@ export const setupRandomWordMacros = () => {
|
|||||||
|
|
||||||
const randomWordPlaceholderRegex = /%%randomWord::([\w\W]*?)%%/g
|
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 = () => {
|
export const setupRandomWordPlaceholders = () => {
|
||||||
const { eventTypes, eventSource } = SillyTavern.getContext()
|
const { eventTypes, eventSource } = SillyTavern.getContext()
|
||||||
eventSource.on(
|
eventSource.on(
|
||||||
@@ -118,43 +134,13 @@ export const setupRandomWordPlaceholders = () => {
|
|||||||
chat: { prompt: string } | { prompt: { role: string; content: string }[] }
|
chat: { prompt: string } | { prompt: { role: string; content: string }[] }
|
||||||
) => {
|
) => {
|
||||||
if (typeof chat.prompt === 'string') {
|
if (typeof chat.prompt === 'string') {
|
||||||
chat.prompt = chat.prompt.replaceAll(
|
chat.prompt = replaceRandomWordPlaceholders(chat.prompt)
|
||||||
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 ?? ''
|
|
||||||
}
|
|
||||||
)
|
|
||||||
} else if (Array.isArray(chat.prompt)) {
|
} else if (Array.isArray(chat.prompt)) {
|
||||||
const newPrompt = chat.prompt.map(
|
const newPrompt = chat.prompt.map(
|
||||||
({ role, content }: { role: string; content: string }) => {
|
({ role, content }: { role: string; content: string }) => {
|
||||||
return {
|
return {
|
||||||
role,
|
role,
|
||||||
content: content.replaceAll(
|
content: replaceRandomWordPlaceholders(content)
|
||||||
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 ?? ''
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
+14
-18
@@ -49,6 +49,18 @@ export const setupShuffleMacro = () => {
|
|||||||
|
|
||||||
const shufflePlaceholderRegex = /%%shuffle::([\w\W]*?)(;;[\w\W]*?)?%%/g
|
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 = () => {
|
export const setupShufflePlaceholders = () => {
|
||||||
const { eventTypes, eventSource } = SillyTavern.getContext()
|
const { eventTypes, eventSource } = SillyTavern.getContext()
|
||||||
eventSource.on(
|
eventSource.on(
|
||||||
@@ -57,26 +69,10 @@ export const setupShufflePlaceholders = () => {
|
|||||||
chat: { prompt: string } | { prompt: { role: string; content: string }[] }
|
chat: { prompt: string } | { prompt: { role: string; content: string }[] }
|
||||||
) => {
|
) => {
|
||||||
if (typeof chat.prompt === 'string') {
|
if (typeof chat.prompt === 'string') {
|
||||||
chat.prompt = chat.prompt.replaceAll(
|
chat.prompt = replaceShufflePlaceholders(chat.prompt)
|
||||||
shufflePlaceholderRegex,
|
|
||||||
(_, toBeshuffleed: string, separator?: string) => {
|
|
||||||
const options = toBeshuffleed.split('::')
|
|
||||||
return fishersYatesShuffle(options).join(
|
|
||||||
separator !== undefined ? separator.substring(2) : ''
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
} else if (Array.isArray(chat.prompt)) {
|
} else if (Array.isArray(chat.prompt)) {
|
||||||
chat.prompt = chat.prompt.map((prompt) => {
|
chat.prompt = chat.prompt.map((prompt) => {
|
||||||
prompt.content = prompt.content.replaceAll(
|
prompt.content = replaceShufflePlaceholders(prompt.content)
|
||||||
shufflePlaceholderRegex,
|
|
||||||
(_, toBeshuffleed: string, separator?: string) => {
|
|
||||||
const options = toBeshuffleed.split('::')
|
|
||||||
return fishersYatesShuffle(options).join(
|
|
||||||
separator !== undefined ? separator.substring(2) : ''
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
return prompt
|
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