feat: add Pick placeholder for random selection from list

This commit is contained in:
2025-12-01 23:58:27 -06:00
parent a45b56fa93
commit cfef4fd73d
5 changed files with 88 additions and 64 deletions

View File

@@ -25,3 +25,7 @@ Get a random word from a WordList. can be used as a Macro or a Placeholder. Macr
### Shuffle
Shuffle a list of values. can be used as Placeholder only. Placeholder format: `%%Shuffle::Value1::Value2::Value3;;separator%%`. Example: `%%Shuffle::Value1::Value2::Value3;;, %%` will return `Value3, Value1, Value2`.
### Pick
Pick a random value from a list of values. can be used as Placeholder only. Placeholder format: `%%Pick::Value1::Value2::Value3%%`. Example: `%%Pick::Red::Blue::Green%%` will return one of `Red`, `Blue`, or `Green` randomly.

View File

@@ -1,14 +1,9 @@
{
"lockfileVersion": 1,
"configVersion": 0,
"workspaces": {
"": {
"name": "st-randomness-helpers",
"dependencies": {
"react": "^19.1.1",
"react-dom": "^19.1.1",
"zod": "^4.0.17",
"zustand": "^5.0.7",
},
"devDependencies": {
"@biomejs/biome": "2.1.4",
"@commitlint/cli": "^19.8.1",
@@ -17,6 +12,10 @@
"@types/react-dom": "^19.1.7",
"husky": "^9.1.7",
"lint-staged": "^16.1.5",
"react": "^19.1.1",
"react-dom": "^19.1.1",
"zod": "^4.0.17",
"zustand": "^5.0.7",
},
"peerDependencies": {
"typescript": "^5",

116
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -1,9 +1,11 @@
import { setupPickPlaceholders } from '@/services/pick'
import { setupRandomWordPlaceholders } from '@/services/randomWord'
import { setupShufflePlaceholders } from '@/services/shuffle'
const initializePlaceholders = () => {
setupRandomWordPlaceholders()
setupShufflePlaceholders()
setupPickPlaceholders()
}
export default initializePlaceholders

19
src/services/pick.ts Normal file
View File

@@ -0,0 +1,19 @@
import type { generationData } from '@/types/SillyTavern'
const pickPlaceholderRegex = /%%pick::([\w\W]*?)%%/g
export const setupPickPlaceholders = () => {
const { eventTypes, eventSource } = SillyTavern.getContext()
eventSource.on(eventTypes.GENERATE_AFTER_DATA, (chat: generationData) => {
chat.prompt = chat.prompt.replaceAll(
pickPlaceholderRegex,
(_, optionsString: string) => {
const options = optionsString.split('::')
if (options.length === 0) {
return ''
}
return options[Math.floor(Math.random() * options.length)] ?? ''
}
)
})
}