File size: 3,740 Bytes
e5e882e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
from core.base_agent import BaseAgent
from core.database import db
from typing import Dict, Any

class DeveloperAgent(BaseAgent):
    def __init__(self):
        super().__init__("Developer")
        self.create_chain("""

        You are an expert Software Developer. Your task is to generate production-ready, well-structured code based on the given design specifications.

        

        Design Specifications:

        {input}

        

        Generate code that follows these strict guidelines:

        

        1. Code Structure:

           - Use proper project structure with separate modules

           - Follow SOLID principles

           - Implement proper error handling

           - Use type hints

           - Include comprehensive docstrings

           - Follow PEP 8 style guide

        

        2. Implementation Details:

           - Use modern Python features (Python 3.9+)

           - Implement proper logging

           - Add input validation

           - Include unit tests

           - Use dependency injection where appropriate

           - Implement proper configuration management

        

        3. Security:

           - Sanitize all inputs

           - Implement proper authentication/authorization

           - Use secure coding practices

           - Handle sensitive data properly

        

        4. Performance:

           - Optimize database queries

           - Implement caching where appropriate

           - Use async/await for I/O operations

           - Implement proper resource management

        

        5. Documentation:

           - Include detailed module docstrings

           - Document all public methods

           - Include usage examples

           - Document configuration options

        

        The code should be:

        - Production-ready

        - Well-tested

        - Scalable

        - Maintainable

        - Secure

        - Performant

        

        Provide the complete implementation with all necessary files and dependencies.

        """)
        
    async def generate_code(self, design: str) -> Dict[str, Any]:
        """Generate code from design specifications"""
        result = await self.process({"input": design})
        
        # Store the code in the database
        db.store_artifact(
            "code",
            result,
            {
                "type": "code",
                "source": "developer",
                "status": "created",
                "version": "1.0.0"
            }
        )
        
        return {
            "status": "success",
            "code": result,
            "message": "Code generated successfully",
            "metadata": {
                "language": "Python",
                "framework": "Standard Library",
                "dependencies": self._extract_dependencies(result)
            }
        }
    
    def _extract_dependencies(self, code: str) -> list:
        """Extract dependencies from the generated code"""
        dependencies = set()
        # Add common dependencies
        dependencies.update([
            "python-dotenv",
            "pydantic",
            "fastapi",
            "uvicorn",
            "pytest",
            "pytest-asyncio"
        ])
        
        # Add dependencies based on code content
        if "import sqlalchemy" in code:
            dependencies.add("sqlalchemy")
        if "import aiohttp" in code:
            dependencies.add("aiohttp")
        if "import redis" in code:
            dependencies.add("redis")
        if "import jwt" in code:
            dependencies.add("PyJWT")
            
        return sorted(list(dependencies))