from pydantic import BaseModel, Field from typing import List, Dict, Any, Optional # Define the model for attribute definitions class AttributeDefinition(BaseModel): name: str label: str type: str required: bool = False placeholder: Optional[str] = None defaultValue: Optional[Any] = None options: Optional[List[Dict[str, Any]]] = None editable: bool = True visible: bool = True order: int = 0 validation: Optional[Dict[str, Any]] = None helpText: Optional[str] = None # Helper classes for type mapping typeMappings = { "int": "number", "str": "string", "float": "number", "bool": "boolean", "List[int]": "array", "List[str]": "array", "Dict[str, Any]": "object", "Optional[str]": "string", "Optional[int]": "number", "Optional[Dict[str, Any]]": "object" } # Special field types based on naming conventions specialFieldTypes = { "content": "textarea", "description": "textarea", "instructions": "textarea", "password": "password", "email": "email", "workspaceId": "select", "agentId": "select", "type": "select" } # Function to convert a Pydantic model into attribute definitions def getModelAttributes(modelClass, userLanguage="de"): """ Converts a Pydantic model into a list of AttributeDefinition objects """ attributes = [] # Go through all fields in the model for i, (fieldName, field) in enumerate(modelClass.__fields__.items()): # Skip internal fields if fieldName.startswith('_') or fieldName in ["label", "fieldLabels"]: continue # Determine the field type fieldType = typeMappings.get(str(field.type_), "string") # Check for special field types if fieldName in specialFieldTypes: fieldType = specialFieldTypes[fieldName] # Get the label (if available) fieldLabel = fieldName.replace('_', ' ').capitalize() if hasattr(modelClass, 'fieldLabels') and fieldName in modelClass.fieldLabels: labelObj = modelClass.fieldLabels[fieldName] fieldLabel = labelObj.getLabel(userLanguage) # Determine default values and required status required = field.required defaultValue = field.default if not field.required else None # Check for validation rules validation = None if field.validators: validation = {"hasValidators": True} # Placeholder text placeholder = f"Please enter {fieldLabel}" # Special options for Select fields options = None if fieldType == "select": if fieldName == "type" and modelClass.__name__ == "Agent": options = [ {"value": "Analysis", "label": "Analysis"}, {"value": "Transformation", "label": "Transformation"}, {"value": "Generation", "label": "Generation"}, {"value": "Classification", "label": "Classification"}, {"value": "Custom", "label": "Custom"} ] # Extract description from Field object description = None # Try to get description from various possible sources if hasattr(field, 'field_info') and hasattr(field.field_info, 'description'): description = field.field_info.description elif hasattr(field, 'description'): description = field.description elif hasattr(field, 'schema') and hasattr(field.schema, 'description'): description = field.schema.description # Create attribute definition attrDef = AttributeDefinition( name=fieldName, label=fieldLabel, type=fieldType, required=required, placeholder=placeholder, defaultValue=defaultValue, options=options, editable=fieldName not in ["id", "_mandateId", "_userId", "uploadDate", "_createdAt", "_modifiedAt"], visible=fieldName not in ["hashedPassword", "_mandateId", "_userId"], order=i, validation=validation, helpText=description or "" # Set empty string as default value if no description found ) attributes.append(attrDef) return attributes