Update main.js

This commit is contained in:
2025-10-24 19:57:21 +08:00
committed by GitHub
parent 3b107382d2
commit 960dd658df

View File

@@ -3,9 +3,10 @@ import {
loadWorldInfo, loadWorldInfo,
saveWorldInfo, saveWorldInfo,
createNewWorldInfo, createNewWorldInfo,
createWorldInfoEntry createWorldInfoEntry,
reloadEditor
} from "/scripts/world-info.js"; } 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 { getContext } from "/scripts/extensions.js";
import { executeSlashCommandsWithOptions } from '/scripts/slash-commands.js'; import { executeSlashCommandsWithOptions } from '/scripts/slash-commands.js';
@@ -38,14 +39,16 @@ class AmilyHelper {
if (!bookData || !bookData.entries) { if (!bookData || !bookData.entries) {
return []; 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]) => ({ return Object.entries(bookData.entries).map(([uid, entry]) => ({
uid: parseInt(uid), uid: parseInt(uid),
comment: entry.comment || '无标题条目', comment: entry.comment || '无标题条目',
content: entry.content || '', content: entry.content || '',
key: entry.key || [], key: entry.key || [],
keys: entry.key || [],
enabled: !entry.disable, enabled: !entry.disable,
constant: entry.constant || false, constant: entry.constant || false,
position: entry.position || 4, position: positionMap[entry.position] || 'at_depth_as_system',
depth: entry.depth || 998, depth: entry.depth || 998,
})); }));
} catch (error) { } catch (error) {
@@ -68,12 +71,20 @@ class AmilyHelper {
if (entryUpdate.enabled !== undefined) existingEntry.disable = !entryUpdate.enabled; if (entryUpdate.enabled !== undefined) existingEntry.disable = !entryUpdate.enabled;
if (entryUpdate.comment !== undefined) existingEntry.comment = entryUpdate.comment; if (entryUpdate.comment !== undefined) existingEntry.comment = entryUpdate.comment;
if (entryUpdate.key !== undefined) existingEntry.key = entryUpdate.key; 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.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; if (entryUpdate.depth !== undefined) existingEntry.depth = entryUpdate.depth;
} }
} }
await saveWorldInfo(bookName, bookData, true); await saveWorldInfo(bookName, bookData, true);
reloadEditor(bookName); // 刷新编辑器
eventSource.emit(event_types.WORLD_INFO_UPDATED, bookName);
return true; return true;
} catch (error) { } catch (error) {
console.error(`[Amily助手] 更新世界书《${bookName}》条目时出错:`, error); console.error(`[Amily助手] 更新世界书《${bookName}》条目时出错:`, error);
@@ -95,17 +106,20 @@ class AmilyHelper {
for (const newEntryData of entries) { for (const newEntryData of entries) {
const newEntry = createWorldInfoEntry(bookName, bookData); 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, { Object.assign(newEntry, {
comment: newEntryData.comment || '新条目', comment: newEntryData.comment || '新条目',
content: newEntryData.content || '', content: newEntryData.content || '',
key: newEntryData.key || [], key: newEntryData.keys || newEntryData.key || [],
constant: newEntryData.constant || false, constant: newEntryData.type === 'constant' ? true : (newEntryData.constant || false),
position: newEntryData.position ?? 4, position: typeof newEntryData.position === 'string' ? (positionMap[newEntryData.position] ?? 4) : (newEntryData.position ?? 4),
depth: newEntryData.depth ?? 998, depth: newEntryData.depth ?? 998,
disable: !(newEntryData.enabled ?? true), disable: !(newEntryData.enabled ?? true),
}); });
if (newEntryData.type === 'selective') newEntry.constant = false;
} }
await saveWorldInfo(bookName, bookData, true); await saveWorldInfo(bookName, bookData, true);
reloadEditor(bookName); // 刷新编辑器
return true; return true;
} catch (error) { } catch (error) {
console.error(`[Amily助手] 在世界书《${bookName}》中创建新条目时出错:`, error); console.error(`[Amily助手] 在世界书《${bookName}》中创建新条目时出错:`, error);
@@ -124,6 +138,8 @@ class AmilyHelper {
world_names.push(bookName); world_names.push(bookName);
world_names.sort(); world_names.sort();
} }
// 派发一个自定义事件通知UI更新
document.dispatchEvent(new CustomEvent('amily-lorebook-created', { detail: { bookName } }));
return true; return true;
} catch (error) { } catch (error) {
console.error(`[Amily助手] 创建世界书《${bookName}》时出错:`, error); console.error(`[Amily助手] 创建世界书《${bookName}》时出错:`, error);
@@ -144,6 +160,14 @@ class AmilyHelper {
throw error; throw error;
} }
} }
async loadWorldInfo(bookName) {
return await loadWorldInfo(bookName);
}
async saveWorldInfo(bookName, data, isWorldInfo) {
await saveWorldInfo(bookName, data, isWorldInfo);
}
} }
export const amilyHelper = new AmilyHelper(); export const amilyHelper = new AmilyHelper();