mcp-prompt-optimizer-local
Version:
Advanced cross-platform prompt optimization with MCP integration and 120+ optimization rules
844 lines (786 loc) • 91.4 kB
JavaScript
/**
* Optimization Rules Engine - Complete rule set with 120+ optimization rules
* Matches Python implementation from ai_aware_optimization_rules.py
*/
// Temporary fallback for GoalAlignmentCalculator until the module is created
class GoalAlignmentCalculator {
constructor(logger = null) {
this.logger = logger;
this.MIN_GOAL_THRESHOLD = 0.05; // Lowered from 0.08 to match MCP server (more aggressive rule application)
}
/**
* Calculate goal alignment score between user goals and rule goals
* @param {string} ruleName - Name of the rule
* @param {Array<string>} userGoals - User's optimization goals
* @param {Array<string>} ruleGoals - Rule's target goals
* @returns {number} - Alignment score
*/
calculateScore(ruleName, userGoals, ruleGoals) {
if (!userGoals || !ruleGoals || userGoals.length === 0 || ruleGoals.length === 0) {
return 0.3; // Changed from 0.1 - give rules without goals a decent base score
}
// Direct goal matches
const directMatches = userGoals.filter(goal => ruleGoals.includes(goal)).length;
// Enhanced partial credit for related goals
let partialCredit = 0;
const goalRelations = {
'clarity': ['educational-value', 'comprehensiveness', 'structure'],
'comprehensiveness': ['clarity', 'educational-value', 'structure'],
'structure': ['actionability', 'comprehensiveness', 'clarity'],
'technical-precision': ['quality-enhancement', 'safety', 'actionability'],
'actionability': ['structure', 'clarity', 'technical-precision'],
'specificity': ['clarity', 'technical-precision', 'quality-enhancement'],
'quality-enhancement': ['technical-precision', 'specificity', 'comprehensiveness'],
'professionalism': ['structure', 'clarity', 'quality-enhancement'],
'educational-value': ['clarity', 'comprehensiveness', 'analytical-depth'],
'analytical-depth': ['comprehensiveness', 'educational-value'],
'context-specificity': ['clarity', 'educational-value']
};
for (const userGoal of userGoals) {
const relatedGoals = goalRelations[userGoal] || [];
const relatedMatches = ruleGoals.filter(ruleGoal => relatedGoals.includes(ruleGoal)).length;
partialCredit += relatedMatches * 0.7; // Increased from 0.5 - more generous partial credit
}
const totalScore = (directMatches * 1.0) + partialCredit;
return Math.min(totalScore, 3.0);
}
/**
* Get dynamic threshold based on rule and context
* @param {Object} rule - The optimization rule
* @param {string} context - AI context type
* @param {number} sophisticationScore - Sophistication level (0-1)
* @returns {number} - Dynamic threshold value
*/
getDynamicThreshold(rule, context, sophisticationScore) {
let baseThreshold = this.MIN_GOAL_THRESHOLD;
// Adjust threshold based on rule priority
if (rule.priority >= 9) {
baseThreshold *= 0.5; // Changed from 0.7 - more aggressive for high-priority rules
} else if (rule.priority <= 5) {
baseThreshold *= 1.1; // Changed from 1.3 - less restrictive for low-priority rules
}
// Reduce sophistication modifier impact
const sophisticationModifier = 1.0 + (sophisticationScore * 0.2); // Changed from 0.5
baseThreshold *= sophisticationModifier;
// Make context modifiers more permissive
const contextModifiers = {
'technical-automation': 0.7, // Changed from 0.9
'code-generation': 0.7, // Changed from 0.9
'image-generation': 0.6, // Changed from 0.8
'human-communication': 0.9, // Changed from 1.1
'llm-interaction': 0.8 // Changed from 1.0
};
const contextModifier = contextModifiers[context] || 0.8; // Changed default from 1.0
baseThreshold *= contextModifier;
return Math.max(baseThreshold, 0.03); // Changed from 0.05 - lower minimum threshold
}
}
// Try to import the real GoalAlignmentCalculator, fall back to our implementation
let ImportedGoalAlignmentCalculator;
try {
ImportedGoalAlignmentCalculator = require('./goal-alignment').GoalAlignmentCalculator;
} catch (error) {
// Use our fallback implementation
ImportedGoalAlignmentCalculator = GoalAlignmentCalculator;
}
class OptimizationRule {
constructor({
name,
pattern,
replacement,
contexts = [],
goals = [],
priority = 5,
goalWeight = 1.0,
universalApplicability = false,
minSophistication = 0.0,
maxSophistication = 1.0,
optimalSophisticationRange = [0.0, 1.0],
sophisticationThreshold = 0.0,
inverseForAi = false,
requiresCrossSegmentContext = false,
enabled = true,
intentKeywords = null,
intentThreshold = null,
confidenceImpact = 1.0,
description = ''
}) {
this.name = name;
this.pattern = new RegExp(pattern, 'gi');
this.replacement = replacement;
this.contexts = contexts;
this.goals = goals;
this.priority = priority;
this.goalWeight = goalWeight;
this.universalApplicability = universalApplicability;
this.minSophistication = minSophistication;
this.maxSophistication = maxSophistication;
this.optimalSophisticationRange = optimalSophisticationRange;
this.sophisticationThreshold = sophisticationThreshold;
this.inverseForAi = inverseForAi;
this.requiresCrossSegmentContext = requiresCrossSegmentContext;
this.enabled = enabled;
this.intentKeywords = intentKeywords;
this.intentThreshold = intentThreshold;
this.confidenceImpact = confidenceImpact;
this.description = description;
}
/**
* Test if the rule pattern matches the given text
* @param {string} text - Text to test against
* @returns {boolean} - True if pattern matches
*/
matches(text) {
return this.pattern.test(text);
}
/**
* Apply the rule to the given text
* @param {string} text - Text to transform
* @returns {string} - Transformed text
*/
apply(text) {
return text.replace(this.pattern, this.replacement);
}
}
class RulesEngine {
constructor(logger = null) {
this.logger = logger;
this.rules = this._initializeCompleteRules();
this.goalCalculator = new ImportedGoalAlignmentCalculator(logger);
this.ruleMetrics = new Map();
this.MIN_GOAL_THRESHOLD = 0.08; // Fallback threshold
}
/**
* Get rules appropriate for context, sophistication level, and goals
* @param {string} contextType - AI context type
* @param {number} sophisticationLevel - Sophistication score (0-1)
* @param {Array<string>} goals - Optimization goals
* @returns {Array<OptimizationRule>} - Sorted list of appropriate rules
*/
getContextAppropriateRules(contextType, sophisticationLevel, goals = []) {
const appropriateRules = [];
let rulesEvaluated = 0;
let rulesMatched = 0;
for (const rule of this.rules) {
rulesEvaluated++;
// Skip disabled rules
if (!rule.enabled) {
this._logDebug(`Rule ${rule.name}: SKIPPED (disabled)`);
continue;
}
// Enhanced context matching
const contextMatch = (
rule.universalApplicability ||
rule.contexts.includes(contextType) ||
rule.contexts.length === 0
);
if (!contextMatch) {
this._logDebug(`Rule ${rule.name}: SKIPPED (context mismatch)`);
continue;
}
// Check sophistication range
const [minSoph, maxSoph] = rule.optimalSophisticationRange;
if (sophisticationLevel < minSoph || sophisticationLevel > maxSoph) {
this._logDebug(`Rule ${rule.name}: SKIPPED (sophistication ${sophisticationLevel.toFixed(2)} outside range ${minSoph}-${maxSoph})`);
continue;
}
// Calculate goal alignment score
let goalAlignmentScore = 0;
if (goals.length > 0 && rule.goals.length > 0) {
const matchingGoals = goals.filter(g => rule.goals.includes(g)).length;
goalAlignmentScore = matchingGoals * rule.goalWeight;
}
appropriateRules.push({ rule, score: goalAlignmentScore });
rulesMatched++;
this._logDebug(`Rule ${rule.name}: MATCHED (goal_score=${goalAlignmentScore.toFixed(2)}, priority=${rule.priority}, goals=${rule.goals.length})`);
}
// Sort with deterministic tie-breaking (matches Python MCP server)
// Order: Priority DESC → Goal Score DESC → Goal Weight DESC → Name ASC (for determinism)
appropriateRules.sort((a, b) => {
// 1. Priority (descending - higher priority first)
if (a.rule.priority !== b.rule.priority) {
return b.rule.priority - a.rule.priority;
}
// 2. Goal alignment score (descending - better match first)
if (a.score !== b.score) {
return b.score - a.score;
}
// 3. Goal weight (descending - higher weight first)
if (a.rule.goalWeight !== b.rule.goalWeight) {
return b.rule.goalWeight - a.rule.goalWeight;
}
// 4. CRITICAL: Alphabetical tie-breaking for determinism
// When all else is equal, sort by name to ensure consistent ordering
return a.rule.name.localeCompare(b.rule.name);
});
const finalRules = appropriateRules.map(item => item.rule);
this._logDebug(`Rule selection complete: ${rulesEvaluated} evaluated, ${rulesMatched} matched, ${finalRules.length} selected`);
return finalRules;
}
/**
* Apply optimization rules to text
* @param {string} text - Text to optimize
* @param {string} context - AI context type
* @param {Array<string>} goals - Optimization goals
* @returns {Object} - Optimization result
*/
async optimizeForContext(text, context, goals) {
const startTime = Date.now();
this._logInfo(`Starting optimization. Context: ${context}, Goals: ${goals.join(', ')}`);
// Calculate sophistication score
const sophisticationScore = this._detectPromptSophistication(text);
// Get appropriate rules
const selectedRules = this.getContextAppropriateRules(context, sophisticationScore, goals);
this._logInfo(`Selected ${selectedRules.length} rules for evaluation: ${selectedRules.slice(0, 5).map(r => r.name).join(', ')}`);
const appliedRules = [];
const skippedRules = [];
const failedRules = [];
let currentText = text;
let confidenceModifier = 1.0;
for (const rule of selectedRules) {
try {
// Calculate goal score
const goalScore = this.goalCalculator.calculateScore(rule.name, goals, rule.goals);
// Use dynamic threshold
const dynamicThreshold = this.goalCalculator.getDynamicThreshold(rule, context, sophisticationScore);
if (goalScore < dynamicThreshold) {
this._trackRuleMetric(rule.name, 'skipped');
skippedRules.push({
name: rule.name,
reason: `goal_score=${goalScore.toFixed(2)} below dynamic_threshold=${dynamicThreshold.toFixed(2)}`
});
this._logInfo(`Rule ${rule.name}: SKIPPED (goal_score=${goalScore.toFixed(2)} below dynamic_threshold=${dynamicThreshold.toFixed(2)})`);
}
// Apply rule to ORIGINAL text to allow multiple rules to match
const enhancedText = rule.apply(currentText);
// Validate result with context-aware limits
if (currentText !== enhancedText && this._validateRuleApplication(text, currentText, enhancedText, rule.name, context)) {
currentText = enhancedText;
appliedRules.push(rule.name);
this._trackRuleMetric(rule.name, 'applied');
confidenceModifier *= rule.confidenceImpact;
this._logInfo(`Rule ${rule.name}: APPLIED (goal_score=${goalScore.toFixed(2)})`);
// Continue to apply more rules - don't break the loop
// The next rule will be evaluated against the newly optimized text
} else if (currentText !== enhancedText) {
this._trackRuleMetric(rule.name, 'failed');
failedRules.push({
name: rule.name,
reason: 'validation_failed'
});
this._logWarning(`Rule ${rule.name}: VALIDATION_FAILED`);
}
} catch (error) {
this._trackRuleMetric(rule.name, 'failed');
failedRules.push({
name: rule.name,
reason: `exception: ${error.message}`
});
this._logError(`Rule ${rule.name} application failed: ${error.message}`);
continue;
}
}
const processingTime = Date.now() - startTime;
this._logInfo(`Optimization complete. Applied ${appliedRules.length} rules, skipped ${skippedRules.length}, failed ${failedRules.length} | Time: ${processingTime}ms`);
if (appliedRules.length > 0) {
this._logDebug(`Applied rules: ${appliedRules.join(', ')}`);
}
return {
optimizedText: currentText,
appliedRules,
skippedRules,
failedRules,
confidenceModifier,
processingTime,
metadata: {
totalRulesEvaluated: selectedRules.length,
sophisticationScore,
context,
goals
}
};
}
/**
* Initialize complete rule set (120+ rules matching Python implementation)
* @returns {Array<OptimizationRule>} - Complete rule set
*/
_initializeCompleteRules() {
const foundation_rules = [];
const hotfix_rules = [];
const technical_rules = [];
const business_rules = [];
const academic_rules = [];
const image_rules = [];
const basic_rules = [];
const intermediate_rules = [];
const advanced_rules = [];
const expert_rules = [];
const parameter_rules = [];
// FOUNDATION RULES (Universal application)
foundation_rules.push(
new OptimizationRule({
name: "foundation_normalize_whitespace",
pattern: "[ \\t]{2,}",
replacement: " ",
contexts: [],
goals: [],
priority: 10,
universalApplicability: true,
description: "Collapse multiple spaces/tabs into single space",
optimalSophisticationRange: [0.0, 1.0]
}),
new OptimizationRule({
name: "foundation_remove_filler_um",
pattern: "^\\s*(?:um|uh|er|ah)\\s*[,.]?\\s*",
replacement: "",
contexts: [],
goals: [],
priority: 10,
universalApplicability: true,
description: "Remove leading conversational fillers",
optimalSophisticationRange: [0.0, 1.0]
})
);
// ========== HIGH PRIORITY: MINIMAL INPUT & DEBUG (PRIORITY 9-10) ==========
// These rules must come BEFORE other hotfix rules to ensure proper matching
hotfix_rules.push(
// CRITICAL: Minimal input enhancement (matches MCP server behavior)
new OptimizationRule({
name: "enhance-minimal-input-greeting",
pattern: "^\\s*(hi|hello|hey|help|assist|start|begin)(?:\\s|[.!?]|$)",
replacement: `I need assistance. Please help me with the following:
**What I'm trying to accomplish:**
[Describe your goal or objective]
**Specific requirements or constraints:**
[List any specific needs, limitations, or preferences]
**Expected outcome:**
[Describe what success looks like]
**Additional context:**
[Provide any relevant background information]`,
contexts: ["llm-interaction"],
goals: ["clarity", "comprehensiveness", "structure"],
priority: 10, // HIGHEST PRIORITY
goalWeight: 3.0,
description: "Transforms minimal/vague input into structured request template",
optimalSophisticationRange: [0.0, 1.0],
confidenceImpact: 1.0,
enabled: true
}),
// CRITICAL: Debug template enhancement (matches MCP server DEBUG_TEMPLATE route)
new OptimizationRule({
name: "enhance-debug-request",
pattern: "^(.*?\\b(?:debug|fix|error|exception|traceback|stack trace|not working|broken|failing|crashes)\\b.*)$",
replacement: `Help debug this [Language, e.g., Python] code issue:
**Problem:** $1
**Code:**
\`\`\`
[Your Code Here]
\`\`\`
**Please provide:**
1. Root cause analysis
2. Corrected code with explanation
3. Prevention strategies for similar issues`,
contexts: ["code-generation", "technical-automation"],
goals: ["technical-precision", "clarity", "actionability", "structure"],
priority: 9, // HIGH PRIORITY
goalWeight: 3.0,
description: "Provides structured debugging template for troubleshooting requests",
optimalSophisticationRange: [0.0, 0.5],
confidenceImpact: 1.0,
enabled: true,
intentKeywords: {
primary: ['debug', 'fix', 'error', 'exception', 'traceback', 'crashes', 'failing', 'broken'],
actions: ['debug', 'fix', 'solve', 'troubleshoot', 'resolve'],
technical: ['stack trace', 'not working', 'broken', 'failing']
},
intentThreshold: 0.3
})
);
// ENHANCED HOTFIX RULES with modern prompt engineering
hotfix_rules.push(
new OptimizationRule({
name: "hotfix_simple_question_enhancement",
pattern: "^(?:what|how|why|when|where|which)\\s+(.+?)(?:\\?|$)",
replacement: "Please provide a comprehensive explanation of $1. Include key concepts, practical examples, and relevant context. Structure your response with clear sections and actionable insights where applicable.",
contexts: ["llm-interaction", "human-communication"],
goals: ["actionability", "clarity", "comprehensiveness"],
priority: 8,
goalWeight: 2.5,
description: "Transform simple questions into structured, comprehensive requests",
optimalSophisticationRange: [0.0, 0.4],
enabled: true
}),
new OptimizationRule({
name: "hotfix_help_request_enhancement",
pattern: "^(?:help|assist|support)(?:\\s+me)?\\s+(?:write|create|make)\\s+(.+?)(?:\\.|$)",
replacement: "Please provide detailed assistance with creating $1. Include step-by-step guidance, best practices, and practical examples. Structure your response to address potential challenges and provide actionable solutions.",
contexts: ["llm-interaction", "human-communication"],
goals: ["actionability", "clarity", "structure"],
priority: 7,
goalWeight: 2.0,
description: "Transform help requests into structured assistance requests",
optimalSophisticationRange: [0.0, 0.3]
}),
new OptimizationRule({
name: "hotfix_content_creation_enhancement",
pattern: "^(?:write|create|generate|make)\\s+(?:something|content|material|a\\s+\\w+)?\\s*(?:about|on|for|regarding)\\s+(.+?)(?:\\.|$)",
replacement: "Create comprehensive content about $1. Structure your response with clear sections covering key aspects. Include relevant examples, practical applications, and actionable insights. Organize the information in a logical flow that builds understanding progressively.",
contexts: ["llm-interaction", "human-communication"],
goals: ["structure", "comprehensiveness", "clarity"],
priority: 8,
goalWeight: 2.5,
description: "Transform vague content requests into structured creation directives",
optimalSophisticationRange: [0.0, 0.4]
}),
new OptimizationRule({
name: "technical_request_enhancement",
pattern: "(?:create|write|build|implement|develop|help.*create).*(?:function|script|program|code|application)",
replacement: "Create a well-structured function that follows best practices. Include proper error handling, input validation, and comprehensive documentation. Provide clear explanations of the implementation approach, key design decisions, and usage examples.",
contexts: ["technical-automation", "code-generation"],
goals: ["technical-precision", "quality-enhancement", "structure", "comprehensiveness", "actionability", "educational-value"],
intentKeywords: {
primary: ['function', 'script', 'program', 'code'],
actions: ['create', 'write', 'build', 'implement', 'develop'],
technical: ['algorithm', 'tool', 'application']
},
intentThreshold: 0.5,
priority: 9,
goalWeight: 3.0,
description: "Transform technical requests into comprehensive development directives",
optimalSophisticationRange: [0.0, 0.8]
})
);
// TECHNICAL DEBUGGING RULES
technical_rules.push(
new OptimizationRule({
name: "technical_debugging_enhancement",
pattern: "^(?:help|debug|fix|solve|troubleshoot)\\s+.*(?:error|bug|issue|problem|crash)",
replacement: "I need help debugging this code issue:\\n\\n**Problem Description:**\\nMy code is experiencing an error or issue that needs resolution.\\n\\n**Please provide:**\\n1. Root cause analysis of the error\\n2. Step-by-step debugging approach\\n3. Corrected code with explanations\\n4. Best practices to prevent similar issues\\n5. Testing strategies to validate the fix\\n\\n**Additional Context:**\\nPlease include the specific error message, relevant code snippet, programming language, and any environmental details that might be relevant to diagnosing and resolving this issue.",
contexts: ["technical-automation", "code-generation"],
goals: ["structure", "analytical-depth", "actionability"],
priority: 12,
goalWeight: 3.0,
description: "Apply structured debugging framework",
optimalSophisticationRange: [0.0, 1.0],
requiresCrossSegmentContext: true
}),
new OptimizationRule({
name: "technical_documentation_enhancement",
pattern: "(?:write|create|document|draft)\\s+(?:documentation|docs|guide|manual|instructions)(?:\\s+for\\s+(.+?))?(?:\\.|$)",
replacement: "Create comprehensive technical documentation for $1 with this professional structure:\\n\\n## Overview\\n- **Purpose:** What this documentation covers\\n- **Audience:** Target users and skill level\\n- **Prerequisites:** Required knowledge or setup\\n\\n## Getting Started\\n- **Installation/Setup:** Step-by-step initial configuration\\n- **Quick Start Guide:** Basic usage in 5 minutes\\n- **Hello World Example:** Simple working demonstration\\n\\n## Detailed Documentation\\n- **Core Concepts:** Fundamental principles and architecture\\n- **API Reference:** Complete function/method documentation\\n- **Usage Examples:** Real-world implementation scenarios\\n- **Best Practices:** Recommended patterns and approaches\\n\\n## Support Resources\\n- **Troubleshooting:** Common issues and solutions\\n- **FAQ:** Frequently asked questions\\n- **Changelog:** Version history and updates\\n- **Community:** Support channels and contribution guidelines",
contexts: ["technical-automation", "code-generation"],
goals: ["structure", "comprehensiveness", "educational-value", "clarity"],
intentKeywords: {
primary: ['documentation', 'docs', 'guide', 'manual', 'instructions'],
actions: ['write', 'create', 'document', 'draft'],
modifiers: ['technical', 'developer', 'user', 'API'],
subjects: ['setup', 'usage', 'reference', 'examples']
},
intentThreshold: 0.5,
priority: 8,
goalWeight: 2.5,
description: "Structure technical documentation with comprehensive framework",
optimalSophisticationRange: [0.2, 0.8]
}),
new OptimizationRule({
name: "technical_code_review_enhancement",
pattern: "(?:review|check|analyze)\\s+(?:my\\s+)?(?:code|function|script)",
replacement: "Please conduct a comprehensive code review including: 1) Logic and algorithm efficiency, 2) Best practices adherence, 3) Security considerations, 4) Maintainability improvements, 5) Testing recommendations.",
contexts: ["technical-automation", "code-generation"],
goals: ["technical-precision", "quality-enhancement", "structure"],
priority: 8,
goalWeight: 2.5,
description: "Structure code review requests",
optimalSophisticationRange: [0.3, 0.9]
}),
new OptimizationRule({
name: "technical_performance_optimization",
pattern: "(?:optimize|improve|speed up)\\s+(?:performance|code|function)",
replacement: "Please analyze and optimize the performance of this code. Include: 1) Performance bottleneck identification, 2) Algorithmic improvements, 3) Memory optimization strategies, 4) Benchmark comparisons, 5) Implementation recommendations.",
contexts: ["technical-automation", "code-generation"],
goals: ["technical-precision", "quality-enhancement", "analytical-depth"],
priority: 8,
goalWeight: 2.8,
description: "Structure performance optimization requests",
optimalSophisticationRange: [0.4, 1.0]
}),
new OptimizationRule({
name: "technical_security_audit",
pattern: "(?:security|secure|vulnerability|audit).*(?:code|application|system)",
replacement: "Please conduct a security audit including: 1) Vulnerability assessment, 2) Input validation review, 3) Authentication/authorization checks, 4) Data protection measures, 5) Security best practices implementation.",
contexts: ["technical-automation", "code-generation"],
goals: ["safety", "technical-precision", "comprehensiveness"],
priority: 9,
goalWeight: 3.0,
description: "Structure security audit requests",
optimalSophisticationRange: [0.5, 1.0]
}),
new OptimizationRule({
name: "technical_database_design",
pattern: "(?:design|create|build)\\s+(?:database|schema|table)",
replacement: "Please design a database solution including: 1) Entity-relationship model, 2) Normalized table structure, 3) Index optimization strategy, 4) Performance considerations, 5) Data integrity constraints.",
contexts: ["technical-automation"],
goals: ["technical-precision", "structure", "quality-enhancement"],
priority: 8,
goalWeight: 2.7,
description: "Structure database design requests",
optimalSophisticationRange: [0.4, 1.0]
}),
new OptimizationRule({
name: "technical_api_design",
pattern: "(?:design|create|build)\\s+(?:API|endpoint|service)",
replacement: "Please design an API including: 1) RESTful endpoint structure, 2) Request/response schemas, 3) Authentication strategy, 4) Error handling, 5) Documentation and testing approach.",
contexts: ["technical-automation", "api-automation"],
goals: ["technical-precision", "structure", "comprehensiveness"],
priority: 8,
goalWeight: 2.8,
description: "Structure API design requests",
optimalSophisticationRange: [0.4, 1.0]
}),
new OptimizationRule({
name: "technical_testing_strategy",
pattern: "(?:test|testing|unit test|integration test)",
replacement: "Please create a comprehensive testing strategy including: 1) Unit test coverage, 2) Integration testing approach, 3) Test data management, 4) Automated testing pipeline, 5) Performance and security testing.",
contexts: ["technical-automation", "code-generation"],
goals: ["quality-enhancement", "technical-precision", "structure"],
priority: 8,
goalWeight: 2.6,
description: "Structure testing strategy requests",
optimalSophisticationRange: [0.3, 1.0]
}),
new OptimizationRule({
name: "technical_deployment_guide",
pattern: "(?:deploy|deployment|production|release)",
replacement: "Please provide a deployment guide including: 1) Environment setup, 2) Build and packaging process, 3) Configuration management, 4) Monitoring and logging, 5) Rollback procedures.",
contexts: ["technical-automation"],
goals: ["structure", "comprehensiveness", "actionability"],
priority: 8,
goalWeight: 2.5,
description: "Structure deployment requests",
optimalSophisticationRange: [0.4, 1.0]
}),
new OptimizationRule({
name: "technical_architecture_review",
pattern: "(?:architecture|design pattern|system design)",
replacement: "Please provide an architectural analysis including: 1) System component breakdown, 2) Design pattern evaluation, 3) Scalability considerations, 4) Technology stack assessment, 5) Improvement recommendations.",
contexts: ["technical-automation"],
goals: ["analytical-depth", "technical-precision", "comprehensiveness"],
priority: 9,
goalWeight: 3.0,
description: "Structure architecture review requests",
optimalSophisticationRange: [0.5, 1.0]
}),
new OptimizationRule({
name: "technical_refactoring_guide",
pattern: "(?:refactor|refactoring|clean up|improve)\\s+(?:code|function)",
replacement: "Please provide a refactoring plan including: 1) Code smell identification, 2) Improvement opportunities, 3) Step-by-step refactoring process, 4) Testing strategy, 5) Risk mitigation.",
contexts: ["technical-automation", "code-generation"],
goals: ["quality-enhancement", "structure", "technical-precision"],
priority: 8,
goalWeight: 2.6,
description: "Structure refactoring requests",
optimalSophisticationRange: [0.3, 1.0]
}),
new OptimizationRule({
name: "technical_error_handling",
pattern: "(?:error handling|exception|try catch|error management)",
replacement: "Please implement comprehensive error handling including: 1) Exception type identification, 2) Graceful degradation strategies, 3) User-friendly error messages, 4) Logging and monitoring, 5) Recovery mechanisms.",
contexts: ["technical-automation", "code-generation"],
goals: ["safety", "technical-precision", "quality-enhancement"],
priority: 8,
goalWeight: 2.7,
description: "Structure error handling requests",
optimalSophisticationRange: [0.3, 1.0]
}),
new OptimizationRule({
name: "technical_monitoring_setup",
pattern: "(?:monitoring|logging|observability|metrics)",
replacement: "Please design a monitoring solution including: 1) Key performance indicators, 2) Alerting thresholds, 3) Dashboard design, 4) Log aggregation strategy, 5) Incident response procedures.",
contexts: ["technical-automation"],
goals: ["structure", "technical-precision", "actionability"],
priority: 8,
goalWeight: 2.5,
description: "Structure monitoring setup requests",
optimalSophisticationRange: [0.4, 1.0]
}),
new OptimizationRule({
name: "technical_scalability_analysis",
pattern: "(?:scale|scalability|performance|load|capacity)",
replacement: "Please provide a scalability analysis including: 1) Current bottleneck identification, 2) Load testing recommendations, 3) Horizontal vs vertical scaling options, 4) Infrastructure requirements, 5) Cost-performance optimization.",
contexts: ["technical-automation"],
goals: ["analytical-depth", "technical-precision", "comprehensiveness"],
priority: 8,
goalWeight: 2.8,
description: "Structure scalability analysis requests",
optimalSophisticationRange: [0.5, 1.0]
}),
new OptimizationRule({
name: "technical_integration_guide",
pattern: "(?:integrate|integration|connect|API integration)",
replacement: "Please provide an integration guide including: 1) Integration architecture, 2) Data mapping strategy, 3) Authentication setup, 4) Error handling and retries, 5) Testing and validation procedures.",
contexts: ["technical-automation", "api-automation"],
goals: ["structure", "technical-precision", "comprehensiveness"],
priority: 8,
goalWeight: 2.6,
description: "Structure integration requests",
optimalSophisticationRange: [0.4, 1.0]
}),
new OptimizationRule({
name: "technical_migration_plan",
pattern: "(?:migrate|migration|upgrade|move|transfer)",
replacement: "Please create a migration plan including: 1) Pre-migration assessment, 2) Step-by-step migration process, 3) Data validation procedures, 4) Rollback strategy, 5) Post-migration testing.",
contexts: ["technical-automation"],
goals: ["structure", "actionability", "safety"],
priority: 8,
goalWeight: 2.7,
description: "Structure migration planning requests",
optimalSophisticationRange: [0.4, 1.0]
}),
new OptimizationRule({
name: "technical_backup_strategy",
pattern: "(?:backup|disaster recovery|data protection|recovery)",
replacement: "Please design a backup and recovery strategy including: 1) Backup schedule and retention, 2) Recovery time objectives, 3) Data validation procedures, 4) Disaster recovery testing, 5) Business continuity planning.",
contexts: ["technical-automation"],
goals: ["safety", "structure", "comprehensiveness"],
priority: 8,
goalWeight: 2.6,
description: "Structure backup strategy requests",
optimalSophisticationRange: [0.4, 1.0]
})
);
// BUSINESS RULES - Enhanced Resilience
business_rules.push(
new OptimizationRule({
name: "business_proposal_enhancement",
pattern: "(?:create|write|draft|develop|prepare)\\s+(?:a\\s+)?(?:business\\s+)?proposal(?:\\s+for\\s+\\w+)*",
replacement: "Create a comprehensive business proposal with the following structure:\\n\\n## Executive Summary\\n## Problem Statement\\n## Proposed Solution\\n## Implementation Timeline\\n## Budget and Resources\\n## Success Metrics and ROI",
contexts: ["human-communication"],
goals: ["structure", "professionalism", "comprehensiveness", "actionability"],
intentKeywords: {
primary: ['proposal', 'plan', 'strategy'],
actions: ['create', 'write', 'draft', 'develop', 'prepare', 'build'],
modifiers: ['business', 'project', 'funding', 'budget', 'policy', 'work', 'company', 'organizational'],
subjects: ['remote', 'implementation', 'software', 'technology', 'hiring', 'training']
},
intentThreshold: 0.4,
priority: 9,
goalWeight: 2.5,
description: "Structure business proposals with professional framework",
optimalSophisticationRange: [0.0, 0.8],
enabled: true
}),
new OptimizationRule({
name: "marketing_content_enhancement",
pattern: "(?:write|create|develop|craft)\\s+(?:marketing|advertising|promotional|sales)\\s+(?:content|copy|material|campaign)(?:\\s+for\\s+(.+?))?(?:\\.|$)",
replacement: "Create strategic marketing content for $1 using this conversion-focused framework:\\n\\n## Audience and Market Analysis\\n- **Target Demographics:** Age, income, location, interests\\n- **Pain Points:** Problems your product/service solves\\n- **Competitive Landscape:** How you differentiate\\n\\n## Messaging Strategy\\n- **Value Proposition:** Unique benefits and advantages\\n- **Key Messages:** Primary and secondary selling points\\n- **Brand Voice:** Tone, personality, and communication style\\n\\n## Content Structure\\n- **Attention-Grabbing Headline:** Clear, benefit-focused opener\\n- **Compelling Body Copy:** Features, benefits, social proof\\n- **Strong Call-to-Action:** Specific next steps for audience\\n\\n## Optimization Elements\\n- **Emotional Triggers:** Appeals to desires and motivations\\n- **Credibility Factors:** Testimonials, guarantees, certifications\\n- **Urgency/Scarcity:** Time-sensitive or limited offers",
contexts: ["human-communication"],
goals: ["structure", "persuasiveness", "quality-enhancement", "actionability"],
intentKeywords: {
primary: ['marketing', 'advertising', 'promotional', 'sales', 'content', 'copy'],
actions: ['write', 'create', 'develop', 'craft'],
modifiers: ['compelling', 'persuasive', 'conversion', 'engagement'],
subjects: ['campaign', 'strategy', 'audience', 'brand']
},
intentThreshold: 0.5,
priority: 8,
goalWeight: 2.3,
description: "Structure marketing content with strategic conversion framework",
optimalSophisticationRange: [0.0, 0.7]
}),
new OptimizationRule({
name: "business_analysis_enhancement",
pattern: "(?:analyze|examine|review|assess|evaluate)\\s+(?:the\\s+)?(?:market|business|financial|performance|company|industry)",
replacement: "Conduct a comprehensive analysis with the following framework:\\n\\n## Current Situation Assessment\\n## Key Findings and Insights\\n## Impact Analysis\\n## Recommendations\\n## Next Steps and Timeline",
contexts: ["human-communication"],
goals: ["structure", "analytical-depth", "comprehensiveness", "actionability"],
intentKeywords: {
primary: ['analyze', 'analysis', 'review', 'assessment'],
subjects: ['market', 'business', 'financial', 'performance'],
modifiers: ['comprehensive', 'detailed', 'thorough']
},
intentThreshold: 0.5,
priority: 8,
goalWeight: 2.0,
description: "Structure business analysis with professional framework",
optimalSophisticationRange: [0.0, 0.7]
}),
new OptimizationRule({
name: "business_strategy_development",
pattern: "(?:strategy|strategic plan|business strategy)\\s+for\\s+(.+?)(?:\\.|$)",
replacement: "Develop a comprehensive business strategy for $1 including:\\n\\n## Strategic Analysis\\n- Market assessment and competitive landscape\\n- SWOT analysis and opportunity identification\\n- Target customer segmentation\\n\\n## Strategic Framework\\n- Vision, mission, and strategic objectives\\n- Key performance indicators and metrics\\n- Resource allocation and investment priorities\\n\\n## Implementation Plan\\n- Tactical initiatives and action steps\\n- Timeline and milestone tracking\\n- Risk management and contingency planning",
contexts: ["human-communication"],
goals: ["structure", "analytical-depth", "comprehensiveness", "actionability"],
priority: 9,
goalWeight: 2.8,
description: "Structure comprehensive strategy development",
optimalSophisticationRange: [0.4, 1.0]
}),
new OptimizationRule({
name: "business_financial_planning",
pattern: "(?:budget|financial plan|financial planning|financial analysis)",
replacement: "Create a comprehensive financial plan including:\\n\\n## Financial Analysis\\n- Revenue projections and forecasting\\n- Cost structure analysis\\n- Cash flow management\\n\\n## Budget Planning\\n- Capital expenditure planning\\n- Operating expense budgets\\n- Financial risk assessment\\n\\n## Performance Metrics\\n- Key financial indicators\\n- Profitability analysis\\n- Return on investment calculations",
contexts: ["human-communication"],
goals: ["structure", "analytical-depth", "technical-precision"],
priority: 8,
goalWeight: 2.6,
description: "Structure financial planning requests",
optimalSophisticationRange: [0.3, 0.9]
}),
new OptimizationRule({
name: "business_market_research",
pattern: "(?:market research|market analysis|competitor analysis)",
replacement: "Conduct comprehensive market research including:\\n\\n## Market Assessment\\n- Market size and growth trends\\n- Customer demographics and behavior\\n- Market segmentation analysis\\n\\n## Competitive Analysis\\n- Competitor landscape mapping\\n- Competitive advantages and weaknesses\\n- Market positioning strategies\\n\\n## Opportunity Analysis\\n- Market gaps and opportunities\\n- Pricing strategy recommendations\\n- Go-to-market strategy",
contexts: ["human-communication"],
goals: ["analytical-depth", "comprehensiveness", "structure"],
priority: 8,
goalWeight: 2.7,
description: "Structure market research requests",
optimalSophisticationRange: [0.3, 0.9]
}),
new OptimizationRule({
name: "business_project_planning",
pattern: "(?:project plan|project management|project planning)",
replacement: "Create a comprehensive project plan including:\\n\\n## Project Foundation\\n- Project scope and objectives\\n- Stakeholder identification and roles\\n- Success criteria and deliverables\\n\\n## Planning Framework\\n- Work breakdown structure\\n- Timeline and milestone planning\\n- Resource allocation and budgeting\\n\\n## Risk and Quality Management\\n- Risk assessment and mitigation\\n- Quality assurance processes\\n- Communication and reporting structure",
contexts: ["human-communication"],
goals: ["structure", "actionability", "comprehensiveness"],
priority: 8,
goalWeight: 2.5,
description: "Structure project planning requests",
optimalSophisticationRange: [0.2, 0.8]
}),
new OptimizationRule({
name: "business_training_program",
pattern: "(?:training|training program|employee development|skill development)",
replacement: "Design a comprehensive training program including:\\n\\n## Training Assessment\\n- Skills gap analysis\\n- Learning objectives definition\\n- Target audience profiling\\n\\n## Program Design\\n- Curriculum development\\n- Training methodology selection\\n- Resource and material planning\\n\\n## Implementation and Evaluation\\n- Delivery schedule and logistics\\n- Progress tracking and assessment\\n- ROI measurement and program optimization",
contexts: ["human-communication"],
goals: ["structure", "educational-value", "actionability"],
priority: 8,
goalWeight: 2.4,
description: "Structure training program requests",
optimalSophisticationRange: [0.2, 0.8]
}),
new OptimizationRule({
name: "business_process_improvement",
pattern: "(?:process improvement|workflow optimization|efficiency|streamline)",
replacement: "Develop a process improvement plan including:\\n\\n## Current State Analysis\\n- Process mapping and documentation\\n- Bottleneck and inefficiency identification\\n- Performance baseline establishment\\n\\n## Improvement Strategy\\n- Optimization opportunities\\n- Technology integration possibilities\\n- Resource reallocation recommendations\\n\\n## Implementation Plan\\n- Change management strategy\\n- Implementation timeline\\n- Success metrics and monitoring",
contexts: ["human-communication"],
goals: ["analytical-depth", "actionability", "quality-enhancement"],
priority: 8,
goalWeight: 2.5,
description: "Structure process improvement requests",
optimalSophisticationRange: [0.3, 0.9]
}),
new OptimizationRule({
name: "business_risk_management",
pattern: "(?:risk management|risk assessment|risk analysis|compliance)",
replacement: "Create a comprehensive risk management framework including:\\n\\n## Risk Identification\\n- Risk category assessment\\n- Threat and vulnerability analysis\\n- Impact and probability evaluation\\n\\n## Risk Mitigation\\n- Control measures and safeguards\\n- Contingency planning\\n- Insurance and transfer strategies\\n\\n## Monitoring and Governance\\n- Risk monitoring procedures\\n- Compliance requirements\\n- Regular review and updating processes",
contexts: ["human-communication"],
goals: ["safety", "structure", "comprehensiveness"],
priority: 8,
goalWeight: 2.6,
description: "Structure risk management requests",
optimalSophisticationRange: [0.4, 1.0]
}),
new OptimizationRule({
name: "business_customer_experience",
pattern: "(?:customer experience|customer service|customer satisfaction|CX)",