feat: auto-import HTML cleaning regex for prompts
- Adds ensureHtmlCleaningRegex() function that automatically imports a regex script - Regex removes HTML tags from outgoing prompts to prevent formatting issues - Only imports if not already present (checks existing scripts by name) - Based on the regex-clean_html_(from_outgoing_prompt).json specification - Runs on extension initialization - Non-blocking: won't fail extension load if regex import fails
This commit is contained in:
@@ -3147,6 +3147,77 @@ function onMessageSwiped(messageIndex) {
|
||||
updateChatThoughts();
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatically imports the HTML cleaning regex script if it doesn't already exist.
|
||||
* This regex removes HTML tags from outgoing prompts to prevent formatting issues.
|
||||
*/
|
||||
async function ensureHtmlCleaningRegex() {
|
||||
try {
|
||||
// Import the regex engine to check existing scripts
|
||||
const { getRegexScripts } = await import('../../../scripts/extensions/regex/engine.js');
|
||||
const existingScripts = getRegexScripts();
|
||||
|
||||
// Check if the HTML cleaning regex already exists
|
||||
const scriptName = 'Clean HTML (From Outgoing Prompt)';
|
||||
const alreadyExists = existingScripts.some(script => script.scriptName === scriptName);
|
||||
|
||||
if (alreadyExists) {
|
||||
console.log('[RPG Companion] HTML cleaning regex already exists, skipping import');
|
||||
return;
|
||||
}
|
||||
|
||||
// Import the regex index to use the import function
|
||||
const regexModule = await import('../../../scripts/extensions/regex/index.js');
|
||||
|
||||
// Create the regex script object based on the attached file
|
||||
const regexScript = {
|
||||
scriptName: scriptName,
|
||||
findRegex: '/\\s?<(?!\\!--)(?:\"[^\"]*\"|\'[^\']*\'|[^\'\">])*>/g',
|
||||
replaceString: '',
|
||||
trimStrings: [],
|
||||
placement: [2], // 2 = Input (affects outgoing prompt)
|
||||
disabled: false,
|
||||
markdownOnly: false,
|
||||
promptOnly: true,
|
||||
runOnEdit: true,
|
||||
substituteRegex: 0,
|
||||
minDepth: null,
|
||||
maxDepth: null
|
||||
};
|
||||
|
||||
// Import using the onRegexImportObjectChange function
|
||||
// We need to access it through the window object or by importing it
|
||||
const { extension_settings } = await import('../../../scripts/extensions.js');
|
||||
|
||||
// Generate a UUID for the script
|
||||
const uuidv4 = () => {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
||||
const r = Math.random() * 16 | 0;
|
||||
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
};
|
||||
|
||||
regexScript.id = uuidv4();
|
||||
|
||||
// Add to global regex scripts
|
||||
if (!Array.isArray(extension_settings.regex)) {
|
||||
extension_settings.regex = [];
|
||||
}
|
||||
|
||||
extension_settings.regex.push(regexScript);
|
||||
|
||||
// Import saveSettingsDebounced to save the changes
|
||||
const { saveSettingsDebounced: saveExtensionSettings } = await import('../../../../script.js');
|
||||
saveExtensionSettings();
|
||||
|
||||
console.log('[RPG Companion] ✅ HTML cleaning regex imported successfully');
|
||||
} catch (error) {
|
||||
console.error('[RPG Companion] Failed to import HTML cleaning regex:', error);
|
||||
// Don't throw - this is a nice-to-have feature
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main initialization function.
|
||||
*/
|
||||
@@ -3159,6 +3230,9 @@ jQuery(async () => {
|
||||
// Load chat-specific data for current chat
|
||||
loadChatData();
|
||||
|
||||
// Import the HTML cleaning regex if needed
|
||||
await ensureHtmlCleaningRegex();
|
||||
|
||||
// Register event listeners
|
||||
eventSource.on(event_types.MESSAGE_SENT, onMessageSent);
|
||||
eventSource.on(event_types.GENERATION_STARTED, onGenerationStarted);
|
||||
|
||||
Reference in New Issue
Block a user