feat: replaceRandom slash command

This commit is contained in:
2026-04-16 13:34:10 -06:00
parent e9736efa05
commit cd186e5a5d
7 changed files with 230 additions and 167 deletions
+12 -20
View File
@@ -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
})
}