MesExamples: Make macros consistent with story string

mesExamples in the story string is formatted while mesExamplesRaw
is unformatted. However mesExamples when used as a normal macro
is unformatted. This causes an inconsistency in usage which is now
corrected.

Signed-off-by: kingbri <8082010+kingbri1@users.noreply.github.com>
This commit is contained in:
kingbri
2025-03-07 13:02:20 -05:00
parent a9c2af19e3
commit 95e0be7e9e

View File

@@ -2716,7 +2716,13 @@ export function substituteParams(content, _name1, _name2, _original, _group, _re
environment.personality = fields.personality || '';
environment.scenario = fields.scenario || '';
environment.persona = fields.persona || '';
environment.mesExamples = fields.mesExamples || '';
environment.mesExamples = () => {
const isInstruct = power_user.instruct.enabled && main_api !== 'openai';
const mesExamplesArray = parseMesExamples(fields.mesExamples, isInstruct);
const instructExamples = formatInstructModeExamples(mesExamplesArray, name1, name2);
return instructExamples.join('');
};
environment.mesExamplesRaw = fields.mesExamples || '';
environment.charVersion = fields.version || '';
environment.char_version = fields.version || '';
}
@@ -3105,6 +3111,27 @@ export function getCharacterCardFields() {
return result;
}
/**
* Parses an examples string.
* @param {string} examplesStr
* @returns {string[]} Examples array with block heading
*/
export function parseMesExamples(examplesStr, isInstruct) {
if (!examplesStr || examplesStr.length === 0 || examplesStr === '<START>') {
return [];
}
if (!examplesStr.startsWith('<START>')) {
examplesStr = '<START>\n' + examplesStr.trim();
}
const exampleSeparator = power_user.context.example_separator ? `${substituteParams(power_user.context.example_separator)}\n` : '';
const blockHeading = main_api === 'openai' ? '<START>\n' : (exampleSeparator || (isInstruct ? '<START>\n' : ''));
const splitExamples = examplesStr.split(/<START>/gi).slice(1).map(block => `${blockHeading}${block.trim()}\n`);
return splitExamples;
}
export function isStreamingEnabled() {
const noStreamSources = [chat_completion_sources.SCALE];
return (
@@ -3993,29 +4020,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
force_name2 = false;
}
// TODO (kingbri): Migrate to a utility function
/**
* Parses an examples string.
* @param {string} examplesStr
* @returns {string[]} Examples array with block heading
*/
function parseMesExamples(examplesStr) {
if (!examplesStr || examplesStr.length === 0 || examplesStr === '<START>') {
return [];
}
if (!examplesStr.startsWith('<START>')) {
examplesStr = '<START>\n' + examplesStr.trim();
}
const exampleSeparator = power_user.context.example_separator ? `${substituteParams(power_user.context.example_separator)}\n` : '';
const blockHeading = main_api === 'openai' ? '<START>\n' : (exampleSeparator || (isInstruct ? '<START>\n' : ''));
const splitExamples = examplesStr.split(/<START>/gi).slice(1).map(block => `${blockHeading}${block.trim()}\n`);
return splitExamples;
}
let mesExamplesArray = parseMesExamples(mesExamples);
let mesExamplesArray = parseMesExamples(mesExamples, isInstruct);
//////////////////////////////////
// Extension added strings
@@ -4040,7 +4045,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
}
const formattedExample = baseChatReplace(exampleMessage, name1, name2);
const cleanedExample = parseMesExamples(formattedExample);
const cleanedExample = parseMesExamples(formattedExample, isInstruct);
// Insert depending on before or after position
if (example.position === wi_anchor_position.before) {