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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# Multi-Agent Platform API Server

A comprehensive FastAPI-based REST API server for managing multi-agent systems with real-time WebSocket support, monitoring, and scalable architecture.

## πŸš€ Features

- **RESTful API**: Complete CRUD operations for agents, tasks, resources, and conflicts
- **Real-time WebSocket**: Live monitoring and event streaming
- **Authentication & Security**: JWT-based authentication and API key support
- **Monitoring & Metrics**: Prometheus metrics and Grafana dashboards
- **Scalable Architecture**: Docker, Kubernetes, and horizontal scaling support
- **Database Integration**: PostgreSQL and Supabase support
- **Performance Testing**: Built-in load testing and benchmarking tools

## πŸ“‹ Table of Contents

1. [Quick Start](#quick-start)
2. [API Endpoints](#api-endpoints)
3. [WebSocket API](#websocket-api)
4. [Authentication](#authentication)
5. [Monitoring](#monitoring)
6. [Deployment](#deployment)
7. [Development](#development)
8. [Troubleshooting](#troubleshooting)

## πŸƒβ€β™‚οΈ Quick Start

### Prerequisites

- Python 3.11+
- Redis 7.0+
- Docker (optional)

### Installation

1. **Clone the repository**
```bash
git clone <repository-url>
cd AI-Agent
```

2. **Install dependencies**
```bash
pip install -r requirements.txt
```

3. **Set up environment variables**
```bash
cp .env.example .env
# Edit .env with your configuration
```

4. **Start Redis**
```bash
# Using Docker
docker run -d -p 6379:6379 redis:7-alpine

# Or using system package manager
sudo systemctl start redis
```

5. **Run the API server**
```bash
# Development mode
uvicorn src.api_server:app --host 0.0.0.0 --port 8000 --reload

# Production mode
uvicorn src.api_server:app --host 0.0.0.0 --port 8000 --workers 4
```

6. **Test the API**
```bash
# Health check
curl http://localhost:8000/api/v1/health

# API documentation
open http://localhost:8000/docs
```

## πŸ”Œ API Endpoints

### Authentication

All API endpoints require authentication using Bearer tokens:

```bash
curl -H "Authorization: Bearer your-token-here" \
     http://localhost:8000/api/v1/agents
```

### Agent Management

#### Register Agent
```bash
curl -X POST http://localhost:8000/api/v1/agents \
  -H "Authorization: Bearer your-token" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "data_processor_agent",
    "capabilities": ["data_processing", "ml_inference"],
    "resources": {"cpu": 2, "memory": "1Gi"},
    "metadata": {"version": "1.0", "team": "ai"}
  }'
```

#### List Agents
```bash
curl -H "Authorization: Bearer your-token" \
     http://localhost:8000/api/v1/agents

# With filters
curl -H "Authorization: Bearer your-token" \
     "http://localhost:8000/api/v1/agents?status_filter=active&capability_filter=data_processing"
```

#### Get Agent Details
```bash
curl -H "Authorization: Bearer your-token" \
     http://localhost:8000/api/v1/agents/{agent_id}
```

#### Deregister Agent
```bash
curl -X DELETE -H "Authorization: Bearer your-token" \
     http://localhost:8000/api/v1/agents/{agent_id}
```

### Task Management

#### Submit Task
```bash
curl -X POST http://localhost:8000/api/v1/tasks \
  -H "Authorization: Bearer your-token" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Process Customer Data",
    "description": "Analyze customer behavior patterns",
    "priority": 8,
    "required_capabilities": ["data_processing"],
    "resources": {"cpu": 1, "memory": "512Mi"},
    "deadline": "2024-01-15T23:59:59Z",
    "metadata": {"customer_id": "12345"}
  }'
```

#### List Tasks
```bash
curl -H "Authorization: Bearer your-token" \
     http://localhost:8000/api/v1/tasks

# With filters
curl -H "Authorization: Bearer your-token" \
     "http://localhost:8000/api/v1/tasks?status_filter=pending&priority_filter=8"
```

#### Get Task Details
```bash
curl -H "Authorization: Bearer your-token" \
     http://localhost:8000/api/v1/tasks/{task_id}
```

### Resource Management

#### Register Resource
```bash
curl -X POST http://localhost:8000/api/v1/resources \
  -H "Authorization: Bearer your-token" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "gpu_cluster_01",
    "type": "gpu",
    "capacity": {"gpus": 4, "memory": "32Gi"},
    "metadata": {"model": "RTX 4090", "location": "rack-1"}
  }'
```

#### List Resources
```bash
curl -H "Authorization: Bearer your-token" \
     http://localhost:8000/api/v1/resources

# Filter by type
curl -H "Authorization: Bearer your-token" \
     "http://localhost:8000/api/v1/resources?type_filter=gpu"
```

### Conflict Management

#### Report Conflict
```bash
curl -X POST http://localhost:8000/api/v1/conflicts \
  -H "Authorization: Bearer your-token" \
  -H "Content-Type: application/json" \
  -d '{
    "agent_id": "agent-123",
    "task_id": "task-456",
    "conflict_type": "resource_contention",
    "description": "Multiple agents requesting same GPU resource",
    "severity": "high",
    "metadata": {"resource_id": "gpu-001"}
  }'
```

#### List Conflicts
```bash
curl -H "Authorization: Bearer your-token" \
     http://localhost:8000/api/v1/conflicts

# Filter by severity
curl -H "Authorization: Bearer your-token" \
     "http://localhost:8000/api/v1/conflicts?severity_filter=high"
```

### Workflow Management

#### Create Workflow
```bash
curl -X POST http://localhost:8000/api/v1/workflows \
  -H "Authorization: Bearer your-token" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Data Processing Pipeline",
    "description": "End-to-end data processing workflow",
    "steps": [
      {
        "name": "data_ingestion",
        "type": "task",
        "parameters": {"source": "database"}
      },
      {
        "name": "data_processing",
        "type": "task",
        "parameters": {"algorithm": "random_forest"}
      },
      {
        "name": "result_export",
        "type": "task",
        "parameters": {"format": "json"}
      }
    ],
    "metadata": {"version": "1.0"}
  }'
```

#### Execute Workflow
```bash
curl -X POST http://localhost:8000/api/v1/workflows/{workflow_id}/execute \
  -H "Authorization: Bearer your-token" \
  -H "Content-Type: application/json" \
  -d '{
    "parameters": {"input_file": "data.csv"},
    "metadata": {"execution_id": "exec-123"}
  }'
```

### Performance Monitoring

#### Get Performance Metrics
```bash
curl -H "Authorization: Bearer your-token" \
     http://localhost:8000/api/v1/metrics/performance

# Filter by agent
curl -H "Authorization: Bearer your-token" \
     "http://localhost:8000/api/v1/metrics/performance?agent_id=agent-123"
```

### Dashboard

#### Get Dashboard Summary
```bash
curl -H "Authorization: Bearer your-token" \
     http://localhost:8000/api/v1/dashboard/summary
```

#### Get Dashboard Activity
```bash
curl -H "Authorization: Bearer your-token" \
     http://localhost:8000/api/v1/dashboard/activity?limit=50
```

## πŸ”Œ WebSocket API

### Connection

Connect to the WebSocket endpoint:

```javascript
const ws = new WebSocket('ws://localhost:8000/api/v1/ws/client-123');
```

### Message Format

All WebSocket messages use JSON format:

```json
{
  "type": "message_type",
  "data": {},
  "timestamp": "2024-01-01T12:00:00Z"
}
```

### Subscription

Subscribe to specific event types:

```javascript
ws.send(JSON.stringify({
  "type": "subscribe",
  "subscriptions": ["agent_activity", "task_activity", "conflict_alerts"]
}));
```

### Event Types

- `agent_activity`: Agent registration, status changes, heartbeats
- `task_activity`: Task submission, assignment, completion
- `conflict_alerts`: Conflict reports and resolutions
- `system_metrics`: Real-time system performance metrics

### Example WebSocket Client

```python
import asyncio
import aiohttp
import json

async def websocket_client():
    async with aiohttp.ClientSession() as session:
        async with session.ws_connect('ws://localhost:8000/api/v1/ws/test-client') as ws:
            # Subscribe to events
            await ws.send_json({
                "type": "subscribe",
                "subscriptions": ["agent_activity", "task_activity"]
            })
            
            # Listen for messages
            async for msg in ws:
                if msg.type == aiohttp.WSMsgType.TEXT:
                    data = json.loads(msg.data)
                    print(f"Received: {data}")
                elif msg.type == aiohttp.WSMsgType.ERROR:
                    print(f"WebSocket error: {ws.exception()}")

asyncio.run(websocket_client())
```

## πŸ” Authentication

### JWT Authentication

The API supports JWT-based authentication:

```python
import jwt
from datetime import datetime, timedelta

# Create token
payload = {
    "sub": "user123",
    "exp": datetime.utcnow() + timedelta(hours=24)
}
token = jwt.encode(payload, "your-secret-key", algorithm="HS256")

# Use token
headers = {"Authorization": f"Bearer {token}"}
```

### API Key Authentication

For simpler integrations, use API key authentication:

```bash
curl -H "X-API-Key: your-api-key" \
     http://localhost:8000/api/v1/agents
```

## πŸ“Š Monitoring

### Prometheus Metrics

The API server exposes metrics at `/metrics`:

```bash
curl http://localhost:8000/metrics
```

Key metrics:
- `http_requests_total`: Total HTTP requests
- `http_request_duration_seconds`: Request duration histogram
- `agent_platform_active_agents_total`: Number of active agents
- `agent_platform_active_tasks_total`: Number of active tasks
- `websocket_connections_total`: Number of WebSocket connections

### Grafana Dashboard

1. Access Grafana at http://localhost:3000
2. Login with admin/admin
3. Import dashboard from `monitoring/grafana/dashboards/agent-platform-dashboard.json`

### Health Check

```bash
curl http://localhost:8000/api/v1/health
```

Response:
```json
{
  "status": "healthy",
  "components": {
    "platform": "healthy",
    "redis": "healthy",
    "database": "healthy"
  },
  "metrics": {
    "active_agents": 5,
    "active_tasks": 12,
    "active_connections": 3
  },
  "timestamp": "2024-01-01T12:00:00Z"
}
```

## πŸš€ Deployment

### Docker Deployment

```bash
# Build image
docker build -t agent-platform:latest .

# Run with Docker Compose
docker-compose up -d
```

### Kubernetes Deployment

```bash
# Create namespace
kubectl create namespace agent-platform

# Apply manifests
kubectl apply -f k8s/

# Check status
kubectl get pods -n agent-platform
```

### Production Configuration

See [DEPLOYMENT_GUIDE.md](DEPLOYMENT_GUIDE.md) for detailed production setup instructions.

## πŸ› οΈ Development

### Project Structure

```
src/
β”œβ”€β”€ api_server.py          # Main FastAPI application
β”œβ”€β”€ unified_architecture/  # Core platform components
β”œβ”€β”€ infrastructure/        # Database, monitoring, etc.
└── tools/                # Utility tools and helpers

monitoring/
β”œβ”€β”€ prometheus.yml        # Prometheus configuration
└── grafana/             # Grafana dashboards and datasources

k8s/                     # Kubernetes manifests
scripts/                 # Utility scripts
```

### Running Tests

```bash
# Unit tests
pytest tests/

# Performance tests
python scripts/performance_test.py --agents 100 --tasks 200

# Load testing
python scripts/performance_test.py \
  --agents 500 \
  --tasks 1000 \
  --websocket-connections 50
```

### Code Quality

```bash
# Format code
black src/ tests/

# Lint code
flake8 src/ tests/

# Type checking
mypy src/
```

### Adding New Endpoints

1. **Define Pydantic models** in `api_server.py`
2. **Add route handlers** with proper authentication
3. **Update WebSocket broadcasts** for real-time updates
4. **Add metrics** for monitoring
5. **Write tests** for the new functionality

Example:
```python
@router.post("/custom-endpoint", response_model=CustomResponse)
async def custom_endpoint(
    request: CustomRequest,
    platform: MultiAgentPlatform = Depends(get_platform),
    token: str = Depends(verify_token)
):
    # Implementation
    result = platform.custom_operation(request)
    
    # Broadcast update
    await manager.broadcast(
        WebSocketMessage(
            type="custom_event",
            data={"result": result}
        ).json()
    )
    
    return CustomResponse(**result)
```

## πŸ”§ Troubleshooting

### Common Issues

#### API Server Won't Start
```bash
# Check logs
docker-compose logs api-server

# Verify dependencies
pip list | grep fastapi

# Check environment variables
echo $DATABASE_URL
```

#### Redis Connection Issues
```bash
# Test Redis connection
redis-cli ping

# Check Redis logs
docker-compose logs redis
```

#### WebSocket Connection Issues
```bash
# Test WebSocket connection
wscat -c ws://localhost:8000/api/v1/ws/test

# Check WebSocket logs
docker-compose logs api-server | grep websocket
```

### Performance Issues

#### High Response Times
```bash
# Check resource usage
docker stats

# Monitor slow queries
docker-compose exec postgres psql -c "SELECT * FROM pg_stat_activity WHERE state = 'active';"

# Check API metrics
curl http://localhost:8000/metrics | grep http_request_duration
```

#### Memory Issues
```bash
# Check memory usage
free -h
docker stats --no-stream

# Analyze memory leaks
python scripts/memory_profiler.py
```

### Debug Mode

Enable debug mode for detailed logging:

```bash
export LOG_LEVEL=DEBUG
export DEBUG=true
uvicorn src.api_server:app --reload --log-level debug
```

## πŸ“š Additional Resources

- [API Documentation](http://localhost:8000/docs) - Interactive API docs
- [Architecture Guide](ARCHITECTURE.md) - System architecture overview
- [Deployment Guide](DEPLOYMENT_GUIDE.md) - Production deployment instructions
- [Performance Testing](scripts/performance_test.py) - Load testing tools

## 🀝 Contributing

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests
5. Submit a pull request

## πŸ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## πŸ†˜ Support

For support and questions:

1. Check the [documentation](http://localhost:8000/docs)
2. Review the [troubleshooting guide](#troubleshooting)
3. Open an issue on GitHub
4. Contact the development team

---

**Happy coding! πŸš€**