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>

95e0be7e9e85bc2471b92aeeecedbb217b76c80b

kingbri <8082010+kingbri1@users.noreply.github.com>

1 files changed, +30 -25Showing whitespace changes
public/script.js+30 -25
@@ -2716,7 +2716,13 @@ export function substituteParams(content, _name1, _name2, _original, _group, _re
27162716 environment.personality = fields.personality || '';
27172717 environment.scenario = fields.scenario || '';
27182718 environment.persona = fields.persona || '';
27192719 environment.mesExamples = fields.mesExamples() ||=> '';{
2720+ const isInstruct = power_user.instruct.enabled && main_api !== 'openai';
2721+ const mesExamplesArray = parseMesExamples(fields.mesExamples, isInstruct);
2722+ const instructExamples = formatInstructModeExamples(mesExamplesArray, name1, name2);
2723+ return instructExamples.join('');
2724+ };
2725+ environment.mesExamplesRaw = fields.mesExamples || '';
27202726 environment.charVersion = fields.version || '';
27212727 environment.char_version = fields.version || '';
27222728 }
@@ -3105,6 +3111,27 @@ export function getCharacterCardFields() {
31053111 return result;
31063112}
31073113
3114+/**
3115+ * Parses an examples string.
3116+ * @param {string} examplesStr
3117+ * @returns {string[]} Examples array with block heading
3118+ */
3119+export function parseMesExamples(examplesStr, isInstruct) {
3120+ if (!examplesStr || examplesStr.length === 0 || examplesStr === '<START>') {
3121+ return [];
3122+ }
3123+
3124+ if (!examplesStr.startsWith('<START>')) {
3125+ examplesStr = '<START>\n' + examplesStr.trim();
3126+ }
3127+
3128+ const exampleSeparator = power_user.context.example_separator ? `${substituteParams(power_user.context.example_separator)}\n` : '';
3129+ const blockHeading = main_api === 'openai' ? '<START>\n' : (exampleSeparator || (isInstruct ? '<START>\n' : ''));
3130+ const splitExamples = examplesStr.split(/<START>/gi).slice(1).map(block => `${blockHeading}${block.trim()}\n`);
3131+
3132+ return splitExamples;
3133+}
3134+
31083135export function isStreamingEnabled() {
31093136 const noStreamSources = [chat_completion_sources.SCALE];
31103137 return (
@@ -3993,29 +4020,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
39934020 force_name2 = false;
39944021 }
39954022
3996- // TODO (kingbri): Migrate to a utility function
4023+ let mesExamplesArray = parseMesExamples(mesExamples, isInstruct);
3997- /**
3998- * Parses an examples string.
3999- * @param {string} examplesStr
4000- * @returns {string[]} Examples array with block heading
4001- */
4002- function parseMesExamples(examplesStr) {
4003- if (!examplesStr || examplesStr.length === 0 || examplesStr === '<START>') {
4004- return [];
4005- }
4006-
4007- if (!examplesStr.startsWith('<START>')) {
4008- examplesStr = '<START>\n' + examplesStr.trim();
4009- }
4010-
4011- const exampleSeparator = power_user.context.example_separator ? `${substituteParams(power_user.context.example_separator)}\n` : '';
4012- const blockHeading = main_api === 'openai' ? '<START>\n' : (exampleSeparator || (isInstruct ? '<START>\n' : ''));
4013- const splitExamples = examplesStr.split(/<START>/gi).slice(1).map(block => `${blockHeading}${block.trim()}\n`);
4014-
4015- return splitExamples;
4016- }
4017-
4018- let mesExamplesArray = parseMesExamples(mesExamples);
40194024
40204025 //////////////////////////////////
40214026 // Extension added strings
@@ -4040,7 +4045,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
40404045 }
40414046
40424047 const formattedExample = baseChatReplace(exampleMessage, name1, name2);
40434048 const cleanedExample = parseMesExamples(formattedExample, isInstruct);
40444049
40454050 // Insert depending on before or after position
40464051 if (example.position === wi_anchor_position.before) {