# n8n Agent System Workflow Import Guide

## 🚀 Quick Setup Instructions

### Step 1: Access n8n Interface
1. Open your browser and go to: `http://localhost:5678`
2. Log in to your n8n instance

### Step 2: Import the Fixed Final Workflow
1. **Create New Workflow**:
   - Click the "+" button in the top-left corner
   - Select "New Workflow"

2. **Import the Workflow**:
   - Click the three dots menu (⋮) in the top-right corner
   - Select "Import from File"
   - Choose the file: `n8n_agent_system_flow_fixed_final.json`
   - Click "Import"

3. **Activate the Workflow**:
   - Toggle the switch in the top-right corner to "Active" (green)
   - Make sure it shows "Active" status

### Step 3: Verify the Webhook
The workflow will create a webhook at:
- **URL**: `http://localhost:5678/webhook/agent-task-fixed`
- **Method**: POST

### Step 4: Test the Workflow
Use this test payload:
```json
{
  "message": "Help me build a mobile app for food delivery"
}
```

## 🔧 Workflow Features

### What the Workflow Does:
1. **Receives** user requests via webhook
2. **Validates** input (task/message is required)
3. **Reads** all agent files from `/home/node/.n8n/agents/`
4. **Routes** to the best 1-3 agents using Ollama
5. **Executes** each selected agent with their knowledge base
6. **Synthesizes** responses into a comprehensive action plan
7. **Returns** structured JSON response

### Expected Response Format:
```json
{
  "success": true,
  "task": "Help me build a mobile app for food delivery",
  "context": "",
  "user_id": "anonymous",
  "agents_consulted": ["mobile-app-builder", "ui-designer", "backend-architect"],
  "synthesized_plan": "## Executive Summary\n[Comprehensive plan...]",
  "individual_responses": [
    {
      "agent": "mobile-app-builder",
      "response": "[Agent's specific advice...]"
    }
  ],
  "timestamp": "2025-10-04T00:45:28.872Z",
  "execution_time": 5000
}
```

## 🐛 Troubleshooting

### If you get "Invalid input: Task is required":
- Make sure you imported the **latest** workflow file
- Check that the workflow is **Active** (green toggle)
- Verify you're using the correct webhook URL: `/webhook/agent-task-fixed`

### If you get "Webhook not registered":
- The workflow is not active
- Toggle the switch to "Active" in the top-right corner

### If agents are not found:
- Check that agent files exist in `/home/node/.n8n/agents/`
- Verify the file structure matches the expected format

## 📋 Testing with Postman

1. **Create New Request**:
   - Method: POST
   - URL: `http://localhost:5678/webhook/agent-task-fixed`

2. **Set Headers**:
   - Content-Type: application/json

3. **Set Body** (raw JSON):
   ```json
   {
     "message": "Help me build a mobile app for food delivery"
   }
   ```

4. **Send Request** and check the response

## 🎯 Success Indicators

✅ **Workflow is working if you see**:
- Status: 200
- Response contains `"success": true`
- `agents_consulted` array has agent names
- `synthesized_plan` contains a comprehensive action plan
- `execution_time` shows processing time

❌ **Workflow needs fixing if you see**:
- Status: 404 (webhook not registered)
- `"success": false` with validation errors
- Empty `agents_consulted` array
- Error messages about missing files

## 🔄 Next Steps

Once the workflow is working:
1. Test with different types of requests
2. Customize agent files in `/home/node/.n8n/agents/`
3. Adjust the Ollama model settings if needed
4. Add more specialized agents as needed

## 📞 Support

If you encounter issues:
1. Check the n8n execution logs
2. Verify all dependencies are running (n8n, Ollama, Redis)
3. Test with the provided test scripts
4. Review the workflow node configurations
