feat: shuffle placeholder
This commit is contained in:
104
dist/index.js
vendored
104
dist/index.js
vendored
File diff suppressed because one or more lines are too long
@@ -1,7 +1,9 @@
|
|||||||
import { setupRandomWordMacros } from '@/services/randomWord'
|
import { setupRandomWordMacros } from '@/services/randomWord'
|
||||||
|
import { setupShuffleMacros } from '@/services/shuffle'
|
||||||
|
|
||||||
const initializeMacros = () => {
|
const initializeMacros = () => {
|
||||||
setupRandomWordMacros()
|
setupRandomWordMacros()
|
||||||
|
setupShuffleMacros()
|
||||||
}
|
}
|
||||||
|
|
||||||
export default initializeMacros
|
export default initializeMacros
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import { setupRandomWordPlaceholders } from '@/services/randomWord'
|
import { setupRandomWordPlaceholders } from '@/services/randomWord'
|
||||||
|
import { setupShufflePlaceholders } from '@/services/shuffle'
|
||||||
|
|
||||||
const initializePlaceholders = () => {
|
const initializePlaceholders = () => {
|
||||||
setupRandomWordPlaceholders()
|
setupRandomWordPlaceholders()
|
||||||
|
setupShufflePlaceholders()
|
||||||
}
|
}
|
||||||
|
|
||||||
export default initializePlaceholders
|
export default initializePlaceholders
|
||||||
|
|||||||
40
src/services/shuffle.ts
Normal file
40
src/services/shuffle.ts
Normal 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) : ''
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user