add QR API

This commit is contained in:
LenAnderson 2023-12-21 20:05:42 +00:00
parent 5125eaf1dc
commit a0918a3f5c
3 changed files with 450 additions and 103 deletions

View File

@ -0,0 +1,357 @@
import { QuickReply } from '../src/QuickReply.js';
import { QuickReplyContextLink } from '../src/QuickReplyContextLink.js';
import { QuickReplySet } from '../src/QuickReplySet.js';
import { QuickReplySettings } from '../src/QuickReplySettings.js';
export class QuickReplyApi {
/**@type {QuickReplySettings}*/ settings;
constructor(/**@type {QuickReplySettings}*/settings) {
this.settings = settings;
}
/**
* Finds and returns an existing Quick Reply Set by its name.
*
* @param {String} name name of the quick reply set
* @returns the quick reply set, or undefined if not found
*/
getSetByName(name) {
return QuickReplySet.get(name);
}
/**
* Finds and returns an existing Quick Reply by its set's name and its label.
*
* @param {String} setName name of the quick reply set
* @param {String} label label of the quick reply
* @returns the quick reply, or undefined if not found
*/
getQrByLabel(setName, label) {
const set = this.getSetByName(setName);
if (!set) return;
return set.qrList.find(it=>it.label == label);
}
/**
* Executes a quick reply by its index and returns the result.
*
* @param {Number} idx the index (zero-based) of the quick reply to execute
* @returns the return value of the quick reply, or undefined if not found
*/
async executeQuickReplyByIndex(idx) {
const qr = [...this.settings.config.setList, ...(this.settings.chatConfig?.setList ?? [])]
.map(it=>it.set.qrList)
.flat()[idx]
;
if (qr) {
return await qr.onExecute();
} else {
throw new Error(`No quick reply at index "${idx}"`);
}
}
/**
* Adds or removes a quick reply set to the list of globally active quick reply sets.
*
* @param {String} name the name of the set
* @param {Boolean} isVisible whether to show the set's buttons or not
*/
toggleGlobalSet(name, isVisible = true) {
const set = this.getSetByName(name);
if (!set) return;
if (this.settings.config.hasSet(set)) {
this.settings.config.removeSet(set);
} else {
this.settings.config.addSet(set, isVisible);
}
}
/**
* Adds a quick reply set to the list of globally active quick reply sets.
*
* @param {String} name the name of the set
* @param {Boolean} isVisible whether to show the set's buttons or not
*/
addGlobalSet(name, isVisible = true) {
const set = this.getSetByName(name);
if (!set) return;
this.settings.config.addSet(set, isVisible);
}
/**
* Removes a quick reply set from the list of globally active quick reply sets.
*
* @param {String} name the name of the set
*/
removeGlobalSet(name) {
const set = this.getSetByName(name);
if (!set) return;
this.settings.config.removeSet(set);
}
/**
* Adds or removes a quick reply set to the list of the current chat's active quick reply sets.
*
* @param {String} name the name of the set
* @param {Boolean} isVisible whether to show the set's buttons or not
*/
toggleChatSet(name, isVisible = true) {
if (!this.settings.chatConfig) return;
const set = this.getSetByName(name);
if (!set) return;
if (this.settings.chatConfig.hasSet(set)) {
this.settings.chatConfig.removeSet(set);
} else {
this.settings.chatConfig.addSet(set, isVisible);
}
}
/**
* Adds a quick reply set to the list of the current chat's active quick reply sets.
*
* @param {String} name the name of the set
* @param {Boolean} isVisible whether to show the set's buttons or not
*/
addChatSet(name, isVisible = true) {
if (!this.settings.chatConfig) return;
const set = this.getSetByName(name);
if (!set) return;
this.settings.chatConfig.addSet(set, isVisible);
}
/**
* Removes a quick reply set from the list of the current chat's active quick reply sets.
*
* @param {String} name the name of the set
*/
removeChatSet(name) {
if (!this.settings.chatConfig) return;
const set = this.getSetByName(name);
if (!set) return;
this.settings.chatConfig.removeSet(set);
}
/**
* Creates a new quick reply in an existing quick reply set.
*
* @param {String} setName name of the quick reply set to insert the new quick reply into
* @param {String} label label for the new quick reply (text on the button)
* @param {Object} [props]
* @param {String} [props.message] the message to be sent or slash command to be executed by the new quick reply
* @param {String} [props.title] the title / tooltip to be shown on the quick reply button
* @param {Boolean} [props.isHidden] whether to hide or show the button
* @param {Boolean} [props.executeOnStartup] whether to execute the quick reply when SillyTavern starts
* @param {Boolean} [props.executeOnUser] whether to execute the quick reply after a user has sent a message
* @param {Boolean} [props.executeOnAi] whether to execute the quick reply after the AI has sent a message
* @param {Boolean} [props.executeOnChatChange] whether to execute the quick reply when a new chat is loaded
* @returns {QuickReply} the new quick reply
*/
createQuickReply(setName, label, {
message,
title,
isHidden,
executeOnStartup,
executeOnUser,
executeOnAi,
executeOnChatChange,
} = {}) {
const set = this.getSetByName(setName);
if (!set) {
throw new Error(`No quick reply set with named "${setName}" found.`);
}
const qr = set.addQuickReply();
qr.label = label ?? '';
qr.message = message ?? '';
qr.title = title ?? '';
qr.isHidden = isHidden ?? false;
qr.executeOnStartup = executeOnStartup ?? false;
qr.executeOnUser = executeOnUser ?? false;
qr.executeOnAi = executeOnAi ?? false;
qr.executeOnChatChange = executeOnChatChange ?? false;
qr.onUpdate();
return qr;
}
/**
* Updates an existing quick reply.
*
* @param {String} setName name of the existing quick reply set
* @param {String} label label of the existing quick reply (text on the button)
* @param {Object} [props]
* @param {String} [props.newLabel] new label for quick reply (text on the button)
* @param {String} [props.message] the message to be sent or slash command to be executed by the quick reply
* @param {String} [props.title] the title / tooltip to be shown on the quick reply button
* @param {Boolean} [props.isHidden] whether to hide or show the button
* @param {Boolean} [props.executeOnStartup] whether to execute the quick reply when SillyTavern starts
* @param {Boolean} [props.executeOnUser] whether to execute the quick reply after a user has sent a message
* @param {Boolean} [props.executeOnAi] whether to execute the quick reply after the AI has sent a message
* @param {Boolean} [props.executeOnChatChange] whether to execute the quick reply when a new chat is loaded
* @returns {QuickReply} the altered quick reply
*/
updateQuickReply(setName, label, {
newLabel,
message,
title,
isHidden,
executeOnStartup,
executeOnUser,
executeOnAi,
executeOnChatChange,
} = {}) {
const qr = this.getQrByLabel(setName, label);
if (!qr) {
throw new Error(`No quick reply with label "${label}" in set "${setName}" found.`);
}
qr.label = newLabel ?? qr.label;
qr.message = message ?? qr.message;
qr.title = title ?? qr.title;
qr.isHidden = isHidden ?? qr.isHidden;
qr.executeOnStartup = executeOnStartup ?? qr.executeOnStartup;
qr.executeOnUser = executeOnUser ?? qr.executeOnUser;
qr.executeOnAi = executeOnAi ?? qr.executeOnAi;
qr.executeOnChatChange = executeOnChatChange ?? qr.executeOnChatChange;
qr.onUpdate();
return qr;
}
/**
* Deletes an existing quick reply.
*
* @param {String} setName name of the existing quick reply set
* @param {String} label label of the existing quick reply (text on the button)
*/
deleteQuickReply(setName, label) {
const qr = this.getQrByLabel(setName, label);
if (!qr) {
throw new Error(`No quick reply with label "${label}" in set "${setName}" found.`);
}
qr.delete();
}
/**
* Adds an existing quick reply set as a context menu to an existing quick reply.
*
* @param {String} setName name of the existing quick reply set containing the quick reply
* @param {String} label label of the existing quick reply
* @param {String} contextSetName name of the existing quick reply set to be used as a context menu
* @param {Boolean} isChained whether or not to chain the context menu quick replies
*/
createContextItem(setName, label, contextSetName, isChained = false) {
const qr = this.getQrByLabel(setName, label);
const set = this.getSetByName(contextSetName);
if (!qr) {
throw new Error(`No quick reply with label "${label}" in set "${setName}" found.`);
}
if (!set) {
throw new Error(`No quick reply set with name "${contextSetName}" found.`);
}
const cl = new QuickReplyContextLink();
cl.set = set;
cl.isChained = isChained;
qr.addContextLink(cl);
}
/**
* Removes a quick reply set from a quick reply's context menu.
*
* @param {String} setName name of the existing quick reply set containing the quick reply
* @param {String} label label of the existing quick reply
* @param {String} contextSetName name of the existing quick reply set to be used as a context menu
*/
deleteContextItem(setName, label, contextSetName) {
const qr = this.getQrByLabel(setName, label);
const set = this.getSetByName(contextSetName);
if (!qr) {
throw new Error(`No quick reply with label "${label}" in set "${setName}" found.`);
}
if (!set) {
throw new Error(`No quick reply set with name "${contextSetName}" found.`);
}
qr.removeContextLink(set.name);
}
/**
* Removes all entries from a quick reply's context menu.
*
* @param {String} setName name of the existing quick reply set containing the quick reply
* @param {String} label label of the existing quick reply
*/
clearContextMenu(setName, label) {
const qr = this.getQrByLabel(setName, label);
if (!qr) {
throw new Error(`No quick reply with label "${label}" in set "${setName}" found.`);
}
qr.clearContextLinks();
}
/**
* Create a new quick reply set.
*
* @param {String} name name of the new quick reply set
* @param {Object} [props]
* @param {Boolean} [props.disableSend] whether or not to send the quick replies or put the message or slash command into the char input box
* @param {Boolean} [props.placeBeforeInput] whether or not to place the quick reply contents before the existing user input
* @param {Boolean} [props.injectInput] whether or not to automatically inject the user input at the end of the quick reply
* @returns {QuickReplySet} the new quick reply set
*/
createSet(name, {
disableSend,
placeBeforeInput,
injectInput,
} = {}) {
const set = new QuickReplySet();
set.name = name;
set.disableSend = disableSend ?? false;
set.placeBeforeInput = placeBeforeInput ?? false;
set.injectInput = injectInput ?? false;
QuickReplySet.list.push(set);
set.save();
//TODO settings UI must be updated
return set;
}
/**
* Update an existing quick reply set.
*
* @param {String} name name of the existing quick reply set
* @param {Object} [props]
* @param {Boolean} [props.disableSend] whether or not to send the quick replies or put the message or slash command into the char input box
* @param {Boolean} [props.placeBeforeInput] whether or not to place the quick reply contents before the existing user input
* @param {Boolean} [props.injectInput] whether or not to automatically inject the user input at the end of the quick reply
* @returns {QuickReplySet} the altered quick reply set
*/
updateSet(name, {
disableSend,
placeBeforeInput,
injectInput,
} = {}) {
const set = this.getSetByName(name);
if (!set) {
throw new Error(`No quick reply set with name "${name}" found.`);
}
set.disableSend = disableSend ?? false;
set.placeBeforeInput = placeBeforeInput ?? false;
set.injectInput = injectInput ?? false;
set.save();
//TODO settings UI must be updated
return set;
}
}

View File

@ -1,5 +1,6 @@
import { chat_metadata, eventSource, event_types, getRequestHeaders } from '../../../script.js'; import { chat_metadata, eventSource, event_types, getRequestHeaders } from '../../../script.js';
import { extension_settings } from '../../extensions.js'; import { extension_settings } from '../../extensions.js';
import { QuickReplyApi } from './api/QuickReplyApi.js';
import { QuickReply } from './src/QuickReply.js'; import { QuickReply } from './src/QuickReply.js';
import { QuickReplyConfig } from './src/QuickReplyConfig.js'; import { QuickReplyConfig } from './src/QuickReplyConfig.js';
import { QuickReplyContextLink } from './src/QuickReplyContextLink.js'; import { QuickReplyContextLink } from './src/QuickReplyContextLink.js';
@ -13,8 +14,6 @@ import { SettingsUi } from './src/ui/SettingsUi.js';
//TODO move advanced QR options into own UI class //TODO move advanced QR options into own UI class
//TODO easy way to CRUD QRs and sets
//TODO easy way to set global and chat sets
@ -44,6 +43,8 @@ let settings;
let manager; let manager;
/** @type {ButtonUi} */ /** @type {ButtonUi} */
let buttons; let buttons;
/** @type {QuickReplyApi} */
export let api;
@ -152,7 +153,8 @@ const init = async () => {
} }
} }
const slash = new SlashCommandHandler(settings); api = new QuickReplyApi(settings);
const slash = new SlashCommandHandler(api);
slash.init(); slash.init();
}; };
eventSource.on(event_types.APP_READY, init); eventSource.on(event_types.APP_READY, init);

View File

@ -1,16 +1,14 @@
import { registerSlashCommand } from '../../../slash-commands.js'; import { registerSlashCommand } from '../../../slash-commands.js';
import { QuickReplyContextLink } from './QuickReplyContextLink.js'; import { QuickReplyApi } from '../api/QuickReplyApi.js';
import { QuickReplySet } from './QuickReplySet.js';
import { QuickReplySettings } from './QuickReplySettings.js';
export class SlashCommandHandler { export class SlashCommandHandler {
/**@type {QuickReplySettings}*/ settings; /**@type {QuickReplyApi}*/ api;
constructor(/**@type {QuickReplySettings}*/settings) { constructor(/**@type {QuickReplyApi}*/api) {
this.settings = settings; this.api = api;
} }
@ -61,7 +59,7 @@ export class SlashCommandHandler {
getSetByName(name) { getSetByName(name) {
const set = QuickReplySet.get(name); const set = this.api.getSetByName(name);
if (!set) { if (!set) {
toastr.error(`No Quick Reply Set with the name "${name}" could be found.`); toastr.error(`No Quick Reply Set with the name "${name}" could be found.`);
} }
@ -69,11 +67,9 @@ export class SlashCommandHandler {
} }
getQrByLabel(setName, label) { getQrByLabel(setName, label) {
const set = this.getSetByName(setName); const qr = this.api.getQrByLabel(setName, label);
if (!set) return;
const qr = set.qrList.find(it=>it.label == label);
if (!qr) { if (!qr) {
toastr.error(`No Quick Reply with the label "${label}" could be found in the set "${set.name}"`); toastr.error(`No Quick Reply with the label "${label}" could be found in the set "${setName}"`);
} }
return qr; return qr;
} }
@ -82,111 +78,102 @@ export class SlashCommandHandler {
async executeQuickReplyByIndex(idx) { async executeQuickReplyByIndex(idx) {
const qr = [...this.settings.config.setList, ...(this.settings.chatConfig?.setList ?? [])] try {
.map(it=>it.set.qrList) return await this.api.executeQuickReplyByIndex(idx);
.flat()[idx] } catch (ex) {
; toastr.error(ex.message);
if (qr) {
return await qr.onExecute();
} else {
toastr.error(`No Quick Reply at index "${idx}"`);
} }
} }
toggleGlobalSet(name, args = {}) { toggleGlobalSet(name, args = {}) {
const set = this.getSetByName(name); this.api.toggleGlobalSet(name, JSON.parse(args.visible ?? 'true') === true);
if (!set) return;
if (this.settings.config.hasSet(set)) {
this.settings.config.removeSet(set);
} else {
this.settings.config.addSet(set, JSON.parse(args.visible ?? 'true'));
}
} }
addGlobalSet(name, args = {}) { addGlobalSet(name, args = {}) {
const set = this.getSetByName(name); this.api.addGlobalSet(name, JSON.parse(args.visible ?? 'true') === true);
if (!set) return;
this.settings.config.addSet(set, JSON.parse(args.visible ?? 'true'));
} }
removeGlobalSet(name) { removeGlobalSet(name) {
const set = this.getSetByName(name); this.api.removeGlobalSet(name);
if (!set) return;
this.settings.config.removeSet(set);
} }
toggleChatSet(name, args = {}) { toggleChatSet(name, args = {}) {
if (!this.settings.chatConfig) return; this.api.toggleChatSet(name, JSON.parse(args.visible ?? 'true') === true);
const set = this.getSetByName(name);
if (!set) return;
if (this.settings.chatConfig.hasSet(set)) {
this.settings.chatConfig.removeSet(set);
} else {
this.settings.chatConfig.addSet(set, JSON.parse(args.visible ?? 'true'));
}
} }
addChatSet(name, args = {}) { addChatSet(name, args = {}) {
if (!this.settings.chatConfig) return; this.api.addChatSet(name, JSON.parse(args.visible ?? 'true') === true);
const set = this.getSetByName(name);
if (!set) return;
this.settings.chatConfig.addSet(set, JSON.parse(args.visible ?? 'true'));
} }
removeChatSet(name) { removeChatSet(name) {
if (!this.settings.chatConfig) return; this.api.removeChatSet(name);
const set = this.getSetByName(name);
if (!set) return;
this.settings.chatConfig.removeSet(set);
} }
createQuickReply(args, message) { createQuickReply(args, message) {
const set = this.getSetByName(args.set); try {
if (!set) return; this.api.createQuickReply(
const qr = set.addQuickReply(); args.set ?? '',
qr.label = args.label ?? ''; args.label ?? '',
qr.message = message ?? ''; {
qr.title = args.title ?? ''; message: message ?? '',
qr.isHidden = JSON.parse(args.hidden ?? 'false') === true; title: args.title,
qr.executeOnStartup = JSON.parse(args.startup ?? 'false') === true; isHidden: JSON.parse(args.hidden ?? 'false') === true,
qr.executeOnUser = JSON.parse(args.user ?? 'false') === true; executeOnStartup: JSON.parse(args.startup ?? 'false') === true,
qr.executeOnAi = JSON.parse(args.bot ?? 'false') === true; executeOnUser: JSON.parse(args.user ?? 'false') === true,
qr.executeOnChatChange = JSON.parse(args.load ?? 'false') === true; executeOnAi: JSON.parse(args.bot ?? 'false') === true,
qr.onUpdate(); executeOnChatChange: JSON.parse(args.load ?? 'false') === true,
},
);
} catch (ex) {
toastr.error(ex.message);
}
} }
updateQuickReply(args, message) { updateQuickReply(args, message) {
const qr = this.getQrByLabel(args.set, args.label); try {
if (!qr) return; this.api.updateQuickReply(
qr.message = (message ?? '').trim().length > 0 ? message : qr.message; args.set ?? '',
qr.label = args.newlabel !== undefined ? (args.newlabel ?? '') : qr.label; args.label ?? '',
qr.title = args.title !== undefined ? (args.title ?? '') : qr.title; {
qr.isHidden = args.hidden !== undefined ? (JSON.parse(args.hidden ?? 'false') === true) : qr.isHidden; newLabel: args.newlabel,
qr.executeOnStartup = args.startup !== undefined ? (JSON.parse(args.startup ?? 'false') === true) : qr.executeOnStartup; message: (message ?? '').trim().length > 0 ? message : undefined,
qr.executeOnUser = args.user !== undefined ? (JSON.parse(args.user ?? 'false') === true) : qr.executeOnUser; title: args.title,
qr.executeOnAi = args.bot !== undefined ? (JSON.parse(args.bot ?? 'false') === true) : qr.executeOnAi; isHidden: args.hidden,
qr.executeOnChatChange = args.load !== undefined ? (JSON.parse(args.load ?? 'false') === true) : qr.executeOnChatChange; executeOnStartup: args.startup,
qr.onUpdate(); executeOnUser: args.user,
executeOnAi: args.bot,
executeOnChatChange: args.load,
},
);
} catch (ex) {
toastr.error(ex.message);
}
} }
deleteQuickReply(args, label) { deleteQuickReply(args, label) {
const qr = this.getQrByLabel(args.set, args.label ?? label); try {
if (!qr) return; this.api.deleteQuickReply(args.set, label);
qr.delete(); } catch (ex) {
toastr.error(ex.message);
}
} }
createContextItem(args, name) { createContextItem(args, name) {
const qr = this.getQrByLabel(args.set, args.label); try {
const set = this.getSetByName(name); this.api.createContextItem(
if (!qr || !set) return; args.set,
const cl = new QuickReplyContextLink(); args.label,
cl.set = set; name,
cl.isChained = JSON.parse(args.chain ?? 'false') ?? false; JSON.parse(args.chain ?? 'false') === true,
qr.addContextLink(cl); );
} catch (ex) {
toastr.error(ex.message);
}
} }
deleteContextItem(args, name) { deleteContextItem(args, name) {
const qr = this.getQrByLabel(args.set, args.label); try {
const set = this.getSetByName(name); this.api.deleteContextItem(args.set, args.label, name);
if (!qr || !set) return; } catch (ex) {
qr.removeContextLink(set.name); toastr.error(ex.message);
}
} }
clearContextMenu(args, label) { clearContextMenu(args, label) {
const qr = this.getQrByLabel(args.set, args.label ?? label); const qr = this.getQrByLabel(args.set, args.label ?? label);
@ -196,22 +183,23 @@ export class SlashCommandHandler {
createSet(name, args) { createSet(name, args) {
const set = new QuickReplySet(); this.api.createSet(
set.name = args.name ?? name; args.name ?? name ?? '',
set.disableSend = JSON.parse(args.nosend ?? 'false') === true; {
set.placeBeforeInput = JSON.parse(args.before ?? 'false') === true; disableSend: JSON.parse(args.nosend ?? 'false') === true,
set.injectInput = JSON.parse(args.inject ?? 'false') === true; placeBeforeInput: JSON.parse(args.before ?? 'false') === true,
QuickReplySet.list.push(set); injectInput: JSON.parse(args.inject ?? 'false') === true,
set.save(); },
//TODO settings UI must be updated );
} }
updateSet(name, args) { updateSet(name, args) {
const set = this.getSetByName(args.name ?? name); this.api.updateSet(
if (!set) return; args.name ?? name ?? '',
set.disableSend = args.nosend !== undefined ? (JSON.parse(args.nosend ?? 'false') === true) : set.disableSend; {
set.placeBeforeInput = args.before !== undefined ? (JSON.parse(args.before ?? 'false') === true) : set.placeBeforeInput; disableSend: args.nosend !== undefined ? JSON.parse(args.nosend ?? 'false') === true : undefined,
set.injectInput = args.inject !== undefined ? (JSON.parse(args.inject ?? 'false') === true) : set.injectInput; placeBeforeInput: args.before !== undefined ? JSON.parse(args.before ?? 'false') === true : undefined,
set.save(); injectInput: args.inject !== undefined ? JSON.parse(args.inject ?? 'false') === true : undefined,
//TODO settings UI must be updated },
);
} }
} }