acecalisto3 commited on
Commit
7c69181
1 Parent(s): 4fb77b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +589 -287
app.py CHANGED
@@ -1,314 +1,631 @@
1
- import threading
2
- import time
3
  import gradio as gr
4
- import logging
5
  import json
6
- import re
7
- import torch
8
- import tempfile
9
- import os
10
- from pathlib import Path
11
- from typing import Dict, List, Tuple, Optional, Any, Union
12
  from dataclasses import dataclass, field
13
- from enum import Enum
14
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
15
- from sentence_transformers import SentenceTransformer
16
- import faiss
17
- import numpy as np
18
- from PIL import Image
19
- import black
20
-
21
- # Configure logging
22
- logging.basicConfig(
23
- level=logging.INFO,
24
- format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
25
- handlers=[
26
- logging.StreamHandler(),
27
- logging.FileHandler('gradio_builder.log')
28
- ]
29
- )
30
  logger = logging.getLogger(__name__)
31
 
32
- # Configuration
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  @dataclass
34
- class Config:
35
- port: int = 7860
36
- debug: bool = False
37
- share: bool = False
38
- model_name: str = "gpt2"
39
- embedding_model: str = "all-mpnet-base-v2"
40
- theme: str = "default"
41
- max_code_length: int = 5000
42
-
43
- @classmethod
44
- def from_file(cls, path: str) -> "Config":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  try:
46
- with open(path) as f:
47
- return cls(**json.load(f))
48
- except Exception as e:
49
- logger.warning(f"Failed to load config from {path}: {e}. Using defaults.")
50
- return cls()
 
 
 
 
 
 
 
 
 
 
51
 
52
- # Constants
53
- CONFIG_PATH = Path("config.json")
54
- MODEL_CACHE_DIR = Path("model_cache")
55
- TEMPLATE_DIR = Path("templates")
56
- TEMP_DIR = Path("temp")
57
- DATABASE_PATH = Path("code_database.json")
58
 
59
- # Ensure directories exist
60
- for directory in [MODEL_CACHE_DIR, TEMPLATE_DIR, TEMP_DIR]:
61
- directory.mkdir(exist_ok=True, parents=True)
62
 
63
- @dataclass
64
- class Template:
65
- code: str
66
- description: str
67
- components: List[str] = field(default_factory=list)
68
- created_at: str = field(default_factory=lambda: time.strftime("%Y-%m-%d %H:%M:%S"))
69
- tags: List[str] = field(default_factory=list)
70
-
71
- class TemplateManager:
72
- def __init__(self, template_dir: Path):
73
- self.template_dir = template_dir
74
- self.templates: Dict[str, Template] = {}
75
-
76
- def load_templates(self) -> None:
77
- for file_path in self.template_dir.glob("*.json"):
78
- try:
79
- with open(file_path, 'r') as f:
80
- template_data = json.load(f)
81
- template = Template(**template_data)
82
- self.templates[template_data['description']] = template
83
- logger.info(f"Loaded template: {file_path.stem}")
84
- except Exception as e:
85
- logger.error(f"Error loading template from {file_path}: {e}")
86
 
87
- def save_template(self, name: str, template: Template) -> bool:
88
- file_path = self.template_dir / f"{name}.json"
89
- try:
90
- with open(file_path, 'w') as f:
91
- json.dump(dataclasses.asdict(template), f, indent=2)
92
- self.templates[name] = template
93
- return True
94
  except Exception as e:
95
- logger.error(f"Error saving template to {file_path}: {e}")
96
- return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
- def get_template(self, name: str) -> Optional[str]:
99
- template = self.templates.get(name)
100
- return template.code if template else ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
- def delete_template(self, name: str) -> bool:
103
- file_path = self.template_dir / f"{name}.json"
 
 
 
 
 
 
 
104
  try:
105
- file_path.unlink()
106
- self.templates.pop(name, None)
107
- return True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  except Exception as e:
109
- logger.error(f"Error deleting template {name}: {e}")
110
- return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
- class RAGSystem:
113
- def __init__(self, config: Config):
114
- self.config = config
115
- self.device = "cuda" if torch.cuda.is_available() else "cpu"
116
- self.embedding_model = None
117
- self.code_embeddings = None
118
- self.index = None
119
- self.database = {'codes': [], 'embeddings': []}
120
- self.pipe = None
121
 
122
- try:
123
- self.tokenizer = AutoTokenizer.from_pretrained(
124
- config.model_name,
125
- cache_dir=MODEL_CACHE_DIR
126
- )
127
- self.model = AutoModelForCausalLM.from_pretrained(
128
- config.model_name,
129
- cache_dir=MODEL_CACHE_DIR
130
- ).to(self.device)
131
- self.pipe = pipeline(
132
- "text-generation",
133
- model=self.model,
134
- tokenizer=self.tokenizer,
135
- device=self.device
136
- )
137
- self.embedding_model = SentenceTransformer(config.embedding_model)
138
- self.load_database()
139
- logger.info("RAG system initialized successfully.")
140
- except Exception as e:
141
- logger.error(f"Error initializing RAG system: {e}")
142
 
143
- def load_database(self) -> None:
144
- if DATABASE_PATH.exists():
145
  try:
146
- with open(DATABASE_PATH, 'r', encoding='utf-8') as f:
147
- self.database = json.load(f)
148
- self.code_embeddings = np.array(self.database['embeddings'])
149
- logger.info(f"Loaded {len(self.database['codes'])} code snippets from database.")
150
- self._build_index()
 
151
  except Exception as e:
152
- logger.error(f"Error loading database: {e}")
153
- self._initialize_empty_database()
154
- else:
155
- logger.info("Creating new database.")
156
- self._initialize_empty_database()
157
-
158
- def _initialize_empty_database(self) -> None:
159
- self.database = {'codes': [], 'embeddings': []}
160
- self.code_embeddings = np.array([])
161
- self._build_index()
162
-
163
- def _build_index(self) -> None:
164
- if len(self.code_embeddings) > 0 and self.embedding_model:
165
- dim = self.code_embeddings.shape[1]
166
- self.index = faiss.IndexFlatL2(dim)
167
- self.index.add(self.code_embeddings)
168
- logger.info(f"Built FAISS index with {len(self.code_embeddings)} vectors")
169
 
170
- class GradioInterface:
171
- def __init__(self, config: Config):
172
- self.config = config
173
- self.template_manager = TemplateManager(TEMPLATE_DIR)
174
- self.template_manager.load_templates()
175
- self.rag_system = RAGSystem(config)
176
 
177
- def format_code(self, code: str) -> str:
178
- try:
179
- return black.format_str(code, mode=black.FileMode())
180
- except Exception as e:
181
- logger.warning(f"Code formatting failed: {e}")
182
- return code
 
183
 
184
- def _extract_components(self, code: str) -> List[str]:
185
- components = []
186
  try:
187
- function_matches = re.findall(r'def (\w+)\(', code)
188
- class_matches = re.findall(r'class (\w+):', code)
189
- components.extend(function_matches)
190
- components.extend(class_matches)
 
191
  except Exception as e:
192
- logger.error(f"Error extracting components: {e}")
193
- return list(set(components))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
 
195
  def launch(self) -> None:
196
  with gr.Blocks(theme=gr.themes.Base()) as interface:
197
- # Custom CSS
198
- gr.Markdown(
199
- """
200
- <style>
201
- .header {
202
- text-align: center;
203
- background-color: #f0f0f0;
204
- padding: 20px;
205
- border-radius: 10px;
206
- margin-bottom: 20px;
207
- }
208
- .container {
209
- max-width: 1200px;
210
- margin: 0 auto;
211
- }
212
- </style>
213
- <div class="header">
214
- <h1>Code Generation Interface</h1>
215
- <p>Generate and manage code templates easily</p>
216
- </div>
217
- """
218
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
 
220
- with gr.Row():
221
- with gr.Column(scale=2):
222
- description_input = gr.Textbox(
223
- label="Description",
224
- placeholder="Enter a description for the code you want to generate",
225
- lines=3
226
- )
227
- template_choice = gr.Dropdown(
228
- label="Select Template",
229
- choices=list(self.template_manager.templates.keys()),
230
- value=None
231
- )
232
-
233
- with gr.Row():
234
- generate_button = gr.Button("Generate Code", variant="primary")
235
- save_button = gr.Button("Save as Template", variant="secondary")
236
- clear_button = gr.Button("Clear", variant="stop")
237
-
238
- with gr.Row():
239
- code_output = gr.Code(
240
- label="Generated Code",
241
- language="python",
242
- interactive=True
243
- )
244
-
245
- status_output = gr.Textbox(
246
- label="Status",
247
- interactive=False
248
- )
249
 
250
- def generate_code_wrapper(description: str, template_choice: str) -> Tuple[str, str]:
251
- if not description.strip():
252
- return "", "Please provide a description"
253
-
254
  try:
255
- template_code = self.template_manager.get_template(template_choice) if template_choice else ""
256
- generated_code = self.rag_system.generate_code(description, template_code)
257
- formatted_code = self.format_code(generated_code)
258
-
259
- if not formatted_code:
260
- return "", "Failed to generate code. Please try again."
261
-
262
- return formatted_code, "Code generated successfully."
263
-
264
  except Exception as e:
265
- logger.error(f"Error in code generation: {str(e)}")
266
- return "", f"Error: {str(e)}"
267
 
268
- def save_template_wrapper(code: str, name: str, description: str) -> Tuple[str, str]:
 
 
 
269
  try:
270
- if not name or not code:
271
- return code, "Template name and code are required."
272
-
273
- components = self._extract_components(code)
274
- template = Template(
275
- code=code,
276
- description=name,
277
- components=components,
278
- tags=[t.strip() for t in description.split(',') if t.strip()]
279
- )
280
-
281
- if self.template_manager.save_template(name, template):
282
- self.rag_system.add_to_database(code)
283
- template_choice.choices = list(self.template_manager.templates.keys())
284
- return code, f"Template '{name}' saved successfully."
285
- else:
286
- return code, "Failed to save template."
 
287
  except Exception as e:
288
- return code, f"Error saving template: {e}"
289
-
290
- def clear_outputs() -> Tuple[str, str, str]:
291
- return "", "", ""
292
-
293
- # Event handlers
294
- generate_button.click(
295
- fn=generate_code_wrapper,
296
- inputs=[description_input, template_choice],
297
- outputs=[code_output, status_output],
298
- api_name="generate_code",
299
- show_progress=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
  )
301
 
302
- save_button.click(
303
- fn=save_template_wrapper,
304
- inputs=[code_output, template_choice, description_input],
305
- outputs=[code_output, status_output]
306
  )
307
 
308
- clear_button.click(
309
- fn=clear_outputs,
310
- inputs=[],
311
- outputs=[description_input, code_output, status_output]
312
  )
313
 
314
  # Launch the interface
@@ -316,19 +633,4 @@ class GradioInterface:
316
  server_port=self.config.port,
317
  share=self.config.share,
318
  debug=self.config.debug
319
- )
320
-
321
- def main():
322
- logger.info("=== Application Startup ===")
323
- try:
324
- config = Config.from_file(CONFIG_PATH)
325
- interface = GradioInterface(config)
326
- interface.launch()
327
- except Exception as e:
328
- logger.error(f"Application error: {e}")
329
- raise
330
- finally:
331
- logger.info("=== Application Shutdown ===")
332
-
333
- if __name__ == "__main__":
334
- main()
 
 
 
1
  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
9
+ import pytest
10
+ from concurrent.futures import ThreadPoolExecutor
11
+ import asyncio
12
+
13
+ # Initialize logger
14
+ logging.basicConfig(level=logging.INFO)
 
 
 
 
 
 
 
 
 
15
  logger = logging.getLogger(__name__)
16
 
17
+ class AgentRole(Enum):
18
+ ARCHITECT = auto()
19
+ FRONTEND = auto()
20
+ BACKEND = auto()
21
+ DATABASE = auto()
22
+ TESTER = auto()
23
+ REVIEWER = auto()
24
+ DEPLOYER = auto()
25
+
26
+ @dataclass
27
+ class AgentDecision:
28
+ agent: 'Agent'
29
+ decision: str
30
+ confidence: float
31
+ reasoning: str
32
+ timestamp: datetime = field(default_factory=datetime.now)
33
+ dependencies: List['AgentDecision'] = field(default_factory=list)
34
+
35
+ class AgentProtocol(Protocol):
36
+ async def decide(self, context: Dict[str, Any]) -> AgentDecision: ...
37
+ async def validate(self, decision: AgentDecision) -> bool: ...
38
+ async def implement(self, decision: AgentDecision) -> Any: ...
39
+ async def test(self, implementation: Any) -> bool: ...
40
+
41
  @dataclass
42
+ class Agent:
43
+ role: AgentRole
44
+ name: str
45
+ autonomy_level: float # 0-10
46
+ expertise: List[str]
47
+ confidence_threshold: float = 0.7
48
+
49
+ async def reason(self, context: Dict[str, Any]) -> str:
50
+ """Generate reasoning based on context and expertise"""
51
+ prompt = f"""
52
+ As {self.name}, a {self.role.name} expert with expertise in {', '.join(self.expertise)},
53
+ analyze the following context and provide reasoning:
54
+
55
+ Context:
56
+ {json.dumps(context, indent=2)}
57
+
58
+ Consider:
59
+ 1. Required components and their interactions
60
+ 2. Potential challenges and solutions
61
+ 3. Best practices and patterns
62
+ 4. Security and performance implications
63
+
64
+ Reasoning:
65
+ """
66
+ return await self.rag_system.generate_reasoning(prompt)
67
+
68
+ class AgentSystem:
69
+ def __init__(self, config: Config):
70
+ self.config = config
71
+ self.autonomy_level = 0.0 # 0-10
72
+ self.agents: Dict[AgentRole, Agent] = self._initialize_agents()
73
+ self.decision_history: List[AgentDecision] = []
74
+ self.executor = ThreadPoolExecutor(max_workers=10)
75
+ self.rag_system = RAGSystem(config)
76
+
77
+ def _initialize_agents(self) -> Dict[AgentRole, Agent]:
78
+ return {
79
+ AgentRole.ARCHITECT: Agent(
80
+ role=AgentRole.ARCHITECT,
81
+ name="System Architect",
82
+ autonomy_level=self.autonomy_level,
83
+ expertise=["system design", "architecture patterns", "integration"]
84
+ ),
85
+ AgentRole.FRONTEND: Agent(
86
+ role=AgentRole.FRONTEND,
87
+ name="Frontend Developer",
88
+ autonomy_level=self.autonomy_level,
89
+ expertise=["UI/UX", "React", "Vue", "Angular"]
90
+ ),
91
+ AgentRole.BACKEND: Agent(
92
+ role=AgentRole.BACKEND,
93
+ name="Backend Developer",
94
+ autonomy_level=self.autonomy_level,
95
+ expertise=["API design", "database", "security"]
96
+ ),
97
+ AgentRole.TESTER: Agent(
98
+ role=AgentRole.TESTER,
99
+ name="Quality Assurance",
100
+ autonomy_level=self.autonomy_level,
101
+ expertise=["testing", "automation", "quality assurance"]
102
+ ),
103
+ AgentRole.REVIEWER: Agent(
104
+ role=AgentRole.REVIEWER,
105
+ name="Code Reviewer",
106
+ autonomy_level=self.autonomy_level,
107
+ expertise=["code quality", "best practices", "security"]
108
+ ),
109
+ }
110
+
111
+ async def set_autonomy_level(self, level: float) -> None:
112
+ """Update autonomy level for all agents"""
113
+ self.autonomy_level = max(0.0, min(10.0, level))
114
+ for agent in self.agents.values():
115
+ agent.autonomy_level = self.autonomy_level
116
+
117
+ async def process_request(self, description: str, context: Dict[str, Any] = None) -> Dict[str, Any]:
118
+ """Process a user request with current autonomy level"""
119
  try:
120
+ context = context or {}
121
+ context['description'] = description
122
+ context['autonomy_level'] = self.autonomy_level
123
+
124
+ # Start with architect's decision
125
+ arch_decision = await self.agents[AgentRole.ARCHITECT].decide(context)
126
+ self.decision_history.append(arch_decision)
127
+
128
+ if self.autonomy_level < 3:
129
+ # Low autonomy: Wait for user confirmation
130
+ return {
131
+ 'status': 'pending_confirmation',
132
+ 'decision': arch_decision,
133
+ 'next_steps': self._get_next_steps(arch_decision)
134
+ }
135
 
136
+ # Medium to high autonomy: Proceed with implementation
137
+ implementation_plan = await self._create_implementation_plan(arch_decision)
 
 
 
 
138
 
139
+ if self.autonomy_level >= 7:
140
+ # High autonomy: Automatic implementation and testing
141
+ return await self._automated_implementation(implementation_plan)
142
 
143
+ # Medium autonomy: Return plan for user review
144
+ return {
145
+ 'status': 'pending_review',
146
+ 'plan': implementation_plan,
147
+ 'decisions': self.decision_history
148
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
 
 
 
 
 
 
 
 
150
  except Exception as e:
151
+ logger.error(f"Error in request processing: {e}")
152
+ return {'status': 'error', 'message': str(e)}
153
+
154
+ async def _create_implementation_plan(self, arch_decision: AgentDecision) -> Dict[str, Any]:
155
+ """Create detailed implementation plan based on architect's decision"""
156
+ tasks = []
157
+
158
+ # Frontend tasks
159
+ if 'frontend' in arch_decision.decision.lower():
160
+ tasks.append(self._create_frontend_tasks(arch_decision))
161
+
162
+ # Backend tasks
163
+ if 'backend' in arch_decision.decision.lower():
164
+ tasks.append(self._create_backend_tasks(arch_decision))
165
+
166
+ # Testing tasks
167
+ tasks.append(self._create_testing_tasks(arch_decision))
168
+
169
+ return {
170
+ 'tasks': await asyncio.gather(*tasks),
171
+ 'dependencies': arch_decision.dependencies,
172
+ 'estimated_time': self._estimate_implementation_time(tasks)
173
+ }
174
+
175
+ async def _automated_implementation(self, plan: Dict[str, Any]) -> Dict[str, Any]:
176
+ """Execute implementation plan automatically"""
177
+ results = {
178
+ 'frontend': None,
179
+ 'backend': None,
180
+ 'tests': None,
181
+ 'review': None
182
+ }
183
 
184
+ try:
185
+ # Parallel implementation of frontend and backend
186
+ impl_tasks = []
187
+ if 'frontend' in plan['tasks']:
188
+ impl_tasks.append(self._implement_frontend(plan['tasks']['frontend']))
189
+ if 'backend' in plan['tasks']:
190
+ impl_tasks.append(self._implement_backend(plan['tasks']['backend']))
191
+
192
+ implementations = await asyncio.gather(*impl_tasks)
193
+
194
+ # Testing
195
+ test_results = await self.agents[AgentRole.TESTER].test(implementations)
196
+
197
+ # Code review
198
+ review_results = await self.agents[AgentRole.REVIEWER].validate({
199
+ 'implementations': implementations,
200
+ 'test_results': test_results
201
+ })
202
+
203
+ return {
204
+ 'status': 'completed',
205
+ 'implementations': implementations,
206
+ 'test_results': test_results,
207
+ 'review': review_results,
208
+ 'decisions': self.decision_history
209
+ }
210
 
211
+ except Exception as e:
212
+ return {
213
+ 'status': 'error',
214
+ 'message': str(e),
215
+ 'partial_results': results
216
+ }
217
+
218
+ async def _handle_implementation_failure(self, error: Exception, context: Dict[str, Any]) -> Dict[str, Any]:
219
+ """Handle implementation failures with adaptive response"""
220
  try:
221
+ # Analyze error
222
+ error_analysis = await self.agents[AgentRole.REVIEWER].reason({
223
+ 'error': str(error),
224
+ 'context': context
225
+ })
226
+
227
+ # Determine correction strategy
228
+ if self.autonomy_level >= 8:
229
+ # High autonomy: Attempt automatic correction
230
+ correction = await self._attempt_automatic_correction(error_analysis)
231
+ if correction['success']:
232
+ return await self.process_request(context['description'], correction['context'])
233
+
234
+ return {
235
+ 'status': 'failure',
236
+ 'error': str(error),
237
+ 'analysis': error_analysis,
238
+ 'suggested_corrections': self._suggest_corrections(error_analysis)
239
+ }
240
+
241
  except Exception as e:
242
+ logger.error(f"Error handling implementation failure: {e}")
243
+ return {'status': 'critical_error', 'message': str(e)}
244
+
245
+ class AgentTester:
246
+ def __init__(self):
247
+ self.test_suites = {
248
+ 'frontend': self._test_frontend,
249
+ 'backend': self._test_backend,
250
+ 'integration': self._test_integration
251
+ }
252
+
253
+ async def _test_frontend(self, implementation: Dict[str, Any]) -> Dict[str, Any]:
254
+ """Run frontend tests"""
255
+ results = {
256
+ 'passed': [],
257
+ 'failed': [],
258
+ 'warnings': []
259
+ }
260
+
261
+ # Component rendering tests
262
+ for component in implementation.get('components', []):
263
+ try:
264
+ # Test component rendering
265
+ result = await self._test_component_render(component)
266
+ if result['success']:
267
+ results['passed'].append(f"Component {component['name']} renders correctly")
268
+ else:
269
+ results['failed'].append(f"Component {component['name']}: {result['error']}")
270
+ except Exception as e:
271
+ results['failed'].append(f"Error testing {component['name']}: {str(e)}")
272
 
273
+ return results
 
 
 
 
 
 
 
 
274
 
275
+ async def _test_backend(self, implementation: Dict[str, Any]) -> Dict[str, Any]:
276
+ """Run backend tests"""
277
+ results = {
278
+ 'passed': [],
279
+ 'failed': [],
280
+ 'warnings': []
281
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
282
 
283
+ # API endpoint tests
284
+ for endpoint in implementation.get('endpoints', []):
285
  try:
286
+ # Test endpoint functionality
287
+ result = await self._test_endpoint(endpoint)
288
+ if result['success']:
289
+ results['passed'].append(f"Endpoint {endpoint['path']} works correctly")
290
+ else:
291
+ results['failed'].append(f"Endpoint {endpoint['path']}: {result['error']}")
292
  except Exception as e:
293
+ results['failed'].append(f"Error testing {endpoint['path']}: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
 
295
+ return results
 
 
 
 
 
296
 
297
+ async def _test_integration(self, implementation: Dict[str, Any]) -> Dict[str, Any]:
298
+ """Run integration tests"""
299
+ results = {
300
+ 'passed': [],
301
+ 'failed': [],
302
+ 'warnings': []
303
+ }
304
 
305
+ # Test frontend-backend integration
 
306
  try:
307
+ result = await self._test_frontend_backend_integration(implementation)
308
+ if result['success']:
309
+ results['passed'].append("Frontend-Backend integration successful")
310
+ else:
311
+ results['failed'].append(f"Integration error: {result['error']}")
312
  except Exception as e:
313
+ results['failed'].append(f"Integration test error: {str(e)}")
314
+
315
+ return results
316
+
317
+ class AgentValidator:
318
+ def __init__(self):
319
+ self.validators = {
320
+ 'code_quality': self._validate_code_quality,
321
+ 'security': self._validate_security,
322
+ 'performance': self._validate_performance
323
+ }
324
+
325
+ async def validate_implementation(self, implementation: Dict[str, Any]) -> Dict[str, Any]:
326
+ """Validate implementation against best practices"""
327
+ results = {
328
+ 'passed': [],
329
+ 'failed': [],
330
+ 'warnings': []
331
+ }
332
+
333
+ for validator_name, validator in self.validators.items():
334
+ try:
335
+ validation_result = await validator(implementation)
336
+ results['passed'].extend(validation_result.get('passed', []))
337
+ results['failed'].extend(validation_result.get('failed', []))
338
+ results['warnings'].extend(validation_result.get('warnings', []))
339
+ except Exception as e:
340
+ results['warnings'].append(f"Validator {validator_name} error: {str(e)}")
341
+
342
+ return results
343
+
344
+ class GradioInterface:
345
+ def __init__(self, config: Config):
346
+ self.config = config
347
+ self.agent_system = AgentSystem(config)
348
+ self.explorer = CodeExplorer()
349
+ self.backend_generator = BackendGenerator(config)
350
+ self.file_handler = FileHandler()
351
+ self.preview_size = {"width": "100%", "height": "600px"}
352
+ self.is_preview_loading = False
353
 
354
  def launch(self) -> None:
355
  with gr.Blocks(theme=gr.themes.Base()) as interface:
356
+ # Header
357
+ gr.Markdown("# AI-Powered Development Environment")
358
+
359
+ with gr.Tabs() as tabs:
360
+ # Code Generation Tab
361
+ with gr.Tab("Code Generation"):
362
+ with gr.Row():
363
+ with gr.Column(scale=1):
364
+ code_input = gr.Code(
365
+ label="Input Code",
366
+ language="python",
367
+ lines=20
368
+ )
369
+ generate_button = gr.Button("Generate")
370
+
371
+ with gr.Column(scale=1):
372
+ code_output = gr.Code(
373
+ label="Generated Code",
374
+ language="python",
375
+ lines=20
376
+ )
377
+ status_message = gr.Markdown("")
378
+
379
+ # Agent Control Tab
380
+ with gr.Tab("Agent Control"):
381
+ with gr.Row():
382
+ autonomy_slider = gr.Slider(
383
+ minimum=0,
384
+ maximum=10,
385
+ value=0,
386
+ step=0.1,
387
+ label="Agent Autonomy Level",
388
+ info="0: Manual, 10: Fully Autonomous"
389
+ )
390
+
391
+ with gr.Row():
392
+ with gr.Column(scale=1):
393
+ project_description = gr.Textbox(
394
+ label="Project Description",
395
+ placeholder="Describe what you want to build...",
396
+ lines=5
397
+ )
398
+
399
+ with gr.Accordion("Advanced Options", open=False):
400
+ framework_choice = gr.Dropdown(
401
+ choices=["React", "Vue", "Angular", "FastAPI", "Flask", "Django"],
402
+ multiselect=True,
403
+ label="Preferred Frameworks"
404
+ )
405
+ architecture_style = gr.Radio(
406
+ choices=["Monolithic", "Microservices", "Serverless"],
407
+ label="Architecture Style",
408
+ value="Monolithic"
409
+ )
410
+ testing_preference = gr.Checkbox(
411
+ label="Include Tests",
412
+ value=True
413
+ )
414
+
415
+ process_button = gr.Button("Process Request")
416
+
417
+ with gr.Column(scale=2):
418
+ with gr.Tabs() as agent_tabs:
419
+ with gr.Tab("Decision Log"):
420
+ decision_log = gr.JSON(
421
+ label="Agent Decisions",
422
+ show_label=True
423
+ )
424
+
425
+ with gr.Tab("Implementation"):
426
+ with gr.Tabs() as impl_tabs:
427
+ with gr.Tab("Frontend"):
428
+ frontend_code = gr.Code(
429
+ label="Frontend Implementation",
430
+ language="javascript",
431
+ lines=20
432
+ )
433
+ with gr.Tab("Backend"):
434
+ backend_code = gr.Code(
435
+ label="Backend Implementation",
436
+ language="python",
437
+ lines=20
438
+ )
439
+ with gr.Tab("Database"):
440
+ database_code = gr.Code(
441
+ label="Database Schema",
442
+ language="sql",
443
+ lines=20
444
+ )
445
+
446
+ with gr.Tab("Test Results"):
447
+ test_results = gr.JSON(
448
+ label="Test Results"
449
+ )
450
+ rerun_tests_button = gr.Button("Rerun Tests")
451
+
452
+ with gr.Tab("Agent Chat"):
453
+ agent_chat = gr.Chatbot(
454
+ label="Agent Discussion",
455
+ height=400
456
+ )
457
+ chat_input = gr.Textbox(
458
+ label="Ask Agents",
459
+ placeholder="Type your question here..."
460
+ )
461
+ chat_button = gr.Button("Send")
462
+
463
+ with gr.Row():
464
+ status_output = gr.Markdown("System ready.")
465
+ with gr.Column():
466
+ progress = gr.Progress(
467
+ label="Implementation Progress",
468
+ show_progress=True
469
+ )
470
+
471
+ # Explorer Tab
472
+ with gr.Tab("Code Explorer"):
473
+ with gr.Row():
474
+ with gr.Column(scale=1):
475
+ component_list = gr.Dropdown(
476
+ label="Components",
477
+ choices=list(self.explorer.components.keys()),
478
+ interactive=True
479
+ )
480
+ refresh_button = gr.Button("Refresh")
481
+
482
+ with gr.Accordion("Add Component", open=False):
483
+ component_name = gr.Textbox(label="Name")
484
+ component_type = gr.Dropdown(
485
+ label="Type",
486
+ choices=['frontend', 'backend', 'database', 'api']
487
+ )
488
+ component_code = gr.Code(
489
+ label="Code",
490
+ language="python"
491
+ )
492
+ add_button = gr.Button("Add")
493
+
494
+ with gr.Column(scale=2):
495
+ component_details = gr.JSON(
496
+ label="Component Details"
497
+ )
498
+ dependency_graph = gr.Plot(
499
+ label="Dependencies"
500
+ )
501
+
502
+ # Event Handlers
503
+ async def update_autonomy(level):
504
+ await self.agent_system.set_autonomy_level(level)
505
+ return f"Autonomy level set to {level}"
506
+
507
+ async def process_request(description, level):
508
+ try:
509
+ # Update autonomy level
510
+ await self.agent_system.set_autonomy_level(level)
511
+
512
+ # Process request
513
+ result = await self.agent_system.process_request(description)
514
+
515
+ # Update UI based on result status
516
+ if result['status'] == 'pending_confirmation':
517
+ return {
518
+ decision_log: result['decision'],
519
+ frontend_code: "",
520
+ backend_code: "",
521
+ database_code: "",
522
+ test_results: {},
523
+ status_output: "Waiting for user confirmation...",
524
+ progress: 0.3
525
+ }
526
+ elif result['status'] == 'completed':
527
+ return {
528
+ decision_log: result['decisions'],
529
+ frontend_code: result['implementations'].get('frontend', ''),
530
+ backend_code: result['implementations'].get('backend', ''),
531
+ database_code: result['implementations'].get('database', ''),
532
+ test_results: result['test_results'],
533
+ status_output: "Implementation completed successfully!",
534
+ progress: 1.0
535
+ }
536
+ else:
537
+ return {
538
+ status_output: f"Error: {result['message']}",
539
+ progress: 0
540
+ }
541
 
542
+ except Exception as e:
543
+ return {
544
+ status_output: f"Error: {str(e)}",
545
+ progress: 0
546
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547
 
548
+ async def handle_chat(message, history):
 
 
 
549
  try:
550
+ response = await self.agent_system.process_chat(message, history)
551
+ history.append((message, response))
552
+ return history
 
 
 
 
 
 
553
  except Exception as e:
554
+ logger.error(f"Chat error: {e}")
555
+ return history + [(message, f"Error: {str(e)}")]
556
 
557
+ async def refresh_components():
558
+ return gr.Dropdown(choices=list(self.explorer.components.keys()))
559
+
560
+ async def add_component(name, type, code):
561
  try:
562
+ success = await self.explorer.add_component(name, type, code)
563
+ return {
564
+ component_list: gr.Dropdown(choices=list(self.explorer.components.keys())),
565
+ status_output: "Component added successfully" if success else "Failed to add component"
566
+ }
567
+ except Exception as e:
568
+ return {
569
+ status_output: f"Error adding component: {str(e)}"
570
+ }
571
+
572
+ async def show_component_details(name):
573
+ try:
574
+ component = await self.explorer.get_component(name)
575
+ if not component:
576
+ return None, None
577
+
578
+ graph = await self.explorer.visualize_dependencies(name)
579
+ return component, graph
580
  except Exception as e:
581
+ logger.error(f"Error showing component details: {e}")
582
+ return None, None
583
+
584
+ # Connect event handlers
585
+ autonomy_slider.change(
586
+ fn=update_autonomy,
587
+ inputs=[autonomy_slider],
588
+ outputs=[status_output]
589
+ )
590
+
591
+ process_button.click(
592
+ fn=process_request,
593
+ inputs=[
594
+ project_description,
595
+ autonomy_slider,
596
+ ],
597
+ outputs=[
598
+ decision_log,
599
+ frontend_code,
600
+ backend_code,
601
+ database_code,
602
+ test_results,
603
+ status_output,
604
+ progress
605
+ ]
606
+ )
607
+
608
+ chat_button.click(
609
+ fn=handle_chat,
610
+ inputs=[chat_input, agent_chat],
611
+ outputs=[agent_chat]
612
+ )
613
+
614
+ refresh_button.click(
615
+ fn=refresh_components,
616
+ outputs=[component_list]
617
  )
618
 
619
+ add_button.click(
620
+ fn=add_component,
621
+ inputs=[component_name, component_type, component_code],
622
+ outputs=[component_list, status_output]
623
  )
624
 
625
+ component_list.change(
626
+ fn=show_component_details,
627
+ inputs=[component_list],
628
+ outputs=[component_details, dependency_graph]
629
  )
630
 
631
  # Launch the interface
 
633
  server_port=self.config.port,
634
  share=self.config.share,
635
  debug=self.config.debug
636
+ )