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/copilot): lock the approval mode until the chat knows its session and expert (#15935)

Nicholas Tindledfec34e275ee
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/autopilot-mode-selector.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/autopilot-mode-selector.test.tsx
index 5095890dd6..063808466a 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/autopilot-mode-selector.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/autopilot-mode-selector.test.tsx
@@ -10,7 +10,8 @@ import {
assistantTextChunks,
streamSseResponse,
} from "@/tests/integrations/copilot-sse";
-import { screen, waitFor } from "@testing-library/react";
+import { focusManager } from "@tanstack/react-query";
+import { act, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { http, HttpResponse } from "msw";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -18,6 +19,7 @@ import { useAutopilotModeStore } from "../autopilotModeStore";
import { resetCopilotChatRegistry } from "../copilotChatRegistry";
import {
renderHost,
+ sessionHandler,
TEST_BACKEND_BASE_URL,
TEST_SESSION_ID,
typeAndSend,
@@ -72,6 +74,7 @@ beforeEach(() => {
afterEach(() => {
resetCopilotChatRegistry();
useAutopilotModeStore.setState({ choices: {} });
+ focusManager.setFocused(undefined);
});
// These flows take 3-4.5 s on CI's slower runners, close to the 5 s default.
@@ -304,6 +307,144 @@ describe("AutoPilot mode selector", { timeout: 15_000 }, () => {
),
).toBeDefined();
});
+
+ it("keeps approvals locked until an expert chat knows whose it is", async () => {
+ let settleRoster: () => void = () => {};
+ const roster = new Promise<void>((resolve) => {
+ settleRoster = () => resolve();
+ });
+ server.use(
+ getListExpertIdentitiesMockHandler(async () => {
+ await roster;
+ return [mariaIdentity("ask_first")];
+ }),
+ );
+ const user = userEvent.setup();
+
+ renderHost({
+ sessionOverride: {
+ expert_id: "expert-maria",
+ metadata: { dry_run: false, autopilot_mode: "ask_first" },
+ },
+ });
+
+ expect(await findModeButton(/approval mode: ask first/i)).toHaveProperty(
+ "disabled",
+ true,
+ );
+ await user.keyboard("{Escape}");
+ await waitFor(() =>
+ expect(
+ screen.queryByRole("button", { name: /approval mode/i }),
+ ).toBeNull(),
+ );
+
+ settleRoster();
+ // The composer unlocks, and takes focus, once the roster is in.
+ await waitFor(() =>
+ expect(screen.getByLabelText(/chat message input/i)).toHaveProperty(
+ "disabled",
+ false,
+ ),
+ );
+ await pickMode(/approval mode: ask first/i, /unsupervised/i);
+ expect(
+ await screen.findByText(
+ /^Maria will not ask before anything in this chat\./,
+ ),
+ ).toBeDefined();
+ });
+
+ it("keeps approvals locked until the chat's session has loaded", async () => {
+ let settleSession: () => void = () => {};
+ const sessionLoaded = new Promise<void>((resolve) => {
+ settleSession = () => resolve();
+ });
+ server.use(
+ getListExpertIdentitiesMockHandler([mariaIdentity("auto")]),
+ sessionHandler({
+ expert_id: "expert-maria",
+ metadata: { dry_run: false, autopilot_mode: "ask_first" },
+ }),
+ );
+ const user = userEvent.setup();
+
+ // Held until settled, then passed on to the session above.
+ renderHost({
+ sessionResponse: http.get(
+ "/api/proxy/api/chat/sessions/:sessionId",
+ async () => {
+ await sessionLoaded;
+ },
+ ),
+ });
+
+ expect(await findModeButton(/approval mode/i)).toHaveProperty(
+ "disabled",
+ true,
+ );
+ await user.keyboard("{Escape}");
+ await waitFor(() =>
+ expect(
+ screen.queryByRole("button", { name: /approval mode/i }),
+ ).toBeNull(),
+ );
+
+ settleSession();
+ await waitFor(() =>
+ expect(screen.getByLabelText(/chat message input/i)).toHaveProperty(
+ "disabled",
+ false,
+ ),
+ );
+ await pickMode(/approval mode: ask first/i, /unsupervised/i);
+ expect(
+ await screen.findByText(
+ /^Maria will not ask before anything in this chat\./,
+ ),
+ ).toBeDefined();
+ });
+
+ it("keeps approvals open while a known expert's roster refetches", async () => {
+ let rosterReads = 0;
+ let settleRefetch: () => void = () => {};
+ const refetched = new Promise<void>((resolve) => {
+ settleRefetch = () => resolve();
+ });
+ server.use(
+ getListExpertIdentitiesMockHandler(async () => {
+ rosterReads += 1;
+ if (rosterReads > 1) await refetched;
+ return [mariaIdentity("ask_first")];
+ }),
+ );
+
+ renderHost({
+ sessionOverride: {
+ expert_id: "expert-maria",
+ metadata: { dry_run: false, autopilot_mode: "ask_first" },
+ },
+ });
+ await waitFor(() =>
+ expect(screen.getByLabelText(/chat message input/i)).toHaveProperty(
+ "disabled",
+ false,
+ ),
+ );
+
+ // Coming back to the tab refetches stale queries, the roster included.
+ act(() => {
+ focusManager.setFocused(false);
+ focusManager.setFocused(true);
+ });
+ await waitFor(() => expect(rosterReads).toBe(2));
+
+ expect(await findModeButton(/approval mode: ask first/i)).toHaveProperty(
+ "disabled",
+ false,
+ );
+ settleRefetch();
+ });
});
describe("autopilotModeStore", () => {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
index c529800379..9759a1416a 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
@@ -270,16 +270,29 @@ export const ChatContainer = ({
: (expertIdentity?.autopilotMode ?? null);
// ChatInput is memoised, and this container renders on every streamed
// flush: each element and callback it gets keeps its identity until its
- // own inputs change, so a flush leaves the composer alone.
+ // own inputs change, so a flush leaves the composer alone. Approvals
+ // stay locked until the chat knows its session and its expert: the mode
+ // shown is the session's, and the Unsupervised confirm names the expert.
+ // A background roster refetch keeps the expert it already knows.
const modeSelector = useMemo(
() =>
isAutoModeEnabled ? (
<AutopilotModeSelector
sessionId={sessionId}
persistedMode={initialMode}
+ disabled={
+ isLoadingSession || (!!isResolvingExpertIdentity && !expertIdentity)
+ }
/>
) : undefined,
- [isAutoModeEnabled, sessionId, initialMode],
+ [
+ isAutoModeEnabled,
+ sessionId,
+ initialMode,
+ isLoadingSession,
+ isResolvingExpertIdentity,
+ expertIdentity,
+ ],
);
const silenceTimeoutMs = useVoiceSilenceTimeout();
const voice = useVoiceMode({
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/AutopilotModeSelector/AutopilotModeSelector.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/AutopilotModeSelector/AutopilotModeSelector.tsx
index 8bc6df3aed..0411e3fa80 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/AutopilotModeSelector/AutopilotModeSelector.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/AutopilotModeSelector/AutopilotModeSelector.tsx
@@ -22,9 +22,14 @@ import { useAutopilotModeSelector } from "./useAutopilotModeSelector";
interface Props {
sessionId: string | null;
persistedMode: AutopilotMode | null;
+ disabled?: boolean;
}
-export function AutopilotModeSelector({ sessionId, persistedMode }: Props) {
+export function AutopilotModeSelector({
+ sessionId,
+ persistedMode,
+ disabled = false,
+}: Props) {
const {
mode,
isDefault,
@@ -39,7 +44,7 @@ export function AutopilotModeSelector({ sessionId, persistedMode }: Props) {
return (
<>
<DropdownMenu>
- <DropdownMenuTrigger asChild>
+ <DropdownMenuTrigger asChild disabled={disabled}>
<button
type="button"
aria-label={`Approval mode: ${current.label}. Change mode`}
@@ -53,6 +58,7 @@ export function AutopilotModeSelector({ sessionId, persistedMode }: Props) {
"bg-zinc-100 text-zinc-700 hover:bg-zinc-200",
mode === "unsupervised" &&
"bg-amber-50 text-amber-700 hover:bg-amber-100",
+ disabled && "opacity-40",
)}
>
<Icon icon={current.icon} size={16} aria-hidden="true" />