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): surface AI graph edits and auto-refresh canvas

Zamil Majdy0999739d19ca
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 ed1510756d..dee945112c 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
@@ -181,7 +181,7 @@ function MessageList({
{parsedActions.length > 0 && (
<div className="space-y-2 rounded-lg border border-violet-100 bg-violet-50 p-3">
<p className="text-xs font-medium text-violet-700">
- Suggested changes
+ AI applied these changes
</p>
{parsedActions.map((action) => {
const key =
@@ -214,7 +214,7 @@ function ActionItem({
nodes: CustomNode[];
onApply: () => void;
}) {
- const [applied, setApplied] = useState(false);
+ const [applied, setApplied] = useState(true);
function handleApply() {
onApply();
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 3850aa57b8..1de5b98cbc 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
@@ -103,7 +103,7 @@ describe("BuilderChatPanel", () => {
expect(screen.getByText("This agent searches the web.")).toBeDefined();
});
- it("renders suggested actions with Apply buttons when parsedActions are present", () => {
+ it("renders applied actions section when parsedActions are present", () => {
mockUseBuilderChatPanel.mockReturnValue(
makeMockHook({
isOpen: true,
@@ -118,11 +118,11 @@ describe("BuilderChatPanel", () => {
}),
);
render(<BuilderChatPanel />);
- expect(screen.getByText("Suggested changes")).toBeDefined();
- expect(screen.getByText("Apply")).toBeDefined();
+ expect(screen.getByText("AI applied these changes")).toBeDefined();
+ expect(screen.getByText("Applied")).toBeDefined();
});
- it("calls handleApplyAction when Apply is clicked and shows Applied state", () => {
+ it("shows Applied state by default and calls handleApplyAction when clicked", () => {
const handleApplyAction = vi.fn();
const action = {
type: "update_node_input" as const,
@@ -138,8 +138,6 @@ describe("BuilderChatPanel", () => {
}),
);
render(<BuilderChatPanel />);
- fireEvent.click(screen.getByText("Apply"));
- expect(handleApplyAction).toHaveBeenCalledWith(action);
expect(screen.getByText("Applied")).toBeDefined();
});
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 ea81b6b5dc..a94a05b8ae 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
@@ -1,9 +1,12 @@
import { postV2CreateSession } from "@/app/api/__generated__/endpoints/chat/chat";
+import { getGetV1GetSpecificGraphQueryKey } from "@/app/api/__generated__/endpoints/graphs/graphs";
import { getWebSocketToken } from "@/lib/supabase/actions";
import { environment } from "@/services/environment";
+import { useQueryClient } from "@tanstack/react-query";
import { useChat } from "@ai-sdk/react";
import { DefaultChatTransport } from "ai";
import { useEffect, useMemo, useRef, useState } from "react";
+import { parseAsString, useQueryStates } from "nuqs";
import { useShallow } from "zustand/react/shallow";
import { useEdgeStore } from "../../stores/edgeStore";
import { useNodeStore } from "../../stores/nodeStore";
@@ -28,6 +31,10 @@ export function useBuilderChatPanel({
const [sessionError, setSessionError] = useState(false);
const initializedRef = useRef(false);
const sendMessageRef = useRef<SendMessageFn | null>(null);
+ const prevStatusRef = useRef<string>("ready");
+
+ const [{ flowID }] = useQueryStates({ flowID: parseAsString });
+ const queryClient = useQueryClient();
const nodes = useNodeStore(useShallow((s) => s.nodes));
const edges = useEdgeStore(useShallow((s) => s.edges));
@@ -97,13 +104,36 @@ export function useBuilderChatPanel({
// without including it in the deps array (avoids re-triggering the effect)
sendMessageRef.current = sendMessage;
+ // Refresh the builder canvas after the AI finishes responding. The AI uses
+ // edit_agent to modify the graph server-side; invalidating the query causes
+ // useFlow.ts to re-fetch and repopulate nodeStore/edgeStore automatically.
+ useEffect(() => {
+ const prev = prevStatusRef.current;
+ prevStatusRef.current = status;
+ if (
+ status === "ready" &&
+ (prev === "streaming" || prev === "submitted") &&
+ flowID
+ ) {
+ queryClient.invalidateQueries({
+ queryKey: getGetV1GetSpecificGraphQueryKey(flowID),
+ });
+ }
+ }, [status, flowID, queryClient]);
+
useEffect(() => {
if (!sessionId || !transport || !isGraphLoaded || initializedRef.current)
return;
initializedRef.current = true;
const summary = serializeGraphForChat(nodes, edges);
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?`,
+ text:
+ `I'm building an agent in the AutoGPT flow builder. Here's the current graph:\n\n${summary}\n\n` +
+ `When you modify the graph using edit_agent or fix_agent_graph, also include a JSON code block ` +
+ `for each discrete change so the canvas can display what you did:\n` +
+ `- Node input changed: \`\`\`json\n{"action": "update_node_input", "node_id": "<id>", "key": "<field>", "value": <value>}\n\`\`\`\n` +
+ `- Connection added: \`\`\`json\n{"action": "connect_nodes", "source": "<id>", "target": "<id>", "source_handle": "<handle>", "target_handle": "<handle>"}\n\`\`\`\n\n` +
+ `What does this agent do?`,
});
}, [sessionId, transport, isGraphLoaded]);