diff --git a/core/tavern-helper/main.js b/core/tavern-helper/main.js index 56ff4b4..781cb10 100644 --- a/core/tavern-helper/main.js +++ b/core/tavern-helper/main.js @@ -31,7 +31,9 @@ import { name2, addOneMessage, messageFormatting, - substituteParamsExtended + substituteParamsExtended, + saveCharacterDebounced, + this_chid } from "/script.js"; import { getContext } from "/scripts/extensions.js"; import { executeSlashCommandsWithOptions } from '/scripts/slash-commands.js'; @@ -489,6 +491,34 @@ class AmilyHelper { } } + async deleteLorebookEntries(bookName, uids) { + try { + const bookData = await loadWorldInfo(bookName); + if (!bookData || !bookData.entries) { + return false; + } + + let deletedCount = 0; + for (const uid of uids) { + if (bookData.entries[uid]) { + delete bookData.entries[uid]; + deletedCount++; + } + } + + if (deletedCount > 0) { + await saveWorldInfo(bookName, bookData, true); + reloadEditor(bookName); + console.log(`[Amily助手] 已从世界书《${bookName}》删除 ${deletedCount} 个条目`); + return true; + } + return false; + } catch (error) { + console.error(`[Amily助手] 删除世界书《${bookName}》条目时出错:`, error); + return false; + } + } + async createLorebook(bookName) { try { if (world_names.includes(bookName)) { @@ -537,6 +567,42 @@ class AmilyHelper { getLastMessageId() { return chat.length - 1; } + + /** + * 将指定世界书绑定到当前角色 + * @param {string} bookName 世界书名称 + */ + async bindLorebookToCharacter(bookName) { + if (this_chid === undefined || !characters[this_chid]) { + console.warn('[Amily助手] 无法绑定世界书:未选中角色'); + return false; + } + + const char = characters[this_chid]; + if (!char.data) char.data = {}; + if (!char.data.extensions) char.data.extensions = {}; + + // 确保 world 字段是数组 + let worlds = char.data.extensions.world; + if (!Array.isArray(worlds)) { + worlds = worlds ? [worlds] : []; + } + + if (!worlds.includes(bookName)) { + worlds.push(bookName); + char.data.extensions.world = worlds; + console.log(`[Amily助手] 已将世界书《${bookName}》绑定到角色 ${char.name}`); + + if (typeof saveCharacterDebounced === 'function') { + saveCharacterDebounced(); + return true; + } else { + console.warn('[Amily助手] 无法保存角色数据:saveCharacterDebounced 不可用'); + return false; + } + } + return true; // 已经绑定 + } } export const amilyHelper = new AmilyHelper();