diff --git a/src/systems/generation/apiClient.js b/src/systems/generation/apiClient.js index 70905c4..e30bec0 100644 --- a/src/systems/generation/apiClient.js +++ b/src/systems/generation/apiClient.js @@ -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} 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} True if switching succeeded, false otherwise @@ -71,11 +102,18 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough const originalHtml = $updateBtn.html(); $updateBtn.html(' 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 diff --git a/src/systems/generation/promptBuilder.js b/src/systems/generation/promptBuilder.js index 8070ad1..40ed4ce 100644 --- a/src/systems/generation/promptBuilder.js +++ b/src/systems/generation/promptBuilder.js @@ -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 += `\n{{persona}}\n\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`; messages.push({ diff --git a/src/systems/rendering/infoBox.js b/src/systems/rendering/infoBox.js index 544ff88..5cf3276 100644 --- a/src/systems/rendering/infoBox.js +++ b/src/systems/rendering/infoBox.js @@ -267,16 +267,36 @@ export function updateInfoBoxField(field, value) { const lines = lastGeneratedData.infoBox.split('\n'); let dateLineFound = false; let dateLineIndex = -1; + let weatherLineIndex = -1; // Find the date line for (let i = 0; i < lines.length; i++) { - if (lines[i].includes('πŸ—“οΈ:')) { + if (lines[i].includes('πŸ—“οΈ:') || lines[i].startsWith('Date:')) { dateLineFound = true; dateLineIndex = i; break; } } + // Find the weather line (look for a line that's not date/temp/time/location) + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + if (line.match(/^[^:]+:\s*.+$/) && + !line.includes('πŸ—“οΈ') && + !line.startsWith('Date:') && + !line.includes('🌑️') && + !line.startsWith('Temperature:') && + !line.includes('πŸ•’') && + !line.startsWith('Time:') && + !line.includes('πŸ—ΊοΈ') && + !line.startsWith('Location:') && + !line.includes('Info Box') && + !line.includes('---')) { + weatherLineIndex = i; + break; + } + } + const updatedLines = lines.map((line, index) => { if (field === 'month' && line.includes('πŸ—“οΈ:')) { const parts = line.split(','); @@ -306,26 +326,58 @@ export function updateInfoBoxField(field, value) { // No existing month/year, add them return `${parts[0]}, Month, ${value}`; } - } else if (field === 'weatherEmoji' && line.match(/^[^:]+:\s*.+$/) && !line.includes('πŸ—“οΈ') && !line.includes('🌑️') && !line.includes('πŸ•’') && !line.includes('πŸ—ΊοΈ') && !line.includes('Info Box') && !line.includes('---')) { - // This is the weather line - const parts = line.split(':'); - if (parts.length >= 2) { - return `${value}: ${parts.slice(1).join(':').trim()}`; + } else if (field === 'weatherEmoji' && index === weatherLineIndex) { + // Only update the specific weather line we found + if (line.startsWith('Weather:')) { + // New format: Weather: emoji, forecast + const weatherContent = line.replace('Weather:', '').trim(); + const parts = weatherContent.split(',').map(p => p.trim()); + const forecast = parts[1] || 'Weather'; + return `Weather: ${value}, ${forecast}`; + } else { + // Legacy format: emoji: forecast + const parts = line.split(':'); + if (parts.length >= 2) { + return `${value}: ${parts.slice(1).join(':').trim()}`; + } } - } else if (field === 'weatherForecast' && line.match(/^[^:]+:\s*.+$/) && !line.includes('πŸ—“οΈ') && !line.includes('🌑️') && !line.includes('πŸ•’') && !line.includes('πŸ—ΊοΈ') && !line.includes('Info Box') && !line.includes('---')) { - // This is the weather line - const parts = line.split(':'); - if (parts.length >= 2) { - return `${parts[0].trim()}: ${value}`; + } else if (field === 'weatherForecast' && index === weatherLineIndex) { + // Only update the specific weather line we found + if (line.startsWith('Weather:')) { + // New format: Weather: emoji, forecast + const weatherContent = line.replace('Weather:', '').trim(); + const parts = weatherContent.split(',').map(p => p.trim()); + const emoji = parts[0] || '🌀️'; + return `Weather: ${emoji}, ${value}`; + } else { + // Legacy format: emoji: forecast + const parts = line.split(':'); + if (parts.length >= 2) { + return `${parts[0].trim()}: ${value}`; + } } - } else if (field === 'temperature' && line.includes('🌑️:')) { - return `🌑️: ${value}`; - } else if (field === 'timeStart' && line.includes('πŸ•’:')) { + } else if (field === 'temperature' && (line.includes('🌑️:') || line.startsWith('Temperature:'))) { + // Support both emoji and text formats + if (line.startsWith('Temperature:')) { + return `Temperature: ${value}`; + } else { + return `🌑️: ${value}`; + } + } else if (field === 'timeStart' && (line.includes('πŸ•’:') || line.startsWith('Time:'))) { // Update time format: "HH:MM β†’ HH:MM" // When user edits, set both start and end time to the new value - return `πŸ•’: ${value} β†’ ${value}`; - } else if (field === 'location' && line.includes('πŸ—ΊοΈ:')) { - return `πŸ—ΊοΈ: ${value}`; + if (line.startsWith('Time:')) { + return `Time: ${value} β†’ ${value}`; + } else { + return `πŸ•’: ${value} β†’ ${value}`; + } + } else if (field === 'location' && (line.includes('πŸ—ΊοΈ:') || line.startsWith('Location:'))) { + // Support both emoji and text formats + if (line.startsWith('Location:')) { + return `Location: ${value}`; + } else { + return `πŸ—ΊοΈ: ${value}`; + } } return line; });