Spaces:
Runtime error
Runtime error
nananie143
commited on
Commit
·
59a4304
1
Parent(s):
cb13344
Made entire workflow async with proper async/await patterns
Browse files
app.py
CHANGED
@@ -755,152 +755,108 @@ class EnhancedAIFlow(AIFlow):
|
|
755 |
self.file_manager = FileOperationManager(self.context_manager)
|
756 |
self.command_manager = CommandManager(self.context_manager)
|
757 |
self.rule_system = RuleSystem()
|
|
|
|
|
|
|
758 |
|
759 |
-
async def
|
760 |
-
"""Execute a specific agent's task with the given prompt."""
|
761 |
-
try:
|
762 |
-
if role == AgentRole.ARCHITECT:
|
763 |
-
agent = get_agent("architect")
|
764 |
-
elif role == AgentRole.UI_DESIGNER:
|
765 |
-
agent = get_agent("ui_designer")
|
766 |
-
elif role == AgentRole.BACKEND_DEVELOPER:
|
767 |
-
agent = get_agent("backend_developer")
|
768 |
-
elif role == AgentRole.DATABASE_ENGINEER:
|
769 |
-
agent = get_agent("database_engineer")
|
770 |
-
elif role == AgentRole.SECURITY_EXPERT:
|
771 |
-
agent = get_agent("security_expert")
|
772 |
-
elif role == AgentRole.QA_ENGINEER:
|
773 |
-
agent = get_agent("qa_engineer")
|
774 |
-
elif role == AgentRole.DEVOPS_ENGINEER:
|
775 |
-
agent = get_agent("devops_engineer")
|
776 |
-
elif role == AgentRole.DOCUMENTATION_WRITER:
|
777 |
-
agent = get_agent("documentation_writer")
|
778 |
-
else:
|
779 |
-
raise ValueError(f"Unknown agent role: {role}")
|
780 |
-
|
781 |
-
# Execute the agent's task
|
782 |
-
result = agent.run(prompt)
|
783 |
-
|
784 |
-
# Log the execution
|
785 |
-
logger.info(f"Agent {role.value} completed task")
|
786 |
-
|
787 |
-
return result
|
788 |
-
except Exception as e:
|
789 |
-
logger.error(f"Agent {role.value} failed: {str(e)}")
|
790 |
-
raise
|
791 |
-
|
792 |
-
async def execute_flow(self, requirements: str):
|
793 |
"""Execute the AI Flow and build the project."""
|
794 |
try:
|
795 |
-
# Initialize flow with
|
796 |
self.initialize_flow()
|
797 |
-
self.global_context["requirements"] = requirements
|
798 |
-
self.context_manager.global_context.update(self.global_context)
|
799 |
-
|
800 |
-
# Add memory of requirements
|
801 |
-
self.context_manager.add_memory(
|
802 |
-
"requirements",
|
803 |
-
requirements,
|
804 |
-
{"timestamp": datetime.now()}
|
805 |
-
)
|
806 |
-
|
807 |
-
# Get all paths through the flow graph
|
808 |
-
paths = list(nx.all_simple_paths(
|
809 |
-
self.flow_graph,
|
810 |
-
AgentRole.ARCHITECT,
|
811 |
-
AgentRole.DOCUMENTATION_WRITER
|
812 |
-
))
|
813 |
-
|
814 |
-
# Execute paths with enhanced context
|
815 |
-
await self._execute_paths(paths)
|
816 |
|
817 |
# Extract app name
|
818 |
app_name = requirements.split()[0].lower().replace(" ", "_")
|
819 |
|
820 |
-
#
|
821 |
-
|
822 |
-
|
823 |
-
"src": {
|
824 |
-
"components": {},
|
825 |
-
"pages": {},
|
826 |
-
"styles": {},
|
827 |
-
},
|
828 |
-
"package.json": "{\n \"name\": \"frontend\",\n \"version\": \"1.0.0\"\n}",
|
829 |
-
"README.md": "# Frontend\n"
|
830 |
-
},
|
831 |
-
"backend": {
|
832 |
-
"src": {
|
833 |
-
"routes": {},
|
834 |
-
"models": {},
|
835 |
-
"controllers": {},
|
836 |
-
},
|
837 |
-
"requirements.txt": "fastapi\nuvicorn\n",
|
838 |
-
"README.md": "# Backend\n"
|
839 |
-
},
|
840 |
-
"README.md": f"# {app_name}\nGenerated by AI Flow\n"
|
841 |
-
}
|
842 |
|
843 |
-
#
|
844 |
-
|
|
|
845 |
|
846 |
-
# Create
|
847 |
download_path = self.output_manager.create_download(app_name)
|
848 |
|
849 |
-
|
850 |
-
|
851 |
-
|
852 |
-
|
853 |
-
|
854 |
-
|
855 |
-
|
856 |
-
|
857 |
-
|
858 |
-
|
859 |
|
|
|
860 |
To download your project, use this path: {download_path}
|
861 |
|
862 |
-
##
|
863 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
864 |
"""
|
865 |
-
for file_path in self.project_builder.file_manifest:
|
866 |
-
rel_path = file_path.relative_to(self.project_builder.current_build)
|
867 |
-
results += f"- {rel_path}\n"
|
868 |
-
|
869 |
-
return results
|
870 |
-
|
871 |
except Exception as e:
|
872 |
-
logger.error(f"
|
873 |
raise
|
874 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
875 |
async def _execute_agent(self, role: AgentRole):
|
876 |
"""Execute a single agent's tasks with enhanced context."""
|
877 |
-
context = self.contexts[role]
|
878 |
-
context.state = FlowState.RUNNING
|
879 |
-
|
880 |
try:
|
881 |
-
|
882 |
-
|
|
|
883 |
|
884 |
-
|
885 |
-
prompt
|
886 |
|
887 |
-
# Execute agent
|
888 |
result = await self._execute_agent_task(role, prompt)
|
889 |
|
890 |
-
#
|
891 |
-
context.artifacts
|
892 |
context.state = FlowState.COMPLETED
|
893 |
|
894 |
-
# Update memory
|
895 |
-
self.context_manager.add_memory(
|
896 |
-
f"agent_result_{role.value}",
|
897 |
-
result,
|
898 |
-
{"role": role.value, "timestamp": datetime.now()}
|
899 |
-
)
|
900 |
-
|
901 |
except Exception as e:
|
902 |
context.state = FlowState.FAILED
|
903 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
904 |
raise
|
905 |
|
906 |
# Update the multi_agent_workflow function to use AI Flows
|
@@ -913,50 +869,20 @@ async def multi_agent_workflow(requirements: str) -> str:
|
|
913 |
str: Generated app code and API details.
|
914 |
"""
|
915 |
try:
|
916 |
-
# Initialize
|
917 |
-
|
918 |
-
results = await flow.execute_flow(requirements)
|
919 |
-
|
920 |
-
# Extract app name
|
921 |
-
app_name = requirements.split()[0].lower().replace(" ", "_")
|
922 |
|
923 |
-
#
|
924 |
-
|
925 |
-
documentation = generate_documentation(app_name, requirements, flow.contexts[AgentRole.DOCUMENTATION_WRITER].artifacts)
|
926 |
|
927 |
-
|
928 |
-
|
929 |
-
|
930 |
-
## Project Structure
|
931 |
-
```
|
932 |
-
{project_structure}
|
933 |
-
```
|
934 |
-
|
935 |
-
{results}
|
936 |
-
|
937 |
-
## Documentation
|
938 |
-
{documentation}
|
939 |
-
|
940 |
-
## Next Steps
|
941 |
-
1. Review the generated architecture and components
|
942 |
-
2. Set up the development environment
|
943 |
-
3. Implement the components following the provided structure
|
944 |
-
4. Run the test suite
|
945 |
-
5. Deploy using the provided configurations
|
946 |
-
|
947 |
-
## Support
|
948 |
-
For any issues or questions, please refer to the documentation or create an issue in the repository.
|
949 |
-
"""
|
950 |
except Exception as e:
|
951 |
-
|
952 |
-
|
953 |
-
return error_msg
|
954 |
-
finally:
|
955 |
-
if torch.cuda.is_available():
|
956 |
-
torch.cuda.empty_cache()
|
957 |
|
958 |
# Update the app_generator function to handle async execution
|
959 |
-
def app_generator(requirements: str):
|
960 |
"""
|
961 |
Generate an app based on the provided requirements using AI Flows.
|
962 |
Args:
|
@@ -964,20 +890,13 @@ def app_generator(requirements: str):
|
|
964 |
Returns:
|
965 |
str: Generated app code and API details.
|
966 |
"""
|
967 |
-
if not requirements or len(requirements.strip()) == 0:
|
968 |
-
return "Please provide app requirements."
|
969 |
-
|
970 |
try:
|
971 |
-
#
|
972 |
-
|
973 |
-
|
974 |
-
return loop.run_until_complete(multi_agent_workflow(requirements))
|
975 |
except Exception as e:
|
976 |
-
|
977 |
-
|
978 |
-
return error_msg
|
979 |
-
finally:
|
980 |
-
loop.close()
|
981 |
|
982 |
class StreamHandler:
|
983 |
"""Handles streaming output for the Gradio interface."""
|
|
|
755 |
self.file_manager = FileOperationManager(self.context_manager)
|
756 |
self.command_manager = CommandManager(self.context_manager)
|
757 |
self.rule_system = RuleSystem()
|
758 |
+
self.flow_graph = nx.DiGraph()
|
759 |
+
self.contexts: Dict[AgentRole, AgentContext] = {}
|
760 |
+
self.global_context = {}
|
761 |
|
762 |
+
async def execute_flow(self, requirements: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
763 |
"""Execute the AI Flow and build the project."""
|
764 |
try:
|
765 |
+
# Initialize flow with requirements
|
766 |
self.initialize_flow()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
767 |
|
768 |
# Extract app name
|
769 |
app_name = requirements.split()[0].lower().replace(" ", "_")
|
770 |
|
771 |
+
# Execute agents in parallel where possible
|
772 |
+
paths = list(nx.all_simple_paths(self.flow_graph, AgentRole.ARCHITECT, AgentRole.DOCUMENTATION_WRITER))
|
773 |
+
await self._execute_paths(paths)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
774 |
|
775 |
+
# Generate project structure and documentation
|
776 |
+
project_structure = generate_project_structure(app_name, self.contexts[AgentRole.ARCHITECT].artifacts)
|
777 |
+
documentation = generate_documentation(app_name, requirements, self.contexts[AgentRole.DOCUMENTATION_WRITER].artifacts)
|
778 |
|
779 |
+
# Create downloadable output
|
780 |
download_path = self.output_manager.create_download(app_name)
|
781 |
|
782 |
+
return f"""
|
783 |
+
# {app_name.title()} - Generated Application
|
784 |
+
|
785 |
+
## Project Structure
|
786 |
+
```
|
787 |
+
{project_structure}
|
788 |
+
```
|
789 |
+
|
790 |
+
## Documentation
|
791 |
+
{documentation}
|
792 |
|
793 |
+
## Download
|
794 |
To download your project, use this path: {download_path}
|
795 |
|
796 |
+
## Next Steps
|
797 |
+
1. Review the generated architecture and components
|
798 |
+
2. Set up the development environment
|
799 |
+
3. Implement the components following the provided structure
|
800 |
+
4. Run the test suite
|
801 |
+
5. Deploy using the provided configurations
|
802 |
+
|
803 |
+
## Support
|
804 |
+
For any issues or questions, please refer to the documentation or create an issue in the repository.
|
805 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
806 |
except Exception as e:
|
807 |
+
logger.error(f"Failed to execute flow: {str(e)}")
|
808 |
raise
|
809 |
+
finally:
|
810 |
+
if torch.cuda.is_available():
|
811 |
+
torch.cuda.empty_cache()
|
812 |
+
|
813 |
+
async def _execute_paths(self, paths: List[List[AgentRole]]):
|
814 |
+
"""Execute all paths in the flow graph."""
|
815 |
+
try:
|
816 |
+
for path in paths:
|
817 |
+
for role in path:
|
818 |
+
if self.contexts[role].state != FlowState.COMPLETED:
|
819 |
+
await self._execute_agent(role)
|
820 |
+
except Exception as e:
|
821 |
+
logger.error(f"Failed to execute paths: {str(e)}")
|
822 |
+
raise
|
823 |
+
|
824 |
async def _execute_agent(self, role: AgentRole):
|
825 |
"""Execute a single agent's tasks with enhanced context."""
|
|
|
|
|
|
|
826 |
try:
|
827 |
+
context = self.contexts[role]
|
828 |
+
if context.state == FlowState.COMPLETED:
|
829 |
+
return
|
830 |
|
831 |
+
context.state = FlowState.RUNNING
|
832 |
+
prompt = self._generate_prompt(role)
|
833 |
|
834 |
+
# Execute agent task
|
835 |
result = await self._execute_agent_task(role, prompt)
|
836 |
|
837 |
+
# Update context with results
|
838 |
+
context.artifacts.update(result)
|
839 |
context.state = FlowState.COMPLETED
|
840 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
841 |
except Exception as e:
|
842 |
context.state = FlowState.FAILED
|
843 |
+
logger.error(f"Failed to execute agent {role}: {str(e)}")
|
844 |
+
raise
|
845 |
+
|
846 |
+
async def _execute_agent_task(self, role: AgentRole, prompt: str) -> Dict[str, str]:
|
847 |
+
"""Execute a specific agent's task with the given prompt."""
|
848 |
+
try:
|
849 |
+
# Get the appropriate agent for the role
|
850 |
+
agent = get_agent(role)
|
851 |
+
|
852 |
+
# Execute the agent's task
|
853 |
+
result = await agent.arun(prompt)
|
854 |
+
|
855 |
+
# Process and return the result
|
856 |
+
return {"output": result}
|
857 |
+
|
858 |
+
except Exception as e:
|
859 |
+
logger.error(f"Failed to execute agent task for {role}: {str(e)}")
|
860 |
raise
|
861 |
|
862 |
# Update the multi_agent_workflow function to use AI Flows
|
|
|
869 |
str: Generated app code and API details.
|
870 |
"""
|
871 |
try:
|
872 |
+
# Initialize AI Flow
|
873 |
+
ai_flow = EnhancedAIFlow()
|
|
|
|
|
|
|
|
|
874 |
|
875 |
+
# Execute the flow with requirements
|
876 |
+
result = await ai_flow.execute_flow(requirements)
|
|
|
877 |
|
878 |
+
# Return the compiled results
|
879 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
880 |
except Exception as e:
|
881 |
+
logger.error(f"Failed to execute multi-agent workflow: {str(e)}")
|
882 |
+
raise
|
|
|
|
|
|
|
|
|
883 |
|
884 |
# Update the app_generator function to handle async execution
|
885 |
+
async def app_generator(requirements: str) -> str:
|
886 |
"""
|
887 |
Generate an app based on the provided requirements using AI Flows.
|
888 |
Args:
|
|
|
890 |
Returns:
|
891 |
str: Generated app code and API details.
|
892 |
"""
|
|
|
|
|
|
|
893 |
try:
|
894 |
+
# Execute the multi-agent workflow
|
895 |
+
result = await multi_agent_workflow(requirements)
|
896 |
+
return result
|
|
|
897 |
except Exception as e:
|
898 |
+
logger.error(f"Failed to generate app: {str(e)}")
|
899 |
+
raise
|
|
|
|
|
|
|
900 |
|
901 |
class StreamHandler:
|
902 |
"""Handles streaming output for the Gradio interface."""
|