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:
@@ -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);
|
||||
|
||||
@@ -100,7 +100,7 @@ export function generateTrackerInstructions(includeHtmlPrompt = true, includeCon
|
||||
// Only add tracker instructions if at least one tracker is enabled
|
||||
if (hasAnyTrackers) {
|
||||
// Universal instruction header
|
||||
instructions += `\nYou must start your response with an appropriate update to the trackers in EXACTLY the same format as below, enclosed in separate Markdown code fences. Replace X with proper numbers and placeholders in [brackets] (while removing the brackets themselves) with in-world details ${userName} perceives about the current scene and the present characters. Consider the last trackers in the conversation (if they exist). Manage them accordingly and realistically; raise, lower, change, or keep the values unchanged based on the user's actions, the passage of time, and logical consequences:\n`;
|
||||
instructions += `\nAt the start of every reply, you must attach update to the trackers in EXACTLY the same format as below, enclosed in separate Markdown code fences. Replace X with proper numbers and [placeholders] with in-world details ${userName} perceives about the current scene and the present characters. Consider the last trackers in the conversation (if they exist). Manage them accordingly and realistically; raise, lower, change, or keep the values unchanged based on the user's actions, the passage of time, and logical consequences:\n`;
|
||||
|
||||
// Add format specifications for each enabled tracker
|
||||
if (extensionSettings.showUserStats) {
|
||||
@@ -150,7 +150,7 @@ export function generateTrackerInstructions(includeHtmlPrompt = true, includeCon
|
||||
|
||||
// Only add continuation instruction if includeContinuation is true
|
||||
if (includeContinuation) {
|
||||
instructions += `After updating the trackers, continue directly from where the last message in the chat history left off. Ensure the trackers you provide naturally reflect and influence the narrative. Character behavior, dialogue, and story events should acknowledge these conditions when relevant, such as fatigue affecting performance, low hygiene influencing social interactions, environmental factors shaping the scene, a character's emotional state coloring their responses, and so on.\n\n`;
|
||||
instructions += `After updating the trackers, continue directly from where the last message in the chat history left off. Ensure the trackers you provide naturally reflect and influence the narrative. Character behavior, dialogue, and story events should acknowledge these conditions when relevant, such as fatigue affecting the protagonist's performance, low hygiene influencing their social interactions, environmental factors shaping the scene, a character's emotional state coloring their responses, and so on. Do not render brackets.\n\n`;
|
||||
}
|
||||
|
||||
// Include attributes and dice roll only if there was a dice roll
|
||||
@@ -373,7 +373,7 @@ export function generateSeparateUpdatePrompt() {
|
||||
// Build the instruction message
|
||||
let instructionMessage = `</history>\n\n`;
|
||||
instructionMessage += generateRPGPromptText().replace('start your response with', 'respond with');
|
||||
instructionMessage += `Provide ONLY the requested data in the exact formats specified above. Do not include any roleplay response, other text, or commentary.`;
|
||||
instructionMessage += `Provide ONLY the requested data in the exact formats specified above. Do not include any roleplay response, other text, or commentary. Do not render brackets.`;
|
||||
|
||||
messages.push({
|
||||
role: 'user',
|
||||
|
||||
Reference in New Issue
Block a user