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(frontend): add credential type selector for multi-auth providers (#12378)
Abhimanyu Yadav7ccfff1040e7
diff --git a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/CredentialsInput.tsx b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/CredentialsInput.tsxindex eba814fe08..53fe620f9a 100644--- a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/CredentialsInput.tsx+++ b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/CredentialsInput.tsx@@ -9,6 +9,7 @@ import { cn } from "@/lib/utils";import { toDisplayName } from "@/providers/agent-credentials/helper";import { APIKeyCredentialsModal } from "./components/APIKeyCredentialsModal/APIKeyCredentialsModal";import { CredentialsFlatView } from "./components/CredentialsFlatView/CredentialsFlatView";+import { CredentialTypeSelector } from "./components/CredentialTypeSelector/CredentialTypeSelector";import { HostScopedCredentialsModal } from "./components/HotScopedCredentialsModal/HotScopedCredentialsModal";import { OAuthFlowWaitingModal } from "./components/OAuthWaitingModal/OAuthWaitingModal";import { PasswordCredentialsModal } from "./components/PasswordCredentialsModal/PasswordCredentialsModal";@@ -70,20 +71,25 @@ export function CredentialsInput({supportsOAuth2,supportsUserPassword,supportsHostScoped,+ hasMultipleCredentialTypes,+ supportedTypes,userCredentials,systemCredentials,oAuthError,isAPICredentialsModalOpen,isUserPasswordCredentialsModalOpen,isHostScopedCredentialsModalOpen,+ isCredentialTypeSelectorOpen,isOAuth2FlowInProgress,oAuthPopupController,actionButtonText,setAPICredentialsModalOpen,setUserPasswordCredentialsModalOpen,setHostScopedCredentialsModalOpen,+ setCredentialTypeSelectorOpen,handleActionButtonClick,handleCredentialSelect,+ handleOAuthLogin,} = hookData;const displayName = toDisplayName(provider);@@ -116,7 +122,28 @@ export function CredentialsInput({{!readOnly && (<>- {supportsApiKey && (+ {hasMultipleCredentialTypes && (+ <CredentialTypeSelector+ schema={schema}+ open={isCredentialTypeSelectorOpen}+ onClose={() => setCredentialTypeSelectorOpen(false)}+ provider={provider}+ providerName={providerName}+ supportedTypes={supportedTypes}+ onCredentialsCreate={(creds) => {+ onSelectCredential(creds);+ }}+ onOAuthLogin={handleOAuthLogin}+ onOpenPasswordModal={() =>+ setUserPasswordCredentialsModalOpen(true)+ }+ onOpenHostScopedModal={() =>+ setHostScopedCredentialsModalOpen(true)+ }+ siblingInputs={siblingInputs}+ />+ )}+ {supportsApiKey && !hasMultipleCredentialTypes && (<APIKeyCredentialsModalschema={schema}open={isAPICredentialsModalOpen}diff --git a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialRow/CredentialRow.tsx b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialRow/CredentialRow.tsxindex dc69c34d93..fbf3e4c10c 100644--- a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialRow/CredentialRow.tsx+++ b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialRow/CredentialRow.tsx@@ -1,4 +1,3 @@-import { IconKey } from "@/components/__legacy__/ui/icons";import { Text } from "@/components/atoms/Text/Text";import {DropdownMenu,@@ -9,9 +8,12 @@ import {import { cn } from "@/lib/utils";import { CaretDownIcon, DotsThreeVertical } from "@phosphor-icons/react";import { useEffect, useRef, useState } from "react";+import { CredentialsType } from "@/lib/autogpt-server-api/types";import {fallbackIcon,getCredentialDisplayName,+ getCredentialTypeIcon,+ getCredentialTypeLabel,MASKED_KEY_LENGTH,providerIcons,} from "../../helpers";@@ -47,6 +49,16 @@ export function CredentialRow({variant = "default",}: CredentialRowProps) {const ProviderIcon = providerIcons[provider] || fallbackIcon;+ const isRealCredentialType = [+ "api_key",+ "oauth2",+ "user_password",+ "host_scoped",+ ].includes(credential.type);+ const credType = credential.type as CredentialsType;+ const TypeIcon = isRealCredentialType+ ? getCredentialTypeIcon(credType, provider)+ : fallbackIcon;const isNodeVariant = variant === "node";const containerRef = useRef<HTMLDivElement>(null);const [showMaskedKey, setShowMaskedKey] = useState(true);@@ -92,22 +104,29 @@ export function CredentialRow({<div className="flex h-6 w-6 shrink-0 items-center justify-center rounded-full bg-gray-900"><ProviderIcon className="h-3 w-3 text-white" /></div>- <IconKey className="h-5 w-5 shrink-0 text-zinc-800" />+ <TypeIcon className="h-5 w-5 shrink-0 text-zinc-800" /><divclassName={cn("relative flex min-w-0 flex-1 flex-nowrap items-center gap-4",isNodeVariant && "overflow-hidden",)}>- <Text- variant="body"- className={cn(- "min-w-0 flex-1 tracking-tight",- isNodeVariant ? "truncate" : "line-clamp-1 text-ellipsis",+ <div className="flex min-w-0 flex-1 items-center gap-2">+ <Text+ variant="body"+ className={cn(+ "min-w-0 shrink tracking-tight",+ isNodeVariant ? "truncate" : "line-clamp-1 text-ellipsis",+ )}+ >+ {getCredentialDisplayName(credential, displayName)}+ </Text>+ {isRealCredentialType && (+ <span className="shrink-0 rounded bg-zinc-100 px-1.5 py-0.5 text-[0.625rem] font-medium leading-tight text-zinc-500">+ {getCredentialTypeLabel(credType)}+ </span>)}- >- {getCredentialDisplayName(credential, displayName)}- </Text>+ </div>{!(asSelectTrigger && isNodeVariant) && showMaskedKey && (<Textvariant="large"diff --git a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialTypeSelector/CredentialTypeSelector.tsx b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialTypeSelector/CredentialTypeSelector.tsxnew file mode 100644index 0000000000..c659ed0fdf--- /dev/null+++ b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialTypeSelector/CredentialTypeSelector.tsx@@ -0,0 +1,298 @@+"use client";++import {+ Form,+ FormDescription,+ FormField,+} from "@/components/__legacy__/ui/form";+import { Button } from "@/components/atoms/Button/Button";+import { Input } from "@/components/atoms/Input/Input";+import { Dialog } from "@/components/molecules/Dialog/Dialog";+import {+ TabsLine,+ TabsLineContent,+ TabsLineList,+ TabsLineTrigger,+} from "@/components/molecules/TabsLine/TabsLine";+import { Skeleton } from "@/components/atoms/Skeleton/Skeleton";+import {+ BlockIOCredentialsSubSchema,+ CredentialsMetaInput,+ CredentialsType,+} from "@/lib/autogpt-server-api/types";+import { useAPIKeyCredentialsModal } from "../APIKeyCredentialsModal/useAPIKeyCredentialsModal";+import { getCredentialTypeIcon, getCredentialTypeLabel } from "../../helpers";++type Props = {+ schema: BlockIOCredentialsSubSchema;+ open: boolean;+ onClose: () => void;+ provider: string;+ providerName: string;+ supportedTypes: CredentialsType[];+ onCredentialsCreate: (creds: CredentialsMetaInput) => void;+ onOAuthLogin: () => void;+ onOpenPasswordModal: () => void;+ onOpenHostScopedModal: () => void;+ siblingInputs?: Record<string, unknown>;+};++export function CredentialTypeSelector({+ schema,+ open,+ onClose,+ provider,+ providerName,+ supportedTypes,+ onCredentialsCreate,+ onOAuthLogin,+ onOpenPasswordModal,+ onOpenHostScopedModal,+ siblingInputs,+}: Props) {+ const defaultTab = supportedTypes[0];++ return (+ <Dialog+ title={`Add credential for ${providerName}`}+ controlled={{+ isOpen: open,+ set: (isOpen) => {+ if (!isOpen) onClose();+ },+ }}+ onClose={onClose}+ styling={{ maxWidth: "28rem" }}+ >+ <Dialog.Content>+ <TabsLine defaultValue={defaultTab}>+ <TabsLineList>+ {supportedTypes.map((type) => {+ const Icon = getCredentialTypeIcon(type, provider);+ return (+ <TabsLineTrigger+ key={type}+ value={type}+ className="inline-flex items-center gap-1.5"+ >+ <Icon size={16} />+ {getCredentialTypeLabel(type)}+ </TabsLineTrigger>+ );+ })}+ </TabsLineList>++ {supportedTypes.includes("oauth2") && (+ <TabsLineContent value="oauth2">+ <OAuthTabContent+ providerName={providerName}+ onOAuthLogin={() => {+ onClose();+ onOAuthLogin();+ }}+ />+ </TabsLineContent>+ )}++ {supportedTypes.includes("api_key") && (+ <TabsLineContent value="api_key">+ <APIKeyTabContent+ schema={schema}+ siblingInputs={siblingInputs}+ onCredentialsCreate={(creds) => {+ onCredentialsCreate(creds);+ onClose();+ }}+ />+ </TabsLineContent>+ )}++ {supportedTypes.includes("user_password") && (+ <TabsLineContent value="user_password">+ <SimpleActionTab+ description="Add a username and password credential."+ buttonLabel="Enter credentials"+ onClick={() => {+ onClose();+ onOpenPasswordModal();+ }}+ />+ </TabsLineContent>+ )}++ {supportedTypes.includes("host_scoped") && (+ <TabsLineContent value="host_scoped">+ <SimpleActionTab+ description="Add host-scoped headers for authentication."+ buttonLabel="Add headers"+ onClick={() => {+ onClose();+ onOpenHostScopedModal();+ }}+ />+ </TabsLineContent>+ )}+ </TabsLine>+ </Dialog.Content>+ </Dialog>+ );+}++type OAuthTabContentProps = {+ providerName: string;+ onOAuthLogin: () => void;+};++function OAuthTabContent({ providerName, onOAuthLogin }: OAuthTabContentProps) {+ return (+ <div className="space-y-4">+ <p className="text-sm text-zinc-600">+ Sign in with your {providerName} account using OAuth.+ </p>+ <Button+ variant="primary"+ size="small"+ onClick={onOAuthLogin}+ type="button"+ >+ Sign in with {providerName}+ </Button>+ </div>+ );+}++type APIKeyTabContentProps = {+ schema: BlockIOCredentialsSubSchema;+ siblingInputs?: Record<string, unknown>;+ onCredentialsCreate: (creds: CredentialsMetaInput) => void;+};++function APIKeyTabContent({+ schema,+ siblingInputs,+ onCredentialsCreate,+}: APIKeyTabContentProps) {+ const {+ form,+ isLoading,+ isSubmitting,+ supportsApiKey,+ schemaDescription,+ onSubmit,+ } = useAPIKeyCredentialsModal({ schema, siblingInputs, onCredentialsCreate });++ if (!supportsApiKey && !isLoading) {+ return null;+ }++ if (isLoading) {+ return (+ <div className="space-y-4">+ <Skeleton className="h-4 w-3/4" />+ <div className="space-y-2">+ <Skeleton className="h-9 w-full" />+ <Skeleton className="h-9 w-full" />+ <Skeleton className="h-9 w-full" />+ <Skeleton className="h-9 w-68" />+ </div>+ </div>+ );+ }++ return (+ <div className="space-y-4">+ {schemaDescription && (+ <p className="text-sm text-zinc-600">{schemaDescription}</p>+ )}++ <Form {...form}>+ <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2">+ <FormField+ control={form.control}+ name="title"+ render={({ field }) => (+ <Input+ id="title"+ label="Name"+ type="text"+ placeholder="Enter a name for this API Key..."+ {...field}+ />+ )}+ />+ <FormField+ control={form.control}+ name="apiKey"+ render={({ field }) => (+ <Input+ id="apiKey"+ label="API Key"+ type="password"+ placeholder="Enter API Key..."+ hint={+ schema.credentials_scopes ? (+ <FormDescription>+ Required scope(s) for this block:{" "}+ {schema.credentials_scopes?.map((s, i, a) => (+ <span key={i}>+ <code className="text-xs font-bold">{s}</code>+ {i < a.length - 1 && ", "}+ </span>+ ))}+ </FormDescription>+ ) : null+ }+ {...field}+ />+ )}+ />+ <FormField+ control={form.control}+ name="expiresAt"+ render={({ field }) => (+ <Input+ id="expiresAt"+ label="Expiration Date"+ type="datetime-local"+ placeholder="Select expiration date..."+ value={field.value}+ onChange={(e) => field.onChange(e.target.value)}+ onBlur={field.onBlur}+ name={field.name}+ />+ )}+ />+ <Button+ type="submit"+ className="min-w-68"+ loading={isSubmitting}+ disabled={isSubmitting}+ >+ Add API Key+ </Button>+ </form>+ </Form>+ </div>+ );+}++type SimpleActionTabProps = {+ description: string;+ buttonLabel: string;+ onClick: () => void;+};++function SimpleActionTab({+ description,+ buttonLabel,+ onClick,+}: SimpleActionTabProps) {+ return (+ <div className="space-y-4">+ <p className="text-sm text-zinc-600">{description}</p>+ <Button variant="primary" size="small" onClick={onClick} type="button">+ {buttonLabel}+ </Button>+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsSelect/CredentialsSelect.tsx b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsSelect/CredentialsSelect.tsxindex 18e772dd00..3cca95267f 100644--- a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsSelect/CredentialsSelect.tsx+++ b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/components/CredentialsSelect/CredentialsSelect.tsx@@ -1,5 +1,9 @@import { CredentialsMetaInput } from "@/app/api/__generated__/models/credentialsMetaInput";-import { getCredentialDisplayName } from "../../helpers";+import { CredentialsType } from "@/lib/autogpt-server-api/types";+import {+ getCredentialDisplayName,+ getCredentialTypeLabel,+} from "../../helpers";import { CredentialRow } from "../CredentialRow/CredentialRow";interface Props {@@ -86,7 +90,8 @@ export function CredentialsSelect({)}{credentials.map((credential) => (<option key={credential.id} value={credential.id}>- {getCredentialDisplayName(credential, displayName)}+ {getCredentialDisplayName(credential, displayName)} (+ {getCredentialTypeLabel(credential.type as CredentialsType)})</option>))}</select>diff --git a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/helpers.ts b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/helpers.tsindex 57653f3804..d4f23cee39 100644--- a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/helpers.ts+++ b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/helpers.ts@@ -1,4 +1,4 @@-import { KeyIcon } from "@phosphor-icons/react";+import { GlobeSimple, KeyIcon, Lock, Password } from "@phosphor-icons/react";import { NotionLogoIcon } from "@radix-ui/react-icons";import {FaDiscord,@@ -8,6 +8,7 @@ import {FaMedium,FaTwitter,} from "react-icons/fa";+import { CredentialsType } from "@/lib/autogpt-server-api/types";export const fallbackIcon = KeyIcon;@@ -68,6 +69,62 @@ export type OAuthPopupResultMessage = { message_type: "oauth_popup_result" } & (});+export function countSupportedTypes(+ supportsOAuth2: boolean,+ supportsApiKey: boolean,+ supportsUserPassword: boolean,+ supportsHostScoped: boolean,+): number {+ return [+ supportsOAuth2,+ supportsApiKey,+ supportsUserPassword,+ supportsHostScoped,+ ].filter(Boolean).length;+}++export function getSupportedTypes(+ supportsOAuth2: boolean,+ supportsApiKey: boolean,+ supportsUserPassword: boolean,+ supportsHostScoped: boolean,+): CredentialsType[] {+ const types: CredentialsType[] = [];+ if (supportsOAuth2) types.push("oauth2");+ if (supportsApiKey) types.push("api_key");+ if (supportsUserPassword) types.push("user_password");+ if (supportsHostScoped) types.push("host_scoped");+ return types;+}++const CREDENTIAL_TYPE_LABELS: Record<CredentialsType, string> = {+ oauth2: "OAuth",+ api_key: "API Key",+ user_password: "Password",+ host_scoped: "Headers",+};++export function getCredentialTypeLabel(type: CredentialsType): string {+ return CREDENTIAL_TYPE_LABELS[type] ?? type;+}++type CredentialIcon = React.FC<{ className?: string; size?: string | number }>;++export function getCredentialTypeIcon(+ type: CredentialsType,+ provider?: string,+): CredentialIcon {+ if (type === "oauth2" && provider) {+ const icon = providerIcons[provider];+ if (icon) return icon as CredentialIcon;+ return GlobeSimple as CredentialIcon;+ }+ if (type === "api_key") return KeyIcon as CredentialIcon;+ if (type === "user_password") return Password as CredentialIcon;+ if (type === "host_scoped") return Lock as CredentialIcon;+ return KeyIcon as CredentialIcon;+}+export function getActionButtonText(supportsOAuth2: boolean,supportsApiKey: boolean,@@ -75,6 +132,18 @@ export function getActionButtonText(supportsHostScoped: boolean,hasExistingCredentials: boolean,): string {+ const multipleTypes =+ countSupportedTypes(+ supportsOAuth2,+ supportsApiKey,+ supportsUserPassword,+ supportsHostScoped,+ ) > 1;++ if (multipleTypes) {+ return hasExistingCredentials ? "Add another credential" : "Add credential";+ }+if (hasExistingCredentials) {if (supportsOAuth2) return "Connect another account";if (supportsApiKey) return "Use a new API key";diff --git a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/useCredentialsInput.ts b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/useCredentialsInput.tsindex 9ab2e08141..60ac72304c 100644--- a/autogpt_platform/frontend/src/components/contextual/CredentialsInput/useCredentialsInput.ts+++ b/autogpt_platform/frontend/src/components/contextual/CredentialsInput/useCredentialsInput.ts@@ -10,8 +10,10 @@ import { openOAuthPopup } from "@/lib/oauth-popup";import { useQueryClient } from "@tanstack/react-query";import { useEffect, useRef, useState } from "react";import {+ countSupportedTypes,filterSystemCredentials,getActionButtonText,+ getSupportedTypes,getSystemCredentials,} from "./helpers";@@ -44,6 +46,8 @@ export function useCredentialsInput({] = useState(false);const [isHostScopedCredentialsModalOpen, setHostScopedCredentialsModalOpen] =useState(false);+ const [isCredentialTypeSelectorOpen, setCredentialTypeSelectorOpen] =+ useState(false);const [isOAuth2FlowInProgress, setOAuth2FlowInProgress] = useState(false);const [oAuthPopupController, setOAuthPopupController] =useState<AbortController | null>(null);@@ -263,7 +267,27 @@ export function useCredentialsInput({}}+ const hasMultipleCredentialTypes =+ countSupportedTypes(+ supportsOAuth2,+ supportsApiKey,+ supportsUserPassword,+ supportsHostScoped,+ ) > 1;++ const supportedTypes = getSupportedTypes(+ supportsOAuth2,+ supportsApiKey,+ supportsUserPassword,+ supportsHostScoped,+ );+function handleActionButtonClick() {+ if (hasMultipleCredentialTypes) {+ setCredentialTypeSelectorOpen(true);+ return;+ }+if (supportsOAuth2) {handleOAuthLogin();} else if (supportsApiKey) {@@ -308,6 +332,8 @@ export function useCredentialsInput({supportsOAuth2,supportsUserPassword,supportsHostScoped,+ hasMultipleCredentialTypes,+ supportedTypes,isSystemProvider,userCredentials,systemCredentials,@@ -317,6 +343,7 @@ export function useCredentialsInput({isAPICredentialsModalOpen,isUserPasswordCredentialsModalOpen,isHostScopedCredentialsModalOpen,+ isCredentialTypeSelectorOpen,isOAuth2FlowInProgress,oAuthPopupController,credentialToDelete,@@ -331,6 +358,7 @@ export function useCredentialsInput({setAPICredentialsModalOpen,setUserPasswordCredentialsModalOpen,setHostScopedCredentialsModalOpen,+ setCredentialTypeSelectorOpen,setCredentialToDelete,handleActionButtonClick,handleCredentialSelect,