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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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
"""
Multi-Agent Platform API Server and Deployment Configuration
Complete FastAPI implementation with WebSocket support for real-time monitoring
"""

from fastapi import FastAPI, HTTPException, Depends, WebSocket, WebSocketDisconnect
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from pydantic import BaseModel, Field
from typing import Dict, List, Optional, Any, Set
import asyncio
import json
import uuid
import os
from datetime import datetime
import logging

# Import from the unified architecture and Enhanced FSM
from src.enhanced_fsm import HierarchicalFSM, AtomicState, ProbabilisticTransition
from src.migrated_enhanced_fsm_agent import MigratedEnhancedFSMAgent

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# =============================
# Platform Core Classes
# =============================

class AgentCapability:
    """Agent capabilities enumeration"""
    REASONING = "REASONING"
    COLLABORATION = "COLLABORATION"
    EXECUTION = "EXECUTION"
    ANALYSIS = "ANALYSIS"
    SYNTHESIS = "SYNTHESIS"
    ERROR_HANDLING = "ERROR_HANDLING"

class AgentStatus:
    """Agent status enumeration"""
    AVAILABLE = "AVAILABLE"
    BUSY = "BUSY"
    IDLE = "IDLE"
    OFFLINE = "OFFLINE"
    ERROR = "ERROR"

class AgentMetadata:
    """Agent metadata"""
    def __init__(self, agent_id: str, name: str, version: str, 
                 capabilities: List[str], tags: List[str] = None):
        self.agent_id = agent_id
        self.name = name
        self.version = version
        self.capabilities = capabilities
        self.tags = tags or []
        self.status = AgentStatus.AVAILABLE
        self.reliability_score = 1.0
        self.last_seen = datetime.now()

class UnifiedTask:
    """Unified task representation"""
    def __init__(self, task_id: str, task_type: str, priority: int, 
                 payload: Dict[str, Any], required_capabilities: List[str],
                 deadline: Optional[datetime] = None, dependencies: List[str] = None):
        self.task_id = task_id
        self.task_type = task_type
        self.priority = priority
        self.payload = payload
        self.required_capabilities = required_capabilities
        self.deadline = deadline
        self.dependencies = dependencies or []
        self.status = "PENDING"
        self.created_at = datetime.now()
        self.completed_at = None
        self.result = None

class ConflictType:
    """Conflict types"""
    RESOURCE_CONFLICT = "RESOURCE_CONFLICT"
    TASK_CONFLICT = "TASK_CONFLICT"
    AGENT_CONFLICT = "AGENT_CONFLICT"
    DATA_CONFLICT = "DATA_CONFLICT"

class Conflict:
    """Conflict representation"""
    def __init__(self, conflict_id: str, conflict_type: str, involved_agents: List[str],
                 description: str, context: Dict[str, Any]):
        self.conflict_id = conflict_id
        self.conflict_type = conflict_type
        self.involved_agents = involved_agents
        self.description = description
        self.context = context
        self.reported_at = datetime.now()
        self.resolved = False
        self.resolution = None

class MarketplaceListing:
    """Marketplace listing"""
    def __init__(self, agent_id: str, metadata: AgentMetadata, description: str,
                 pricing: Dict[str, float], keywords: List[str]):
        self.agent_id = agent_id
        self.metadata = metadata
        self.description = description
        self.pricing = pricing
        self.keywords = keywords
        self.ratings = []
        self.total_usage = 0
        self.average_rating = 0.0
    
    def add_rating(self, rating: float, review: str, user_id: str):
        """Add a rating to the listing"""
        self.ratings.append({
            'rating': rating,
            'review': review,
            'user_id': user_id,
            'timestamp': datetime.now()
        })
        self.average_rating = sum(r['rating'] for r in self.ratings) / len(self.ratings)

class AgentRegistry:
    """Agent registry for managing agent registrations"""
    def __init__(self):
        self.agents: Dict[str, AgentMetadata] = {}
        self.agent_instances: Dict[str, MigratedEnhancedFSMAgent] = {}
    
    async def register(self, agent_id: str, metadata: AgentMetadata, 
                      agent_instance: MigratedEnhancedFSMAgent) -> bool:
        """Register an agent"""
        self.agents[agent_id] = metadata
        self.agent_instances[agent_id] = agent_instance
        return True
    
    async def unregister(self, agent_id: str) -> bool:
        """Unregister an agent"""
        if agent_id in self.agents:
            del self.agents[agent_id]
            if agent_id in self.agent_instances:
                del self.agent_instances[agent_id]
            return True
        return False
    
    async def discover(self, capabilities: List[str] = None, tags: List[str] = None,
                      status: str = None) -> List[AgentMetadata]:
        """Discover agents based on criteria"""
        agents = list(self.agents.values())
        
        if capabilities:
            agents = [a for a in agents if any(cap in a.capabilities for cap in capabilities)]
        
        if tags:
            agents = [a for a in agents if any(tag in a.tags for tag in tags)]
        
        if status:
            agents = [a for a in agents if a.status == status]
        
        return agents

class TaskManager:
    """Task manager for handling task execution"""
    def __init__(self):
        self.tasks: Dict[str, UnifiedTask] = {}
        self.task_queue: List[str] = []
    
    async def submit_task(self, task: UnifiedTask) -> str:
        """Submit a task for execution"""
        self.tasks[task.task_id] = task
        self.task_queue.append(task.task_id)
        return task.task_id
    
    async def get_task_status(self, task_id: str) -> Optional[Dict[str, Any]]:
        """Get task status"""
        if task_id not in self.tasks:
            return None
        
        task = self.tasks[task_id]
        return {
            "task_id": task.task_id,
            "status": task.status,
            "created_at": task.created_at.isoformat(),
            "completed_at": task.completed_at.isoformat() if task.completed_at else None,
            "result": task.result
        }

class ResourceManager:
    """Resource manager for tracking resource utilization"""
    def __init__(self):
        self.allocated_resources: Dict[str, Dict[str, float]] = {}
        self.total_resources = {
            "cpu_cores": 100.0,
            "memory_mb": 102400.0,
            "gpu_memory_mb": 51200.0
        }
    
    async def allocate_resources(self, agent_id: str, resources: Dict[str, float]) -> bool:
        """Allocate resources to an agent"""
        # Check if resources are available
        for resource, amount in resources.items():
            if resource in self.total_resources:
                allocated = sum(
                    agent_resources.get(resource, 0) 
                    for agent_resources in self.allocated_resources.values()
                )
                if allocated + amount > self.total_resources[resource]:
                    return False
        
        self.allocated_resources[agent_id] = resources
        return True
    
    async def release_resources(self, agent_id: str):
        """Release resources from an agent"""
        if agent_id in self.allocated_resources:
            del self.allocated_resources[agent_id]
    
    def get_resource_utilization(self) -> Dict[str, float]:
        """Get current resource utilization"""
        utilization = {}
        
        for resource, total in self.total_resources.items():
            allocated = sum(
                agent_resources.get(resource, 0) 
                for agent_resources in self.allocated_resources.values()
            )
            utilization[resource] = (allocated / total) * 100
        
        return utilization

class Marketplace:
    """Marketplace for agent discovery and rating"""
    def __init__(self):
        self.listings: Dict[str, MarketplaceListing] = {}
    
    async def publish_agent(self, agent_id: str, description: str,
                           pricing: Dict[str, float], keywords: List[str]) -> bool:
        """Publish an agent to marketplace"""
        # This would typically fetch agent metadata from registry
        metadata = AgentMetadata(agent_id, f"Agent_{agent_id}", "1.0.0", 
                               [AgentCapability.REASONING], ["marketplace"])
        
        listing = MarketplaceListing(agent_id, metadata, description, pricing, keywords)
        self.listings[agent_id] = listing
        return True
    
    async def search_agents(self, query: str, min_rating: float = 0.0,
                           max_price: Optional[float] = None) -> List[MarketplaceListing]:
        """Search for agents in marketplace"""
        results = []
        
        for listing in self.listings.values():
            # Simple search implementation
            if (query.lower() in listing.description.lower() or
                query.lower() in listing.metadata.name.lower() or
                any(query.lower() in keyword.lower() for keyword in listing.keywords)):
                
                if listing.average_rating >= min_rating:
                    if max_price is None or listing.pricing.get("per_task", 0) <= max_price:
                        results.append(listing)
        
        return results

class ConflictResolver:
    """Conflict resolution system"""
    def __init__(self):
        self.conflicts: Dict[str, Conflict] = {}
    
    async def report_conflict(self, conflict: Conflict) -> str:
        """Report a conflict for resolution"""
        self.conflicts[conflict.conflict_id] = conflict
        
        # Simple auto-resolution logic
        if conflict.conflict_type == ConflictType.RESOURCE_CONFLICT:
            conflict.resolved = True
            conflict.resolution = "Resources reallocated automatically"
        elif conflict.conflict_type == ConflictType.TASK_CONFLICT:
            conflict.resolved = True
            conflict.resolution = "Task priority adjusted"
        
        return conflict.conflict_id

class Dashboard:
    """Dashboard for system monitoring"""
    def __init__(self, agent_registry: AgentRegistry, task_manager: TaskManager):
        self.agent_registry = agent_registry
        self.task_manager = task_manager
    
    async def get_system_overview(self) -> Dict[str, Any]:
        """Get system-wide overview"""
        agents = list(self.agent_registry.agents.values())
        
        # Calculate agent breakdown by status
        status_breakdown = {}
        for agent in agents:
            status = agent.status
            status_breakdown[status] = status_breakdown.get(status, 0) + 1
        
        # Calculate performance metrics
        completed_tasks = sum(1 for task in self.task_manager.tasks.values() 
                            if task.status == "COMPLETED")
        total_tasks = len(self.task_manager.tasks)
        success_rate = completed_tasks / total_tasks if total_tasks > 0 else 0.0
        
        return {
            "total_agents": len(agents),
            "active_agents": sum(1 for a in agents if a.status == AgentStatus.AVAILABLE),
            "agent_breakdown": {
                "by_status": status_breakdown
            },
            "performance_summary": {
                "total_tasks_completed": completed_tasks,
                "total_tasks": total_tasks,
                "overall_success_rate": success_rate
            }
        }
    
    async def get_agent_details(self, agent_id: str) -> Optional[Dict[str, Any]]:
        """Get detailed agent information"""
        if agent_id not in self.agent_registry.agents:
            return None
        
        agent = self.agent_registry.agents[agent_id]
        agent_instance = self.agent_registry.agent_instances.get(agent_id)
        
        details = {
            "agent_id": agent.agent_id,
            "name": agent.name,
            "version": agent.version,
            "capabilities": agent.capabilities,
            "tags": agent.tags,
            "status": agent.status,
            "reliability_score": agent.reliability_score,
            "last_seen": agent.last_seen.isoformat()
        }
        
        if agent_instance:
            # Add Enhanced FSM metrics
            fsm_metrics = agent_instance.get_metrics()
            details["fsm_metrics"] = fsm_metrics
        
        return details

class MultiAgentPlatform:
    """Main platform orchestrator"""
    def __init__(self, redis_url: str = None):
        self.redis_url = redis_url
        self.agent_registry = AgentRegistry()
        self.task_manager = TaskManager()
        self.resource_manager = ResourceManager()
        self.marketplace = Marketplace()
        self.conflict_resolver = ConflictResolver()
        self.dashboard = Dashboard(self.agent_registry, self.task_manager)
    
    async def initialize(self):
        """Initialize the platform"""
        logger.info("Initializing Multi-Agent Platform")
        # Initialize Redis connection if needed
        # Initialize other components
    
    async def shutdown(self):
        """Shutdown the platform"""
        logger.info("Shutting down Multi-Agent Platform")
        # Cleanup resources
    
    async def register_agent(self, agent_instance: MigratedEnhancedFSMAgent, 
                           metadata: AgentMetadata, resources: Dict[str, float]) -> bool:
        """Register an agent with the platform"""
        # Allocate resources
        if not await self.resource_manager.allocate_resources(metadata.agent_id, resources):
            return False
        
        # Register agent
        return await self.agent_registry.register(metadata.agent_id, metadata, agent_instance)
    
    async def submit_task(self, task: UnifiedTask) -> str:
        """Submit a task to the platform"""
        return await self.task_manager.submit_task(task)
    
    async def get_task_status(self, task_id: str) -> Optional[Dict[str, Any]]:
        """Get task status"""
        return await self.task_manager.get_task_status(task_id)

# =============================
# API Models
# =============================

class AgentRegistrationRequest(BaseModel):
    """Request model for agent registration"""
    name: str
    version: str
    capabilities: List[str]
    tags: List[str] = Field(default_factory=list)
    resources: Dict[str, float] = Field(default_factory=dict)
    description: Optional[str] = None

class TaskSubmissionRequest(BaseModel):
    """Request model for task submission"""
    task_type: str
    priority: int = Field(default=5, ge=1, le=10)
    payload: Dict[str, Any]
    required_capabilities: List[str]
    deadline: Optional[datetime] = None
    dependencies: List[str] = Field(default_factory=list)

class MarketplaceListingRequest(BaseModel):
    """Request model for marketplace listing"""
    agent_id: str
    description: str
    pricing: Dict[str, float]
    keywords: List[str]

class ConflictReportRequest(BaseModel):
    """Request model for conflict reporting"""
    conflict_type: str
    involved_agents: List[str]
    description: str
    context: Dict[str, Any]

class AgentSearchRequest(BaseModel):
    """Request model for agent search"""
    query: str
    min_rating: float = Field(default=0.0, ge=0.0, le=5.0)
    max_price: Optional[float] = None
    capabilities: Optional[List[str]] = None
    tags: Optional[List[str]] = None

# =============================
# API Server
# =============================

app = FastAPI(
    title="Multi-Agent Collaboration Platform API",
    description="API for managing and orchestrating AI agents with Enhanced FSM",
    version="2.0.0"
)

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Security
security = HTTPBearer()

# Global platform instance
platform: Optional[MultiAgentPlatform] = None

# WebSocket connections for real-time updates
websocket_connections: Set[WebSocket] = set()

# =============================
# Lifecycle Events
# =============================

@app.on_event("startup")
async def startup_event():
    """Initialize the platform on startup"""
    global platform
    
    redis_url = os.getenv("REDIS_URL", "redis://localhost:6379")
    platform = MultiAgentPlatform(redis_url=redis_url)
    await platform.initialize()
    
    logger.info("Multi-Agent Platform API started")

@app.on_event("shutdown")
async def shutdown_event():
    """Cleanup on shutdown"""
    global platform
    
    if platform:
        await platform.shutdown()
    
    # Close all WebSocket connections
    for websocket in websocket_connections.copy():
        await websocket.close()
    
    logger.info("Multi-Agent Platform API shut down")

# =============================
# Authentication
# =============================

async def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
    """Verify API token"""
    # Simplified - implement proper authentication
    if credentials.credentials != "valid_token":
        raise HTTPException(status_code=401, detail="Invalid token")
    return credentials.credentials

# =============================
# Agent Management Endpoints
# =============================

@app.post("/api/v2/agents/register", response_model=Dict[str, str])
async def register_agent(
    request: AgentRegistrationRequest,
    token: str = Depends(verify_token)
):
    """Register a new agent with the platform"""
    try:
        # Create Enhanced FSM agent instance
        agent_id = f"agent_{uuid.uuid4().hex[:8]}"
        
        # Create mock tools for the agent
        class MockTool:
            def __init__(self, name):
                self.name = name
                self.tool_name = name
        
        mock_tools = [MockTool("search"), MockTool("calculator"), MockTool("database")]
        
        agent = MigratedEnhancedFSMAgent(
            tools=mock_tools,
            enable_hierarchical=True,
            enable_probabilistic=True,
            enable_discovery=True,
            enable_metrics=True,
            fsm_name=f"Agent_{agent_id}"
        )
        
        # Create metadata
        metadata = AgentMetadata(
            agent_id=agent_id,
            name=request.name,
            version=request.version,
            capabilities=request.capabilities,
            tags=request.tags
        )
        
        # Register with platform
        success = await platform.register_agent(agent, metadata, request.resources)
        
        if not success:
            raise HTTPException(status_code=400, detail="Failed to register agent")
        
        # Broadcast update
        await broadcast_update({
            "event": "agent_registered",
            "agent_id": agent_id,
            "name": request.name
        })
        
        return {
            "agent_id": agent_id,
            "status": "registered",
            "message": f"Agent {request.name} successfully registered"
        }
        
    except Exception as e:
        logger.error(f"Agent registration failed: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/v2/agents", response_model=List[Dict[str, Any]])
async def list_agents(
    status: Optional[str] = None,
    capability: Optional[str] = None,
    tag: Optional[str] = None,
    token: str = Depends(verify_token)
):
    """List all registered agents with optional filters"""
    try:
        # Parse filters
        status_filter = status if status else None
        capability_filter = [capability] if capability else None
        tag_filter = [tag] if tag else None
        
        # Discover agents
        agents = await platform.agent_registry.discover(
            capabilities=capability_filter,
            tags=tag_filter,
            status=status_filter
        )
        
        # Format response
        return [
            {
                "agent_id": agent.agent_id,
                "name": agent.name,
                "version": agent.version,
                "capabilities": agent.capabilities,
                "tags": agent.tags,
                "status": agent.status,
                "reliability_score": agent.reliability_score,
                "last_seen": agent.last_seen.isoformat()
            }
            for agent in agents
        ]
        
    except Exception as e:
        logger.error(f"Failed to list agents: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/v2/agents/{agent_id}", response_model=Dict[str, Any])
async def get_agent_details(
    agent_id: str,
    token: str = Depends(verify_token)
):
    """Get detailed information about a specific agent"""
    try:
        details = await platform.dashboard.get_agent_details(agent_id)
        
        if not details:
            raise HTTPException(status_code=404, detail="Agent not found")
        
        return details
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Failed to get agent details: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

@app.delete("/api/v2/agents/{agent_id}")
async def unregister_agent(
    agent_id: str,
    token: str = Depends(verify_token)
):
    """Unregister an agent from the platform"""
    try:
        success = await platform.agent_registry.unregister(agent_id)
        
        if not success:
            raise HTTPException(status_code=404, detail="Agent not found")
        
        # Release resources
        await platform.resource_manager.release_resources(agent_id)
        
        # Broadcast update
        await broadcast_update({
            "event": "agent_unregistered",
            "agent_id": agent_id
        })
        
        return {"status": "unregistered", "agent_id": agent_id}
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Failed to unregister agent: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

# =============================
# Task Management Endpoints
# =============================

@app.post("/api/v2/tasks/submit", response_model=Dict[str, str])
async def submit_task(
    request: TaskSubmissionRequest,
    token: str = Depends(verify_token)
):
    """Submit a task to the platform"""
    try:
        # Create task
        task = UnifiedTask(
            task_id=str(uuid.uuid4()),
            task_type=request.task_type,
            priority=request.priority,
            payload=request.payload,
            required_capabilities=request.required_capabilities,
            deadline=request.deadline,
            dependencies=request.dependencies
        )
        
        # Submit to platform
        task_id = await platform.submit_task(task)
        
        # Broadcast update
        await broadcast_update({
            "event": "task_submitted",
            "task_id": task_id,
            "task_type": request.task_type
        })
        
        return {
            "task_id": task_id,
            "status": "submitted",
            "message": "Task successfully submitted for execution"
        }
        
    except Exception as e:
        logger.error(f"Task submission failed: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/v2/tasks/{task_id}", response_model=Dict[str, Any])
async def get_task_status(
    task_id: str,
    token: str = Depends(verify_token)
):
    """Get the status of a submitted task"""
    try:
        status = await platform.get_task_status(task_id)
        
        if not status:
            raise HTTPException(status_code=404, detail="Task not found")
        
        return status
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Failed to get task status: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/api/v2/tasks/batch", response_model=List[Dict[str, str]])
async def submit_batch_tasks(
    tasks: List[TaskSubmissionRequest],
    token: str = Depends(verify_token)
):
    """Submit multiple tasks as a batch"""
    try:
        results = []
        
        for task_request in tasks:
            task = UnifiedTask(
                task_id=str(uuid.uuid4()),
                task_type=task_request.task_type,
                priority=task_request.priority,
                payload=task_request.payload,
                required_capabilities=task_request.required_capabilities,
                deadline=task_request.deadline,
                dependencies=task_request.dependencies
            )
            
            task_id = await platform.submit_task(task)
            results.append({
                "task_id": task_id,
                "status": "submitted"
            })
        
        return results
        
    except Exception as e:
        logger.error(f"Batch task submission failed: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

# =============================
# Marketplace Endpoints
# =============================

@app.post("/api/v2/marketplace/publish", response_model=Dict[str, str])
async def publish_to_marketplace(
    request: MarketplaceListingRequest,
    token: str = Depends(verify_token)
):
    """Publish an agent to the marketplace"""
    try:
        success = await platform.marketplace.publish_agent(
            request.agent_id,
            request.description,
            request.pricing,
            request.keywords
        )
        
        if not success:
            raise HTTPException(status_code=400, detail="Failed to publish agent")
        
        return {
            "status": "published",
            "agent_id": request.agent_id,
            "message": "Agent successfully published to marketplace"
        }
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Marketplace publishing failed: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/api/v2/marketplace/search", response_model=List[Dict[str, Any]])
async def search_marketplace(
    request: AgentSearchRequest,
    token: str = Depends(verify_token)
):
    """Search for agents in the marketplace"""
    try:
        listings = await platform.marketplace.search_agents(
            request.query,
            request.min_rating,
            request.max_price
        )
        
        return [
            {
                "agent_id": listing.agent_id,
                "name": listing.metadata.name,
                "description": listing.description,
                "capabilities": listing.metadata.capabilities,
                "pricing": listing.pricing,
                "average_rating": listing.average_rating,
                "total_usage": listing.total_usage
            }
            for listing in listings
        ]
        
    except Exception as e:
        logger.error(f"Marketplace search failed: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/api/v2/marketplace/rate/{agent_id}")
async def rate_agent(
    agent_id: str,
    rating: float = Field(..., ge=1.0, le=5.0),
    review: Optional[str] = None,
    token: str = Depends(verify_token)
):
    """Rate an agent in the marketplace"""
    try:
        listing = platform.marketplace.listings.get(agent_id)
        
        if not listing:
            raise HTTPException(status_code=404, detail="Agent not found in marketplace")
        
        listing.add_rating(rating, review, "anonymous")  # Would use actual user ID
        
        return {
            "status": "rated",
            "agent_id": agent_id,
            "new_average_rating": listing.average_rating
        }
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Agent rating failed: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

# =============================
# Monitoring & Analytics Endpoints
# =============================

@app.get("/api/v2/dashboard/overview", response_model=Dict[str, Any])
async def get_system_overview(token: str = Depends(verify_token)):
    """Get system-wide metrics and overview"""
    try:
        overview = await platform.dashboard.get_system_overview()
        return overview
        
    except Exception as e:
        logger.error(f"Failed to get system overview: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/v2/resources/utilization", response_model=Dict[str, float])
async def get_resource_utilization(token: str = Depends(verify_token)):
    """Get current resource utilization"""
    try:
        utilization = platform.resource_manager.get_resource_utilization()
        
        return {
            resource: percentage
            for resource, percentage in utilization.items()
        }
        
    except Exception as e:
        logger.error(f"Failed to get resource utilization: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/api/v2/conflicts/report", response_model=Dict[str, str])
async def report_conflict(
    request: ConflictReportRequest,
    token: str = Depends(verify_token)
):
    """Report a conflict for resolution"""
    try:
        conflict = Conflict(
            conflict_id=str(uuid.uuid4()),
            conflict_type=request.conflict_type,
            involved_agents=request.involved_agents,
            description=request.description,
            context=request.context
        )
        
        conflict_id = await platform.conflict_resolver.report_conflict(conflict)
        
        return {
            "conflict_id": conflict_id,
            "status": "reported",
            "resolution": conflict.resolution if conflict.resolved else None
        }
        
    except Exception as e:
        logger.error(f"Conflict reporting failed: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

# =============================
# WebSocket Endpoints
# =============================

@app.websocket("/ws/dashboard")
async def dashboard_websocket(websocket: WebSocket):
    """WebSocket endpoint for real-time dashboard updates"""
    await websocket.accept()
    websocket_connections.add(websocket)
    
    try:
        # Send initial system overview
        overview = await platform.dashboard.get_system_overview()
        await websocket.send_json({"type": "overview", "data": overview})
        
        # Keep connection alive and send periodic updates
        while True:
            # Wait for messages or send periodic updates
            try:
                message = await asyncio.wait_for(websocket.receive_text(), timeout=30.0)
                # Handle client messages if needed
                
            except asyncio.TimeoutError:
                # Send periodic update
                overview = await platform.dashboard.get_system_overview()
                await websocket.send_json({"type": "overview", "data": overview})
                
    except WebSocketDisconnect:
        websocket_connections.remove(websocket)
    except Exception as e:
        logger.error(f"WebSocket error: {str(e)}")
        websocket_connections.discard(websocket)

async def broadcast_update(update: Dict[str, Any]):
    """Broadcast update to all connected WebSocket clients"""
    disconnected = set()
    
    for websocket in websocket_connections:
        try:
            await websocket.send_json({"type": "update", "data": update})
        except:
            disconnected.add(websocket)
    
    # Remove disconnected clients
    websocket_connections.difference_update(disconnected)

# =============================
# HTML Dashboard
# =============================

@app.get("/dashboard", response_class=HTMLResponse)
async def dashboard():
    """Serve the web dashboard"""
    return """
    <!DOCTYPE html>
    <html>
    <head>
        <title>Multi-Agent Collaboration Dashboard</title>
        <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
        <style>
            body {
                font-family: Arial, sans-serif;
                margin: 20px;
                background-color: #f5f5f5;
            }
            .container {
                max-width: 1200px;
                margin: 0 auto;
            }
            .metrics-grid {
                display: grid;
                grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
                gap: 20px;
                margin-bottom: 30px;
            }
            .metric-card {
                background: white;
                padding: 20px;
                border-radius: 8px;
                box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }
            .metric-value {
                font-size: 2em;
                font-weight: bold;
                color: #2196F3;
            }
            .metric-label {
                color: #666;
                margin-top: 5px;
            }
            .chart-container {
                background: white;
                padding: 20px;
                border-radius: 8px;
                box-shadow: 0 2px 4px rgba(0,0,0,0.1);
                margin-bottom: 20px;
            }
            .status-indicator {
                width: 10px;
                height: 10px;
                border-radius: 50%;
                display: inline-block;
                margin-right: 5px;
            }
            .status-active { background-color: #4CAF50; }
            .status-idle { background-color: #FFC107; }
            .status-offline { background-color: #F44336; }
            #connectionStatus {
                position: fixed;
                top: 20px;
                right: 20px;
                padding: 10px 20px;
                border-radius: 5px;
                font-weight: bold;
            }
            .connected { background-color: #4CAF50; color: white; }
            .disconnected { background-color: #F44336; color: white; }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>Multi-Agent Collaboration Platform</h1>
            
            <div id="connectionStatus" class="disconnected">Disconnected</div>
            
            <div class="metrics-grid" id="metricsGrid">
                <div class="metric-card">
                    <div class="metric-value" id="totalAgents">0</div>
                    <div class="metric-label">Total Agents</div>
                </div>
                <div class="metric-card">
                    <div class="metric-value" id="activeAgents">0</div>
                    <div class="metric-label">Active Agents</div>
                </div>
                <div class="metric-card">
                    <div class="metric-value" id="tasksCompleted">0</div>
                    <div class="metric-label">Tasks Completed</div>
                </div>
                <div class="metric-card">
                    <div class="metric-value" id="successRate">0%</div>
                    <div class="metric-label">Success Rate</div>
                </div>
            </div>
            
            <div class="chart-container">
                <h3>Agent Status Distribution</h3>
                <canvas id="statusChart" width="400" height="200"></canvas>
            </div>
            
            <div class="chart-container">
                <h3>Resource Utilization</h3>
                <canvas id="resourceChart" width="400" height="200"></canvas>
            </div>
            
            <div class="chart-container">
                <h3>Real-time Events</h3>
                <div id="eventLog" style="max-height: 200px; overflow-y: auto;">
                    <!-- Events will be added here -->
                </div>
            </div>
        </div>
        
        <script>
            // WebSocket connection
            let ws = null;
            const wsUrl = `ws://${window.location.host}/ws/dashboard`;
            
            // Charts
            let statusChart = null;
            let resourceChart = null;
            
            function connectWebSocket() {
                ws = new WebSocket(wsUrl);
                
                ws.onopen = () => {
                    document.getElementById('connectionStatus').className = 'connected';
                    document.getElementById('connectionStatus').textContent = 'Connected';
                };
                
                ws.onclose = () => {
                    document.getElementById('connectionStatus').className = 'disconnected';
                    document.getElementById('connectionStatus').textContent = 'Disconnected';
                    // Reconnect after 3 seconds
                    setTimeout(connectWebSocket, 3000);
                };
                
                ws.onmessage = (event) => {
                    const message = JSON.parse(event.data);
                    
                    if (message.type === 'overview') {
                        updateDashboard(message.data);
                    } else if (message.type === 'update') {
                        addEvent(message.data);
                    }
                };
            }
            
            function updateDashboard(data) {
                // Update metrics
                document.getElementById('totalAgents').textContent = data.total_agents;
                document.getElementById('activeAgents').textContent = data.active_agents;
                document.getElementById('tasksCompleted').textContent = 
                    data.performance_summary.total_tasks_completed;
                document.getElementById('successRate').textContent = 
                    (data.performance_summary.overall_success_rate * 100).toFixed(1) + '%';
                
                // Update status chart
                updateStatusChart(data.agent_breakdown.by_status);
                
                // Update resource chart (would need additional API call)
                fetchResourceUtilization();
            }
            
            function updateStatusChart(statusData) {
                const ctx = document.getElementById('statusChart').getContext('2d');
                
                if (statusChart) {
                    statusChart.destroy();
                }
                
                statusChart = new Chart(ctx, {
                    type: 'doughnut',
                    data: {
                        labels: Object.keys(statusData),
                        datasets: [{
                            data: Object.values(statusData),
                            backgroundColor: [
                                '#4CAF50',
                                '#2196F3',
                                '#FFC107',
                                '#F44336',
                                '#9C27B0'
                            ]
                        }]
                    },
                    options: {
                        responsive: true,
                        maintainAspectRatio: false
                    }
                });
            }
            
            async function fetchResourceUtilization() {
                try {
                    const response = await fetch('/api/v2/resources/utilization', {
                        headers: {
                            'Authorization': 'Bearer valid_token'
                        }
                    });
                    const data = await response.json();
                    updateResourceChart(data);
                } catch (error) {
                    console.error('Failed to fetch resource utilization:', error);
                }
            }
            
            function updateResourceChart(resourceData) {
                const ctx = document.getElementById('resourceChart').getContext('2d');
                
                if (resourceChart) {
                    resourceChart.destroy();
                }
                
                resourceChart = new Chart(ctx, {
                    type: 'bar',
                    data: {
                        labels: Object.keys(resourceData),
                        datasets: [{
                            label: 'Utilization %',
                            data: Object.values(resourceData),
                            backgroundColor: '#2196F3'
                        }]
                    },
                    options: {
                        responsive: true,
                        maintainAspectRatio: false,
                        scales: {
                            y: {
                                beginAtZero: true,
                                max: 100
                            }
                        }
                    }
                });
            }
            
            function addEvent(event) {
                const eventLog = document.getElementById('eventLog');
                const eventElement = document.createElement('div');
                eventElement.style.padding = '10px';
                eventElement.style.borderBottom = '1px solid #eee';
                
                const timestamp = new Date().toLocaleTimeString();
                eventElement.innerHTML = `
                    <strong>${timestamp}</strong> - 
                    ${event.event}: ${JSON.stringify(event)}
                `;
                
                eventLog.insertBefore(eventElement, eventLog.firstChild);
                
                // Keep only last 50 events
                while (eventLog.children.length > 50) {
                    eventLog.removeChild(eventLog.lastChild);
                }
            }
            
            // Initialize
            connectWebSocket();
        </script>
    </body>
    </html>
    """

if __name__ == "__main__":
    import uvicorn
    import os
    
    port = int(os.getenv("PORT", 8080))
    uvicorn.run(app, host="0.0.0.0", port=port)