mirror of
https://github.com/Wx-2025/ST-Amily2-Chat-Optimisation.git
synced 2026-06-06 15:05:51 +00:00
Update cwb_lorebookManager.js
This commit is contained in:
@@ -317,3 +317,84 @@ export async function manageAutoCardUpdateLorebookEntry() {
|
||||
logError('管理世界书条目时出错:', error);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* (重构) 通用函数,用于同步小说处理生成的世界书条目。
|
||||
* @param {string} bookName - 目标世界书名称。
|
||||
* @param {Array<{title: string, content: string}>} entries - 从API回复中解析出的条目数组。
|
||||
*/
|
||||
export async function syncNovelLorebookEntries(bookName, entries) {
|
||||
if (!bookName || !Array.isArray(entries) || entries.length === 0) {
|
||||
logError('[CWB-NovelSync] 参数无效或条目为空');
|
||||
if (Array.isArray(entries) && entries.length === 0) {
|
||||
showToastr('warning', '[小说处理] API回复中未找到有效条目。');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const allEntries = (await TavernHelper.getLorebookEntries(bookName)) || [];
|
||||
const managedEntries = allEntries.filter(e => e.comment?.startsWith(`[Amily2小说处理]`));
|
||||
|
||||
const entriesToUpdate = [];
|
||||
const entriesToCreate = [];
|
||||
|
||||
// 查找“章节内容概述”的最新部分编号
|
||||
let maxPart = 0;
|
||||
managedEntries.forEach(entry => {
|
||||
const match = entry.comment.match(/章节内容概述-第(\d+)部分/);
|
||||
if (match && parseInt(match[1], 10) > maxPart) {
|
||||
maxPart = parseInt(match[1], 10);
|
||||
}
|
||||
});
|
||||
let nextPart = maxPart + 1;
|
||||
|
||||
for (const entry of entries) {
|
||||
const { title, content } = entry;
|
||||
|
||||
if (title === '章节内容概述') {
|
||||
// “章节内容概述”条目总是新建
|
||||
const loreData = {
|
||||
keys: [`小说处理`, title, `第${nextPart}部分`],
|
||||
content: content,
|
||||
comment: `[Amily2小说处理] ${title}-第${nextPart}部分`,
|
||||
enabled: true,
|
||||
order: 100,
|
||||
position: 'before_char',
|
||||
};
|
||||
entriesToCreate.push(loreData);
|
||||
nextPart++; // 为同一批次中的下一个概述增加编号
|
||||
} else {
|
||||
// 其他条目(世界观、时间线等)是动态更新的
|
||||
const existingEntry = managedEntries.find(e => e.comment === `[Amily2小说处理] ${title}`);
|
||||
|
||||
const loreData = {
|
||||
keys: [`小说处理`, title],
|
||||
content: content,
|
||||
comment: `[Amily2小说处理] ${title}`,
|
||||
enabled: true,
|
||||
order: 100,
|
||||
position: 'before_char',
|
||||
};
|
||||
|
||||
if (existingEntry) {
|
||||
entriesToUpdate.push({ uid: existingEntry.uid, ...loreData });
|
||||
} else {
|
||||
entriesToCreate.push(loreData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (entriesToUpdate.length > 0) {
|
||||
await TavernHelper.setLorebookEntries(bookName, entriesToUpdate);
|
||||
showToastr('info', `[小说处理] 更新了 ${entriesToUpdate.length} 个世界书条目。`);
|
||||
}
|
||||
if (entriesToCreate.length > 0) {
|
||||
await TavernHelper.createLorebookEntries(bookName, entriesToCreate);
|
||||
showToastr('success', `[小说处理] 创建了 ${entriesToCreate.length} 个新世界书条目。`);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
logError('同步小说世界书条目时出错:', error);
|
||||
showToastr('error', '同步世界书失败,详情请查看控制台。');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user