refactor: move randomWord to own service

This commit is contained in:
2025-08-14 21:22:07 -06:00
parent a3c6c9fa76
commit 9ce947075a
5 changed files with 161 additions and 132 deletions

174
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -1,5 +1,7 @@
import renderSettingsGui from '@/gui'
import initializeMacros from '@/macros/initializeMacros'
// Initialize the React GUI
renderSettingsGui()
// Setup the macros
initializeMacros()

View File

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

View File

@@ -1,43 +0,0 @@
import { useStore } from '@/store'
const randomWord = () => {
const { registerMacro, unregisterMacro } = SillyTavern.getContext()
useStore.subscribe(
(state) => Object.keys(state.wordLists),
(wordLists, oldWordLists) => {
const wordListsSet = new Set(wordLists)
const oldWordListsSet = new Set(oldWordLists)
const deletedWordLists = oldWordListsSet.difference(wordListsSet)
const newWordLists = wordListsSet.difference(oldWordListsSet)
for (const wordList of deletedWordLists) {
unregisterMacro(`rndWord::${wordList}`)
}
for (const wordList of newWordLists) {
registerMacro(
`rndWord::${wordList}`,
() => {
const words = useStore.getState().wordLists[wordList] as string[]
return words[Math.floor(Math.random() * words.length)] as string
},
`Generates a random word from the word list '${wordList}'`
)
}
}
)
for (const wordList of Object.keys(useStore.getState().wordLists)) {
registerMacro(
`rndWord::${wordList}`,
() => {
const words = useStore.getState().wordLists[wordList] as string[]
return words[Math.floor(Math.random() * words.length)] as string
},
`Generates a random word from the word list '${wordList}'`
)
}
}
export default randomWord

View File

@@ -0,0 +1,70 @@
import { useStore } from '@/store'
import type { generationData } from '@/types/SillyTavern'
const registerRandomWordMacros = (dictName: string) => {
const { registerMacro } = SillyTavern.getContext()
const words = useStore.getState().wordLists[dictName]
if (words === undefined || words.length === 0) {
// If the word list is empty or undefined, don't register the macro
return
}
registerMacro(
`randomWord::${dictName}`,
() => {
const words = useStore.getState().wordLists[dictName] as string[]
return words[Math.floor(Math.random() * words.length)] as string
},
`Generates a random word from the word list '${dictName}'`
)
registerMacro(
`placeholder::randomWord::${dictName}`,
() => {
return `%%randomWord::${dictName}%%`
},
`Returns a placeholder for a random word from the word list '${dictName}'`
)
}
export const setupRandomWordMacros = () => {
for (const wordList of Object.keys(useStore.getState().wordLists)) {
registerRandomWordMacros(wordList)
}
useStore.subscribe(
(state) => Object.keys(state.wordLists),
(wordLists, oldWordLists) => {
const wordListsSet = new Set(wordLists)
const oldWordListsSet = new Set(oldWordLists)
const deletedWordLists = oldWordListsSet.difference(wordListsSet)
const newWordLists = wordListsSet.difference(oldWordListsSet)
const { unregisterMacro } = SillyTavern.getContext()
for (const wordList of deletedWordLists) {
unregisterMacro(`randomWord::${wordList}`)
unregisterMacro(`placeholder::randomWord::${wordList}`)
}
for (const wordList of newWordLists) {
registerRandomWordMacros(wordList)
}
}
)
}
const randomWordPlaceholderRegex = /%%randomWord::([\w\W]*?)%%/g
export const setupRandomWordPlaceholders = () => {
const { eventTypes, eventSource } = SillyTavern.getContext()
eventSource.on(eventTypes.GENERATE_AFTER_DATA, (chat: generationData) => {
chat.prompt = chat.prompt.replaceAll(
randomWordPlaceholderRegex,
(_, wordList: string) => {
const words = useStore.getState().wordLists[wordList]
if (words === undefined || words.length === 0) {
return ''
}
return words[Math.floor(Math.random() * words.length)] ?? ''
}
)
})
}