# Complete n8n Agent System Setup Guide

## 🎯 Overview
This guide will help you create a working dynamic multi-agent system using n8n and Ollama. The system will intelligently route user requests to the most appropriate AI agents and provide comprehensive responses.

## 📋 Prerequisites
- ✅ n8n running on http://localhost:5678
- ✅ Ollama running on http://localhost:11434
- ✅ Agent files in `/home/node/.n8n/agents/` (or `n8n_data/agents/`)
- ✅ Docker Compose setup completed

## 🚀 Step-by-Step Setup

### Step 1: Access n8n Interface
1. Open your browser
2. Navigate to: `http://localhost:5678`
3. Log in to your n8n instance

### Step 2: Create New Workflow
1. Click the **"+"** button in the top-left corner
2. Select **"New Workflow"**
3. You'll see an empty workflow canvas

### Step 3: Import the Fixed Final Workflow
1. Click the **three dots menu (⋮)** in the top-right corner
2. Select **"Import from File"**
3. Choose the file: `n8n_agent_system_flow_fixed_final.json`
4. Click **"Import"**

### Step 4: Activate the Workflow
1. **Toggle the switch** in the top-right corner to **"Active"** (green)
2. Make sure it shows **"Active"** status
3. The webhook will be available at: `http://localhost:5678/webhook/agent-task-fixed`

### Step 5: Verify Agent Files
Make sure your agent files are in the correct location:
- **Path**: `/home/node/.n8n/agents/` (inside the n8n container)
- **Structure**: 
  ```
  agents/
  ├── engineering/
  │   ├── mobile-app-builder.md
  │   ├── ui-designer.md
  │   └── backend-architect.md
  ├── marketing/
  │   ├── content-creator.md
  │   └── growth-hacker.md
  └── product/
      └── feedback-synthesizer.md
  ```

## 🧪 Testing the Workflow

### Test 1: Basic Functionality Test
Run this command in your terminal:
```bash
python test_final_fixed.py
```

### Test 2: Manual API Test
Use Postman or curl:
```bash
curl -X POST http://localhost:5678/webhook/agent-task-fixed \
  -H "Content-Type: application/json" \
  -d '{"message": "Help me build a mobile app for food delivery"}'
```

### Test 3: Interactive Test
Run the interactive test script:
```bash
python interactive_test.py
```

## 📊 Expected Results

### Successful 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

### Issue: "Webhook not registered" (404 Error)
**Solution**: 
- Make sure the workflow is **Active** (green toggle)
- Check the webhook path: `/webhook/agent-task-fixed`

### Issue: "Invalid input: Task is required"
**Solution**: 
- You're using an old workflow version
- Import the **latest** `n8n_agent_system_flow_fixed_final.json`
- Make sure it's activated

### Issue: "No suitable agents found"
**Solution**: 
- Check agent files exist in `/home/node/.n8n/agents/`
- Verify file structure and content
- Check n8n container has access to the files

### Issue: Ollama connection errors
**Solution**: 
- Ensure Ollama is running on `http://localhost:11434`
- Check the workflow uses `http://host.docker.internal:11434`
- Verify the model `llama3:latest` is available

## 🎯 Workflow Features

### What the System Does:
1. **Receives** user requests via webhook
2. **Validates** input (task/message required)
3. **Reads** all agent files from the agents directory
4. **Routes** to the best 1-3 agents using Ollama AI
5. **Executes** each selected agent with their knowledge base
6. **Synthesizes** responses into a comprehensive action plan
7. **Returns** structured JSON response with:
   - Executive summary
   - Phased action plan
   - Key priorities
   - Success metrics
   - Risk mitigation
   - Next steps

### Agent Selection Process:
- Analyzes the user's task requirements
- Selects agents that complement each other
- Considers both primary and supporting roles
- Prioritizes agents with most relevant expertise
- Ensures effective collaboration between agents

## 📈 Performance Optimization

### Caching (Optional):
- Redis is configured for agent catalog caching
- Reduces file system reads on subsequent requests
- Improves response times

### Model Configuration:
- Uses `llama3:latest` for both routing and execution
- Temperature settings optimized for each task
- Token limits set appropriately

## 🔄 Next Steps

Once the workflow is working:
1. **Test with different request types**
2. **Customize agent files** with your specific knowledge
3. **Add new agents** for specialized domains
4. **Adjust Ollama model settings** if needed
5. **Monitor performance** and optimize as needed

## 📞 Support

If you encounter issues:
1. Check the n8n execution logs
2. Verify all services are running (n8n, Ollama, Redis)
3. Test with the provided test scripts
4. Review the workflow node configurations
5. Check agent file permissions and content

## 🎉 Success!

Once everything is working, you'll have a powerful dynamic multi-agent system that can:
- Intelligently route requests to the right experts
- Provide comprehensive, actionable advice
- Scale to handle complex, multi-domain tasks
- Learn and adapt as you add more agents
