mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add slash commands and connection profiles support
This commit is contained in:
@ -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,
|
||||
}));
|
||||
});
|
||||
|
Reference in New Issue
Block a user