From bdb11e305adfe79004a2a0cde441274c95dae09b Mon Sep 17 00:00:00 2001 From: Wx-2025 <351320169@qq.com> Date: Fri, 24 Oct 2025 19:54:21 +0800 Subject: [PATCH] Update tavernhelper-compatibility.js --- core/tavernhelper-compatibility.js | 107 ++++++++++++++++++++++------- 1 file changed, 84 insertions(+), 23 deletions(-) diff --git a/core/tavernhelper-compatibility.js b/core/tavernhelper-compatibility.js index cd7180d..0205809 100644 --- a/core/tavernhelper-compatibility.js +++ b/core/tavernhelper-compatibility.js @@ -1,9 +1,17 @@ import { amilyHelper } from './tavern-helper/main.js'; -import { eventSource, event_types } from "/script.js"; +import { + world_names, + loadWorldInfo, + createNewWorldInfo, + createWorldInfoEntry, + saveWorldInfo, + reloadEditor +} from "/scripts/world-info.js"; +import { refreshWorldbookListOnly } from './lore.js'; -// 我们现在总是“可用”的,因为我们依赖自己的实现,而不是那个屎山酒馆。 +// 检查我们自己的 amilyHelper 是否存在 export function isTavernHelperAvailable() { - return true; + return typeof amilyHelper !== 'undefined' && amilyHelper !== null; } export async function compatibleTriggerSlash(command) { return await amilyHelper.triggerSlash(command); @@ -27,43 +35,96 @@ export async function safeUpdateLorebookEntries(bookName, entries) { export async function compatibleWriteToLorebook(targetLorebookName, entryComment, contentUpdateCallback, options = {}) { - console.log('[Amily助手-写入模块] 接收到的写入选项:', options); + console.log('[兼容写入模块] 接收到的写入选项:', options); + // 优先使用 AmilyHelper + if (isTavernHelperAvailable()) { + try { + console.log('[兼容写入模块] 检测到 AmilyHelper,优先使用新逻辑...'); + const entries = await amilyHelper.getLorebookEntries(targetLorebookName); + const existingEntry = entries.find((e) => e.comment === entryComment && e.enabled); + + if (existingEntry) { + const newContent = contentUpdateCallback(existingEntry.content); + await amilyHelper.setLorebookEntries(targetLorebookName, [{ uid: existingEntry.uid, content: newContent }]); + } else { + const newContent = contentUpdateCallback(null); + const { keys = [], isConstant = false, insertion_position, depth: insertion_depth } = options; + const positionMap = { 'before_char': 0, 'after_char': 1, 'before_an': 2, 'after_an': 3, 'at_depth': 4 }; + + const newEntryData = { + comment: entryComment, + content: newContent, + key: keys, + constant: isConstant, + position: positionMap[insertion_position] ?? 4, + depth: parseInt(insertion_depth) || 998, + enabled: true, + }; + await amilyHelper.createLorebookEntries(targetLorebookName, [newEntryData]); + } + console.log(`[Amily助手] 成功将条目 "${entryComment}" 写入《${targetLorebookName}》。`); + + // 派发被证明有效的自定义刷新事件 + document.dispatchEvent(new CustomEvent('amily-lorebook-created', { detail: { bookName: targetLorebookName } })); + refreshWorldbookListOnly(); // 刷新UI + return true; + } catch (error) { + console.error(`[Amily助手] 写入失败,将尝试回退到传统逻辑。错误:`, error); + toastr.warning('Amily助手写入失败,尝试使用传统方式...', '兼容模式'); + } + } + + // AmilyHelper 不可用或失败时的后备传统逻辑 try { - const entries = await amilyHelper.getLorebookEntries(targetLorebookName); - const existingEntry = entries.find((e) => e.comment === entryComment && e.enabled); + console.log('[兼容写入模块] AmilyHelper 不可用或失败,使用传统逻辑...'); + let bookData = await loadWorldInfo(targetLorebookName); + + if (!bookData) { + console.warn(`[传统逻辑] 世界书《${targetLorebookName}》不存在,将自动创建。`); + await createNewWorldInfo(targetLorebookName); + if (!world_names.includes(targetLorebookName)) { + world_names.push(targetLorebookName); + world_names.sort(); + refreshWorldbookListOnly(); // 刷新UI + } + document.dispatchEvent(new CustomEvent('amily-lorebook-created', { detail: { bookName: targetLorebookName } })); + bookData = await loadWorldInfo(targetLorebookName); + if (!bookData) throw new Error(`创建并加载世界书《${targetLorebookName}》失败。`); + } + + const existingEntry = Object.values(bookData.entries).find(e => e.comment === entryComment && !e.disable); if (existingEntry) { - const newContent = contentUpdateCallback(existingEntry.content); - await amilyHelper.setLorebookEntries(targetLorebookName, [{ uid: existingEntry.uid, content: newContent }]); + existingEntry.content = contentUpdateCallback(existingEntry.content); } else { - const newContent = contentUpdateCallback(null); + const newEntry = createWorldInfoEntry(targetLorebookName, bookData); const { keys = [], isConstant = false, insertion_position, depth: insertion_depth } = options; - const positionMap = { 'before_char': 0, 'after_char': 1, 'before_an': 2, 'after_an': 3, 'at_depth': 4 }; - - const newEntryData = { + + Object.assign(newEntry, { comment: entryComment, - content: newContent, + content: contentUpdateCallback(null), key: keys, constant: isConstant, position: positionMap[insertion_position] ?? 4, depth: parseInt(insertion_depth) || 998, - enabled: true, - }; - - await amilyHelper.createLorebookEntries(targetLorebookName, [newEntryData]); + disable: false, + }); } - if (eventSource && typeof eventSource.emit === "function" && event_types.CHARACTER_PAGE_LOADED) { - eventSource.emit(event_types.CHARACTER_PAGE_LOADED); - } + await saveWorldInfo(targetLorebookName, bookData, true); + console.log(`[传统逻辑] 成功将条目 "${entryComment}" 写入《${targetLorebookName}》。`); - console.log(`[Amily助手] 成功将条目 "${entryComment}" 写入《${targetLorebookName}》。`); + // 刷新编辑器(如果正在查看) + reloadEditor(targetLorebookName); + + // 派发被证明有效的自定义刷新事件 + document.dispatchEvent(new CustomEvent('amily-lorebook-created', { detail: { bookName: targetLorebookName } })); return true; } catch (error) { - console.error(`[Amily助手] 写入世界书时发生严重错误:`, error); - toastr.error(`写入世界书失败: ${error.message}`, "Amily助手"); + console.error(`[传统逻辑] 写入世界书时发生严重错误:`, error); + toastr.error(`写入世界书失败: ${error.message}`, "传统逻辑"); return false; } }