Migrate system prompts from imported instruct template

b0c537d014f3707e12bd32e830cf2556152aebd5

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

3 files changed, +43 -7Showing whitespace changes
public/scripts/preset-manager.js+6 -2
@@ -25,7 +25,7 @@ import { ARGUMENT_TYPE, SlashCommandArgument } from './slash-commands/SlashComma
2525import { enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';
2626import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js';
2727import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
2828import { checkForSystemPromptInInstructTemplate, system_prompts } from './sysprompt.js';
2929import {
3030 textgenerationwebui_preset_names,
3131 textgenerationwebui_presets,
@@ -72,7 +72,7 @@ function autoSelectPreset() {
7272 * @param {string} apiId API id
7373 * @returns {PresetManager} Preset manager
7474 */
7575export function getPresetManager(apiId = '') {
7676 if (!apiId) {
7777 apiId = main_api == 'koboldhorde' ? 'kobold' : main_api;
7878 }
@@ -183,6 +183,10 @@ class PresetManager {
183183 }
184184
185185 async savePreset(name, settings) {
186+ if (this.apiId === 'instruct') {
187+ await checkForSystemPromptInInstructTemplate(name, settings);
188+ }
189+
186190 const preset = settings ?? this.getPresetSettings(name);
187191
188192 const response = await fetch('/api/presets/save', {
public/scripts/sysprompt.js+30 -5
@@ -1,10 +1,13 @@
11import { saveSettingsDebounced } from '../script.js';
2+import { callGenericPopup, POPUP_TYPE } from './popup.js';
23import { power_user } from './power-user.js';
4+import { getPresetManager } from './preset-manager.js';
35import { SlashCommand } from './slash-commands/SlashCommand.js';
46import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js';
57import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';
68import { enumTypes, SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js';
79import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
10+import { renderTemplateAsync } from './templates.js';
811import { isTrueBoolean, resetScrollHeight } from './utils.js';
912
1013export let system_prompts = [];
@@ -39,7 +42,7 @@ export async function loadSystemPrompts(data) {
3942 }
4043
4144 migrateSystemPromptFromInstructMode();
4245 toggleSyspromptDisabledControlstoggleSystemPromptDisabledControls();
4346
4447 for (const prompt of system_prompts) {
4548 $('<option>').val(prompt.name).text(prompt.name).appendTo($select);
@@ -53,7 +56,29 @@ export async function loadSystemPrompts(data) {
5356 }
5457}
5558
56-function toggleSyspromptDisabledControls() {
59+/**
60+ * Checks if the instruct template has a system prompt and prompts the user to save it as a system prompt.
61+ * @param {string} name Name of the instruct template
62+ * @param {object} template Instruct template object
63+ */
64+export async function checkForSystemPromptInInstructTemplate(name, template) {
65+ if ('system_prompt' in template && name && template.system_prompt && !system_prompts.some(x => x.name === name)) {
66+ const html = await renderTemplateAsync('migrateInstructPrompt', { prompt: template.system_prompt });
67+ const confirm = await callGenericPopup(html, POPUP_TYPE.CONFIRM);
68+ if (confirm) {
69+ const prompt = { name: name, content: template.system_prompt };
70+ const presetManager = getPresetManager('sysprompt');
71+ await presetManager.savePreset(prompt.name, prompt);
72+ toastr.success(`System prompt "${prompt.name}" has been saved.`);
73+ } else {
74+ toastr.info('System prompt has been discarded.');
75+ }
76+
77+ delete template.system_prompt;
78+ }
79+}
80+
81+function toggleSystemPromptDisabledControls() {
5782 $enabled.parent().find('i').toggleClass('toggleEnabled', !!power_user.sysprompt.enabled);
5883 $contentBlock.toggleClass('disabled', !power_user.sysprompt.enabled);
5984}
@@ -61,7 +86,7 @@ function toggleSyspromptDisabledControls() {
6186function enableSystemPromptCallback() {
6287 power_user.sysprompt.enabled = true;
6388 $enabled.prop('checked', true);
6489 toggleSyspromptDisabledControlstoggleSystemPromptDisabledControls();
6590 saveSettingsDebounced();
6691 return '';
6792}
@@ -69,7 +94,7 @@ function enableSystemPromptCallback() {
6994function disableSystemPromptCallback() {
7095 power_user.sysprompt.enabled = false;
7196 $enabled.prop('checked', false);
7297 toggleSyspromptDisabledControlstoggleSystemPromptDisabledControls();
7398 saveSettingsDebounced();
7499 return '';
75100}
@@ -112,7 +137,7 @@ function selectSystemPromptCallback(args, name) {
112137jQuery(function () {
113138 $enabled.on('input', function () {
114139 power_user.sysprompt.enabled = !!$(this).prop('checked');
115140 toggleSyspromptDisabledControlstoggleSystemPromptDisabledControls();
116141 saveSettingsDebounced();
117142 });
118143
public/scripts/templates/migrateInstructPrompt.html+7 -0
@@ -0,0 +1,7 @@
1+<h3>
2+ This instruct template also contains a system prompt.
3+</h3>
4+<div class="marginBot10">
5+ Would you like to migrate the system prompt from the template?
6+</div>
7+<textarea class="wide100p textarea_compact" rows="8">{{prompt}}</textarea>