diff --git a/index.js b/index.js index 09b35ee..7131b9e 100644 --- a/index.js +++ b/index.js @@ -42,7 +42,7 @@ import { setMusicPlayerContainer, clearSessionAvatarPrompts } from './src/core/state.js'; -import { loadSettings, saveSettings, saveChatData, loadChatData, updateMessageSwipeData } from './src/core/persistence.js'; +import { loadSettings, saveSettings, saveChatData, loadChatData, updateMessageSwipeData, commitTrackerDataFromPriorMessage } from './src/core/persistence.js'; import { registerAllEvents } from './src/core/events.js'; // Generation & Parsing modules @@ -800,6 +800,17 @@ async function initUI() { // console.log('[RPG Companion] Extension is disabled. Please enable it in the Extensions tab.'); return; } + const currentChat = getContext().chat; + let lastAssistantIndex = -1; + for (let i = currentChat.length - 1; i >= 0; i--) { + if (!currentChat[i].is_user && !currentChat[i].is_system) { + lastAssistantIndex = i; + break; + } + } + if (lastAssistantIndex !== -1) { + commitTrackerDataFromPriorMessage(lastAssistantIndex); + } await updateRPGData(renderUserStats, renderInfoBox, renderThoughts, renderInventory); }); @@ -808,6 +819,17 @@ async function initUI() { if (!extensionSettings.enabled) { return; } + const currentChat = getContext().chat; + let lastAssistantIndex = -1; + for (let i = currentChat.length - 1; i >= 0; i--) { + if (!currentChat[i].is_user && !currentChat[i].is_system) { + lastAssistantIndex = i; + break; + } + } + if (lastAssistantIndex !== -1) { + commitTrackerDataFromPriorMessage(lastAssistantIndex); + } await updateRPGData(renderUserStats, renderInfoBox, renderThoughts, renderInventory); }); diff --git a/src/core/persistence.js b/src/core/persistence.js index adeeb53..5cf7c96 100644 --- a/src/core/persistence.js +++ b/src/core/persistence.js @@ -322,6 +322,8 @@ export function commitTrackerDataFromPriorMessage(currentMessageIndex) { return; } + // console.log('[RPG Companion] commitTrackerDataFromPriorMessage called with index', currentMessageIndex, '| chat.length =', chat.length); + for (let i = currentMessageIndex - 1; i >= 0; i--) { const message = chat[i]; if (message.is_user || message.is_system) continue; @@ -329,6 +331,7 @@ export function commitTrackerDataFromPriorMessage(currentMessageIndex) { // Found the prior assistant message — commit its active swipe data const swipeId = message.swipe_id || 0; const swipeData = getSwipeData(message, swipeId); + // console.log('[RPG Companion] Committing from chat[' + i + '] swipe', swipeId, '| has swipe data:', !!swipeData); committedTrackerData.userStats = swipeData?.userStats || null; committedTrackerData.infoBox = swipeData?.infoBox || null; const rawCharacterThoughts = swipeData?.characterThoughts;