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(platform): make chat credentials type selection deterministic (#11795)
Nicholas Tindlefa0b7029ddb8
diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/run_agent.py b/autogpt_platform/backend/backend/api/features/chat/tools/run_agent.pyindex 4d93a3af30..b212c11e8a 100644--- a/autogpt_platform/backend/backend/api/features/chat/tools/run_agent.py+++ b/autogpt_platform/backend/backend/api/features/chat/tools/run_agent.py@@ -33,7 +33,7 @@ from .models import (UserReadiness,)from .utils import (- check_user_has_required_credentials,+ build_missing_credentials_from_graph,extract_credentials_from_schema,fetch_graph_from_store_slug,get_or_create_library_agent,@@ -237,15 +237,13 @@ class RunAgentTool(BaseTool):# Return credentials needed response with input data info# The UI handles credential setup automatically, so the message# focuses on asking about input data- credentials = extract_credentials_from_schema(- graph.credentials_input_schema+ requirements_creds_dict = build_missing_credentials_from_graph(+ graph, None)- missing_creds_check = await check_user_has_required_credentials(- user_id, credentials+ missing_credentials_dict = build_missing_credentials_from_graph(+ graph, graph_credentials)- missing_credentials_dict = {- c.id: c.model_dump() for c in missing_creds_check- }+ requirements_creds_list = list(requirements_creds_dict.values())return SetupRequirementsResponse(message=self._build_inputs_message(graph, MSG_WHAT_VALUES_TO_USE),@@ -259,7 +257,7 @@ class RunAgentTool(BaseTool):ready_to_run=False,),requirements={- "credentials": [c.model_dump() for c in credentials],+ "credentials": requirements_creds_list,"inputs": self._get_inputs_list(graph.input_schema),"execution_modes": self._get_execution_modes(graph),},diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/run_block.py b/autogpt_platform/backend/backend/api/features/chat/tools/run_block.pyindex 02f493df71..c29cc92556 100644--- a/autogpt_platform/backend/backend/api/features/chat/tools/run_block.py+++ b/autogpt_platform/backend/backend/api/features/chat/tools/run_block.py@@ -22,6 +22,7 @@ from .models import (ToolResponseBase,UserReadiness,)+from .utils import build_missing_credentials_from_field_infologger = logging.getLogger(__name__)@@ -189,7 +190,11 @@ class RunBlockTool(BaseTool):if missing_credentials:# Return setup requirements response with missing credentials- missing_creds_dict = {c.id: c.model_dump() for c in missing_credentials}+ credentials_fields_info = block.input_schema.get_credentials_fields_info()+ missing_creds_dict = build_missing_credentials_from_field_info(+ credentials_fields_info, set(matched_credentials.keys())+ )+ missing_creds_list = list(missing_creds_dict.values())return SetupRequirementsResponse(message=(@@ -206,7 +211,7 @@ class RunBlockTool(BaseTool):ready_to_run=False,),requirements={- "credentials": [c.model_dump() for c in missing_credentials],+ "credentials": missing_creds_list,"inputs": self._get_inputs_list(block),"execution_modes": ["immediate"],},diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/utils.py b/autogpt_platform/backend/backend/api/features/chat/tools/utils.pyindex 19e092c312..a2ac91dc65 100644--- a/autogpt_platform/backend/backend/api/features/chat/tools/utils.py+++ b/autogpt_platform/backend/backend/api/features/chat/tools/utils.py@@ -8,7 +8,7 @@ from backend.api.features.library import model as library_modelfrom backend.api.features.store import db as store_dbfrom backend.data import graph as graph_dbfrom backend.data.graph import GraphModel-from backend.data.model import CredentialsMetaInput+from backend.data.model import CredentialsFieldInfo, CredentialsMetaInputfrom backend.integrations.creds_manager import IntegrationCredentialsManagerfrom backend.util.exceptions import NotFoundError@@ -89,6 +89,59 @@ def extract_credentials_from_schema(return credentials+def _serialize_missing_credential(+ field_key: str, field_info: CredentialsFieldInfo+) -> dict[str, Any]:+ """+ Convert credential field info into a serializable dict that preserves all supported+ credential types (e.g., api_key + oauth2) so the UI can offer multiple options.+ """+ supported_types = sorted(field_info.supported_types)+ provider = next(iter(field_info.provider), "unknown")+ scopes = sorted(field_info.required_scopes or [])++ return {+ "id": field_key,+ "title": field_key.replace("_", " ").title(),+ "provider": provider,+ "provider_name": provider.replace("_", " ").title(),+ "type": supported_types[0] if supported_types else "api_key",+ "types": supported_types,+ "scopes": scopes,+ }+++def build_missing_credentials_from_graph(+ graph: GraphModel, matched_credentials: dict[str, CredentialsMetaInput] | None+) -> dict[str, Any]:+ """+ Build a missing_credentials mapping from a graph's aggregated credentials inputs,+ preserving all supported credential types for each field.+ """+ matched_keys = set(matched_credentials.keys()) if matched_credentials else set()+ aggregated_fields = graph.aggregate_credentials_inputs()++ return {+ field_key: _serialize_missing_credential(field_key, field_info)+ for field_key, (field_info, _node_fields) in aggregated_fields.items()+ if field_key not in matched_keys+ }+++def build_missing_credentials_from_field_info(+ credential_fields: dict[str, CredentialsFieldInfo],+ matched_keys: set[str],+) -> dict[str, Any]:+ """+ Build missing_credentials mapping from a simple credentials field info dictionary.+ """+ return {+ field_key: _serialize_missing_credential(field_key, field_info)+ for field_key, field_info in credential_fields.items()+ if field_key not in matched_keys+ }++def extract_credentials_as_dict(credentials_input_schema: dict[str, Any] | None,) -> dict[str, CredentialsMetaInput]:diff --git a/autogpt_platform/frontend/src/app/(platform)/chat/components/Chat/components/ChatContainer/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/chat/components/Chat/components/ChatContainer/helpers.tsindex cd05563369..ab7dbd275d 100644--- a/autogpt_platform/frontend/src/app/(platform)/chat/components/Chat/components/ChatContainer/helpers.ts+++ b/autogpt_platform/frontend/src/app/(platform)/chat/components/Chat/components/ChatContainer/helpers.ts@@ -267,23 +267,34 @@ export function extractCredentialsNeeded(| undefined;if (missingCreds && Object.keys(missingCreds).length > 0) {const agentName = (setupInfo?.agent_name as string) || "this block";- const credentials = Object.values(missingCreds).map((credInfo) => ({- provider: (credInfo.provider as string) || "unknown",- providerName:- (credInfo.provider_name as string) ||- (credInfo.provider as string) ||- "Unknown Provider",- credentialType:+ const credentials = Object.values(missingCreds).map((credInfo) => {+ // Normalize to array at boundary - prefer 'types' array, fall back to single 'type'+ const typesArray = credInfo.types as+ | Array<"api_key" | "oauth2" | "user_password" | "host_scoped">+ | undefined;+ const singleType =(credInfo.type as| "api_key"| "oauth2"| "user_password"- | "host_scoped") || "api_key",- title:- (credInfo.title as string) ||- `${(credInfo.provider_name as string) || (credInfo.provider as string)} credentials`,- scopes: credInfo.scopes as string[] | undefined,- }));+ | "host_scoped"+ | undefined) || "api_key";+ const credentialTypes =+ typesArray && typesArray.length > 0 ? typesArray : [singleType];++ return {+ provider: (credInfo.provider as string) || "unknown",+ providerName:+ (credInfo.provider_name as string) ||+ (credInfo.provider as string) ||+ "Unknown Provider",+ credentialTypes,+ title:+ (credInfo.title as string) ||+ `${(credInfo.provider_name as string) || (credInfo.provider as string)} credentials`,+ scopes: credInfo.scopes as string[] | undefined,+ };+ });return {type: "credentials_needed",toolName,@@ -358,11 +369,14 @@ export function extractInputsNeeded(credentials.forEach((cred) => {const id = cred.id as string;if (id) {+ const credentialTypes = Array.isArray(cred.types)+ ? cred.types+ : [(cred.type as string) || "api_key"];credentialsSchema[id] = {type: "object",properties: {},credentials_provider: [cred.provider as string],- credentials_types: [(cred.type as string) || "api_key"],+ credentials_types: credentialTypes,credentials_scopes: cred.scopes as string[] | undefined,};}diff --git a/autogpt_platform/frontend/src/app/(platform)/chat/components/Chat/components/ChatCredentialsSetup/ChatCredentialsSetup.tsx b/autogpt_platform/frontend/src/app/(platform)/chat/components/Chat/components/ChatCredentialsSetup/ChatCredentialsSetup.tsxindex 4b9da57286..f0dfadd1f7 100644--- a/autogpt_platform/frontend/src/app/(platform)/chat/components/Chat/components/ChatCredentialsSetup/ChatCredentialsSetup.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/chat/components/Chat/components/ChatCredentialsSetup/ChatCredentialsSetup.tsx@@ -9,7 +9,9 @@ import { useChatCredentialsSetup } from "./useChatCredentialsSetup";export interface CredentialInfo {provider: string;providerName: string;- credentialType: "api_key" | "oauth2" | "user_password" | "host_scoped";+ credentialTypes: Array<+ "api_key" | "oauth2" | "user_password" | "host_scoped"+ >;title: string;scopes?: string[];}@@ -30,7 +32,7 @@ function createSchemaFromCredentialInfo(type: "object",properties: {},credentials_provider: [credential.provider],- credentials_types: [credential.credentialType],+ credentials_types: credential.credentialTypes,credentials_scopes: credential.scopes,discriminator: undefined,discriminator_mapping: undefined,diff --git a/autogpt_platform/frontend/src/app/(platform)/chat/components/Chat/components/ChatMessage/useChatMessage.ts b/autogpt_platform/frontend/src/app/(platform)/chat/components/Chat/components/ChatMessage/useChatMessage.tsindex 9a597d4b26..5ee61bc554 100644--- a/autogpt_platform/frontend/src/app/(platform)/chat/components/Chat/components/ChatMessage/useChatMessage.ts+++ b/autogpt_platform/frontend/src/app/(platform)/chat/components/Chat/components/ChatMessage/useChatMessage.ts@@ -41,7 +41,9 @@ export type ChatMessageData =credentials: Array<{provider: string;providerName: string;- credentialType: "api_key" | "oauth2" | "user_password" | "host_scoped";+ credentialTypes: Array<+ "api_key" | "oauth2" | "user_password" | "host_scoped"+ >;title: string;scopes?: string[];}>;