# Dynamic Multi-Agent Router System

A sophisticated n8n workflow that intelligently routes user requests to the most appropriate AI agents based on their expertise, enabling multi-agent collaboration for complex tasks.

## 🚀 Quick Start

### 1. Automated Setup

**Windows:**
```cmd
setup_agent_system.bat
```

**Linux/Mac:**
```bash
chmod +x setup_agent_system.sh
./setup_agent_system.sh
```

### 2. Manual Setup

```bash
# Start services
docker-compose up -d

# Pull required model
docker exec ollama-server ollama pull llama3.1

# Access n8n
open http://localhost:5678
# Login: admin / admin123
```

### 3. Import Workflow

1. Open n8n at `http://localhost:5678`
2. Login with `admin` / `admin123`
3. Import one of these workflows:
   - `n8n_agent_system_flow_simple.json` (No Ollama required - start here)
   - `n8n_agent_system_flow_corrected.json` (Full system with Ollama)
4. Activate the workflow

### 4. Test the System

```bash
# Basic test
curl -X POST http://localhost:5678/webhook/agent-task \
  -H "Content-Type: application/json" \
  -d '{"task": "Build a mobile app"}'

# Full test
python test_agent_system.py
```

## 🤖 Built-in Agents

- **mobile-app-builder** (engineering) - Mobile app development
- **ui-designer** (design) - User interface design  
- **backend-architect** (engineering) - Backend architecture
- **frontend-developer** (engineering) - Frontend development
- **content-creator** (marketing) - Content creation
- **growth-hacker** (marketing) - Growth strategies

## 📋 Features

✅ **Intelligent Agent Selection** - Uses Ollama LLM to select 1-3 most relevant agents  
✅ **Multi-Agent Collaboration** - Agents work together on complex tasks  
✅ **Parallel Execution** - Multiple agents execute simultaneously for faster responses  
✅ **Comprehensive Synthesis** - Combines expert perspectives into unified action plans  
✅ **Error Handling** - Graceful fallbacks and detailed error responses  
✅ **Built-in Agents** - No external dependencies required  

## 🏗️ Architecture

```
User Request → Webhook → Input Validation → Agent Selection (Ollama) 
    ↓
Parallel Agent Execution → Response Synthesis → Final Response
```

## 📁 Project Structure

```
├── docker-compose.yml              # Docker services configuration
├── n8n_agent_system_flow_working.json  # Main workflow file
├── setup_agent_system.sh          # Linux/Mac setup script
├── setup_agent_system.bat         # Windows setup script
├── test_agent_system.py           # Test script
├── agents/                        # Custom agent files (optional)
└── docs/                          # Documentation
```

## 🔧 Configuration

### Docker Services
- **n8n**: Workflow automation platform
- **Ollama**: Local LLM for agent selection and responses

### Environment Variables
- `OLLAMA_BASE_URL`: Ollama service URL
- `N8N_BASIC_AUTH_USER`: n8n username (default: admin)
- `N8N_BASIC_AUTH_PASSWORD`: n8n password (default: admin123)

## 📚 Documentation

- [Complete Setup Guide](DYNAMIC_AGENT_SYSTEM_SETUP.md)
- [Agent System Documentation](AGENT_SYSTEM_DOCUMENTATION.md)
- [Workflow Import Guide](WORKFLOW_IMPORT_GUIDE.md)

## 🧪 Testing

```bash
# Run comprehensive tests
python test_agent_system.py

# Test specific scenarios
curl -X POST http://localhost:5678/webhook/agent-task \
  -H "Content-Type: application/json" \
  -d '{"task": "Design a mobile app", "context": "Food delivery startup"}'
```

## 🚨 Troubleshooting

### Common Issues

1. **"Webhook not registered"** - Ensure workflow is active
2. **"No suitable agents found"** - Check agent selection logic
3. **Ollama connection errors** - Verify Ollama is running and accessible
4. **Workflow activation errors** - Use the provided working workflow

### Debug Commands

```bash
# Check services
docker-compose ps

# View logs
docker-compose logs n8n
docker-compose logs ollama

# Test Ollama
curl http://localhost:11434/api/tags

# Test n8n
curl http://localhost:5678
```

## 🎯 Example Usage

### Request
```json
{
  "task": "I need help building a mobile app for food delivery",
  "context": "We're a startup with 3 developers, 6-week timeline"
}
```

### Response
```json
{
  "success": true,
  "task": "I need help building a mobile app for food delivery",
  "agents_consulted": ["mobile-app-builder", "ui-designer", "backend-architect"],
  "synthesized_plan": "# Executive Summary\n...",
  "individual_responses": [...],
  "execution_time": 2500
}
```

## 🔄 Customization

### Adding Custom Agents

1. Create agent files in `./agents/` directory
2. Follow the agent file format (see documentation)
3. Restart the system to load new agents

### Modifying Agent Selection

Edit the "Select Agents" node in the workflow to:
- Change selection criteria
- Adjust temperature and token limits
- Modify the selection prompt

## 📈 Performance

- **Response Time**: 2-5 seconds for typical requests
- **Concurrent Requests**: Supports multiple simultaneous requests
- **Agent Selection**: ~1-2 seconds
- **Agent Execution**: Parallel processing for faster responses

## 🤝 Contributing

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Test thoroughly
5. Submit a pull request

## 📄 License

This project is open source and available under the MIT License.

---

**Ready to get started?** Run `setup_agent_system.bat` (Windows) or `./setup_agent_system.sh` (Linux/Mac) to begin!