Spaces:
Sleeping
Sleeping
Update definitions.py
Browse files- definitions.py +163 -159
definitions.py
CHANGED
@@ -25,167 +25,11 @@ import json
|
|
25 |
import traceback
|
26 |
from pathlib import Path
|
27 |
|
28 |
-
# Set logging level from environment variable
|
29 |
-
logging.basicConfig(level=os.getenv('LOG_LEVEL', 'INFO'))
|
30 |
-
|
31 |
-
class AutonomousAgentApp:
|
32 |
-
"""Main application class for the Autonomous Agent System"""
|
33 |
-
|
34 |
-
def __init__(self):
|
35 |
-
self.workspace_manager = self.WorkspaceManager(workspace_dir=os.getenv('WORKSPACE_DIR', 'workspace')) # Use self.WorkspaceManager
|
36 |
-
self.pipeline = self._initialize_pipeline()
|
37 |
-
self.refinement_loop = self.RefinementLoop(pipeline=self.pipeline) # Use self.RefinementLoop
|
38 |
-
self.interface = self.StreamlitInterface(self) # Use self.StreamlitInterface
|
39 |
-
|
40 |
-
def _initialize_pipeline(self) -> 'AutonomousAgentApp.DevelopmentPipeline':
|
41 |
-
"""Initialize the development pipeline"""
|
42 |
-
return self.DevelopmentPipeline(
|
43 |
-
workspace_manager=self.workspace_manager,
|
44 |
-
tool_manager=self._setup_tool_manager()
|
45 |
-
)
|
46 |
-
|
47 |
-
def _setup_tool_manager(self):
|
48 |
-
"""Setup tool manager with configuration"""
|
49 |
-
return self.ToolManager() # Use self.ToolManager
|
50 |
-
|
51 |
-
class ChatSystem:
|
52 |
-
"""Manages the chat interaction between users and the autonomous system"""
|
53 |
-
|
54 |
-
def __init__(self, agent: 'AutonomousAgentApp.AutonomousAgent'):
|
55 |
-
self.agent = agent
|
56 |
-
self.chat_history = []
|
57 |
-
self.active_tasks = {}
|
58 |
-
self.command_handlers = {
|
59 |
-
'/task': self.handle_task_command,
|
60 |
-
'/status': self.handle_status_command,
|
61 |
-
'/stop': self.handle_stop_command,
|
62 |
-
'/help': self.handle_help_command,
|
63 |
-
'/modify': self.handle_modify_command
|
64 |
-
}
|
65 |
-
|
66 |
-
def render_chat_interface(self):
|
67 |
-
"""Render the chat interface in Streamlit sidebar"""
|
68 |
-
with st.sidebar:
|
69 |
-
st.markdown("---")
|
70 |
-
st.subheader("System Chat")
|
71 |
-
|
72 |
-
# Chat controls
|
73 |
-
if st.button("Clear Chat History"):
|
74 |
-
self.clear_chat_history()
|
75 |
-
|
76 |
-
# Chat history display
|
77 |
-
chat_container = st.container()
|
78 |
-
with chat_container:
|
79 |
-
for message in self.chat_history:
|
80 |
-
self._render_message(message)
|
81 |
-
|
82 |
-
# Input area
|
83 |
-
user_input = st.text_input("Type message/command...", key="chat_input")
|
84 |
-
if st.button("Send", key="send_message"):
|
85 |
-
self.process_user_input(user_input)
|
86 |
-
|
87 |
-
class RefinementLoop:
|
88 |
-
"""Manages the iterative refinement process"""
|
89 |
-
|
90 |
-
def __init__(self, pipeline):
|
91 |
-
self.pipeline = pipeline
|
92 |
-
self.max_iterations = 10
|
93 |
-
self.quality_metrics = QualityMetrics()
|
94 |
-
self.logger = logging.getLogger(__name__)
|
95 |
-
self.current_iteration = 0
|
96 |
-
self.history = []
|
97 |
-
|
98 |
-
async def run_refinement_cycle(self, task: str) -> Dict[str, Any]:
|
99 |
-
"""Run a complete refinement cycle for the given task"""
|
100 |
-
self.logger.info(f"Starting refinement cycle for task: {task}")
|
101 |
-
self.current_iteration = 0
|
102 |
-
|
103 |
-
try:
|
104 |
-
while self.current_iteration < self.max_iterations:
|
105 |
-
self.logger.info(f"Starting iteration {self.current_iteration + 1}")
|
106 |
-
|
107 |
-
# Execute pipeline stages
|
108 |
-
planning_result = await self.pipeline.execute_stage(
|
109 |
-
self.pipeline.PipelineStage.PLANNING,
|
110 |
-
{"task": task}
|
111 |
-
)
|
112 |
-
|
113 |
-
development_result = await self.pipeline.execute_stage(
|
114 |
-
self.pipeline.PipelineStage.DEVELOPMENT,
|
115 |
-
planning_result["result"]
|
116 |
-
)
|
117 |
-
|
118 |
-
testing_result = await self.pipeline.execute_stage(
|
119 |
-
self.pipeline.PipelineStage.TESTING,
|
120 |
-
development_result["result"]
|
121 |
-
)
|
122 |
-
|
123 |
-
# Analyze results
|
124 |
-
quality_analysis = self._analyze_quality(testing_result["result"])
|
125 |
-
|
126 |
-
# Record iteration history
|
127 |
-
self.history.append({
|
128 |
-
"iteration": self.current_iteration,
|
129 |
-
"quality_metrics": quality_analysis,
|
130 |
-
"timestamp": datetime.now()
|
131 |
-
})
|
132 |
-
|
133 |
-
# Check if quality requirements are met
|
134 |
-
if self._meets_quality_requirements(quality_analysis):
|
135 |
-
self.logger.info("Quality requirements met. Refinement cycle complete.")
|
136 |
-
return self._prepare_final_result(quality_analysis)
|
137 |
-
|
138 |
-
self.current_iteration += 1
|
139 |
-
|
140 |
-
return {
|
141 |
-
"status": "max_iterations_reached",
|
142 |
-
"iterations_completed": self.current_iteration,
|
143 |
-
"final_quality": quality_analysis
|
144 |
-
}
|
145 |
-
|
146 |
-
except Exception as e:
|
147 |
-
self.logger.error(f"Error in refinement cycle: {str(e)}")
|
148 |
-
return {"status": "error", "error": str(e)}
|
149 |
-
|
150 |
-
def _analyze_quality(self, result: Dict[str, Any]) -> Dict[str, float]:
|
151 |
-
"""Analyze the quality metrics of the current iteration"""
|
152 |
-
return {
|
153 |
-
"code_quality": self.quality_metrics.code_quality_score,
|
154 |
-
"test_coverage": self.quality_metrics.test_coverage,
|
155 |
-
"security_score": float(self.quality_metrics.security_score)
|
156 |
-
}
|
157 |
|
158 |
-
def _meets_quality_requirements(self, quality_analysis: Dict[str, float]) -> bool:
|
159 |
-
"""Check if the current quality metrics meet the requirements"""
|
160 |
-
thresholds = self.quality_metrics.thresholds
|
161 |
-
return (
|
162 |
-
quality_analysis["code_quality"] >= thresholds["code_quality"] and
|
163 |
-
quality_analysis["test_coverage"] >= thresholds["test_coverage"] and
|
164 |
-
quality_analysis["security_score"] >= thresholds["security"]
|
165 |
-
)
|
166 |
|
167 |
-
|
168 |
-
|
169 |
-
return {
|
170 |
-
"status": "success",
|
171 |
-
"iterations_completed": self.current_iteration,
|
172 |
-
"final_quality": quality_analysis,
|
173 |
-
"history": self.history
|
174 |
-
}
|
175 |
|
176 |
-
def get_refinement_history(self) -> List[Dict[str, Any]]:
|
177 |
-
"""Get the history of refinement iterations"""
|
178 |
-
return self.history
|
179 |
-
|
180 |
-
def run(self):
|
181 |
-
"""Main application entry point"""
|
182 |
-
try:
|
183 |
-
logging.info("Starting Autonomous Agent Application")
|
184 |
-
self.interface.render_main_interface()
|
185 |
-
except Exception as e:
|
186 |
-
logging.error(f"Application error: {str(e)}")
|
187 |
-
st.error("An error occurred while starting the application. Please check the logs.")
|
188 |
-
raise
|
189 |
class CodeMetricsAnalyzer:
|
190 |
"""Analyzes code metrics using various tools"""
|
191 |
|
@@ -380,6 +224,166 @@ class AutonomousAgentApp:
|
|
380 |
"previous": recent_values[0]
|
381 |
}
|
382 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
383 |
@dataclass
|
384 |
class QualityMetrics:
|
385 |
"""Advanced quality metrics tracking and analysis"""
|
@@ -719,4 +723,4 @@ class AutonomousAgentApp:
|
|
719 |
st.write(f"Available Memory: {psutil.virtual_memory().available / (1024**3):.1f} GB free")
|
720 |
|
721 |
if __name__ == "__main__":
|
722 |
-
|
|
|
25 |
import traceback
|
26 |
from pathlib import Path
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
# Set logging level from environment variable
|
31 |
+
logging.basicConfig(level=os.getenv('LOG_LEVEL', 'INFO'))
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
class CodeMetricsAnalyzer:
|
34 |
"""Analyzes code metrics using various tools"""
|
35 |
|
|
|
224 |
"previous": recent_values[0]
|
225 |
}
|
226 |
|
227 |
+
class AutonomousAgentApp:
|
228 |
+
"""Main application class for the Autonomous Agent System"""
|
229 |
+
|
230 |
+
def __init__(self):
|
231 |
+
self.workspace_manager = self.WorkspaceManager(workspace_dir=os.getenv('WORKSPACE_DIR', 'workspace')) # Use self.WorkspaceManager
|
232 |
+
self.pipeline = self._initialize_pipeline()
|
233 |
+
self.refinement_loop = self.RefinementLoop(pipeline=self.pipeline) # Use self.RefinementLoop
|
234 |
+
self.interface = self.StreamlitInterface(self) # Use self.StreamlitInterface
|
235 |
+
|
236 |
+
def _initialize_pipeline(self) -> 'AutonomousAgentApp.DevelopmentPipeline':
|
237 |
+
"""Initialize the development pipeline"""
|
238 |
+
return self.DevelopmentPipeline(
|
239 |
+
workspace_manager=self.workspace_manager,
|
240 |
+
tool_manager=self._setup_tool_manager()
|
241 |
+
)
|
242 |
+
|
243 |
+
def _setup_tool_manager(self):
|
244 |
+
"""Setup tool manager with configuration"""
|
245 |
+
return self.ToolManager() # Use self.ToolManager
|
246 |
+
|
247 |
+
class ChatSystem:
|
248 |
+
"""Manages the chat interaction between users and the autonomous system"""
|
249 |
+
|
250 |
+
def __init__(self, agent: 'AutonomousAgentApp.AutonomousAgent'):
|
251 |
+
self.agent = agent
|
252 |
+
self.chat_history = []
|
253 |
+
self.active_tasks = {}
|
254 |
+
self.command_handlers = {
|
255 |
+
'/task': self.handle_task_command,
|
256 |
+
'/status': self.handle_status_command,
|
257 |
+
'/stop': self.handle_stop_command,
|
258 |
+
'/help': self.handle_help_command,
|
259 |
+
'/modify': self.handle_modify_command
|
260 |
+
}
|
261 |
+
|
262 |
+
def render_chat_interface(self):
|
263 |
+
"""Render the chat interface in Streamlit sidebar"""
|
264 |
+
with st.sidebar:
|
265 |
+
st.markdown("---")
|
266 |
+
st.subheader("System Chat")
|
267 |
+
|
268 |
+
# Chat controls
|
269 |
+
if st.button("Clear Chat History"):
|
270 |
+
self.clear_chat_history()
|
271 |
+
|
272 |
+
# Chat history display
|
273 |
+
chat_container = st.container()
|
274 |
+
with chat_container:
|
275 |
+
for message in self.chat_history:
|
276 |
+
self._render_message(message)
|
277 |
+
|
278 |
+
# Input area
|
279 |
+
user_input = st.text_input("Type message/command...", key="chat_input")
|
280 |
+
if st.button("Send", key="send_message"):
|
281 |
+
self.process_user_input(user_input)
|
282 |
+
|
283 |
+
class RefinementLoop:
|
284 |
+
"""Manages the iterative refinement process"""
|
285 |
+
|
286 |
+
def __init__(self, pipeline):
|
287 |
+
self.pipeline = pipeline
|
288 |
+
self.max_iterations = 10
|
289 |
+
self.quality_metrics = QualityMetrics()
|
290 |
+
self.logger = logging.getLogger(__name__)
|
291 |
+
self.current_iteration = 0
|
292 |
+
self.history = []
|
293 |
+
|
294 |
+
async def run_refinement_cycle(self, task: str) -> Dict[str, Any]:
|
295 |
+
"""Run a complete refinement cycle for the given task"""
|
296 |
+
self.logger.info(f"Starting refinement cycle for task: {task}")
|
297 |
+
self.current_iteration = 0
|
298 |
+
|
299 |
+
try:
|
300 |
+
while self.current_iteration < self.max_iterations:
|
301 |
+
self.logger.info(f"Starting iteration {self.current_iteration + 1}")
|
302 |
+
|
303 |
+
# Execute pipeline stages
|
304 |
+
planning_result = await self.pipeline.execute_stage(
|
305 |
+
self.pipeline.PipelineStage.PLANNING,
|
306 |
+
{"task": task}
|
307 |
+
)
|
308 |
+
|
309 |
+
development_result = await self.pipeline.execute_stage(
|
310 |
+
self.pipeline.PipelineStage.DEVELOPMENT,
|
311 |
+
planning_result["result"]
|
312 |
+
)
|
313 |
+
|
314 |
+
testing_result = await self.pipeline.execute_stage(
|
315 |
+
self.pipeline.PipelineStage.TESTING,
|
316 |
+
development_result["result"]
|
317 |
+
)
|
318 |
+
|
319 |
+
# Analyze results
|
320 |
+
quality_analysis = self._analyze_quality(testing_result["result"])
|
321 |
+
|
322 |
+
# Record iteration history
|
323 |
+
self.history.append({
|
324 |
+
"iteration": self.current_iteration,
|
325 |
+
"quality_metrics": quality_analysis,
|
326 |
+
"timestamp": datetime.now()
|
327 |
+
})
|
328 |
+
|
329 |
+
# Check if quality requirements are met
|
330 |
+
if self._meets_quality_requirements(quality_analysis):
|
331 |
+
self.logger.info("Quality requirements met. Refinement cycle complete.")
|
332 |
+
return self._prepare_final_result(quality_analysis)
|
333 |
+
|
334 |
+
self.current_iteration += 1
|
335 |
+
|
336 |
+
return {
|
337 |
+
"status": "max_iterations_reached",
|
338 |
+
"iterations_completed": self.current_iteration,
|
339 |
+
"final_quality": quality_analysis
|
340 |
+
}
|
341 |
+
|
342 |
+
except Exception as e:
|
343 |
+
self.logger.error(f"Error in refinement cycle: {str(e)}")
|
344 |
+
return {"status": "error", "error": str(e)}
|
345 |
+
|
346 |
+
def _analyze_quality(self, result: Dict[str, Any]) -> Dict[str, float]:
|
347 |
+
"""Analyze the quality metrics of the current iteration"""
|
348 |
+
return {
|
349 |
+
"code_quality": self.quality_metrics.code_quality_score,
|
350 |
+
"test_coverage": self.quality_metrics.test_coverage,
|
351 |
+
"security_score": float(self.quality_metrics.security_score)
|
352 |
+
}
|
353 |
+
|
354 |
+
def _meets_quality_requirements(self, quality_analysis: Dict[str, float]) -> bool:
|
355 |
+
"""Check if the current quality metrics meet the requirements"""
|
356 |
+
thresholds = self.quality_metrics.thresholds
|
357 |
+
return (
|
358 |
+
quality_analysis["code_quality"] >= thresholds["code_quality"] and
|
359 |
+
quality_analysis["test_coverage"] >= thresholds["test_coverage"] and
|
360 |
+
quality_analysis["security_score"] >= thresholds["security"]
|
361 |
+
)
|
362 |
+
|
363 |
+
def _prepare_final_result(self, quality_analysis: Dict[str, float]) -> Dict[str, Any]:
|
364 |
+
"""Prepare the final result of the refinement cycle"""
|
365 |
+
return {
|
366 |
+
"status": "success",
|
367 |
+
"iterations_completed": self.current_iteration,
|
368 |
+
"final_quality": quality_analysis,
|
369 |
+
"history": self.history
|
370 |
+
}
|
371 |
+
|
372 |
+
def get_refinement_history(self) -> List[Dict[str, Any]]:
|
373 |
+
"""Get the history of refinement iterations"""
|
374 |
+
return self.history
|
375 |
+
|
376 |
+
def run(self):
|
377 |
+
"""Main application entry point"""
|
378 |
+
try:
|
379 |
+
logging.info("Starting Autonomous Agent Application")
|
380 |
+
self.interface.render_main_interface()
|
381 |
+
except Exception as e:
|
382 |
+
logging.error(f"Application error: {str(e)}")
|
383 |
+
st.error("An error occurred while starting the application. Please check the logs.")
|
384 |
+
raise
|
385 |
+
|
386 |
+
|
387 |
@dataclass
|
388 |
class QualityMetrics:
|
389 |
"""Advanced quality metrics tracking and analysis"""
|
|
|
723 |
st.write(f"Available Memory: {psutil.virtual_memory().available / (1024**3):.1f} GB free")
|
724 |
|
725 |
if __name__ == "__main__":
|
726 |
+
app.launch()
|