From 2ce58bb0a6f1b7504db5bcce113071248496eb69 Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Fri, 9 Aug 2024 00:37:26 +0200 Subject: [PATCH] Allow switching on /addswipe - Add optional "switch" arg to /addswipe - Make /addswipe return the new swipe id --- public/scripts/slash-commands.js | 39 ++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 33ff9dccf..a1446c9c0 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -912,13 +912,28 @@ export function initDefaultSlashCommands() { SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'addswipe', callback: addSwipeCallback, + returns: 'the new swipe id', aliases: ['swipeadd'], + namedArgumentList: [ + SlashCommandNamedArgument.fromProps({ + name: 'switch', + description: 'switch to the new swipe', + typeList: [ARGUMENT_TYPE.BOOLEAN], + enumList: commonEnumProviders.boolean()(), + }), + ], unnamedArgumentList: [ new SlashCommandArgument( 'text', [ARGUMENT_TYPE.STRING], true, ), ], - helpString: 'Adds a swipe to the last chat message.', + helpString: ` +
+ Adds a swipe to the last chat message. +
+
+ Use switch=true to switch to directly switch to the new swipe. +
`, })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'stop', @@ -2154,8 +2169,11 @@ async function echoCallback(args, value) { } } - -async function addSwipeCallback(_, arg) { +/** + * @param {{switch?: string}} args - named arguments + * @param {string} value - The swipe text to add (unnamed argument) + */ +async function addSwipeCallback(args, value) { const lastMessage = chat[chat.length - 1]; if (!lastMessage) { @@ -2163,7 +2181,7 @@ async function addSwipeCallback(_, arg) { return ''; } - if (!arg) { + if (!value) { console.warn('WARN: No argument provided for /addswipe command'); return ''; } @@ -2192,23 +2210,30 @@ async function addSwipeCallback(_, arg) { lastMessage.swipe_info = lastMessage.swipes.map(() => ({})); } - lastMessage.swipes.push(arg); + lastMessage.swipes.push(value); lastMessage.swipe_info.push({ send_date: getMessageTimeStamp(), gen_started: null, gen_finished: null, extra: { - bias: extractMessageBias(arg), + bias: extractMessageBias(value), gen_id: Date.now(), api: 'manual', model: 'slash command', }, }); + const newSwipeId = lastMessage.swipes.length - 1; + + if (isTrueBoolean(args.switch)) { + lastMessage.swipe_id = newSwipeId; + lastMessage.mes = lastMessage.swipes[newSwipeId]; + } + await saveChatConditional(); await reloadCurrentChat(); - return ''; + return String(newSwipeId); } async function deleteSwipeCallback(_, arg) {