refactor: support array prompts in placeholder services

This commit is contained in:
2026-01-19 18:40:16 -06:00
parent 0c243c17c2
commit 5e2d4821b9
4 changed files with 199 additions and 125 deletions
+32 -13
View File
@@ -1,19 +1,38 @@
import type { generationData } from '@/types/SillyTavern'
const pickPlaceholderRegex = /%%pick::([\w\W]*?)%%/g
export const setupPickPlaceholders = () => {
const { eventTypes, eventSource } = SillyTavern.getContext()
eventSource.on(eventTypes.GENERATE_AFTER_DATA, (chat: generationData) => {
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)] ?? ''
eventSource.on(
eventTypes.GENERATE_AFTER_DATA,
(
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)] ?? ''
}
)
} 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)] ?? ''
}
)
return prompt
})
}
)
})
}
)
}