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