feat: shuffle placeholder

This commit is contained in:
2025-08-15 00:07:18 -06:00
parent 9ae8be6a50
commit c6346e9119
4 changed files with 96 additions and 52 deletions

104
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -1,7 +1,9 @@
import { setupRandomWordMacros } from '@/services/randomWord'
import { setupShuffleMacros } from '@/services/shuffle'
const initializeMacros = () => {
setupRandomWordMacros()
setupShuffleMacros()
}
export default initializeMacros

View File

@@ -1,7 +1,9 @@
import { setupRandomWordPlaceholders } from '@/services/randomWord'
import { setupShufflePlaceholders } from '@/services/shuffle'
const initializePlaceholders = () => {
setupRandomWordPlaceholders()
setupShufflePlaceholders()
}
export default initializePlaceholders

40
src/services/shuffle.ts Normal file
View File

@@ -0,0 +1,40 @@
import type { generationData } from '@/types/SillyTavern'
const fishersYatesShuffle = <Type>(arr: Type[]): Type[] => {
const shuffled = [...arr]
for (let index = shuffled.length - 1; index > 0; index--) {
const secondIndex = Math.floor(Math.random() * (index + 1))
// @ts-ignore We know index and secondIndex are valid
;[shuffled[index], shuffled[secondIndex]] = [
shuffled[secondIndex],
shuffled[index]
]
}
return shuffled
}
export const setupShuffleMacros = () => {
const { registerMacro } = SillyTavern.getContext()
registerMacro(
'placeholder::shuffle',
'%%shuffle::option1::option2;;separator%%',
'Returns a Shuffle placeholder'
)
}
const shufflePlaceholderRegex = /%%shuffle::([\w\W]*?)(;;[\w\W]*?)?%%/g
export const setupShufflePlaceholders = () => {
const { eventTypes, eventSource } = SillyTavern.getContext()
eventSource.on(eventTypes.GENERATE_AFTER_DATA, (chat: generationData) => {
chat.prompt = chat.prompt.replaceAll(
shufflePlaceholderRegex,
(_, toBeshuffleed: string, separator?: string) => {
const options = toBeshuffleed.split('::')
return fishersYatesShuffle(options).join(
separator !== undefined ? separator.substring(2) : ''
)
}
)
})
}