Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/data/block.py (Python)

Code before:
            if event_filter[k] is True
        ]


# ======================= Block Helper Functions ======================= #


def get_blocks() -> dict[str, Type[Block]]:
    from backend.blocks import load_all_blocks

    return load_all_blocks()


def is_block_provider_configured(block_cls: type["Block"]) -> bool:
    """
    Check if a block has a valid authentication method configured at runtime.

    For example if a block is an OAuth-only block and there env vars are not set,
    do not show it in the UI.

    """
    from backend.sdk.registry import AutoRegistry

    # Create an instance to access input_schema
    try:
        block = block_cls.create()
    except Exception as e:
        # If we can't create a block instance, assume it's not OAuth-only
        logger.error(f"Error creating block instance for {block_cls.__name__}: {e}")
        return True
    logger.debug(
        f"Checking if block {block_cls.__name__} has a valid provider configured"
    )

    # Get all credentials fields from input schema
    credentials_fields = block.input_schema.get_credentials_fields()
    if not credentials_fields:
        logger.debug(
            f"Block {block_cls.__name__} has no credentials fields - Treating as valid"
        )
        return True

    # Check required credentials fields
    required_fields: dict[str, CredentialsMetaInput] = (
        block.input_schema.get_required_fields()
    )
    required_credentials = [f for f in credentials_fields if f in required_fields]

    if len(required_credentials) == 0:
        logger.warn(
            f"Block {block_cls.__name__} has no required credentials fields - therefore will worth without credentials configured"

Review comment (suggestion):
`create()` is just a wrapper for `__init__`, but with a less well-defined type, causing type checking issues (`block.input_schema` currently resolves to `Unknown` in the code below)
```suggestion
        block = block_cls()
```
I have checked that `create()` isn't overridden by any block implementations

Code after:
    block_cls: type["Block[BlockSchema, BlockSchema]"],
) -> bool:
    """
    Check if a block has a valid authentication method configured at runtime.

    For example if a block is an OAuth-only block and there env vars are not set,
    do not show it in the UI.

    """
    from backend.sdk.registry import AutoRegistry

    # Create an instance to access input_schema
    try:
        block = block_cls()
    except Exception as e:
        # If we can't create a block instance, assume it's not OAuth-only
        logger.error(f"Error creating block instance for {block_cls.__name__}: {e}")
        return True
    logger.debug(
        f"Checking if block {block_cls.__name__} has a valid provider configured"
    )

    # Get all credential inputs from input schema
    credential_inputs = block.input_schema.get_credentials_fields_info()
    required_inputs = block.input_schema.get_required_fields()
    if not credential_inputs:
        logger.debug(
            f"Block {block_cls.__name__} has no credential inputs - Treating as valid"
        )
        return True

    # Check credential inputs
    if len(required_inputs.intersection(credential_inputs.keys())) == 0:
        logger.debug(
            f"Block {block_cls.__name__} has only optional credential inputs"
            " - will work without credentials configured"
        )
    if len(credential_inputs) > 1:
        logger.warning(
            f"Block {block_cls.__name__} has multiple credential inputs: "
            f"{', '.join(credential_inputs.keys())}"
        )

    # Check if the credential inputs for this block are correctly configured
    for field_name, field_info in credential_inputs.items():
        provider_names = field_info.provider
        if not provider_names:
            logger.warning(
                f"Block {block_cls.__name__} "
                f"has credential input '{field_name}' with no provider options"
                " - Disabling"