Add /messages and /setinput commands

This commit is contained in:
Cohee 2023-11-24 17:12:59 +02:00
parent c9b3ccc585
commit adb3badcc1
3 changed files with 62 additions and 17 deletions

View File

@ -2242,15 +2242,25 @@ async function processCommands(message, type, dryRun) {
return null;
}
const previousText = String($("#send_textarea").val());
const result = await executeSlashCommands(message);
$("#send_textarea").val(result.newText).trigger('input');
if (!result || typeof result !== 'object') {
return null;
}
const currentText = String($("#send_textarea").val());
if (previousText === currentText) {
$("#send_textarea").val(result.newText).trigger('input');
}
// interrupt generation if the input was nothing but a command
if (message.length > 0 && result.newText.length === 0) {
if (message.length > 0 && result?.newText.length === 0) {
return true;
}
return result.interrupt;
return result?.interrupt;
}
function sendSystemMessage(type, text, extra = {}) {
@ -2798,7 +2808,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
const interruptedByCommand = await processCommands($("#send_textarea").val(), type, dryRun);
if (interruptedByCommand) {
$("#send_textarea").val('').trigger('input');
//$("#send_textarea").val('').trigger('input');
unblockGeneration();
return;
}

View File

@ -172,12 +172,47 @@ parser.addCommand('pass', (_, arg) => arg, [], '<span class="monospace">(text)</
parser.addCommand('delay', delayCallback, ['wait', 'sleep'], '<span class="monospace">(milliseconds)</span> delays the next command in the pipe by the specified number of milliseconds.', true, true);
parser.addCommand('input', inputCallback, ['prompt'], '<span class="monospace">(prompt)</span> shows a popup with the provided prompt and passes the user input to the next command through the pipe.', true, true);
parser.addCommand('run', runCallback, ['call', 'exec'], '<span class="monospace">(QR label)</span> runs a Quick Reply with the specified name from the current preset.', true, true);
parser.addCommand('messages', getMessagesCallback, ['message'], '<span class="monospace">(names=off/on [message index or range])</span> returns the specified message or range of messages as a string.', true, true);
parser.addCommand('setinput', setInputCallback, [], '<span class="monospace">(text)</span> sets the user input to the specified text and passes it to the next command through the pipe.', true, true);
registerVariableCommands();
const NARRATOR_NAME_KEY = 'narrator_name';
const NARRATOR_NAME_DEFAULT = 'System';
export const COMMENT_NAME_DEFAULT = 'Note';
function setInputCallback(_, value) {
$('#send_textarea').val(value || '').trigger('input');
return value;
}
function getMessagesCallback(args, value) {
const includeNames = !isFalseBoolean(args?.names);
const range = stringToRange(value, 0, chat.length - 1);
if (!range) {
console.warn(`WARN: Invalid range provided for /getmessages command: ${value}`);
return '';
}
const messages = [];
for (let messageId = range.start; messageId <= range.end; messageId++) {
const message = chat[messageId];
if (!message) {
console.warn(`WARN: No message found with ID ${messageId}`);
continue;
}
if (includeNames) {
messages.push(`${message.name}: ${message.mes}`);
} else {
messages.push(message.mes);
}
}
return messages.join('\n\n');
}
async function runCallback(_, name) {
if (!name) {
toastr.warning('No name provided for /run command');
@ -199,7 +234,7 @@ async function runCallback(_, name) {
}
function abortCallback() {
$('#send_textarea').val('');
$('#send_textarea').val('').trigger('input');
throw new Error('/abort command executed');
}
@ -264,7 +299,7 @@ async function generateRawCallback(args, value) {
}
// Prevent generate recursion
$('#send_textarea').val('');
$('#send_textarea').val('').trigger('input');
const lock = isTrueBoolean(args?.lock);
if (typeof args.stop === 'string' && args.stop.length) {
@ -301,7 +336,7 @@ async function generateCallback(args, value) {
}
// Prevent generate recursion
$('#send_textarea').val('');
$('#send_textarea').val('').trigger('input');
const lock = isTrueBoolean(args?.lock);
try {
@ -415,7 +450,7 @@ async function deleteSwipeCallback(_, arg) {
async function askCharacter(_, text) {
// Prevent generate recursion
$('#send_textarea').val('');
$('#send_textarea').val('').trigger('input');
// Not supported in group chats
// TODO: Maybe support group chats?
@ -704,7 +739,7 @@ async function triggerGroupMessageCallback(_, arg) {
}
// Prevent generate recursion
$('#send_textarea').val('');
$('#send_textarea').val('').trigger('input');
const chid = findGroupMemberId(arg);
@ -804,12 +839,12 @@ function openChat(id) {
function continueChatCallback() {
// Prevent infinite recursion
$('#send_textarea').val('');
$('#send_textarea').val('').trigger('input');
$('#option_continue').trigger('click', { fromSlashCommand: true });
}
export async function generateSystemMessage(_, prompt) {
$('#send_textarea').val('');
$('#send_textarea').val('').trigger('input');
if (!prompt) {
console.warn('WARN: No prompt provided for /sysgen command');

View File

@ -72,39 +72,39 @@ function addGlobalVariable(name, value) {
export function replaceVariableMacros(str) {
// Replace {{getvar::name}} with the value of the variable name
str = str.replace(/{{getvar::([^}]+)}}/gi, (_, name) => {
name = name.toLowerCase().trim();
name = name.trim();
return getLocalVariable(name);
});
// Replace {{setvar::name::value}} with empty string and set the variable name to value
str = str.replace(/{{setvar::([^:]+)::([^}]+)}}/gi, (_, name, value) => {
name = name.toLowerCase().trim();
name = name.trim();
setLocalVariable(name, value);
return '';
});
// Replace {{addvar::name::value}} with empty string and add value to the variable value
str = str.replace(/{{addvar::([^:]+)::([^}]+)}}/gi, (_, name, value) => {
name = name.toLowerCase().trim();
name = name.trim();
return addLocalVariable(name, value);;
});
// Replace {{getglobalvar::name}} with the value of the global variable name
str = str.replace(/{{getglobalvar::([^}]+)}}/gi, (_, name) => {
name = name.toLowerCase().trim();
name = name.trim();
return getGlobalVariable(name);
});
// Replace {{setglobalvar::name::value}} with empty string and set the global variable name to value
str = str.replace(/{{setglobalvar::([^:]+)::([^}]+)}}/gi, (_, name, value) => {
name = name.toLowerCase().trim();
name = name.trim();
setGlobalVariable(name, value);
return '';
});
// Replace {{addglobalvar::name::value}} with empty string and add value to the global variable value
str = str.replace(/{{addglobalvar::([^:]+)::([^}]+)}}/gi, (_, name, value) => {
name = name.toLowerCase().trim();
name = name.trim();
return addGlobalVariable(name, value);
});