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/builder): address chat panel review comments

Zamil Majdy817b80a1983d
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 7420b189a6..4f09292b8d 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
@@ -10,6 +10,7 @@ import {
X,
} from "@phosphor-icons/react";
import { KeyboardEvent, useRef, useState } from "react";
+import type { CustomNode } from "../FlowEditor/nodes/CustomNode/CustomNode";
import { GraphAction } from "./helpers";
import { useBuilderChatPanel } from "./useBuilderChatPanel";
@@ -26,6 +27,8 @@ export function BuilderChatPanel({ className }: Props) {
stop,
status,
isCreatingSession,
+ sessionError,
+ nodes,
parsedActions,
handleApplyAction,
} = useBuilderChatPanel();
@@ -65,6 +68,8 @@ export function BuilderChatPanel({ className }: Props) {
<MessageList
messages={messages}
isCreatingSession={isCreatingSession}
+ sessionError={sessionError}
+ nodes={nodes}
parsedActions={parsedActions}
onApplyAction={handleApplyAction}
messagesEndRef={messagesEndRef}
@@ -116,6 +121,8 @@ function PanelHeader({ onClose }: { onClose: () => void }) {
interface MessageListProps {
messages: ReturnType<typeof useBuilderChatPanel>["messages"];
isCreatingSession: boolean;
+ sessionError: boolean;
+ nodes: CustomNode[];
parsedActions: GraphAction[];
onApplyAction: (action: GraphAction) => void;
messagesEndRef: React.RefObject<HTMLDivElement>;
@@ -124,6 +131,8 @@ interface MessageListProps {
function MessageList({
messages,
isCreatingSession,
+ sessionError,
+ nodes,
parsedActions,
onApplyAction,
messagesEndRef,
@@ -137,6 +146,12 @@ function MessageList({
</div>
)}
+ {sessionError && (
+ <div className="rounded-lg border border-red-100 bg-red-50 px-3 py-2 text-xs text-red-600">
+ Failed to start chat session. Please close and try again.
+ </div>
+ )}
+
{messages.map((msg) => {
const textParts = msg.parts
.filter(
@@ -167,13 +182,20 @@ function MessageList({
<p className="text-xs font-medium text-violet-700">
Suggested changes
</p>
- {parsedActions.map((action, i) => (
- <ActionItem
- key={i}
- action={action}
- onApply={() => onApplyAction(action)}
- />
- ))}
+ {parsedActions.map((action) => {
+ const key =
+ action.type === "update_node_input"
+ ? `${action.nodeId}:${action.key}`
+ : `${action.source}->${action.target}`;
+ return (
+ <ActionItem
+ key={key}
+ action={action}
+ nodes={nodes}
+ onApply={() => onApplyAction(action)}
+ />
+ );
+ })}
</div>
)}
@@ -184,9 +206,11 @@ function MessageList({
function ActionItem({
action,
+ nodes,
onApply,
}: {
action: GraphAction;
+ nodes: CustomNode[];
onApply: () => void;
}) {
const [applied, setApplied] = useState(false);
@@ -196,10 +220,13 @@ function ActionItem({
setApplied(true);
}
+ const nodeName = (id: string) =>
+ nodes.find((n) => n.id === id)?.data.title ?? id;
+
const label =
action.type === "update_node_input"
- ? `Set node ${action.nodeId} "${action.key}" = ${JSON.stringify(action.value)}`
- : `Connect node ${action.source} → ${action.target}`;
+ ? `Set "${nodeName(action.nodeId)}" "${action.key}" = ${JSON.stringify(action.value)}`
+ : `Connect "${nodeName(action.source)}" → "${nodeName(action.target)}"`;
return (
<div className="flex items-start justify-between gap-2 rounded bg-white p-2 text-xs shadow-sm">
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 bce315bbcc..01fe08ad8d 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
@@ -30,7 +30,9 @@ function makeMockHook(
stop: vi.fn(),
status: "ready",
isCreatingSession: false,
+ sessionError: false,
sessionId: null,
+ nodes: [],
parsedActions: [],
handleApplyAction: vi.fn(),
...overrides,
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 6d201b8ed1..f7c0f1f41f 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
@@ -19,6 +19,7 @@ export function useBuilderChatPanel() {
const [isOpen, setIsOpen] = useState(false);
const [sessionId, setSessionId] = useState<string | null>(null);
const [isCreatingSession, setIsCreatingSession] = useState(false);
+ const [sessionError, setSessionError] = useState(false);
const initializedRef = useRef(false);
const sendMessageRef = useRef<SendMessageFn | null>(null);
@@ -28,7 +29,7 @@ export function useBuilderChatPanel() {
const addEdge = useEdgeStore(useShallow((s) => s.addEdge));
useEffect(() => {
- if (!isOpen || sessionId || isCreatingSession) return;
+ if (!isOpen || sessionId || isCreatingSession || sessionError) return;
async function createSession() {
setIsCreatingSession(true);
@@ -36,14 +37,18 @@ export function useBuilderChatPanel() {
const res = await postV2CreateSession(null);
if (res.status === 200) {
setSessionId(res.data.id);
+ } else {
+ setSessionError(true);
}
+ } catch {
+ setSessionError(true);
} finally {
setIsCreatingSession(false);
}
}
createSession();
- }, [isOpen, sessionId, isCreatingSession]);
+ }, [isOpen, sessionId, isCreatingSession, sessionError]);
const transport = useMemo(
() =>
@@ -93,7 +98,7 @@ export function useBuilderChatPanel() {
sendMessageRef.current?.({
text: `I'm building an agent in the AutoGPT flow builder. Here's the current graph:\n\n${summary}\n\nWhat does this agent do?`,
});
- }, [sessionId, transport, nodes, edges]);
+ }, [sessionId, transport]);
function handleToggle() {
setIsOpen((o) => !o);
@@ -142,7 +147,9 @@ export function useBuilderChatPanel() {
stop,
status,
isCreatingSession,
+ sessionError,
sessionId,
+ nodes,
parsedActions,
handleApplyAction,
};