Spaces:
Sleeping
Sleeping
Update definitions.py
Browse files- definitions.py +187 -55
definitions.py
CHANGED
@@ -306,16 +306,26 @@ class AutonomousAgent:
|
|
306 |
def __init__(self, app):
|
307 |
self.app = app
|
308 |
self.workspace_manager = WorkspaceManager(workspace_dir=os.getenv('WORKSPACE_DIR', 'workspace'))
|
309 |
-
self.pipeline = self._initialize_pipeline()
|
310 |
self.refinement_loop = RefinementLoop(pipeline=self.pipeline)
|
311 |
self.interface = self.StreamlitInterface(self)
|
312 |
self.tools_repository = self._initialize_tool_repository()
|
313 |
self.chat_system = ChatSystem(self)
|
314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
315 |
def initialize_tool_repository(self, tool_repository: object) -> None:
|
316 |
"""
|
317 |
Initializes the tool repository.
|
318 |
-
|
319 |
Args:
|
320 |
tool_repository (object): The tool repository instance.
|
321 |
"""
|
@@ -348,7 +358,6 @@ class AutonomousAgent:
|
|
348 |
return self.SecurityAgent(agent_name)
|
349 |
else:
|
350 |
return None
|
351 |
-
|
352 |
class StreamlitInterface:
|
353 |
"""Streamlit UI integration for the Autonomous Agent system."""
|
354 |
|
@@ -592,20 +601,19 @@ class RefinementLoop:
|
|
592 |
def get_refinement_history(self) -> List[Dict[str, Any]]:
|
593 |
"""Get the history of refinement iterations."""
|
594 |
return self.history
|
|
|
|
|
595 |
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
self
|
600 |
-
|
601 |
-
|
602 |
-
self.refinement_loop = RefinementLoop(pipeline=self.pipeline)
|
603 |
-
self.interface = self.StreamlitInterface(self)
|
604 |
-
self.tools_repository = self._initialize_tool_repository()
|
605 |
-
self.chat_system = ChatSystem(self)
|
606 |
|
607 |
class ChatSystem:
|
608 |
"""Manages the chat interaction between users and the autonomous system."""
|
|
|
609 |
def __init__(self, agent):
|
610 |
self.agent = agent
|
611 |
self.chat_history = []
|
@@ -619,50 +627,45 @@ class RefinementLoop:
|
|
619 |
}
|
620 |
self.logger = logging.getLogger(__name__)
|
621 |
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
|
637 |
-
def _initialize_pipeline(self) -> 'DevelopmentPipeline':
|
638 |
-
"""Initialize the development pipeline."""
|
639 |
-
return self.DevelopmentPipeline(
|
640 |
-
workspace_manager=self.workspace_manager,
|
641 |
-
tool_manager=self._setup_tool_manager()
|
642 |
-
)
|
643 |
-
def _setup_tool_manager(self):
|
644 |
-
"""Setup tool manager with configuration."""
|
645 |
-
return ToolManager()
|
646 |
-
|
647 |
-
class StreamlitInterface:
|
648 |
-
"""Streamlit UI integration for the Autonomous Agent system."""
|
649 |
-
|
650 |
-
def __init__(self, app: 'AutonomousAgentApp'): # Use string forward reference
|
651 |
-
self.app = app
|
652 |
-
self.chat_system = ChatSystem(self.app.autonomous_agent)
|
653 |
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
659 |
-
|
660 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
661 |
def __init__(self, workspace_manager, tool_manager):
|
662 |
"""Initialize the development pipeline with the given workspace and tool managers."""
|
663 |
self.workspace_manager = workspace_manager
|
664 |
self.tool_manager = tool_manager
|
665 |
self.logger = logging.getLogger(__name__)
|
|
|
666 |
|
667 |
async def execute_stage(self, stage: PipelineStage, input_data: Dict) -> Dict[str, Any]:
|
668 |
"""Execute a pipeline stage and return results."""
|
@@ -684,20 +687,149 @@ class RefinementLoop:
|
|
684 |
async def _handle_planning(self, input_data: Dict) -> Dict:
|
685 |
"""Handle planning stage execution."""
|
686 |
self.logger.info("Handling planning stage")
|
687 |
-
|
688 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
689 |
|
690 |
async def _handle_development(self, input_data: Dict) -> Dict:
|
691 |
"""Handle development stage execution."""
|
692 |
self.logger.info("Handling development stage")
|
693 |
-
|
694 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
695 |
|
696 |
async def _handle_testing(self, input_data: Dict) -> Dict:
|
697 |
"""Handle testing stage execution."""
|
698 |
self.logger.info("Handling testing stage")
|
699 |
-
|
700 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
701 |
|
702 |
@dataclass
|
703 |
class QualityMetrics:
|
|
|
306 |
def __init__(self, app):
|
307 |
self.app = app
|
308 |
self.workspace_manager = WorkspaceManager(workspace_dir=os.getenv('WORKSPACE_DIR', 'workspace'))
|
309 |
+
self.pipeline = self._initialize_pipeline() # This line will now work
|
310 |
self.refinement_loop = RefinementLoop(pipeline=self.pipeline)
|
311 |
self.interface = self.StreamlitInterface(self)
|
312 |
self.tools_repository = self._initialize_tool_repository()
|
313 |
self.chat_system = ChatSystem(self)
|
314 |
|
315 |
+
def _setup_tool_manager(self):
|
316 |
+
"""Setup tool manager with configuration."""
|
317 |
+
return ToolManager()
|
318 |
+
|
319 |
+
def _initialize_pipeline(self) -> 'DevelopmentPipeline':
|
320 |
+
"""Initialize the development pipeline."""
|
321 |
+
return DevelopmentPipeline(
|
322 |
+
workspace_manager=self.workspace_manager,
|
323 |
+
tool_manager=self._setup_tool_manager()
|
324 |
+
)
|
325 |
+
|
326 |
def initialize_tool_repository(self, tool_repository: object) -> None:
|
327 |
"""
|
328 |
Initializes the tool repository.
|
|
|
329 |
Args:
|
330 |
tool_repository (object): The tool repository instance.
|
331 |
"""
|
|
|
358 |
return self.SecurityAgent(agent_name)
|
359 |
else:
|
360 |
return None
|
|
|
361 |
class StreamlitInterface:
|
362 |
"""Streamlit UI integration for the Autonomous Agent system."""
|
363 |
|
|
|
601 |
def get_refinement_history(self) -> List[Dict[str, Any]]:
|
602 |
"""Get the history of refinement iterations."""
|
603 |
return self.history
|
604 |
+
|
605 |
+
|
606 |
|
607 |
+
class StreamlitInterface:
|
608 |
+
"""Streamlit UI integration for the Autonomous Agent system."""
|
609 |
+
|
610 |
+
def __init__(self, app):
|
611 |
+
self.app = app
|
612 |
+
self.chat_system = self.app.chat_system
|
|
|
|
|
|
|
|
|
613 |
|
614 |
class ChatSystem:
|
615 |
"""Manages the chat interaction between users and the autonomous system."""
|
616 |
+
|
617 |
def __init__(self, agent):
|
618 |
self.agent = agent
|
619 |
self.chat_history = []
|
|
|
627 |
}
|
628 |
self.logger = logging.getLogger(__name__)
|
629 |
|
630 |
+
def initialize_autonomous_agent(self):
|
631 |
+
self.autonomous_agent = AutonomousAgent(self)
|
632 |
+
self.chat_system = self.autonomous_agent.ChatSystem(self.autonomous_agent)
|
633 |
+
self.interface = self.StreamlitInterface(self)
|
634 |
+
self.tools_repository = self._initialize_tool_repository()
|
635 |
+
self.autonomous_agent.initialize_tool_repository(self.tools_repository)
|
636 |
|
637 |
+
def _initialize_tool_repository(self):
|
638 |
+
"""Initializes the tool repository."""
|
639 |
+
tool_repository = ToolRepository()
|
640 |
+
tool_repository.add_tool(Tool(name="Code Review Tool", task="Review Code Quality"))
|
641 |
+
tool_repository.add_tool(Tool(name="Automated Testing Tool", task="Run Automated Tests"))
|
642 |
+
tool_repository.add_tool(Tool(name="Security Audit Tool", task="Run Security Audits"))
|
643 |
+
return tool_repository
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
644 |
|
645 |
+
def _initialize_pipeline(self) -> 'DevelopmentPipeline':
|
646 |
+
"""Initialize the development pipeline."""
|
647 |
+
return self.DevelopmentPipeline(
|
648 |
+
workspace_manager=self.workspace_manager,
|
649 |
+
tool_manager=self._setup_tool_manager()
|
650 |
+
)
|
651 |
|
652 |
+
def _setup_tool_manager(self):
|
653 |
+
"""Setup tool manager with configuration."""
|
654 |
+
return ToolManager()
|
655 |
+
|
656 |
+
class DevelopmentPipeline:
|
657 |
+
class PipelineStage(Enum):
|
658 |
+
"""Enum for pipeline stages."""
|
659 |
+
PLANNING = 1
|
660 |
+
DEVELOPMENT = 2
|
661 |
+
TESTING = 3
|
662 |
+
|
663 |
def __init__(self, workspace_manager, tool_manager):
|
664 |
"""Initialize the development pipeline with the given workspace and tool managers."""
|
665 |
self.workspace_manager = workspace_manager
|
666 |
self.tool_manager = tool_manager
|
667 |
self.logger = logging.getLogger(__name__)
|
668 |
+
|
669 |
|
670 |
async def execute_stage(self, stage: PipelineStage, input_data: Dict) -> Dict[str, Any]:
|
671 |
"""Execute a pipeline stage and return results."""
|
|
|
687 |
async def _handle_planning(self, input_data: Dict) -> Dict:
|
688 |
"""Handle planning stage execution."""
|
689 |
self.logger.info("Handling planning stage")
|
690 |
+
|
691 |
+
try:
|
692 |
+
task = input_data.get("task", "")
|
693 |
+
if not task:
|
694 |
+
raise ValueError("No task provided for planning")
|
695 |
+
|
696 |
+
# Step 1: Analyze the task and break it into subtasks
|
697 |
+
subtasks = self._break_down_task(task)
|
698 |
+
|
699 |
+
# Step 2: Generate a development plan
|
700 |
+
development_plan = {
|
701 |
+
"task": task,
|
702 |
+
"subtasks": subtasks,
|
703 |
+
"milestones": self._define_milestones(subtasks),
|
704 |
+
"timeline": self._estimate_timeline(subtasks)
|
705 |
+
}
|
706 |
+
|
707 |
+
# Step 3: Create initial project artifacts (e.g., requirements.txt)
|
708 |
+
self.workspace_manager.create_file("requirements.txt", self._generate_requirements(subtasks))
|
709 |
+
|
710 |
+
return {
|
711 |
+
"status": "success",
|
712 |
+
"result": {"plan": development_plan},
|
713 |
+
"artifacts": ["requirements.txt"]
|
714 |
+
}
|
715 |
+
|
716 |
+
except Exception as e:
|
717 |
+
self.logger.error(f"Error in planning stage: {str(e)}")
|
718 |
+
return {"status": "error", "error": str(e)}
|
719 |
+
|
720 |
+
def _break_down_task(self, task: str) -> List[str]:
|
721 |
+
"""Break down a task into smaller subtasks."""
|
722 |
+
# Example: Use a simple heuristic to split the task into subtasks
|
723 |
+
return [f"Subtask {i+1}: {part}" for i, part in enumerate(task.split(","))]
|
724 |
+
|
725 |
+
def _define_milestones(self, subtasks: List[str]) -> List[str]:
|
726 |
+
"""Define milestones based on subtasks."""
|
727 |
+
return [f"Complete {subtask}" for subtask in subtasks]
|
728 |
+
|
729 |
+
def _estimate_timeline(self, subtasks: List[str]) -> Dict[str, int]:
|
730 |
+
"""Estimate a timeline for the subtasks."""
|
731 |
+
# Example: Assign 1 day per subtask
|
732 |
+
return {subtask: 1 for subtask in subtasks}
|
733 |
+
|
734 |
+
def _generate_requirements(self, subtasks: List[str]) -> str:
|
735 |
+
"""Generate a requirements document based on subtasks."""
|
736 |
+
return "\n".join([f"Requirement: {subtask}" for subtask in subtasks])
|
737 |
|
738 |
async def _handle_development(self, input_data: Dict) -> Dict:
|
739 |
"""Handle development stage execution."""
|
740 |
self.logger.info("Handling development stage")
|
741 |
+
|
742 |
+
try:
|
743 |
+
plan = input_data.get("result", {}).get("plan", {})
|
744 |
+
if not plan:
|
745 |
+
raise ValueError("No development plan provided")
|
746 |
+
|
747 |
+
# Step 1: Generate boilerplate code
|
748 |
+
self.workspace_manager.create_file("main.py", self._generate_boilerplate_code(plan))
|
749 |
+
|
750 |
+
# Step 2: Implement functionality for each subtask
|
751 |
+
for subtask in plan.get("subtasks", []):
|
752 |
+
self._implement_subtask(subtask)
|
753 |
+
|
754 |
+
return {
|
755 |
+
"status": "success",
|
756 |
+
"result": {"code": "print('Hello World')"}, # Example output
|
757 |
+
"artifacts": ["main.py"]
|
758 |
+
}
|
759 |
+
|
760 |
+
except Exception as e:
|
761 |
+
self.logger.error(f"Error in development stage: {str(e)}")
|
762 |
+
return {"status": "error", "error": str(e)}
|
763 |
+
|
764 |
+
def _generate_boilerplate_code(self, plan: Dict) -> str:
|
765 |
+
"""Generate boilerplate code based on the development plan."""
|
766 |
+
return f"# Project: {plan.get('task', 'Untitled')}\n\n" \
|
767 |
+
f"# Subtasks:\n" \
|
768 |
+
f"{''.join([f'# {subtask}\n' for subtask in plan.get('subtasks', [])])}\n" \
|
769 |
+
f"def main():\n" \
|
770 |
+
f" print('Hello World')\n\n" \
|
771 |
+
f"if __name__ == '__main__':\n" \
|
772 |
+
f" main()"
|
773 |
+
|
774 |
+
def _implement_subtask(self, subtask: str) -> None:
|
775 |
+
"""Implement functionality for a subtask."""
|
776 |
+
# Example: Append subtask implementation to main.py
|
777 |
+
with open(os.path.join(self.workspace_manager.workspace_dir, "main.py"), "a") as file:
|
778 |
+
file.write(f"\n# Implementation for {subtask}\n")
|
779 |
|
780 |
async def _handle_testing(self, input_data: Dict) -> Dict:
|
781 |
"""Handle testing stage execution."""
|
782 |
self.logger.info("Handling testing stage")
|
783 |
+
|
784 |
+
try:
|
785 |
+
code_path = os.path.join(self.workspace_manager.workspace_dir, "main.py")
|
786 |
+
if not os.path.exists(code_path):
|
787 |
+
raise FileNotFoundError("No code found for testing")
|
788 |
+
|
789 |
+
# Step 1: Run unit tests
|
790 |
+
test_results = self._run_unit_tests(code_path)
|
791 |
+
|
792 |
+
# Step 2: Generate a test report
|
793 |
+
test_report = self._generate_test_report(test_results)
|
794 |
+
self.workspace_manager.create_file("test_report.html", test_report)
|
795 |
+
|
796 |
+
return {
|
797 |
+
"status": "success",
|
798 |
+
"result": {"test_results": test_results},
|
799 |
+
"artifacts": ["test_report.html"]
|
800 |
+
}
|
801 |
+
|
802 |
+
except Exception as e:
|
803 |
+
self.logger.error(f"Error in testing stage: {str(e)}")
|
804 |
+
return {"status": "error", "error": str(e)}
|
805 |
+
|
806 |
+
def _run_unit_tests(self, code_path: str) -> Dict[str, Any]:
|
807 |
+
"""Run unit tests on the code."""
|
808 |
+
# Example: Use a testing framework like pytest or unittest
|
809 |
+
# For simplicity, return a dummy result
|
810 |
+
return {
|
811 |
+
"tests_run": 5,
|
812 |
+
"tests_passed": 5,
|
813 |
+
"tests_failed": 0,
|
814 |
+
"coverage": "100%"
|
815 |
+
}
|
816 |
+
|
817 |
+
def _generate_test_report(self, test_results: Dict) -> str:
|
818 |
+
"""Generate an HTML test report."""
|
819 |
+
return f"""
|
820 |
+
<html>
|
821 |
+
<head><title>Test Report</title></head>
|
822 |
+
<body>
|
823 |
+
<h1>Test Report</h1>
|
824 |
+
<ul>
|
825 |
+
<li>Tests Run: {test_results.get('tests_run', 0)}</li>
|
826 |
+
<li>Tests Passed: {test_results.get('tests_passed', 0)}</li>
|
827 |
+
<li>Tests Failed: {test_results.get('tests_failed', 0)}</li>
|
828 |
+
<li>Coverage: {test_results.get('coverage', '0%')}</li>
|
829 |
+
</ul>
|
830 |
+
</body>
|
831 |
+
</html>
|
832 |
+
"""
|
833 |
|
834 |
@dataclass
|
835 |
class QualityMetrics:
|