feat: replaceRandom slash command
This commit is contained in:
+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
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user