mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2024-12-15 10:48:36 +01:00
5848ec498b
* Kobold Presets fixed * Help texts fixed * Scale API for connectAPISlash * Quick Reply checkboxes fixed * New Instruct Mode Presets * More date/time macros * ChatML context template and instruct prompt format * Mistral context template and instruct prompt format * Removed use_default_badwordsids from kobold presets * Renamed ChatML to Mistral-OpenOrca (ChatML) * Renamed Mistral-OpenOrca (removed ChatML) * Removed single_line from kobold presets * Removed obsolete use_stop_sequence setting * Ban EOS Token off by default * Split AI Resp. Conf. in global and preset-specific settings * Resolve conflicts * Fix title * Add translations for new help texts * Fix i18n.json whitespace * Make Mistral-OpenOrca system prompt more generic * Renamed "Mistral-OpenOrca" to "ChatML" again * More (UI) fixes and improvements * Sendas hint fixed --------- Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>
179 lines
6.5 KiB
JavaScript
179 lines
6.5 KiB
JavaScript
import { eventSource, event_types, generateQuietPrompt } from "../../../script.js";
|
||
import { getContext, saveMetadataDebounced } from "../../extensions.js";
|
||
import { registerSlashCommand } from "../../slash-commands.js";
|
||
import { stringFormat } from "../../utils.js";
|
||
export { MODULE_NAME };
|
||
|
||
const MODULE_NAME = 'backgrounds';
|
||
const METADATA_KEY = 'custom_background';
|
||
|
||
/**
|
||
* @param {string} background
|
||
*/
|
||
function forceSetBackground(background) {
|
||
saveBackgroundMetadata(background);
|
||
setCustomBackground();
|
||
}
|
||
|
||
async function moduleWorker() {
|
||
if (hasCustomBackground()) {
|
||
$('#unlock_background').show();
|
||
$('#lock_background').hide();
|
||
setCustomBackground();
|
||
}
|
||
else {
|
||
$('#unlock_background').hide();
|
||
$('#lock_background').show();
|
||
unsetCustomBackground();
|
||
}
|
||
}
|
||
|
||
function onLockBackgroundClick() {
|
||
const bgImage = window.getComputedStyle(document.getElementById('bg1')).backgroundImage;
|
||
|
||
// Extract the URL from the CSS string
|
||
const urlRegex = /url\((['"])?(.*?)\1\)/;
|
||
const matches = bgImage.match(urlRegex);
|
||
const url = matches[2];
|
||
|
||
// Remove the protocol and host, leaving the relative URL
|
||
const relativeUrl = new URL(url).pathname;
|
||
const relativeBgImage = `url("${relativeUrl}")`
|
||
|
||
saveBackgroundMetadata(relativeBgImage);
|
||
setCustomBackground();
|
||
$('#unlock_background').show();
|
||
$('#lock_background').hide();
|
||
}
|
||
|
||
function onUnlockBackgroundClick() {
|
||
removeBackgroundMetadata();
|
||
unsetCustomBackground();
|
||
$('#unlock_background').hide();
|
||
$('#lock_background').show();
|
||
}
|
||
|
||
function hasCustomBackground() {
|
||
const context = getContext();
|
||
return !!context.chatMetadata[METADATA_KEY];
|
||
}
|
||
|
||
function saveBackgroundMetadata(file) {
|
||
const context = getContext();
|
||
context.chatMetadata[METADATA_KEY] = file;
|
||
saveMetadataDebounced();
|
||
}
|
||
|
||
function removeBackgroundMetadata() {
|
||
const context = getContext();
|
||
delete context.chatMetadata[METADATA_KEY];
|
||
saveMetadataDebounced();
|
||
}
|
||
|
||
function setCustomBackground() {
|
||
const context = getContext();
|
||
const file = context.chatMetadata[METADATA_KEY];
|
||
|
||
// bg already set
|
||
if (document.getElementById("bg_custom").style.backgroundImage == file) {
|
||
return;
|
||
}
|
||
|
||
$("#bg_custom").css("background-image", file);
|
||
$("#custom_bg_preview").css("background-image", file);
|
||
}
|
||
|
||
function unsetCustomBackground() {
|
||
$("#bg_custom").css("background-image", 'none');
|
||
$("#custom_bg_preview").css("background-image", 'none');
|
||
}
|
||
|
||
function onSelectBackgroundClick() {
|
||
const bgfile = $(this).attr("bgfile");
|
||
|
||
if (hasCustomBackground()) {
|
||
saveBackgroundMetadata(`url("backgrounds/${bgfile}")`);
|
||
setCustomBackground();
|
||
}
|
||
}
|
||
|
||
const autoBgPrompt = `Pause your roleplay and choose a location ONLY from the provided list that is the most suitable for the current scene. Do not output any other text:\n{0}`;
|
||
|
||
async function autoBackgroundCommand() {
|
||
const options = Array.from(document.querySelectorAll('.BGSampleTitle')).map(x => ({ element: x, text: x.innerText.trim() })).filter(x => x.text.length > 0);
|
||
if (options.length == 0) {
|
||
toastr.warning('No backgrounds to choose from. Please upload some images to the "backgrounds" folder.');
|
||
return;
|
||
}
|
||
|
||
const list = options.map(option => `- ${option.text}`).join('\n');
|
||
const prompt = stringFormat(autoBgPrompt, list);
|
||
const reply = await generateQuietPrompt(prompt);
|
||
const fuse = new Fuse(options, { keys: ['text'] });
|
||
const bestMatch = fuse.search(reply, { limit: 1 });
|
||
|
||
if (bestMatch.length == 0) {
|
||
toastr.warning('No match found. Please try again.');
|
||
return;
|
||
}
|
||
|
||
console.debug('Automatically choosing background:', bestMatch);
|
||
bestMatch[0].item.element.click();
|
||
}
|
||
|
||
$(document).ready(function () {
|
||
function addSettings() {
|
||
const html = `
|
||
<div class="background_settings">
|
||
<div class="inline-drawer">
|
||
<div class="inline-drawer-toggle inline-drawer-header">
|
||
<b>Chat Backgrounds</b>
|
||
<div class="inline-drawer-icon fa-solid fa-circle-chevron-down down"></div>
|
||
</div>
|
||
<div class="inline-drawer-content">
|
||
<div class="background_controls">
|
||
<div id="lock_background" class="menu_button">
|
||
<i class="fa-solid fa-lock"></i>
|
||
Lock
|
||
</div>
|
||
<div id="unlock_background" class="menu_button">
|
||
<i class="fa-solid fa-unlock"></i>
|
||
Unlock
|
||
</div>
|
||
<small>
|
||
Press "Lock" to assign a currently selected background to a character or group chat.<br>
|
||
Any background image selected while lock is engaged will be saved automatically.
|
||
</small>
|
||
</div>
|
||
<div class="background_controls">
|
||
<div id="auto_background" class="menu_button">
|
||
<i class="fa-solid fa-wand-magic"></i>
|
||
Auto
|
||
</div>
|
||
<small>
|
||
Automatically select a background based on the chat context.<br>
|
||
Respects the "Lock" setting state.
|
||
</small>
|
||
</div>
|
||
<div>Preview</div>
|
||
<div id="custom_bg_preview">
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
`;
|
||
$('#extensions_settings').append(html);
|
||
$('#lock_background').on('click', onLockBackgroundClick);
|
||
$('#unlock_background').on('click', onUnlockBackgroundClick);
|
||
$(document).on("click", ".bg_example", onSelectBackgroundClick);
|
||
$('#auto_background').on("click", autoBackgroundCommand);
|
||
}
|
||
|
||
addSettings();
|
||
registerSlashCommand('lockbg', onLockBackgroundClick, ['bglock'], "– locks a background for the currently selected chat", true, true);
|
||
registerSlashCommand('unlockbg', onUnlockBackgroundClick, ['bgunlock'], '– unlocks a background for the currently selected chat', true, true);
|
||
registerSlashCommand('autobg', autoBackgroundCommand, ['bgauto'], '– automatically changes the background based on the chat context using the AI request prompt', true, true);
|
||
eventSource.on(event_types.FORCE_SET_BACKGROUND, forceSetBackground);
|
||
eventSource.on(event_types.CHAT_CHANGED, moduleWorker);
|
||
});
|