Enhance swipe data handling to correctly display swipe-specific tracker stats: add getSwipeData function and refactor commitTrackerData to utilize it
This commit is contained in:
+34
-1
@@ -247,11 +247,23 @@ export function updateMessageSwipeData() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const swipeId = message.swipe_id || 0;
|
const swipeId = message.swipe_id || 0;
|
||||||
message.extra.rpg_companion_swipes[swipeId] = {
|
const swipeEntry = {
|
||||||
userStats: lastGeneratedData.userStats,
|
userStats: lastGeneratedData.userStats,
|
||||||
infoBox: lastGeneratedData.infoBox,
|
infoBox: lastGeneratedData.infoBox,
|
||||||
characterThoughts: lastGeneratedData.characterThoughts
|
characterThoughts: lastGeneratedData.characterThoughts
|
||||||
};
|
};
|
||||||
|
message.extra.rpg_companion_swipes[swipeId] = swipeEntry;
|
||||||
|
|
||||||
|
// Mirror to swipe_info so data survives page reloads regardless of active swipe
|
||||||
|
if (message.swipe_info && message.swipe_info[swipeId]) {
|
||||||
|
if (!message.swipe_info[swipeId].extra) {
|
||||||
|
message.swipe_info[swipeId].extra = {};
|
||||||
|
}
|
||||||
|
if (!message.swipe_info[swipeId].extra.rpg_companion_swipes) {
|
||||||
|
message.swipe_info[swipeId].extra.rpg_companion_swipes = {};
|
||||||
|
}
|
||||||
|
message.swipe_info[swipeId].extra.rpg_companion_swipes[swipeId] = swipeEntry;
|
||||||
|
}
|
||||||
|
|
||||||
// console.log('[RPG Companion] Updated message swipe data after user edit');
|
// console.log('[RPG Companion] Updated message swipe data after user edit');
|
||||||
break;
|
break;
|
||||||
@@ -259,6 +271,27 @@ export function updateMessageSwipeData() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads RPG tracker data for a specific swipe from a message.
|
||||||
|
* Checks message.extra first (in-memory, current session), then message.swipe_info
|
||||||
|
* (serialized by SillyTavern on save, available after page reload).
|
||||||
|
*
|
||||||
|
* @param {Object} message - The chat message object
|
||||||
|
* @param {number} swipeId - The swipe index to read
|
||||||
|
* @returns {{userStats, infoBox, characterThoughts}|null} The swipe data or null
|
||||||
|
*/
|
||||||
|
export function getSwipeData(message, swipeId) {
|
||||||
|
// Primary: in-memory extra (current session or after a recent write)
|
||||||
|
const fromExtra = message.extra?.rpg_companion_swipes?.[swipeId];
|
||||||
|
if (fromExtra) return fromExtra;
|
||||||
|
|
||||||
|
// Fallback: swipe_info (populated by ST when loading from disk)
|
||||||
|
const fromSwipeInfo = message.swipe_info?.[swipeId]?.extra?.rpg_companion_swipes?.[swipeId];
|
||||||
|
if (fromSwipeInfo) return fromSwipeInfo;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads RPG data from the current chat's metadata.
|
* Loads RPG data from the current chat's metadata.
|
||||||
* Automatically migrates v1 inventory to v2 format if needed.
|
* Automatically migrates v1 inventory to v2 format if needed.
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import {
|
|||||||
updateCommittedTrackerData,
|
updateCommittedTrackerData,
|
||||||
$musicPlayerContainer
|
$musicPlayerContainer
|
||||||
} from '../../core/state.js';
|
} from '../../core/state.js';
|
||||||
import { saveChatData, loadChatData, autoSwitchPresetForEntity } from '../../core/persistence.js';
|
import { saveChatData, loadChatData, autoSwitchPresetForEntity, getSwipeData } from '../../core/persistence.js';
|
||||||
import { i18n } from '../../core/i18n.js';
|
import { i18n } from '../../core/i18n.js';
|
||||||
|
|
||||||
// Generation & Parsing
|
// Generation & Parsing
|
||||||
@@ -67,9 +67,8 @@ export function commitTrackerData() {
|
|||||||
const message = chat[i];
|
const message = chat[i];
|
||||||
if (!message.is_user) {
|
if (!message.is_user) {
|
||||||
// Found last assistant message - commit its tracker data
|
// Found last assistant message - commit its tracker data
|
||||||
if (message.extra && message.extra.rpg_companion_swipes) {
|
|
||||||
const swipeId = message.swipe_id || 0;
|
const swipeId = message.swipe_id || 0;
|
||||||
const swipeData = message.extra.rpg_companion_swipes[swipeId];
|
const swipeData = getSwipeData(message, swipeId);
|
||||||
|
|
||||||
if (swipeData) {
|
if (swipeData) {
|
||||||
// console.log('[RPG Companion] Committing tracker data from assistant message at index', i, 'swipe', swipeId);
|
// console.log('[RPG Companion] Committing tracker data from assistant message at index', i, 'swipe', swipeId);
|
||||||
@@ -77,10 +76,10 @@ export function commitTrackerData() {
|
|||||||
committedTrackerData.infoBox = swipeData.infoBox || null;
|
committedTrackerData.infoBox = swipeData.infoBox || null;
|
||||||
committedTrackerData.characterThoughts = swipeData.characterThoughts || null;
|
committedTrackerData.characterThoughts = swipeData.characterThoughts || null;
|
||||||
} else {
|
} else {
|
||||||
// console.log('[RPG Companion] No swipe data found for swipe', swipeId);
|
// No saved swipe data — treat as empty (e.g. first message, no prior generation)
|
||||||
}
|
committedTrackerData.userStats = null;
|
||||||
} else {
|
committedTrackerData.infoBox = null;
|
||||||
// console.log('[RPG Companion] No RPG data found in last assistant message');
|
committedTrackerData.characterThoughts = null;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -386,12 +385,12 @@ export function onMessageSwiped(messageIndex) {
|
|||||||
|
|
||||||
// console.log('[RPG Companion] Loading data for swipe', currentSwipeId);
|
// console.log('[RPG Companion] Loading data for swipe', currentSwipeId);
|
||||||
|
|
||||||
// IMPORTANT: onMessageSwiped is for DISPLAY only!
|
// Load saved swipe data into both display (lastGeneratedData) and extensionSettings.
|
||||||
// lastGeneratedData is for DISPLAY, committedTrackerData is for GENERATION
|
// Safe to call parseUserStats() unconditionally because updateMessageSwipeData() is called
|
||||||
// It's safe to load swipe data into lastGeneratedData - it won't be committed due to !lastActionWasSwipe check
|
// on every manual edit, so the swipe store always reflects the latest user changes before
|
||||||
if (message.extra && message.extra.rpg_companion_swipes && message.extra.rpg_companion_swipes[currentSwipeId]) {
|
// any navigation can overwrite them.
|
||||||
const swipeData = message.extra.rpg_companion_swipes[currentSwipeId];
|
const swipeData = getSwipeData(message, currentSwipeId);
|
||||||
|
if (swipeData) {
|
||||||
// Load swipe data into lastGeneratedData for display (both modes)
|
// Load swipe data into lastGeneratedData for display (both modes)
|
||||||
lastGeneratedData.userStats = swipeData.userStats || null;
|
lastGeneratedData.userStats = swipeData.userStats || null;
|
||||||
lastGeneratedData.infoBox = swipeData.infoBox || null;
|
lastGeneratedData.infoBox = swipeData.infoBox || null;
|
||||||
@@ -403,13 +402,12 @@ export function onMessageSwiped(messageIndex) {
|
|||||||
lastGeneratedData.characterThoughts = swipeData.characterThoughts || null;
|
lastGeneratedData.characterThoughts = swipeData.characterThoughts || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// DON'T parse user stats when loading swipe data
|
// Sync extensionSettings.userStats so stat bars reflect this swipe
|
||||||
// This would overwrite manually edited fields (like Conditions) with old swipe data
|
if (swipeData.userStats) {
|
||||||
// The lastGeneratedData is loaded for display purposes only
|
parseUserStats(swipeData.userStats);
|
||||||
// parseUserStats() updates extensionSettings.userStats which should only be modified
|
}
|
||||||
// by new generations or manual edits, not by swipe navigation
|
|
||||||
|
|
||||||
// console.log('[RPG Companion] 🔄 Loaded swipe data into lastGeneratedData for display:', currentSwipeId);
|
// console.log('[RPG Companion] 🔄 Loaded swipe data for swipe:', currentSwipeId);
|
||||||
} else {
|
} else {
|
||||||
// console.log('[RPG Companion] ℹ️ No stored data for swipe:', currentSwipeId);
|
// console.log('[RPG Companion] ℹ️ No stored data for swipe:', currentSwipeId);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user