From 9d713825c24c4f1e6d9f92375dad7319891fe50d Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:23:01 +0200 Subject: [PATCH] #1827 Consolidate {{group}} macro behavior --- public/script.js | 19 +++++++++++++++++-- public/scripts/openai.js | 3 +-- public/scripts/templates/macros.html | 2 ++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/public/script.js b/public/script.js index 024dfbecd..066a02f99 100644 --- a/public/script.js +++ b/public/script.js @@ -2169,6 +2169,22 @@ function substituteParams(content, _name1, _name2, _original, _group, _replaceCh }; } + const getGroupValue = () => { + if (typeof _group === 'string') { + return _group; + } + + if (selected_group) { + const members = groups.find(x => x.id === selected_group)?.members; + const names = Array.isArray(members) + ? members.map(m => characters.find(c => c.avatar === m)?.name).filter(Boolean).join(', ') + : ''; + return names; + } else { + return _name2 ?? name2; + } + }; + if (_replaceCharacterCard) { const fields = getCharacterCardFields(); environment.charPrompt = fields.system || ''; @@ -2181,10 +2197,9 @@ function substituteParams(content, _name1, _name2, _original, _group, _replaceCh } // Must be substituted last so that they're replaced inside {{description}} - // TODO: evaluate macros recursively so we don't need to rely on substitution order environment.user = _name1 ?? name1; environment.char = _name2 ?? name2; - environment.group = environment.charIfNotGroup = _group ?? name2; + environment.group = environment.charIfNotGroup = getGroupValue(); environment.model = getGeneratingModel(); return evaluateMacros(content, environment); diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 02ba1a6fe..e1552090f 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -670,10 +670,9 @@ export function isOpenRouterWithInstruct() { async function populateChatHistory(messages, prompts, chatCompletion, type = null, cyclePrompt = null) { chatCompletion.add(new MessageCollection('chatHistory'), prompts.index('chatHistory')); - let names = (selected_group && groups.find(x => x.id === selected_group)?.members.map(member => characters.find(c => c.avatar === member)?.name).filter(Boolean).join(', ')) || ''; // Reserve budget for new chat message const newChat = selected_group ? oai_settings.new_group_chat_prompt : oai_settings.new_chat_prompt; - const newChatMessage = new Message('system', substituteParams(newChat, null, null, null, names), 'newMainChat'); + const newChatMessage = new Message('system', substituteParams(newChat), 'newMainChat'); chatCompletion.reserveBudget(newChatMessage); // Reserve budget for group nudge diff --git a/public/scripts/templates/macros.html b/public/scripts/templates/macros.html index 9bf203dff..34b768ac1 100644 --- a/public/scripts/templates/macros.html +++ b/public/scripts/templates/macros.html @@ -16,6 +16,8 @@
  • {{mesExamplesRaw}} – unformatted Dialogue Examples (only for Story String)
  • {{user}} – your current Persona username
  • {{char}} – the Character's name
  • +
  • {{group}} – a comma-separated list of group member names or the character name in solo chats. Alias: {{charIfNotGroup}}
  • +
  • {{model}} – a text generation model name for the currently selected API. Can be inaccurate!
  • {{lastMessage}} - the text of the latest chat message.
  • {{lastMessageId}} – index # of the latest chat message. Useful for slash command batching.
  • {{firstIncludedMessageId}} - the ID of the first message included in the context. Requires generation to be ran at least once in the current session.