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(frontend): give a second card the credential the expert was granted (#14618)

Reinier van der Leerb11d6f939489
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/ConnectorRow.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/ConnectorRow.tsx
index 0b0e448eee..58334e2f42 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/ConnectorRow.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/ConnectorRow.tsx
@@ -147,9 +147,14 @@ export function ConnectorRow({ row }: Props) {
...(connected ? [connected] : []),
...(expertGrant?.credentials ?? []).filter((c) => c.id !== connected?.id),
];
- const isSatisfied = expertGrant
- ? grantedCredentials.isSelectionGranted
- : Boolean(row.selected);
+ // A merged card whose own field is still empty leaves the row unanswered,
+ // whatever the first target holds — saying "Granted" there claims a run can
+ // proceed that cannot.
+ const isSatisfied =
+ !row.hasUnansweredTarget &&
+ (expertGrant
+ ? grantedCredentials.isSelectionGranted
+ : Boolean(row.selected));
function openDialog() {
knownIds.current = allProviders
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/ConnectorRow.expert.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/ConnectorRow.expert.test.tsx
index 1aae7b2564..34b363858b 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/ConnectorRow.expert.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/ConnectorRow.expert.test.tsx
@@ -1,4 +1,5 @@
import type { CredentialsMetaResponse } from "@/app/api/__generated__/models/credentialsMetaResponse";
+import type { CredentialsMetaInput } from "@/lib/autogpt-server-api/types";
import type { ExistingCredentialsOffer } from "@/components/contextual/CredentialsInput/components/ConnectCredentialDialog/helpers";
import { CredentialsProvidersContext } from "@/providers/agent-credentials/credentials-provider";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
@@ -12,7 +13,11 @@ import {
import { useState, type ReactElement, type ReactNode } from "react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { ConnectorRow } from "../ConnectorRow";
-import type { ConnectorRow as Row } from "../helpers";
+import {
+ toConnectorRows,
+ type ConnectorRequest,
+ type ConnectorRow as Row,
+} from "../helpers";
const mockGrant = vi.fn();
// What the mocked dialog reports as the credential a sign-in produced.
@@ -192,6 +197,7 @@ function row(overrides: Partial<Row> = {}): Row {
description: "Connect your GitHub account",
schema: { credentials_provider: ["github"], credentials_types: ["oauth2"] },
selected: undefined,
+ hasUnansweredTarget: false,
select: vi.fn(),
onConnected: vi.fn(),
...overrides,
@@ -216,6 +222,134 @@ function StatefulRow({ current }: { current: Row }) {
);
}
+/** The chain as the card really builds it: one row per provider, answering
+ * every request that asked for it, with the values fed back through state. */
+function Chain({
+ ids,
+ onWrite,
+}: {
+ ids: string[];
+ onWrite: (id: string, value?: CredentialsMetaInput) => void;
+}) {
+ const [values, setValues] = useState<
+ Record<string, CredentialsMetaInput | undefined>
+ >({});
+ const requests: ConnectorRequest[] = ids.map((id) => ({
+ id,
+ fields: [
+ [
+ "credentials",
+ {
+ credentials_provider: ["github"],
+ credentials_types: ["oauth2"],
+ expert_grant: { expertId: "expert-a", credentials: [] },
+ },
+ ],
+ ],
+ selected: { credentials: values[id] },
+ onChange: (key: string, value?: CredentialsMetaInput) => {
+ onWrite(id, value);
+ setValues((current) => ({ ...current, [id]: value }));
+ },
+ onConnected: vi.fn(),
+ }));
+ return (
+ <>
+ {toConnectorRows(requests, []).map((row) => (
+ <ConnectorRow key={row.provider} row={row} />
+ ))}
+ </>
+ );
+}
+
+describe("a second card asking for a provider the row already answered", () => {
+ it("gives the new card the credential the expert was granted", async () => {
+ // #14615, from the screenshot: the row shows the provider as granted from
+ // the first card, the second card's field stays empty, and the run it
+ // belongs to reports the credential as not shared with the expert.
+ serverGrants = ["spare-cred"];
+ const onWrite = vi.fn();
+ const { rerender } = render(
+ <CredentialsProvidersContext.Provider value={providersWithGithub()}>
+ <Chain ids={["first"]} onWrite={onWrite} />
+ </CredentialsProvidersContext.Provider>,
+ );
+ await waitFor(() =>
+ expect(onWrite).toHaveBeenCalledWith(
+ "first",
+ expect.objectContaining({ id: "spare-cred" }),
+ ),
+ );
+
+ rerender(
+ <CredentialsProvidersContext.Provider value={providersWithGithub()}>
+ <Chain ids={["first", "second"]} onWrite={onWrite} />
+ </CredentialsProvidersContext.Provider>,
+ );
+
+ await waitFor(() =>
+ expect(onWrite).toHaveBeenCalledWith(
+ "second",
+ expect.objectContaining({ id: "spare-cred" }),
+ ),
+ );
+ });
+
+ it("does not read Granted while the new card is unanswered", async () => {
+ serverGrants = ["spare-cred"];
+ const row = toConnectorRows(
+ [
+ {
+ id: "first",
+ fields: [
+ [
+ "credentials",
+ {
+ credentials_provider: ["github"],
+ credentials_types: ["oauth2"],
+ expert_grant: { expertId: "expert-a", credentials: [] },
+ },
+ ],
+ ],
+ selected: {
+ credentials: {
+ id: "spare-cred",
+ provider: "github",
+ type: "oauth2",
+ title: "GH spare",
+ },
+ },
+ onChange: vi.fn(),
+ onConnected: vi.fn(),
+ },
+ {
+ id: "second",
+ fields: [
+ [
+ "credentials",
+ {
+ credentials_provider: ["github"],
+ credentials_types: ["oauth2"],
+ expert_grant: { expertId: "expert-a", credentials: [] },
+ },
+ ],
+ ],
+ selected: {},
+ onChange: vi.fn(),
+ onConnected: vi.fn(),
+ },
+ ],
+ [],
+ )[0];
+ render(
+ <CredentialsProvidersContext.Provider value={providersWithGithub()}>
+ <ConnectorRow row={row} />
+ </CredentialsProvidersContext.Provider>,
+ );
+ expect(screen.queryByText("Granted")).toBeNull();
+ });
+});
+
describe("ConnectorRow in an expert chat", () => {
it("restores a persisted expert grant without triggering another run", async () => {
serverGrants = ["spare-cred"];
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/helpers.expertGrant.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/helpers.expertGrant.test.ts
index cac3e33061..abbff03a0e 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/helpers.expertGrant.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/helpers.expertGrant.test.ts
@@ -27,6 +27,53 @@ function request(
};
}
+describe("a merged row reports whether every card it answers is answered", () => {
+ const picked = {
+ id: "cred-1",
+ provider: "github",
+ type: "oauth2" as const,
+ title: "GH",
+ };
+
+ function withSelection(
+ id: string,
+ selected: ConnectorRequest["selected"],
+ ): ConnectorRequest {
+ return { ...request(id, []), selected };
+ }
+
+ it("is unanswered while a second card's own field is still empty", () => {
+ // The row reports the FIRST target's value, so without this it reads as
+ // answered and the second card sits blocked on a credential it has.
+ const rows = toConnectorRows(
+ [withSelection("a", { credentials: picked }), withSelection("b", {})],
+ [],
+ );
+ expect(rows).toHaveLength(1);
+ expect(rows[0].selected?.id).toBe("cred-1");
+ expect(rows[0].hasUnansweredTarget).toBe(true);
+ });
+
+ it("is answered once both cards hold the same credential", () => {
+ const rows = toConnectorRows(
+ [
+ withSelection("a", { credentials: picked }),
+ withSelection("b", { credentials: picked }),
+ ],
+ [],
+ );
+ expect(rows[0].hasUnansweredTarget).toBe(false);
+ });
+
+ it("is answered when nothing is picked yet", () => {
+ const rows = toConnectorRows(
+ [withSelection("a", {}), withSelection("b", {})],
+ [],
+ );
+ expect(rows[0].hasUnansweredTarget).toBe(false);
+ });
+});
+
describe("expert grant candidates on merged connector rows", () => {
it("offers only accounts eligible for every merged requirement", () => {
const shared = { id: "both", title: "Both", type: "oauth2" };
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/helpers.ts
index eba937f860..71c5354a6a 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/helpers.ts
@@ -81,6 +81,10 @@ export interface ConnectorRow {
* expert has been granted it, so the connect dialog offers the account's
* existing credentials first and a freshly connected one is granted. */
expertGrant?: ExpertGrant;
+ /** A merged request whose own field does not hold `selected`. The row
+ * reports the FIRST target's value, so without this a second card asking
+ * for the same provider reads as answered while its field is still empty. */
+ hasUnansweredTarget: boolean;
}
/** Flattens every request into one row per provider: two tools asking for
@@ -140,6 +144,9 @@ export function toConnectorRows(
schema: row.schema,
selected: row.selected,
expertGrant: row.expertGrant,
+ hasUnansweredTarget: row.targets.some(
+ ({ request, key }) => request.selected[key]?.id !== row.selected?.id,
+ ),
select: (value?: CredentialsMetaInput) =>
row.targets.forEach(({ request, key }) => request.onChange(key, value)),
onConnected: () =>
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/useExpertCredentialSelection.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/useExpertCredentialSelection.ts
index 28c17540fc..b6606c39a6 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/useExpertCredentialSelection.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/useExpertCredentialSelection.ts
@@ -9,7 +9,7 @@ import { findSavedUserCredentialByProviderAndType } from "@/components/contextua
import type { CredentialsMetaInput } from "@/lib/autogpt-server-api/types";
import type { CredentialsProvidersContextType } from "@/providers/agent-credentials/credentials-provider";
import { useQueryClient } from "@tanstack/react-query";
-import { useEffect } from "react";
+import { useEffect, useRef } from "react";
import type { ConnectorRow } from "./helpers";
export function useExpertCredentialSelection(
@@ -80,25 +80,38 @@ export function useExpertCredentialSelection(
hasGrant && !grants.isError && grants.data && keepsSelection,
);
+ // `select` writes every merged target at once, so the effect has to run
+ // again when a second card joins the row: the row reports the first
+ // target's value, so it still reads as settled while the new field is
+ // empty, and the run that field belongs to is blocked on a credential the
+ // expert already holds.
+ const { hasUnansweredTarget } = row;
+ const rowRef = useRef(row);
+ rowRef.current = row;
+
useEffect(() => {
if (!hasGrant || !grants.data || !providers || grants.isFetching) return;
- if (keepsSelection || selectedID === hydrated?.id) return;
- row.select(
- hydrated
- ? {
- id: hydrated.id,
- provider: row.provider,
- type: hydrated.type as CredentialsMetaInput["type"],
- title: hydrated.title ?? undefined,
- }
- : undefined,
- );
- // eslint-disable-next-line react-hooks/exhaustive-deps -- row is rebuilt each render by the card; track the fields it reads
+ const settled = keepsSelection || selectedID === hydrated?.id;
+ if (settled && !hasUnansweredTarget) return;
+ const current = rowRef.current;
+ // A kept selection is written as-is: `hydrated` is undefined precisely
+ // when the current one stays, so writing that would clear the row.
+ const value = keepsSelection
+ ? current.selected
+ : hydrated && {
+ id: hydrated.id,
+ provider: current.provider,
+ type: hydrated.type as CredentialsMetaInput["type"],
+ title: hydrated.title ?? undefined,
+ };
+ current.select(value ?? undefined);
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- the row is read through rowRef; these are the values that decide whether to write
}, [
hasGrant,
selectedID,
hydrated?.id,
keepsSelection,
+ hasUnansweredTarget,
grants.data,
grants.isFetching,
providers,