Add shouldRegister arg to /tools-register

649c3911eb458abdc40b3722ce6e0ce22d7c56a5

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

1 files changed, +19 -6Ignore whitespace
public/scripts/tool-calling.js+19 -6
@@ -10,6 +10,7 @@ import { enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';
1010import { enumTypes, SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js';
1111import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
1212import { slashCommandReturnHelper } from './slash-commands/SlashCommandReturnHelper.js';
13+import { isTrueBoolean } from './utils.js';
1314
1415/**
1516 * @typedef {object} ToolInvocation
@@ -852,6 +853,13 @@ export class ToolManager {
852853 isRequired: true,
853854 acceptsMultiple: false,
854855 }),
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+ }),
855863 ],
856864 unnamedArgumentList: [
857865 SlashCommandArgument.fromProps({
@@ -865,9 +873,10 @@ export class ToolManager {
865873 /**
866874 * Converts a slash command closure to a function.
867875 * @param {SlashCommandClosure} action Closure to convert to a function
876+ * @param {function(any): any} convertResult Function to convert the result
868877 * @returns {function} Function that executes the closure
869878 */
870879 function closureToFunction(action, convertResult) {
871880 return async (args) => {
872881 const localClosure = action.getCopy();
873882 localClosure.onProgress = () => { };
@@ -878,11 +887,11 @@ export class ToolManager {
878887 scope.letVariable('arg', args);
879888 }
880889 const result = await localClosure.execute();
881890 return convertResult(result.pipe);
882891 };
883892 }
884893
885894 const { name, displayName, description, parameters, formatMessage, shouldRegister } = args;
886895
887896 if (!(action instanceof SlashCommandClosure)) {
888897 throw new Error('The unnamed argument must be a closure.');
@@ -902,9 +911,13 @@ export class ToolManager {
902911 if (formatMessage && !(formatMessage instanceof SlashCommandClosure)) {
903912 throw new Error('The "formatMessage" argument must be a closure.');
904913 }
914+ if (shouldRegister && !(shouldRegister instanceof SlashCommandClosure)) {
915+ throw new Error('The "shouldRegister" argument must be a closure.');
916+ }
905917
906918 const actionFunc = closureToFunction(action, x => x);
907919 const formatMessageFunc = formatMessage instanceof SlashCommandClosure ? closureToFunction(formatMessage, x => String(x)) : null;
920+ const shouldRegisterFunc = shouldRegister instanceof SlashCommandClosure ? closureToFunction(shouldRegister, x => isTrueBoolean(x)) : null;
908921
909922 ToolManager.registerFunctionTool({
910923 name: String(name ?? ''),
@@ -913,7 +926,7 @@ export class ToolManager {
913926 parameters: JSON.parse(parameters ?? '{}'),
914927 action: actionFunc,
915928 formatMessage: formatMessageFunc,
916- shouldRegister: async () => true, // TODO: Implement shouldRegister
929+ shouldRegister: shouldRegisterFunc,
917930 });
918931
919932 return '';