Spaces:
Runtime error
Runtime error
acecalisto3
commited on
Commit
•
7c69181
1
Parent(s):
4fb77b7
Update app.py
Browse files
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
|
7 |
-
import
|
8 |
-
import
|
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
|
14 |
-
|
15 |
-
|
16 |
-
import
|
17 |
-
import
|
18 |
-
|
19 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
@dataclass
|
34 |
-
class
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
try:
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
#
|
53 |
-
|
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 |
-
|
60 |
-
|
61 |
-
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
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
|
96 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
try:
|
105 |
-
|
106 |
-
self.
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
except Exception as e:
|
109 |
-
logger.error(f"Error
|
110 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
-
|
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 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
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 |
-
|
144 |
-
|
145 |
try:
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
151 |
except Exception as e:
|
152 |
-
|
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 |
-
|
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
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
183 |
|
184 |
-
|
185 |
-
components = []
|
186 |
try:
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
|
|
191 |
except Exception as e:
|
192 |
-
|
193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
|
195 |
def launch(self) -> None:
|
196 |
with gr.Blocks(theme=gr.themes.Base()) as interface:
|
197 |
-
#
|
198 |
-
gr.Markdown(
|
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 |
-
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
|
251 |
-
if not description.strip():
|
252 |
-
return "", "Please provide a description"
|
253 |
-
|
254 |
try:
|
255 |
-
|
256 |
-
|
257 |
-
|
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"
|
266 |
-
return
|
267 |
|
268 |
-
def
|
|
|
|
|
|
|
269 |
try:
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
return
|
285 |
-
|
286 |
-
|
|
|
287 |
except Exception as e:
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
300 |
)
|
301 |
|
302 |
-
|
303 |
-
fn=
|
304 |
-
inputs=[
|
305 |
-
outputs=[
|
306 |
)
|
307 |
|
308 |
-
|
309 |
-
fn=
|
310 |
-
inputs=[],
|
311 |
-
outputs=[
|
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 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|