#629 Random list select substitution

This commit is contained in:
Cohee
2023-07-05 17:59:53 +03:00
parent af6d9d48e0
commit f3ebea6ad8
2 changed files with 39 additions and 18 deletions

View File

@@ -101,9 +101,9 @@ import {
nai_settings,
} from "./scripts/nai-settings.js";
import {
import {
createNewBookmark,
showBookmarksButtons
showBookmarksButtons
} from "./scripts/bookmarks.js";
import {
@@ -1496,9 +1496,25 @@ function substituteParams(content, _name1, _name2, _original) {
content = content.replace(/<BOT>/gi, _name2);
content = content.replace(/{{time}}/gi, moment().format('LT'));
content = content.replace(/{{date}}/gi, moment().format('LL'));
content = randomReplace(content);
return content;
}
function randomReplace(input, emptyListPlaceholder = '') {
const randomPattern = /{{random:([^}]+)}}/g;
return input.replace(randomPattern, (match, listString) => {
const list = listString.split(',').map(item => item.trim()).filter(item => item.length > 0);
if (list.length === 0) {
return emptyListPlaceholder;
}
const randomIndex = Math.floor(Math.random() * list.length);
return list[randomIndex];
});
}
function getStoppingStrings(isImpersonate, addSpace) {
const charString = `\n${name2}:`;
const youString = `\nYou:`;
@@ -1600,7 +1616,7 @@ export function extractMessageBias(message) {
return null;
}
const forbiddenMatches = ['user', 'char', 'time', 'date'];
const forbiddenMatches = ['user', 'char', 'time', 'date', 'random'];
const found = [];
const rxp = /\{\{([\s\S]+?)\}\}/gm;
//const rxp = /{([^}]+)}/g;
@@ -1609,7 +1625,12 @@ export function extractMessageBias(message) {
while ((curMatch = rxp.exec(message))) {
const match = curMatch[1].trim();
if (forbiddenMatches.includes(match)) {
// Ignore random pattern matches
if (/^random:.+/i.test(match)) {
continue;
}
if (forbiddenMatches.includes(match.toLowerCase())) {
continue;
}
@@ -3465,7 +3486,7 @@ function extractMessageFromData(data) {
function cleanUpMessage(getMessage, isImpersonate, displayIncompleteSentences = false) {
// Add the prompt bias before anything else
if (
power_user.user_prompt_bias &&
power_user.user_prompt_bias &&
!isImpersonate &&
power_user.user_prompt_bias.length !== 0
) {
@@ -4908,8 +4929,8 @@ function updateMessage(div) {
}
const regexResult = getRegexedString(
text,
regexPlacement,
text,
regexPlacement,
{
characterOverride: regexPlacement === regex_placement.SENDAS ? mes.name : undefined
}

View File

@@ -333,7 +333,7 @@ function switchSpoilerMode() {
}
}
function peekSpoilerMode(){
function peekSpoilerMode() {
$("#description_div").toggle();
$("#description_textarea").toggle();
$("#firstmessage_textarea").toggle();
@@ -1166,7 +1166,7 @@ function setAvgBG() {
.attr('src')
.replace(/^url\(['"]?/, '')
.replace(/['"]?\)$/, '');
const userAvatar = new Image()
userAvatar.src = $("#user_avatar_block .avatar.selected img")
.attr('src')
@@ -1185,7 +1185,7 @@ function setAvgBG() {
.replace('rgb', '')
.replace('(', '[')
.replace(')', ']'); //[50, 120, 200, 1]; // Example background color
const backgroundColorArray = JSON.parse(backgroundColorString) //[200, 200, 200, 1]
const backgroundColorArray = JSON.parse(backgroundColorString) //[200, 200, 200, 1]
console.log(backgroundColorArray)
$("#main-text-color-picker").attr('color', getReadableTextColor(backgroundColorArray));
console.log($("#main-text-color-picker").attr('color')); // Output: 'rgba(0, 47, 126, 1)'
@@ -1197,7 +1197,7 @@ function setAvgBG() {
//console.log(rgb);
$("#bot-mes-blur-tint-color-picker").attr('color', 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')');
}
userAvatar.onload = function () {
var rgb = getAverageRGB(userAvatar);
//console.log(`average color of the user avatar is:`);
@@ -1300,16 +1300,16 @@ function setAvgBG() {
//this version keeps BG and main text in same hue
/* function getReadableTextColor(rgb) {
const [r, g, b] = rgb;
// Convert RGB to HSL
const rgbToHsl = (r, g, b) => {
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const d = max - min;
const l = (max + min) / 2;
if (d === 0) return [0, 0, l];
const s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
const h = (() => {
switch (max) {
@@ -1321,16 +1321,16 @@ function setAvgBG() {
return (r - g) / d + 4;
}
})() / 6;
return [h, s, l];
};
const [h, s, l] = rgbToHsl(r / 255, g / 255, b / 255);
// Calculate appropriate text color based on background color
const targetLuminance = l > 0.5 ? 0.2 : 0.8;
const targetSaturation = s > 0.5 ? s - 0.2 : s + 0.2;
const [rNew, gNew, bNew] = hslToRgb(h, targetSaturation, targetLuminance);
// Return the text color in RGBA format
return `rgba(${rNew.toFixed(0)}, ${gNew.toFixed(0)}, ${bNew.toFixed(0)}, 1)`;
}*/
@@ -1824,7 +1824,7 @@ $(document).ready(() => {
peekSpoilerMode();
$(this).toggleClass('fa-eye fa-eye-slash');
});
$(window).on('focus', function () {
browser_has_focus = true;
});