From 960dd658df3313b562113c940aaa64fc6e2d0bff Mon Sep 17 00:00:00 2001 From: Wx-2025 <351320169@qq.com> Date: Fri, 24 Oct 2025 19:57:21 +0800 Subject: [PATCH] Update main.js --- core/tavern-helper/main.js | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/core/tavern-helper/main.js b/core/tavern-helper/main.js index 432d6b8..22f5caf 100644 --- a/core/tavern-helper/main.js +++ b/core/tavern-helper/main.js @@ -3,9 +3,10 @@ import { loadWorldInfo, saveWorldInfo, createNewWorldInfo, - createWorldInfoEntry + createWorldInfoEntry, + reloadEditor } from "/scripts/world-info.js"; -import { characters } from "/script.js"; +import { characters, eventSource, event_types } from "/script.js"; import { getContext } from "/scripts/extensions.js"; import { executeSlashCommandsWithOptions } from '/scripts/slash-commands.js'; @@ -38,14 +39,16 @@ class AmilyHelper { if (!bookData || !bookData.entries) { return []; } + const positionMap = { 0: 'before_character_definition', 1: 'after_character_definition', 2: 'before_author_note', 3: 'after_author_note', 4: 'at_depth_as_system' }; return Object.entries(bookData.entries).map(([uid, entry]) => ({ uid: parseInt(uid), comment: entry.comment || '无标题条目', content: entry.content || '', key: entry.key || [], + keys: entry.key || [], enabled: !entry.disable, constant: entry.constant || false, - position: entry.position || 4, + position: positionMap[entry.position] || 'at_depth_as_system', depth: entry.depth || 998, })); } catch (error) { @@ -68,12 +71,20 @@ class AmilyHelper { if (entryUpdate.enabled !== undefined) existingEntry.disable = !entryUpdate.enabled; if (entryUpdate.comment !== undefined) existingEntry.comment = entryUpdate.comment; if (entryUpdate.key !== undefined) existingEntry.key = entryUpdate.key; + if (entryUpdate.keys !== undefined) existingEntry.key = entryUpdate.keys; if (entryUpdate.constant !== undefined) existingEntry.constant = entryUpdate.constant; - if (entryUpdate.position !== undefined) existingEntry.position = entryUpdate.position; + if (entryUpdate.type === 'constant') existingEntry.constant = true; + if (entryUpdate.type === 'selective') existingEntry.constant = false; + if (entryUpdate.position !== undefined) { + const positionMap = { 'before_character_definition': 0, 'after_character_definition': 1, 'before_author_note': 2, 'after_author_note': 3, 'at_depth': 4, 'at_depth_as_system': 4 }; + existingEntry.position = positionMap[entryUpdate.position] ?? 4; + } if (entryUpdate.depth !== undefined) existingEntry.depth = entryUpdate.depth; } } await saveWorldInfo(bookName, bookData, true); + reloadEditor(bookName); // 刷新编辑器 + eventSource.emit(event_types.WORLD_INFO_UPDATED, bookName); return true; } catch (error) { console.error(`[Amily助手] 更新世界书《${bookName}》条目时出错:`, error); @@ -95,17 +106,20 @@ class AmilyHelper { for (const newEntryData of entries) { const newEntry = createWorldInfoEntry(bookName, bookData); + const positionMap = { 'before_character_definition': 0, 'after_character_definition': 1, 'before_author_note': 2, 'after_author_note': 3, 'at_depth': 4, 'at_depth_as_system': 4 }; Object.assign(newEntry, { comment: newEntryData.comment || '新条目', content: newEntryData.content || '', - key: newEntryData.key || [], - constant: newEntryData.constant || false, - position: newEntryData.position ?? 4, + key: newEntryData.keys || newEntryData.key || [], + constant: newEntryData.type === 'constant' ? true : (newEntryData.constant || false), + position: typeof newEntryData.position === 'string' ? (positionMap[newEntryData.position] ?? 4) : (newEntryData.position ?? 4), depth: newEntryData.depth ?? 998, disable: !(newEntryData.enabled ?? true), }); + if (newEntryData.type === 'selective') newEntry.constant = false; } await saveWorldInfo(bookName, bookData, true); + reloadEditor(bookName); // 刷新编辑器 return true; } catch (error) { console.error(`[Amily助手] 在世界书《${bookName}》中创建新条目时出错:`, error); @@ -124,6 +138,8 @@ class AmilyHelper { world_names.push(bookName); world_names.sort(); } + // 派发一个自定义事件,通知UI更新 + document.dispatchEvent(new CustomEvent('amily-lorebook-created', { detail: { bookName } })); return true; } catch (error) { console.error(`[Amily助手] 创建世界书《${bookName}》时出错:`, error); @@ -144,6 +160,14 @@ class AmilyHelper { throw error; } } + + async loadWorldInfo(bookName) { + return await loadWorldInfo(bookName); + } + + async saveWorldInfo(bookName, data, isWorldInfo) { + await saveWorldInfo(bookName, data, isWorldInfo); + } } export const amilyHelper = new AmilyHelper();