mirror of
https://github.com/SilenceLurker/ST-Amily2-Chat-Optimisation.git
synced 2026-06-06 20:35:51 +00:00
Update index.js
This commit is contained in:
112
index.js
112
index.js
@@ -1,9 +1,12 @@
|
|||||||
import { createDrawer } from "./ui/drawer.js";
|
import { createDrawer } from "./ui/drawer.js";
|
||||||
import "./MiZheSi/index.js";
|
import "./MiZheSi/index.js";
|
||||||
import "./PreOptimizationViewer/index.js";
|
import "./PreOptimizationViewer/index.js";
|
||||||
import { registerSlashCommands } from "./core/commands.js";
|
import { registerSlashCommands } from "./core/commands.js";
|
||||||
import { onMessageReceived, handleTableUpdate } from "./core/events.js";
|
import { onMessageReceived, handleTableUpdate } from "./core/events.js";
|
||||||
import { injectTableData } from "./core/table-system/injector.js";
|
import { processPlotOptimization } from "./core/summarizer.js";
|
||||||
|
import { getContext } from "/scripts/extensions.js";
|
||||||
|
import { characters, this_chid } from '/script.js';
|
||||||
|
import { injectTableData } from "./core/table-system/injector.js";
|
||||||
import { loadTables, clearHighlights } from './core/table-system/manager.js';
|
import { loadTables, clearHighlights } from './core/table-system/manager.js';
|
||||||
import { renderTables } from './ui/table-bindings.js';
|
import { renderTables } from './ui/table-bindings.js';
|
||||||
import { log } from './core/table-system/logger.js';
|
import { log } from './core/table-system/logger.js';
|
||||||
@@ -193,7 +196,7 @@ async function handleMessageBoard() {
|
|||||||
function loadPluginStyles() {
|
function loadPluginStyles() {
|
||||||
const loadStyleFile = (fileName) => {
|
const loadStyleFile = (fileName) => {
|
||||||
const styleId = `amily2-style-${fileName.split('.')[0]}`;
|
const styleId = `amily2-style-${fileName.split('.')[0]}`;
|
||||||
if (document.getElementById(styleId)) return;
|
if (document.getElementById(styleId)) return;
|
||||||
|
|
||||||
const extensionPath = `scripts/extensions/third-party/${extensionName}/assets/${fileName}?v=${Date.now()}`;
|
const extensionPath = `scripts/extensions/third-party/${extensionName}/assets/${fileName}?v=${Date.now()}`;
|
||||||
|
|
||||||
@@ -206,11 +209,12 @@ function loadPluginStyles() {
|
|||||||
console.log(`[Amily2号-皇家制衣局] 已为帝国披上华服: ${fileName}`);
|
console.log(`[Amily2号-皇家制衣局] 已为帝国披上华服: ${fileName}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
// 颁布四道制衣圣谕
|
// 颁布五道制衣圣谕
|
||||||
loadStyleFile("style.css"); // 【第一道圣谕】为帝国主体宫殿披上通用华服
|
loadStyleFile("style.css"); // 【第一道圣谕】为帝国主体宫殿披上通用华服
|
||||||
loadStyleFile("historiography.css"); // 【第二道圣谕】为敕史局披上其专属华服
|
loadStyleFile("historiography.css"); // 【第二道圣谕】为敕史局披上其专属华服
|
||||||
loadStyleFile("hanlinyuan.css"); // 【第三道圣谕】为翰林院披上其专属华服
|
loadStyleFile("hanlinyuan.css"); // 【第三道圣谕】为翰林院披上其专属华服
|
||||||
loadStyleFile("table.css"); // 【第四道圣谕】为内存储司披上其专属华服
|
loadStyleFile("table.css"); // 【第四道圣谕】为内存储司披上其专属华服
|
||||||
|
loadStyleFile("optimization.css"); // 【第五道圣谕】为剧情优化披上其专属华服
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -229,9 +233,7 @@ jQuery(async () => {
|
|||||||
if (!extension_settings[extensionName]) {
|
if (!extension_settings[extensionName]) {
|
||||||
extension_settings[extensionName] = {};
|
extension_settings[extensionName] = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
const combinedDefaultSettings = { ...defaultSettings, ...tableSystemDefaultSettings };
|
const combinedDefaultSettings = { ...defaultSettings, ...tableSystemDefaultSettings };
|
||||||
|
|
||||||
Object.assign(extension_settings[extensionName], {
|
Object.assign(extension_settings[extensionName], {
|
||||||
...combinedDefaultSettings,
|
...combinedDefaultSettings,
|
||||||
...extension_settings[extensionName],
|
...extension_settings[extensionName],
|
||||||
@@ -261,35 +263,112 @@ jQuery(async () => {
|
|||||||
|
|
||||||
|
|
||||||
console.log("[Amily2号-开国大典] 步骤四:部署帝国哨兵网络...");
|
console.log("[Amily2号-开国大典] 步骤四:部署帝国哨兵网络...");
|
||||||
if (!window.amily2EventsRegistered) {
|
|
||||||
|
|
||||||
|
let isProcessingPlotOptimization = false;
|
||||||
|
async function onPlotGenerationAfterCommands(type, params, dryRun) {
|
||||||
|
console.log("[Amily2-剧情优化] Generation after commands triggered", { type, params, dryRun, isProcessing: isProcessingPlotOptimization });
|
||||||
|
|
||||||
|
if (type === 'regenerate' || isProcessingPlotOptimization || dryRun) {
|
||||||
|
console.log("[Amily2-剧情优化] Skipping due to conditions:", { type, isProcessing: isProcessingPlotOptimization, dryRun });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const globalSettings = extension_settings[extensionName];
|
||||||
|
|
||||||
|
console.log("[Amily2-剧情优化] Checking plot optimization settings", {
|
||||||
|
enabled: globalSettings?.plotOpt_enabled,
|
||||||
|
hasApiUrl: !!globalSettings?.apiUrl,
|
||||||
|
plotSettings: globalSettings
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("[Amily2-剧情优化] Detailed settings check:", {
|
||||||
|
globalEnabled: globalSettings?.enabled,
|
||||||
|
plotSettingsExists: !!globalSettings,
|
||||||
|
plotSettingsEnabled: globalSettings?.plotOpt_enabled,
|
||||||
|
plotSettingsApiUrl: globalSettings?.apiUrl,
|
||||||
|
fullPlotSettings: globalSettings
|
||||||
|
});
|
||||||
|
|
||||||
|
const isPlotOptEnabled = globalSettings?.plotOpt_enabled !== false; // 如果未设置,默认启用
|
||||||
|
if (!isPlotOptEnabled || !globalSettings?.apiUrl) {
|
||||||
|
console.log("[Amily2-剧情优化] Plot optimization disabled or missing API URL", {
|
||||||
|
optimizationEnabled: globalSettings?.plotOpt_enabled,
|
||||||
|
apiUrl: globalSettings?.apiUrl
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
isProcessingPlotOptimization = true;
|
||||||
|
let $toast = toastr.info('正在进行剧情优化...', '剧情优化', { timeOut: 0, extendedTimeOut: 0 });
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log("[Amily2-剧情优化] Event parameters:", { type, params, dryRun });
|
||||||
|
const userMessage = $('#send_textarea').val();
|
||||||
|
|
||||||
|
if (!userMessage) {
|
||||||
|
console.log("[Amily2-剧情优化] No user message found in textarea");
|
||||||
|
if ($toast) toastr.clear($toast);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Amily2-剧情优化] Processing plot optimization for message:", userMessage);
|
||||||
|
|
||||||
|
const context = getContext();
|
||||||
|
const contextTurnCount = globalSettings.plotOpt_contextLimit || 10;
|
||||||
|
let slicedContext = [];
|
||||||
|
if (contextTurnCount > 0) {
|
||||||
|
slicedContext = context.chat.slice(-contextTurnCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await processPlotOptimization({ mes: userMessage }, slicedContext);
|
||||||
|
|
||||||
|
if (result && result.contentToAppend) {
|
||||||
|
const currentUserInput = $('#send_textarea').val();
|
||||||
|
const finalMessage = currentUserInput + '\n' + result.contentToAppend;
|
||||||
|
|
||||||
|
$('#send_textarea').val(finalMessage);
|
||||||
|
$('#send_textarea').trigger('input');
|
||||||
|
|
||||||
|
if ($toast) toastr.clear($toast);
|
||||||
|
toastr.success('剧情优化已完成并注入。', '操作成功');
|
||||||
|
console.log("[Amily2-剧情优化] Plot optimization completed and appended successfully.");
|
||||||
|
} else {
|
||||||
|
if ($toast) toastr.clear($toast);
|
||||||
|
console.log("[Amily2-剧情优化] Plot optimization returned no result to append.");
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[Amily2-剧情优化] 处理发送前事件时出错:`, error);
|
||||||
|
if ($toast) toastr.clear($toast);
|
||||||
|
toastr.error('剧情优化处理失败。', '错误');
|
||||||
|
} finally {
|
||||||
|
isProcessingPlotOptimization = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!window.amily2EventsRegistered) {
|
||||||
|
eventSource.on(event_types.GENERATION_AFTER_COMMANDS, onPlotGenerationAfterCommands);
|
||||||
|
|
||||||
eventSource.on(event_types.MESSAGE_RECEIVED, onMessageReceived);
|
eventSource.on(event_types.MESSAGE_RECEIVED, onMessageReceived);
|
||||||
eventSource.on(event_types.IMPERSONATE_READY, onMessageReceived);
|
eventSource.on(event_types.IMPERSONATE_READY, onMessageReceived);
|
||||||
|
|
||||||
|
|
||||||
eventSource.on(event_types.MESSAGE_RECEIVED, (chat_id) => handleTableUpdate(chat_id));
|
eventSource.on(event_types.MESSAGE_RECEIVED, (chat_id) => handleTableUpdate(chat_id));
|
||||||
eventSource.on(event_types.MESSAGE_SWIPED, (chat_id) => {
|
eventSource.on(event_types.MESSAGE_SWIPED, (chat_id) => {
|
||||||
|
|
||||||
clearHighlights();
|
clearHighlights();
|
||||||
handleTableUpdate(chat_id);
|
handleTableUpdate(chat_id);
|
||||||
});
|
});
|
||||||
eventSource.on(event_types.MESSAGE_EDITED, (mes_id) => handleTableUpdate(mes_id));
|
eventSource.on(event_types.MESSAGE_EDITED, (mes_id) => handleTableUpdate(mes_id));
|
||||||
|
|
||||||
eventSource.on(event_types.CHAT_CHANGED, () => {
|
eventSource.on(event_types.CHAT_CHANGED, () => {
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
log("【监察系统】检测到“朝代更迭”(CHAT_CHANGED),开始重修史书并刷新宫殿...", 'info');
|
log("【监察系统】检测到“朝代更迭”(CHAT_CHANGED),开始重修史书并刷新宫殿...", 'info');
|
||||||
|
|
||||||
clearHighlights();
|
clearHighlights();
|
||||||
loadTables();
|
loadTables();
|
||||||
renderTables();
|
renderTables();
|
||||||
}, 100);
|
}, 100);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
eventSource.on(event_types.MESSAGE_DELETED, (message, index) => {
|
eventSource.on(event_types.MESSAGE_DELETED, (message, index) => {
|
||||||
log(`【监察系统】检测到消息 ${index} 被删除,开始精确回滚UI状态。`, 'warn');
|
log(`【监察系统】检测到消息 ${index} 被删除,开始精确回滚UI状态。`, 'warn');
|
||||||
|
|
||||||
clearHighlights();
|
clearHighlights();
|
||||||
loadTables(index);
|
loadTables(index);
|
||||||
renderTables();
|
renderTables();
|
||||||
@@ -303,11 +382,8 @@ jQuery(async () => {
|
|||||||
const originalFunction = window[officialFunctionName];
|
const originalFunction = window[officialFunctionName];
|
||||||
|
|
||||||
if (typeof originalFunction === 'function') {
|
if (typeof originalFunction === 'function') {
|
||||||
|
|
||||||
window[officialFunctionName] = async function(...args) {
|
window[officialFunctionName] = async function(...args) {
|
||||||
|
|
||||||
injectTableData(...args);
|
injectTableData(...args);
|
||||||
|
|
||||||
await originalFunction.apply(this, args);
|
await originalFunction.apply(this, args);
|
||||||
};
|
};
|
||||||
console.log(`[Amily2-内存储司] 已成功代理 ${officialFunctionName},与翰林院协同工作。`);
|
console.log(`[Amily2-内存储司] 已成功代理 ${officialFunctionName},与翰林院协同工作。`);
|
||||||
@@ -337,7 +413,7 @@ jQuery(async () => {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
log(`【凤凰阁】内联主题系统初始化失败: ${error}`, 'error');
|
log(`【凤凰阁】内联主题系统初始化失败: ${error}`, 'error');
|
||||||
}
|
}
|
||||||
}, 500);
|
}, 500); // 延迟500毫秒,给予充分的渲染时间
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("!!!【开国大典失败】在执行系列法令时发生严重错误:", error);
|
console.error("!!!【开国大典失败】在执行系列法令时发生严重错误:", error);
|
||||||
|
|||||||
Reference in New Issue
Block a user