mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
#1226 Add chat-bound lorebooks
This commit is contained in:
@ -1110,6 +1110,7 @@ function select_group_chats(groupId, skipAnimation) {
|
||||
$("#rm_group_submit").hide();
|
||||
$("#rm_group_delete").show();
|
||||
$("#rm_group_scenario").show();
|
||||
$('#group-metadata-controls .chat_lorebook_button').removeClass('disabled').prop('disabled', false);
|
||||
} else {
|
||||
$("#rm_group_submit").show();
|
||||
if ($("#groupAddMemberListToggle .inline-drawer-content").css('display') !== 'block') {
|
||||
@ -1117,6 +1118,7 @@ function select_group_chats(groupId, skipAnimation) {
|
||||
}
|
||||
$("#rm_group_delete").hide();
|
||||
$("#rm_group_scenario").hide();
|
||||
$('#group-metadata-controls .chat_lorebook_button').addClass('disabled').prop('disabled', true);
|
||||
}
|
||||
|
||||
updateFavButtonState(group?.fav ?? false);
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { saveSettings, callPopup, substituteParams, getRequestHeaders, chat_metadata, this_chid, characters, saveCharacterDebounced, menu_type, eventSource, event_types, getExtensionPrompt, MAX_INJECTION_DEPTH, extension_prompt_types, getExtensionPromptByName } from "../script.js";
|
||||
import { saveSettings, callPopup, substituteParams, getRequestHeaders, chat_metadata, this_chid, characters, saveCharacterDebounced, menu_type, eventSource, event_types, getExtensionPrompt, MAX_INJECTION_DEPTH, extension_prompt_types, getExtensionPromptByName, saveMetadata, getCurrentChatId } from "../script.js";
|
||||
import { download, debounce, initScrollHeight, resetScrollHeight, parseJsonFile, extractDataFromPng, getFileBuffer, getCharaFilename, getSortableDelay, escapeRegex, PAGINATION_TEMPLATE, navigation_option, waitUntilCondition } from "./utils.js";
|
||||
import { extension_settings, getContext } from "./extensions.js";
|
||||
import { NOTE_MODULE_NAME, metadata_keys, shouldWIAddPrompt } from "./authors-note.js";
|
||||
@ -53,6 +53,7 @@ let updateEditor = (navigation) => { navigation; };
|
||||
// Do not optimize. updateEditor is a function that is updated by the displayWorldEntries with new data.
|
||||
const worldInfoFilter = new FilterHelper(() => updateEditor());
|
||||
const SORT_ORDER_KEY = 'world_info_sort_order';
|
||||
const METADATA_KEY = 'world_info';
|
||||
|
||||
const InputWidthReference = $("#WIInputWidthReference");
|
||||
|
||||
@ -167,6 +168,11 @@ function setWorldInfoSettings(settings, data) {
|
||||
|
||||
$('#world_info_sort_order').val(localStorage.getItem(SORT_ORDER_KEY) || '0');
|
||||
$("#world_editor_select").trigger("change");
|
||||
|
||||
eventSource.on(event_types.CHAT_CHANGED, () => {
|
||||
const hasWorldInfo = !!chat_metadata[METADATA_KEY] && world_names.includes(chat_metadata[METADATA_KEY]);
|
||||
$('.chat_lorebook_button').toggleClass('world_set', hasWorldInfo);
|
||||
});
|
||||
}
|
||||
|
||||
// World Info Editor
|
||||
@ -1301,10 +1307,26 @@ async function getGlobalLore() {
|
||||
return entries;
|
||||
}
|
||||
|
||||
async function getChatLore() {
|
||||
const chatWorld = chat_metadata[METADATA_KEY];
|
||||
|
||||
if (!chatWorld) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const data = await loadWorldInfoData(chatWorld);
|
||||
const entries = data ? Object.keys(data.entries).map((x) => data.entries[x]) : [];
|
||||
|
||||
console.debug(`Chat lore has ${entries.length} entries`);
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
async function getSortedEntries() {
|
||||
try {
|
||||
const globalLore = await getGlobalLore();
|
||||
const characterLore = await getCharacterLore();
|
||||
const chatLore = await getChatLore();
|
||||
|
||||
let entries;
|
||||
|
||||
@ -1327,6 +1349,9 @@ async function getSortedEntries() {
|
||||
break;
|
||||
}
|
||||
|
||||
// Chat lore always goes first
|
||||
entries = [...chatLore.sort(sortFn), ...entries];
|
||||
|
||||
console.debug(`Sorted ${entries.length} world lore entries using strategy ${world_info_character_strategy}`);
|
||||
|
||||
// Need to deep clone the entries to avoid modifying the cached data
|
||||
@ -1911,6 +1936,39 @@ export async function importWorldInfo(file) {
|
||||
});
|
||||
}
|
||||
|
||||
function assignLorebookToChat() {
|
||||
const selectedName = chat_metadata[METADATA_KEY];
|
||||
const template = $('#chat_world_template .chat_world').clone();
|
||||
|
||||
const worldSelect = template.find('select');
|
||||
const chatName = template.find('.chat_name');
|
||||
chatName.text(getCurrentChatId());
|
||||
|
||||
for (const worldName of world_names) {
|
||||
const option = document.createElement('option');
|
||||
option.value = worldName;
|
||||
option.innerText = worldName;
|
||||
option.selected = selectedName === worldName;
|
||||
worldSelect.append(option);
|
||||
}
|
||||
|
||||
worldSelect.on('change', function () {
|
||||
const worldName = $(this).val();
|
||||
|
||||
if (worldName) {
|
||||
chat_metadata[METADATA_KEY] = worldName;
|
||||
$('.chat_lorebook_button').addClass('world_set');
|
||||
} else {
|
||||
delete chat_metadata[METADATA_KEY];
|
||||
$('.chat_lorebook_button').removeClass('world_set');
|
||||
}
|
||||
|
||||
saveMetadata();
|
||||
});
|
||||
|
||||
callPopup(template, 'text');
|
||||
}
|
||||
|
||||
jQuery(() => {
|
||||
|
||||
$(document).ready(function () {
|
||||
@ -2051,6 +2109,8 @@ jQuery(() => {
|
||||
updateEditor(navigation_option.none);
|
||||
})
|
||||
|
||||
$(document).on('click', '.chat_lorebook_button', assignLorebookToChat);
|
||||
|
||||
// Not needed on mobile
|
||||
const deviceInfo = getDeviceInfo();
|
||||
if (deviceInfo && deviceInfo.device.type === 'desktop') {
|
||||
|
Reference in New Issue
Block a user