refactor(randomWord): update macro registration to use new SillyTavern API

This commit is contained in:
2026-01-03 19:38:48 -06:00
parent cfef4fd73d
commit 07f6124f99
2 changed files with 96 additions and 97 deletions

162
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -2,27 +2,26 @@ import { useStore } from '@/store'
import type { generationData } from '@/types/SillyTavern' import type { generationData } from '@/types/SillyTavern'
const registerRandomWordMacros = (dictName: string) => { const registerRandomWordMacros = (dictName: string) => {
const { registerMacro } = SillyTavern.getContext() const { macros } = SillyTavern.getContext()
const words = useStore.getState().wordLists[dictName] const words = useStore.getState().wordLists[dictName]
if (words === undefined || words.length === 0) { if (words === undefined || words.length === 0) {
// If the word list is empty or undefined, don't register the macro // If the word list is empty or undefined, don't register the macro
return return
} }
registerMacro(
`randomWord::${dictName}`, macros.register(`randomWord::${dictName}`, {
() => { category: 'random',
handler: () => {
const words = useStore.getState().wordLists[dictName] as string[] const words = useStore.getState().wordLists[dictName] as string[]
return words[Math.floor(Math.random() * words.length)] as string return words[Math.floor(Math.random() * words.length)] as string
}, }
`Generates a random word from the word list '${dictName}'` })
) macros.register(`placeholder::randomWord::${dictName}`, {
registerMacro( category: 'random',
`placeholder::randomWord::${dictName}`, handler: () => {
() => {
return `%%randomWord::${dictName}%%` return `%%randomWord::${dictName}%%`
}, }
`Returns a placeholder for a random word from the word list '${dictName}'` })
)
} }
export const setupRandomWordMacros = () => { export const setupRandomWordMacros = () => {
@@ -38,10 +37,10 @@ export const setupRandomWordMacros = () => {
const deletedWordLists = oldWordListsSet.difference(wordListsSet) const deletedWordLists = oldWordListsSet.difference(wordListsSet)
const newWordLists = wordListsSet.difference(oldWordListsSet) const newWordLists = wordListsSet.difference(oldWordListsSet)
const { unregisterMacro } = SillyTavern.getContext() const { macros } = SillyTavern.getContext()
for (const wordList of deletedWordLists) { for (const wordList of deletedWordLists) {
unregisterMacro(`randomWord::${wordList}`) macros.registry.unregisterMacro(`randomWord::${wordList}`)
unregisterMacro(`placeholder::randomWord::${wordList}`) macros.registry.unregisterMacro(`placeholder::randomWord::${wordList}`)
} }
for (const wordList of newWordLists) { for (const wordList of newWordLists) {