diff --git a/public/scripts/extensions/quick-reply/api/QuickReplyApi.js b/public/scripts/extensions/quick-reply/api/QuickReplyApi.js index 6565ff3bb..926003634 100644 --- a/public/scripts/extensions/quick-reply/api/QuickReplyApi.js +++ b/public/scripts/extensions/quick-reply/api/QuickReplyApi.js @@ -202,6 +202,7 @@ export class QuickReplyApi { * @param {boolean} [props.executeOnChatChange] whether to execute the quick reply when a new chat is loaded * @param {boolean} [props.executeOnGroupMemberDraft] whether to execute the quick reply when a group member is selected * @param {boolean} [props.executeOnNewChat] whether to execute the quick reply when a new chat is created + * @param {boolean} [props.executeBeforeGeneration] whether to execute the quick reply before message generation * @param {string} [props.automationId] when not empty, the quick reply will be executed when the WI with the given automation ID is activated * @returns {QuickReply} the new quick reply */ @@ -217,6 +218,7 @@ export class QuickReplyApi { executeOnChatChange, executeOnGroupMemberDraft, executeOnNewChat, + executeBeforeGeneration, automationId, } = {}) { const set = this.getSetByName(setName); @@ -236,6 +238,7 @@ export class QuickReplyApi { qr.executeOnChatChange = executeOnChatChange ?? false; qr.executeOnGroupMemberDraft = executeOnGroupMemberDraft ?? false; qr.executeOnNewChat = executeOnNewChat ?? false; + qr.executeBeforeGeneration = executeBeforeGeneration ?? false; qr.automationId = automationId ?? ''; qr.onUpdate(); return qr; @@ -259,6 +262,7 @@ export class QuickReplyApi { * @param {boolean} [props.executeOnChatChange] whether to execute the quick reply when a new chat is loaded * @param {boolean} [props.executeOnGroupMemberDraft] whether to execute the quick reply when a group member is selected * @param {boolean} [props.executeOnNewChat] whether to execute the quick reply when a new chat is created + * @param {boolean} [props.executeBeforeGeneration] whether to execute the quick reply before message generation * @param {string} [props.automationId] when not empty, the quick reply will be executed when the WI with the given automation ID is activated * @returns {QuickReply} the altered quick reply */ @@ -275,6 +279,7 @@ export class QuickReplyApi { executeOnChatChange, executeOnGroupMemberDraft, executeOnNewChat, + executeBeforeGeneration, automationId, } = {}) { const qr = this.getQrByLabel(setName, label); @@ -293,6 +298,7 @@ export class QuickReplyApi { qr.executeOnChatChange = executeOnChatChange ?? qr.executeOnChatChange; qr.executeOnGroupMemberDraft = executeOnGroupMemberDraft ?? qr.executeOnGroupMemberDraft; qr.executeOnNewChat = executeOnNewChat ?? qr.executeOnNewChat; + qr.executeBeforeGeneration = executeBeforeGeneration ?? qr.executeBeforeGeneration; qr.automationId = automationId ?? qr.automationId; qr.onUpdate(); return qr; diff --git a/public/scripts/extensions/quick-reply/html/qrEditor.html b/public/scripts/extensions/quick-reply/html/qrEditor.html index e81a11ba2..6fc47b573 100644 --- a/public/scripts/extensions/quick-reply/html/qrEditor.html +++ b/public/scripts/extensions/quick-reply/html/qrEditor.html @@ -116,6 +116,10 @@ Execute on group member draft +
Automation ID diff --git a/public/scripts/extensions/quick-reply/index.js b/public/scripts/extensions/quick-reply/index.js index bbfc4dfc0..18e786bf1 100644 --- a/public/scripts/extensions/quick-reply/index.js +++ b/public/scripts/extensions/quick-reply/index.js @@ -85,6 +85,7 @@ const loadSets = async () => { qr.executeOnChatChange = slot.autoExecute_chatLoad ?? false; qr.executeOnGroupMemberDraft = slot.autoExecute_groupMemberDraft ?? false; qr.executeOnNewChat = slot.autoExecute_newChat ?? false; + qr.executeBeforeGeneration = slot.autoExecute_beforeGeneration ?? false; qr.automationId = slot.automationId ?? ''; qr.contextList = (slot.contextMenu ?? []).map(it=>({ set: it.preset, @@ -307,3 +308,16 @@ const onNewChat = async () => { await autoExec.handleNewChat(); }; eventSource.on(event_types.CHAT_CREATED, (...args) => executeIfReadyElseQueue(onNewChat, args)); + +const onBeforeGeneration = async (_generationType, _options = {}, isDryRun = false) => { + if (isDryRun) { + log('Before-generation hook skipped due to dryRun.'); + return; + } + if (selected_group && this_chid === undefined) { + log('Before-generation hook skipped for event before group wrapper.'); + return; + } + await autoExec.handleBeforeGeneration(); +}; +eventSource.on(event_types.GENERATION_AFTER_COMMANDS, (...args) => executeIfReadyElseQueue(onBeforeGeneration, args)); diff --git a/public/scripts/extensions/quick-reply/src/AutoExecuteHandler.js b/public/scripts/extensions/quick-reply/src/AutoExecuteHandler.js index b80e29e0e..85cfd02ac 100644 --- a/public/scripts/extensions/quick-reply/src/AutoExecuteHandler.js +++ b/public/scripts/extensions/quick-reply/src/AutoExecuteHandler.js @@ -78,6 +78,11 @@ export class AutoExecuteHandler { await this.performAutoExecute(this.getCommands('executeOnNewChat')); } + async handleBeforeGeneration() { + if (!this.checkExecute()) return; + await this.performAutoExecute(this.getCommands('executeBeforeGeneration')); + } + /** * @param {any[]} entries Set of activated entries */ diff --git a/public/scripts/extensions/quick-reply/src/QuickReply.js b/public/scripts/extensions/quick-reply/src/QuickReply.js index 4afccd512..2f3a87ff1 100644 --- a/public/scripts/extensions/quick-reply/src/QuickReply.js +++ b/public/scripts/extensions/quick-reply/src/QuickReply.js @@ -46,6 +46,7 @@ export class QuickReply { /**@type {boolean}*/ executeOnChatChange = false; /**@type {boolean}*/ executeOnGroupMemberDraft = false; /**@type {boolean}*/ executeOnNewChat = false; + /**@type {boolean}*/ executeBeforeGeneration = false; /**@type {string}*/ automationId = ''; /**@type {function}*/ onExecute; @@ -1065,6 +1066,13 @@ export class QuickReply { this.updateContext(); }); /**@type {HTMLInputElement}*/ + const executeBeforeGeneration = dom.querySelector('#qr--executeBeforeGeneration'); + executeBeforeGeneration.checked = this.executeBeforeGeneration; + executeBeforeGeneration.addEventListener('click', ()=>{ + this.executeBeforeGeneration = executeBeforeGeneration.checked; + this.updateContext(); + }); + /**@type {HTMLInputElement}*/ const executeOnNewChat = dom.querySelector('#qr--executeOnNewChat'); executeOnNewChat.checked = this.executeOnNewChat; executeOnNewChat.addEventListener('click', ()=>{ @@ -1919,6 +1927,7 @@ export class QuickReply { executeOnChatChange: this.executeOnChatChange, executeOnGroupMemberDraft: this.executeOnGroupMemberDraft, executeOnNewChat: this.executeOnNewChat, + executeBeforeGeneration: this.executeBeforeGeneration, automationId: this.automationId, }; } diff --git a/public/scripts/extensions/quick-reply/src/SlashCommandHandler.js b/public/scripts/extensions/quick-reply/src/SlashCommandHandler.js index 1c14dcbd2..555aed880 100644 --- a/public/scripts/extensions/quick-reply/src/SlashCommandHandler.js +++ b/public/scripts/extensions/quick-reply/src/SlashCommandHandler.js @@ -36,6 +36,7 @@ export class SlashCommandHandler { if (qr.executeOnChatChange) icons += '💬'; if (qr.executeOnNewChat) icons += '🆕'; if (qr.executeOnGroupMemberDraft) icons += enumIcons.group; + if (qr.executeBeforeGeneration) icons += '✈️'; return icons; } @@ -267,6 +268,7 @@ export class SlashCommandHandler { new SlashCommandNamedArgument('load', 'auto execute on chat load, e.g., load=true', [ARGUMENT_TYPE.BOOLEAN], false, false, 'false'), new SlashCommandNamedArgument('new', 'auto execute on new chat, e.g., new=true', [ARGUMENT_TYPE.BOOLEAN], false, false, 'false'), new SlashCommandNamedArgument('group', 'auto execute on group member selection, e.g., group=true', [ARGUMENT_TYPE.BOOLEAN], false, false, 'false'), + new SlashCommandNamedArgument('generation', 'auto execute before message generation, e.g., generation=true', [ARGUMENT_TYPE.BOOLEAN], false, false, 'false'), new SlashCommandNamedArgument('title', 'title / tooltip to be shown on button, e.g., title="My Fancy Button"', [ARGUMENT_TYPE.STRING], false), ]; const qrUpdateArgs = [ @@ -874,6 +876,7 @@ export class SlashCommandHandler { executeOnChatChange: isTrueBoolean(args.load), executeOnNewChat: isTrueBoolean(args.new), executeOnGroupMemberDraft: isTrueBoolean(args.group), + executeBeforeGeneration: isTrueBoolean(args.generation), automationId: args.automationId ?? '', }, ); @@ -910,6 +913,7 @@ export class SlashCommandHandler { executeOnChatChange: args.load === undefined ? undefined : isTrueBoolean(args.load), executeOnGroupMemberDraft: args.group === undefined ? undefined : isTrueBoolean(args.group), executeOnNewChat: args.new === undefined ? undefined : isTrueBoolean(args.new), + executeBeforeGeneration: args.generation === undefined ? undefined : isTrueBoolean(args.generation), automationId: args.automationId ?? '', }, );