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): pass isGraphLoaded from Flow.tsx + Escape key containment check

Zamil Majdy5d0330615f9f
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/BuilderChatPanel.tsx b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/BuilderChatPanel.tsx
index 733bbf3dd3..26db817375 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/BuilderChatPanel.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/BuilderChatPanel.tsx
@@ -23,10 +23,16 @@ import { useBuilderChatPanel } from "./useBuilderChatPanel";
interface Props {
className?: string;
+ isGraphLoaded?: boolean;
onGraphEdited?: () => void;
}
-export function BuilderChatPanel({ className, onGraphEdited }: Props) {
+export function BuilderChatPanel({
+ className,
+ isGraphLoaded,
+ onGraphEdited,
+}: Props) {
+ const panelRef = useRef<HTMLDivElement>(null);
const {
isOpen,
handleToggle,
@@ -48,7 +54,7 @@ export function BuilderChatPanel({ className, onGraphEdited }: Props) {
handleKeyDown,
isStreaming,
canSend,
- } = useBuilderChatPanel({ onGraphEdited });
+ } = useBuilderChatPanel({ isGraphLoaded, onGraphEdited, panelRef });
const messagesEndRef = useRef<HTMLDivElement>(null);
const textareaRef = useRef<HTMLTextAreaElement>(null);
@@ -73,6 +79,7 @@ export function BuilderChatPanel({ className, onGraphEdited }: Props) {
>
{isOpen && (
<div
+ ref={panelRef}
role="complementary"
aria-label="Builder chat panel"
className="pointer-events-auto flex h-[70vh] w-96 max-w-[calc(100vw-2rem)] flex-col overflow-hidden rounded-xl border border-slate-200 bg-white shadow-2xl"
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/BuilderChatPanel.test.tsx b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/BuilderChatPanel.test.tsx
index 3c7a4c1cbc..5730a9ef2f 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/BuilderChatPanel.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/BuilderChatPanel.test.tsx
@@ -341,13 +341,14 @@ describe("BuilderChatPanel", () => {
expect(screen.queryByLabelText("Undo last applied change")).toBeNull();
});
- it("passes onGraphEdited to useBuilderChatPanel", () => {
+ it("passes onGraphEdited and isGraphLoaded to useBuilderChatPanel", () => {
const onGraphEdited = vi.fn();
- render(<BuilderChatPanel onGraphEdited={onGraphEdited} />);
- expect(mockUseBuilderChatPanel).toHaveBeenCalledWith({
- isGraphLoaded: undefined,
- onGraphEdited,
- });
+ render(
+ <BuilderChatPanel onGraphEdited={onGraphEdited} isGraphLoaded={true} />,
+ );
+ expect(mockUseBuilderChatPanel).toHaveBeenCalledWith(
+ expect.objectContaining({ isGraphLoaded: true, onGraphEdited }),
+ );
});
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
index ef1be41dfe..0ba5542bce 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
@@ -7,6 +7,7 @@ import { DefaultChatTransport } from "ai";
import { MarkerType } from "@xyflow/react";
import {
type KeyboardEvent,
+ type RefObject,
useEffect,
useMemo,
useRef,
@@ -53,6 +54,7 @@ export function clearGraphSessionCacheForTesting() {
interface UseBuilderChatPanelArgs {
isGraphLoaded?: boolean;
onGraphEdited?: () => void;
+ panelRef?: RefObject<HTMLElement | null>;
}
/**
@@ -75,6 +77,7 @@ interface UseBuilderChatPanelArgs {
export function useBuilderChatPanel({
isGraphLoaded = false,
onGraphEdited,
+ panelRef,
}: UseBuilderChatPanelArgs = {}) {
const [isOpen, setIsOpen] = useState(false);
const [sessionId, setSessionId] = useState<string | null>(null);
@@ -282,15 +285,23 @@ export function useBuilderChatPanel({
}
}, [messages, status, onGraphEdited, setQueryStates]);
- // Close the panel on Escape so keyboard users can dismiss it quickly.
+ // Close the panel on Escape when focus is inside the panel, so pressing Escape
+ // in another dialog or canvas element does not accidentally close the chat panel.
useEffect(() => {
if (!isOpen) return;
function onKeyDown(e: globalThis.KeyboardEvent) {
- if (e.key === "Escape") setIsOpen(false);
+ if (e.key !== "Escape") return;
+ if (
+ panelRef &&
+ panelRef.current &&
+ !panelRef.current.contains(e.target as Node)
+ )
+ return;
+ setIsOpen(false);
}
document.addEventListener("keydown", onKeyDown);
return () => document.removeEventListener("keydown", onKeyDown);
- }, [isOpen]);
+ }, [isOpen, panelRef]);
const isStreaming = status === "streaming" || status === "submitted";
const canSend =
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/Flow.tsx b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/Flow.tsx
index f21869b50f..3a55fabf1d 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/Flow.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/Flow.tsx
@@ -139,7 +139,10 @@ export const Flow = () => {
graphId={flowID || undefined}
/>
{isBuilderChatEnabled && (
- <BuilderChatPanel onGraphEdited={() => void refetchGraph()} />
+ <BuilderChatPanel
+ isGraphLoaded={isInitialLoadComplete}
+ onGraphEdited={() => void refetchGraph()}
+ />
)}
</div>
);