Update lore.js

This commit is contained in:
2025-10-25 17:58:05 +08:00
committed by GitHub
parent 5a26dd7d60
commit 0f9ac3e811

View File

@@ -270,7 +270,12 @@ export async function getPlotOptimizedWorldbookContent(context, apiSettings) {
const panel = $('#amily2_plot_optimization_panel');
let liveSettings = {};
if (panel.length > 0) {
// Check if the panel exists and its dynamic content (the entry list) has been populated.
// This helps prevent a race condition where we read from an empty, partially-rendered panel.
const isPanelReady = panel.length > 0 && panel.find('#amily2_opt_worldbook_entry_list_container input[type="checkbox"]').length > 0;
if (isPanelReady) {
// Panel is ready, so we can trust the live values from the UI.
liveSettings.worldbookEnabled = panel.find('#amily2_opt_worldbook_enabled').is(':checked');
liveSettings.worldbookSource = panel.find('input[name="amily2_opt_worldbook_source"]:checked').val() || 'character';
@@ -284,25 +289,30 @@ export async function getPlotOptimizedWorldbookContent(context, apiSettings) {
liveSettings.worldbookCharLimit = parseInt(panel.find('#amily2_opt_worldbook_char_limit').val(), 10) || 60000;
let enabledEntries = {};
panel.find('#amily2_opt_worldbook_entry_list_container input[type="checkbox"]').each(function() {
if ($(this).is(':checked')) {
const bookName = $(this).data('book');
const uid = parseInt($(this).data('uid'));
if (!enabledEntries[bookName]) {
enabledEntries[bookName] = [];
}
enabledEntries[bookName].push(uid);
panel.find('#amily2_opt_worldbook_entry_list_container input[type="checkbox"]:checked').each(function() {
const bookName = $(this).data('book');
const uid = parseInt($(this).data('uid'));
if (!enabledEntries[bookName]) {
enabledEntries[bookName] = [];
}
enabledEntries[bookName].push(uid);
});
liveSettings.enabledWorldbookEntries = enabledEntries;
} else {
console.warn('[剧情优化大师] 未找到设置面板,世界书功能将回退到使用已保存的设置。');
// Panel is not ready or doesn't exist. Fall back to the saved settings from the extension.
// This uses the correct, prefixed keys.
if (panel.length > 0) {
console.warn('[剧情优化大师] 检测到UI面板但内容未完全加载回退到使用已保存的设置。');
} else {
console.warn('[剧情优化大师] 未找到设置面板,世界书功能将使用已保存的设置。');
}
liveSettings = {
worldbookEnabled: apiSettings.worldbookEnabled,
worldbookSource: apiSettings.worldbookSource,
selectedWorldbooks: apiSettings.selectedWorldbooks,
worldbookCharLimit: apiSettings.worldbookCharLimit,
enabledWorldbookEntries: apiSettings.enabledWorldbookEntries,
worldbookEnabled: apiSettings.plotOpt_worldbook_enabled,
worldbookSource: apiSettings.plotOpt_worldbook_source || 'character', // Default to 'character'
selectedWorldbooks: apiSettings.plotOpt_worldbook_selected_worldbooks,
worldbookCharLimit: apiSettings.plotOpt_worldbook_char_limit,
enabledWorldbookEntries: apiSettings.plotOpt_worldbook_selected_entries,
};
}
@@ -344,7 +354,8 @@ export async function getPlotOptimizedWorldbookContent(context, apiSettings) {
const userEnabledEntries = allEntries.filter(entry => {
if (!entry.enabled) return false;
const bookConfig = enabledEntriesMap[entry.bookName];
return bookConfig ? bookConfig.includes(entry.uid) : false;
// 同时检查数字和字符串类型的UID以兼容从实时UI数字和已保存设置可能为字符串中读取的配置
return bookConfig ? (bookConfig.includes(entry.uid) || bookConfig.includes(String(entry.uid))) : false;
});
if (userEnabledEntries.length === 0) return '';
@@ -352,8 +363,8 @@ export async function getPlotOptimizedWorldbookContent(context, apiSettings) {
const chatHistory = context.chat.map(message => message.mes).join('\n').toLowerCase();
const getEntryKeywords = (entry) => [...new Set([...(entry.key || []), ...(entry.keys || [])])].map(k => k.toLowerCase());
const blueLightEntries = userEnabledEntries.filter(entry => entry.type === 'constant');
let pendingGreenLights = userEnabledEntries.filter(entry => entry.type !== 'constant');
const blueLightEntries = userEnabledEntries.filter(entry => entry.constant);
let pendingGreenLights = userEnabledEntries.filter(entry => !entry.constant);
const triggeredEntries = new Set([...blueLightEntries]);