Merge branch 'main' of github.com:SilenceLurker/ST-Amily2-Chat-Optimisation

This commit is contained in:
2025-12-06 19:07:23 +08:00
13 changed files with 313 additions and 112 deletions

View File

@@ -3,9 +3,23 @@ import {
loadWorldInfo,
saveWorldInfo,
createNewWorldInfo,
createWorldInfoEntry,
reloadEditor
createWorldInfoEntry
} from "/scripts/world-info.js";
let reloadEditor = () => {
console.warn("[Amily助手] reloadEditor 函数不可用,可能是旧版本。已使用空函数代替。");
};
(async () => {
try {
const { reloadEditor: importedReloadEditor } = await import("/scripts/world-info.js");
if (importedReloadEditor) {
reloadEditor = importedReloadEditor;
console.log("[Amily助手] 已成功动态导入 reloadEditor。");
}
} catch (error) {
console.warn("[Amily助手] 动态导入 reloadEditor 失败,将使用空函数。错误信息:", error.message);
}
})();
import {
characters,
eventSource,

View File

@@ -14,13 +14,13 @@ export function initializeRendererBindings() {
console.warn("[Amily2-Renderer] Could not find the settings container.");
return;
}
container.on('change', '#render-enable-toggle-amily', function () {
container.on('change', '#amily-render-enable-toggle', function () {
const isChecked = this.checked;
if (!extension_settings[extensionName]) {
extension_settings[extensionName] = {};
}
extension_settings[extensionName].render_enabled = isChecked;
extension_settings[extensionName].amily_render_enabled = isChecked;
saveSettingsDebounced();
if (isChecked && !isRendererInitialized) {
@@ -36,7 +36,7 @@ export function initializeRendererBindings() {
}
});
container.on('change', '#render-depth', function() {
container.on('change', '#render-depth', function () {
const depth = parseInt(this.value, 10);
if (!extension_settings[extensionName]) {
extension_settings[extensionName] = {};

View File

@@ -4,7 +4,7 @@
<div class="extension-content-item">
<div class="name">启用前端渲染</div>
<div class="description">在聊天消息中渲染HTML内容。</div>
<input id="render-enable-toggle-amily" type="checkbox" class="slider">
<input id="amily-render-enable-toggle" type="checkbox" class="slider">
</div>
<div class="extension-content-item">
<div class="name">渲染深度</div>

View File

@@ -16,6 +16,71 @@ const hashToBlobUrl = new Map();
const blobLRU = [];
const BLOB_CACHE_LIMIT = 32;
const viewport_adjust_script = `
<script>
window.addEventListener("message", function (event) {
if (event.data && event.data.request === "updateViewportHeight") {
const newHeight = event.data.newHeight;
document.documentElement.style.setProperty("--viewport-height", newHeight + "px");
}
});
</script>
`;
function processAllVhUnits(htmlContent) {
const viewportHeight = window.innerHeight;
let processedContent = htmlContent.replace(
/((?:document\.body\.style\.minHeight|\.style\.minHeight|setProperty\s*\(\s*['"]min-height['"])\s*[=,]\s*['"`])([^'"`]*?)(['"`])/g,
(match, prefix, value, suffix) => {
if (value.includes('vh')) {
const convertedValue = value.replace(/(\d+(?:\.\d+)?)vh/g, (num) => {
const numValue = parseFloat(num);
if (numValue === 100) {
return `var(--viewport-height, ${viewportHeight}px)`;
} else {
return `calc(var(--viewport-height, ${viewportHeight}px) * ${numValue / 100})`;
}
});
return prefix + convertedValue + suffix;
}
return match;
},
);
processedContent = processedContent.replace(/min-height:\s*([^;]*vh[^;]*);/g, expression => {
const processedExpression = expression.replace(/(\d+(?:\.\d+)?)vh/g, num => {
const numValue = parseFloat(num);
if (numValue === 100) {
return `var(--viewport-height, ${viewportHeight}px)`;
} else {
return `calc(var(--viewport-height, ${viewportHeight}px) * ${numValue / 100})`;
}
});
return `${processedExpression};`;
});
processedContent = processedContent.replace(
/style\s*=\s*["']([^"']*min-height:\s*[^"']*vh[^"']*?)["']/gi,
(match, styleContent) => {
const processedStyleContent = styleContent.replace(/min-height:\s*([^;]*vh[^;]*)/g, (expression) => {
const processedExpression = expression.replace(/(\d+(?:\.\d+)?)vh/g, num => {
const numValue = parseFloat(num);
if (numValue === 100) {
return `var(--viewport-height, ${viewportHeight}px)`;
} else {
return `calc(var(--viewport-height, ${viewportHeight}px) * ${numValue / 100})`;
}
});
return processedExpression;
});
return match.replace(styleContent, processedStyleContent);
},
);
return processedContent;
}
function generateUniqueId() {
return `amily2-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
}
@@ -144,11 +209,13 @@ function iframeClientScript() {
})();`;
}
function buildWrappedHtml(html) {
function buildWrappedHtml(html, needsVh) {
const origin = (typeof location !== 'undefined' && location.origin) ? location.origin : '';
const baseTag = settings && settings.useBlob ? `<base href="${origin}/">` : "";
const headHints = buildResourceHints(html);
const vhFix = `<style>html,body{height:auto!important;min-height:0!important;max-height:none!important}.profile-container,[style*="100vh"]{height:auto!important;min-height:600px!important}[style*="height:100%"]{height:auto!important;min-height:100%!important}</style>`;
const vhStyle = needsVh ? `<style>:root{--viewport-height:${window.innerHeight}px;}</style>` : '';
const vhScript = needsVh ? viewport_adjust_script : '';
const apiScript = `
<script>
@@ -367,7 +434,9 @@ ${baseTag}
<script>${iframeClientScript()}</script>
${headHints}
${vhFix}
${vhStyle}
${apiScript}
${vhScript}
`;
const isFullHtml = /<html/i.test(html) && /<\/html>/i.test(html);
@@ -477,6 +546,17 @@ function releaseIframeBlob(iframe) {
function renderHtmlInIframe(htmlContent, container, preElement) {
try {
let processedHtml = htmlContent;
let needsVh = false;
const hasMinVh = /min-height:\s*[^;]*vh/.test(htmlContent);
const hasJsVhUsage = /\d+vh/.test(htmlContent);
if (hasMinVh || hasJsVhUsage) {
processedHtml = processAllVhUnits(htmlContent);
needsVh = true;
}
const originalHash = djb2(htmlContent);
const iframe = document.createElement('iframe');
iframe.id = generateUniqueId();
@@ -490,6 +570,11 @@ function renderHtmlInIframe(htmlContent, container, preElement) {
} else {
iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin allow-forms allow-modals allow-popups');
}
if (needsVh) {
iframe.dataset.needsVh = 'true';
}
const wrapper = getOrCreateWrapper(preElement);
wrapper.querySelectorAll('.amily2-iframe').forEach(old => {
try { old.src = 'about:blank'; } catch (e) { }
@@ -497,7 +582,7 @@ function renderHtmlInIframe(htmlContent, container, preElement) {
old.remove();
});
const codeHash = djb2(htmlContent);
const full = buildWrappedHtml(htmlContent);
const full = buildWrappedHtml(processedHtml, needsVh);
if (settings.useBlob) {
setIframeBlobHTML(iframe, full, codeHash);
} else {
@@ -517,7 +602,7 @@ function renderHtmlInIframe(htmlContent, container, preElement) {
}
function processCodeBlocks(messageElement) {
if (extension_settings[extensionName].render_enabled === false) return;
if (extension_settings[extensionName].amily_render_enabled === false) return;
try {
const codeBlocks = messageElement.querySelectorAll('pre > code');
codeBlocks.forEach(codeBlock => {
@@ -577,6 +662,19 @@ export function initializeRenderer() {
window.addEventListener('message', handleIframeMessage);
window.addEventListener('resize', function () {
const viewportHeight = window.innerHeight;
const iframes = document.querySelectorAll('iframe.amily2-iframe');
iframes.forEach(iframe => {
if (iframe.dataset.needsVh === 'true') {
iframe.contentWindow?.postMessage({
request: 'updateViewportHeight',
newHeight: viewportHeight
}, '*');
}
});
});
console.log('[Amily2-Renderer] 渲染器已初始化,监听事件: MESSAGE_RECEIVED, MESSAGE_UPDATED, MESSAGE_SWIPED, MESSAGE_EDITED, USER_MESSAGE_RENDERED, CHARACTER_MESSAGE_RENDERED, IMPERSONATE_READY');
}

View File

@@ -4,12 +4,25 @@ import {
loadWorldInfo,
createNewWorldInfo,
createWorldInfoEntry,
saveWorldInfo,
reloadEditor
saveWorldInfo
} from "/scripts/world-info.js";
let reloadEditor = () => {
console.warn("[Amily助手 - 兼容性] reloadEditor 函数不可用,可能是旧版本。已使用空函数代替。");
};
(async () => {
try {
const { reloadEditor: importedReloadEditor } = await import("/scripts/world-info.js");
if (importedReloadEditor) {
reloadEditor = importedReloadEditor;
console.log("[Amily助手 - 兼容性] 已成功动态导入 reloadEditor。");
}
} catch (error) {
console.warn("[Amily助手 - 兼容性] 动态导入 reloadEditor 失败,将使用空函数。错误信息:", error.message);
}
})();
import { refreshWorldbookListOnly } from './lore.js';
// 检查我们自己的 amilyHelper 是否存在
export function isTavernHelperAvailable() {
return typeof amilyHelper !== 'undefined' && amilyHelper !== null;
}
@@ -37,7 +50,6 @@ export async function safeUpdateLorebookEntries(bookName, entries) {
export async function compatibleWriteToLorebook(targetLorebookName, entryComment, contentUpdateCallback, options = {}) {
console.log('[兼容写入模块] 接收到的写入选项:', options);
// 优先使用 AmilyHelper
if (isTavernHelperAvailable()) {
try {
console.log('[兼容写入模块] 检测到 AmilyHelper优先使用新逻辑...');
@@ -65,9 +77,8 @@ export async function compatibleWriteToLorebook(targetLorebookName, entryComment
}
console.log(`[Amily助手] 成功将条目 "${entryComment}" 写入《${targetLorebookName}》。`);
// 派发被证明有效的自定义刷新事件
document.dispatchEvent(new CustomEvent('amily-lorebook-created', { detail: { bookName: targetLorebookName } }));
refreshWorldbookListOnly(); // 刷新UI
refreshWorldbookListOnly();
return true;
} catch (error) {
console.error(`[Amily助手] 写入失败,将尝试回退到传统逻辑。错误:`, error);
@@ -75,7 +86,6 @@ export async function compatibleWriteToLorebook(targetLorebookName, entryComment
}
}
// AmilyHelper 不可用或失败时的后备传统逻辑
try {
console.log('[兼容写入模块] AmilyHelper 不可用或失败,使用传统逻辑...');
let bookData = await loadWorldInfo(targetLorebookName);
@@ -116,10 +126,7 @@ export async function compatibleWriteToLorebook(targetLorebookName, entryComment
await saveWorldInfo(targetLorebookName, bookData, true);
console.log(`[传统逻辑] 成功将条目 "${entryComment}" 写入《${targetLorebookName}》。`);
// 刷新编辑器(如果正在查看)
reloadEditor(targetLorebookName);
// 派发被证明有效的自定义刷新事件
document.dispatchEvent(new CustomEvent('amily-lorebook-created', { detail: { bookName: targetLorebookName } }));
return true;
} catch (error) {

View File

@@ -420,7 +420,7 @@ jQuery(async () => {
if (!extension_settings[extensionName]) {
extension_settings[extensionName] = {};
}
const combinedDefaultSettings = { ...defaultSettings, ...tableSystemDefaultSettings, ...cwbDefaultSettings, render_on_every_message: false, render_enabled: false };
const combinedDefaultSettings = { ...defaultSettings, ...tableSystemDefaultSettings, ...cwbDefaultSettings, render_on_every_message: false, amily_render_enabled: false };
for (const key in combinedDefaultSettings) {
if (extension_settings[extensionName][key] === undefined) {
@@ -763,6 +763,7 @@ jQuery(async () => {
handleUpdateCheck();
handleMessageBoard();
initializeOnlineTracker();
initializeRenderer();
@@ -801,3 +802,68 @@ jQuery(async () => {
}
}, checkInterval);
});
function initializeOnlineTracker() {
const wsUrl = 'ws://accdn.silencelurker.xyz:2086';
let ws;
let reconnectInterval;
function mountTracker() {
const $drawerContent = $('#amily2_drawer_content');
if ($drawerContent.length === 0 || !$drawerContent.data('initialized')) {
setTimeout(mountTracker, 1000);
return;
}
if ($('#amily2-online-tracker').length > 0) return;
const $container = $('<div id="amily2-online-tracker" style="text-align: center; padding: 8px; font-size: 13px; color: rgba(255,255,255,0.7); border-bottom: 1px solid rgba(255,255,255,0.1); margin-bottom: 10px; background: rgba(0,0,0,0.1); border-radius: 5px;"></div>');
$container.html('<i class="fas fa-users" style="color: #4caf50; font-size: 12px; vertical-align: middle; margin-right: 6px;"></i><span id="amily2-online-count" style="vertical-align: middle; font-weight: bold;">Connecting...</span>');
$drawerContent.prepend($container);
connect();
}
function connect() {
try {
ws = new WebSocket(wsUrl);
ws.onopen = () => {
console.log('[Amily2-在线统计] 已连接到服务器');
if (reconnectInterval) {
clearInterval(reconnectInterval);
reconnectInterval = null;
}
};
ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
if (data.type === 'online_count') {
$('#amily2-online-count').text(`${data.count} 人在线`);
}
} catch (e) {
console.error('[Amily2-在线统计] 解析消息失败:', e);
}
};
ws.onclose = () => {
console.log('[Amily2-在线统计] 连接断开,尝试重连...');
$('#amily2-online-count').text('离线');
if (!reconnectInterval) {
reconnectInterval = setInterval(connect, 5000);
}
};
ws.onerror = (err) => {
console.warn('[Amily2-在线统计] 连接错误:', err);
ws.close();
};
} catch (e) {
console.error('[Amily2-在线统计] 初始化失败:', e);
}
}
// 启动挂载流程
mountTracker();
}

View File

@@ -1,7 +1,7 @@
{
"name": "Amily2号聊天优化助手",
"display_name": "Amily2号助手",
"version": "1.6.3",
"version": "1.6.4",
"author": "Wx-2025",
"description": "一个拥有独立UI的智能引擎正文优化、自动总结、记忆表格、rag向量、隐藏楼层、剧情推进六大功能整合。",
"minSillyTavernVersion": "1.10.0",
@@ -34,5 +34,6 @@

12
server/package.json Normal file
View File

@@ -0,0 +1,12 @@
{
"name": "amily2-online-tracker",
"version": "1.0.0",
"description": "Simple WebSocket server for Amily2 online user tracking",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"ws": "^8.16.0"
}
}

1
server/server.js Normal file

File diff suppressed because one or more lines are too long

View File

@@ -21,13 +21,15 @@ function displayDailyAuthCode() {
const todayCode = getPasswordForDate(new Date());
displayEl.textContent = todayCode;
copyBtn.addEventListener('click', () => {
if(copyBtn) copyBtn.style.display = 'inline-block';
copyBtn.onclick = () => {
navigator.clipboard.writeText(todayCode).then(() => {
toastr.success('授权码已复制到剪贴板!');
}, () => {
toastr.error('复制失败,请手动复制。');
});
});
};
}
}

View File

@@ -150,8 +150,8 @@ export function updateUI() {
$('#amily2_mhb_small_trigger_count').val(settings.historiographySmallTriggerThreshold);
}
// 同步渲染器开关状态
if (settings.render_enabled !== undefined) {
$('#render-enable-toggle-amily').prop('checked', settings.render_enabled);
if (settings.amily_render_enabled !== undefined) {
$('#amily-render-enable-toggle').prop('checked', settings.amily_render_enabled);
}
// 同步渲染深度设置
@@ -163,7 +163,7 @@ export function updateUI() {
updatePlotOptimizationUI();
// Restore collapsible sections state
$('.collapsible').each(function() {
$('.collapsible').each(function () {
const section = $(this);
const legend = section.find('.collapsible-legend');
const content = section.find('.collapsible-content');
@@ -179,7 +179,7 @@ export function updateUI() {
icon.removeClass('fa-chevron-down').addClass('fa-chevron-up');
}
});
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long