chore: final cleanup

This commit is contained in:
Subarashimo
2025-12-05 18:10:21 +01:00
parent 38328de1bf
commit 7e47dbfd7c
29 changed files with 338 additions and 2168 deletions
+20 -66
View File
@@ -10,17 +10,11 @@ import {
lastGeneratedData,
committedTrackerData,
isGenerating,
lastActionWasSwipe,
setIsGenerating,
setLastActionWasSwipe
setIsGenerating
} from '../../core/state.js';
import { saveChatData } from '../../core/persistence.js';
import { generateSeparateUpdatePrompt } from './promptBuilder.js';
import { parseResponse, parseUserStats, parseSkills, tryParseJSONResponse } from './parser.js';
import { renderUserStats } from '../rendering/userStats.js';
import { renderInfoBox } from '../rendering/infoBox.js';
import { renderThoughts } from '../rendering/thoughts.js';
import { renderInventory } from '../rendering/inventory.js';
import { renderQuests } from '../rendering/quests.js';
import { renderSkills } from '../rendering/skills.js';
import { i18n } from '../../core/i18n.js';
@@ -37,12 +31,8 @@ async function getCurrentPresetName() {
// 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;
}
@@ -63,11 +53,7 @@ async function getCurrentPresetName() {
*/
async function switchToPreset(presetName) {
try {
// 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 to preset "${presetName}"`);
return true;
} catch (error) {
console.error('[RPG Companion] Error switching preset:', error);
@@ -88,7 +74,6 @@ async function switchToPreset(presetName) {
*/
export async function updateRPGData(renderUserStats, renderInfoBox, renderThoughts, renderInventory) {
if (isGenerating) {
// console.log('[RPG Companion] Already generating, skipping...');
return;
}
@@ -97,7 +82,6 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
}
if (extensionSettings.generationMode !== 'separate') {
// console.log('[RPG Companion] Not in separate mode, skipping manual update');
return;
}
@@ -133,9 +117,6 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
});
if (response) {
// console.log('[RPG Companion] Raw AI response:', response);
// Try JSON parsing first if structured data mode is enabled
const jsonParsed = tryParseJSONResponse(response);
if (jsonParsed) {
@@ -149,23 +130,13 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
renderSkills();
saveChatData();
} else {
// JSON parsing failed - try legacy text-based parsing as fallback
console.warn('[RPG Companion] JSON parsing failed, attempting legacy text parsing...');
const parsedData = parseResponse(response);
// console.log('[RPG Companion] Parsed data:', parsedData);
// console.log('[RPG Companion] parsedData.userStats:', parsedData.userStats ? parsedData.userStats.substring(0, 100) + '...' : 'null');
// Legacy text parsing does not provide structured characters; clear stale structured data
extensionSettings.charactersData = [];
const parsedCharacterThoughts = parsedData.characterThoughts || '';
extensionSettings.charactersData = [];
const parsedCharacterThoughts = parsedData.characterThoughts || '';
// DON'T update lastGeneratedData here - it should only reflect the data
// from the assistant message the user replied to, not auto-generated updates
// This ensures swipes/regenerations use consistent source data
// Store RPG data for the last assistant message (separate mode)
const lastMessage = chat && chat.length > 0 ? chat[chat.length - 1] : null;
// console.log('[RPG Companion] Last message is_user:', lastMessage ? lastMessage.is_user : 'no message');
if (lastMessage && !lastMessage.is_user) {
if (!lastMessage.extra) {
lastMessage.extra = {};
@@ -175,16 +146,13 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
}
const currentSwipeId = lastMessage.swipe_id || 0;
lastMessage.extra.rpg_companion_swipes[currentSwipeId] = {
userStats: parsedData.userStats,
infoBox: parsedData.infoBox,
characterThoughts: parsedCharacterThoughts
};
lastMessage.extra.rpg_companion_swipes[currentSwipeId] = {
userStats: parsedData.userStats,
infoBox: parsedData.infoBox,
characterThoughts: parsedCharacterThoughts
};
// console.log('[RPG Companion] Stored separate mode RPG data for message swipe', currentSwipeId);
// Update lastGeneratedData for display AND future commit
if (parsedData.userStats) {
if (parsedData.userStats) {
lastGeneratedData.userStats = parsedData.userStats;
parseUserStats(parsedData.userStats);
}
@@ -194,28 +162,19 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
if (parsedData.infoBox) {
lastGeneratedData.infoBox = parsedData.infoBox;
}
lastGeneratedData.characterThoughts = parsedCharacterThoughts;
// console.log('[RPG Companion] 💾 SEPARATE MODE: Updated lastGeneratedData:', {
// userStats: lastGeneratedData.userStats ? 'exists' : 'null',
// infoBox: lastGeneratedData.infoBox ? 'exists' : 'null',
// characterThoughts: lastGeneratedData.characterThoughts ? 'exists' : 'null'
// });
lastGeneratedData.characterThoughts = parsedCharacterThoughts;
// Only auto-commit on TRULY first generation (no committed data exists at all)
// This prevents auto-commit after refresh when we have saved committed data
const hasAnyCommittedContent = (
(committedTrackerData.userStats && committedTrackerData.userStats.trim() !== '') ||
(committedTrackerData.infoBox && committedTrackerData.infoBox.trim() !== '' && committedTrackerData.infoBox !== 'Info Box\n---\n') ||
(committedTrackerData.characterThoughts && committedTrackerData.characterThoughts.trim() !== '' && committedTrackerData.characterThoughts !== 'Present Characters\n---\n')
);
const hasAnyCommittedContent = (
(committedTrackerData.userStats && committedTrackerData.userStats.trim() !== '') ||
(committedTrackerData.infoBox && committedTrackerData.infoBox.trim() !== '' && committedTrackerData.infoBox !== 'Info Box\n---\n') ||
(committedTrackerData.characterThoughts && committedTrackerData.characterThoughts.trim() !== '' && committedTrackerData.characterThoughts !== 'Present Characters\n---\n')
);
// Only commit if we have NO committed content at all (truly first time ever)
if (!hasAnyCommittedContent) {
committedTrackerData.userStats = parsedData.userStats;
committedTrackerData.infoBox = parsedData.infoBox;
committedTrackerData.characterThoughts = parsedCharacterThoughts;
// console.log('[RPG Companion] 🔆 FIRST TIME: Auto-committed tracker data');
}
if (!hasAnyCommittedContent) {
committedTrackerData.userStats = parsedData.userStats;
committedTrackerData.infoBox = parsedData.infoBox;
committedTrackerData.characterThoughts = parsedCharacterThoughts;
}
// Render the updated data
renderUserStats();
@@ -254,14 +213,9 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
setIsGenerating(false);
// Restore button to original state
const $updateBtn = $('#rpg-manual-update');
const refreshText = i18n.getTranslation('template.mainPanel.refreshRpgInfo') || 'Refresh RPG Info';
$updateBtn.html(`<i class="fa-solid fa-sync"></i> ${refreshText}`).prop('disabled', false);
// Reset the flag after tracker generation completes
// This ensures the flag persists through both main generation AND tracker generation
// console.log('[RPG Companion] 🔄 Tracker generation complete - resetting lastActionWasSwipe to false');
setLastActionWasSwipe(false);
}
}