mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Merge branch 'staging' into neo-server
This commit is contained in:
103
public/script.js
103
public/script.js
@ -153,7 +153,7 @@ import {
|
||||
ensureImageFormatSupported,
|
||||
} from './scripts/utils.js';
|
||||
|
||||
import { ModuleWorkerWrapper, doDailyExtensionUpdatesCheck, extension_settings, getContext, loadExtensionSettings, renderExtensionTemplate, runGenerationInterceptors, saveMetadataDebounced, writeExtensionField } from './scripts/extensions.js';
|
||||
import { ModuleWorkerWrapper, doDailyExtensionUpdatesCheck, extension_settings, getContext, loadExtensionSettings, renderExtensionTemplate, renderExtensionTemplateAsync, runGenerationInterceptors, saveMetadataDebounced, writeExtensionField } from './scripts/extensions.js';
|
||||
import { COMMENT_NAME_DEFAULT, executeSlashCommands, getSlashCommandsHelp, processChatSlashCommands, registerSlashCommand } from './scripts/slash-commands.js';
|
||||
import {
|
||||
tag_map,
|
||||
@ -213,6 +213,7 @@ import { initPresetManager } from './scripts/preset-manager.js';
|
||||
import { evaluateMacros } from './scripts/macros.js';
|
||||
import { currentUser, setUserControls } from './scripts/user.js';
|
||||
import { callGenericPopup } from './scripts/popup.js';
|
||||
import { renderTemplate, renderTemplateAsync } from './scripts/templates.js';
|
||||
|
||||
//exporting functions and vars for mods
|
||||
export {
|
||||
@ -287,6 +288,7 @@ export {
|
||||
printCharactersDebounced,
|
||||
isOdd,
|
||||
countOccurrences,
|
||||
renderTemplate,
|
||||
};
|
||||
|
||||
showLoader();
|
||||
@ -576,14 +578,14 @@ export const MAX_INJECTION_DEPTH = 1000;
|
||||
|
||||
let system_messages = {};
|
||||
|
||||
function getSystemMessages() {
|
||||
async function getSystemMessages() {
|
||||
system_messages = {
|
||||
help: {
|
||||
name: systemUserName,
|
||||
force_avatar: system_avatar,
|
||||
is_user: false,
|
||||
is_system: true,
|
||||
mes: renderTemplate('help'),
|
||||
mes: await renderTemplateAsync('help'),
|
||||
},
|
||||
slash_commands: {
|
||||
name: systemUserName,
|
||||
@ -597,21 +599,21 @@ function getSystemMessages() {
|
||||
force_avatar: system_avatar,
|
||||
is_user: false,
|
||||
is_system: true,
|
||||
mes: renderTemplate('hotkeys'),
|
||||
mes: await renderTemplateAsync('hotkeys'),
|
||||
},
|
||||
formatting: {
|
||||
name: systemUserName,
|
||||
force_avatar: system_avatar,
|
||||
is_user: false,
|
||||
is_system: true,
|
||||
mes: renderTemplate('formatting'),
|
||||
mes: await renderTemplateAsync('formatting'),
|
||||
},
|
||||
macros: {
|
||||
name: systemUserName,
|
||||
force_avatar: system_avatar,
|
||||
is_user: false,
|
||||
is_system: true,
|
||||
mes: renderTemplate('macros'),
|
||||
mes: await renderTemplateAsync('macros'),
|
||||
},
|
||||
welcome:
|
||||
{
|
||||
@ -619,7 +621,7 @@ function getSystemMessages() {
|
||||
force_avatar: system_avatar,
|
||||
is_user: false,
|
||||
is_system: true,
|
||||
mes: renderTemplate('welcome'),
|
||||
mes: await renderTemplateAsync('welcome'),
|
||||
},
|
||||
group: {
|
||||
name: systemUserName,
|
||||
@ -673,52 +675,6 @@ $(document).ajaxError(function myErrorHandler(_, xhr) {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Loads a URL content using XMLHttpRequest synchronously.
|
||||
* @param {string} url URL to load synchronously
|
||||
* @returns {string} Response text
|
||||
*/
|
||||
function getUrlSync(url) {
|
||||
console.debug('Loading URL synchronously', url);
|
||||
const request = new XMLHttpRequest();
|
||||
request.open('GET', url, false); // `false` makes the request synchronous
|
||||
request.send();
|
||||
|
||||
if (request.status >= 200 && request.status < 300) {
|
||||
return request.responseText;
|
||||
}
|
||||
|
||||
throw new Error(`Error loading ${url}: ${request.status} ${request.statusText}`);
|
||||
}
|
||||
|
||||
const templateCache = new Map();
|
||||
|
||||
export function renderTemplate(templateId, templateData = {}, sanitize = true, localize = true, fullPath = false) {
|
||||
try {
|
||||
const pathToTemplate = fullPath ? templateId : `/scripts/templates/${templateId}.html`;
|
||||
let template = templateCache.get(pathToTemplate);
|
||||
if (!template) {
|
||||
const templateContent = getUrlSync(pathToTemplate);
|
||||
template = Handlebars.compile(templateContent);
|
||||
templateCache.set(pathToTemplate, template);
|
||||
}
|
||||
let result = template(templateData);
|
||||
|
||||
if (sanitize) {
|
||||
result = DOMPurify.sanitize(result);
|
||||
}
|
||||
|
||||
if (localize) {
|
||||
result = applyLocale(result);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (err) {
|
||||
console.error('Error rendering template', templateId, templateData, err);
|
||||
toastr.error('Check the DevTools console for more information.', 'Error rendering template');
|
||||
}
|
||||
}
|
||||
|
||||
async function getClientVersion() {
|
||||
try {
|
||||
const response = await fetch('/version');
|
||||
@ -782,7 +738,7 @@ function getCurrentChatId() {
|
||||
if (selected_group) {
|
||||
return groups.find(x => x.id == selected_group)?.chat_id;
|
||||
}
|
||||
else if (this_chid) {
|
||||
else if (this_chid !== undefined) {
|
||||
return characters[this_chid]?.chat;
|
||||
}
|
||||
}
|
||||
@ -902,7 +858,7 @@ async function firstLoadInit() {
|
||||
await getClientVersion();
|
||||
await readSecretState();
|
||||
await getSettings();
|
||||
getSystemMessages();
|
||||
await getSystemMessages();
|
||||
sendSystemMessage(system_message_types.WELCOME);
|
||||
initLocales();
|
||||
initTags();
|
||||
@ -1205,7 +1161,7 @@ export function resultCheckStatus() {
|
||||
}
|
||||
|
||||
export async function selectCharacterById(id) {
|
||||
if (characters[id] == undefined) {
|
||||
if (characters[id] === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -1566,7 +1522,7 @@ async function getCharacters() {
|
||||
|
||||
characters[i]['chat'] = String(characters[i]['chat']);
|
||||
}
|
||||
if (this_chid != undefined && this_chid != 'invalid-safety-id') {
|
||||
if (this_chid !== undefined) {
|
||||
$('#avatar_url_pole').val(characters[this_chid].avatar);
|
||||
}
|
||||
|
||||
@ -1719,11 +1675,12 @@ export async function reloadCurrentChat() {
|
||||
if (selected_group) {
|
||||
await getGroupChat(selected_group, true);
|
||||
}
|
||||
else if (this_chid) {
|
||||
else if (this_chid !== undefined) {
|
||||
await getChat();
|
||||
}
|
||||
else {
|
||||
resetChatState();
|
||||
await getCharacters();
|
||||
await printMessages();
|
||||
await eventSource.emit(event_types.CHAT_CHANGED, getCurrentChatId());
|
||||
}
|
||||
@ -1826,7 +1783,7 @@ function messageFormatting(mes, ch_name, isSystem, isUser, messageId) {
|
||||
mes = mes.replaceAll('<', '<').replaceAll('>', '>');
|
||||
}
|
||||
|
||||
if ((this_chid === undefined || this_chid === 'invalid-safety-id') && !selected_group) {
|
||||
if (this_chid === undefined && !selected_group) {
|
||||
mes = mes
|
||||
.replace(/\*\*(.+?)\*\*/g, '<b>$1</b>')
|
||||
.replace(/\n/g, '<br/>');
|
||||
@ -2083,7 +2040,7 @@ function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll = true
|
||||
if (!mes['is_user']) {
|
||||
if (mes.force_avatar) {
|
||||
avatarImg = mes.force_avatar;
|
||||
} else if (this_chid === undefined || this_chid === 'invalid-safety-id') {
|
||||
} else if (this_chid === undefined) {
|
||||
avatarImg = system_avatar;
|
||||
} else {
|
||||
if (characters[this_chid].avatar != 'none') {
|
||||
@ -3176,12 +3133,12 @@ async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, qu
|
||||
quiet_prompt = main_api == 'novel' && !quietToLoud ? adjustNovelInstructionPrompt(quiet_prompt) : quiet_prompt;
|
||||
}
|
||||
|
||||
const isChatValid = online_status != 'no_connection' && this_chid != undefined && this_chid !== 'invalid-safety-id';
|
||||
const isChatValid = online_status !== 'no_connection' && this_chid !== undefined;
|
||||
|
||||
// We can't do anything because we're not in a chat right now. (Unless it's a dry run, in which case we need to
|
||||
// assemble the prompt so we can count its tokens regardless of whether a chat is active.)
|
||||
if (!dryRun && !isChatValid) {
|
||||
if (this_chid === undefined || this_chid === 'invalid-safety-id') {
|
||||
if (this_chid === undefined) {
|
||||
toastr.warning('Сharacter is not selected');
|
||||
}
|
||||
is_send_press = false;
|
||||
@ -4613,7 +4570,7 @@ async function DupeChar() {
|
||||
}
|
||||
}
|
||||
|
||||
function promptItemize(itemizedPrompts, requestedMesId) {
|
||||
async function promptItemize(itemizedPrompts, requestedMesId) {
|
||||
console.log('PROMPT ITEMIZE ENTERED');
|
||||
var incomingMesId = Number(requestedMesId);
|
||||
console.debug(`looking for MesId ${incomingMesId}`);
|
||||
@ -4654,7 +4611,7 @@ function promptItemize(itemizedPrompts, requestedMesId) {
|
||||
chatInjects: getTokenCount(itemizedPrompts[thisPromptSet].chatInjects),
|
||||
};
|
||||
|
||||
if (params.chatInjects){
|
||||
if (params.chatInjects) {
|
||||
params.ActualChatHistoryTokens = params.ActualChatHistoryTokens - params.chatInjects;
|
||||
}
|
||||
|
||||
@ -4736,10 +4693,12 @@ function promptItemize(itemizedPrompts, requestedMesId) {
|
||||
}
|
||||
|
||||
if (params.this_main_api == 'openai') {
|
||||
callPopup(renderTemplate('itemizationChat', params), 'text');
|
||||
const template = await renderTemplateAsync('itemizationChat', params);
|
||||
callPopup(template, 'text');
|
||||
|
||||
} else {
|
||||
callPopup(renderTemplate('itemizationText', params), 'text');
|
||||
const template = await renderTemplateAsync('itemizationText', params);
|
||||
callPopup(template, 'text');
|
||||
}
|
||||
}
|
||||
|
||||
@ -5308,7 +5267,7 @@ export function deactivateSendButtons() {
|
||||
|
||||
function resetChatState() {
|
||||
//unsets expected chid before reloading (related to getCharacters/printCharacters from using old arrays)
|
||||
this_chid = 'invalid-safety-id';
|
||||
this_chid = undefined;
|
||||
// replaces deleted charcter name with system user since it will be displayed next.
|
||||
name2 = systemUserName;
|
||||
// sets up system user to tell user about having deleted a character
|
||||
@ -7693,7 +7652,7 @@ async function createOrEditCharacter(e) {
|
||||
|
||||
$('#create_button').attr('value', '✅');
|
||||
let oldSelectedChar = null;
|
||||
if (this_chid != undefined && this_chid != 'invalid-safety-id') {
|
||||
if (this_chid !== undefined) {
|
||||
oldSelectedChar = characters[this_chid].avatar;
|
||||
}
|
||||
|
||||
@ -7815,7 +7774,11 @@ window['SillyTavern'].getContext = function () {
|
||||
*/
|
||||
registerHelper: () => { },
|
||||
registedDebugFunction: registerDebugFunction,
|
||||
/**
|
||||
* @deprecated Use renderExtensionTemplateAsync instead.
|
||||
*/
|
||||
renderExtensionTemplate: renderExtensionTemplate,
|
||||
renderExtensionTemplateAsync: renderExtensionTemplateAsync,
|
||||
callPopup: callPopup,
|
||||
callGenericPopup: callGenericPopup,
|
||||
mainApi: main_api,
|
||||
@ -8440,7 +8403,7 @@ async function importCharacter(file, preserveFileName = false) {
|
||||
$('#character_search_bar').val('').trigger('input');
|
||||
|
||||
let oldSelectedChar = null;
|
||||
if (this_chid != undefined && this_chid != 'invalid-safety-id') {
|
||||
if (this_chid !== undefined) {
|
||||
oldSelectedChar = characters[this_chid].avatar;
|
||||
}
|
||||
|
||||
@ -8571,7 +8534,7 @@ export async function handleDeleteCharacter(popup_type, this_chid, delete_chats)
|
||||
export async function deleteCharacter(name, avatar, reloadCharacters = true) {
|
||||
await clearChat();
|
||||
$('#character_cross').click();
|
||||
this_chid = 'invalid-safety-id';
|
||||
this_chid = undefined;
|
||||
characters.length = 0;
|
||||
name2 = systemUserName;
|
||||
chat = [...safetychat];
|
||||
|
Reference in New Issue
Block a user