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

Code before:

            # If provider supports API key or other auth methods that are configured,
            # and this is a required field, then the block can work
            if "api_key" in supported_types:
                logger.debug(
                    f"Block {block_cls.__name__} has a required credentials field {field_name} that has a provider - Treating as valid"
                )
                configured_providers[field_name].append(True)

            if "user_password" in supported_types:
                logger.debug(
                    f"Block {block_cls.__name__} has a required credentials field {field_name} that has a provider - Treating as valid"
                )
                configured_providers[field_name].append(True)

            if "host_scoped" in supported_types:
                logger.debug(
                    f"Block {block_cls.__name__} has a required credentials field {field_name} that has a provider - Treating as valid"
                )
                configured_providers[field_name].append(True)

    is_provider_configured = {
        field_name: any(configured_providers[field_name])
        for field_name in required_credentials
    }
    return all(is_provider_configured.values())


async def initialize_blocks() -> None:
    # First, sync all provider costs to blocks
    # Imported here to avoid circular import
    from backend.sdk.cost_integration import sync_all_provider_costs

    sync_all_provider_costs()

    for cls in get_blocks().values():
        block = cls()
        existing_block = await AgentBlock.prisma().find_first(
            where={"OR": [{"id": block.id}, {"name": block.name}]}
        )
        if not existing_block:
            await AgentBlock.prisma().create(
                data=AgentBlockCreateInput(
                    id=block.id,
                    name=block.name,
                    inputSchema=json.dumps(block.input_schema.jsonschema()),
                    outputSchema=json.dumps(block.output_schema.jsonschema()),
                )
            )
            continue


Review comment (bug):
nit: can be simplified quite a bit with early exit:
```suggestion
    # Check if the credential inputs for this block are correctly configured
    for field_name in required_credentials:
        provider_names = block.input_schema.get_provider_from_credentials_field(
            field_name
        )
        if not provider_names:
            logger.warning(
                f"Block {block_cls.__name__} "
                f"has required credentials field {field_name} with no providers"
            )
            return False

        # If a field has multiple possible providers, each one needs to be usable to
        # prevent breaking the UX
        for provider_name in provider_names:
            if provider_name in ProviderName.__members__.values():
                logger.debug(
                    f"Block {block_cls.__name__} input {field_name} provider {provider_name} is part of the legacy provider system - Treating as valid"
                )
                break

            provider = AutoRegistry.get_provider(provider_name)
            if not provider:
                logger.warning(
                    f"Block {block_cls.__name__} has a required credentials field {field_name} with no provider options - Disabling"
                )
                return False

            # Check the provider's supported auth types
            supported_types = provider.supported_auth_types

            # If provider has no supported auth types, it means no auth method is configured
            if not supported_types:
                logger.warning(
                    f"Block {block_cls.__name__} has a required credentials field {field_name} but provider {provider_name} has no authentication methods configured - Disabling"
                )
                return False

            # Check if provider supports OAuth
            if "oauth2" in supported_types:
                # Check if OAuth environment variables are set
                if provider.oauth_config:
                    oauth_configured = bool(
                        os.getenv(provider.oauth_config.client_id_env_var)
                        and os.getenv(provider.oauth_config.client_secret_env_var)
                    )
                    if not oauth_configured:
                        logger.warning(
                            f"Block {block_cls.__name__} has a required credentials field {field_name} but provider {provider_name} is missing OAuth credentials (client ID or client secret) - Disabling"
                        )
                        return False

        logger.debug(
            f"Block {block_cls.__name__} credentials field {field_name} is valid; "
            f"supported credential types: {supported_types}"
        )

    return True
```

Code after:
    return True


async def initialize_blocks() -> None:
    # First, sync all provider costs to blocks
    # Imported here to avoid circular import
    from backend.sdk.cost_integration import sync_all_provider_costs

    sync_all_provider_costs()

    for cls in get_blocks().values():
        block = cls()
        existing_block = await AgentBlock.prisma().find_first(
            where={"OR": [{"id": block.id}, {"name": block.name}]}
        )
        if not existing_block:
            await AgentBlock.prisma().create(
                data=AgentBlockCreateInput(
                    id=block.id,
                    name=block.name,
                    inputSchema=json.dumps(block.input_schema.jsonschema()),
                    outputSchema=json.dumps(block.output_schema.jsonschema()),
                )
            )
            continue

        input_schema = json.dumps(block.input_schema.jsonschema())
        output_schema = json.dumps(block.output_schema.jsonschema())
        if (
            block.id != existing_block.id
            or block.name != existing_block.name
            or input_schema != existing_block.inputSchema
            or output_schema != existing_block.outputSchema
        ):
            await AgentBlock.prisma().update(
                where={"id": existing_block.id},
                data={
                    "id": block.id,
                    "name": block.name,
                    "inputSchema": input_schema,
                    "outputSchema": output_schema,
                },
            )


# Note on the return type annotation: https://github.com/microsoft/pyright/issues/10281
def get_block(block_id: str) -> Block[BlockSchema, BlockSchema] | None:
    cls = get_blocks().get(block_id)
    return cls() if cls else None