File size: 3,413 Bytes
f2bc6e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#FallnAI Autonomous Software Developer
#app.py

import os
import json
import subprocess
import tempfile
import time
import re
from flask import Flask, request, jsonify
from crewai import Crew, Process
from crewai.agent import Agent
from crewai.task import Task
from crewai.tools import Tool
from textwrap import dedent
from crewai_tools import SerperDevTool

# Import agent and task classes
from agents import planner_agent, coder_agent, tester_agent, ui_designer_agent, docs_agent, devops_agent
from tasks import SoftwareDevelopmentTasks

# Import the GitHub Manager
from github_manager import GitHubManager

app = Flask(__name__)

# --- Configuration ---
# GitHub Token from environment variable
# IMPORTANT: Never hardcode this token.
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")
if not GITHUB_TOKEN:
    print("Warning: GITHUB_TOKEN environment variable not set. GitHub push will fail.")

# Initialize the GitHub tool
github_tool = Tool(
    name="GitHub_Manager",
    func=GitHubManager(GITHUB_TOKEN).create_and_push_repo,
    description="A tool to create and push files to a new GitHub repository."
)

@app.route('/develop', methods=['POST'])
def develop_software():
    data = request.json
    user_prompt = data.get('prompt')

    if not user_prompt:
        return jsonify({"error": "No prompt provided"}), 400

    # Sanitize and create a unique repository name
    sanitized_prompt = re.sub(r'[^a-zA-Z0-9-]', '', user_prompt[:30]).lower()
    timestamp = int(time.time())
    repo_name = f"auto-dev-{sanitized_prompt}-{timestamp}"

    try:
        # Create a new crew for this request
        tasks = SoftwareDevelopmentTasks(user_prompt)
        
        # Add the GitHub tool to the DevOps agent
        devops_agent.tools.append(github_tool)

        crew = Crew(
            agents=[planner_agent, coder_agent, tester_agent, ui_designer_agent, docs_agent, devops_agent],
            tasks=[
                tasks.plan_software(planner_agent),
                tasks.write_code(coder_agent),
                tasks.write_ui(ui_designer_agent),
                tasks.review_and_test(tester_agent),
                tasks.write_documentation(docs_agent), # New documentation task
                tasks.push_to_github(devops_agent, repo_name=repo_name) # Pass repo_name
            ],
            process=Process.sequential,
            verbose=2
        )
        
        # Kick off the development process
        result = crew.kickoff()

        # Extract the final outputs to be pushed
        final_code = ""
        final_ui = ""
        final_docs = ""
        for task_result in crew.tasks_outputs:
            if "code" in task_result.description.lower():
                final_code = task_result.output
            if "ui" in task_result.description.lower():
                final_ui = task_result.output
            if "documentation" in task_result.description.lower():
                final_docs = task_result.output

        # Final step is to get the push result and include it in the response
        final_result = crew.kickoff()

        return jsonify({
            "status": "success",
            "log": final_result,
            "final_code": final_code,
            "final_ui": final_ui,
            "final_docs": final_docs
        })

    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)