refactor: move randomWord to own service
This commit is contained in:
174
dist/index.js
vendored
174
dist/index.js
vendored
File diff suppressed because one or more lines are too long
@@ -1,5 +1,7 @@
|
|||||||
import renderSettingsGui from '@/gui'
|
import renderSettingsGui from '@/gui'
|
||||||
import initializeMacros from '@/macros/initializeMacros'
|
import initializeMacros from '@/macros/initializeMacros'
|
||||||
|
|
||||||
|
// Initialize the React GUI
|
||||||
renderSettingsGui()
|
renderSettingsGui()
|
||||||
|
// Setup the macros
|
||||||
initializeMacros()
|
initializeMacros()
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import randomWord from '@/macros/randomWord'
|
import { setupRandomWordMacros } from '@/services/randomWord'
|
||||||
|
|
||||||
const initializeMacros = () => {
|
const initializeMacros = () => {
|
||||||
randomWord()
|
setupRandomWordMacros()
|
||||||
}
|
}
|
||||||
|
|
||||||
export default initializeMacros
|
export default initializeMacros
|
||||||
|
|||||||
@@ -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
|
|
||||||
70
src/services/randomWord.ts
Normal file
70
src/services/randomWord.ts
Normal 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)] ?? ''
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user