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.

feat(backend): describe an AI connection on the server that enforces it (#14082)

Nicholas Tindle47cc451b9139
diff --git a/autogpt_platform/backend/backend/api/features/chat/routes.py b/autogpt_platform/backend/backend/api/features/chat/routes.py
index 9affe240c6..369c4e0102 100644
--- a/autogpt_platform/backend/backend/api/features/chat/routes.py
+++ b/autogpt_platform/backend/backend/api/features/chat/routes.py
@@ -52,6 +52,7 @@ from backend.copilot.model import (
update_session_pinned,
update_session_title,
)
+from backend.copilot.offers import AIConnectionOffersResponse, get_connection_offers
from backend.copilot.pending_message_helpers import (
QueuePendingMessageResponse,
StreamRegistryUnavailable,
@@ -554,6 +555,23 @@ async def list_chat_transports(
return ChatTransportsResponse(transports=await get_chat_transports(user_id))
+ "/connections",
+ dependencies=[Security(auth.requires_user)],
+)
+async def list_chat_connections(
+ user_id: Annotated[str, Security(auth.get_user_id)],
+) -> AIConnectionOffersResponse:
+ """Every AI connection the user can chat over, described by the server.
+
+ Additive alongside ``GET /transports``, which keeps its shape. This
+ carries what a client would otherwise have to infer — provider family,
+ what backs a run, the quality tiers, and the limitations that apply —
+ so product and billing statements come from the side that enforces them.
+ """
+ return AIConnectionOffersResponse(offers=await get_connection_offers(user_id))
+
+
class SetDefaultTransportRequest(BaseModel):
"""The connection new chats should start on.
diff --git a/autogpt_platform/backend/backend/copilot/offers.py b/autogpt_platform/backend/backend/copilot/offers.py
new file mode 100644
index 0000000000..5f89c94822
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/offers.py
@@ -0,0 +1,147 @@
+"""Server-owned description of the AI connections a user can chat over.
+
+``transports`` answers "which routes exist and which is default". That is
+enough to route a turn, but not enough to render one: the client has been
+deciding what a connection is called, what backs it, and which provider it
+belongs to. Those are product and billing statements, and a client that
+computes them drifts from the server that enforces them.
+
+This module states them once, on the side that knows. It is additive —
+``GET /transports`` keeps its shape, so nothing has to migrate at once.
+"""
+
+import logging
+
+from pydantic import BaseModel
+
+from backend.copilot.config import CopilotLlmAuthProvider, CopilotLLMModel
+from backend.copilot.transports import (
+ ChatTransportResponse,
+ get_chat_transports,
+ is_deployment_chat_available,
+ settings,
+)
+from backend.util.settings import BehaveAs
+
+logger = logging.getLogger(__name__)
+
+# Product vocabulary. "Fast" and "Thinking" are execution paths, not tiers a
+# user picks between; the two labels below are what the product exposes.
+TIER_LABELS: dict[CopilotLLMModel, str] = {
+ "standard": "Balanced",
+ "advanced": "Advanced",
+}
+
+
+class ConnectionTier(BaseModel):
+ """One quality level on a connection.
+
+ ``display_model`` is deliberately absent. Naming the model a tier will
+ resolve to needs the execution path, which is decided per turn by which
+ service handles it — and on ChatGPT it needs a live call against the
+ account. A name that is right half the time is worse here than no name.
+ """
+
+ tier: CopilotLLMModel
+ label: str
+ selectable: bool
+
+
+class AIConnectionOffer(BaseModel):
+ offer_id: str
+ provider_family: str
+ display_name: str
+ auth_method: str
+ credential_id: str | None
+ backed_by_label: str
+ description: str
+ state: str
+ selectable: bool
+ is_default: bool
+ tiers: list[ConnectionTier]
+ limitations: list[str]
+
+
+class AIConnectionOffersResponse(BaseModel):
+ offers: list[AIConnectionOffer]
+
+
+async def get_connection_offers(user_id: str) -> list[AIConnectionOffer]:
+ return [_offer(transport) for transport in await get_chat_transports(user_id)]
+
+
+def offer_id_for(transport: ChatTransportResponse) -> str:
+ """Stable across requests, and unique per account.
+
+ The platform route carries no credential, and a user may hold several
+ ChatGPT accounts, so neither half identifies an offer on its own.
+ """
+ return f"{transport.auth_provider}:{transport.credential_id or 'deployment'}"
+
+
+def _offer(transport: ChatTransportResponse) -> AIConnectionOffer:
+ return AIConnectionOffer(
+ offer_id=offer_id_for(transport),
+ provider_family=_provider_family(transport.auth_provider),
+ display_name=transport.label,
+ auth_method=_auth_method(transport.auth_provider),
+ credential_id=transport.credential_id,
+ backed_by_label=_backed_by_label(transport),
+ description=_description(transport),
+ state="ready" if transport.available else "unavailable",
+ selectable=transport.available,
+ is_default=transport.default,
+ tiers=[
+ ConnectionTier(tier=tier, label=label, selectable=transport.available)
+ for tier, label in TIER_LABELS.items()
+ ],
+ limitations=_limitations(transport),
+ )
+
+
+def _provider_family(auth_provider: CopilotLlmAuthProvider) -> str:
+ # Presentation grouping, not the credential discriminator: ChatGPT OAuth
+ # and an OpenAI API key are one family and two very different credentials.
+ return "openai" if auth_provider == "codex" else "autogpt"
+
+
+def _auth_method(auth_provider: CopilotLlmAuthProvider) -> str:
+ return "chatgpt_oauth" if auth_provider == "codex" else "deployment"
+
+
+def _is_hosted() -> bool:
+ return settings.config.behave_as == BehaveAs.CLOUD
+
+
+def _backed_by_label(transport: ChatTransportResponse) -> str:
+ if transport.auth_provider == "codex":
+ return "Your ChatGPT plan"
+ return "Your AutoGPT plan" if _is_hosted() else "This server's chat provider"
+
+
+def _description(transport: ChatTransportResponse) -> str:
+ """What backs a run, in the user's terms.
+
+ The credits contrast only means something where credits exist, so the
+ self-hosted line says what it actually has instead of denying something
+ that was never there.
+ """
+ if transport.auth_provider == "codex":
+ return (
+ "New chats are backed by your ChatGPT plan, and spend no "
+ "AutoGPT credits."
+ )
+ if _is_hosted():
+ return "New chats are backed by your AutoGPT plan, and spend AutoGPT credits."
+ return "New chats are backed by the chat provider configured on this server."
+
+
+def _limitations(transport: ChatTransportResponse) -> list[str]:
+ limitations: list[str] = []
+ if transport.auth_provider == "codex":
+ # Stated because it is a real edge a user can hit, not a policy note:
+ # the builder panel rejects a codex route outright.
+ limitations.append("The agent builder's chat panel always runs on AutoGPT.")
+ if transport.auth_provider == "platform" and not is_deployment_chat_available():
+ limitations.append("No chat provider is configured on this server yet.")
+ return limitations
diff --git a/autogpt_platform/backend/backend/copilot/offers_test.py b/autogpt_platform/backend/backend/copilot/offers_test.py
new file mode 100644
index 0000000000..ffca12dfa9
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/offers_test.py
@@ -0,0 +1,164 @@
+"""The server-owned description of an AI connection."""
+
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+import pytest_mock
+
+from backend.copilot import offers, transports
+from backend.copilot.offers import get_connection_offers, offer_id_for
+from backend.copilot.transports import ChatTransportResponse
+from backend.util.settings import BehaveAs
+
+USER_ID = "3e53486c-cf57-477e-ba2a-cb02dc828e1a"
+
+
+def _transport(
+ auth_provider: str = "platform",
+ credential_id: str | None = None,
+ label: str = "AutoGPT Platform",
+ available: bool = True,
+ default: bool = False,
+) -> ChatTransportResponse:
+ return ChatTransportResponse(
+ auth_provider=auth_provider, # type: ignore[arg-type]
+ credential_id=credential_id,
+ label=label,
+ available=available,
+ default=default,
+ )
+
+
[email protected](autouse=True)
+def hosted(mocker: pytest_mock.MockerFixture):
+ mocker.patch.object(offers.settings.config, "behave_as", BehaveAs.CLOUD)
+ mocker.patch.object(transports.settings.config, "behave_as", BehaveAs.CLOUD)
+
+
+def _mock_transports(
+ mocker: pytest_mock.MockerFixture, transport_list: list[ChatTransportResponse]
+) -> None:
+ mocker.patch.object(
+ offers,
+ "get_chat_transports",
+ new=AsyncMock(return_value=transport_list),
+ )
+
+
+async def test_an_offer_says_what_backs_a_run(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ _mock_transports(
+ mocker,
+ [_transport(default=True), _transport("codex", "cred-1", "ChatGPT")],
+ )
+
+ platform, chatgpt = await get_connection_offers(USER_ID)
+
+ assert platform.backed_by_label == "Your AutoGPT plan"
+ assert "spend AutoGPT credits" in platform.description
+ assert chatgpt.backed_by_label == "Your ChatGPT plan"
+ assert "spend no AutoGPT credits" in chatgpt.description
+
+
+async def test_self_host_does_not_deny_credits_it_never_had(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ mocker.patch.object(offers.settings.config, "behave_as", BehaveAs.LOCAL)
+ _mock_transports(mocker, [_transport(label="Self-hosted chat", default=True)])
+
+ (offer,) = await get_connection_offers(USER_ID)
+
+ assert offer.backed_by_label == "This server's chat provider"
+ assert "credits" not in offer.description
+
+
+async def test_provider_family_groups_without_becoming_the_credential(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ _mock_transports(mocker, [_transport(), _transport("codex", "cred-1", "ChatGPT")])
+
+ platform, chatgpt = await get_connection_offers(USER_ID)
+
+ assert platform.provider_family == "autogpt"
+ assert chatgpt.provider_family == "openai"
+ assert chatgpt.auth_method == "chatgpt_oauth"
+ assert chatgpt.credential_id == "cred-1"
+
+
+async def test_offer_ids_are_stable_and_per_account(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ first = _transport("codex", "cred-1", "ChatGPT")
+ second = _transport("codex", "cred-2", "ChatGPT")
+ _mock_transports(mocker, [_transport(), first, second])
+
+ ids = [offer.offer_id for offer in await get_connection_offers(USER_ID)]
+
+ assert ids == ["platform:deployment", "codex:cred-1", "codex:cred-2"]
+ assert offer_id_for(first) == offer_id_for(first)
+
+
+async def test_tiers_are_labelled_but_never_named_with_a_model(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ """Naming the model needs the execution path, decided per turn."""
+ _mock_transports(mocker, [_transport(default=True)])
+
+ (offer,) = await get_connection_offers(USER_ID)
+
+ assert [tier.label for tier in offer.tiers] == ["Balanced", "Advanced"]
+ assert [tier.tier for tier in offer.tiers] == ["standard", "advanced"]
+ assert not any(hasattr(tier, "display_model") for tier in offer.tiers)
+
+
+async def test_an_unavailable_connection_is_not_selectable(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ mocker.patch.object(offers.settings.config, "behave_as", BehaveAs.LOCAL)
+ chat_config = MagicMock()
+ chat_config.test_mode = False
+ chat_config.use_claude_code_subscription = False
+ chat_config.main_client_credentials = (None, "https://api.anthropic.com/v1/")
+ mocker.patch.object(transports, "config", chat_config)
+ _mock_transports(mocker, [_transport(label="Self-hosted chat", available=False)])
+
+ (offer,) = await get_connection_offers(USER_ID)
+
+ assert offer.state == "unavailable"
+ assert offer.selectable is False
+ assert all(tier.selectable is False for tier in offer.tiers)
+ assert "No chat provider is configured on this server yet." in offer.limitations
+
+
+async def test_chatgpt_states_the_edge_a_user_can_actually_hit(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ _mock_transports(mocker, [_transport("codex", "cred-1", "ChatGPT")])
+
+ (offer,) = await get_connection_offers(USER_ID)
+
+ assert offer.limitations == [
+ "The agent builder's chat panel always runs on AutoGPT."
+ ]
+
+
+async def test_exactly_one_offer_is_the_default(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ _mock_transports(
+ mocker,
+ [_transport(default=True), _transport("codex", "cred-1", "ChatGPT")],
+ )
+
+ offer_list = await get_connection_offers(USER_ID)
+
+ assert [offer.is_default for offer in offer_list].count(True) == 1
diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.json
index f9271165fc..c61f24ff0d 100644
--- a/autogpt_platform/frontend/src/app/api/openapi.json
+++ b/autogpt_platform/frontend/src/app/api/openapi.json
@@ -3699,6 +3699,30 @@
}
}
},
+ "/api/chat/connections": {
+ "get": {
+ "tags": ["v2", "chat", "chat"],
+ "summary": "List Chat Connections",
+ "description": "Every AI connection the user can chat over, described by the server.\n\nAdditive alongside ``GET /transports``, which keeps its shape. This\ncarries what a client would otherwise have to infer — provider family,\nwhat backs a run, the quality tiers, and the limitations that apply —\nso product and billing statements come from the side that enforces them.",
+ "operationId": "getV2ListChatConnections",
+ "responses": {
+ "200": {
+ "description": "Successful Response",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/AIConnectionOffersResponse"
+ }
+ }
+ }
+ },
+ "401": {
+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"
+ }
+ },
+ "security": [{ "HTTPBearerJWT": [] }]
+ }
+ },
"/api/chat/health": {
"get": {
"tags": ["v2", "chat", "chat"],
@@ -15017,6 +15041,61 @@
},
"components": {
"schemas": {
+ "AIConnectionOffer": {
+ "properties": {
+ "offer_id": { "type": "string", "title": "Offer Id" },
+ "provider_family": { "type": "string", "title": "Provider Family" },
+ "display_name": { "type": "string", "title": "Display Name" },
+ "auth_method": { "type": "string", "title": "Auth Method" },
+ "credential_id": {
+ "anyOf": [{ "type": "string" }, { "type": "null" }],
+ "title": "Credential Id"
+ },
+ "backed_by_label": { "type": "string", "title": "Backed By Label" },
+ "description": { "type": "string", "title": "Description" },
+ "state": { "type": "string", "title": "State" },
+ "selectable": { "type": "boolean", "title": "Selectable" },
+ "is_default": { "type": "boolean", "title": "Is Default" },
+ "tiers": {
+ "items": { "$ref": "#/components/schemas/ConnectionTier" },
+ "type": "array",
+ "title": "Tiers"
+ },
+ "limitations": {
+ "items": { "type": "string" },
+ "type": "array",
+ "title": "Limitations"
+ }
+ },
+ "type": "object",
+ "required": [
+ "offer_id",
+ "provider_family",
+ "display_name",
+ "auth_method",
+ "credential_id",
+ "backed_by_label",
+ "description",
+ "state",
+ "selectable",
+ "is_default",
+ "tiers",
+ "limitations"
+ ],
+ "title": "AIConnectionOffer"
+ },
+ "AIConnectionOffersResponse": {
+ "properties": {
+ "offers": {
+ "items": { "$ref": "#/components/schemas/AIConnectionOffer" },
+ "type": "array",
+ "title": "Offers"
+ }
+ },
+ "type": "object",
+ "required": ["offers"],
+ "title": "AIConnectionOffersResponse"
+ },
"APIKeyCredentials": {
"properties": {
"id": { "type": "string", "title": "Id" },
@@ -17400,6 +17479,21 @@
"required": ["success", "platform", "platform_user_id"],
"title": "ConfirmUserLinkResponse"
},
+ "ConnectionTier": {
+ "properties": {
+ "tier": {
+ "type": "string",
+ "enum": ["standard", "advanced"],
+ "title": "Tier"
+ },
+ "label": { "type": "string", "title": "Label" },
+ "selectable": { "type": "boolean", "title": "Selectable" }
+ },
+ "type": "object",
+ "required": ["tier", "label", "selectable"],
+ "title": "ConnectionTier",
+ "description": "One quality level on a connection.\n\n``display_model`` is deliberately absent. Naming the model a tier will\nresolve to needs the execution path, which is decided per turn by which\nservice handles it — and on ChatGPT it needs a live call against the\naccount. A name that is right half the time is worse here than no name."
+ },
"ContentType": {
"type": "string",
"enum": [