async def _sync_entity_definitions(db: AsyncSession) -> Dict[str, dict]:
    """Sync entity definitions with the database based on chunk classes.

    Args:
        db (AsyncSession): Database session

    Returns:
        Dict[str, dict]: Mapping of module names to their entity details:
            - entity_ids: list[str] - UUIDs of entity definitions for this module
            - entity_classes: list[str] - Full class names of the entities in this module
    """
    sync_logger.info("Syncing entity definitions to database.")

    # Get all Python files in the entities directory that aren't base or init files
    entity_files = [
        f
        for f in os.listdir("airweave/platform/entities")
        if f.endswith(".py") and not f.startswith("__")
    ]

    from airweave.platform.entities._base import BaseEntity

    entity_definitions = []
    entity_registry = {}  # Track all entities system-wide
    module_registry = {}  # Track entities by module

    for entity_file in entity_files:
        module_name = entity_file[:-3]  # Remove .py extension
        # Initialize module entry if not exists
        if module_name not in module_registry:
            module_registry[module_name] = {
                "entity_classes": [],
                "entity_names": [],
            }

        # Import the module to get its chunk classes
        full_module_name = f"airweave.platform.entities.{module_name}"
        module = importlib.import_module(full_module_name)

        # Find all chunk classes (subclasses of BaseEntity) in the module
        for name, cls in inspect.getmembers(module, inspect.isclass):
            # Check if it's a subclass of BaseEntity (or any class from _base.py)
            # AND the class is actually defined in this module (not imported)
            if (
                issubclass(cls, BaseEntity)
                and cls.__module__
                != "airweave.platform.entities._base"  # Exclude all classes from _base.py
                and cls.__module__ == full_module_name
            ):
                if name in entity_registry:
                    raise ValueError(
                        f"Duplicate entity name '{name}' found in {full_module_name}. "
                        f"Already registered from {entity_registry[name]['module']}"
                    )

                # Validate that all fields in the class use Pydantic Field with descriptions
                _validate_entity_class_fields(cls, name, module_name)

                # Register the entity
                entity_registry[name] = {
                    "class_name": f"{cls.__module__}.{cls.__name__}",
                    "module": module_name,
                }

                # Add to module registry
                module_registry[module_name]["entity_classes"].append(
                    f"{cls.__module__}.{cls.__name__}"
                )
                module_registry[module_name]["entity_names"].append(name)

                # Create entity definition
                entity_def = schemas.EntityDefinitionCreate(
                    name=name,
                    description=cls.__doc__ or f"Data from {name}",
                    type=EntityType.JSON,
                    entity_schema=cls.model_json_schema(),  # Get the actual schema
                    module_name=module_name,
                    class_name=cls.__name__,
                )
                entity_definitions.append(entity_def)

    # Sync entities
    await crud.entity_definition.sync(db, entity_definitions, unique_field="name")

    # Get all entities to build the mapping
    all_entities = await crud.entity_definition.get_all(db)

    # Create a mapping of entity names to their IDs
    entity_id_map = {e.name: str(e.id) for e in all_entities}

    # Add entity IDs to the module registry
    for module_name, module_info in module_registry.items():
        entity_ids = [
            entity_id_map[name] for name in module_info["entity_names"] if name in entity_id_map
        ]
        module_registry[module_name]["entity_ids"] = entity_ids

    sync_logger.info(f"Synced {len(entity_definitions)} entity definitions to database.")
    return module_registry
Sync entity definitions with the database based on chunk classes.

    Args:
        db (AsyncSession): Database session

    Returns:
        Dict[str, dict]: Mapping of module names to their entity details:
            - entity_ids: list[str] - UUIDs of entity definitions for this module
            - entity_classes: list[str] - Full class names of the entities in this module
    
backend/airweave/platform/db_sync.py