Update cwb_settingsManager.js

This commit is contained in:
2025-09-07 06:15:21 +08:00
committed by GitHub
parent c9932ebe86
commit c913e8fd97

View File

@@ -209,20 +209,54 @@ export function bindSettingsEvents($settingsPanel) {
}); });
$panel.on('change', '#cwb-api-mode', function() { $panel.on('change', '#cwb-api-mode', function() {
const selectedMode = $(this).val(); const selectedMode = $(this).val();
getSettings().cwb_api_mode = selectedMode;
saveSettingsDebounced();
updateApiModeUI(selectedMode); updateApiModeUI(selectedMode);
if (selectedMode === 'sillytavern_preset') { if (selectedMode === 'sillytavern_preset') {
loadSillyTavernPresets(); loadSillyTavernPresets(true);
} }
showToastr('success', `API模式已切换为: ${selectedMode === 'sillytavern_preset' ? 'SillyTavern预设' : '全兼容'}`);
}); });
$panel.on('change', '#cwb-tavern-profile', function() { $panel.on('change', '#cwb-tavern-profile', function() {
const selectedProfile = $(this).val(); const selectedProfile = $(this).val();
getSettings().cwb_tavern_profile = selectedProfile;
saveSettingsDebounced();
if (selectedProfile) { if (selectedProfile) {
console.log(`[CWB] 选择了预设: ${selectedProfile}`); console.log(`[CWB] 选择了预设: ${selectedProfile}`);
showToastr('success', `SillyTavern预设已选择: ${selectedProfile}`);
}
updateApiStatusDisplay($panel);
});
$panel.on('input', '#cwb-api-url', function() {
const apiUrl = $(this).val().trim();
getSettings().cwb_api_url = apiUrl;
saveSettingsDebounced();
updateApiStatusDisplay($panel);
});
$panel.on('input', '#cwb-api-key', function() {
const apiKey = $(this).val();
getSettings().cwb_api_key = apiKey;
saveSettingsDebounced();
});
$panel.on('change', '#cwb-api-model', function() {
const model = $(this).val();
getSettings().cwb_api_model = model;
saveSettingsDebounced();
updateApiStatusDisplay($panel);
if (model) {
showToastr('success', `模型已选择: ${model}`);
} }
}); });
$panel.on('click', '#cwb-load-models', () => fetchModelsAndConnect($panel)); $panel.on('click', '#cwb-load-models', () => fetchModelsAndConnect($panel));
$panel.on('click', '#cwb-save-config', saveApiConfig);
$panel.on('click', '#cwb-clear-config', clearApiConfig);
$panel.on('click', '#cwb-save-break-armor-prompt', saveBreakArmorPrompt); $panel.on('click', '#cwb-save-break-armor-prompt', saveBreakArmorPrompt);
$panel.on('click', '#cwb-reset-break-armor-prompt', resetBreakArmorPrompt); $panel.on('click', '#cwb-reset-break-armor-prompt', resetBreakArmorPrompt);
@@ -326,45 +360,34 @@ export function bindSettingsEvents($settingsPanel) {
} }
function updateApiModeUI(mode) { function updateApiModeUI(mode) {
const $apiUrlLabel = $panel.find('label[for="cwb-api-url"]'); const fields = {
const $apiUrlField = $panel.find('#cwb-api-url'); openai: [
const $apiKeyLabel = $panel.find('label[for="cwb-api-key"]'); 'label[for="cwb-api-url"]',
const $apiKeyField = $panel.find('#cwb-api-key'); '#cwb-api-url',
const $apiModelLabel = $panel.find('label[for="cwb-api-model"]'); 'label[for="cwb-api-key"]',
const $apiModelWrapper = $panel.find('#cwb-api-model').parent(); '#cwb-api-key',
const $loadModelsButton = $panel.find('#cwb-load-models'); 'label[for="cwb-api-model"]',
'#cwb-api-model',
const $tavernProfileLabel = $panel.find('label[for="cwb-tavern-profile"]'); '#cwb-load-models'
const $tavernProfileField = $panel.find('#cwb-tavern-profile'); ],
sillytavern: [
'label[for="cwb-tavern-profile"]',
'#cwb-tavern-profile'
]
};
if (mode === 'sillytavern_preset') { if (mode === 'sillytavern_preset') {
$apiUrlLabel.hide(); fields.openai.forEach(selector => $panel.find(selector).hide());
$apiUrlField.hide(); fields.sillytavern.forEach(selector => $panel.find(selector).show());
$apiKeyLabel.hide();
$apiKeyField.hide();
$apiModelLabel.hide();
$apiModelWrapper.hide();
$loadModelsButton.hide();
$tavernProfileLabel.show();
$tavernProfileField.show();
} else { } else {
$apiUrlLabel.show(); fields.sillytavern.forEach(selector => $panel.find(selector).hide());
$apiUrlField.show(); fields.openai.forEach(selector => $panel.find(selector).show());
$apiKeyLabel.show();
$apiKeyField.show();
$apiModelLabel.show();
$apiModelWrapper.show();
$loadModelsButton.show();
$tavernProfileLabel.hide();
$tavernProfileField.hide();
} }
updateApiStatusDisplay($panel); updateApiStatusDisplay($panel);
} }
function loadSillyTavernPresets() { function loadSillyTavernPresets(showNotification = false) {
const $profileSelect = $panel.find('#cwb-tavern-profile'); const $profileSelect = $panel.find('#cwb-tavern-profile');
try { try {
@@ -387,7 +410,9 @@ function loadSillyTavernPresets() {
$profileSelect.val(currentProfile); $profileSelect.val(currentProfile);
} }
if (showNotification) {
showToastr('success', `已加载 ${profiles.length} 个SillyTavern预设`); showToastr('success', `已加载 ${profiles.length} 个SillyTavern预设`);
}
} catch (error) { } catch (error) {
logError('加载SillyTavern预设失败:', error); logError('加载SillyTavern预设失败:', error);
@@ -423,6 +448,11 @@ function updateUiWithSettings() {
$panel.find('#cwb-break-armor-prompt-textarea').val(settings.cwb_break_armor_prompt); $panel.find('#cwb-break-armor-prompt-textarea').val(settings.cwb_break_armor_prompt);
$panel.find('#cwb-char-card-prompt-textarea').val(settings.cwb_char_card_prompt); $panel.find('#cwb-char-card-prompt-textarea').val(settings.cwb_char_card_prompt);
$panel.find('#cwb-temperature').val(settings.cwb_temperature);
$panel.find('#cwb-temperature-value').text(settings.cwb_temperature);
$panel.find('#cwb-max-tokens').val(settings.cwb_max_tokens);
$panel.find('#cwb-max-tokens-value').text(settings.cwb_max_tokens);
$panel.find('#cwb-auto-update-threshold').val(settings.cwb_auto_update_threshold); $panel.find('#cwb-auto-update-threshold').val(settings.cwb_auto_update_threshold);
$panel.find('#cwb_master_enabled-checkbox').prop('checked', settings.cwb_master_enabled); $panel.find('#cwb_master_enabled-checkbox').prop('checked', settings.cwb_master_enabled);
$panel.find('#cwb-auto-update-enabled-checkbox').prop('checked', settings.cwb_auto_update_enabled); $panel.find('#cwb-auto-update-enabled-checkbox').prop('checked', settings.cwb_auto_update_enabled);
@@ -447,62 +477,72 @@ function updateUiWithSettings() {
} }
export function loadSettings() { export function loadSettings() {
if (!$panel) { console.log('[CWB] Loading settings...');
logError('Settings panel is not yet available for loading settings.');
return;
}
const settings = getSettings(); const settings = getSettings();
if (!settings) { if (!settings) {
logError('CWB settings not found in extension_settings.'); extension_settings[extensionName] = { ...cwbCompleteDefaultSettings };
return; console.log('[CWB] Initialized default settings');
} } else {
Object.keys(cwbCompleteDefaultSettings).forEach(key => { Object.keys(cwbCompleteDefaultSettings).forEach(key => {
if (settings[key] === undefined || settings[key] === null) { if (settings[key] === undefined || settings[key] === null) {
settings[key] = cwbCompleteDefaultSettings[key]; settings[key] = cwbCompleteDefaultSettings[key];
} }
}); });
}
const finalSettings = getSettings();
const overrides = JSON.parse(localStorage.getItem(CWB_BOOLEAN_SETTINGS_OVERRIDE_KEY) || '{}'); const overrides = JSON.parse(localStorage.getItem(CWB_BOOLEAN_SETTINGS_OVERRIDE_KEY) || '{}');
if (overrides.cwb_master_enabled !== undefined) { if (overrides.cwb_master_enabled !== undefined) {
settings.cwb_master_enabled = overrides.cwb_master_enabled; finalSettings.cwb_master_enabled = overrides.cwb_master_enabled;
} }
if (overrides.cwb_auto_update_enabled !== undefined) { if (overrides.cwb_auto_update_enabled !== undefined) {
settings.cwb_auto_update_enabled = overrides.cwb_auto_update_enabled; finalSettings.cwb_auto_update_enabled = overrides.cwb_auto_update_enabled;
} }
if (overrides.cwb_viewer_enabled !== undefined) { if (overrides.cwb_viewer_enabled !== undefined) {
settings.cwb_viewer_enabled = overrides.cwb_viewer_enabled; finalSettings.cwb_viewer_enabled = overrides.cwb_viewer_enabled;
} }
if (overrides.cwb_incremental_update_enabled !== undefined) { if (overrides.cwb_incremental_update_enabled !== undefined) {
settings.cwb_incremental_update_enabled = overrides.cwb_incremental_update_enabled; finalSettings.cwb_incremental_update_enabled = overrides.cwb_incremental_update_enabled;
} }
state.masterEnabled = settings.cwb_master_enabled; state.masterEnabled = finalSettings.cwb_master_enabled;
state.customApiConfig.url = settings.cwb_api_url; state.viewerEnabled = finalSettings.cwb_viewer_enabled;
state.customApiConfig.apiKey = settings.cwb_api_key; state.autoUpdateEnabled = finalSettings.cwb_auto_update_enabled;
state.customApiConfig.model = settings.cwb_api_model; state.isIncrementalUpdateEnabled = finalSettings.cwb_incremental_update_enabled;
state.currentBreakArmorPrompt = settings.cwb_break_armor_prompt;
state.currentCharCardPrompt = settings.cwb_char_card_prompt;
state.currentIncrementalCharCardPrompt = settings.cwb_incremental_char_card_prompt;
state.currentBreakArmorPrompt = settings.cwb_break_armor_prompt; state.customApiConfig.url = finalSettings.cwb_api_url || '';
state.currentCharCardPrompt = settings.cwb_char_card_prompt; state.customApiConfig.apiKey = finalSettings.cwb_api_key || '';
state.currentIncrementalCharCardPrompt = settings.cwb_incremental_char_card_prompt; state.customApiConfig.model = finalSettings.cwb_api_model || '';
state.autoUpdateThreshold = settings.cwb_auto_update_threshold; state.currentBreakArmorPrompt = finalSettings.cwb_break_armor_prompt;
state.autoUpdateEnabled = settings.cwb_auto_update_enabled; state.currentCharCardPrompt = finalSettings.cwb_char_card_prompt;
state.viewerEnabled = settings.cwb_viewer_enabled; state.currentIncrementalCharCardPrompt = finalSettings.cwb_incremental_char_card_prompt;
state.isIncrementalUpdateEnabled = settings.cwb_incremental_update_enabled;
state.worldbookTarget = settings.cwb_worldbook_target; state.autoUpdateThreshold = finalSettings.cwb_auto_update_threshold;
state.customWorldBook = settings.cwb_custom_worldbook; state.worldbookTarget = finalSettings.cwb_worldbook_target;
state.customWorldBook = finalSettings.cwb_custom_worldbook;
console.log('[CWB] State updated:', {
masterEnabled: state.masterEnabled,
viewerEnabled: state.viewerEnabled,
autoUpdateEnabled: state.autoUpdateEnabled
});
if ($panel) { if ($panel) {
updateUiWithSettings(); updateUiWithSettings();
} }
updateControlsLockState(); updateControlsLockState();
setTimeout(() => {
const $viewerButton = $(`#${CHAR_CARD_VIEWER_BUTTON_ID}`);
if ($viewerButton.length > 0) {
const shouldShow = isCwbEnabled() && state.viewerEnabled;
$viewerButton.toggle(shouldShow);
console.log('[CWB] Viewer button visibility updated:', shouldShow);
}
}, 100);
saveSettingsDebounced(); saveSettingsDebounced();
} }