Update cwb_lorebookManager

一定程度上可以避免出现获取世界书时TavernHelper未定义导致世界书名无法获取的情况(我必须得说这是个很不优雅的修正方案)
This commit is contained in:
2025-10-07 17:05:31 +08:00
parent 09a39b41fc
commit b5510c60db

View File

@@ -184,7 +184,7 @@ function createCharCardViewerPopupHtml(displayItems) {
}
function bindCharCardViewerPopupEvents($popup) {
$popup.on('change', '.cwb-insertion-position', function() {
$popup.on('change', '.cwb-insertion-position', function () {
const $this = $(this);
const $depthContainer = $this.closest('.cwb-insertion-settings-content').find('.cwb-insertion-depth-container');
if ($this.val() === 'at_depth') {
@@ -200,7 +200,7 @@ function bindCharCardViewerPopupEvents($popup) {
showCharCardViewerPopup();
});
$popup.find('#cwb-manual-update-btn').on('click', async function() {
$popup.find('#cwb-manual-update-btn').on('click', async function () {
const $button = $(this);
$button.prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> 更新中...');
await manualUpdateLogic();
@@ -217,7 +217,7 @@ function bindCharCardViewerPopupEvents($popup) {
$popup.find(`#cwb-char-content-${targetUid}`).addClass('active');
});
$popup.find('.cwb-cyber-tab__delete').on('click', async function(e) {
$popup.find('.cwb-cyber-tab__delete').on('click', async function (e) {
e.stopPropagation();
if (confirm('您确定要删除这个角色条目吗?此操作不可撤销。')) {
const uidToDelete = $(this).data('char-uid');
@@ -235,9 +235,9 @@ function bindCharCardViewerPopupEvents($popup) {
}
});
$popup.find('#cwb-viewer-delete-all').on('click', async function() {
$popup.find('#cwb-viewer-delete-all').on('click', async function () {
if (confirm('您确定要清除当前聊天中的所有角色卡和总览吗?此操作将删除所有相关条目,且不可撤销。')) {
const allUids = $popup.find('.cwb-cyber-tab__button').map(function() {
const allUids = $popup.find('.cwb-cyber-tab__button').map(function () {
return $(this).data('char-uid');
}).get();
if (allUids.length > 0) {
@@ -278,10 +278,15 @@ function bindCharCardViewerPopupEvents($popup) {
if ($field.data('is-array')) {
value = value.split('\n').map(l => l.trim()).filter(Boolean);
}
if(path){
setNestedValue(collectedData, path, value);
if (path) {
setNestedValue(collectedData, path, value);
}
});
let localTavernHelper = TavernHelper;
if (!localTavernHelper) {
// TavernHelper 未定义的情况下触发,但是为什么?
(localTavernHelper = window.TavernHelper);
}
const finalContentToSave = buildCustomFormat(collectedData);
const allEntries = await TavernHelper.getLorebookEntries(book);
const entryToUpdate = allEntries.find(e => e.uid === targetUid);
@@ -326,7 +331,11 @@ function bindCharCardViewerPopupEvents($popup) {
order: finalEntryData.order,
hasDepthField: 'depth' in finalEntryData
});
localTavernHelper = TavernHelper;
if (!localTavernHelper) {
// TavernHelper 未定义的情况下触发,但是为什么?
(localTavernHelper = window.TavernHelper);
}
await TavernHelper.setLorebookEntries(book, [finalEntryData]);
showToastr('success', '角色卡已成功保存!');
} catch (error) {
@@ -353,6 +362,11 @@ export async function showCharCardViewerPopup() {
bindCharCardViewerPopupEvents($(`#${CHAR_CARD_VIEWER_POPUP_ID}`));
return;
}
let localTavernHelper = TavernHelper;
if (!localTavernHelper) {
// TavernHelper 未定义的情况下触发,但是为什么?
(localTavernHelper = window.TavernHelper);
}
const allEntries = await TavernHelper.getLorebookEntries(book);
let currentChatId = state.currentChatFileIdentifier;
@@ -618,39 +632,39 @@ export function updateViewerButtonVisibility() {
export function bindCwbApiEvents() {
console.log('[CWB] Binding API events');
$('#cwb-api-url').off('input').on('input', function() {
$('#cwb-api-url').off('input').on('input', function () {
const value = $(this).val();
extension_settings[extensionName].cwb_api_url = value;
saveSettingsDebounced();
});
$('#cwb-api-key').off('input').on('input', function() {
$('#cwb-api-key').off('input').on('input', function () {
const value = $(this).val();
extension_settings[extensionName].cwb_api_key = value;
saveSettingsDebounced();
});
$('#cwb-model').off('input').on('input', function() {
$('#cwb-model').off('input').on('input', function () {
const value = $(this).val();
extension_settings[extensionName].cwb_model = value;
saveSettingsDebounced();
});
$('#cwb-temperature').off('input').on('input', function() {
$('#cwb-temperature').off('input').on('input', function () {
const value = parseFloat($(this).val());
$('#cwb-temperature-value').text(value);
extension_settings[extensionName].cwb_temperature = value;
saveSettingsDebounced();
});
$('#cwb-max-tokens').off('input').on('input', function() {
$('#cwb-max-tokens').off('input').on('input', function () {
const value = parseInt($(this).val());
$('#cwb-max-tokens-value').text(value);
extension_settings[extensionName].cwb_max_tokens = value;
saveSettingsDebounced();
});
$('#cwb-test-connection').off('click').on('click', async function() {
$('#cwb-test-connection').off('click').on('click', async function () {
const $button = $(this);
$button.prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> 测试中...');
@@ -663,7 +677,7 @@ export function bindCwbApiEvents() {
}
});
$('#cwb-fetch-models').off('click').on('click', async function() {
$('#cwb-fetch-models').off('click').on('click', async function () {
const $button = $(this);
$button.prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> 获取中...');