Add slash commands and connection profiles support

This commit is contained in:
Cohee 2024-09-17 12:14:13 +00:00
parent 912bebeb01
commit 0bc6a572c6
2 changed files with 139 additions and 1 deletions

View File

@ -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;

View File

@ -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: `
<div>
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 <code>forceGet=true</code> is passed.
</div>
<div>
<strong>Example:</strong>
<ul>
<li>
<pre><code class="language-stscript">/sysprompt </code></pre>
</li>
</ul>
</div>
`,
}));
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,
}));
});