Update lore.js

This commit is contained in:
2025-07-12 17:50:22 +08:00
committed by GitHub
parent 14e1eb62ae
commit 744d634620

View File

@@ -119,42 +119,29 @@ async function refreshWorldbookListOnly(newBookName = null) {
} }
export async function writeSummaryToLorebook(pendingData) { export async function writeSummaryToLorebook(pendingData) {
if (!pendingData || !pendingData.summary || !pendingData.sourceAiMessageTimestamp) return; if (!pendingData || !pendingData.summary || !pendingData.sourceAiMessageTimestamp || !pendingData.settings) {
console.warn("[Amily2-国史馆] 接到一份残缺的待办文书,写入任务已中止。", pendingData);
return;
}
const context = getContext(); const context = getContext();
const chat = context.chat; const chat = context.chat;
let isSourceMessageValid = false; let isSourceMessageValid = false;
let sourceMessageCandidate = null; let sourceMessageCandidate = null;
for (let i = chat.length - 2; i >= 0; i--) { for (let i = chat.length - 2; i >= 0; i--) {
if (!chat[i].is_user) { if (!chat[i].is_user) { sourceMessageCandidate = chat[i]; break; }
sourceMessageCandidate = chat[i];
break;
} }
} if (sourceMessageCandidate && sourceMessageCandidate.send_date === pendingData.sourceAiMessageTimestamp) {
if (
sourceMessageCandidate &&
sourceMessageCandidate.send_date === pendingData.sourceAiMessageTimestamp
) {
isSourceMessageValid = true; isSourceMessageValid = true;
} }
const summaryToCommit = pendingData.summary;
const targetLorebookFromPending = pendingData.targetLorebook;
if (!isSourceMessageValid) { if (!isSourceMessageValid) {
console.log( console.log("[Amily2号-逆时寻踪] 裁决: 源消息已被修改或删除,遵旨废黜过时总结。");
"[Amily2号-逆时寻踪] 裁决: 源消息已被修改或删除,遵旨废黜过时总结。",
);
return; return;
} }
console.groupCollapsed( const { summary: summaryToCommit, settings } = pendingData;
`[Amily2号-存档任务-v19.0 最终版] ${new Date().toLocaleTimeString()}`,
); console.groupCollapsed(`[Amily2号-存档任务-v21.0 最终圣旨版] ${new Date().toLocaleTimeString()}`);
console.time("总结写入总耗时"); console.time("总结写入总耗时");
try { try {
@@ -162,15 +149,11 @@ export async function writeSummaryToLorebook(pendingData) {
const character = characters[context.characterId]; const character = characters[context.characterId];
let targetLorebookName = null; let targetLorebookName = null;
let isNewBook = false; let isNewBook = false;
switch (settings.target) {
switch (targetLorebookFromPending) {
case "character_main": case "character_main":
targetLorebookName = character?.data?.extensions?.world; targetLorebookName = character?.data?.extensions?.world;
if (!targetLorebookName) { if (!targetLorebookName) {
toastr.warning( toastr.warning("角色未绑定主世界书,总结写入任务已中止。", "Amily2号");
"角色未绑定主世界书,总结写入任务已中止。",
"Amily2号",
);
console.groupEnd(); console.groupEnd();
return; return;
} }
@@ -179,10 +162,7 @@ export async function writeSummaryToLorebook(pendingData) {
targetLorebookName = `${DEDICATED_LOREBOOK_NAME}-${chatIdentifier}`; targetLorebookName = `${DEDICATED_LOREBOOK_NAME}-${chatIdentifier}`;
break; break;
default: default:
toastr.error( toastr.error(`收到未知的写入指令: "${settings.target}"`, "Amily2号");
`收到未知的写入指令: "${targetLorebookFromPending}"`,
"Amily2号",
);
console.groupEnd(); console.groupEnd();
return; return;
} }
@@ -200,36 +180,41 @@ export async function writeSummaryToLorebook(pendingData) {
return; return;
} }
const existingEntry = Object.values(bookData.entries).find( const existingEntry = Object.values(bookData.entries).find(e => e.comment === uniqueLoreName && !e.disable);
(e) => e.comment === uniqueLoreName && !e.disable,
);
if (existingEntry) { if (existingEntry) {
const existingContent = existingEntry.content const existingContent = existingEntry.content.replace(INTRODUCTORY_TEXT, "").trim();
.replace(INTRODUCTORY_TEXT, "")
.trim();
const lines = existingContent ? existingContent.split("\n") : []; const lines = existingContent ? existingContent.split("\n") : [];
const nextNumber = lines.length + 1; const nextNumber = lines.length + 1;
existingEntry.content += `\n${nextNumber}. ${summaryToCommit}`; existingEntry.content += `\n${nextNumber}. ${summaryToCommit}`;
} else { } else {
const positionMap = {
'before_char': 0, 'after_char': 1, 'before_an': 2,
'after_an': 3, 'at_depth': 4
};
const finalKeywords = settings.keywords.split(',').map(k => k.trim()).filter(Boolean);
const isConstant = settings.activationMode === 'always';
const newEntry = createWorldInfoEntry(targetLorebookName, bookData); const newEntry = createWorldInfoEntry(targetLorebookName, bookData);
Object.assign(newEntry, { Object.assign(newEntry, {
comment: uniqueLoreName, comment: uniqueLoreName,
content: `${INTRODUCTORY_TEXT}1. ${summaryToCommit}`, content: `${INTRODUCTORY_TEXT}1. ${summaryToCommit}`,
key: [chatIdentifier, "Amily2", "总结"], key: finalKeywords,
constant: isConstant,
position: positionMap[settings.insertionPosition] ?? 4,
depth: settings.depth,
disable: false, disable: false,
}); });
} }
await saveWorldInfo(targetLorebookName, bookData, true); await saveWorldInfo(targetLorebookName, bookData, true);
console.log(`[史官司] 总结已遵旨写入《${targetLorebookName}》文件。`); console.log(`[史官司] 总结已遵旨写入《${targetLorebookName}》文件。`);
if (isNewBook) { if (isNewBook) {
await refreshWorldbookListOnly(targetLorebookName); await refreshWorldbookListOnly(targetLorebookName);
toastr.success( toastr.success(`已创建并写入新档案《${targetLorebookName}》!`, "Amily2号");
`已创建并写入新档案《${targetLorebookName}》!`,
"Amily2号",
);
} }
} catch (error) { } catch (error) {
console.error("[Amily2号-写入失败] 写入流程发生意外错误:", error); console.error("[Amily2号-写入失败] 写入流程发生意外错误:", error);