# Modify 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 modifies an existing n8n workflow based on user requirements. 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, changes needed, specific nodes Summarize your understanding Skip directly to Step 2 (Load Existing Workflow) Note what you already know Only ask about missing information in Step 1 Proceed with full elicitation in Step 1 Understand WHY the user wants to modify the workflow, not just WHAT to change Ask Question 1: "Which workflow file do you want to modify?" 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 problem are you trying to solve by modifying this workflow?" Examples: "It's not handling errors properly", "Need to add Slack notifications", "Missing data validation" WAIT for user input Store in {{problem_to_solve}} Ask Question 3: "What's currently not working or missing?" Encourage specific details about the issue or gap WAIT for user input Store in {{current_issue}} Ask Question 4: "What should the workflow do after these changes?" Focus on the desired behavior and outcome WAIT for user input Store in {{desired_behavior}} Ask Question 5: "Are there any specific nodes, integrations, or logic you want to change?" Examples: "Add a Slack node after approval", "Change the IF condition to check status", "Remove the delay node" WAIT for user input Store in {{specific_changes}} Summarize understanding: - Problem: {{problem_to_solve}} - Current Issue: {{current_issue}} - Desired Behavior: {{desired_behavior}} - Specific Changes: {{specific_changes}} Ask: "Does this capture what you need?" Present numbered options: 1. Yes - Proceed with modifications 2. No - Let me clarify WAIT for user selection (1-2) Ask: "What needs clarification?" WAIT for user input Update relevant variables Repeat summary and confirmation 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 - List of nodes (names, types, IDs) - Connections map - Current settings Display workflow summary to user: - Name: [workflow name] - Nodes: [count] nodes - Node list: [node names and types] - Connections: [connection count] 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}" Search for n8n documentation relevant to the modifications needed Inform user: "Researching n8n documentation for your modifications..." Perform web search based on modification needs: - Problem to solve: {{problem_to_solve}} - Specific changes: {{specific_changes}} - Desired behavior: {{desired_behavior}} Search queries to use: - "n8n [specific feature] documentation" - "n8n [node type] configuration" - "n8n workflow modification best practices" - "n8n [integration] setup" Focus on official n8n documentation at docs.n8n.io Store relevant node configurations and modification patterns Summarize modification strategy based on documentation: - How to implement {{desired_behavior}} - Required node changes or additions - Configuration updates needed - Best practices for these modifications Inform user: "Based on n8n documentation, I've identified how to implement your changes." Load {{helpers}} for node creation guidelines Analyze current workflow structure Plan modifications based on requirements: 1. Identify where new nodes should be inserted 2. Determine node types needed 3. Plan connections to/from new nodes 4. Calculate positions for new nodes 1. Identify nodes to modify by name or ID 2. Determine what parameters to change 3. Validate new parameter values 1. Identify nodes to remove by name or ID 2. Identify connections that will be affected 3. Plan how to reconnect remaining nodes 1. Identify connections to modify 2. Validate new connection targets exist 3. Update connection indices if needed Present modification plan to user: - Changes to be made: [detailed list] - Nodes affected: [list] - New connections: [list] - Removed connections: [list] Ask: "Does this modification plan look correct?" Present numbered options: 1. Yes - Proceed with modifications 2. No - Adjust the plan 3. Add more changes - Include additional modifications WAIT for user selection (1-3) Ask: "What changes or additions are needed?" WAIT for user input Adjust plan based on feedback Repeat this step Follow guidelines from {{helpers}} for proper node creation Load current workflow JSON into memory If Adding Nodes: 1. Generate unique node IDs (check against existing IDs) 2. Create node objects with proper structure 3. Calculate positions (avoid overlaps with existing nodes) 4. Add nodes to workflow.nodes array 5. Create connections to/from new nodes 6. Update connections object If Modifying Nodes: 1. Find nodes by name or ID 2. Update parameters as specified 3. Preserve node ID and other unchanged properties 4. Validate new parameter values 5. Update node in workflow.nodes array If Removing Nodes: 1. Find nodes by name or ID 2. Remove from workflow.nodes array 3. Remove all connections to/from removed nodes 4. Update connections object 5. Reconnect remaining nodes if needed If Changing Connections: 1. Update connections object 2. Validate all referenced nodes exist 3. Ensure connection indices are correct 4. Remove orphaned connections If Updating Error Handling: 1. Find affected nodes 2. Add or update error handling properties: - continueOnFail - retryOnFail - maxTries - waitBetweenTries 3. Validate error handling configuration Validate modified workflow: - All node IDs are unique - All node names are unique - All connections reference existing nodes - No orphaned nodes (except trigger) - Node positions don't overlap Save modified workflow to {{workflow_file}} Preserve original file location 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 modified successfully at {{workflow_file}}" Provide summary of changes made: - Nodes added: [count and names] - Nodes modified: [count and names] - Nodes removed: [count and names] - Connections changed: [count] - Backup location: {{workflow_file}}.backup-{timestamp} Ask: "Would you like to make additional changes?" Present numbered options: 1. No - I'm done 2. Yes - Make more modifications 3. Revert - Restore from backup WAIT for user selection (1-3) Return to Step 1 with current workflow Restore workflow from backup Confirm restoration to user Validate against checklist at {{validation}} using {{bmad_folder}}/core/tasks/validate-workflow.xml ```