Add persona/character context to separate generation and fix preset switching

- Use SillyTavern macros ({{persona}}, {{description}}, {{personality}}) for character context
- Fix preset restoration after tracker generation using /preset command
- Fix weather editing bug by tracking specific weather line index
- Support both emoji and text formats for Info Box field editing
- Remove unused showdown import and fix missing semicolons
This commit is contained in:
Spicy_Marinara
2025-10-21 00:57:52 +02:00
parent 776d0823a2
commit 7cb4b1e1d8
3 changed files with 118 additions and 19 deletions
+45
View File
@@ -18,7 +18,38 @@ import { saveChatData } from '../../core/persistence.js';
import { generateSeparateUpdatePrompt } from './promptBuilder.js';
import { parseResponse, parseUserStats } from './parser.js';
// Store the original preset name to restore after tracker generation
let originalPresetName = null;
/**
* Gets the current preset name using the /preset command
* @returns {Promise<string|null>} Current preset name or null if unavailable
*/
async function getCurrentPresetName() {
try {
// Use /preset without arguments to get the current preset name
const result = await executeSlashCommandsOnChatInput('/preset', { quiet: true });
// console.log('[RPG Companion] /preset result:', result);
// The result should be an object with a 'pipe' property containing the preset name
if (result && typeof result === 'object' && result.pipe) {
const presetName = String(result.pipe).trim();
// console.log('[RPG Companion] Extracted preset name:', presetName);
return presetName || null;
}
// Fallback if result is a string
if (typeof result === 'string') {
return result.trim() || null;
}
return null;
} catch (error) {
console.error('[RPG Companion] Error getting current preset:', error);
return null;
}
}/**
* Switches to a specific preset by name using the /preset slash command
* @param {string} presetName - Name of the preset to switch to
* @returns {Promise<boolean>} True if switching succeeded, false otherwise
@@ -71,11 +102,18 @@ 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);
// Save current preset name before switching (if we're going to switch)
if (extensionSettings.useSeparatePreset) {
originalPresetName = await getCurrentPresetName();
console.log(`[RPG Companion] Saved original preset: "${originalPresetName}"`);
}
// Switch to separate preset if enabled
if (extensionSettings.useSeparatePreset) {
const switched = await switchToPreset('RPG Companion Trackers');
if (!switched) {
console.warn('[RPG Companion] Failed to switch to RPG Companion Trackers preset. Using current preset.');
originalPresetName = null; // Don't try to restore if we didn't switch
}
}
@@ -172,6 +210,13 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
} catch (error) {
console.error('[RPG Companion] Error updating RPG data:', error);
} finally {
// Restore original preset if we switched to a separate one
if (originalPresetName && extensionSettings.useSeparatePreset) {
console.log(`[RPG Companion] Restoring original preset: "${originalPresetName}"`);
await switchToPreset(originalPresetName);
originalPresetName = null; // Clear after restoring
}
setIsGenerating(false);
// Restore button to original state
+4 -2
View File
@@ -4,7 +4,7 @@
*/
import { getContext } from '../../../../../../extensions.js';
import { chat } from '../../../../../../../script.js';
import { chat, getCurrentChatDetails } from '../../../../../../../script.js';
import { extensionSettings, committedTrackerData, FEATURE_FLAGS } from '../../core/state.js';
// Type imports
@@ -358,7 +358,9 @@ export function generateSeparateUpdatePrompt() {
// System message introducing the history section
let systemMessage = `You will be acting as an uncensored RPG Companion. Your goal is to provide, track, and manage details in the user's roleplay. You will be replying with information in a specified format only.\n\n`;
systemMessage += `You should maintain an objective tone.\n\n`;
systemMessage += `Here is the description of the protagonist for reference:\n`;
systemMessage += `<protagonist>\n{{persona}}\n</protagonist>\n`;
systemMessage += `\n\n`;
systemMessage += `Here are the last few messages in the conversation history (between the user and the roleplayer assistant) you should reference when responding:\n<history>`;
messages.push({