diff --git a/src/modules/bmm/workflows/n8n-expert/optimize-workflow/checklist.md b/src/modules/bmm/workflows/n8n-expert/optimize-workflow/checklist.md new file mode 100644 index 00000000..a5bbd1f2 --- /dev/null +++ b/src/modules/bmm/workflows/n8n-expert/optimize-workflow/checklist.md @@ -0,0 +1,130 @@ +# Optimize n8n Workflow - Validation Checklist + +## Pre-Optimization + +- [ ] Original workflow was successfully loaded +- [ ] Workflow JSON was valid before optimization +- [ ] Optimization focus areas were identified +- [ ] Backup was created before making changes +- [ ] User requirements were clearly understood + +## Analysis Completeness + +- [ ] Performance analysis was conducted +- [ ] Error handling was reviewed +- [ ] Code quality was assessed +- [ ] Structure was evaluated +- [ ] Best practices were checked +- [ ] Security was reviewed +- [ ] All issues were documented + +## Recommendations Quality + +- [ ] Recommendations are specific and actionable +- [ ] Recommendations are prioritized correctly +- [ ] Impact of each recommendation is clear +- [ ] Implementation steps are provided +- [ ] Expected improvements are quantified +- [ ] No breaking changes are recommended + +## Performance Optimizations + +- [ ] Unnecessary nodes were identified/removed +- [ ] Data transformations were optimized +- [ ] Batch processing opportunities were identified +- [ ] Redundant API calls were consolidated +- [ ] Parallel execution opportunities were identified +- [ ] Node execution order was optimized + +## Error Handling Improvements + +- [ ] Critical nodes have retry logic +- [ ] continueOnFail is set appropriately +- [ ] Error workflows are configured where needed +- [ ] Timeout configurations are appropriate +- [ ] Error notifications are set up +- [ ] Error handling doesn't mask real issues + +## Code Quality Improvements + +- [ ] Set nodes are properly configured +- [ ] Code nodes are optimized +- [ ] Expressions use correct syntax +- [ ] Data types are handled correctly +- [ ] Hardcoded values are replaced with variables +- [ ] Node names are descriptive and consistent + +## Structure Improvements + +- [ ] Node positions are logical and organized +- [ ] Complex branches are simplified where possible +- [ ] Duplicate logic is eliminated +- [ ] Merge points are optimized +- [ ] Connection patterns are clean +- [ ] Workflow flow is easy to follow + +## Best Practices Applied + +- [ ] Credentials are used correctly +- [ ] Security issues are addressed +- [ ] Node types are appropriate +- [ ] Node versions are up to date +- [ ] Data handling follows best practices +- [ ] Workflow settings are optimal + +## Security Improvements + +- [ ] No credentials are exposed +- [ ] Sensitive data is handled properly +- [ ] No hardcoded secrets remain +- [ ] Authentication is properly configured +- [ ] Data is sanitized where needed +- [ ] Security best practices are followed + +## Workflow Integrity + +- [ ] All node IDs remain unique +- [ ] All node names remain unique +- [ ] All connections are valid +- [ ] No functionality is lost +- [ ] Workflow still achieves original purpose +- [ ] No breaking changes introduced + +## Validation + +- [ ] Optimized workflow passes JSON validation +- [ ] All optimizations were applied correctly +- [ ] No unintended changes were made +- [ ] Workflow structure is still logical +- [ ] All improvements are documented + +## Backup & Recovery + +- [ ] Backup file was created successfully +- [ ] Backup location was communicated to user +- [ ] Original workflow can be restored if needed + +## Testing Readiness + +- [ ] Optimized workflow can be imported into n8n +- [ ] Test scenarios are identified +- [ ] Expected improvements are measurable +- [ ] Comparison approach is defined +- [ ] Monitoring plan is suggested + +## Documentation + +- [ ] Analysis report is comprehensive +- [ ] All findings are documented +- [ ] Recommendations are clearly explained +- [ ] Expected improvements are quantified +- [ ] Testing recommendations are provided +- [ ] User understands all changes made + +## Expected Improvements + +- [ ] Performance improvements are quantified +- [ ] Reliability improvements are identified +- [ ] Maintainability improvements are clear +- [ ] Security improvements are documented +- [ ] Cost savings are estimated (if applicable) diff --git a/src/modules/bmm/workflows/n8n-expert/optimize-workflow/instructions.md b/src/modules/bmm/workflows/n8n-expert/optimize-workflow/instructions.md new file mode 100644 index 00000000..b8823d11 --- /dev/null +++ b/src/modules/bmm/workflows/n8n-expert/optimize-workflow/instructions.md @@ -0,0 +1,446 @@ +# Optimize n8n Workflow - Workflow Instructions + +```xml +The workflow execution engine is governed by: {project_root}/{bmad_folder}/core/tasks/workflow.xml +You MUST have already loaded and processed: {installed_path}/workflow.yaml +This workflow analyzes and optimizes existing n8n workflows for performance and best practices. + + + + + Before asking any questions, analyze what the user has already told you + + Review the user's initial request and conversation history + Extract any mentioned: workflow file path, performance issues, optimization goals + + + Summarize your understanding + Skip directly to Step 2 (Load Workflow) + + + + Note what you already know + Only ask about missing information in Step 1 + + + + Proceed with full elicitation in Step 1 + + + + + Ask Question 1: "Which workflow do you want to optimize?" + Present numbered options: + 1. Provide file path - Specify exact path to workflow JSON + 2. Search in workflows folder - List available workflows + 3. Paste workflow JSON - Provide workflow content directly + + WAIT for user selection (1-3) + + + Ask: "Please provide the file path to the workflow JSON" + WAIT for user input + Store in {{workflow_file}} + + + + List all .json files in workflows/ directory + Present as numbered list + WAIT for user selection + Store selected file path in {{workflow_file}} + + + + Ask: "Please paste the workflow JSON content" + WAIT for user input + Create temporary file with content + Store temp file path in {{workflow_file}} + + + Ask Question 2: "What aspects do you want to optimize?" + Present numbered options (can select multiple): + 1. Performance - Improve execution speed and efficiency + 2. Error Handling - Add or improve error handling and retries + 3. Code Quality - Improve node configuration and data transformations + 4. Structure - Improve workflow organization and readability + 5. Best Practices - Apply n8n best practices and patterns + 6. Security - Improve credential handling and data security + 7. All - Comprehensive optimization review + + WAIT for user selection (1-7 or multiple) + Store selections in {{optimization_focus}} + + Ask Question 3: "Are there specific issues or pain points?" + Present numbered options: + 1. No - Just general optimization + 2. Yes - Describe specific issues + + WAIT for user selection (1-2) + + + Ask: "Please describe the issues (slow execution, errors, etc.)" + WAIT for user input + Store in {{specific_issues}} + + + + + Only load the workflow file when user provides it - never preload + + Read workflow file from {{workflow_file}} + Parse JSON content + Validate JSON structure + + + Inform user: "The workflow file has invalid JSON syntax" + Show error details + Ask: "Would you like me to fix the JSON syntax first? (yes/no)" + WAIT for user response + + + Fix JSON syntax errors + Save corrected file + Proceed with loading + + + + Exit workflow with error + + + + Extract workflow structure: + - Workflow name and settings + - Node count and types + - Connection patterns + - Error handling configuration + - Credential usage + + Display workflow summary to user: + - Name: [workflow name] + - Nodes: [count] nodes + - Complexity: [simple/medium/complex] + - Integrations: [list of services] + + + + Check if Context7 MCP server is configured + Try to list available MCP tools + + + Store true in {{context7_available}} + Proceed to Step 4 + + + + Store false in {{context7_available}} + Inform user: "Context7 MCP not configured. Using built-in n8n best practices." + Proceed to Step 5 + + + + + + Resolve n8n library ID using Context7 + Query Context7 for n8n best practices documentation + Query for optimization techniques + Query for performance tips + Store relevant documentation for reference + + + + + Load {{helpers}} for best practices reference + + Perform comprehensive analysis based on {{optimization_focus}}: + + Performance Analysis: + - Check for unnecessary nodes + - Identify inefficient data transformations + - Look for missing batch processing opportunities + - Check for redundant API calls + - Analyze node execution order + - Identify parallel execution opportunities + + + Error Handling Analysis: + - Check if critical nodes have retry logic + - Verify continueOnFail settings + - Look for missing error workflows + - Check timeout configurations + - Verify error notification setup + + + Code Quality Analysis: + - Review Set node configurations + - Review Code node implementations + - Check expression syntax and efficiency + - Verify data type handling + - Check for hardcoded values + - Review node naming conventions + + + Structure Analysis: + - Check node positioning and layout + - Verify logical flow organization + - Look for overly complex branches + - Check for duplicate logic + - Verify proper use of merge nodes + - Check connection patterns + + + Best Practices Analysis: + - Verify proper credential usage + - Check for security issues + - Verify proper use of node types + - Check for deprecated node versions + - Verify proper data handling + - Check workflow settings + + + Security Analysis: + - Check credential exposure + - Verify sensitive data handling + - Check for hardcoded secrets + - Verify proper authentication + - Check data sanitization + + + Store all findings in {{issues_found}} + + + + For each issue found, generate specific recommendations: + + Categorize recommendations by priority: + - Critical: Security issues, major performance problems + - High: Error handling gaps, significant inefficiencies + - Medium: Code quality improvements, minor optimizations + - Low: Cosmetic improvements, nice-to-haves + + For each recommendation, provide: + 1. Issue description + 2. Impact explanation + 3. Specific solution + 4. Implementation steps + 5. Expected improvement + + Store recommendations in {{recommendations}} + + + + Present comprehensive optimization report: + + ## Workflow Analysis Report + Workflow: {{workflow_name}} + Analysis Date: {timestamp} + Optimization Focus: {{optimization_focus}} + + ### Summary + - Total Issues Found: [count] + - Critical: [count] + - High Priority: [count] + - Medium Priority: [count] + - Low Priority: [count] + + ### Detailed Findings + Present each issue with: + - Priority level + - Issue description + - Current state + - Recommended solution + - Expected impact + + ### Performance Opportunities + List specific performance improvements with estimated impact + + ### Best Practice Violations + List n8n best practices not being followed + + Ask: "Would you like me to apply these optimizations?" + Present numbered options: + 1. Yes - Apply all recommendations + 2. Yes - Apply only critical and high priority + 3. Yes - Let me choose which to apply + 4. No - Just provide the report + 5. Explain more - I need more details first + + WAIT for user selection (1-5) + + + Ask: "Which recommendations would you like explained?" + WAIT for user input + Provide detailed explanation + Repeat this step + + + + Present recommendations as numbered list + Ask: "Select recommendations to apply (comma-separated numbers)" + WAIT for user input + Store selected recommendations + + + + Store true in {{apply_changes}} + Proceed to Step 8 + + + + Store false in {{apply_changes}} + Skip to Step 11 (provide report only) + + + + + + Create backup of original workflow + Save backup to: {{workflow_file}}.backup-{timestamp} + Store true in {{backup_created}} + Inform user: "Backup created at {{workflow_file}}.backup-{timestamp}" + + + + + Follow guidelines from {{helpers}} for proper node configuration + + Load current workflow JSON into memory + + Apply each selected recommendation: + + Performance Optimizations: + - Remove unnecessary nodes + - Optimize data transformations + - Add batch processing where applicable + - Consolidate redundant API calls + - Optimize node execution order + - Add parallel execution where possible + + + Error Handling Improvements: + - Add retry logic to critical nodes + - Set appropriate continueOnFail values + - Add error workflows if needed + - Configure timeouts + - Add error notifications + + + Code Quality Improvements: + - Refactor Set node configurations + - Optimize Code node implementations + - Improve expression syntax + - Fix data type handling + - Replace hardcoded values with variables + - Improve node naming + + + Structure Improvements: + - Reorganize node positions + - Simplify complex branches + - Remove duplicate logic + - Optimize merge points + - Improve connection patterns + + + Best Practice Applications: + - Fix credential usage + - Address security issues + - Update deprecated nodes + - Improve data handling + - Update workflow settings + + + Validate optimized workflow: + - All node IDs remain unique + - All connections are valid + - No functionality is lost + - All improvements are applied + + + + Save optimized workflow to {{workflow_file}} + + + + + NEVER delete the file if validation fails - always fix syntax errors + + Run: node -e "JSON.parse(require('fs').readFileSync('{{workflow_file}}', 'utf8')); console.log('✓ Valid JSON')" + + + Read the error message carefully - it shows the syntax error and position + Open the file and navigate to the error location + Fix the syntax error (add missing comma, bracket, or quote as indicated) + Save the file + Re-run validation with the same command + Repeat until validation passes + + + Once validation passes, confirm with user: "Workflow optimized successfully at {{workflow_file}}" + + + + + Provide comprehensive summary: + + + ### Optimizations Applied + - Total changes: [count] + - Performance improvements: [list] + - Error handling added: [list] + - Code quality fixes: [list] + - Structure improvements: [list] + - Best practices applied: [list] + - Backup location: {{workflow_file}}.backup-{timestamp} + + ### Expected Improvements + - Execution speed: [estimated improvement] + - Reliability: [improvements] + - Maintainability: [improvements] + - Security: [improvements] + + ### Testing Recommendations + 1. Import optimized workflow into n8n + 2. Test with sample data + 3. Compare execution times with original + 4. Verify all functionality works correctly + 5. Monitor error rates + + + + ### Optimization Report + Report saved with all recommendations + No changes applied to workflow + Review recommendations and apply manually if desired + + + Ask: "Would you like additional help?" + Present numbered options: + 1. No - I'm done + 2. Yes - Explain specific optimizations + 3. Yes - Optimize another workflow + 4. Revert - Restore from backup + + WAIT for user selection (1-4) + + + Ask: "Which optimization would you like explained?" + WAIT for user input + Provide detailed explanation + + + + Return to Step 1 for new workflow + + + + Restore workflow from backup + Confirm restoration to user + + + + + Validate against checklist at {{validation}} using {{bmad_folder}}/core/tasks/validate-workflow.xml + + + +``` diff --git a/src/modules/bmm/workflows/n8n-expert/optimize-workflow/workflow.yaml b/src/modules/bmm/workflows/n8n-expert/optimize-workflow/workflow.yaml new file mode 100644 index 00000000..5d0cf138 --- /dev/null +++ b/src/modules/bmm/workflows/n8n-expert/optimize-workflow/workflow.yaml @@ -0,0 +1,29 @@ +name: optimize-workflow +description: "Review and improve existing n8n workflows for performance and best practices" +author: "Saif" + +# Workflow components +installed_path: "{project-root}/{bmad_folder}/bmm/workflows/n8n-expert/optimize-workflow" +shared_path: "{project-root}/{bmad_folder}/bmm/workflows/n8n-expert/_shared" +instructions: "{installed_path}/instructions.md" +validation: "{installed_path}/checklist.md" + +# Shared resources +helpers: "{shared_path}/n8n-helpers.md" +templates: "{shared_path}/n8n-templates.yaml" +platform_mappings: "{shared_path}/platform-mappings.yaml" + +# Variables +variables: + workflow_file: "" # Will be elicited + optimization_focus: [] # Will be elicited + issues_found: [] # Will be identified + recommendations: [] # Will be generated + apply_changes: false # Will be elicited + backup_created: false # Will be set + context7_available: false # Will be checked + +default_output_file: "" # Will use existing file location + +standalone: true +web_bundle: false