Add shouldRegister arg to /tools-register
| @@ -10,6 +10,7 @@ import { enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js'; | ||
| 10 | 10 | import { enumTypes, SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js'; |
| 11 | 11 | import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; |
| 12 | 12 | import { slashCommandReturnHelper } from './slash-commands/SlashCommandReturnHelper.js'; |
| 13 | +import { isTrueBoolean } from './utils.js'; | |
| 13 | 14 | |
| 14 | 15 | /** |
| 15 | 16 | * @typedef {object} ToolInvocation |
| @@ -852,6 +853,13 @@ export class ToolManager { | ||
| 852 | 853 | isRequired: true, |
| 853 | 854 | acceptsMultiple: false, |
| 854 | 855 | }), |
| 856 | + SlashCommandNamedArgument.fromProps({ | |
| 857 | + name: 'shouldRegister', | |
| 858 | + description: 'The closure to be executed to determine if the tool should be registered. Must return a boolean.', | |
| 859 | + typeList: [ARGUMENT_TYPE.CLOSURE], | |
| 860 | + isRequired: false, | |
| 861 | + acceptsMultiple: false, | |
| 862 | + }), | |
| 855 | 863 | ], |
| 856 | 864 | unnamedArgumentList: [ |
| 857 | 865 | SlashCommandArgument.fromProps({ |
| @@ -865,9 +873,10 @@ export class ToolManager { | ||
| 865 | 873 | /** |
| 866 | 874 | * Converts a slash command closure to a function. |
| 867 | 875 | * @param {SlashCommandClosure} action Closure to convert to a function |
| 876 | + * @param {function(any): any} convertResult Function to convert the result | |
| 868 | 877 | * @returns {function} Function that executes the closure |
| 869 | 878 | */ |
| 870 | 879 | function closureToFunction(action, convertResult) { |
| 871 | 880 | return async (args) => { |
| 872 | 881 | const localClosure = action.getCopy(); |
| 873 | 882 | localClosure.onProgress = () => { }; |
| @@ -878,11 +887,11 @@ export class ToolManager { | ||
| 878 | 887 | scope.letVariable('arg', args); |
| 879 | 888 | } |
| 880 | 889 | const result = await localClosure.execute(); |
| 881 | 890 | return convertResult(result.pipe); |
| 882 | 891 | }; |
| 883 | 892 | } |
| 884 | 893 | |
| 885 | 894 | const { name, displayName, description, parameters, formatMessage, shouldRegister } = args; |
| 886 | 895 | |
| 887 | 896 | if (!(action instanceof SlashCommandClosure)) { |
| 888 | 897 | throw new Error('The unnamed argument must be a closure.'); |
| @@ -902,9 +911,13 @@ export class ToolManager { | ||
| 902 | 911 | if (formatMessage && !(formatMessage instanceof SlashCommandClosure)) { |
| 903 | 912 | throw new Error('The "formatMessage" argument must be a closure.'); |
| 904 | 913 | } |
| 914 | + if (shouldRegister && !(shouldRegister instanceof SlashCommandClosure)) { | |
| 915 | + throw new Error('The "shouldRegister" argument must be a closure.'); | |
| 916 | + } | |
| 905 | 917 | |
| 906 | 918 | const actionFunc = closureToFunction(action, x => x); |
| 907 | 919 | const formatMessageFunc = formatMessage instanceof SlashCommandClosure ? closureToFunction(formatMessage, x => String(x)) : null; |
| 920 | + const shouldRegisterFunc = shouldRegister instanceof SlashCommandClosure ? closureToFunction(shouldRegister, x => isTrueBoolean(x)) : null; | |
| 908 | 921 | |
| 909 | 922 | ToolManager.registerFunctionTool({ |
| 910 | 923 | name: String(name ?? ''), |
| @@ -913,7 +926,7 @@ export class ToolManager { | ||
| 913 | 926 | parameters: JSON.parse(parameters ?? '{}'), |
| 914 | 927 | action: actionFunc, |
| 915 | 928 | formatMessage: formatMessageFunc, |
| 916 | - shouldRegister: async () => true, // TODO: Implement shouldRegister | |
| 929 | + shouldRegister: shouldRegisterFunc, | |
| 917 | 930 | }); |
| 918 | 931 | |
| 919 | 932 | return ''; |