From 0bc6a572c6ca35a863fe2688911dabfee5a5056d Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 17 Sep 2024 12:14:13 +0000 Subject: [PATCH] Add slash commands and connection profiles support --- .../extensions/connection-manager/index.js | 9 ++ public/scripts/sysprompt.js | 131 +++++++++++++++++- 2 files changed, 139 insertions(+), 1 deletion(-) diff --git a/public/scripts/extensions/connection-manager/index.js b/public/scripts/extensions/connection-manager/index.js index 2fbcdec2b..d135c502e 100644 --- a/public/scripts/extensions/connection-manager/index.js +++ b/public/scripts/extensions/connection-manager/index.js @@ -33,6 +33,8 @@ const CC_COMMANDS = [ const TC_COMMANDS = [ ...COMMON_COMMANDS, + 'sysprompt', + 'sysprompt-state', 'instruct', 'context', 'instruct-state', @@ -45,6 +47,8 @@ const FANCY_NAMES = { 'preset': 'Settings Preset', 'model': 'Model', 'proxy': 'Proxy Preset', + 'sysprompt-state': 'Use System Prompt', + 'sysprompt': 'System Prompt Name', 'instruct-state': 'Instruct Mode', 'instruct': 'Instruct Template', 'context': 'Context Template', @@ -180,6 +184,11 @@ async function readProfileFromCommands(mode, profile, cleanUp = false) { } if (cleanUp) { + for (const command of commands) { + if (command.endsWith('-state') && profile[command] === 'false') { + delete profile[command.replace('-state', '')]; + } + } for (const command of opposingCommands) { if (commands.includes(command)) { continue; diff --git a/public/scripts/sysprompt.js b/public/scripts/sysprompt.js index a9d752012..b212bf816 100644 --- a/public/scripts/sysprompt.js +++ b/public/scripts/sysprompt.js @@ -1,6 +1,11 @@ import { saveSettingsDebounced } from '../script.js'; import { power_user } from './power-user.js'; -import { resetScrollHeight } from './utils.js'; +import { SlashCommand } from './slash-commands/SlashCommand.js'; +import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js'; +import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js'; +import { enumTypes, SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js'; +import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; +import { isTrueBoolean, resetScrollHeight } from './utils.js'; export let system_prompts = []; @@ -53,6 +58,57 @@ function toggleSyspromptDisabledControls() { $contentBlock.toggleClass('disabled', !power_user.sysprompt.enabled); } +function enableSystemPromptCallback() { + power_user.sysprompt.enabled = true; + $enabled.prop('checked', true); + toggleSyspromptDisabledControls(); + saveSettingsDebounced(); + return ''; +} + +function disableSystemPromptCallback() { + power_user.sysprompt.enabled = false; + $enabled.prop('checked', false); + toggleSyspromptDisabledControls(); + saveSettingsDebounced(); + return ''; +} + +function toggleSystemPromptCallback(_args, state) { + if (!state || typeof state !== 'string') { + return String(power_user.sysprompt.enabled); + } + + const newState = isTrueBoolean(state); + newState ? enableSystemPromptCallback() : disableSystemPromptCallback(); + return String(power_user.sysprompt.enabled); +} + +function selectSystemPromptCallback(args, name) { + if (!power_user.sysprompt.enabled && !isTrueBoolean(args.forceGet)) { + return ''; + } + + if (!name) { + return power_user.sysprompt.name; + } + + const quiet = isTrueBoolean(args?.quiet); + const instructNames = system_prompts.map(preset => preset.name); + const fuse = new Fuse(instructNames); + const result = fuse.search(name); + + if (result.length === 0) { + !quiet && toastr.warning(`System prompt "${name}" not found`); + return ''; + } + + const foundName = result[0].item; + $select.val(foundName).trigger('input'); + !quiet && toastr.success(`System prompt "${foundName}" selected`); + return foundName; +} + jQuery(function () { $enabled.on('input', function () { power_user.sysprompt.enabled = !!$(this).prop('checked'); @@ -61,6 +117,10 @@ jQuery(function () { }); $select.on('input', async function () { + if (!power_user.sysprompt.enabled) { + $enabled.prop('checked', true).trigger('input'); + } + const name = String($(this).val()); const prompt = system_prompts.find(p => p.name === name); if (prompt) { @@ -79,4 +139,73 @@ jQuery(function () { power_user.sysprompt.content = String($(this).val()); saveSettingsDebounced(); }); + + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'sysprompt', + aliases: ['system-prompt'], + callback: selectSystemPromptCallback, + returns: 'current prompt name', + namedArgumentList: [ + SlashCommandNamedArgument.fromProps({ + name: 'quiet', + description: 'Suppress the toast message on prompt change', + typeList: [ARGUMENT_TYPE.BOOLEAN], + defaultValue: 'false', + enumList: commonEnumProviders.boolean('trueFalse')(), + }), + SlashCommandNamedArgument.fromProps({ + name: 'forceGet', + description: 'Force getting a name even if system prompt is disabled', + typeList: [ARGUMENT_TYPE.BOOLEAN], + defaultValue: 'false', + enumList: commonEnumProviders.boolean('trueFalse')(), + }), + ], + unnamedArgumentList: [ + SlashCommandArgument.fromProps({ + description: 'system prompt name', + typeList: [ARGUMENT_TYPE.STRING], + enumProvider: () => system_prompts.map(x => new SlashCommandEnumValue(x.name, null, enumTypes.enum, enumIcons.preset)), + }), + ], + helpString: ` +
+ Selects a system prompt by name. Enables the use of system prompt if not already enabled. + Gets the current system prompt if no name is provided and sysprompt is enabled or forceGet=true is passed. +
+
+ Example: + +
+ `, + })); + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'sysprompt-on', + aliases: ['sysprompt-enable'], + callback: enableSystemPromptCallback, + helpString: 'Enables system prompt.', + })); + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'sysprompt-off', + aliases: ['sysprompt-disable'], + callback: disableSystemPromptCallback, + helpString: 'Disables system prompt', + })); + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'sysprompt-state', + aliases: ['sysprompt-toggle'], + helpString: 'Gets the current system prompt state. If an argument is provided, it will set the system prompt state.', + unnamedArgumentList: [ + SlashCommandArgument.fromProps({ + description: 'system prompt state', + typeList: [ARGUMENT_TYPE.BOOLEAN], + enumList: commonEnumProviders.boolean('trueFalse')(), + }), + ], + callback: toggleSystemPromptCallback, + })); });