refactor: support array prompts in placeholder services
This commit is contained in:
+32
-13
@@ -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
|
||||
})
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user