mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Fix character card parsing
This commit is contained in:
174
public/script.js
174
public/script.js
@@ -323,48 +323,6 @@ $.get("/csrf-token").then((data) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
///////////// UNUSED FUNCTIONS MOVED TO TOP ///////////////
|
///////////// UNUSED FUNCTIONS MOVED TO TOP ///////////////
|
||||||
function cleanGroupMessage(getMessage) {
|
|
||||||
const group = groups.find((x) => x.id == selected_group);
|
|
||||||
|
|
||||||
if (group && Array.isArray(group.members) && group.members) {
|
|
||||||
for (let member of group.members) {
|
|
||||||
// Skip current speaker.
|
|
||||||
if (member === name2) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const indexOfMember = getMessage.indexOf(member + ":");
|
|
||||||
if (indexOfMember != -1) {
|
|
||||||
getMessage = getMessage.substr(0, indexOfMember);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return getMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getExtensionPrompt() {
|
|
||||||
let extension_prompt = Object.keys(extension_prompts)
|
|
||||||
.sort()
|
|
||||||
.map((x) => extension_prompts[x])
|
|
||||||
.filter(x => x)
|
|
||||||
.join("\n");
|
|
||||||
if (extension_prompt.length && !extension_prompt.endsWith("\n")) {
|
|
||||||
extension_prompt += "\n";
|
|
||||||
}
|
|
||||||
return extension_prompt;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getWorldInfoPrompt(chat2) {
|
|
||||||
let worldInfoString = "", worldInfoBefore = "", worldInfoAfter = "";
|
|
||||||
|
|
||||||
if (world_info && world_info_data) {
|
|
||||||
const activatedWorldInfo = checkWorldInfo(chat2);
|
|
||||||
worldInfoBefore = activatedWorldInfo.worldInfoBefore;
|
|
||||||
worldInfoAfter = activatedWorldInfo.worldInfoAfter;
|
|
||||||
worldInfoString = worldInfoBefore + worldInfoAfter;
|
|
||||||
}
|
|
||||||
return { worldInfoString, worldInfoBefore, worldInfoAfter };
|
|
||||||
}
|
|
||||||
|
|
||||||
function newMesPattern(name) {
|
function newMesPattern(name) {
|
||||||
//Patern which denotes a new message
|
//Patern which denotes a new message
|
||||||
@@ -944,7 +902,69 @@ function extractMessageBias(message) {
|
|||||||
return ` ${found.join(" ")} `;
|
return ` ${found.join(" ")} `;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cleanGroupMessage(getMessage) {
|
||||||
|
const group = groups.find((x) => x.id == selected_group);
|
||||||
|
|
||||||
|
if (group && Array.isArray(group.members) && group.members) {
|
||||||
|
for (let member of group.members) {
|
||||||
|
// Skip current speaker.
|
||||||
|
if (member === name2) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const indexOfMember = getMessage.indexOf(member + ":");
|
||||||
|
if (indexOfMember != -1) {
|
||||||
|
getMessage = getMessage.substr(0, indexOfMember);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return getMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getExtensionPrompt() {
|
||||||
|
let extension_prompt = Object.keys(extension_prompts)
|
||||||
|
.sort()
|
||||||
|
.map((x) => extension_prompts[x])
|
||||||
|
.filter(x => x)
|
||||||
|
.join("\n");
|
||||||
|
if (extension_prompt.length && !extension_prompt.endsWith("\n")) {
|
||||||
|
extension_prompt += "\n";
|
||||||
|
}
|
||||||
|
return extension_prompt;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getWorldInfoPrompt(chat2) {
|
||||||
|
let worldInfoString = "", worldInfoBefore = "", worldInfoAfter = "";
|
||||||
|
|
||||||
|
if (world_info && world_info_data) {
|
||||||
|
const activatedWorldInfo = checkWorldInfo(chat2);
|
||||||
|
worldInfoBefore = activatedWorldInfo.worldInfoBefore;
|
||||||
|
worldInfoAfter = activatedWorldInfo.worldInfoAfter;
|
||||||
|
worldInfoString = worldInfoBefore + worldInfoAfter;
|
||||||
|
}
|
||||||
|
return { worldInfoString, worldInfoBefore, worldInfoAfter };
|
||||||
|
}
|
||||||
|
|
||||||
|
function baseChatReplaceAndSplit(value, name1, name2) {
|
||||||
|
if (value !== undefined && value.length > 0) {
|
||||||
|
if (is_pygmalion) {
|
||||||
|
value = value.replace(/{{user}}:/gi, "You:");
|
||||||
|
value = value.replace(/<USER>:/gi, "You:");
|
||||||
|
}
|
||||||
|
value = value.replace(/{{user}}/gi, name1);
|
||||||
|
value = value.replace(/{{char}}/gi, name2);
|
||||||
|
value = value.replace(/<USER>/gi, name1);
|
||||||
|
value = value.replace(/<BOT>/gi, name2);;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
function appendToStoryString(value, prefix) {
|
||||||
|
if (value !== undefined && value.length > 0) {
|
||||||
|
return prefix + value + '\n';
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
async function Generate(type, automatic_trigger) {//encode("dsfs").length
|
async function Generate(type, automatic_trigger) {//encode("dsfs").length
|
||||||
console.log('Generate entered');
|
console.log('Generate entered');
|
||||||
@@ -1047,38 +1067,24 @@ async function Generate(type, automatic_trigger) {//encode("dsfs").length
|
|||||||
addOneMessage(chat[chat.length - 1]);
|
addOneMessage(chat[chat.length - 1]);
|
||||||
}
|
}
|
||||||
////////////////////////////////////
|
////////////////////////////////////
|
||||||
var chatString = '';
|
let chatString = '';
|
||||||
var arrMes = [];
|
let arrMes = [];
|
||||||
var mesSend = [];
|
let mesSend = [];
|
||||||
var mesExamplesArray = [];
|
let charDescription = baseChatReplaceAndSplit($.trim(characters[this_chid].description), name1, name2);
|
||||||
var charDescription = baseChatReplaceAndSplit($.trim(characters[this_chid].description), name1, name2);
|
let charPersonality = baseChatReplaceAndSplit($.trim(characters[this_chid].personality), name1, name2);
|
||||||
var charPersonality = baseChatReplaceAndSplit($.trim(characters[this_chid].personality), name1, name2);
|
let Scenario = baseChatReplaceAndSplit($.trim(characters[this_chid].scenario), name1, name2);
|
||||||
var Scenario = baseChatReplaceAndSplit($.trim(characters[this_chid].scenario), name1, name2);
|
let mesExamples = baseChatReplaceAndSplit($.trim(characters[this_chid].mes_example), name1, name2);
|
||||||
var mesExamples = baseChatReplaceAndSplit($.trim(characters[this_chid].mes_example.replace(/<START>/gi, '')), name1, name2);
|
|
||||||
|
|
||||||
function baseChatReplaceAndSplit(value, name1, name2) {
|
if (!mesExamples.startsWith('<START>')) {
|
||||||
if (value !== undefined && value.length > 0) {
|
mesExamples = '<START>\n' + mesExamples.trim();
|
||||||
if (is_pygmalion) {
|
|
||||||
value = value.replace(/{{user}}:/gi, "You:");
|
|
||||||
value = value.replace(/<USER>:/gi, "You:");
|
|
||||||
}
|
|
||||||
value = value.replace(/{{user}}/gi, name1);
|
|
||||||
value = value.replace(/{{char}}/gi, name2);
|
|
||||||
value = value.replace(/<USER>/gi, name1);
|
|
||||||
value = value.replace(/<BOT>/gi, name2);
|
|
||||||
let blocks = value.split(/<START>/gi);
|
|
||||||
return blocks.slice(1).map(block => `<START>\n${block.trim()}\n`).join('');
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function appendToStoryString(value, prefix) {
|
if (mesExamples.replace(/<START>/gi, '').trim().length === 0) {
|
||||||
if (value !== undefined && value.length > 0) {
|
mesExamples = '';
|
||||||
return prefix + value + '\n';
|
|
||||||
}
|
|
||||||
return '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mesExamplesArray = mesExamples.split(/<START>/gi).slice(1).map(block => `<START>\n${block.trim()}\n`);
|
||||||
|
|
||||||
if (is_pygmalion) {
|
if (is_pygmalion) {
|
||||||
storyString += appendToStoryString(charDescription, name2 + "'s Persona: ");
|
storyString += appendToStoryString(charDescription, name2 + "'s Persona: ");
|
||||||
storyString += appendToStoryString(charPersonality, 'Personality: ');
|
storyString += appendToStoryString(charPersonality, 'Personality: ');
|
||||||
@@ -1151,19 +1157,9 @@ async function Generate(type, automatic_trigger) {//encode("dsfs").length
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let worldInfoString = '', worldInfoBefore = '', worldInfoAfter = '';
|
let { worldInfoString, worldInfoBefore, worldInfoAfter } = getWorldInfoPrompt(chat2);
|
||||||
|
let extension_prompt = getExtensionPrompt();
|
||||||
|
|
||||||
if (world_info && world_info_data) {
|
|
||||||
const activatedWorldInfo = checkWorldInfo(chat2);
|
|
||||||
worldInfoBefore = activatedWorldInfo.worldInfoBefore;
|
|
||||||
worldInfoAfter = activatedWorldInfo.worldInfoAfter;
|
|
||||||
worldInfoString = worldInfoBefore + worldInfoAfter;
|
|
||||||
}
|
|
||||||
|
|
||||||
let extension_prompt = Object.keys(extension_prompts).sort().map(x => extension_prompts[x]).filter(x => x).join('\n');
|
|
||||||
if (extension_prompt.length && !extension_prompt.endsWith('\n')) {
|
|
||||||
extension_prompt += '\n';
|
|
||||||
}
|
|
||||||
/////////////////////// swipecode
|
/////////////////////// swipecode
|
||||||
if (type == 'swipe') {
|
if (type == 'swipe') {
|
||||||
|
|
||||||
@@ -1529,21 +1525,7 @@ async function Generate(type, automatic_trigger) {//encode("dsfs").length
|
|||||||
}
|
}
|
||||||
// clean-up group message from excessive generations
|
// clean-up group message from excessive generations
|
||||||
if (type == 'group_chat' && selected_group) {
|
if (type == 'group_chat' && selected_group) {
|
||||||
const group = groups.find(x => x.id == selected_group);
|
getMessage = cleanGroupMessage(getMessage);
|
||||||
|
|
||||||
if (group && Array.isArray(group.members) && group.members) {
|
|
||||||
for (let member of group.members) {
|
|
||||||
// Skip current speaker.
|
|
||||||
if (member === name2) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const indexOfMember = getMessage.indexOf(member + ":");
|
|
||||||
if (indexOfMember != -1) {
|
|
||||||
getMessage = getMessage.substr(0, indexOfMember);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
let this_mes_is_name = true;
|
let this_mes_is_name = true;
|
||||||
if (getMessage.indexOf(name2 + ":") === 0) {
|
if (getMessage.indexOf(name2 + ":") === 0) {
|
||||||
|
Reference in New Issue
Block a user