2008robocode-crypto/code-generation-system
0
1"""2Schema definitions for the code generation system.3Defines strict contracts for all intermediate and final outputs.4"""5 6from typing import Any, Dict, List, Optional7from dataclasses import dataclass8from enum import Enum9 10 11class FieldType(str, Enum):12 """Supported field types in schemas."""13 STRING = "string"14 NUMBER = "number"15 BOOLEAN = "boolean"16 ARRAY = "array"17 OBJECT = "object"18 DATE = "date"19 EMAIL = "email"20 ENUM = "enum"21 22 23@dataclass24class Field:25 """Represents a field in any schema."""26 name: str27 type: FieldType28 required: bool = True29 description: str = ""30 validation_rules: Optional[Dict[str, Any]] = None31 enum_values: Optional[List[str]] = None32 33 34@dataclass35class DBTable:36 """Database table schema."""37 name: str38 fields: List[Field]39 primary_key: str40 relations: Optional[Dict[str, str]] = None # field_name -> other_table41 indexes: Optional[List[str]] = None42 43 44@dataclass45class APIEndpoint:46 """API endpoint schema."""47 path: str48 method: str # GET, POST, PUT, DELETE49 description: str50 request_body: Optional[Dict[str, Field]] = None51 response_body: Optional[Dict[str, Field]] = None52 required_role: Optional[str] = None53 validation_rules: Optional[List[str]] = None54 55 56@dataclass57class UIComponent:58 """UI component schema."""59 name: str60 type: str # button, input, form, table, etc.61 fields: List[Field]62 visibility_rules: Optional[List[str]] = None63 validation_rules: Optional[List[str]] = None64 65 66@dataclass67class UIPage:68 """UI page schema."""69 path: str70 title: str71 components: List[UIComponent]72 required_role: Optional[str] = None73 data_source: Optional[str] = None # API endpoint74 75 76@dataclass77class Role:78 """Authorization role."""79 name: str80 permissions: List[str] # e.g., ["read_contacts", "create_contact"]81 description: str82 83 84@dataclass85class AppIntentExtraction:86 """Output of Intent Extraction stage."""87 app_name: str88 app_description: str89 key_features: List[str]90 user_roles: List[str]91 core_entities: List[str]92 business_requirements: List[str]93 constraints: List[str]94 95 96@dataclass97class SystemDesign:98 """Output of System Design stage."""99 entities: Dict[str, List[str]] # entity_name -> attributes100 user_flows: List[Dict[str, Any]] # flow descriptions101 roles_and_permissions: Dict[str, List[str]] # role -> permissions102 data_models: List[str]103 api_patterns: List[str]104 ui_structure: List[str]105 106 107@dataclass108class GeneratedConfig:109 """Final generated configuration."""110 app_name: str111 app_description: str112 database_schema: List[DBTable]113 api_schema: List[APIEndpoint]114 ui_schema: List[UIPage]115 auth_config: Dict[str, Any]116 roles: List[Role]117 business_logic: Dict[str, Any]118 validation_metadata: Dict[str, Any]119 120 121class ValidationResult:122 """Represents validation result with detailed errors."""123 124 def __init__(self, is_valid: bool = True, errors: Optional[List[str]] = None,125 warnings: Optional[List[str]] = None, repairs_applied: Optional[List[str]] = None):126 self.is_valid = is_valid127 self.errors = errors or []128 self.warnings = warnings or []129 self.repairs_applied = repairs_applied or []130 131 def add_error(self, error: str):132 self.errors.append(error)133 self.is_valid = False134 135 def add_warning(self, warning: str):136 self.warnings.append(warning)137 138 def add_repair(self, repair: str):139 self.repairs_applied.append(repair)140 141 def to_dict(self):142 return {143 "is_valid": self.is_valid,144 "errors": self.errors,145 "warnings": self.warnings,146 "repairs_applied": self.repairs_applied147 }148 