Add /sendas command

This commit is contained in:
SillyLossy
2023-05-18 18:49:49 +03:00
parent 1f07722025
commit 0d1f291003
4 changed files with 56 additions and 9 deletions

View File

@ -1990,7 +1990,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
// Add quiet generation prompt at depth 0
if (quiet_prompt && quiet_prompt.length) {
const name = is_pygmalion ? 'You' : name1;
const quietAppend = isInstruct ? formatInstructModeChat(name, quiet_prompt, false, true) : `\n${name}: ${quiet_prompt}`;
const quietAppend = isInstruct ? formatInstructModeChat(name, quiet_prompt, false, true, false) : `\n${name}: ${quiet_prompt}`;
mesSendString += quietAppend;
}
@ -2362,14 +2362,14 @@ function getBiasStrings(textareaText) {
function formatMessageHistoryItem(chatItem, isInstruct) {
const isNarratorType = chatItem?.extra?.type === system_message_types.NARRATOR;
const characterName = selected_group ? chatItem.name : name2;
const characterName = (selected_group || chatItem.force_avatar) ? chatItem.name : name2;
const itemName = chatItem.is_user ? chatItem['name'] : characterName;
const shouldPrependName = (chatItem.is_name || selected_group) && !isNarratorType;
const shouldPrependName = (chatItem.is_name || chatItem.force_avatar || selected_group) && !isNarratorType;
let textResult = shouldPrependName ? `${itemName}: ${chatItem.mes}\n` : `${chatItem.mes}\n`;
if (isInstruct) {
textResult = formatInstructModeChat(itemName, chatItem.mes, chatItem.is_user, isNarratorType);
textResult = formatInstructModeChat(itemName, chatItem.mes, chatItem.is_user, isNarratorType, chatItem.force_avatar);
}
// replace bias markup

View File

@ -171,8 +171,8 @@ function setOpenAIMessages(chat) {
role = 'system';
}
// for groups - prepend a character's name
if (selected_group) {
// for groups or sendas command - prepend a character's name
if (selected_group || chat[j].force_avatar) {
content = `${chat[j].name}: ${content}`;
}

View File

@ -640,8 +640,8 @@ function loadInstructMode() {
});
}
export function formatInstructModeChat(name, mes, isUser, isNarrator) {
const includeNames = isNarrator ? false : power_user.instruct.names || !!selected_group;
export function formatInstructModeChat(name, mes, isUser, isNarrator, forceAvatar) {
const includeNames = isNarrator ? false : (power_user.instruct.names || !!selected_group || !!forceAvatar);
const sequence = (isUser || isNarrator) ? power_user.instruct.input_sequence : power_user.instruct.output_sequence;
const separator = power_user.instruct.wrap ? '\n' : '';
const separatorSequence = power_user.instruct.separator_sequence && !isUser

View File

@ -1,7 +1,10 @@
import {
addOneMessage,
characters,
chat,
chat_metadata,
default_avatar,
getThumbnailUrl,
saveChatConditional,
sendSystemMessage,
system_avatar,
@ -85,7 +88,8 @@ const registerSlashCommand = parser.addCommand.bind(parser);
const getSlashCommandsHelp = parser.getHelpString.bind(parser);
parser.addCommand('help', helpCommandCallback, ['?'], ' displays this help message', true, true);
parser.addCommand('bg', setBackgroundCallback, ['background'], '<span class="monospace">(filename)</span> sets a background according to filename, partial names allowed, will set the first one alphebetically if multiple files begin with the provided argument string', false, true);
parser.addCommand('bg', setBackgroundCallback, ['background'], '<span class="monospace">(filename)</span> sets a background according to filename, partial names allowed, will set the first one alphabetically if multiple files begin with the provided argument string', false, true);
parser.addCommand('sendas', sendMessageAs, [], ` sends message as a specific character.<br>Example:<br><pre><code>/sendas Chloe\nHello, guys!</code></pre>will send "Hello, guys!" from "Chloe".<br>Uses character avatar if it exists in the characters list.`, true, true);
parser.addCommand('sys', sendNarratorMessage, [], '<span class="monospace">(text)</span> sends message as a system narrator', false, true);
parser.addCommand('sysname', setNarratorName, [], '<span class="monospace">(name)</span> sets a name for future system narrator messages in this chat (display only). Default: System. Leave empty to reset.', true, true);
@ -99,6 +103,49 @@ function setNarratorName(_, text) {
saveChatConditional();
}
function sendMessageAs(_, text) {
if (!text) {
return;
}
const parts = text.split('\n');
if (parts.length <= 1) {
toastr.warning('Both character name and message are required. Separate them with a new line.');
return;
}
const name = parts.shift().trim();
const mesText = parts.join('\n').trim();
const character = characters.find(x => x.name === name);
let force_avatar, original_avatar;
if (character && character.avatar !== 'none') {
force_avatar = getThumbnailUrl('avatar', character.avatar);
original_avatar = character.avatar;
}
else {
force_avatar = default_avatar;
original_avatar = default_avatar;
}
const message = {
name: name,
is_user: false,
is_name: true,
is_system: false,
send_date: humanizedDateTime(),
mes: mesText,
force_avatar: force_avatar,
original_avatar: original_avatar,
};
chat.push(message);
addOneMessage(message);
saveChatConditional();
}
function sendNarratorMessage(_, text) {
if (!text) {
return;