Refactor historical context handling and remove unused initialization function
This commit is contained in:
@@ -55,7 +55,7 @@ import {
|
||||
} from './src/systems/generation/promptBuilder.js';
|
||||
import { parseResponse, parseUserStats } from './src/systems/generation/parser.js';
|
||||
import { updateRPGData, testExternalAPIConnection } from './src/systems/generation/apiClient.js';
|
||||
import { onGenerationStarted, initHistoricalContextInjection } from './src/systems/generation/injector.js';
|
||||
import { onGenerationStarted } from './src/systems/generation/injector.js';
|
||||
|
||||
// Rendering modules
|
||||
import { getSafeThumbnailUrl } from './src/utils/avatars.js';
|
||||
@@ -1031,9 +1031,6 @@ jQuery(async () => {
|
||||
[event_types.USER_MESSAGE_RENDERED]: updatePersonaAvatar,
|
||||
[event_types.SETTINGS_UPDATED]: updatePersonaAvatar
|
||||
});
|
||||
|
||||
// Initialize historical context injection (uses CHAT_COMPLETION_PROMPT_READY event)
|
||||
initHistoricalContextInjection();
|
||||
} catch (error) {
|
||||
console.error('[RPG Companion] Event registration failed:', error);
|
||||
throw error; // This is critical - can't continue without events
|
||||
|
||||
@@ -98,12 +98,21 @@ function buildHistoricalContextMap() {
|
||||
}
|
||||
|
||||
// Get the rpg_companion_swipes data for current swipe
|
||||
const swipeData = message.extra?.rpg_companion_swipes;
|
||||
// Data can be in two places:
|
||||
// 1. message.extra.rpg_companion_swipes (current session, before save)
|
||||
// 2. message.swipe_info[swipeId].extra.rpg_companion_swipes (loaded from file)
|
||||
const currentSwipeId = message.swipe_id || 0;
|
||||
let swipeData = message.extra?.rpg_companion_swipes;
|
||||
|
||||
// If not in message.extra, check swipe_info
|
||||
if (!swipeData && message.swipe_info && message.swipe_info[currentSwipeId]) {
|
||||
swipeData = message.swipe_info[currentSwipeId].extra?.rpg_companion_swipes;
|
||||
}
|
||||
|
||||
if (!swipeData) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const currentSwipeId = message.swipe_id || 0;
|
||||
const trackerData = swipeData[currentSwipeId];
|
||||
if (!trackerData) {
|
||||
continue;
|
||||
@@ -575,13 +584,3 @@ export function onGenerationEndedCleanup() {
|
||||
restoreOriginalMessageContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the historical context injection event listener
|
||||
* This should be called once during extension initialization
|
||||
*/
|
||||
export function initHistoricalContextInjection() {
|
||||
// Historical context injection is now handled directly in onGenerationStarted
|
||||
// by temporarily modifying chat messages. This works for ALL API types.
|
||||
// Restoration happens in onGenerationEndedCleanup.
|
||||
console.log('[RPG Companion] Historical context injection initialized (direct chat modification mode)');
|
||||
}
|
||||
|
||||
@@ -1228,54 +1228,6 @@ export async function generateSeparateUpdatePrompt() {
|
||||
return messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds historical tracker context for AI generation prompts.
|
||||
* Iterates through recent messages and extracts tracker data for persistence-enabled fields.
|
||||
*
|
||||
* @param {number} depth - Number of messages to look back
|
||||
* @param {Object} trackerConfig - The tracker configuration
|
||||
* @param {string} userName - The user's name
|
||||
* @returns {string} Formatted historical context or empty string
|
||||
*/
|
||||
function buildHistoricalContextForGeneration(depth, trackerConfig, userName) {
|
||||
if (!chat || chat.length < 2) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const historyPersistence = extensionSettings.historyPersistence;
|
||||
const messageCount = historyPersistence?.messageCount || 0;
|
||||
const maxMessages = messageCount === 0 ? depth : Math.min(messageCount, depth);
|
||||
|
||||
let historicalContext = '';
|
||||
let processedCount = 0;
|
||||
let messageIndex = 0;
|
||||
|
||||
// Start from older messages and work forward for chronological order
|
||||
const startIndex = Math.max(0, chat.length - 1 - maxMessages);
|
||||
for (let i = startIndex; i < chat.length - 1 && processedCount < maxMessages; i++) {
|
||||
const message = chat[i];
|
||||
const swipeData = message.extra?.rpg_companion_swipes;
|
||||
if (!swipeData) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const currentSwipeId = message.swipe_id || 0;
|
||||
const trackerData = swipeData[currentSwipeId];
|
||||
if (!trackerData) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const formattedContext = formatHistoricalTrackerData(trackerData, trackerConfig, userName);
|
||||
if (formattedContext) {
|
||||
messageIndex++;
|
||||
historicalContext += `[Message ${messageIndex}]\n${formattedContext}\n`;
|
||||
processedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
return historicalContext.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Default custom instruction for avatar prompt generation
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user