File size: 3,815 Bytes
d0fdbcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
Schema definitions for the code generation system.
Defines strict contracts for all intermediate and final outputs.
"""

from typing import Any, Dict, List, Optional
from dataclasses import dataclass
from enum import Enum


class FieldType(str, Enum):
    """Supported field types in schemas."""
    STRING = "string"
    NUMBER = "number"
    BOOLEAN = "boolean"
    ARRAY = "array"
    OBJECT = "object"
    DATE = "date"
    EMAIL = "email"
    ENUM = "enum"


@dataclass
class Field:
    """Represents a field in any schema."""
    name: str
    type: FieldType
    required: bool = True
    description: str = ""
    validation_rules: Optional[Dict[str, Any]] = None
    enum_values: Optional[List[str]] = None


@dataclass
class DBTable:
    """Database table schema."""
    name: str
    fields: List[Field]
    primary_key: str
    relations: Optional[Dict[str, str]] = None  # field_name -> other_table
    indexes: Optional[List[str]] = None


@dataclass
class APIEndpoint:
    """API endpoint schema."""
    path: str
    method: str  # GET, POST, PUT, DELETE
    description: str
    request_body: Optional[Dict[str, Field]] = None
    response_body: Optional[Dict[str, Field]] = None
    required_role: Optional[str] = None
    validation_rules: Optional[List[str]] = None


@dataclass
class UIComponent:
    """UI component schema."""
    name: str
    type: str  # button, input, form, table, etc.
    fields: List[Field]
    visibility_rules: Optional[List[str]] = None
    validation_rules: Optional[List[str]] = None


@dataclass
class UIPage:
    """UI page schema."""
    path: str
    title: str
    components: List[UIComponent]
    required_role: Optional[str] = None
    data_source: Optional[str] = None  # API endpoint


@dataclass
class Role:
    """Authorization role."""
    name: str
    permissions: List[str]  # e.g., ["read_contacts", "create_contact"]
    description: str


@dataclass
class AppIntentExtraction:
    """Output of Intent Extraction stage."""
    app_name: str
    app_description: str
    key_features: List[str]
    user_roles: List[str]
    core_entities: List[str]
    business_requirements: List[str]
    constraints: List[str]


@dataclass
class SystemDesign:
    """Output of System Design stage."""
    entities: Dict[str, List[str]]  # entity_name -> attributes
    user_flows: List[Dict[str, Any]]  # flow descriptions
    roles_and_permissions: Dict[str, List[str]]  # role -> permissions
    data_models: List[str]
    api_patterns: List[str]
    ui_structure: List[str]


@dataclass
class GeneratedConfig:
    """Final generated configuration."""
    app_name: str
    app_description: str
    database_schema: List[DBTable]
    api_schema: List[APIEndpoint]
    ui_schema: List[UIPage]
    auth_config: Dict[str, Any]
    roles: List[Role]
    business_logic: Dict[str, Any]
    validation_metadata: Dict[str, Any]


class ValidationResult:
    """Represents validation result with detailed errors."""
    
    def __init__(self, is_valid: bool = True, errors: Optional[List[str]] = None,
                 warnings: Optional[List[str]] = None, repairs_applied: Optional[List[str]] = None):
        self.is_valid = is_valid
        self.errors = errors or []
        self.warnings = warnings or []
        self.repairs_applied = repairs_applied or []
    
    def add_error(self, error: str):
        self.errors.append(error)
        self.is_valid = False
    
    def add_warning(self, warning: str):
        self.warnings.append(warning)
    
    def add_repair(self, repair: str):
        self.repairs_applied.append(repair)
    
    def to_dict(self):
        return {
            "is_valid": self.is_valid,
            "errors": self.errors,
            "warnings": self.warnings,
            "repairs_applied": self.repairs_applied
        }