Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend): Ensure we only present working auth options on blocks (#10454)

Swifty83f96b75c712
diff --git a/autogpt_platform/backend/backend/blocks/__init__.py b/autogpt_platform/backend/backend/blocks/__init__.py
index 4715d44611..f6299cbb53 100644
--- a/autogpt_platform/backend/backend/blocks/__init__.py
+++ b/autogpt_platform/backend/backend/blocks/__init__.py
@@ -1,10 +1,14 @@
import functools
import importlib
+import logging
import os
import re
from pathlib import Path
from typing import TYPE_CHECKING, TypeVar
+logger = logging.getLogger(__name__)
+
+
if TYPE_CHECKING:
from backend.data.block import Block
@@ -99,7 +103,15 @@ def load_all_blocks() -> dict[str, type["Block"]]:
available_blocks[block.id] = block_cls
- return available_blocks
+ # Filter out blocks with incomplete auth configs, e.g. missing OAuth server secrets
+ from backend.data.block import is_block_auth_configured
+
+ filtered_blocks = {}
+ for block_id, block_cls in available_blocks.items():
+ if is_block_auth_configured(block_cls):
+ filtered_blocks[block_id] = block_cls
+
+ return filtered_blocks
__all__ = ["load_all_blocks"]
diff --git a/autogpt_platform/backend/backend/blocks/airtable/_config.py b/autogpt_platform/backend/backend/blocks/airtable/_config.py
index d804d84360..ee168881d8 100644
--- a/autogpt_platform/backend/backend/blocks/airtable/_config.py
+++ b/autogpt_platform/backend/backend/blocks/airtable/_config.py
@@ -2,30 +2,18 @@
Shared configuration for all Airtable blocks using the SDK pattern.
"""
-import os
-
from backend.sdk import BlockCostType, ProviderBuilder
from ._oauth import AirtableOAuthHandler, AirtableScope
from ._webhook import AirtableWebhookManager
# Configure the Airtable provider with API key authentication
-builder = (
+airtable = (
ProviderBuilder("airtable")
.with_api_key("AIRTABLE_API_KEY", "Airtable Personal Access Token")
.with_webhook_manager(AirtableWebhookManager)
.with_base_cost(1, BlockCostType.RUN)
-)
-
-
-# Check if Linear OAuth is configured
-client_id = os.getenv("AIRTABLE_CLIENT_ID")
-client_secret = os.getenv("AIRTABLE_CLIENT_SECRET")
-AIRTABLE_OAUTH_IS_CONFIGURED = bool(client_id and client_secret)
-
-# Linear only supports OAuth authentication
-if AIRTABLE_OAUTH_IS_CONFIGURED:
- builder = builder.with_oauth(
+ .with_oauth(
AirtableOAuthHandler,
scopes=[
v.value
@@ -40,6 +28,5 @@ if AIRTABLE_OAUTH_IS_CONFIGURED:
client_id_env_var="AIRTABLE_CLIENT_ID",
client_secret_env_var="AIRTABLE_CLIENT_SECRET",
)
-
-# Build the provider
-airtable = builder.build()
+ .build()
+)
diff --git a/autogpt_platform/backend/backend/blocks/linear/_config.py b/autogpt_platform/backend/backend/blocks/linear/_config.py
index fdaa94dc1f..c5337c481c 100644
--- a/autogpt_platform/backend/backend/blocks/linear/_config.py
+++ b/autogpt_platform/backend/backend/blocks/linear/_config.py
@@ -2,7 +2,6 @@
Shared configuration for all Linear blocks using the new SDK pattern.
"""
-import os
from enum import Enum
from backend.sdk import (
@@ -38,21 +37,11 @@ class LinearScope(str, Enum):
ADMIN = "admin"
-# Check if Linear OAuth is configured
-client_id = os.getenv("LINEAR_CLIENT_ID")
-client_secret = os.getenv("LINEAR_CLIENT_SECRET")
-LINEAR_OAUTH_IS_CONFIGURED = bool(client_id and client_secret)
-
-# Build the Linear provider
-builder = (
+linear = (
ProviderBuilder("linear")
.with_api_key(env_var_name="LINEAR_API_KEY", title="Linear API Key")
.with_base_cost(1, BlockCostType.RUN)
-)
-
-# Linear only supports OAuth authentication
-if LINEAR_OAUTH_IS_CONFIGURED:
- builder = builder.with_oauth(
+ .with_oauth(
LinearOAuthHandler,
scopes=[
LinearScope.READ,
@@ -63,9 +52,8 @@ if LINEAR_OAUTH_IS_CONFIGURED:
client_id_env_var="LINEAR_CLIENT_ID",
client_secret_env_var="LINEAR_CLIENT_SECRET",
)
-
-# Build the provider
-linear = builder.build()
+ .build()
+)
TEST_CREDENTIALS_OAUTH = OAuth2Credentials(
diff --git a/autogpt_platform/backend/backend/blocks/linear/comment.py b/autogpt_platform/backend/backend/blocks/linear/comment.py
index 3af28ac35c..17cd54c212 100644
--- a/autogpt_platform/backend/backend/blocks/linear/comment.py
+++ b/autogpt_platform/backend/backend/blocks/linear/comment.py
@@ -11,7 +11,6 @@ from backend.sdk import (
from ._api import LinearAPIException, LinearClient
from ._config import (
- LINEAR_OAUTH_IS_CONFIGURED,
TEST_CREDENTIALS_INPUT_OAUTH,
TEST_CREDENTIALS_OAUTH,
LinearScope,
@@ -50,7 +49,6 @@ class LinearCreateCommentBlock(Block):
"comment": "Test comment",
"credentials": TEST_CREDENTIALS_INPUT_OAUTH,
},
- disabled=not LINEAR_OAUTH_IS_CONFIGURED,
test_credentials=TEST_CREDENTIALS_OAUTH,
test_output=[("comment_id", "abc123"), ("comment_body", "Test comment")],
test_mock={
diff --git a/autogpt_platform/backend/backend/blocks/linear/issues.py b/autogpt_platform/backend/backend/blocks/linear/issues.py
index 35452f4a81..cd0fa0e98a 100644
--- a/autogpt_platform/backend/backend/blocks/linear/issues.py
+++ b/autogpt_platform/backend/backend/blocks/linear/issues.py
@@ -11,7 +11,6 @@ from backend.sdk import (
from ._api import LinearAPIException, LinearClient
from ._config import (
- LINEAR_OAUTH_IS_CONFIGURED,
TEST_CREDENTIALS_INPUT_OAUTH,
TEST_CREDENTIALS_OAUTH,
LinearScope,
@@ -53,7 +52,6 @@ class LinearCreateIssueBlock(Block):
super().__init__(
id="f9c68f55-dcca-40a8-8771-abf9601680aa",
description="Creates a new issue on Linear",
- disabled=not LINEAR_OAUTH_IS_CONFIGURED,
input_schema=self.Input,
output_schema=self.Output,
categories={BlockCategory.PRODUCTIVITY, BlockCategory.ISSUE_TRACKING},
@@ -147,7 +145,6 @@ class LinearSearchIssuesBlock(Block):
description="Searches for issues on Linear",
input_schema=self.Input,
output_schema=self.Output,
- disabled=not LINEAR_OAUTH_IS_CONFIGURED,
test_input={
"term": "Test issue",
"credentials": TEST_CREDENTIALS_INPUT_OAUTH,
diff --git a/autogpt_platform/backend/backend/blocks/linear/projects.py b/autogpt_platform/backend/backend/blocks/linear/projects.py
index 57c7950300..4eeb1ed99d 100644
--- a/autogpt_platform/backend/backend/blocks/linear/projects.py
+++ b/autogpt_platform/backend/backend/blocks/linear/projects.py
@@ -11,7 +11,6 @@ from backend.sdk import (
from ._api import LinearAPIException, LinearClient
from ._config import (
- LINEAR_OAUTH_IS_CONFIGURED,
TEST_CREDENTIALS_INPUT_OAUTH,
TEST_CREDENTIALS_OAUTH,
LinearScope,
@@ -45,7 +44,6 @@ class LinearSearchProjectsBlock(Block):
"term": "Test project",
"credentials": TEST_CREDENTIALS_INPUT_OAUTH,
},
- disabled=not LINEAR_OAUTH_IS_CONFIGURED,
test_credentials=TEST_CREDENTIALS_OAUTH,
test_output=[
(
diff --git a/autogpt_platform/backend/backend/blocks/wordpress/_config.py b/autogpt_platform/backend/backend/blocks/wordpress/_config.py
index a8a6bac092..64cd8bdb75 100644
--- a/autogpt_platform/backend/backend/blocks/wordpress/_config.py
+++ b/autogpt_platform/backend/backend/blocks/wordpress/_config.py
@@ -1,18 +1,11 @@
-import os
-
from backend.sdk import BlockCostType, ProviderBuilder
from ._oauth import WordPressOAuthHandler, WordPressScope
-builder = ProviderBuilder("wordpress").with_base_cost(1, BlockCostType.RUN)
-
-
-client_id = os.getenv("WORDPRESS_CLIENT_ID")
-client_secret = os.getenv("WORDPRESS_CLIENT_SECRET")
-WORDPRESS_OAUTH_IS_CONFIGURED = bool(client_id and client_secret)
-
-if WORDPRESS_OAUTH_IS_CONFIGURED:
- builder = builder.with_oauth(
+wordpress = (
+ ProviderBuilder("wordpress")
+ .with_base_cost(1, BlockCostType.RUN)
+ .with_oauth(
WordPressOAuthHandler,
scopes=[
v.value
@@ -23,5 +16,5 @@ if WORDPRESS_OAUTH_IS_CONFIGURED:
client_id_env_var="WORDPRESS_CLIENT_ID",
client_secret_env_var="WORDPRESS_CLIENT_SECRET",
)
-
-wordpress = builder.build()
+ .build()
+)
diff --git a/autogpt_platform/backend/backend/data/block.py b/autogpt_platform/backend/backend/data/block.py
index 2d77cb04eb..0b47ae9362 100644
--- a/autogpt_platform/backend/backend/data/block.py
+++ b/autogpt_platform/backend/backend/data/block.py
@@ -1,5 +1,7 @@
import functools
import inspect
+import logging
+import os
from abc import ABC, abstractmethod
from collections.abc import AsyncGenerator as AsyncGen
from enum import Enum
@@ -35,6 +37,8 @@ from .model import (
is_credentials_field_name,
)
+logger = logging.getLogger(__name__)
+
if TYPE_CHECKING:
from .graph import Link
@@ -492,6 +496,125 @@ def get_blocks() -> dict[str, Type[Block]]:
return load_all_blocks()
+def is_block_auth_configured(
+ 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"
+ )
+ 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:
+ provider_name = _provider_name.value
+ if provider_name in ProviderName.__members__.values():
+ logger.debug(
+ f"Block {block_cls.__name__} credential input '{field_name}' "
+ f"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__} credential input '{field_name}' "
+ f"refers to unknown provider '{provider_name}' - Disabling"
+ )
+ return False
+
+ # Check the provider's supported auth types
+ if field_info.supported_types != provider.supported_auth_types:
+ logger.warning(
+ f"Block {block_cls.__name__} credential input '{field_name}' "
+ f"has mismatched supported auth types (field <> Provider): "
+ f"{field_info.supported_types} != {provider.supported_auth_types}"
+ )
+
+ if not (supported_auth_types := provider.supported_auth_types):
+ # No auth methods are been configured for this provider
+ logger.warning(
+ f"Block {block_cls.__name__} credential input '{field_name}' "
+ f"provider '{provider_name}' "
+ "has no authentication methods configured - Disabling"
+ )
+ return False
+
+ # Check if provider supports OAuth
+ if "oauth2" in supported_auth_types:
+ # Check if OAuth environment variables are set
+ if (oauth_config := provider.oauth_config) and bool(
+ os.getenv(oauth_config.client_id_env_var)
+ and os.getenv(oauth_config.client_secret_env_var)
+ ):
+ logger.debug(
+ f"Block {block_cls.__name__} credential input '{field_name}' "
+ f"provider '{provider_name}' is configured for OAuth"
+ )
+ else:
+ logger.error(
+ f"Block {block_cls.__name__} credential input '{field_name}' "
+ f"provider '{provider_name}' "
+ "is missing OAuth client ID or secret - Disabling"
+ )
+ return False
+
+ logger.debug(
+ f"Block {block_cls.__name__} credential input '{field_name}' is valid; "
+ f"supported credential types: {', '.join(field_info.supported_types)}"
+ )
+
+ return True
+
+
async def initialize_blocks() -> None:
# First, sync all provider costs to blocks
# Imported here to avoid circular import
diff --git a/autogpt_platform/backend/backend/integrations/creds_manager.py b/autogpt_platform/backend/backend/integrations/creds_manager.py
index d782a31dd2..e5ae0cdb72 100644
--- a/autogpt_platform/backend/backend/integrations/creds_manager.py
+++ b/autogpt_platform/backend/backend/integrations/creds_manager.py
@@ -1,4 +1,5 @@
import logging
+import os
from contextlib import asynccontextmanager
from datetime import datetime
from typing import TYPE_CHECKING, Any, Callable, Coroutine
@@ -9,7 +10,7 @@ from redis.asyncio.lock import Lock as AsyncRedisLock
from backend.data.model import Credentials, OAuth2Credentials
from backend.data.redis_client import get_redis_async
from backend.integrations.credentials_store import IntegrationCredentialsStore
-from backend.integrations.oauth import HANDLERS_BY_NAME
+from backend.integrations.oauth import CREDENTIALS_BY_PROVIDER, HANDLERS_BY_NAME
from backend.integrations.providers import ProviderName
from backend.util.exceptions import MissingConfigError
from backend.util.settings import Settings
@@ -196,8 +197,25 @@ async def _get_provider_oauth_handler(provider_name_str: str) -> "BaseOAuthHandl
if provider_name not in HANDLERS_BY_NAME:
raise KeyError(f"Unknown provider '{provider_name}'")
- client_id = getattr(settings.secrets, f"{provider_name.value}_client_id")
- client_secret = getattr(settings.secrets, f"{provider_name.value}_client_secret")
+ provider_creds = CREDENTIALS_BY_PROVIDER[provider_name]
+ if not provider_creds.use_secrets:
+ # This is safe to do as we check that the env vars exist in the registry
+ client_id = (
+ os.getenv(provider_creds.client_id_env_var)
+ if provider_creds.client_id_env_var
+ else None
+ )
+ client_secret = (
+ os.getenv(provider_creds.client_secret_env_var)
+ if provider_creds.client_secret_env_var
+ else None
+ )
+ else:
+ client_id = getattr(settings.secrets, f"{provider_name.value}_client_id")
+ client_secret = getattr(
+ settings.secrets, f"{provider_name.value}_client_secret"
+ )
+
if not (client_id and client_secret):
raise MissingConfigError(
f"Integration with provider '{provider_name}' is not configured",
diff --git a/autogpt_platform/backend/backend/sdk/builder.py b/autogpt_platform/backend/backend/sdk/builder.py
index 3d05eeed4b..a015560d73 100644
--- a/autogpt_platform/backend/backend/sdk/builder.py
+++ b/autogpt_platform/backend/backend/sdk/builder.py
@@ -2,19 +2,27 @@
Builder class for creating provider configurations with a fluent API.
"""
+import logging
import os
from typing import Callable, List, Optional, Type
from pydantic import SecretStr
from backend.data.cost import BlockCost, BlockCostType
-from backend.data.model import APIKeyCredentials, Credentials, UserPasswordCredentials
+from backend.data.model import (
+ APIKeyCredentials,
+ Credentials,
+ CredentialsType,
+ UserPasswordCredentials,
+)
from backend.integrations.oauth.base import BaseOAuthHandler
from backend.integrations.webhooks._base import BaseWebhooksManager
from backend.sdk.provider import OAuthConfig, Provider
from backend.sdk.registry import AutoRegistry
from backend.util.settings import Settings
+logger = logging.getLogger(__name__)
+
class ProviderBuilder:
"""Builder for creating provider configurations."""
@@ -25,7 +33,7 @@ class ProviderBuilder:
self._webhook_manager: Optional[Type[BaseWebhooksManager]] = None
self._default_credentials: List[Credentials] = []
self._base_costs: List[BlockCost] = []
- self._supported_auth_types: set = set()
+ self._supported_auth_types: set[CredentialsType] = set()
self._api_client_factory: Optional[Callable] = None
self._error_handler: Optional[Callable[[Exception], str]] = None
self._default_scopes: Optional[List[str]] = None
@@ -41,13 +49,26 @@ class ProviderBuilder:
client_secret_env_var: Optional[str] = None,
) -> "ProviderBuilder":
"""Add OAuth support."""
- self._oauth_config = OAuthConfig(
- oauth_handler=handler_class,
- scopes=scopes,
- client_id_env_var=client_id_env_var,
- client_secret_env_var=client_secret_env_var,
- )
- self._supported_auth_types.add("oauth2")
+ if not client_id_env_var or not client_secret_env_var:
+ client_id_env_var = f"{self.name}_client_id".upper()
+ client_secret_env_var = f"{self.name}_client_secret".upper()
+
+ if os.getenv(client_id_env_var) and os.getenv(client_secret_env_var):
+ self._client_id_env_var = client_id_env_var
+ self._client_secret_env_var = client_secret_env_var
+
+ self._oauth_config = OAuthConfig(
+ oauth_handler=handler_class,
+ scopes=scopes,
+ client_id_env_var=client_id_env_var,
+ client_secret_env_var=client_secret_env_var,
+ )
+ self._supported_auth_types.add("oauth2")
+ else:
+ logger.warning(
+ f"Provider {self.name.upper()} implements OAuth but the required env "
+ f"vars {client_id_env_var} and {client_secret_env_var} are not both set"
+ )
return self
def with_api_key(self, env_var_name: str, title: str) -> "ProviderBuilder":
diff --git a/autogpt_platform/backend/backend/sdk/cost_integration.py b/autogpt_platform/backend/backend/sdk/cost_integration.py
index 3ede3cea1f..3eb4b47062 100644
--- a/autogpt_platform/backend/backend/sdk/cost_integration.py
+++ b/autogpt_platform/backend/backend/sdk/cost_integration.py
@@ -71,7 +71,7 @@ def register_provider_costs_for_block(block_class: Type[Block]) -> None:
# Add provider's base costs to the block
if provider.base_costs:
- logger.info(
+ logger.debug(
f"Registering {len(provider.base_costs)} base costs from provider {provider_name} for block {block_class.__name__}"
)
block_costs.extend(provider.base_costs)
@@ -79,7 +79,7 @@ def register_provider_costs_for_block(block_class: Type[Block]) -> None:
# Register costs if any were found
if block_costs:
BLOCK_COSTS[block_class] = block_costs
- logger.info(
+ logger.debug(
f"Registered {len(block_costs)} total costs for block {block_class.__name__}"
)
diff --git a/autogpt_platform/backend/backend/sdk/provider.py b/autogpt_platform/backend/backend/sdk/provider.py
index 565feab774..8d9b488562 100644
--- a/autogpt_platform/backend/backend/sdk/provider.py
+++ b/autogpt_platform/backend/backend/sdk/provider.py
@@ -13,6 +13,7 @@ from backend.data.model import (
Credentials,
CredentialsField,
CredentialsMetaInput,
+ CredentialsType,
)
from backend.integrations.oauth.base import BaseOAuthHandler
from backend.integrations.webhooks._base import BaseWebhooksManager
@@ -23,8 +24,8 @@ class OAuthConfig(BaseModel):
oauth_handler: Type[BaseOAuthHandler]
scopes: Optional[List[str]] = None
- client_id_env_var: Optional[str] = None
- client_secret_env_var: Optional[str] = None
+ client_id_env_var: str
+ client_secret_env_var: str
class Provider:
@@ -49,7 +50,7 @@ class Provider:
webhook_manager: Optional[Type[BaseWebhooksManager]] = None,
default_credentials: Optional[List[Credentials]] = None,
base_costs: Optional[List[BlockCost]] = None,
- supported_auth_types: Optional[Set[str]] = None,
+ supported_auth_types: Optional[Set[CredentialsType]] = None,
api_client_factory: Optional[Callable] = None,
error_handler: Optional[Callable[[Exception], str]] = None,
**kwargs,
@@ -81,9 +82,7 @@ class Provider:
json_schema_extra = {
"credentials_provider": [self.name],
"credentials_types": (
- list(self.supported_auth_types)
- if self.supported_auth_types
- else ["api_key"]
+ list(self.supported_auth_types) if self.supported_auth_types else []
),
}
diff --git a/autogpt_platform/backend/backend/sdk/registry.py b/autogpt_platform/backend/backend/sdk/registry.py
index d65d690644..888f857949 100644
--- a/autogpt_platform/backend/backend/sdk/registry.py
+++ b/autogpt_platform/backend/backend/sdk/registry.py
@@ -11,6 +11,7 @@ from pydantic import BaseModel, SecretStr
from backend.blocks.basic import Block
from backend.data.model import APIKeyCredentials, Credentials
from backend.integrations.oauth.base import BaseOAuthHandler
+from backend.integrations.providers import ProviderName
from backend.integrations.webhooks._base import BaseWebhooksManager
if TYPE_CHECKING:
@@ -68,16 +69,12 @@ class AutoRegistry:
not hasattr(provider.oauth_config.oauth_handler, "PROVIDER_NAME")
or provider.oauth_config.oauth_handler.PROVIDER_NAME is None
):
- # Import ProviderName to create dynamic enum value
- from backend.integrations.providers import ProviderName
-
# This works because ProviderName has _missing_ method
provider.oauth_config.oauth_handler.PROVIDER_NAME = ProviderName(
provider.name
)
cls._oauth_handlers[provider.name] = provider.oauth_config.oauth_handler
- # Register OAuth credentials configuration
oauth_creds = SDKOAuthCredentials(
use_secrets=False, # SDK providers use custom env vars
client_id_env_var=provider.oauth_config.client_id_env_var,
@@ -92,8 +89,6 @@ class AutoRegistry:
not hasattr(provider.webhook_manager, "PROVIDER_NAME")
or provider.webhook_manager.PROVIDER_NAME is None
):
- # Import ProviderName to create dynamic enum value
- from backend.integrations.providers import ProviderName
# This works because ProviderName has _missing_ method
provider.webhook_manager.PROVIDER_NAME = ProviderName(provider.name)
@@ -206,9 +201,6 @@ class AutoRegistry:
# Add SDK-registered managers
sdk_managers = cls.get_webhook_managers()
if isinstance(sdk_managers, dict):
- # Import ProviderName for conversion
- from backend.integrations.providers import ProviderName
-
# Convert string keys to ProviderName for consistency
for provider_str, manager in sdk_managers.items():
provider_name = ProviderName(provider_str)
diff --git a/autogpt_platform/backend/backend/server/rest_api.py b/autogpt_platform/backend/backend/server/rest_api.py
index 9eb75251ef..2b376ca269 100644
--- a/autogpt_platform/backend/backend/server/rest_api.py
+++ b/autogpt_platform/backend/backend/server/rest_api.py
@@ -62,10 +62,13 @@ def launch_darkly_context():
@contextlib.asynccontextmanager
async def lifespan_context(app: fastapi.FastAPI):
await backend.data.db.connect()
- await backend.data.block.initialize_blocks()
- # SDK auto-registration is now handled by AutoRegistry.patch_integrations()
- # which is called when the SDK module is imported
+ # Ensure SDK auto-registration is patched before initializing blocks
+ from backend.sdk.registry import AutoRegistry
+
+ AutoRegistry.patch_integrations()
+
+ await backend.data.block.initialize_blocks()
await backend.data.user.migrate_and_encrypt_user_integrations()
await backend.data.graph.fix_llm_provider_credentials()
diff --git a/autogpt_platform/backend/test/sdk/test_sdk_registry.py b/autogpt_platform/backend/test/sdk/test_sdk_registry.py
index 701e1d75e1..4970ba5933 100644
--- a/autogpt_platform/backend/test/sdk/test_sdk_registry.py
+++ b/autogpt_platform/backend/test/sdk/test_sdk_registry.py
@@ -9,6 +9,7 @@ This test suite verifies:
5. Block configuration association
"""
+import os
from unittest.mock import MagicMock, Mock, patch
import pytest
@@ -61,20 +62,29 @@ class TestAutoRegistry:
from backend.sdk.provider import OAuthConfig
- provider = Provider(
- name="oauth_provider",
- oauth_config=OAuthConfig(oauth_handler=TestOAuthHandler),
- webhook_manager=None,
- default_credentials=[],
- base_costs=[],
- supported_auth_types={"oauth2"},
- )
+ # Set environment variables so OAuth handler gets registered
+ with patch.dict(
+ os.environ,
+ {"TEST_CLIENT_ID": "test_id", "TEST_CLIENT_SECRET": "test_secret"},
+ ):
+ provider = Provider(
+ name="oauth_provider",
+ oauth_config=OAuthConfig(
+ oauth_handler=TestOAuthHandler,
+ client_id_env_var="TEST_CLIENT_ID",
+ client_secret_env_var="TEST_CLIENT_SECRET",
+ ),
+ webhook_manager=None,
+ default_credentials=[],
+ base_costs=[],
+ supported_auth_types={"oauth2"},
+ )
- AutoRegistry.register_provider(provider)
+ AutoRegistry.register_provider(provider)
- # Verify OAuth handler is registered
- assert "oauth_provider" in AutoRegistry._oauth_handlers
- assert AutoRegistry._oauth_handlers["oauth_provider"] == TestOAuthHandler
+ # Verify OAuth handler is registered
+ assert "oauth_provider" in AutoRegistry._oauth_handlers
+ assert AutoRegistry._oauth_handlers["oauth_provider"] == TestOAuthHandler
def test_provider_with_webhook_manager(self):
"""Test provider registration with webhook manager."""
@@ -170,32 +180,45 @@ class TestAutoRegistry:
from backend.sdk.provider import OAuthConfig
- provider1 = Provider(
- name="provider1",
- oauth_config=OAuthConfig(oauth_handler=TestOAuth1),
- webhook_manager=None,
- default_credentials=[],
- base_costs=[],
- supported_auth_types={"oauth2"},
- )
+ # Set environment variables so OAuth handlers get registered
+ with patch.dict(
+ os.environ,
+ {"TEST_CLIENT_ID": "test_id", "TEST_CLIENT_SECRET": "test_secret"},
+ ):
+ provider1 = Provider(
+ name="provider1",
+ oauth_config=OAuthConfig(
+ oauth_handler=TestOAuth1,
+ client_id_env_var="TEST_CLIENT_ID",
+ client_secret_env_var="TEST_CLIENT_SECRET",
+ ),
+ webhook_manager=None,
+ default_credentials=[],
+ base_costs=[],
+ supported_auth_types={"oauth2"},
+ )
- provider2 = Provider(
- name="provider2",
- oauth_config=OAuthConfig(oauth_handler=TestOAuth2),
- webhook_manager=None,
- default_credentials=[],
- base_costs=[],
- supported_auth_types={"oauth2"},
- )
+ provider2 = Provider(
+ name="provider2",
+ oauth_config=OAuthConfig(
+ oauth_handler=TestOAuth2,
+ client_id_env_var="TEST_CLIENT_ID",
+ client_secret_env_var="TEST_CLIENT_SECRET",
+ ),
+ webhook_manager=None,
+ default_credentials=[],
+ base_costs=[],
+ supported_auth_types={"oauth2"},
+ )
- AutoRegistry.register_provider(provider1)
- AutoRegistry.register_provider(provider2)
+ AutoRegistry.register_provider(provider1)
+ AutoRegistry.register_provider(provider2)
- handlers = AutoRegistry.get_oauth_handlers()
- assert "provider1" in handlers
- assert "provider2" in handlers
- assert handlers["provider1"] == TestOAuth1
- assert handlers["provider2"] == TestOAuth2
+ handlers = AutoRegistry.get_oauth_handlers()
+ assert "provider1" in handlers
+ assert "provider2" in handlers
+ assert handlers["provider1"] == TestOAuth1
+ assert handlers["provider2"] == TestOAuth2
def test_block_configuration_registration(self):
"""Test registering block configuration."""
@@ -316,15 +339,22 @@ class TestProviderBuilder:
class TestOAuth(BaseOAuthHandler):
PROVIDER_NAME = ProviderName.GITHUB
- provider = (
- ProviderBuilder("oauth_test")
- .with_oauth(TestOAuth, scopes=["read", "write"])
- .build()
- )
+ with patch.dict(
+ os.environ,
+ {
+ "OAUTH_TEST_CLIENT_ID": "test_id",
+ "OAUTH_TEST_CLIENT_SECRET": "test_secret",
+ },
+ ):
+ provider = (
+ ProviderBuilder("oauth_test")
+ .with_oauth(TestOAuth, scopes=["read", "write"])
+ .build()
+ )
- assert provider.oauth_config is not None
- assert provider.oauth_config.oauth_handler == TestOAuth
- assert "oauth2" in provider.supported_auth_types
+ assert provider.oauth_config is not None
+ assert provider.oauth_config.oauth_handler == TestOAuth
+ assert "oauth2" in provider.supported_auth_types
def test_provider_builder_with_webhook(self):
"""Test building a provider with webhook manager."""
@@ -395,34 +425,43 @@ class TestProviderBuilder:
def error_handler(exc):
return str(exc)
- provider = (
- ProviderBuilder("complete_test")
- .with_api_key("COMPLETE_API_KEY", "Complete API Key")
- .with_oauth(TestOAuth, scopes=["read"])
- .with_webhook_manager(TestWebhook)
- .with_base_cost(100, BlockCostType.RUN)
- .with_api_client(client_factory)
- .with_error_handler(error_handler)
- .with_config(custom_setting="value")
- .build()
- )
-
- # Verify all settings
- assert provider.name == "complete_test"
- assert "api_key" in provider.supported_auth_types
- assert "oauth2" in provider.supported_auth_types
- assert provider.oauth_config is not None
- assert provider.oauth_config.oauth_handler == TestOAuth
- assert provider.webhook_manager == TestWebhook
- assert len(provider.base_costs) == 1
- assert provider._api_client_factory == client_factory
- assert provider._error_handler == error_handler
- assert provider.get_config("custom_setting") == "value" # from with_config
+ # Set environment variables for OAuth to be registered
+ with patch.dict(
+ os.environ,
+ {
+ "COMPLETE_TEST_CLIENT_ID": "test_id",
+ "COMPLETE_TEST_CLIENT_SECRET": "test_secret",
+ "COMPLETE_API_KEY": "test_api_key",
+ },
+ ):
+ provider = (
+ ProviderBuilder("complete_test")
+ .with_api_key("COMPLETE_API_KEY", "Complete API Key")
+ .with_oauth(TestOAuth, scopes=["read"])
+ .with_webhook_manager(TestWebhook)
+ .with_base_cost(100, BlockCostType.RUN)
+ .with_api_client(client_factory)
+ .with_error_handler(error_handler)
+ .with_config(custom_setting="value")
+ .build()
+ )
- # Verify it's registered
- assert AutoRegistry.get_provider("complete_test") == provider
- assert "complete_test" in AutoRegistry._oauth_handlers
- assert "complete_test" in AutoRegistry._webhook_managers
+ # Verify all settings
+ assert provider.name == "complete_test"
+ assert "api_key" in provider.supported_auth_types
+ assert "oauth2" in provider.supported_auth_types
+ assert provider.oauth_config is not None
+ assert provider.oauth_config.oauth_handler == TestOAuth
+ assert provider.webhook_manager == TestWebhook
+ assert len(provider.base_costs) == 1
+ assert provider._api_client_factory == client_factory
+ assert provider._error_handler == error_handler
+ assert provider.get_config("custom_setting") == "value" # from with_config
+
+ # Verify it's registered
+ assert AutoRegistry.get_provider("complete_test") == provider
+ assert "complete_test" in AutoRegistry._oauth_handlers
+ assert "complete_test" in AutoRegistry._webhook_managers
class TestSDKImports: