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/copilot): AutoPilot runs a block with an optional credentials field when the user has no key (#14887)

Reinier van der Leerddd393d7097d
diff --git a/autogpt_platform/backend/backend/copilot/tools/helpers.py b/autogpt_platform/backend/backend/copilot/tools/helpers.py
index f1a0ce8840..ffef4bfaf0 100644
--- a/autogpt_platform/backend/backend/copilot/tools/helpers.py
+++ b/autogpt_platform/backend/backend/copilot/tools/helpers.py
@@ -830,8 +830,18 @@ async def resolve_block_credentials(
if not requirements:
return {}, []
+ # An optional field runs without a credential, as in the executor. Picker
+ # kwargs are not model fields; discriminated fields were decided above.
+ required_fields = block.input_schema.get_required_fields()
+ optional_fields = {
+ name
+ for name, info in requirements.items()
+ if name not in required_fields
+ and name in block.input_schema.model_fields
+ and not info.discriminator
+ }
return await match_credentials_to_requirements(
- user_id, requirements, expert_id, session_id
+ user_id, requirements, expert_id, session_id, optional_fields
)
diff --git a/autogpt_platform/backend/backend/copilot/tools/run_block_test.py b/autogpt_platform/backend/backend/copilot/tools/run_block_test.py
index 718055d9ba..bf414c0882 100644
--- a/autogpt_platform/backend/backend/copilot/tools/run_block_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/run_block_test.py
@@ -1827,3 +1827,218 @@ class TestSpendApproval:
add_spend.assert_awaited_once_with("expert-1", 5)
else:
add_spend.assert_not_awaited()
+
+
+class TestRunBlockOptionalCredentials:
+ """An optional credentials field runs empty when the user has none to
+ offer, as the executor does; a discriminated field keeps its own rules."""
+
+ @staticmethod
+ def _block(schema: type, seen: list):
+ block = make_mock_block_with_schema(
+ block_id="optional-creds-id",
+ name="Optional Key Search",
+ input_properties={},
+ required_fields=[],
+ )
+ block.input_schema = schema
+
+ async def _execute(input_data, **kwargs):
+ seen.append(kwargs.get("credentials"))
+ yield "result", "ok"
+
+ block.execute = _execute
+ return block
+
+ @staticmethod
+ async def _run(block, available: list, input_data: dict):
+ workspace = MagicMock()
+ workspace.get_or_create_workspace = AsyncMock(return_value=MagicMock(id="ws"))
+ creds_manager = MagicMock()
+ creds_manager.get = AsyncMock(side_effect=lambda _u, cid, **_: available[0])
+ reviews = MagicMock()
+ reviews.get_pending_reviews_for_chat_session = AsyncMock(return_value=[])
+ users = MagicMock()
+ users.get_user_by_id = AsyncMock(return_value=MagicMock(timezone="UTC"))
+ with (
+ patch("backend.copilot.tools.helpers.get_block", return_value=block),
+ patch("backend.copilot.tools.helpers.review_db", return_value=reviews),
+ patch("backend.copilot.tools.helpers.user_db", return_value=users),
+ patch(
+ "backend.copilot.tools.helpers.credit_db", return_value=_StubCreditDB()
+ ),
+ patch(
+ "backend.copilot.tools.helpers.block_usage_cost", return_value=(0, {})
+ ),
+ patch(
+ "backend.copilot.tools.utils.get_user_credentials",
+ new_callable=AsyncMock,
+ return_value=available,
+ ),
+ patch(
+ "backend.copilot.tools.utils.selected_credentials",
+ new_callable=AsyncMock,
+ return_value={},
+ ),
+ patch("backend.copilot.tools.helpers.workspace_db", return_value=workspace),
+ patch(
+ "backend.copilot.tools.helpers.IntegrationCredentialsManager",
+ return_value=creds_manager,
+ ),
+ ):
+ return await RunBlockTool()._execute(
+ user_id=_TEST_USER_ID,
+ session=make_session(user_id=_TEST_USER_ID),
+ block_id=block.id,
+ input_data=input_data,
+ dry_run=False,
+ )
+
+ @staticmethod
+ def _api_key():
+ from pydantic import SecretStr
+
+ from backend.data.model import APIKeyCredentials
+
+ return APIKeyCredentials(
+ id="key-1", provider="openai", api_key=SecretStr("sk"), title="Key"
+ )
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_runs_without_a_credential_when_none_is_connected(self):
+ seen: list = []
+ response = await self._run(
+ self._block(_OptionalKeyInput, seen), [], {"query": "q"}
+ )
+
+ assert isinstance(response, BlockOutputResponse), response
+ assert seen == [None]
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_passes_a_connected_credential(self):
+ seen: list = []
+ key = self._api_key()
+ response = await self._run(
+ self._block(_OptionalKeyInput, seen), [key], {"query": "q"}
+ )
+
+ assert isinstance(response, BlockOutputResponse), response
+ assert seen == [key]
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_asks_which_key_when_several_fit(self):
+ from .models import SetupRequirementsResponse
+
+ seen: list = []
+ second = self._api_key().model_copy(update={"id": "key-2"})
+ response = await self._run(
+ self._block(_OptionalKeyInput, seen),
+ [self._api_key(), second],
+ {"query": "q"},
+ )
+
+ assert isinstance(response, SetupRequirementsResponse), response
+ assert seen == []
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_picker_credentials_are_still_reported_missing(self):
+ from backend.blocks.google.sheets import GoogleSheetsReadBlock
+
+ from .helpers import resolve_block_credentials
+
+ with (
+ patch(
+ "backend.copilot.tools.utils.get_user_credentials",
+ new_callable=AsyncMock,
+ return_value=[],
+ ),
+ patch(
+ "backend.copilot.tools.utils.selected_credentials",
+ new_callable=AsyncMock,
+ return_value={},
+ ),
+ ):
+ _, missing = await resolve_block_credentials(
+ _TEST_USER_ID, GoogleSheetsReadBlock(), {}
+ )
+
+ assert [m.id for m in missing] == ["credentials"]
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_optional_discriminated_field_still_asks_for_a_credential(self):
+ from .models import SetupRequirementsResponse
+
+ seen: list = []
+ response = await self._run(
+ self._block(_OptionalDiscriminatedInput, seen),
+ [],
+ {"query": "q", "model": "paid-model"},
+ )
+
+ assert isinstance(response, SetupRequirementsResponse), response
+ assert seen == []
+
+ @pytest.mark.parametrize(
+ "pinned_provider, runs", [("openai", False), ("anthropic", True)]
+ )
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_a_schedule_pin_on_its_provider_is_never_run_around(
+ self, pinned_provider, runs
+ ):
+ """The schedule's account is gone: running without it is the switch
+ the pin exists to prevent. A pin on another provider changes nothing."""
+ from backend.copilot.credential_selection import (
+ CredentialPin,
+ set_turn_credential_pins,
+ )
+
+ from .models import SetupRequirementsResponse
+
+ seen: list = []
+ set_turn_credential_pins(
+ {pinned_provider: CredentialPin(id="key-gone", title="Work key")}
+ )
+ try:
+ response = await self._run(
+ self._block(_OptionalKeyInput, seen), [], {"query": "q"}
+ )
+ finally:
+ set_turn_credential_pins(None)
+
+ if runs:
+ assert isinstance(response, BlockOutputResponse), response
+ assert seen == [None]
+ else:
+ assert isinstance(response, SetupRequirementsResponse), response
+ assert seen == []
+
+
+def _optional_schemas():
+ from typing import Literal
+
+ from backend.blocks._base import BlockSchemaInput
+ from backend.data.model import CredentialsField, CredentialsMetaInput, SchemaField
+ from backend.integrations.providers import ProviderName
+
+ OpenAIKey = CredentialsMetaInput[Literal[ProviderName.OPENAI], Literal["api_key"]]
+
+ class OptionalKeyInput(BlockSchemaInput):
+ query: str = SchemaField(description="Query")
+ credentials: OpenAIKey = CredentialsField(
+ description="Optional key", default=None
+ )
+
+ class OptionalDiscriminatedInput(BlockSchemaInput):
+ query: str = SchemaField(description="Query")
+ model: str = SchemaField(description="Model", default="paid-model")
+ credentials: OpenAIKey = CredentialsField(
+ description="Key for the model",
+ discriminator="model",
+ discriminator_mapping={"paid-model": ProviderName.OPENAI},
+ default=None,
+ )
+
+ return OptionalKeyInput, OptionalDiscriminatedInput
+
+
+_OptionalKeyInput, _OptionalDiscriminatedInput = _optional_schemas()
diff --git a/autogpt_platform/backend/backend/copilot/tools/utils.py b/autogpt_platform/backend/backend/copilot/tools/utils.py
index 41f0b829da..42ba6b601d 100644
--- a/autogpt_platform/backend/backend/copilot/tools/utils.py
+++ b/autogpt_platform/backend/backend/copilot/tools/utils.py
@@ -272,6 +272,7 @@ async def match_credentials_to_requirements(
requirements: dict[str, CredentialsFieldInfo],
expert_id: str | None = None,
session_id: str | None = None,
+ optional_fields: set[str] | frozenset[str] = frozenset(),
) -> tuple[dict[str, CredentialsMetaInput], list[CredentialsMetaInput]]:
"""
Match user's credentials against a dictionary of credential requirements.
@@ -279,6 +280,9 @@ async def match_credentials_to_requirements(
This is the core matching logic shared by both graph and block credential matching.
With a ``session_id`` the match is a chat tool's: the credential the user
picked in that chat is used, and a choice between several is left to them.
+ A field in ``optional_fields`` that no credential fits is left out rather
+ than reported missing, unless the running turn's schedule pinned its
+ account: running without it is the switch the pin exists to prevent.
"""
matched: dict[str, CredentialsMetaInput] = {}
missing: list[CredentialsMetaInput] = []
@@ -315,6 +319,12 @@ async def match_credentials_to_requirements(
title=f"{field_name} (validation failed: {e})",
)
)
+ elif (
+ field_name in optional_fields
+ and not _pinned_for(field_info)
+ and not any(_credential_fits(c, field_info) for c in available_creds)
+ ):
+ continue
else:
provider = next(iter(field_info.provider), "unknown")
cred_type = next(iter(field_info.supported_types), "api_key")
@@ -384,6 +394,17 @@ def keep_to_pins(available: list[Credentials]) -> list[Credentials]:
]
+def _pinned_for(field_info: CredentialsFieldInfo) -> bool:
+ """Whether the running turn's schedule pinned an account for this field;
+ as in ``keep_to_pins``, a pin never covers a per-target credential."""
+ pins = turn_credential_pins()
+ return field_info.supported_types != {"host_scoped"} and any(
+ str(getattr(p, "value", p)) in pins
+ for p in field_info.provider
+ if p != ProviderName.MCP
+ )
+
+
def is_per_target_credential(cred: Credentials) -> bool:
"""One credential per host or MCP server, rather than one per account."""
return cred.type == "host_scoped" or cred.provider == ProviderName.MCP