Update cwb_uiManager.js

This commit is contained in:
2025-11-23 22:14:53 +08:00
committed by GitHub
parent b676a98a7e
commit b5a1d401ea

View File

@@ -1,44 +1,43 @@
import { SCRIPT_ID_PREFIX, CHAR_CARD_VIEWER_BUTTON_ID, CHAR_CARD_VIEWER_POPUP_ID, state } from './cwb_state.js'; import { SCRIPT_ID_PREFIX, CHAR_CARD_VIEWER_BUTTON_ID, CHAR_CARD_VIEWER_POPUP_ID, state } from './cwb_state.js';
import { logDebug, logError, showToastr, escapeHtml, parseCustomFormat, buildCustomFormat, isCwbEnabled } from './cwb_utils.js'; import { logDebug, logError, showToastr, escapeHtml, parseCustomFormat, buildCustomFormat, isCwbEnabled } from './cwb_utils.js';
import { deleteLorebookEntries, getTargetWorldBook } from './cwb_lorebookManager.js'; import { deleteLorebookEntries, getTargetWorldBook } from './cwb_lorebookManager.js';
import { manualUpdateLogic } from './cwb_core.js'; import { manualUpdateLogic } from './cwb_core.js';
import { testCwbConnection, fetchCwbModels } from './cwb_apiService.js'; import { testCwbConnection, fetchCwbModels } from './cwb_apiService.js';
import { extensionName } from '../../utils/settings.js'; import { extensionName } from '../../utils/settings.js';
import { extension_settings } from '/scripts/extensions.js'; import { extension_settings } from '/scripts/extensions.js';
import { saveSettingsDebounced } from '/script.js'; import { saveSettingsDebounced } from '/script.js';
import { amilyHelper } from '../../core/tavern-helper/main.js'; import { amilyHelper } from '../../core/tavern-helper/main.js';
const { jQuery: $, SillyTavern } = window; const { jQuery: $, SillyTavern } = window;
function createCharCardViewerPopupHtml(displayItems) { function createCharCardViewerPopupHtml(displayItems) {
const pathToLabelMap = { const pathToLabelMap = {
'narrative_essence.core_traits.name': '特质名称', 'narrative_essence.core_traits.name': '特质名称',
'narrative_essence.key_relationships.name': '关系人姓名', 'narrative_essence.key_relationships.name': '关系人姓名',
'NE.trait.name': '特质名称',
'NE.rel.name': '关系人姓名',
}; };
const keyToLabelMap = { const keyToLabelMap = {
'name': '姓名', 'name': '姓名',
// Old keys
'archetype': '身份原型', 'archetype': '身份原型',
'gender': '性别', 'gender': '性别',
'age': '年龄', 'age': '年龄',
'race': '种族', 'race': '种族',
'current_status': '当前状态', 'current_status': '当前状态',
'first_impression': '第一印象', 'first_impression': '第一印象',
'key_features': '显著特征', 'key_features': '显著特征',
'attire': '衣着风格', 'attire': '衣着风格',
'mannerisms': '习惯举止', 'mannerisms': '习惯举止',
'voice': '声音特征', 'voice': '声音特征',
'tags': '性格标签', 'tags': '性格标签',
'description': '性格详述', 'description': '性格详述',
'motivation': '内在驱动', 'motivation': '内在驱动',
'values': '价值观', 'values': '价值观',
'inner_conflict': '内心挣扎', 'inner_conflict': '内心挣扎',
'interaction_style': '互动风格', 'interaction_style': '互动风格',
'skills': '技能能力', 'skills': '技能能力',
'reputation': '他人声望', 'reputation': '他人声望',
'core_traits': '核心特质', 'core_traits': '核心特质',
'verbal_patterns': '语言范式', 'verbal_patterns': '语言范式',
'key_relationships': '关键关系', 'key_relationships': '关键关系',
@@ -47,6 +46,44 @@ function createCharCardViewerPopupHtml(displayItems) {
'style_summary': '风格总结', 'style_summary': '风格总结',
'quotes': '代表性引言', 'quotes': '代表性引言',
'summary': '关系概述', 'summary': '关系概述',
// New short keys
'CI': '核心认同',
'PI': '物理印记',
'PP': '心智侧写',
'SM': '社交矩阵',
'NE': '叙事精粹',
'arch': '身份原型',
'gen': '性别',
// age is same
// race is same
'status': '当前状态',
'first': '第一印象',
'feat': '显著特征',
// attire is same
'manner': '习惯举止',
// voice is same
// tags is same
'desc': '性格详述',
'mot': '内在驱动',
'val': '价值观',
'conf': '内心挣扎',
'style': '互动风格/风格总结', // Shared by SM.style and NE.verb.style
'skill': '技能能力',
'rep': '他人声望',
'trait': '核心特质',
'verb': '语言范式',
'rel': '关键关系',
'def': '特质定义',
'evid': '具体事例',
'quote': '代表性引言',
'sum': '关系概述',
}; };
const getLabel = (key, path) => { const getLabel = (key, path) => {
const pathKey = path.replace(/\.\d+\./g, '.'); const pathKey = path.replace(/\.\d+\./g, '.');
@@ -141,11 +178,22 @@ function createCharCardViewerPopupHtml(displayItems) {
if (charData) { if (charData) {
const charName = charData.name || `角色 ${index + 1}`; const charName = charData.name || `角色 ${index + 1}`;
if (charData.name) html += renderCard('姓名', { name: charData.name }, ''); if (charData.name) html += renderCard('姓名', { name: charData.name }, '');
// Support both old and new formats
if (charData.core_identity) html += renderCard('核心认同', charData.core_identity, 'core_identity'); if (charData.core_identity) html += renderCard('核心认同', charData.core_identity, 'core_identity');
if (charData.CI) html += renderCard('核心认同', charData.CI, 'CI');
if (charData.physical_imprint) html += renderCard('物理印记', charData.physical_imprint, 'physical_imprint'); if (charData.physical_imprint) html += renderCard('物理印记', charData.physical_imprint, 'physical_imprint');
if (charData.PI) html += renderCard('物理印记', charData.PI, 'PI');
if (charData.psyche_profile) html += renderCard('心智侧写', charData.psyche_profile, 'psyche_profile'); if (charData.psyche_profile) html += renderCard('心智侧写', charData.psyche_profile, 'psyche_profile');
if (charData.PP) html += renderCard('心智侧写', charData.PP, 'PP');
if (charData.social_matrix) html += renderCard('社交矩阵', charData.social_matrix, 'social_matrix'); if (charData.social_matrix) html += renderCard('社交矩阵', charData.social_matrix, 'social_matrix');
if (charData.SM) html += renderCard('社交矩阵', charData.SM, 'SM');
if (charData.narrative_essence) html += renderCard('叙事精粹', charData.narrative_essence, 'narrative_essence'); if (charData.narrative_essence) html += renderCard('叙事精粹', charData.narrative_essence, 'narrative_essence');
if (charData.NE) html += renderCard('叙事精粹', charData.NE, 'NE');
html += `<div class="cwb-cyber-card cwb-insertion-settings-card"> html += `<div class="cwb-cyber-card cwb-insertion-settings-card">
<h4 class="cwb-cyber-card__title">注入设置</h4> <h4 class="cwb-cyber-card__title">注入设置</h4>
@@ -182,9 +230,9 @@ function createCharCardViewerPopupHtml(displayItems) {
}); });
html += `</div></div></div>`; html += `</div></div></div>`;
return html; return html;
} }
function bindCharCardViewerPopupEvents($popup) { function bindCharCardViewerPopupEvents($popup) {
$popup.on('change', '.cwb-insertion-position', function() { $popup.on('change', '.cwb-insertion-position', function() {
const $this = $(this); const $this = $(this);
const $depthContainer = $this.closest('.cwb-insertion-settings-content').find('.cwb-insertion-depth-container'); const $depthContainer = $this.closest('.cwb-insertion-settings-content').find('.cwb-insertion-depth-container');
@@ -332,13 +380,13 @@ function bindCharCardViewerPopupEvents($popup) {
$button.prop('disabled', false).text(`保存修改`); $button.prop('disabled', false).text(`保存修改`);
} }
}); });
} }
function closeCharCardViewerPopup() { function closeCharCardViewerPopup() {
$(`#${CHAR_CARD_VIEWER_POPUP_ID}`).remove(); $(`#${CHAR_CARD_VIEWER_POPUP_ID}`).remove();
} }
export async function showCharCardViewerPopup() { export async function showCharCardViewerPopup() {
if (!isCwbEnabled()) return; if (!isCwbEnabled()) return;
closeCharCardViewerPopup(); closeCharCardViewerPopup();
try { try {
@@ -460,17 +508,17 @@ export async function showCharCardViewerPopup() {
logError('无法显示角色卡查看器:', error); logError('无法显示角色卡查看器:', error);
showToastr('error', '加载角色卡数据时出错。'); showToastr('error', '加载角色卡数据时出错。');
} }
} }
function toggleCharCardViewerPopup() { function toggleCharCardViewerPopup() {
if ($(`#${CHAR_CARD_VIEWER_POPUP_ID}`).length > 0) { if ($(`#${CHAR_CARD_VIEWER_POPUP_ID}`).length > 0) {
closeCharCardViewerPopup(); closeCharCardViewerPopup();
} else { } else {
showCharCardViewerPopup(); showCharCardViewerPopup();
} }
} }
function keepButtonInBounds($element, savePosition = false) { function keepButtonInBounds($element, savePosition = false) {
if (!$element || !$element.length) return; if (!$element || !$element.length) return;
const windowWidth = $(window).width(); const windowWidth = $(window).width();
const windowHeight = $(window).height(); const windowHeight = $(window).height();
@@ -483,9 +531,9 @@ function keepButtonInBounds($element, savePosition = false) {
if (savePosition) { if (savePosition) {
localStorage.setItem(state.STORAGE_KEY_VIEWER_BUTTON_POS, JSON.stringify({ top: $element.css('top'), left: $element.css('left') })); localStorage.setItem(state.STORAGE_KEY_VIEWER_BUTTON_POS, JSON.stringify({ top: $element.css('top'), left: $element.css('left') }));
} }
} }
function makeButtonDraggable($button) { function makeButtonDraggable($button) {
let isDragging = false, wasDragged = false, offset = { x: 0, y: 0 }, startPos = { x: 0, y: 0 }; let isDragging = false, wasDragged = false, offset = { x: 0, y: 0 }, startPos = { x: 0, y: 0 };
const DRAG_THRESHOLD = 5; // 5 pixels threshold const DRAG_THRESHOLD = 5; // 5 pixels threshold
@@ -550,9 +598,9 @@ function makeButtonDraggable($button) {
} }
toggleCharCardViewerPopup(); toggleCharCardViewerPopup();
}); });
} }
export function initializeCharCardViewer() { export function initializeCharCardViewer() {
const $existingButton = $(`#${CHAR_CARD_VIEWER_BUTTON_ID}`); const $existingButton = $(`#${CHAR_CARD_VIEWER_BUTTON_ID}`);
if ($existingButton.length > 0) { if ($existingButton.length > 0) {
@@ -590,9 +638,9 @@ export function initializeCharCardViewer() {
clearTimeout(resizeTimeout); clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => keepButtonInBounds($(`#${CHAR_CARD_VIEWER_BUTTON_ID}`), true), 150); resizeTimeout = setTimeout(() => keepButtonInBounds($(`#${CHAR_CARD_VIEWER_BUTTON_ID}`), true), 150);
}); });
} }
export function updateViewerButtonVisibility() { export function updateViewerButtonVisibility() {
const $button = $(`#${CHAR_CARD_VIEWER_BUTTON_ID}`); const $button = $(`#${CHAR_CARD_VIEWER_BUTTON_ID}`);
const shouldShow = isCwbEnabled() && state.viewerEnabled; const shouldShow = isCwbEnabled() && state.viewerEnabled;
@@ -614,9 +662,9 @@ export function updateViewerButtonVisibility() {
viewerEnabled: state.viewerEnabled, viewerEnabled: state.viewerEnabled,
shouldShow: shouldShow shouldShow: shouldShow
}); });
} }
export function bindCwbApiEvents() { export function bindCwbApiEvents() {
console.log('[CWB] Binding API events'); console.log('[CWB] Binding API events');
$('#cwb-api-url').off('input').on('input', function() { $('#cwb-api-url').off('input').on('input', function() {
@@ -689,4 +737,4 @@ export function bindCwbApiEvents() {
$button.prop('disabled', false).html('<i class="fas fa-download"></i> 获取模型'); $button.prop('disabled', false).html('<i class="fas fa-download"></i> 获取模型');
} }
}); });
} }