From 806a7078a76230845969b60e27532687d279b116 Mon Sep 17 00:00:00 2001 From: Subarashimo Date: Fri, 5 Dec 2025 11:40:50 +0100 Subject: [PATCH] feat: message interception --- index.js | 130 +++++++++++++++++++++++- src/core/config.js | 3 + src/core/state.js | 4 + src/i18n/en.json | 11 +- src/i18n/zh-tw.json | 11 +- src/systems/generation/apiClient.js | 14 ++- src/systems/generation/parser.js | 83 ++++++++------- src/systems/generation/promptBuilder.js | 16 ++- src/systems/integration/sillytavern.js | 117 +++++++++++++++++++-- src/systems/rendering/thoughts.js | 3 +- src/systems/ui/trackerEditor.js | 15 ++- template.html | 36 +++++++ 12 files changed, 374 insertions(+), 69 deletions(-) diff --git a/index.js b/index.js index f14bc7d..8422091 100644 --- a/index.js +++ b/index.js @@ -119,7 +119,7 @@ import { setupClassicStatsButtons } from './src/systems/features/classicStats.js import { ensureHtmlCleaningRegex, detectConflictingRegexScripts } from './src/systems/features/htmlCleaning.js'; import { setupMemoryRecollectionButton, updateMemoryRecollectionButton } from './src/systems/features/memoryRecollection.js'; import { initLorebookLimiter } from './src/systems/features/lorebookLimiter.js'; -import { DEFAULT_HTML_PROMPT, DEFAULT_JSON_TRACKER_PROMPT } from './src/systems/generation/promptBuilder.js'; +import { DEFAULT_HTML_PROMPT, DEFAULT_JSON_TRACKER_PROMPT, DEFAULT_MESSAGE_INTERCEPTION_PROMPT } from './src/systems/generation/promptBuilder.js'; // Integration modules import { @@ -170,6 +170,95 @@ function updateDynamicLabels() { // Update mobile tab labels updateMobileTabLabels(); + + // Update inline interception toggle text if present + updateInterceptionToggleState(); +} + +/** + * Updates the inline interception toggle text and styling near the send form. + */ +function updateInterceptionToggleState() { + const $toggle = $('#rpg-interception-toggle'); + if ($toggle.length === 0) { + return; + } + + const active = extensionSettings.messageInterceptionActive !== false; + const labelKey = active + ? 'template.settingsModal.advanced.interceptionOn' + : 'template.settingsModal.advanced.interceptionOff'; + const label = i18n.getTranslation(labelKey) || (active ? 'Interception On' : 'Interception Off'); + const prefix = i18n.getTranslation('template.settingsModal.advanced.interceptionModeLabel') || 'Interception:'; + const icon = active ? 'fa-bolt' : 'fa-ban'; + const background = active ? '#4a90e2' : '#666'; + + $toggle + .css({ + 'background-color': background, + color: '#fff' + }) + .html(` ${prefix} ${label}`); +} + +/** + * Shows/hides the inline interception toggle based on interception setting. + */ +function updateInterceptionToggleVisibility() { + const $toggle = $('#rpg-interception-toggle'); + if ($toggle.length === 0) { + return; + } + + $toggle.toggle(extensionSettings.enableMessageInterception); + + if (extensionSettings.enableMessageInterception) { + updateInterceptionToggleState(); + } +} + +/** + * Ensures the extension buttons wrapper exists above the send form. + */ +function ensureExtensionButtonsWrapper() { + if ($('#extension-buttons-wrapper').length === 0) { + $('#send_form').prepend('
'); + } +} + +/** + * Renders the inline interception toggle near plot buttons. + */ +function renderInterceptionToggle() { + ensureExtensionButtonsWrapper(); + + if ($('#rpg-interception-toggle').length === 0) { + const buttonHtml = ` + + `; + $('#extension-buttons-wrapper').append(buttonHtml); + + $('#rpg-interception-toggle').on('click', () => { + const active = extensionSettings.messageInterceptionActive !== false; + extensionSettings.messageInterceptionActive = !active; + saveSettings(); + updateInterceptionToggleState(); + }); + } + + updateInterceptionToggleVisibility(); } /** @@ -285,6 +374,14 @@ async function initUI() { saveSettings(); }); + $('#rpg-message-interception-depth').on('change', function() { + const value = parseInt(String($(this).val())); + if (!Number.isNaN(value)) { + extensionSettings.messageInterceptionContextDepth = value; + saveSettings(); + } + }); + $('#rpg-memory-messages').on('change', function() { const value = $(this).val(); extensionSettings.memoryMessagesToProcess = parseInt(String(value)); @@ -409,6 +506,24 @@ async function initUI() { toastr.success('HTML prompt restored to default'); }); + $('#rpg-toggle-message-interception').on('change', function() { + extensionSettings.enableMessageInterception = $(this).prop('checked'); + saveSettings(); + updateInterceptionToggleVisibility(); + }); + + $('#rpg-custom-message-interception-prompt').on('input', function() { + extensionSettings.customMessageInterceptionPrompt = $(this).val().trim(); + saveSettings(); + }); + + $('#rpg-restore-default-message-interception-prompt').on('click', function() { + extensionSettings.customMessageInterceptionPrompt = ''; + $('#rpg-custom-message-interception-prompt').val(DEFAULT_MESSAGE_INTERCEPTION_PROMPT); + saveSettings(); + toastr.success('Message interception prompt restored to default'); + }); + // Custom Tracker Prompt handlers $('#rpg-custom-tracker-prompt').on('input', function() { extensionSettings.customTrackerPrompt = $(this).val().trim(); @@ -529,6 +644,8 @@ async function initUI() { $('#rpg-toggle-thoughts-in-chat').prop('checked', extensionSettings.showThoughtsInChat); $('#rpg-toggle-always-show-bubble').prop('checked', extensionSettings.alwaysShowThoughtBubble); $('#rpg-toggle-html-prompt').prop('checked', extensionSettings.enableHtmlPrompt); + $('#rpg-toggle-message-interception').prop('checked', extensionSettings.enableMessageInterception); + updateInterceptionToggleVisibility(); // Set default HTML prompt as actual text if no custom prompt exists $('#rpg-custom-html-prompt').val(extensionSettings.customHtmlPrompt || DEFAULT_HTML_PROMPT); @@ -536,6 +653,16 @@ async function initUI() { // Set default tracker prompt as actual text if no custom prompt exists $('#rpg-custom-tracker-prompt').val(extensionSettings.customTrackerPrompt || DEFAULT_JSON_TRACKER_PROMPT); + // Set default message interception prompt as actual text if no custom prompt exists + $('#rpg-custom-message-interception-prompt').val( + extensionSettings.customMessageInterceptionPrompt || DEFAULT_MESSAGE_INTERCEPTION_PROMPT + ); + + // Message interception depth + $('#rpg-message-interception-depth').val( + extensionSettings.messageInterceptionContextDepth || extensionSettings.updateDepth || 4 + ); + $('#rpg-toggle-plot-buttons').prop('checked', extensionSettings.enablePlotButtons); $('#rpg-toggle-animations').prop('checked', extensionSettings.enableAnimations); $('#rpg-stat-bar-color-low').val(extensionSettings.statBarColorLow); @@ -581,6 +708,7 @@ async function initUI() { initTrackerEditor(); addDiceQuickReply(); setupPlotButtons(sendPlotProgression); + renderInterceptionToggle(); setupMobileKeyboardHandling(); setupContentEditableScrolling(); initInventoryEventListeners(); diff --git a/src/core/config.js b/src/core/config.js index 4901cec..c39de4c 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -25,6 +25,7 @@ export const defaultSettings = { enabled: true, autoUpdate: true, updateDepth: 4, // How many messages to include in the context + messageInterceptionContextDepth: 4, // How many recent messages to send when intercepting user messages generationMode: 'together', // 'separate' or 'together' - whether to generate with main response or separately useSeparatePreset: false, // Use 'RPG Companion Trackers' preset for tracker generation instead of main API model showUserStats: true, @@ -34,6 +35,8 @@ export const defaultSettings = { showThoughtsInChat: true, // Show thoughts overlay in chat alwaysShowThoughtBubble: false, // Auto-expand thought bubble without clicking icon enableHtmlPrompt: false, // Enable immersive HTML prompt injection + enableMessageInterception: false, // Enable intercepting user messages with LLM rewrite + messageInterceptionActive: true, // Runtime toggle to allow/skip interception // Controls when the extension skips injecting tracker instructions/examples/HTML // into generations that appear to be user-injected instructions. Valid values: // - 'none' -> never skip (legacy behavior: always inject) diff --git a/src/core/state.js b/src/core/state.js index 4e11e26..2b49b78 100644 --- a/src/core/state.js +++ b/src/core/state.js @@ -13,6 +13,7 @@ export let extensionSettings = { enabled: true, autoUpdate: true, updateDepth: 4, // How many messages to include in the context + messageInterceptionContextDepth: 4, // How many recent messages to send when intercepting user messages generationMode: 'together', // 'separate' or 'together' - whether to generate with main response or separately useSeparatePreset: false, // Use 'RPG Companion Trackers' preset for tracker generation instead of main API model showUserStats: true, @@ -28,6 +29,9 @@ export let extensionSettings = { enableHtmlPrompt: false, // Enable immersive HTML prompt injection customHtmlPrompt: '', // Custom HTML prompt text (empty = use default) customTrackerPrompt: '', // Custom tracker instruction prompt (empty = use default) + enableMessageInterception: false, // Enable intercepting user messages with LLM rewrite + messageInterceptionActive: true, // Runtime toggle to allow/skip interception + customMessageInterceptionPrompt: '', // Custom prompt for message interception (empty = default) skipInjectionsForGuided: 'none', // skip injections for instruct injections and quiet prompts (GuidedGenerations compatibility) enablePlotButtons: true, // Show plot progression buttons above chat input panelPosition: 'right', // 'left', 'right', or 'top' diff --git a/src/i18n/en.json b/src/i18n/en.json index 6b9130f..f506dbe 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -65,6 +65,16 @@ "template.settingsModal.advanced.skipInjectionsOptions.impersonation": "Only on impersonation requests", "template.settingsModal.advanced.skipInjectionsOptions.guided": "Always for guided or quiet prompts", "template.settingsModal.advanced.skipInjectionsNote": "When set, the extension will not inject tracker prompts, examples, or HTML instructions according to the selected mode when a guided generation (via `instruct` or `quiet_prompt`) is detected. Useful when using GuidedGenerations or similar extensions.", + "template.settingsModal.advanced.enableMessageInterception": "Intercept & rewrite user messages with AI", + "template.settingsModal.advanced.enableMessageInterceptionNote": "When enabled, user messages are sent to the AI and rewritten in-place.", + "template.settingsModal.advanced.interceptionModeLabel": "Interception:", + "template.settingsModal.advanced.interceptionOn": "On", + "template.settingsModal.advanced.interceptionOff": "Off", + "template.settingsModal.advanced.messageInterceptionDepth": "Interception Context Messages:", + "template.settingsModal.advanced.messageInterceptionDepthNote": "How many recent messages to send with the interception prompt.", + "template.settingsModal.advanced.customMessageInterceptionPromptTitle": "Custom Message Interception Prompt:", + "template.settingsModal.advanced.restoreDefaultMessageInterceptionPrompt": "Restore Default", + "template.settingsModal.advanced.customMessageInterceptionPromptNote": "Customize the instructions sent to the AI when rewriting user messages. Leave empty to use the default guidance. The AI receives this prompt, the current RPG state JSON, and the recent messages you specify above.", "template.settingsModal.advanced.customTrackerPromptTitle": "Custom Tracker Prompt:", "template.settingsModal.advanced.restoreDefaultTrackerPrompt": "Restore Default", "template.settingsModal.advanced.customTrackerPromptNote": "Customize the instructions sent to the AI for generating tracker data. Use {{user}} as a placeholder for the user's name. This is the main prompt that tells the AI how to format and update the RPG trackers.", @@ -88,7 +98,6 @@ "template.trackerEditorModal.userStatsTab.alwaysIncludeAttributes": "Always Include Attributes in Prompt", "template.trackerEditorModal.userStatsTab.alwaysIncludeAttributesNote": "If disabled, attributes are only sent when a dice roll is active.", "template.trackerEditorModal.userStatsTab.allowAIUpdateAttributes": "Allow AI to Update RPG Attributes", - "template.trackerEditorModal.userStatsTab.allowAIUpdateAttributesNote": "If enabled, the AI can update attribute values and level from its responses. If disabled, attributes are read-only and can only be changed manually.", "template.trackerEditorModal.userStatsTab.addAttributeButton": "Add Attribute", "template.trackerEditorModal.userStatsTab.statusSectionTitle": "Status Section", "template.trackerEditorModal.userStatsTab.enableStatusSection": "Enable Status Section", diff --git a/src/i18n/zh-tw.json b/src/i18n/zh-tw.json index b94b1aa..af6687e 100644 --- a/src/i18n/zh-tw.json +++ b/src/i18n/zh-tw.json @@ -65,6 +65,16 @@ "template.settingsModal.advanced.skipInjectionsOptions.impersonation": "僅在模擬請求時跳過", "template.settingsModal.advanced.skipInjectionsOptions.guided": "始終跳過引導", "template.settingsModal.advanced.skipInjectionsNote": "當設置後,擴充功能在檢測到引導生成(通過 `instruct` 或 `quiet_prompt`)時,將根據所選模式不注入追蹤提示詞、範例或 HTML 指令。當與 GuidedGenerations 或類似擴充功能一起使用時非常有用。", + "template.settingsModal.advanced.enableMessageInterception": "以 AI 攔截並改寫使用者訊息", + "template.settingsModal.advanced.enableMessageInterceptionNote": "啟用後,使用者訊息會連同當前 RPG 狀態與近期聊天內容一起送交 AI,然後在原位改寫。", + "template.settingsModal.advanced.interceptionModeLabel": "攔截模式:", + "template.settingsModal.advanced.interceptionOn": "開啟", + "template.settingsModal.advanced.interceptionOff": "關閉", + "template.settingsModal.advanced.messageInterceptionDepth": "攔截時的上下文訊息數:", + "template.settingsModal.advanced.messageInterceptionDepthNote": "攔截時要附帶的近期訊息數量。", + "template.settingsModal.advanced.customMessageInterceptionPromptTitle": "自訂訊息攔截提示:", + "template.settingsModal.advanced.restoreDefaultMessageInterceptionPrompt": "恢復預設", + "template.settingsModal.advanced.customMessageInterceptionPromptNote": "自訂 AI 改寫使用者訊息時的指令。留空則使用預設指引。AI 會收到這段提示詞、目前的 RPG 狀態 JSON,以及上方設定的近期訊息。", "template.settingsModal.advanced.customTrackerPromptTitle": "自訂追蹤器提示詞:", "template.settingsModal.advanced.restoreDefaultTrackerPrompt": "恢復預設", "template.settingsModal.advanced.customTrackerPromptNote": "自訂發送給 AI 生成追蹤器數據的指令。使用 {{user}} 作為使用者名稱的佔位符。這是告訴 AI 如何格式化和更新 RPG 追蹤器的主要提示詞。", @@ -88,7 +98,6 @@ "template.trackerEditorModal.userStatsTab.alwaysIncludeAttributes": "始終發送屬性(prompt)", "template.trackerEditorModal.userStatsTab.alwaysIncludeAttributesNote": "將 RPG 屬性始終包含在提示詞中,即使它們未顯示在面板上也一樣。", "template.trackerEditorModal.userStatsTab.allowAIUpdateAttributes": "允許 AI 更新 RPG 屬性", - "template.trackerEditorModal.userStatsTab.allowAIUpdateAttributesNote": "如果啟用,AI 可以從其 JSON 回應中更新屬性值和等級。如果禁用,屬性為唯讀,只能手動更改。", "template.trackerEditorModal.userStatsTab.addAttributeButton": "添加屬性", "template.trackerEditorModal.userStatsTab.statusSectionTitle": "狀態欄", "template.trackerEditorModal.userStatsTab.enableStatusSection": "啟用狀態欄", diff --git a/src/systems/generation/apiClient.js b/src/systems/generation/apiClient.js index ca049df..df50f83 100644 --- a/src/systems/generation/apiClient.js +++ b/src/systems/generation/apiClient.js @@ -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(); diff --git a/src/systems/generation/parser.js b/src/systems/generation/parser.js index 803765e..ae154cf 100644 --- a/src/systems/generation/parser.js +++ b/src/systems/generation/parser.js @@ -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) diff --git a/src/systems/generation/promptBuilder.js b/src/systems/generation/promptBuilder.js index 7cf787b..e73b131 100644 --- a/src/systems/generation/promptBuilder.js +++ b/src/systems/generation/promptBuilder.js @@ -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'; diff --git a/src/systems/integration/sillytavern.js b/src/systems/integration/sillytavern.js index 7c2aa56..a26fb7f 100644 --- a/src/systems/integration/sillytavern.js +++ b/src/systems/integration/sillytavern.js @@ -4,7 +4,14 @@ */ import { getContext } from '../../../../../../extensions.js'; -import { chat, user_avatar, setExtensionPrompt, extension_prompt_types, updateMessageBlock } from '../../../../../../../script.js'; +import { + chat, + user_avatar, + setExtensionPrompt, + extension_prompt_types, + updateMessageBlock, + generateRaw +} from '../../../../../../../script.js'; // Core modules import { @@ -14,15 +21,14 @@ import { lastActionWasSwipe, isPlotProgression, setLastActionWasSwipe, - setIsPlotProgression, - updateLastGeneratedData, - updateCommittedTrackerData + setIsPlotProgression } from '../../core/state.js'; import { saveChatData, loadChatData } from '../../core/persistence.js'; // Generation & Parsing import { parseResponse, parseUserStats, parseSkills, tryParseJSONResponse } from '../generation/parser.js'; import { updateRPGData } from '../generation/apiClient.js'; +import { generateContextualSummary, DEFAULT_MESSAGE_INTERCEPTION_PROMPT } from '../generation/promptBuilder.js'; // Rendering import { renderUserStats } from '../rendering/userStats.js'; @@ -76,13 +82,22 @@ export function commitTrackerData() { * Sets the flag to indicate this is NOT a swipe. * In separate mode with auto-update disabled, commits the displayed tracker data. */ -export function onMessageSent() { +export async function onMessageSent() { if (!extensionSettings.enabled) return; // User sent a new message - NOT a swipe setLastActionWasSwipe(false); // console.log('[RPG Companion] 🟢 EVENT: onMessageSent - lastActionWasSwipe =', lastActionWasSwipe); + // Optionally intercept and rewrite the user message via LLM + if (extensionSettings.enableMessageInterception && extensionSettings.messageInterceptionActive !== false) { + try { + await interceptAndModifyUserMessage(); + } catch (error) { + console.error('[RPG Companion] Message interception failed:', error); + } + } + // In separate mode with auto-update disabled, commit displayed tracker when user sends a message if (extensionSettings.generationMode === 'separate' && !extensionSettings.autoUpdate) { // Commit whatever is currently displayed in lastGeneratedData @@ -99,6 +114,84 @@ export function onMessageSent() { } } +/** + * Intercepts the last user message, asks the LLM to rewrite it using RPG state and recent chat, + * and updates the chat/DOM with the modified content. + */ +async function interceptAndModifyUserMessage() { + const context = getContext(); + const chatHistory = context.chat || chat; + + if (!chatHistory || chatHistory.length === 0) { + return; + } + + const lastMessage = chatHistory[chatHistory.length - 1]; + if (!lastMessage || !lastMessage.is_user) { + return; // Only rewrite user messages + } + + const originalText = lastMessage.mes || ''; + const stateJson = generateContextualSummary(); + const depth = extensionSettings.messageInterceptionContextDepth || extensionSettings.updateDepth || 4; + const startIndex = Math.max(0, chatHistory.length - 1 - depth); + const recentMessages = chatHistory.slice(startIndex, chatHistory.length - 1); + + const recentContext = recentMessages + .map((m) => { + const role = m.is_system ? 'system' : m.is_user ? '{{user}}' : '{{char}}'; + const content = (m.mes || '').replace(/\s+/g, ' ').trim(); + return `- ${role}: ${content}`; + }) + .join('\n'); + + const basePrompt = + (extensionSettings.customMessageInterceptionPrompt || '').trim() || + DEFAULT_MESSAGE_INTERCEPTION_PROMPT; + + const promptMessages = [ + { + role: 'system', + content: basePrompt + }, + { + role: 'system', + content: `{{user}}'s persona definition:\n{{persona}}` + }, + { + role: 'system', + content: `Current RPG state (JSON):\n${stateJson ? `\`\`\`json\n${stateJson}\n\`\`\`` : 'None'}` + }, + { + role: 'system', + content: `Recent messages (newest last):\n${recentContext || 'None'}` + }, + { + role: 'user', + content: `User draft message:\n${originalText}\n\nReturn only the modified message text.` + } + ]; + + const response = await generateRaw({ + prompt: promptMessages, + quietToLoud: false + }); + + if (!response || typeof response !== 'string') { + return; + } + + const cleaned = response.trim(); + if (!cleaned) { + return; + } + + // Update chat history and DOM + lastMessage.mes = cleaned; + const messageId = chatHistory.length - 1; + updateMessageBlock(messageId, lastMessage, { rerenderMessage: true }); +} + /** * Event handler for when a message is generated. */ @@ -137,6 +230,10 @@ export async function onMessageReceived(data) { const parsedData = parseResponse(responseText); // console.log('[RPG Companion] Parsed data:', parsedData); + // Legacy text parsing does not produce structured characters; clear old state to avoid stale UI/state + extensionSettings.charactersData = []; + const parsedCharacterThoughts = parsedData.characterThoughts || ''; + // Update stored data if (parsedData.userStats) { lastGeneratedData.userStats = parsedData.userStats; @@ -148,9 +245,9 @@ export async function onMessageReceived(data) { if (parsedData.infoBox) { lastGeneratedData.infoBox = parsedData.infoBox; } - if (parsedData.characterThoughts) { - lastGeneratedData.characterThoughts = parsedData.characterThoughts; - } + + // Response omitted characters section - clear any previous thoughts to reflect removal + lastGeneratedData.characterThoughts = parsedCharacterThoughts; // Store RPG data for this specific swipe in the message's extra field if (!lastMessage.extra) { @@ -164,7 +261,7 @@ export async function onMessageReceived(data) { lastMessage.extra.rpg_companion_swipes[currentSwipeId] = { userStats: parsedData.userStats, infoBox: parsedData.infoBox, - characterThoughts: parsedData.characterThoughts + characterThoughts: parsedCharacterThoughts }; // console.log('[RPG Companion] Stored RPG data for swipe', currentSwipeId); @@ -173,7 +270,7 @@ export async function onMessageReceived(data) { if (!committedTrackerData.userStats && !committedTrackerData.infoBox && !committedTrackerData.characterThoughts) { 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'); } else { // console.log('[RPG Companion] Data will be committed when user replies'); diff --git a/src/systems/rendering/thoughts.js b/src/systems/rendering/thoughts.js index 1bed375..1e45f5d 100644 --- a/src/systems/rendering/thoughts.js +++ b/src/systems/rendering/thoughts.js @@ -163,7 +163,8 @@ export function renderThoughts() { const hasRelationshipEnabled = relationshipFields.length > 0; // Convert structured character data to text format for the original fancy renderer - let characterThoughtsData = lastGeneratedData.characterThoughts || committedTrackerData.characterThoughts || ''; + // Use nullish coalescing so an empty string from the latest response clears UI + let characterThoughtsData = lastGeneratedData.characterThoughts ?? committedTrackerData.characterThoughts ?? ''; // If we have structured data, convert it to text format if (extensionSettings.charactersData && Array.isArray(extensionSettings.charactersData) && extensionSettings.charactersData.length > 0) { diff --git a/src/systems/ui/trackerEditor.js b/src/systems/ui/trackerEditor.js index b34b5eb..a895cc4 100644 --- a/src/systems/ui/trackerEditor.js +++ b/src/systems/ui/trackerEditor.js @@ -234,6 +234,13 @@ function renderUserStatsTab() { html += ``; html += ''; + // Allow AI to update attributes toggle + const allowAIUpdateAttributes = config.allowAIUpdateAttributes !== undefined ? config.allowAIUpdateAttributes : true; + html += '
'; + html += ``; + html += ``; + html += '
'; + // Always send attributes toggle const alwaysSendAttributes = config.alwaysSendAttributes !== undefined ? config.alwaysSendAttributes : false; html += '
'; @@ -242,14 +249,6 @@ function renderUserStatsTab() { html += '
'; html += `${i18n.getTranslation('template.trackerEditorModal.userStatsTab.alwaysIncludeAttributesNote')}`; - // Allow AI to update attributes toggle - const allowAIUpdateAttributes = config.allowAIUpdateAttributes !== undefined ? config.allowAIUpdateAttributes : true; - html += '
'; - html += ``; - html += ``; - html += '
'; - html += `${i18n.getTranslation('template.trackerEditorModal.userStatsTab.allowAIUpdateAttributesNote')}`; - html += '
'; // Ensure rpgAttributes exists in the actual config (not just local fallback) diff --git a/template.html b/template.html index 090aed4..1cb8122 100644 --- a/template.html +++ b/template.html @@ -289,6 +289,20 @@ Number of recent messages to include (Separate mode only)
+ + + When enabled, user messages are sent to the AI along with current RPG state and recent chat, then rewritten in-place. + + +
+ + + How many recent messages to send with the interception prompt. +
+
@@ -359,6 +373,28 @@
+ +
+ + + +
+ +
+ + Customize the instructions sent to the AI when rewriting user messages. Leave empty to use the default guidance. The AI receives this prompt, the current RPG state JSON, and the recent messages you specify above. + +
+