Merge pull request #1995 from Wolfsblvt/pick-macro
Add {{pick}} macro replacement
This commit is contained in:
commit
3b333d3803
|
@ -1,5 +1,5 @@
|
|||
import { chat, main_api, getMaxContextSize } from '../script.js';
|
||||
import { timestampToMoment, isDigitsOnly } from './utils.js';
|
||||
import { chat, main_api, getMaxContextSize, getCurrentChatId } from '../script.js';
|
||||
import { timestampToMoment, isDigitsOnly, getStringHash } from './utils.js';
|
||||
import { textgenerationwebui_banned_in_macros } from './textgen-settings.js';
|
||||
import { replaceInstructMacros } from './instruct-mode.js';
|
||||
import { replaceVariableMacros } from './variables.js';
|
||||
|
@ -190,7 +190,10 @@ function randomReplace(input, emptyListPlaceholder = '') {
|
|||
|
||||
input = input.replace(randomPatternNew, (match, listString) => {
|
||||
//split on double colons instead of commas to allow for commas inside random items
|
||||
const list = listString.split('::').filter(item => item.length > 0);
|
||||
const list = listString.includes('::')
|
||||
? listString.split('::').filter(item => item.length > 0)
|
||||
: listString.split(',').map(item => item.trim()).filter(item => item.length > 0);
|
||||
|
||||
if (list.length === 0) {
|
||||
return emptyListPlaceholder;
|
||||
}
|
||||
|
@ -211,6 +214,29 @@ function randomReplace(input, emptyListPlaceholder = '') {
|
|||
return input;
|
||||
}
|
||||
|
||||
function pickReplace(input, rawContent, emptyListPlaceholder = '') {
|
||||
const pickPattern = /{{pick\s?::\s?([^}]+)}}/gi;
|
||||
const chatIdHash = getStringHash(getCurrentChatId());
|
||||
const rawContentHash = getStringHash(rawContent);
|
||||
|
||||
return input.replace(pickPattern, (match, listString, offset) => {
|
||||
const list = listString.includes('::')
|
||||
? listString.split('::').filter(item => item.length > 0)
|
||||
: listString.split(',').map(item => item.trim()).filter(item => item.length > 0);
|
||||
|
||||
if (list.length === 0) {
|
||||
return emptyListPlaceholder;
|
||||
}
|
||||
|
||||
const combinedSeedString = `${chatIdHash}-${rawContentHash}-${offset}`;
|
||||
const finalSeed = getStringHash(combinedSeedString);
|
||||
const rng = new Math.seedrandom(finalSeed);
|
||||
|
||||
const randomIndex = Math.floor(rng() * list.length);
|
||||
return list[randomIndex].trim();
|
||||
});
|
||||
}
|
||||
|
||||
function diceRollReplace(input, invalidRollPlaceholder = '') {
|
||||
const rollPattern = /{{roll[ : ]([^}]+)}}/gi;
|
||||
|
||||
|
@ -245,6 +271,8 @@ export function evaluateMacros(content, env) {
|
|||
return '';
|
||||
}
|
||||
|
||||
const rawContent = content;
|
||||
|
||||
// Legacy non-macro substitutions
|
||||
content = content.replace(/<USER>/gi, typeof env.user === 'function' ? env.user() : env.user);
|
||||
content = content.replace(/<BOT>/gi, typeof env.char === 'function' ? env.char() : env.char);
|
||||
|
@ -300,5 +328,6 @@ export function evaluateMacros(content, env) {
|
|||
});
|
||||
content = bannedWordsReplace(content);
|
||||
content = randomReplace(content);
|
||||
content = pickReplace(content, rawContent);
|
||||
return content;
|
||||
}
|
||||
|
|
|
@ -34,8 +34,9 @@
|
|||
<li><tt>{{idle_duration}}</tt> – the time since the last user message was sent</li>
|
||||
<li><tt>{{bias "text here"}}</tt> – sets a behavioral bias for the AI until the next user input. Quotes around the text are important.</li>
|
||||
<li><tt>{{roll:(formula)}}</tt> – rolls a dice. (ex: <tt>>{{roll:1d6}}</tt> will roll a 6-sided dice and return a number between 1 and 6)</li>
|
||||
<li><tt>{{random:(args)}}</tt> – returns a random item from the list. (ex: <tt>{{random:1,2,3,4}}</tt> will return 1 of the 4 numbers at random. Works with text lists too.</li>
|
||||
<li><tt>{{random::(args)}}</tt> – returns a random item from the list. (ex: <tt>{{random::1::2::3::4}}</tt> will return 1 of the 4 numbers at random. Works with text lists too.</li>
|
||||
<li><tt>{{random::(arg1)::(arg2)}}</tt> – alternative syntax for random that allows to use commas in the list items.</li>
|
||||
<li><tt>{{pick::(args)}}</tt> – picks a random item from the list. Works the same as {{random}}, with the same possible syntax options, but the pick will stay consistent for this chat once picked and won't be re-rolled on consecutive messages and prompt processing.</li>
|
||||
<li><tt>{{banned "text here"}}</tt> – dynamically add text in the quotes to banned words sequences, if Text Generation WebUI backend used. Do nothing for others backends. Can be used anywhere (Character description, WI, AN, etc.) Quotes around the text are important.</li>
|
||||
</ul>
|
||||
<div>
|
||||
|
|
Loading…
Reference in New Issue