Add preset switching feature and clean up console logs

- Add 'Use separate preset for tracker generation' setting
- Implement automatic preset switching using /preset slash command
- Import getCurrentPresetName() from SillyTavern's regex engine
- Automatically import 'RPG Companion Trackers' preset on first load
- Comment out non-essential console.log statements
- Keep initialization, error, and migration logs for debugging
This commit is contained in:
Spicy_Marinara
2025-10-19 20:05:17 +02:00
parent 4a3170c661
commit f5418841cb
10 changed files with 410 additions and 8 deletions
+64
View File
@@ -4,6 +4,7 @@
*/
import { generateRaw, chat } from '../../../../../../../script.js';
import { executeSlashCommandsOnChatInput } from '../../../../../../../scripts/slash-commands.js';
import {
extensionSettings,
lastGeneratedData,
@@ -16,6 +17,49 @@ import {
import { saveChatData } from '../../core/persistence.js';
import { generateSeparateUpdatePrompt } from './promptBuilder.js';
import { parseResponse, parseUserStats } from './parser.js';
import { getCurrentPresetName } from '../../../../../regex/engine.js';
/**
* Switches to a specific preset by name using the /preset slash command
* @param {string} presetName - Name of the preset to switch to
* @returns {Promise<string|null>} The previous preset name, or null if switching failed
*/
async function switchToPreset(presetName) {
try {
// Get the current preset before switching using SillyTavern's built-in API
const previousPreset = getCurrentPresetName();
// Use the /preset slash command to switch presets
// This is the proper way to change presets in SillyTavern
await executeSlashCommandsOnChatInput(`/preset ${presetName}`, { quiet: true });
// console.log(`[RPG Companion] Switched from preset "${previousPreset || 'none'}" to "${presetName}"`);
return previousPreset;
} catch (error) {
console.error('[RPG Companion] Error switching preset:', error);
return null;
}
}
/**
* Restores a previously saved preset using the /preset slash command
* @param {string} presetName - Name of the preset to restore
*/
async function restorePreset(presetName) {
try {
if (!presetName) {
console.warn('[RPG Companion] No preset name to restore');
return;
}
// Use the /preset slash command to restore the preset
await executeSlashCommandsOnChatInput(`/preset ${presetName}`, { quiet: true });
// console.log(`[RPG Companion] Restored preset to "${presetName}"`);
} catch (error) {
console.error('[RPG Companion] Error restoring preset:', error);
}
}
/**
* Updates RPG tracker data using separate API call (separate mode only).
@@ -42,6 +86,8 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
return;
}
let previousPreset = null;
try {
setIsGenerating(true);
@@ -50,6 +96,14 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
const originalHtml = $updateBtn.html();
$updateBtn.html('<i class="fa-solid fa-spinner fa-spin"></i> Updating...').prop('disabled', true);
// Switch to separate preset if enabled
if (extensionSettings.useSeparatePreset) {
previousPreset = await switchToPreset('RPG Companion Trackers');
if (!previousPreset) {
console.warn('[RPG Companion] Failed to switch to RPG Companion Trackers preset. Using current preset.');
}
}
const prompt = generateSeparateUpdatePrompt();
// Generate using raw prompt (uses current preset, no chat history)
@@ -142,9 +196,19 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
} catch (error) {
console.error('[RPG Companion] Error updating RPG data:', error);
// Restore preset on error if we switched it
if (extensionSettings.useSeparatePreset && previousPreset) {
await restorePreset(previousPreset);
}
} finally {
setIsGenerating(false);
// Restore preset after successful generation
if (extensionSettings.useSeparatePreset && previousPreset) {
await restorePreset(previousPreset);
}
// Restore button to original state
const $updateBtn = $('#rpg-manual-update');
$updateBtn.html('<i class="fa-solid fa-sync"></i> Refresh RPG Info').prop('disabled', false);