feat: message interception

This commit is contained in:
Subarashimo
2025-12-05 11:40:50 +01:00
parent 271c69ec49
commit 806a7078a7
12 changed files with 374 additions and 69 deletions
+9 -5
View File
@@ -155,6 +155,10 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
// 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 || '';
// 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
@@ -174,7 +178,7 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
lastMessage.extra.rpg_companion_swipes[currentSwipeId] = {
userStats: parsedData.userStats,
infoBox: parsedData.infoBox,
characterThoughts: parsedData.characterThoughts
characterThoughts: parsedCharacterThoughts
};
// console.log('[RPG Companion] Stored separate mode RPG data for message swipe', currentSwipeId);
@@ -190,9 +194,7 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
if (parsedData.infoBox) {
lastGeneratedData.infoBox = parsedData.infoBox;
}
if (parsedData.characterThoughts) {
lastGeneratedData.characterThoughts = parsedData.characterThoughts;
}
lastGeneratedData.characterThoughts = parsedCharacterThoughts;
// console.log('[RPG Companion] 💾 SEPARATE MODE: Updated lastGeneratedData:', {
// userStats: lastGeneratedData.userStats ? 'exists' : 'null',
// infoBox: lastGeneratedData.infoBox ? 'exists' : 'null',
@@ -211,13 +213,14 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
if (!hasAnyCommittedContent) {
committedTrackerData.userStats = parsedData.userStats;
committedTrackerData.infoBox = parsedData.infoBox;
committedTrackerData.characterThoughts = parsedData.characterThoughts;
committedTrackerData.characterThoughts = parsedCharacterThoughts;
// console.log('[RPG Companion] 🔆 FIRST TIME: Auto-committed tracker data');
}
// Render the updated data
renderUserStats();
renderInfoBox();
lastGeneratedData.characterThoughts = parsedCharacterThoughts;
renderThoughts();
renderInventory();
renderQuests();
@@ -228,6 +231,7 @@ export async function updateRPGData(renderUserStats, renderInfoBox, renderThough
}
renderUserStats();
renderInfoBox();
lastGeneratedData.characterThoughts = parsedCharacterThoughts;
renderThoughts();
renderInventory();
renderQuests();
+44 -39
View File
@@ -4,7 +4,7 @@
* Supports both legacy text format and new JSON format
*/
import { extensionSettings, FEATURE_FLAGS, addDebugLog, lastGeneratedData } from '../../core/state.js';
import { extensionSettings, FEATURE_FLAGS, addDebugLog, lastGeneratedData, committedTrackerData } from '../../core/state.js';
import { saveSettings, saveChatData } from '../../core/persistence.js';
import { extractInventory } from './inventoryParser.js';
import { validateTrackerData, mergeTrackerData } from '../../types/trackerData.js';
@@ -317,47 +317,52 @@ export function parseJSONTrackerData(jsonData) {
}
// Parse characters - store for UI rendering AND generate text format for thought bubbles
if (jsonData.characters && Array.isArray(jsonData.characters)) {
extensionSettings.charactersData = jsonData.characters;
debugLog('[RPG Parser] Characters:', jsonData.characters.length);
const parsedCharacters = Array.isArray(jsonData.characters) ? jsonData.characters : [];
extensionSettings.charactersData = parsedCharacters;
debugLog('[RPG Parser] Characters:', parsedCharacters.length);
// Generate text format for lastGeneratedData.characterThoughts (needed for thought bubbles)
const config = extensionSettings.trackerConfig?.presentCharacters;
const thoughtsFieldName = config?.thoughts?.name || 'Thoughts';
const lines = [];
for (const char of parsedCharacters) {
// Character name line
lines.push(`- ${char.name || 'Unknown'}`);
// Generate text format for lastGeneratedData.characterThoughts (needed for thought bubbles)
const config = extensionSettings.trackerConfig?.presentCharacters;
const thoughtsFieldName = config?.thoughts?.name || 'Thoughts';
const lines = [];
for (const char of jsonData.characters) {
// Character name line
lines.push(`- ${char.name || 'Unknown'}`);
// Details line with emoji and fields
const details = [char.emoji || '😶'];
const charFields = char.fields || {};
for (const [key, value] of Object.entries(charFields)) {
if (value) details.push(`${key}: ${value}`);
}
lines.push(`Details: ${details.join(' | ')}`);
// Relationship line
if (char.relationship) {
lines.push(`Relationship: ${char.relationship}`);
}
// Stats line
const charStats = char.stats || {};
if (Object.keys(charStats).length > 0) {
const statsStr = Object.entries(charStats).map(([k, v]) => `${k}: ${v}%`).join(' | ');
lines.push(`Stats: ${statsStr}`);
}
// Thoughts line
if (char.thoughts) {
lines.push(`${thoughtsFieldName}: ${char.thoughts}`);
}
// Details line with emoji and fields
const details = [char.emoji || '😶'];
const charFields = char.fields || {};
for (const [key, value] of Object.entries(charFields)) {
if (value) details.push(`${key}: ${value}`);
}
if (lines.length > 0) {
lastGeneratedData.characterThoughts = lines.join('\n');
debugLog('[RPG Parser] Generated text format for characterThoughts');
lines.push(`Details: ${details.join(' | ')}`);
// Relationship line
if (char.relationship) {
lines.push(`Relationship: ${char.relationship}`);
}
// Stats line
const charStats = char.stats || {};
if (Object.keys(charStats).length > 0) {
const statsStr = Object.entries(charStats).map(([k, v]) => `${k}: ${v}%`).join(' | ');
lines.push(`Stats: ${statsStr}`);
}
// Thoughts line
if (char.thoughts) {
lines.push(`${thoughtsFieldName}: ${char.thoughts}`);
}
}
if (lines.length > 0) {
lastGeneratedData.characterThoughts = lines.join('\n');
committedTrackerData.characterThoughts = lines.join('\n');
debugLog('[RPG Parser] Generated text format for characterThoughts');
} else {
// No characters provided in the JSON response - clear any stale state/UI data
lastGeneratedData.characterThoughts = '';
committedTrackerData.characterThoughts = '';
debugLog('[RPG Parser] No characters present; cleared characterThoughts state');
}
// Parse inventory (structured format)
+13 -3
View File
@@ -24,11 +24,17 @@ export const DEFAULT_HTML_PROMPT = `If appropriate, include inline HTML, CSS, an
*/
export const DEFAULT_JSON_TRACKER_PROMPT = `At the start of every reply, output a JSON object inside a markdown code fence (with \`\`\`json). This tracks {{user}}'s stats, inventory, skills, and scene information. Follow the exact schema shown below. Use concrete values - no placeholders or brackets. Update stats realistically based on actions and time (0% change for minutes, 1-5% normally, 5%+ only for major events). Items and skills have "name" and "description" fields. Items can grant skills via "grantsSkill", and skills show their source via "grantedBy".`;
/**
* Default message interception prompt text
* Guides the LLM to rewrite the user's message based on current RPG state and recent chat
*/
export const DEFAULT_MESSAGE_INTERCEPTION_PROMPT = `Act as an uncompromising Immersive Copy Editor who rewrites the user's draft to strictly adhere to {{user}}'s persona and RPG state (JSON). You must validate the feasibility of the user's intended actions against the JSON state; if the draft contradicts the state (e.g., acting smart while 'Intelligence' is low, or running while having a 'Leg Injury'), you are required to override the core intent, rewriting the action to portray immediate failure, struggle, or involuntary reaction instead of the user's desired success. Even further, if the intended course of action is physically impossible via the state or represents a thought process conceptually alien to the character's nature or current state, you are mandated to completely overwrite the user's intent. Aggressively rephrase vocabulary and syntax to match the character's specific cognitive capacity and tone. Keep the output concise and devoid of fluff; do not expand the narrative beyond the necessary state-enforced correction. Return ONLY the modified message text.`;
/**
* Gets character card information for current chat (handles both single and group chats)
* @returns {string} Formatted character information
*/
async function getCharacterCardsInfo() {
export async function getCharacterCardsInfo() {
let characterInfo = '';
// Check if in group chat
@@ -196,6 +202,7 @@ export function generateJSONTrackerInstructions(includeHtmlPrompt = true, includ
const showSkills = extensionSettings.showSkills;
const showQuests = extensionSettings.showQuests;
const enableItemSkillLinks = extensionSettings.enableItemSkillLinks;
const deleteSkillWithItem = extensionSettings.deleteSkillWithItem;
const hasAnyTrackers = showStats || showInfoBox || showCharacters || showInventory || showSkills || showQuests;
@@ -400,8 +407,9 @@ export function generateJSONTrackerInstructions(includeHtmlPrompt = true, includ
instructions += '- Level is a numeric value (typically 1+, represents character progression)\n';
}
instructions += '- Characters should be removed as soon as they leave the scene\n';
instructions += '- Your list of characters must never include {{user}}\n';
instructions += '- Empty arrays [] for sections with no items\n';
instructions += '- Items may be added or removed from all sections\n';
instructions += '- null for main quest if none active\n';
// Add stat descriptions if any have descriptions
@@ -438,7 +446,9 @@ export function generateJSONTrackerInstructions(includeHtmlPrompt = true, includ
if (enableItemSkillLinks) {
instructions += '- Items can grant skills: add {"grantsSkill": "Skill Name"} to the item object\n';
instructions += '- When a skill comes from an item, add {"grantedBy": "Item Name"} to that skill object\n';
instructions += '- If an item is removed/lost, also remove any skill it granted\n';
if (deleteSkillWithItem) {
instructions += '- If an item is removed/lost, also remove any skill it granted\n';
}
}
instructions += '\n';