Merge branch 'staging' of https://github.com/Tony-sama/SillyTavern into staging

This commit is contained in:
Tony Ribeiro
2023-08-24 02:34:14 +02:00
21 changed files with 1549 additions and 242 deletions

View File

@@ -1772,6 +1772,12 @@ const chatCompletionDefaultPrompts = {
"system_prompt": true,
"marker": true,
},
{
"identifier": "personaDescription",
"name": "Persona Description",
"system_prompt": true,
"marker": true,
},
]
};
@@ -1788,6 +1794,10 @@ const promptManagerDefaultPromptOrder = [
"identifier": "worldInfoBefore",
"enabled": true
},
{
"identifier": "personaDescription",
"enabled": true
},
{
"identifier": "charDescription",
"enabled": true

View File

@@ -315,7 +315,8 @@ function RA_checkOnlineStatus() {
if (online_status == "no_connection") {
$("#send_textarea").attr("placeholder", "Not connected to API!"); //Input bar placeholder tells users they are not connected
$("#send_form").addClass('no-connection'); //entire input form area is red when not connected
$("#send_but").css("display", "none"); //send button is hidden when not connected;
$("#send_but").addClass("displayNone"); //send button is hidden when not connected;
$("#mes_continue").addClass("displayNone"); //continue button is hidden when not connected;
$("#API-status-top").removeClass("fa-plug");
$("#API-status-top").addClass("fa-plug-circle-exclamation redOverlayGlow");
connection_made = false;
@@ -330,7 +331,8 @@ function RA_checkOnlineStatus() {
RA_AC_retries = 1;
if (!is_send_press && !(selected_group && is_group_generating)) {
$("#send_but").css("display", "flex"); //on connect, send button shows
$("#send_but").removeClass("displayNone"); //on connect, send button shows
$("#mes_continue").removeClass("displayNone"); //continue button is shown when connected
}
}
}

View File

@@ -14,7 +14,37 @@ export {
let extensionNames = [];
let manifests = {};
const defaultUrl = "http://localhost:5100";
export const saveMetadataDebounced = debounce(async () => await getContext().saveMetadata(), 1000);
let saveMetadataTimeout = null;
export function saveMetadataDebounced() {
const context = getContext();
const groupId = context.groupId;
const characterId = context.characterId;
if (saveMetadataTimeout) {
console.debug('Clearing save metadata timeout');
clearTimeout(saveMetadataTimeout);
}
saveMetadataTimeout = setTimeout(async () => {
const newContext = getContext();
if (groupId !== newContext.groupId) {
console.warn('Group changed, not saving metadata');
return;
}
if (characterId !== newContext.characterId) {
console.warn('Character changed, not saving metadata');
return;
}
console.debug('Saving metadata...');
newContext.saveMetadata();
console.debug('Saved metadata...');
}, 1000);
}
export const extensionsHandlebars = Handlebars.create();

View File

@@ -164,7 +164,7 @@ function getNextIncompleteTaskRecurse(task){
}
// Set a task in extensionPrompt context. Defaults to first incomplete
function setCurrentTask(taskId = null) {
function setCurrentTask(taskId = null, skipSave = false) {
const context = getContext();
// TODO: Should probably null this rather than set empty object
@@ -202,7 +202,10 @@ function setCurrentTask(taskId = null) {
console.info(`No current task`);
}
saveState();
// Save state if not skipping
if (!skipSave) {
saveState();
}
}
function getHighestTaskIdRecurse(task) {
@@ -731,7 +734,7 @@ function loadSettings() {
$('#objective-check-frequency').val(chat_metadata['objective'].checkFrequency)
$('#objective-hide-tasks').prop('checked', chat_metadata['objective'].hideTasks)
$('#objective-tasks').prop('hidden', $('#objective-hide-tasks').prop('checked'))
setCurrentTask()
setCurrentTask(null, true)
}
function addManualTaskCheckUi() {

View File

@@ -818,7 +818,7 @@ function addSDGenButtons() {
$(document).on('click touchend', function (e) {
const target = $(e.target);
if (target.is(dropdown)) return;
if (target.is(button) && !dropdown.is(":visible") && $("#send_but").css('display') === 'flex') {
if (target.is(button) && !dropdown.is(":visible") && $("#send_but").is(":visible")) {
e.preventDefault();
dropdown.fadeIn(250);

View File

@@ -1,3 +1,5 @@
import { deepClone } from "../../utils.js";
export { ElevenLabsTtsProvider }
class ElevenLabsTtsProvider {
@@ -47,18 +49,20 @@ class ElevenLabsTtsProvider {
loadSettings(settings) {
// Pupulate Provider UI given input settings
if (Object.keys(settings).length == 0) {
if (!settings || Object.keys(settings).length == 0) {
console.info("Using default TTS Provider settings")
}
// Only accept keys defined in defaultSettings
this.settings = this.defaultSettings
this.settings = deepClone(this.defaultSettings);
for (const key in settings){
if (key in this.settings){
this.settings[key] = settings[key]
} else {
throw `Invalid setting passed to TTS Provider: ${key}`
if (settings) {
for (const key in settings) {
if (key in this.settings) {
this.settings[key] = settings[key]
} else {
throw `Invalid setting passed to TTS Provider: ${key}`
}
}
}

View File

@@ -63,6 +63,7 @@ import {
setScenarioOverride,
getCropPopup,
system_avatar,
isChatSaving,
} from "../script.js";
import { appendTagToList, createTagMapFromList, getTagsList, applyTagsOnCharacterSelect, tag_map, printTagFilters } from './tags.js';
import { FILTER_TYPES, FilterHelper } from './filters.js';
@@ -1272,6 +1273,11 @@ function updateFavButtonState(state) {
}
export async function openGroupById(groupId) {
if (isChatSaving) {
toastr.info("Please wait until the chat is saved before switching characters.", "Your chat is still saving...");
return;
}
if (!groups.find(x => x.id === groupId)) {
console.log('Group not found', groupId);
return;

View File

@@ -2,7 +2,10 @@
import { saveSettingsDebounced, substituteParams } from "../script.js";
import { selected_group } from "./group-chats.js";
import { power_user } from "./power-user.js";
import {
power_user,
context_presets,
} from "./power-user.js";
/**
* @type {any[]} Instruct mode presets.
@@ -69,6 +72,48 @@ function highlightDefaultPreset() {
$('#instruct_set_default').toggleClass('default', power_user.default_instruct === power_user.instruct.preset);
}
/**
* Select context template if not already selected.
* @param {string} preset Preset name.
*/
function selectContextPreset(preset) {
// If context template is not already selected, select it
if (preset !== power_user.context.preset) {
$('#context_presets').val(preset).trigger('change');
toastr.info(`Context Template: preset "${preset}" auto-selected`);
}
// If instruct mode is disabled, enable it, except for default context template
if (!power_user.instruct.enabled && preset !== 'Default') {
power_user.instruct.enabled = true;
$('#instruct_enabled').prop('checked', true).trigger('change');
toastr.info(`Instruct Mode enabled`);
}
saveSettingsDebounced();
}
/**
* Select instruct preset if not already selected.
* @param {string} preset Preset name.
*/
export function selectInstructPreset(preset) {
// If instruct preset is not already selected, select it
if (preset !== power_user.instruct.preset) {
$('#instruct_presets').val(preset).trigger('change');
toastr.info(`Instruct Mode: preset "${preset}" auto-selected`);
}
// If instruct mode is disabled, enable it
if (!power_user.instruct.enabled) {
power_user.instruct.enabled = true;
$('#instruct_enabled').prop('checked', true).trigger('change');
toastr.info(`Instruct Mode enabled`);
}
saveSettingsDebounced();
}
/**
* Automatically select instruct preset based on model id.
* Otherwise, if default instruct preset is set, selects it.
@@ -81,33 +126,42 @@ export function autoSelectInstructPreset(modelId) {
return false;
}
for (const preset of instruct_presets) {
// If activation regex is set, check if it matches the model id
if (preset.activation_regex) {
try {
const regex = new RegExp(preset.activation_regex, 'i');
// Select matching instruct preset
let foundMatch = false;
for (const instruct_preset of instruct_presets) {
// If instruct preset matches the context template
if (instruct_preset.name === power_user.context.preset) {
foundMatch = true;
selectInstructPreset(instruct_preset.name);
break;
}
}
// If no match was found, auto-select instruct preset
if (!foundMatch) {
for (const preset of instruct_presets) {
// If activation regex is set, check if it matches the model id
if (preset.activation_regex) {
try {
const regex = new RegExp(preset.activation_regex, 'i');
// Stop on first match so it won't cycle back and forth between presets if multiple regexes match
if (regex.test(modelId)) {
// If preset is not already selected, select it
if (power_user.instruct.preset !== preset.name) {
$('#instruct_presets').val(preset.name).trigger('change');
toastr.info(`Instruct mode: preset "${preset.name}" auto-selected`);
// Stop on first match so it won't cycle back and forth between presets if multiple regexes match
if (regex.test(modelId)) {
selectInstructPreset(preset.name);
return true;
}
} catch {
// If regex is invalid, ignore it
console.warn(`Invalid instruct activation regex in preset "${preset.name}"`);
}
} catch {
// If regex is invalid, ignore it
console.warn(`Invalid instruct activation regex in preset "${preset.name}"`);
}
}
}
if (power_user.default_instruct && power_user.instruct.preset !== power_user.default_instruct) {
if (instruct_presets.some(p => p.name === power_user.default_instruct)) {
console.log(`Instruct mode: default preset "${power_user.default_instruct}" selected`);
$('#instruct_presets').val(power_user.default_instruct).trigger('change');
if (power_user.default_instruct && power_user.instruct.preset !== power_user.default_instruct) {
if (instruct_presets.some(p => p.name === power_user.default_instruct)) {
console.log(`Instruct mode: default preset "${power_user.default_instruct}" selected`);
$('#instruct_presets').val(power_user.default_instruct).trigger('change');
}
}
}
@@ -165,9 +219,10 @@ export function getInstructStoppingSequences() {
* @param {string} forceAvatar Force avatar string.
* @param {string} name1 User name.
* @param {string} name2 Character name.
* @param {boolean} forceLastOutputSequence Force to use last outline sequence (if configured).
* @returns {string} Formatted instruct mode chat message.
*/
export function formatInstructModeChat(name, mes, isUser, isNarrator, forceAvatar, name1, name2) {
export function formatInstructModeChat(name, mes, isUser, isNarrator, forceAvatar, name1, name2, forceLastOutputSequence) {
let includeNames = isNarrator ? false : power_user.instruct.names;
if (!isNarrator && power_user.instruct.names_force_groups && (selected_group || forceAvatar)) {
@@ -176,6 +231,10 @@ export function formatInstructModeChat(name, mes, isUser, isNarrator, forceAvata
let sequence = (isUser || isNarrator) ? power_user.instruct.input_sequence : power_user.instruct.output_sequence;
if (sequence === power_user.instruct.output_sequence && forceLastOutputSequence && power_user.instruct.last_output_sequence) {
sequence = power_user.instruct.last_output_sequence;
}
if (power_user.instruct.macro) {
sequence = substituteParams(sequence, name1, name2);
}
@@ -273,6 +332,16 @@ jQuery(() => {
saveSettingsDebounced();
});
$('#instruct_enabled').on('change', function () {
// When instruct mode gets enabled, select context template matching selected instruct preset
if (power_user.instruct.enabled) {
$('#instruct_presets').trigger('change');
// When instruct mode gets disabled, select default context preset
} else {
selectContextPreset('Default');
}
});
$('#instruct_presets').on('change', function () {
const name = $(this).find(':selected').val();
const preset = instruct_presets.find(x => x.name === name);
@@ -295,6 +364,21 @@ jQuery(() => {
}
});
// Select matching context template
let foundMatch = false;
for (const context_preset of context_presets) {
// If context template matches the instruct preset
if (context_preset.name === name) {
foundMatch = true;
selectContextPreset(context_preset.name);
break;
}
}
if (!foundMatch) {
// If no match was found, select default context preset
selectContextPreset('Default');
}
highlightDefaultPreset();
});
});

View File

@@ -339,7 +339,7 @@ function setupChatCompletionPromptManager(openAiSettings) {
},
promptOrder: {
strategy: 'global',
dummyId: 100000
dummyId: 100001
},
};
@@ -577,6 +577,7 @@ function populateChatCompletion(prompts, chatCompletion, { bias, quietPrompt, ty
addToChatCompletion('charDescription');
addToChatCompletion('charPersonality');
addToChatCompletion('scenario');
addToChatCompletion('personaDescription')
// Collection of control prompts that will always be positioned last
const controlPrompts = new MessageCollection('controlPrompts');
@@ -626,34 +627,6 @@ function populateChatCompletion(prompts, chatCompletion, { bias, quietPrompt, ty
if (true === afterScenario) chatCompletion.insert(authorsNote, 'scenario');
}
// Persona Description
if (power_user.persona_description) {
const personaDescription = Message.fromPrompt(prompts.get('personaDescription'));
try {
switch (power_user.persona_description_position) {
case persona_description_positions.BEFORE_CHAR:
chatCompletion.insertAtStart(personaDescription, 'charDescription');
break;
case persona_description_positions.AFTER_CHAR:
chatCompletion.insertAtEnd(personaDescription, 'charDescription');
break;
case persona_description_positions.TOP_AN:
chatCompletion.insertAtStart(personaDescription, 'authorsNote');
break;
case persona_description_positions.BOTTOM_AN:
chatCompletion.insertAtEnd(personaDescription, 'authorsNote');
break;
}
} catch (error) {
if (error instanceof IdentifierNotFoundError) {
// Error is acceptable in this context
} else {
throw error;
}
}
}
// Decide whether dialogue examples should always be added
if (power_user.pin_examples) {
populateDialogueExamples(prompts, chatCompletion);
@@ -679,10 +652,12 @@ function populateChatCompletion(prompts, chatCompletion, { bias, quietPrompt, ty
* @param {string} quietPrompt - The quiet prompt to be used in the conversation.
* @param {string} bias - The bias to be added in the conversation.
* @param {Object} extensionPrompts - An object containing additional prompts.
*
* @param {string} systemPromptOverride
* @param {string} jailbreakPromptOverride
* @param {string} personaDescription
* @returns {Object} prompts - The prepared and merged system and user-defined prompts.
*/
function preparePromptsForChatCompletion(Scenario, charPersonality, name2, worldInfoBefore, worldInfoAfter, charDescription, quietPrompt, bias, extensionPrompts, systemPromptOverride, jailbreakPromptOverride) {
function preparePromptsForChatCompletion({Scenario, charPersonality, name2, worldInfoBefore, worldInfoAfter, charDescription, quietPrompt, bias, extensionPrompts, systemPromptOverride, jailbreakPromptOverride, personaDescription} = {}) {
const scenarioText = Scenario ? `[Circumstances and context of the dialogue: ${Scenario}]` : '';
const charPersonalityText = charPersonality ? `[${name2}'s personality: ${charPersonality}]` : ''
const groupNudge = `[Write the next reply only as ${name2}]`;
@@ -695,6 +670,7 @@ function preparePromptsForChatCompletion(Scenario, charPersonality, name2, world
{ role: 'system', content: charDescription, identifier: 'charDescription' },
{ role: 'system', content: charPersonalityText, identifier: 'charPersonality' },
{ role: 'system', content: scenarioText, identifier: 'scenario' },
{ role: 'system', content: personaDescription, identifier: 'personaDescription' },
// Unordered prompts without marker
{ role: 'system', content: oai_settings.nsfw_avoidance_prompt, identifier: 'nsfwAvoidance' },
{ role: 'system', content: oai_settings.impersonation_prompt, identifier: 'impersonate' },
@@ -793,6 +769,7 @@ function prepareOpenAIMessages({
cyclePrompt,
systemPromptOverride,
jailbreakPromptOverride,
personaDescription
} = {}, dryRun) {
// Without a character selected, there is no way to accurately calculate tokens
if (!promptManager.activeCharacter && dryRun) return [null, false];
@@ -805,7 +782,19 @@ function prepareOpenAIMessages({
try {
// Merge markers and ordered user prompts with system prompts
const prompts = preparePromptsForChatCompletion(Scenario, charPersonality, name2, worldInfoBefore, worldInfoAfter, charDescription, quietPrompt, bias, extensionPrompts, systemPromptOverride, jailbreakPromptOverride);
const prompts = preparePromptsForChatCompletion({
Scenario,
charPersonality,
name2,
worldInfoBefore,
worldInfoAfter,
charDescription,
quietPrompt,
bias,
extensionPrompts,
systemPromptOverride,
jailbreakPromptOverride,
personaDescription});
// Fill the chat completion with as much context as the budget allows
populateChatCompletion(prompts, chatCompletion, { bias, quietPrompt, type, cyclePrompt });
@@ -2548,7 +2537,7 @@ function getMaxContextWindowAI(value) {
async function onModelChange() {
biasCache = undefined;
let value = String($(this).val());
let value = String($(this).val() || '');
if ($(this).is('#model_claude_select')) {
console.log('Claude model changed to', value);

View File

@@ -20,7 +20,11 @@ import {
groups,
resetSelectedGroup,
} from "./group-chats.js";
import { loadInstructMode } from "./instruct-mode.js";
import {
instruct_presets,
loadInstructMode,
selectInstructPreset,
} from "./instruct-mode.js";
import { registerSlashCommand } from "./slash-commands.js";
import { tokenizers } from "./tokenizers.js";
@@ -147,7 +151,7 @@ let power_user = {
max_context_unlocked: false,
prefer_character_prompt: true,
prefer_character_jailbreak: true,
continue_on_send: false,
quick_continue: false,
trim_spaces: true,
relaxed_api_urls: false,
@@ -193,7 +197,7 @@ let power_user = {
let themes = [];
let movingUIPresets = [];
let context_presets = [];
export let context_presets = [];
const storage_keys = {
fast_ui_mode: "TavernAI_fast_ui_mode",
@@ -704,7 +708,8 @@ function loadPowerUserSettings(settings, data) {
$('#relaxed_api_urls').prop("checked", power_user.relaxed_api_urls);
$('#trim_spaces').prop("checked", power_user.trim_spaces);
$('#continue_on_send').prop("checked", power_user.continue_on_send);
$('#quick_continue').prop("checked", power_user.quick_continue);
$('#mes_continue').css('display', power_user.quick_continue ? '' : 'none');
$('#auto_swipe').prop("checked", power_user.auto_swipe);
$('#auto_swipe_minimum_length').val(power_user.auto_swipe_minimum_length);
$('#auto_swipe_blacklist').val(power_user.auto_swipe_blacklist.join(", "));
@@ -920,6 +925,15 @@ function loadContextSettings() {
}
}
});
// Select matching instruct preset
for (const instruct_preset of instruct_presets) {
// If instruct preset matches the context template
if (instruct_preset.name === name) {
selectInstructPreset(instruct_preset.name);
break;
}
}
});
}
@@ -1954,9 +1968,10 @@ $(document).ready(() => {
saveSettingsDebounced();
});
$("#continue_on_send").on("input", function () {
$("#quick_continue").on("input", function () {
const value = !!$(this).prop('checked');
power_user.continue_on_send = value;
power_user.quick_continue = value;
$("#mes_continue").css('display', value ? '' : 'none');
saveSettingsDebounced();
});

View File

@@ -1156,8 +1156,8 @@ async function checkWorldInfo(chat, maxContext) {
}
});
const worldInfoBefore = WIBeforeEntries.length ? `${WIBeforeEntries.join("\n")}\n` : '';
const worldInfoAfter = WIAfterEntries.length ? `${WIAfterEntries.join("\n")}\n` : '';
const worldInfoBefore = WIBeforeEntries.length ? WIBeforeEntries.join("\n") : '';
const worldInfoAfter = WIAfterEntries.length ? WIAfterEntries.join("\n") : '';
if (shouldWIAddPrompt) {
const originalAN = context.extensionPrompts[NOTE_MODULE_NAME].value;