Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import json
|
| 3 |
import logging
|
| 4 |
from enum import Enum, auto
|
| 5 |
-
from typing import Protocol, List, Dict, Any
|
| 6 |
from dataclasses import dataclass, field
|
| 7 |
from datetime import datetime
|
| 8 |
import difflib
|
|
@@ -22,26 +22,321 @@ class Config:
|
|
| 22 |
log_level: str = "INFO"
|
| 23 |
model_settings: Dict[str, Any] = field(default_factory=dict)
|
| 24 |
api_keys: Dict[str, str] = field(default_factory=dict)
|
|
|
|
| 25 |
|
| 26 |
def __post_init__(self):
|
| 27 |
"""Validate configuration after initialization"""
|
| 28 |
-
if not
|
| 29 |
raise ValueError("RAG system path must be specified in config")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
class
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
class AgentRole(Enum):
|
| 47 |
ARCHITECT = auto()
|
|
@@ -51,6 +346,7 @@ class AgentRole(Enum):
|
|
| 51 |
TESTER = auto()
|
| 52 |
REVIEWER = auto()
|
| 53 |
DEPLOYER = auto()
|
|
|
|
| 54 |
|
| 55 |
@dataclass
|
| 56 |
class AgentDecision:
|
|
@@ -60,548 +356,432 @@ class AgentDecision:
|
|
| 60 |
reasoning: str
|
| 61 |
timestamp: datetime = field(default_factory=datetime.now)
|
| 62 |
dependencies: List['AgentDecision'] = field(default_factory=list)
|
|
|
|
| 63 |
|
| 64 |
-
class AgentProtocol(Protocol):
|
| 65 |
-
async def decide(self, context: Dict[str, Any]) -> AgentDecision: ...
|
| 66 |
-
async def validate(self, decision: AgentDecision) -> bool: ...
|
| 67 |
-
async def implement(self, decision: AgentDecision) -> Any: ...
|
| 68 |
-
async def test(self, implementation: Any) -> bool: ...
|
| 69 |
-
|
| 70 |
-
@dataclass
|
| 71 |
class Agent:
|
| 72 |
-
role: AgentRole
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
prompt = f"""
|
| 85 |
-
As {self.name}, a {self.role.name} expert with expertise in {', '.join(self.expertise)},
|
| 86 |
-
analyze the following context and provide reasoning:
|
| 87 |
-
|
| 88 |
-
Context:
|
| 89 |
-
{json.dumps(context, indent=2)}
|
| 90 |
-
|
| 91 |
-
Consider:
|
| 92 |
-
1. Required components and their interactions
|
| 93 |
-
2. Potential challenges and solutions
|
| 94 |
-
3. Best practices and patterns
|
| 95 |
-
4. Security and performance implications
|
| 96 |
-
|
| 97 |
-
Reasoning:
|
| 98 |
-
"""
|
| 99 |
-
return await self.rag_system.generate_reasoning(prompt)
|
| 100 |
-
|
| 101 |
-
async def decide(self, context: Dict[str, Any]) -> AgentDecision:
|
| 102 |
-
"""Make a decision based on context and expertise"""
|
| 103 |
-
reasoning = await self.reason(context)
|
| 104 |
-
confidence = 0.8 # Placeholder for actual confidence calculation
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
self.agents: Dict[AgentRole, Agent] = self._initialize_agents()
|
| 119 |
-
self.decision_history: List[AgentDecision] = []
|
| 120 |
-
self.executor = ThreadPoolExecutor(max_workers=config.max_workers)
|
| 121 |
-
self.validator = AgentValidator()
|
| 122 |
-
self.tester = AgentTester()
|
| 123 |
|
| 124 |
-
def
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
),
|
| 132 |
-
|
| 133 |
-
role=AgentRole.FRONTEND,
|
| 134 |
-
name="Frontend Developer",
|
| 135 |
-
autonomy_level=self.autonomy_level,
|
| 136 |
-
expertise=["UI/UX", "React", "Vue", "Angular"]
|
| 137 |
-
),
|
| 138 |
-
AgentRole.BACKEND: Agent(
|
| 139 |
-
role=AgentRole.BACKEND,
|
| 140 |
-
name="Backend Developer",
|
| 141 |
-
autonomy_level=self.autonomy_level,
|
| 142 |
-
expertise=["API design", "database", "security"]
|
| 143 |
-
),
|
| 144 |
-
AgentRole.TESTER: Agent(
|
| 145 |
-
role=AgentRole.TESTER,
|
| 146 |
-
name="Quality Assurance",
|
| 147 |
-
autonomy_level=self.autonomy_level,
|
| 148 |
-
expertise=["testing", "automation", "quality assurance"]
|
| 149 |
-
),
|
| 150 |
-
AgentRole.REVIEWER: Agent(
|
| 151 |
-
role=AgentRole.REVIEWER,
|
| 152 |
-
name="Code Reviewer",
|
| 153 |
-
autonomy_level=self.autonomy_level,
|
| 154 |
-
expertise=["code quality", "best practices", "security"]
|
| 155 |
-
),
|
| 156 |
}
|
| 157 |
|
| 158 |
-
#
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
"""Update autonomy level for all agents"""
|
| 166 |
-
self.autonomy_level = max(0.0, min(10.0, level))
|
| 167 |
-
for agent in self.agents.values():
|
| 168 |
-
agent.autonomy_level = self.autonomy_level
|
| 169 |
-
|
| 170 |
-
async def process_request(self, description: str, context: Dict[str, Any] = None) -> Dict[str, Any]:
|
| 171 |
-
"""Process a user request with current autonomy level"""
|
| 172 |
-
try:
|
| 173 |
-
context = context or {}
|
| 174 |
-
context['description'] = description
|
| 175 |
-
context['autonomy_level'] = self.autonomy_level
|
| 176 |
-
|
| 177 |
-
# Start with architect's decision
|
| 178 |
-
arch_decision = await self.agents[AgentRole.ARCHITECT].decide(context)
|
| 179 |
-
self.decision_history.append(arch_decision)
|
| 180 |
-
|
| 181 |
-
if self.autonomy_level < 3:
|
| 182 |
-
# Low autonomy: Wait for user confirmation
|
| 183 |
-
return {
|
| 184 |
-
'status': 'pending_confirmation',
|
| 185 |
-
'decision': arch_decision,
|
| 186 |
-
'next_steps': self._get_next_steps(arch_decision)
|
| 187 |
-
}
|
| 188 |
-
|
| 189 |
-
# Medium to high autonomy: Proceed with implementation
|
| 190 |
-
implementation_plan = await self._create_implementation_plan(arch_decision)
|
| 191 |
-
|
| 192 |
-
if self.autonomy_level >= 7:
|
| 193 |
-
# High autonomy: Automatic implementation and testing
|
| 194 |
-
return await self._automated_implementation(implementation_plan)
|
| 195 |
-
|
| 196 |
-
# Medium autonomy: Return plan for user review
|
| 197 |
return {
|
| 198 |
-
'status': '
|
| 199 |
-
'
|
| 200 |
-
'
|
|
|
|
| 201 |
}
|
| 202 |
-
|
| 203 |
-
except Exception as e:
|
| 204 |
-
logger.error(f"Error in request processing: {e}")
|
| 205 |
-
return {'status': 'error', 'message': str(e)}
|
| 206 |
-
|
| 207 |
-
async def _create_implementation_plan(self, arch_decision: AgentDecision) -> Dict[str, Any]:
|
| 208 |
-
"""Create detailed implementation plan based on architect's decision"""
|
| 209 |
-
tasks = []
|
| 210 |
-
|
| 211 |
-
# Frontend tasks
|
| 212 |
-
if 'frontend' in arch_decision.decision.lower():
|
| 213 |
-
tasks.append(self._create_frontend_tasks(arch_decision))
|
| 214 |
-
|
| 215 |
-
# Backend tasks
|
| 216 |
-
if 'backend' in arch_decision.decision.lower():
|
| 217 |
-
tasks.append(self._create_backend_tasks(arch_decision))
|
| 218 |
-
|
| 219 |
-
# Testing tasks
|
| 220 |
-
tasks.append(self._create_testing_tasks(arch_decision))
|
| 221 |
-
|
| 222 |
return {
|
| 223 |
-
'
|
| 224 |
-
'
|
| 225 |
-
'
|
| 226 |
}
|
| 227 |
|
| 228 |
-
async def
|
| 229 |
-
"""Create frontend implementation tasks"""
|
| 230 |
return {
|
| 231 |
-
'
|
| 232 |
-
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
}
|
| 235 |
|
| 236 |
-
async def
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
-
async def
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
return {
|
| 247 |
-
'
|
| 248 |
-
'
|
| 249 |
-
'
|
| 250 |
}
|
| 251 |
|
| 252 |
-
def
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
}
|
| 265 |
|
|
|
|
| 266 |
try:
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
impl_tasks.append(self._implement_backend(plan['tasks']['backend']))
|
| 273 |
-
|
| 274 |
-
implementations = await asyncio.gather(*impl_tasks)
|
| 275 |
-
|
| 276 |
-
# Testing
|
| 277 |
-
test_results = await self.agents[AgentRole.TESTER].test(implementations)
|
| 278 |
-
|
| 279 |
-
# Code review
|
| 280 |
-
review_results = await self.agents[AgentRole.REVIEWER].validate({
|
| 281 |
-
'implementations': implementations,
|
| 282 |
-
'test_results': test_results
|
| 283 |
-
})
|
| 284 |
-
|
| 285 |
-
return {
|
| 286 |
-
'status': 'completed',
|
| 287 |
-
'implementations': implementations,
|
| 288 |
-
'test_results': test_results,
|
| 289 |
-
'review': review_results,
|
| 290 |
-
'decisions': self.decision_history
|
| 291 |
-
}
|
| 292 |
-
|
| 293 |
-
except Exception as e:
|
| 294 |
-
return {
|
| 295 |
-
'status': 'error',
|
| 296 |
-
'message': str(e),
|
| 297 |
-
'partial_results': results
|
| 298 |
}
|
| 299 |
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
async def _implement_backend(self, tasks: Dict[str, Any]) -> Dict[str, Any]:
|
| 305 |
-
"""Implement backend components"""
|
| 306 |
-
return {'endpoints': [], 'status': 'implemented'}
|
| 307 |
-
|
| 308 |
-
def _get_next_steps(self, decision: AgentDecision) -> List[str]:
|
| 309 |
-
"""Get next steps based on decision"""
|
| 310 |
-
return [
|
| 311 |
-
f"Review {decision.decision}",
|
| 312 |
-
"Provide feedback on the proposed approach",
|
| 313 |
-
"Approve or request changes"
|
| 314 |
-
]
|
| 315 |
-
|
| 316 |
-
async def _handle_implementation_failure(self, error: Exception, context: Dict[str, Any]) -> Dict[str, Any]:
|
| 317 |
-
"""Handle implementation failures with adaptive response"""
|
| 318 |
-
try:
|
| 319 |
-
# Analyze error
|
| 320 |
-
error_analysis = await self.agents[AgentRole.REVIEWER].reason({
|
| 321 |
-
'error': str(error),
|
| 322 |
-
'context': context
|
| 323 |
-
})
|
| 324 |
|
| 325 |
-
#
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
if correction['success']:
|
| 330 |
-
return await self.process_request(context['description'], correction['context'])
|
| 331 |
|
|
|
|
|
|
|
|
|
|
| 332 |
return {
|
| 333 |
-
'status': '
|
| 334 |
-
'
|
| 335 |
-
'
|
| 336 |
-
'
|
| 337 |
}
|
| 338 |
|
| 339 |
except Exception as e:
|
| 340 |
-
logger.error(f"Error
|
| 341 |
-
return {'status': '
|
| 342 |
-
|
| 343 |
-
async def _attempt_automatic_correction(self, error_analysis: Dict[str, Any]) -> Dict[str, Any]:
|
| 344 |
-
"""Attempt to automatically correct implementation issues"""
|
| 345 |
-
return {
|
| 346 |
-
'success': False,
|
| 347 |
-
'context': {},
|
| 348 |
-
'message': 'Automatic correction not implemented'
|
| 349 |
-
}
|
| 350 |
-
|
| 351 |
-
def _suggest_corrections(self, error_analysis: Dict[str, Any]) -> List[str]:
|
| 352 |
-
"""Generate suggested corrections based on error analysis"""
|
| 353 |
-
return [
|
| 354 |
-
"Review error details",
|
| 355 |
-
"Check implementation requirements",
|
| 356 |
-
"Verify dependencies"
|
| 357 |
-
]
|
| 358 |
-
|
| 359 |
-
class AgentTester:
|
| 360 |
-
def __init__(self):
|
| 361 |
-
self.test_suites = {
|
| 362 |
-
'frontend': self._test_frontend,
|
| 363 |
-
'backend': self._test_backend,
|
| 364 |
-
'integration': self._test_integration
|
| 365 |
-
}
|
| 366 |
-
|
| 367 |
-
async def _test_frontend(self, implementation: Dict[str, Any]) -> Dict[str, Any]:
|
| 368 |
-
"""Run frontend tests"""
|
| 369 |
-
results = {
|
| 370 |
-
'passed': [],
|
| 371 |
-
'failed': [],
|
| 372 |
-
'warnings': []
|
| 373 |
-
}
|
| 374 |
-
|
| 375 |
-
# Component rendering tests
|
| 376 |
-
for component in implementation.get('components', []):
|
| 377 |
-
try:
|
| 378 |
-
# Test component rendering
|
| 379 |
-
result = await self._test_component_render(component)
|
| 380 |
-
if result['success']:
|
| 381 |
-
results['passed'].append(f"Component {component['name']} renders correctly")
|
| 382 |
-
else:
|
| 383 |
-
results['failed'].append(f"Component {component['name']}: {result['error']}")
|
| 384 |
-
except Exception as e:
|
| 385 |
-
results['failed'].append(f"Error testing {component['name']}: {str(e)}")
|
| 386 |
|
| 387 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
'
|
| 393 |
-
'failed': [],
|
| 394 |
-
'warnings': []
|
| 395 |
}
|
| 396 |
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
try:
|
| 400 |
-
# Test endpoint functionality
|
| 401 |
-
result = await self._test_endpoint(endpoint)
|
| 402 |
-
if result['success']:
|
| 403 |
-
results['passed'].append(f"Endpoint {endpoint['path']} works correctly")
|
| 404 |
-
else:
|
| 405 |
-
results['failed'].append(f"Endpoint {endpoint['path']}: {result['error']}")
|
| 406 |
-
except Exception as e:
|
| 407 |
-
results['failed'].append(f"Error testing {endpoint['path']}: {str(e)}")
|
| 408 |
-
|
| 409 |
-
return results
|
| 410 |
|
| 411 |
-
async def
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
'warnings': []
|
| 417 |
-
}
|
| 418 |
|
| 419 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
try:
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
else:
|
| 425 |
-
results['failed'].append(f"Integration error: {result['error']}")
|
| 426 |
except Exception as e:
|
| 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 |
-
return {
|
| 461 |
-
'success': False,
|
| 462 |
-
'error': str(e),
|
| 463 |
-
'details': None
|
| 464 |
-
}
|
| 465 |
-
|
| 466 |
|
| 467 |
-
async def
|
| 468 |
-
"""Run backend tests"""
|
| 469 |
results = {
|
| 470 |
'passed': [],
|
| 471 |
'failed': [],
|
| 472 |
-
'
|
|
|
|
| 473 |
}
|
| 474 |
|
| 475 |
-
|
| 476 |
-
for endpoint in implementation.get('endpoints', []):
|
| 477 |
try:
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 482 |
else:
|
| 483 |
-
results['failed'].append(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
except Exception as e:
|
| 485 |
-
results['failed'].append(
|
|
|
|
|
|
|
|
|
|
| 486 |
|
| 487 |
return results
|
| 488 |
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
}
|
|
|
|
| 496 |
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
if result['success']:
|
| 501 |
-
results['passed'].append("Frontend-Backend integration successful")
|
| 502 |
-
else:
|
| 503 |
-
results['failed'].append(f"Integration error: {result['error']}")
|
| 504 |
-
except Exception as e:
|
| 505 |
-
results['failed'].append(f"Integration test error: {str(e)}")
|
| 506 |
-
|
| 507 |
-
return results
|
| 508 |
-
|
| 509 |
-
async def _test_component_render(self, component: Dict[str, Any]) -> Dict[str, Any]:
|
| 510 |
-
"""Test component rendering"""
|
| 511 |
-
# Placeholder for actual component rendering test
|
| 512 |
-
return {'success': True, 'error': None}
|
| 513 |
-
|
| 514 |
-
async def _test_endpoint(self, endpoint: Dict[str, Any]) -> Dict[str, Any]:
|
| 515 |
-
"""Test endpoint functionality"""
|
| 516 |
-
# Placeholder for actual endpoint test
|
| 517 |
-
return {'success': True, 'error': None}
|
| 518 |
-
|
| 519 |
-
async def _test_component_render(self, component: Dict[str, Any]) -> Dict[str, Any]:
|
| 520 |
-
"""Test component rendering"""
|
| 521 |
-
# Placeholder for actual component rendering test
|
| 522 |
-
return {'success': True, 'error': None}
|
| 523 |
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
# Placeholder for actual endpoint test
|
| 527 |
-
return {'success': True, 'error': None}
|
| 528 |
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
async def _validate_code_quality(self, code: str) -> Dict[str, Any]:
|
| 544 |
-
"""Validate code quality metrics"""
|
| 545 |
-
results = {
|
| 546 |
-
'passed': [],
|
| 547 |
-
'failed': [],
|
| 548 |
-
'warnings': []
|
| 549 |
-
}
|
| 550 |
-
|
| 551 |
-
# Add code quality validation logic here
|
| 552 |
-
return results
|
| 553 |
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
# Add security validation logic here
|
| 563 |
-
return results
|
| 564 |
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 571 |
}
|
| 572 |
-
|
| 573 |
-
# Add performance validation logic here
|
| 574 |
-
return results
|
| 575 |
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
|
|
|
|
|
|
|
|
|
|
| 585 |
|
| 586 |
-
# Example usage
|
| 587 |
if __name__ == "__main__":
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
import logging
|
| 4 |
from enum import Enum, auto
|
| 5 |
+
from typing import Protocol, List, Dict, Any, Optional
|
| 6 |
from dataclasses import dataclass, field
|
| 7 |
from datetime import datetime
|
| 8 |
import difflib
|
|
|
|
| 22 |
log_level: str = "INFO"
|
| 23 |
model_settings: Dict[str, Any] = field(default_factory=dict)
|
| 24 |
api_keys: Dict[str, str] = field(default_factory=dict)
|
| 25 |
+
design_system_config: Dict[str, Any] = field(default_factory=dict)
|
| 26 |
|
| 27 |
def __post_init__(self):
|
| 28 |
"""Validate configuration after initialization"""
|
| 29 |
+
if not self.rag_system_path:
|
| 30 |
raise ValueError("RAG system path must be specified in config")
|
| 31 |
+
if not self.design_system_config:
|
| 32 |
+
self.design_system_config = {
|
| 33 |
+
'theme': 'light',
|
| 34 |
+
'responsive': True,
|
| 35 |
+
'accessibility_level': 'AAA'
|
| 36 |
+
}
|
| 37 |
|
| 38 |
+
class DesignSystem:
|
| 39 |
+
def __init__(self):
|
| 40 |
+
self.theme = {
|
| 41 |
+
'colors': {
|
| 42 |
+
'primary': '#007bff',
|
| 43 |
+
'secondary': '#6c757d',
|
| 44 |
+
'success': '#28a745',
|
| 45 |
+
'danger': '#dc3545',
|
| 46 |
+
'warning': '#ffc107',
|
| 47 |
+
'info': '#17a2b8',
|
| 48 |
+
'light': '#f8f9fa',
|
| 49 |
+
'dark': '#343a40'
|
| 50 |
+
},
|
| 51 |
+
'typography': {
|
| 52 |
+
'fontFamily': {
|
| 53 |
+
'primary': '"Helvetica Neue", Arial, sans-serif',
|
| 54 |
+
'secondary': 'Georgia, serif',
|
| 55 |
+
'monospace': 'Monaco, Consolas, monospace'
|
| 56 |
+
},
|
| 57 |
+
'fontSizes': {
|
| 58 |
+
'xs': '12px',
|
| 59 |
+
'sm': '14px',
|
| 60 |
+
'base': '16px',
|
| 61 |
+
'lg': '18px',
|
| 62 |
+
'xl': '20px',
|
| 63 |
+
'xxl': '24px',
|
| 64 |
+
'display': '32px'
|
| 65 |
+
},
|
| 66 |
+
'lineHeight': {
|
| 67 |
+
'tight': '1.25',
|
| 68 |
+
'normal': '1.5',
|
| 69 |
+
'relaxed': '1.75'
|
| 70 |
+
}
|
| 71 |
+
},
|
| 72 |
+
'spacing': {
|
| 73 |
+
'unit': '8px',
|
| 74 |
+
'scales': {
|
| 75 |
+
'xs': '0.25rem',
|
| 76 |
+
'sm': '0.5rem',
|
| 77 |
+
'md': '1rem',
|
| 78 |
+
'lg': '1.5rem',
|
| 79 |
+
'xl': '2rem'
|
| 80 |
+
}
|
| 81 |
+
},
|
| 82 |
+
'breakpoints': {
|
| 83 |
+
'mobile': '320px',
|
| 84 |
+
'tablet': '768px',
|
| 85 |
+
'desktop': '1024px',
|
| 86 |
+
'large': '1440px'
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
|
| 90 |
+
def get_color(self, color_name: str) -> str:
|
| 91 |
+
return self.theme['colors'].get(color_name)
|
| 92 |
+
|
| 93 |
+
def get_font_family(self, type_name: str) -> str:
|
| 94 |
+
return self.theme['typography']['fontFamily'].get(type_name)
|
| 95 |
+
|
| 96 |
+
def get_spacing(self, scale: str) -> str:
|
| 97 |
+
return self.theme['spacing']['scales'].get(scale)
|
| 98 |
+
|
| 99 |
+
class UIComponentLibrary:
|
| 100 |
+
def __init__(self, design_system: DesignSystem):
|
| 101 |
+
self.design_system = design_system
|
| 102 |
+
|
| 103 |
+
def get_component_template(self, component_type: str) -> Dict[str, Any]:
|
| 104 |
+
components = {
|
| 105 |
+
'button': {
|
| 106 |
+
'template': '''
|
| 107 |
+
<button class="btn {{variant}}" {{attributes}}>
|
| 108 |
+
{{text}}
|
| 109 |
+
</button>
|
| 110 |
+
''',
|
| 111 |
+
'styles': self._get_button_styles()
|
| 112 |
+
},
|
| 113 |
+
'card': {
|
| 114 |
+
'template': '''
|
| 115 |
+
<div class="card {{variant}}">
|
| 116 |
+
<div class="card-header">{{header}}</div>
|
| 117 |
+
<div class="card-body">{{content}}</div>
|
| 118 |
+
<div class="card-footer">{{footer}}</div>
|
| 119 |
+
</div>
|
| 120 |
+
''',
|
| 121 |
+
'styles': self._get_card_styles()
|
| 122 |
+
},
|
| 123 |
+
'input': {
|
| 124 |
+
'template': '''
|
| 125 |
+
<div class="form-group">
|
| 126 |
+
<label for="{{id}}">{{label}}</label>
|
| 127 |
+
<input type="{{type}}" id="{{id}}"
|
| 128 |
+
class="form-control {{variant}}" {{attributes}}>
|
| 129 |
+
<small class="form-text">{{helper_text}}</small>
|
| 130 |
+
</div>
|
| 131 |
+
''',
|
| 132 |
+
'styles': self._get_input_styles()
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
return components.get(component_type)
|
| 136 |
+
|
| 137 |
+
def _get_button_styles(self) -> Dict[str, str]:
|
| 138 |
+
return {
|
| 139 |
+
'base': f'''
|
| 140 |
+
font-family: {self.design_system.get_font_family('primary')};
|
| 141 |
+
padding: {self.design_system.get_spacing('sm')} {self.design_system.get_spacing('md')};
|
| 142 |
+
border-radius: 4px;
|
| 143 |
+
border: none;
|
| 144 |
+
cursor: pointer;
|
| 145 |
+
transition: all 0.3s ease;
|
| 146 |
+
''',
|
| 147 |
+
'primary': f'background-color: {self.design_system.get_color("primary")};',
|
| 148 |
+
'secondary': f'background-color: {self.design_system.get_color("secondary")};'
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
def _get_card_styles(self) -> Dict[str, str]:
|
| 152 |
+
return {
|
| 153 |
+
'base': f'''
|
| 154 |
+
border-radius: 8px;
|
| 155 |
+
padding: {self.design_system.get_spacing('md')};
|
| 156 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 157 |
+
'''
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
def _get_input_styles(self) -> Dict[str, str]:
|
| 161 |
+
return {
|
| 162 |
+
'base': f'''
|
| 163 |
+
font-family: {self.design_system.get_font_family('primary')};
|
| 164 |
+
padding: {self.design_system.get_spacing('sm')};
|
| 165 |
+
border-radius: 4px;
|
| 166 |
+
border: 1px solid {self.design_system.get_color('secondary')};
|
| 167 |
+
'''
|
| 168 |
+
}
|
| 169 |
+
class WebDesignValidator:
|
| 170 |
+
def __init__(self):
|
| 171 |
+
self.accessibility_rules = {
|
| 172 |
+
'aria_labels': True,
|
| 173 |
+
'alt_texts': True,
|
| 174 |
+
'semantic_html': True,
|
| 175 |
+
'keyboard_navigation': True,
|
| 176 |
+
'color_contrast': True,
|
| 177 |
+
'focus_indicators': True
|
| 178 |
+
}
|
| 179 |
+
self.responsive_breakpoints = {
|
| 180 |
+
'mobile': '320px',
|
| 181 |
+
'tablet': '768px',
|
| 182 |
+
'desktop': '1024px',
|
| 183 |
+
'large': '1440px'
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
async def validate_design_principles(self, implementation: Dict[str, Any]) -> Dict[str, Any]:
|
| 187 |
+
results = {
|
| 188 |
+
'accessibility': await self._check_accessibility(implementation),
|
| 189 |
+
'responsive_design': await self._check_responsive_design(implementation),
|
| 190 |
+
'color_contrast': await self._check_color_contrast(implementation),
|
| 191 |
+
'typography': await self._check_typography(implementation),
|
| 192 |
+
'performance': await self._check_performance(implementation)
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
return {
|
| 196 |
+
'validation_results': results,
|
| 197 |
+
'overall_status': self._calculate_overall_status(results),
|
| 198 |
+
'recommendations': self._generate_recommendations(results)
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
async def _check_accessibility(self, implementation: Dict[str, Any]) -> Dict[str, Any]:
|
| 202 |
+
results = {
|
| 203 |
+
'passed': [],
|
| 204 |
+
'failed': [],
|
| 205 |
+
'warnings': []
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
for component in implementation.get('components', []):
|
| 209 |
+
for rule, enabled in self.accessibility_rules.items():
|
| 210 |
+
if enabled:
|
| 211 |
+
result = await self._validate_accessibility_rule(component, rule)
|
| 212 |
+
if result['status'] == 'passed':
|
| 213 |
+
results['passed'].append(f"{component['name']}: {rule}")
|
| 214 |
+
elif result['status'] == 'failed':
|
| 215 |
+
results['failed'].append({
|
| 216 |
+
'component': component['name'],
|
| 217 |
+
'rule': rule,
|
| 218 |
+
'message': result['message']
|
| 219 |
+
})
|
| 220 |
+
else:
|
| 221 |
+
results['warnings'].append({
|
| 222 |
+
'component': component['name'],
|
| 223 |
+
'rule': rule,
|
| 224 |
+
'message': result['message']
|
| 225 |
+
})
|
| 226 |
+
|
| 227 |
+
return results
|
| 228 |
+
|
| 229 |
+
async def _validate_accessibility_rule(self, component: Dict[str, Any], rule: str) -> Dict[str, Any]:
|
| 230 |
+
validators = {
|
| 231 |
+
'aria_labels': self._check_aria_labels,
|
| 232 |
+
'alt_texts': self._check_alt_texts,
|
| 233 |
+
'semantic_html': self._check_semantic_html,
|
| 234 |
+
'keyboard_navigation': self._check_keyboard_navigation,
|
| 235 |
+
'color_contrast': self._check_specific_color_contrast,
|
| 236 |
+
'focus_indicators': self._check_focus_indicators
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
validator = validators.get(rule)
|
| 240 |
+
if validator:
|
| 241 |
+
return await validator(component)
|
| 242 |
+
return {'status': 'warning', 'message': f'No validator found for {rule}'}
|
| 243 |
+
|
| 244 |
+
async def _check_responsive_design(self, implementation: Dict[str, Any]) -> Dict[str, Any]:
|
| 245 |
+
results = {
|
| 246 |
+
'breakpoints_tested': [],
|
| 247 |
+
'layout_issues': [],
|
| 248 |
+
'recommendations': []
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
for breakpoint, size in self.responsive_breakpoints.items():
|
| 252 |
+
test_result = await self._test_breakpoint(implementation, breakpoint, size)
|
| 253 |
+
results['breakpoints_tested'].append({
|
| 254 |
+
'breakpoint': breakpoint,
|
| 255 |
+
'size': size,
|
| 256 |
+
'result': test_result
|
| 257 |
+
})
|
| 258 |
+
|
| 259 |
+
if test_result.get('issues'):
|
| 260 |
+
results['layout_issues'].extend(test_result['issues'])
|
| 261 |
+
results['recommendations'].extend(test_result['recommendations'])
|
| 262 |
+
|
| 263 |
+
return results
|
| 264 |
+
|
| 265 |
+
async def _test_breakpoint(self, implementation: Dict[str, Any], breakpoint: str, size: str) -> Dict[str, Any]:
|
| 266 |
+
# Simulate testing at different viewport sizes
|
| 267 |
+
return {
|
| 268 |
+
'breakpoint': breakpoint,
|
| 269 |
+
'size': size,
|
| 270 |
+
'layout_integrity': True,
|
| 271 |
+
'issues': [],
|
| 272 |
+
'recommendations': []
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
async def _check_color_contrast(self, implementation: Dict[str, Any]) -> Dict[str, Any]:
|
| 276 |
+
results = {
|
| 277 |
+
'passed': [],
|
| 278 |
+
'failed': [],
|
| 279 |
+
'warnings': []
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
for component in implementation.get('components', []):
|
| 283 |
+
contrast_ratio = await self._calculate_contrast_ratio(
|
| 284 |
+
component.get('foreground_color'),
|
| 285 |
+
component.get('background_color')
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
if contrast_ratio >= 4.5: # WCAG AA standard
|
| 289 |
+
results['passed'].append({
|
| 290 |
+
'component': component['name'],
|
| 291 |
+
'ratio': contrast_ratio
|
| 292 |
+
})
|
| 293 |
+
else:
|
| 294 |
+
results['failed'].append({
|
| 295 |
+
'component': component['name'],
|
| 296 |
+
'ratio': contrast_ratio,
|
| 297 |
+
'recommendation': 'Increase contrast to at least 4.5:1'
|
| 298 |
+
})
|
| 299 |
+
|
| 300 |
+
return results
|
| 301 |
+
|
| 302 |
+
async def _check_typography(self, implementation: Dict[str, Any]) -> Dict[str, Any]:
|
| 303 |
+
return {
|
| 304 |
+
'font_hierarchy': await self._check_font_hierarchy(implementation),
|
| 305 |
+
'line_height': await self._check_line_height(implementation),
|
| 306 |
+
'font_sizes': await self._check_font_sizes(implementation),
|
| 307 |
+
'recommendations': []
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
async def _check_performance(self, implementation: Dict[str, Any]) -> Dict[str, Any]:
|
| 311 |
+
return {
|
| 312 |
+
'image_optimization': await self._check_image_optimization(implementation),
|
| 313 |
+
'css_optimization': await self._check_css_optimization(implementation),
|
| 314 |
+
'loading_time': await self._check_loading_time(implementation),
|
| 315 |
+
'recommendations': []
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
def _calculate_overall_status(self, results: Dict[str, Any]) -> str:
|
| 319 |
+
# Implementation of overall status calculation
|
| 320 |
+
failed_count = sum(len(category.get('failed', [])) for category in results.values())
|
| 321 |
+
warning_count = sum(len(category.get('warnings', [])) for category in results.values())
|
| 322 |
+
|
| 323 |
+
if failed_count == 0 and warning_count == 0:
|
| 324 |
+
return 'PASSED'
|
| 325 |
+
elif failed_count == 0:
|
| 326 |
+
return 'PASSED_WITH_WARNINGS'
|
| 327 |
+
else:
|
| 328 |
+
return 'FAILED'
|
| 329 |
+
|
| 330 |
+
def _generate_recommendations(self, results: Dict[str, Any]) -> List[str]:
|
| 331 |
+
recommendations = []
|
| 332 |
+
for category, result in results.items():
|
| 333 |
+
if 'failed' in result:
|
| 334 |
+
for failure in result['failed']:
|
| 335 |
+
recommendations.append(f"Fix {category}: {failure}")
|
| 336 |
+
if 'warnings' in result:
|
| 337 |
+
for warning in result['warnings']:
|
| 338 |
+
recommendations.append(f"Consider improving {category}: {warning}")
|
| 339 |
+
return recommendations
|
| 340 |
|
| 341 |
class AgentRole(Enum):
|
| 342 |
ARCHITECT = auto()
|
|
|
|
| 346 |
TESTER = auto()
|
| 347 |
REVIEWER = auto()
|
| 348 |
DEPLOYER = auto()
|
| 349 |
+
DESIGNER = auto()
|
| 350 |
|
| 351 |
@dataclass
|
| 352 |
class AgentDecision:
|
|
|
|
| 356 |
reasoning: str
|
| 357 |
timestamp: datetime = field(default_factory=datetime.now)
|
| 358 |
dependencies: List['AgentDecision'] = field(default_factory=list)
|
| 359 |
+
design_implications: Dict[str, Any] = field(default_factory=dict)
|
| 360 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
class Agent:
|
| 362 |
+
def __init__(self, role: AgentRole, design_system: DesignSystem, ui_library: UIComponentLibrary):
|
| 363 |
+
self.role = role
|
| 364 |
+
self.design_system = design_system
|
| 365 |
+
self.ui_library = ui_library
|
| 366 |
+
self.decisions: List[AgentDecision] = []
|
| 367 |
+
self.current_task: Optional[Dict[str, Any]] = None
|
| 368 |
+
self.autonomy_level: float = 5.0 # Scale of 0-10
|
| 369 |
+
self.validator = WebDesignValidator()
|
| 370 |
+
|
| 371 |
+
async def process_task(self, task: Dict[str, Any]) -> Dict[str, Any]:
|
| 372 |
+
self.current_task = task
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
|
| 374 |
+
try:
|
| 375 |
+
if self.role == AgentRole.DESIGNER:
|
| 376 |
+
return await self._process_design_task(task)
|
| 377 |
+
elif self.role == AgentRole.FRONTEND:
|
| 378 |
+
return await self._process_frontend_task(task)
|
| 379 |
+
elif self.role == AgentRole.BACKEND:
|
| 380 |
+
return await self._process_backend_task(task)
|
| 381 |
+
else:
|
| 382 |
+
return await self._process_generic_task(task)
|
| 383 |
+
except Exception as e:
|
| 384 |
+
logger.error(f"Error processing task in {self.role}: {str(e)}")
|
| 385 |
+
return {'status': 'error', 'message': str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
|
| 387 |
+
async def _process_design_task(self, task: Dict[str, Any]) -> Dict[str, Any]:
|
| 388 |
+
design_requirements = task.get('design_requirements', {})
|
| 389 |
+
|
| 390 |
+
# Create design specifications
|
| 391 |
+
design_specs = {
|
| 392 |
+
'layout': await self._create_layout_specs(design_requirements),
|
| 393 |
+
'components': await self._create_component_specs(design_requirements),
|
| 394 |
+
'styling': await self._create_styling_specs(design_requirements),
|
| 395 |
+
'interactions': await self._create_interaction_specs(design_requirements)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
}
|
| 397 |
|
| 398 |
+
# Validate design specifications
|
| 399 |
+
validation_results = await self.validator.validate_design_principles({
|
| 400 |
+
'specs': design_specs,
|
| 401 |
+
'requirements': design_requirements
|
| 402 |
+
})
|
| 403 |
+
|
| 404 |
+
if validation_results['overall_status'] == 'FAILED':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
return {
|
| 406 |
+
'status': 'revision_needed',
|
| 407 |
+
'specs': design_specs,
|
| 408 |
+
'validation': validation_results,
|
| 409 |
+
'recommendations': validation_results['recommendations']
|
| 410 |
}
|
| 411 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
return {
|
| 413 |
+
'status': 'success',
|
| 414 |
+
'specs': design_specs,
|
| 415 |
+
'validation': validation_results
|
| 416 |
}
|
| 417 |
|
| 418 |
+
async def _create_layout_specs(self, requirements: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
| 419 |
return {
|
| 420 |
+
'grid_system': {
|
| 421 |
+
'type': 'flexbox',
|
| 422 |
+
'columns': 12,
|
| 423 |
+
'breakpoints': self.design_system.theme['breakpoints']
|
| 424 |
+
},
|
| 425 |
+
'layout_type': requirements.get('layout_type', 'responsive'),
|
| 426 |
+
'containers': {
|
| 427 |
+
'max_width': '1200px',
|
| 428 |
+
'padding': self.design_system.get_spacing('md')
|
| 429 |
+
}
|
| 430 |
}
|
| 431 |
|
| 432 |
+
async def _create_component_specs(self, requirements: Dict[str, Any]) -> Dict[str, Any]:
|
| 433 |
+
components = {}
|
| 434 |
+
for component_type in requirements.get('components', []):
|
| 435 |
+
template = self.ui_library.get_component_template(component_type)
|
| 436 |
+
if template:
|
| 437 |
+
components[component_type] = {
|
| 438 |
+
'template': template['template'],
|
| 439 |
+
'styles': template['styles'],
|
| 440 |
+
'variants': self._generate_component_variants(component_type)
|
| 441 |
+
}
|
| 442 |
+
return components
|
| 443 |
|
| 444 |
+
async def _process_frontend_task(self, task: Dict[str, Any]) -> Dict[str, Any]:
|
| 445 |
+
design_specs = task.get('design_specs', {})
|
| 446 |
+
|
| 447 |
+
implementation = {
|
| 448 |
+
'components': await self._implement_components(design_specs),
|
| 449 |
+
'styles': await self._implement_styles(design_specs),
|
| 450 |
+
'layouts': await self._implement_layouts(design_specs)
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
# Validate frontend implementation
|
| 454 |
+
validation_results = await self.validator.validate_design_principles(implementation)
|
| 455 |
+
|
| 456 |
return {
|
| 457 |
+
'status': 'success',
|
| 458 |
+
'implementation': implementation,
|
| 459 |
+
'validation': validation_results
|
| 460 |
}
|
| 461 |
|
| 462 |
+
async def _implement_components(self, design_specs: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 463 |
+
implemented_components = []
|
| 464 |
+
for component_name, specs in design_specs.get('components', {}).items():
|
| 465 |
+
implemented_components.append({
|
| 466 |
+
'name': component_name,
|
| 467 |
+
'implementation': self.ui_library.get_component_template(component_name),
|
| 468 |
+
'styles': specs.get('styles', {}),
|
| 469 |
+
'variants': specs.get('variants', [])
|
| 470 |
+
})
|
| 471 |
+
return implemented_components
|
| 472 |
|
| 473 |
+
class AgentSystem:
|
| 474 |
+
def __init__(self, config: Config):
|
| 475 |
+
self.config = config
|
| 476 |
+
self.design_system = DesignSystem()
|
| 477 |
+
self.ui_library = UIComponentLibrary(self.design_system)
|
| 478 |
+
self.validator = WebDesignValidator()
|
| 479 |
+
self.agents = self._initialize_agents()
|
| 480 |
+
self.executor = ThreadPoolExecutor(max_workers=config.max_workers)
|
| 481 |
+
self.current_project: Optional[Dict[str, Any]] = None
|
| 482 |
+
|
| 483 |
+
def _initialize_agents(self) -> Dict[AgentRole, Agent]:
|
| 484 |
+
return {
|
| 485 |
+
role: Agent(role, self.design_system, self.ui_library)
|
| 486 |
+
for role in AgentRole
|
| 487 |
}
|
| 488 |
|
| 489 |
+
async def process_request(self, description: str, context: Dict[str, Any]) -> Dict[str, Any]:
|
| 490 |
try:
|
| 491 |
+
self.current_project = {
|
| 492 |
+
'description': description,
|
| 493 |
+
'context': context,
|
| 494 |
+
'status': 'initializing',
|
| 495 |
+
'timestamp': datetime.now()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
}
|
| 497 |
|
| 498 |
+
# Design Phase
|
| 499 |
+
design_result = await self._process_design_phase(context)
|
| 500 |
+
if design_result['status'] != 'success':
|
| 501 |
+
return design_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 502 |
|
| 503 |
+
# Implementation Phase
|
| 504 |
+
implementation_result = await self._process_implementation_phase(design_result['specs'])
|
| 505 |
+
if implementation_result['status'] != 'success':
|
| 506 |
+
return implementation_result
|
|
|
|
|
|
|
| 507 |
|
| 508 |
+
# Validation Phase
|
| 509 |
+
validation_result = await self._process_validation_phase(implementation_result['implementation'])
|
| 510 |
+
|
| 511 |
return {
|
| 512 |
+
'status': 'success',
|
| 513 |
+
'design': design_result,
|
| 514 |
+
'implementation': implementation_result,
|
| 515 |
+
'validation': validation_result
|
| 516 |
}
|
| 517 |
|
| 518 |
except Exception as e:
|
| 519 |
+
logger.error(f"Error processing request: {str(e)}")
|
| 520 |
+
return {'status': 'error', 'message': str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 521 |
|
| 522 |
+
async def _process_design_phase(self, context: Dict[str, Any]) -> Dict[str, Any]:
|
| 523 |
+
design_agent = self.agents[AgentRole.DESIGNER]
|
| 524 |
+
return await design_agent.process_task({
|
| 525 |
+
'type': 'design',
|
| 526 |
+
'requirements': context.get('design_requirements', {}),
|
| 527 |
+
'constraints': context.get('constraints', {})
|
| 528 |
+
})
|
| 529 |
+
|
| 530 |
+
async def _process_implementation_phase(self, design_specs: Dict[str, Any]) -> Dict[str, Any]:
|
| 531 |
+
frontend_result = await self.agents[AgentRole.FRONTEND].process_task({
|
| 532 |
+
'type': 'implementation',
|
| 533 |
+
'design_specs': design_specs
|
| 534 |
+
})
|
| 535 |
+
|
| 536 |
+
backend_result = await self.agents[AgentRole.BACKEND].process_task({
|
| 537 |
+
'type': 'implementation',
|
| 538 |
+
'design_specs': design_specs
|
| 539 |
+
})
|
| 540 |
|
| 541 |
+
return {
|
| 542 |
+
'status': 'success',
|
| 543 |
+
'frontend': frontend_result,
|
| 544 |
+
'backend': backend_result
|
|
|
|
|
|
|
| 545 |
}
|
| 546 |
|
| 547 |
+
async def _process_validation_phase(self, implementation: Dict[str, Any]) -> Dict[str, Any]:
|
| 548 |
+
return await self.validator.validate_design_principles(implementation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 549 |
|
| 550 |
+
async def set_autonomy_level(self, level: float) -> None:
|
| 551 |
+
if not 0 <= level <= 10:
|
| 552 |
+
raise ValueError("Autonomy level must be between 0 and 10")
|
| 553 |
+
for agent in self.agents.values():
|
| 554 |
+
agent.autonomy_level = level
|
|
|
|
|
|
|
| 555 |
|
| 556 |
+
# Main execution and utility functions
|
| 557 |
+
async def create_gradio_interface(agent_system: AgentSystem):
|
| 558 |
+
"""Create the Gradio interface for the web design system"""
|
| 559 |
+
|
| 560 |
+
def process_design_request(description: str, requirements: str) -> Dict[str, Any]:
|
| 561 |
try:
|
| 562 |
+
context = json.loads(requirements) if requirements else {}
|
| 563 |
+
result = asyncio.run(agent_system.process_request(description, context))
|
| 564 |
+
return json.dumps(result, indent=2)
|
|
|
|
|
|
|
| 565 |
except Exception as e:
|
| 566 |
+
return f"Error: {str(e)}"
|
| 567 |
+
|
| 568 |
+
interface = gr.Interface(
|
| 569 |
+
fn=process_design_request,
|
| 570 |
+
inputs=[
|
| 571 |
+
gr.Textbox(label="Project Description", lines=3),
|
| 572 |
+
gr.Textbox(label="Design Requirements (JSON)", lines=5)
|
| 573 |
+
],
|
| 574 |
+
outputs=gr.JSON(label="Results"),
|
| 575 |
+
title="Web Design Agent System",
|
| 576 |
+
description="Enter your project details and design requirements to generate web design specifications and implementation."
|
| 577 |
+
)
|
| 578 |
+
|
| 579 |
+
return interface
|
| 580 |
+
|
| 581 |
+
class TestSuite:
|
| 582 |
+
"""Test suite for the web design system"""
|
| 583 |
+
|
| 584 |
+
def __init__(self, agent_system: AgentSystem):
|
| 585 |
+
self.agent_system = agent_system
|
| 586 |
+
self.test_cases = self._initialize_test_cases()
|
| 587 |
+
|
| 588 |
+
def _initialize_test_cases(self) -> List[Dict[str, Any]]:
|
| 589 |
+
return [
|
| 590 |
+
{
|
| 591 |
+
'name': 'basic_website',
|
| 592 |
+
'description': 'Create a basic website with homepage and contact form',
|
| 593 |
+
'context': {
|
| 594 |
+
'design_requirements': {
|
| 595 |
+
'theme': 'light',
|
| 596 |
+
'components': ['header', 'footer', 'contact_form'],
|
| 597 |
+
'responsive': True
|
| 598 |
+
}
|
| 599 |
+
}
|
| 600 |
+
},
|
| 601 |
+
{
|
| 602 |
+
'name': 'e_commerce',
|
| 603 |
+
'description': 'Create an e-commerce product page',
|
| 604 |
+
'context': {
|
| 605 |
+
'design_requirements': {
|
| 606 |
+
'theme': 'modern',
|
| 607 |
+
'components': ['product_card', 'shopping_cart', 'checkout_form'],
|
| 608 |
+
'responsive': True
|
| 609 |
+
}
|
| 610 |
}
|
| 611 |
}
|
| 612 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 613 |
|
| 614 |
+
async def run_tests(self) -> Dict[str, Any]:
|
|
|
|
| 615 |
results = {
|
| 616 |
'passed': [],
|
| 617 |
'failed': [],
|
| 618 |
+
'total_tests': len(self.test_cases),
|
| 619 |
+
'timestamp': datetime.now()
|
| 620 |
}
|
| 621 |
|
| 622 |
+
for test_case in self.test_cases:
|
|
|
|
| 623 |
try:
|
| 624 |
+
result = await self.agent_system.process_request(
|
| 625 |
+
test_case['description'],
|
| 626 |
+
test_case['context']
|
| 627 |
+
)
|
| 628 |
+
|
| 629 |
+
if result['status'] == 'success':
|
| 630 |
+
results['passed'].append({
|
| 631 |
+
'test_name': test_case['name'],
|
| 632 |
+
'result': result
|
| 633 |
+
})
|
| 634 |
else:
|
| 635 |
+
results['failed'].append({
|
| 636 |
+
'test_name': test_case['name'],
|
| 637 |
+
'error': result
|
| 638 |
+
})
|
| 639 |
+
|
| 640 |
except Exception as e:
|
| 641 |
+
results['failed'].append({
|
| 642 |
+
'test_name': test_case['name'],
|
| 643 |
+
'error': str(e)
|
| 644 |
+
})
|
| 645 |
|
| 646 |
return results
|
| 647 |
|
| 648 |
+
class ProjectManager:
|
| 649 |
+
"""Manages web design projects and their lifecycle"""
|
| 650 |
+
|
| 651 |
+
def __init__(self, agent_system: AgentSystem):
|
| 652 |
+
self.agent_system = agent_system
|
| 653 |
+
self.projects: Dict[str, Dict[str, Any]] = {}
|
| 654 |
+
self.active_project_id: Optional[str] = None
|
| 655 |
+
|
| 656 |
+
async def create_project(self, name: str, description: str, requirements: Dict[str, Any]) -> str:
|
| 657 |
+
project_id = f"proj_{len(self.projects) + 1}"
|
| 658 |
+
self.projects[project_id] = {
|
| 659 |
+
'name': name,
|
| 660 |
+
'description': description,
|
| 661 |
+
'requirements': requirements,
|
| 662 |
+
'status': 'created',
|
| 663 |
+
'created_at': datetime.now(),
|
| 664 |
+
'history': []
|
| 665 |
}
|
| 666 |
+
return project_id
|
| 667 |
|
| 668 |
+
async def process_project(self, project_id: str) -> Dict[str, Any]:
|
| 669 |
+
if project_id not in self.projects:
|
| 670 |
+
raise ValueError(f"Project {project_id} not found")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 671 |
|
| 672 |
+
project = self.projects[project_id]
|
| 673 |
+
self.active_project_id = project_id
|
|
|
|
|
|
|
| 674 |
|
| 675 |
+
try:
|
| 676 |
+
result = await self.agent_system.process_request(
|
| 677 |
+
project['description'],
|
| 678 |
+
{'design_requirements': project['requirements']}
|
| 679 |
+
)
|
| 680 |
+
|
| 681 |
+
project['status'] = result['status']
|
| 682 |
+
project['history'].append({
|
| 683 |
+
'timestamp': datetime.now(),
|
| 684 |
+
'action': 'process',
|
| 685 |
+
'result': result
|
| 686 |
+
})
|
| 687 |
+
|
| 688 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 689 |
|
| 690 |
+
except Exception as e:
|
| 691 |
+
project['status'] = 'error'
|
| 692 |
+
project['history'].append({
|
| 693 |
+
'timestamp': datetime.now(),
|
| 694 |
+
'action': 'process',
|
| 695 |
+
'error': str(e)
|
| 696 |
+
})
|
| 697 |
+
raise
|
|
|
|
|
|
|
| 698 |
|
| 699 |
+
def main():
|
| 700 |
+
"""Main entry point for the application"""
|
| 701 |
+
|
| 702 |
+
# Initialize configuration
|
| 703 |
+
config = Config(
|
| 704 |
+
rag_system_path="/path/to/rag",
|
| 705 |
+
max_workers=10,
|
| 706 |
+
log_level="INFO",
|
| 707 |
+
model_settings={
|
| 708 |
+
'temperature': 0.7,
|
| 709 |
+
'max_tokens': 1000
|
| 710 |
+
},
|
| 711 |
+
api_keys={},
|
| 712 |
+
design_system_config={
|
| 713 |
+
'theme': 'light',
|
| 714 |
+
'responsive': True,
|
| 715 |
+
'accessibility_level': 'AAA'
|
| 716 |
}
|
| 717 |
+
)
|
|
|
|
|
|
|
| 718 |
|
| 719 |
+
# Initialize agent system
|
| 720 |
+
agent_system = AgentSystem(config)
|
| 721 |
+
|
| 722 |
+
# Initialize project manager
|
| 723 |
+
project_manager = ProjectManager(agent_system)
|
| 724 |
+
|
| 725 |
+
# Initialize test suite
|
| 726 |
+
test_suite = TestSuite(agent_system)
|
| 727 |
|
| 728 |
+
# Create and launch Gradio interface
|
| 729 |
+
interface = asyncio.run(create_gradio_interface(agent_system))
|
| 730 |
+
interface.launch(share=True)
|
| 731 |
|
|
|
|
| 732 |
if __name__ == "__main__":
|
| 733 |
+
# Run tests before starting the application
|
| 734 |
+
config = Config(rag_system_path="/path/to/rag")
|
| 735 |
+
agent_system = AgentSystem(config)
|
| 736 |
+
test_suite = TestSuite(agent_system)
|
| 737 |
+
|
| 738 |
+
# Run tests
|
| 739 |
+
test_results = asyncio.run(test_suite.run_tests())
|
| 740 |
+
print("Test Results:", json.dumps(test_results, indent=2))
|
| 741 |
+
|
| 742 |
+
# Start the application
|
| 743 |
+
main()
|
| 744 |
+
|
| 745 |
+
# Pytest functions for testing
|
| 746 |
+
def test_agent_system():
|
| 747 |
+
config = Config(rag_system_path="/path/to/rag")
|
| 748 |
+
agent_system = AgentSystem(config)
|
| 749 |
+
|
| 750 |
+
# Test design system initialization
|
| 751 |
+
assert agent_system.design_system is not None
|
| 752 |
+
assert agent_system.ui_library is not None
|
| 753 |
+
|
| 754 |
+
# Test agent initialization
|
| 755 |
+
assert len(agent_system.agents) == len(AgentRole)
|
| 756 |
+
|
| 757 |
+
# Test validator initialization
|
| 758 |
+
assert agent_system.validator is not None
|
| 759 |
+
|
| 760 |
+
def test_design_validation():
|
| 761 |
+
config = Config(rag_system_path="/path/to/rag")
|
| 762 |
+
agent_system = AgentSystem(config)
|
| 763 |
+
|
| 764 |
+
implementation = {
|
| 765 |
+
'components': [
|
| 766 |
+
{
|
| 767 |
+
'name': 'button',
|
| 768 |
+
'foreground_color': '#FFFFFF',
|
| 769 |
+
'background_color': '#007BFF'
|
| 770 |
+
}
|
| 771 |
+
]
|
| 772 |
+
}
|
| 773 |
+
|
| 774 |
+
validation_result = asyncio.run(agent_system.validator.validate_design_principles(implementation))
|
| 775 |
+
assert validation_result is not None
|
| 776 |
+
assert 'validation_results' in validation_result
|
| 777 |
+
assert 'overall_status' in validation_result
|
| 778 |
+
|
| 779 |
+
def test_ui_component_library():
|
| 780 |
+
design_system = DesignSystem()
|
| 781 |
+
ui_library = UIComponentLibrary(design_system)
|
| 782 |
+
|
| 783 |
+
# Test component template retrieval
|
| 784 |
+
button_template = ui_library.get_component_template('button')
|
| 785 |
+
assert button_template is not None
|
| 786 |
+
assert 'template' in button_template
|
| 787 |
+
assert 'styles' in button_template
|