Update lore.js

This commit is contained in:
2026-01-06 10:39:22 +08:00
committed by GitHub
parent 68365839c6
commit c1e0a0fa1e

View File

@@ -266,16 +266,25 @@ export async function getOptimizationWorldbookContent() {
} }
export async function getPlotOptimizedWorldbookContent(context, apiSettings) { export async function getPlotOptimizedWorldbookContent(context, apiSettings, isConcurrent = false) {
const panel = $('#amily2_plot_optimization_panel'); const panel = $('#amily2_plot_optimization_panel');
let liveSettings = {}; let liveSettings = {};
// 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; const isPanelReady = panel.length > 0 && panel.find('#amily2_opt_worldbook_entry_list_container input[type="checkbox"]').length > 0;
if (isPanelReady) { if (isConcurrent) {
// Panel is ready, so we can trust the live values from the UI. // This is a concurrent call, force use of passed apiSettings
console.log('[剧情优化大师] 检测到并发调用,强制使用传入的并发世界书设置。');
liveSettings = {
worldbookEnabled: apiSettings.plotOpt_worldbook_enabled,
worldbookSource: apiSettings.plotOpt_worldbook_source || 'character',
selectedWorldbooks: apiSettings.plotOpt_selectedWorldbooks || [],
autoSelectWorldbooks: apiSettings.plotOpt_autoSelectWorldbooks || [],
worldbookCharLimit: apiSettings.plotOpt_worldbookCharLimit,
enabledWorldbookEntries: null, // Let the logic below handle it based on selected books.
};
} else if (isPanelReady) {
// This is a main call and the panel is ready, read from UI.
liveSettings.worldbookEnabled = panel.find('#amily2_opt_worldbook_enabled').is(':checked'); liveSettings.worldbookEnabled = panel.find('#amily2_opt_worldbook_enabled').is(':checked');
liveSettings.worldbookSource = panel.find('input[name="amily2_opt_worldbook_source"]:checked').val() || 'character'; liveSettings.worldbookSource = panel.find('input[name="amily2_opt_worldbook_source"]:checked').val() || 'character';
@@ -304,8 +313,7 @@ export async function getPlotOptimizedWorldbookContent(context, apiSettings) {
}); });
liveSettings.enabledWorldbookEntries = enabledEntries; liveSettings.enabledWorldbookEntries = enabledEntries;
} else { } else {
// Panel is not ready or doesn't exist. Fall back to the saved settings from the extension. // Fallback for main call when panel is not ready.
// This uses the correct, prefixed keys.
if (panel.length > 0) { if (panel.length > 0) {
console.warn('[剧情优化大师] 检测到UI面板但内容未完全加载回退到使用已保存的设置。'); console.warn('[剧情优化大师] 检测到UI面板但内容未完全加载回退到使用已保存的设置。');
} else { } else {
@@ -313,12 +321,12 @@ export async function getPlotOptimizedWorldbookContent(context, apiSettings) {
} }
liveSettings = { liveSettings = {
worldbookEnabled: apiSettings.plotOpt_worldbook_enabled, worldbookEnabled: apiSettings.plotOpt_worldbookEnabled,
worldbookSource: apiSettings.plotOpt_worldbook_source || 'character', // Default to 'character' worldbookSource: apiSettings.plotOpt_worldbookSource || 'character',
selectedWorldbooks: apiSettings.plotOpt_worldbook_selected_worldbooks, selectedWorldbooks: apiSettings.plotOpt_selectedWorldbooks,
autoSelectWorldbooks: apiSettings.plotOpt_autoSelectWorldbooks || [], autoSelectWorldbooks: apiSettings.plotOpt_autoSelectWorldbooks || [],
worldbookCharLimit: apiSettings.plotOpt_worldbook_char_limit, worldbookCharLimit: apiSettings.plotOpt_worldbookCharLimit,
enabledWorldbookEntries: apiSettings.plotOpt_worldbook_selected_entries, enabledWorldbookEntries: apiSettings.plotOpt_enabledWorldbookEntries,
}; };
} }
@@ -356,24 +364,34 @@ export async function getPlotOptimizedWorldbookContent(context, apiSettings) {
if (allEntries.length === 0) return ''; if (allEntries.length === 0) return '';
const enabledEntriesMap = liveSettings.enabledWorldbookEntries || {}; const enabledEntriesMap = liveSettings.enabledWorldbookEntries; // Can be null for concurrent
const autoSelectedBooks = liveSettings.autoSelectWorldbooks || []; const autoSelectedBooks = liveSettings.autoSelectWorldbooks || [];
const userEnabledEntries = allEntries.filter(entry => { const userEnabledEntries = allEntries.filter(entry => {
if (!entry.enabled) return false; if (!entry.enabled) return false;
// 检查是否在UI中被勾选或被自动全选 // For concurrent calls where enabledWorldbookEntries is null, or for books marked as "auto-select",
// we consider all enabled entries within that book as selected.
const isAuto = autoSelectedBooks.includes(entry.bookName); const isAuto = autoSelectedBooks.includes(entry.bookName);
const bookConfig = enabledEntriesMap[entry.bookName]; if (isConcurrent || isAuto) {
const isChecked = isAuto || (bookConfig ? (bookConfig.includes(entry.uid) || bookConfig.includes(String(entry.uid))) : false); entry.constant = true; // Force as constant if auto-selected or concurrent
return true;
if (isChecked) { }
// 勾选状态下必读 (强制设为 Constant)
entry.constant = true; // For main calls with manual entry selection
if (enabledEntriesMap) {
const bookConfig = enabledEntriesMap[entry.bookName];
const isChecked = (bookConfig ? (bookConfig.includes(entry.uid) || bookConfig.includes(String(entry.uid))) : false);
if (isChecked) {
entry.constant = true; // Force as constant if checked in UI
}
// If not checked, it relies on its own constant/green-light status.
return true;
} }
// 不勾选则依靠蓝绿灯 (保持原样,不返回 false)
return true; // Default case if something goes wrong (should not be reached)
return false;
}); });
if (userEnabledEntries.length === 0) return ''; if (userEnabledEntries.length === 0) return '';