Reverting cleanUpMessage() functionality changes.

841f8141370f474afaa0452fd5b31b408e6fd2af

qvink <qvink@users.noreply.github.com>

1 files changed, +35 -22Ignore whitespace
public/script.js+35 -22
@@ -3563,9 +3563,10 @@ class StreamingProcessor {
35633563 * @param {boolean} quietToLoud true to generate a message in system mode, false to generate a message in character mode
35643564 * @param {string} [systemPrompt] System prompt to use. Only Instruct mode or OpenAI.
35653565 * @param {number} [responseLength] Maximum response length. If unset, the global default value is used.
3566+ * @param {boolean} [trimNames] Whether to allow trimming "{{user}}:" and "{{char}}:" from the response.
35663567 * @returns {Promise<string>} Generated message
35673568 */
35683569export async function generateRaw(prompt, api, instructOverride, quietToLoud, systemPrompt, responseLength, trimNames = true) {
35693570 if (!api) {
35703571 api = main_api;
35713572 }
@@ -3661,6 +3662,8 @@ export async function generateRaw(prompt, api, instructOverride, quietToLoud, sy
36613662 isContinue: false,
36623663 displayIncompleteSentences: true,
36633664 includeUserPromptBias: false,
3665+ trimNames: trimNames,
3666+ trimWrongNames: trimNames,
36643667 });
36653668
36663669 if (!message) {
@@ -5970,10 +5973,10 @@ function extractMultiSwipes(data, type) {
59705973 *
59715974 * @returns {string} The formatted message
59725975 */
59735976export function cleanUpMessage({ getMessage, isImpersonate, isContinue, displayIncompleteSentences = false, stoppingStrings = null, includeUserPromptBias = true, trimNames = true, trimWrongNames = true } = {}) {
59745977 if (arguments.length > 0 && typeof arguments[0] !== 'object') {
59755978 console.trace('cleanUpMessage called with positional arguments. Please use an object instead.');
59765979 [getMessage, isImpersonate, isContinue, displayIncompleteSentences, stoppingStrings, includeUserPromptBias, trimNames, trimWrongNames] = arguments;
59775980 }
59785981
59795982 if (!getMessage) {
@@ -6020,26 +6023,33 @@ export function cleanUpMessage({ getMessage, isImpersonate, isContinue, displayI
60206023 // "trailing whitespace on newlines\nevery line of the string\nsample text"
60216024 getMessage = getMessage.replace(/[^\S\r\n]+$/gm, '');
60226025
6023- // Trim instances of "{{name}}:" from the start of the message+
6026+ if (trimWrongNames) {
6024- if (trimNames) {
6027+ // If this is an impersonation, delete the entire response if it starts with "{{char}}:"
6025- let nameToTrim = isImpersonate ? name2 : name1;
6028+ // If this isn't an impersonation, delete the entire response if it starts with "{{user}}:"
6026- if (isImpersonate) {
6029+ // Also delete any trailing text that starts with the wrong name.
6027- nameToTrim = power_user.allow_name2_display ? '' : name2;
6030+ // This only occurs if the corresponding "power_user.allow_nameX_display" is false.
6028- } else {
6029- nameToTrim = power_user.allow_name1_display ? '' : name1;
6030- }
60316031
6032- // get text from after the name (and colon) to the end of the string
6032+ let wrongName = isImpersonate
6033- if (nameToTrim && getMessage.indexOf(`${nameToTrim}:`) === 0) {
6033+ ? (power_user.allow_name2_display ? '' : name2) // char
6034- getMessage = getMessage.substring(nameToTrim.length + 1);
6034+ : (power_user.allow_name1_display ? '' : name1); // user
6035- }
60366035
6037- // account for case where the name is after a newline
6036+ if (wrongName) {
6038- let startIndex = getMessage.indexOf(`\n${nameToTrim}:`);
6037+ // If the message starts with the wrong name, delete the entire response
6039- if (nameToTrim && startIndex >= 0) {
6038+ let startIndex = getMessage.indexOf(`${wrongName}`);
6040- getMessage = getMessage.substring(startIndex + nameToTrim.length + 2);
6039+ if (startIndex === 0) {
6040+ getMessage = '';
6041+ console.debug(`Message started with the wrong name: "${wrongName}" - response was deleted.`);
6042+ }
6043+
6044+ // If there is trailing text starting with the wrong name, trim it off.
6045+ startIndex = getMessage.indexOf(`\n${wrongName}:`);
6046+ if (startIndex >= 0) {
6047+ getMessage = getMessage.substring(0, startIndex);
6048+ }
6049+
6050+ // remove any whitespace at the beginning
6051+ getMessage = getMessage.trimStart();
60416052 }
6042- getMessage.trimStart();
60436053 }
60446054
60456055 if (getMessage.indexOf('<|endoftext|>') != -1) {
@@ -6102,9 +6112,12 @@ export function cleanUpMessage({ getMessage, isImpersonate, isContinue, displayI
61026112 }
61036113
61046114 if (trimNames) {
6115+ // If this is an impersonation, trim "{{user}}:" from the beginning
6116+ // If this isn't an impersonation, trim "{{char}}:" from the beginning.
6117+ // Only applied when the corresponding "power_user.allow_nameX_display" is false.
61056118 const nameToTrim2 = isImpersonate
61066119 ? (!power_user.allow_name1_display ? name1 : '') // user
61076120 : (!power_user.allow_name2_display ? name2 : ''); // char
61086121
61096122 if (nameToTrim2 && getMessage.startsWith(nameToTrim2 + ':')) {
61106123 getMessage = getMessage.replace(nameToTrim2 + ':', '');