diff --git a/public/script.js b/public/script.js index 0c04d8b73..fa9664511 100644 --- a/public/script.js +++ b/public/script.js @@ -315,6 +315,7 @@ export const event_types = { FORCE_SET_BACKGROUND: 'force_set_background', CHAT_DELETED: 'chat_deleted', GROUP_CHAT_DELETED: 'group_chat_deleted', + GENERATE_BEFORE_COMBINE_PROMPTS: 'generate_before_combine_prompts', }; export const eventSource = new EventEmitter(); @@ -3623,30 +3624,57 @@ async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, qu generatedPromptCache = cleanupPromptCache(generatedPromptCache); } - // Right now, everything is suffixed with a newline - mesSendString = finalMesSend.map((e) => `${e.extensionPrompts.join('')}${e.message}`).join(''); + // Flattens the multiple prompt objects to a string. + const combine = () => { + // Right now, everything is suffixed with a newline + mesSendString = finalMesSend.map((e) => `${e.extensionPrompts.join('')}${e.message}`).join(''); - // add chat preamble - mesSendString = addChatsPreamble(mesSendString); + // add a custom dingus (if defined) + mesSendString = addChatsSeparator(mesSendString); - // add a custom dingus (if defined) - mesSendString = addChatsSeparator(mesSendString); + // add chat preamble + mesSendString = addChatsPreamble(mesSendString); - let combinedPrompt = - beforeScenarioAnchor + - storyString + - afterScenarioAnchor + - mesExmString + - mesSendString + - generatedPromptCache; + let combinedPrompt = beforeScenarioAnchor + + storyString + + afterScenarioAnchor + + mesExmString + + mesSendString + + generatedPromptCache; - combinedPrompt = combinedPrompt.replace(/\r/gm, ''); + combinedPrompt = combinedPrompt.replace(/\r/gm, ''); - if (power_user.collapse_newlines) { - combinedPrompt = collapseNewlines(combinedPrompt); - } + if (power_user.collapse_newlines) { + combinedPrompt = collapseNewlines(combinedPrompt); + } - return combinedPrompt; + return combinedPrompt; + }; + + let data = { + api: main_api, + combinedPrompt: null, + description, + personality, + persona, + scenario, + char: name2, + user: name1, + beforeScenarioAnchor, + afterScenarioAnchor, + mesExmString, + finalMesSend, + generatedPromptCache, + main: system, + jailbreak, + naiPreamble: nai_settings.preamble, + }; + + // Before returning the combined prompt, give available context related information to all subscribers. + eventSource.emitAndWait(event_types.GENERATE_BEFORE_COMBINE_PROMPTS, data); + + // If one or multiple subscribers return a value, forfeit the responsibillity of flattening the context. + return !data.combinedPrompt ? combine() : data.combinedPrompt; } // Get the negative prompt first since it has the unmodified mesSend array