refactor: simplify random word macro registration to use single macros with arguments
This commit is contained in:
152
dist/index.js
vendored
152
dist/index.js
vendored
File diff suppressed because one or more lines are too long
@@ -1,51 +1,110 @@
|
||||
import { useStore } from '@/store'
|
||||
import type { generationData } from '@/types/SillyTavern'
|
||||
|
||||
const registerRandomWordMacros = (dictName: string) => {
|
||||
const registerRandomWordMacros = () => {
|
||||
const { macros } = 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
|
||||
const wordsLists = new Map(Object.entries(useStore.getState().wordLists))
|
||||
|
||||
if (wordsLists.size === 0) {
|
||||
console.info(
|
||||
`[st-randomness-helpers] No word lists found, skipping random word macros registration`
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
macros.register(`randomWord::${dictName}`, {
|
||||
macros.register(`randomWord`, {
|
||||
category: 'random',
|
||||
handler: () => {
|
||||
const words = useStore.getState().wordLists[dictName] as string[]
|
||||
description: `Get a random word from the specified word list`,
|
||||
returns:
|
||||
'A random word from the specified word list or an empty string if the word list is empty or the dictionary name is not found',
|
||||
exampleUsage: wordsLists
|
||||
.keys()
|
||||
.toArray()
|
||||
.map((wordList) => `{{randomWord::${wordList}}}`),
|
||||
returnType: 'string',
|
||||
unnamedArgs: [
|
||||
{
|
||||
name: 'dictionary name',
|
||||
description: `The name of the word list to use. Available dictionaries: ${wordsLists
|
||||
.keys()
|
||||
.toArray()
|
||||
.map((wordList) => `'${wordList}'`)
|
||||
.join(`, `)}`,
|
||||
type: 'string',
|
||||
optional: false,
|
||||
sampleValue: wordsLists.keys().next().value ?? 'wordlist'
|
||||
}
|
||||
],
|
||||
handler: ({ unnamedArgs: [dictName] }) => {
|
||||
if (!dictName) {
|
||||
console.error(
|
||||
`[st-randomness-helpers] randomWord: No dictionary name provided, returning empty string`
|
||||
)
|
||||
return ``
|
||||
}
|
||||
if (!wordsLists.has(dictName)) {
|
||||
console.error(
|
||||
`[st-randomness-helpers] randomWord: Dictionary ${dictName} not found, returning empty string`
|
||||
)
|
||||
return ``
|
||||
}
|
||||
const words = wordsLists.get(dictName) as string[] // We know it exists
|
||||
return words[Math.floor(Math.random() * words.length)] as string
|
||||
}
|
||||
})
|
||||
macros.register(`placeholder::randomWord::${dictName}`, {
|
||||
macros.register(`randomWordPlaceholder`, {
|
||||
category: 'random',
|
||||
handler: () => {
|
||||
description: `Get a placeholder for a random word from the specified word list. The placeholder is in the form of %%randomWord::dictionary name%%`,
|
||||
returns:
|
||||
'A placeholder for a random word from the specified word list or an empty string if the word list is empty or the dictionary name is not found',
|
||||
exampleUsage: wordsLists
|
||||
.keys()
|
||||
.toArray()
|
||||
.map((wordList) => `{{randomWordPlaceholder::${wordList}}}`),
|
||||
returnType: 'string',
|
||||
unnamedArgs: [
|
||||
{
|
||||
name: 'dictionary name',
|
||||
description: `The name of the word list to use. Available dictionaries: ${wordsLists
|
||||
.keys()
|
||||
.toArray()
|
||||
.map((wordList) => `'${wordList}'`)
|
||||
.join(`, `)}`,
|
||||
type: 'string',
|
||||
optional: false,
|
||||
sampleValue: wordsLists.keys().next().value ?? 'wordlist'
|
||||
}
|
||||
],
|
||||
handler: ({ unnamedArgs: [dictName] }) => {
|
||||
if (!dictName) {
|
||||
console.error(
|
||||
`[st-randomness-helpers] randomWordPlaceholder: No dictionary name provided, returning empty string`
|
||||
)
|
||||
return ``
|
||||
}
|
||||
if (!wordsLists.has(dictName)) {
|
||||
console.error(
|
||||
`[st-randomness-helpers] randomWordPlaceholder: Dictionary ${dictName} not found, returning empty string`
|
||||
)
|
||||
return ``
|
||||
}
|
||||
return `%%randomWord::${dictName}%%`
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const setupRandomWordMacros = () => {
|
||||
for (const wordList of Object.keys(useStore.getState().wordLists)) {
|
||||
registerRandomWordMacros(wordList)
|
||||
}
|
||||
registerRandomWordMacros()
|
||||
|
||||
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 { macros } = SillyTavern.getContext()
|
||||
for (const wordList of deletedWordLists) {
|
||||
macros.registry.unregisterMacro(`randomWord::${wordList}`)
|
||||
macros.registry.unregisterMacro(`placeholder::randomWord::${wordList}`)
|
||||
}
|
||||
|
||||
for (const wordList of newWordLists) {
|
||||
registerRandomWordMacros(wordList)
|
||||
}
|
||||
macros.registry.unregisterMacro(`randomWord`)
|
||||
macros.registry.unregisterMacro(`randomWordPlaceholder`)
|
||||
|
||||
registerRandomWordMacros()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user