Merge pull request #2629 from SillyTavern/improve-swipe-commands Improve `/addswipe` and `/delswipe` slightly

478e1a6bb5a57e46cc8f63433742bcdd4b0bde53

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +34 -8Showing whitespace changes
public/scripts/slash-commands.js+34 -8
@@ -717,6 +717,7 @@ export function initDefaultSlashCommands() {
717717 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
718718 name: 'delswipe',
719719 callback: deleteSwipeCallback,
720+ returns: 'the new, currently selected swipe id',
720721 aliases: ['swipedel'],
721722 unnamedArgumentList: [
722723 SlashCommandArgument.fromProps({
@@ -912,13 +913,28 @@ export function initDefaultSlashCommands() {
912913 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
913914 name: 'addswipe',
914915 callback: addSwipeCallback,
916+ returns: 'the new swipe id',
915917 aliases: ['swipeadd'],
918+ namedArgumentList: [
919+ SlashCommandNamedArgument.fromProps({
920+ name: 'switch',
921+ description: 'switch to the new swipe',
922+ typeList: [ARGUMENT_TYPE.BOOLEAN],
923+ enumList: commonEnumProviders.boolean()(),
924+ }),
925+ ],
916926 unnamedArgumentList: [
917927 new SlashCommandArgument(
918928 'text', [ARGUMENT_TYPE.STRING], true,
919929 ),
920930 ],
921- helpString: 'Adds a swipe to the last chat message.',
931+ helpString: `
932+ <div>
933+ Adds a swipe to the last chat message.
934+ </div>
935+ <div>
936+ Use switch=true to switch to directly switch to the new swipe.
937+ </div>`,
922938 }));
923939 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
924940 name: 'stop',
@@ -2154,8 +2170,11 @@ async function echoCallback(args, value) {
21542170 }
21552171}
21562172
2157-
2173+/**
2158-async function addSwipeCallback(_, arg) {
2174+ * @param {{switch?: string}} args - named arguments
2175+ * @param {string} value - The swipe text to add (unnamed argument)
2176+ */
2177+async function addSwipeCallback(args, value) {
21592178 const lastMessage = chat[chat.length - 1];
21602179
21612180 if (!lastMessage) {
@@ -2163,7 +2182,7 @@ async function addSwipeCallback(_, arg) {
21632182 return '';
21642183 }
21652184
21662185 if (!argvalue) {
21672186 console.warn('WARN: No argument provided for /addswipe command');
21682187 return '';
21692188 }
@@ -2192,23 +2211,30 @@ async function addSwipeCallback(_, arg) {
21922211 lastMessage.swipe_info = lastMessage.swipes.map(() => ({}));
21932212 }
21942213
21952214 lastMessage.swipes.push(argvalue);
21962215 lastMessage.swipe_info.push({
21972216 send_date: getMessageTimeStamp(),
21982217 gen_started: null,
21992218 gen_finished: null,
22002219 extra: {
22012220 bias: extractMessageBias(argvalue),
22022221 gen_id: Date.now(),
22032222 api: 'manual',
22042223 model: 'slash command',
22052224 },
22062225 });
22072226
2227+ const newSwipeId = lastMessage.swipes.length - 1;
2228+
2229+ if (isTrueBoolean(args.switch)) {
2230+ lastMessage.swipe_id = newSwipeId;
2231+ lastMessage.mes = lastMessage.swipes[newSwipeId];
2232+ }
2233+
22082234 await saveChatConditional();
22092235 await reloadCurrentChat();
22102236
22112237 return ''String(newSwipeId);
22122238}
22132239
22142240async function deleteSwipeCallback(_, arg) {
@@ -2244,7 +2270,7 @@ async function deleteSwipeCallback(_, arg) {
22442270 await saveChatConditional();
22452271 await reloadCurrentChat();
22462272
22472273 return ''String(newSwipeId);
22482274}
22492275
22502276async function askCharacter(args, text) {