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(rnd): Fix broken save feature on Agent Builder (#8052)
Zamil Majdyf32244a1123c
diff --git a/rnd/autogpt_builder/src/components/Flow.tsx b/rnd/autogpt_builder/src/components/Flow.tsxindex 92e384eb40..a794cb0164 100644--- a/rnd/autogpt_builder/src/components/Flow.tsx+++ b/rnd/autogpt_builder/src/components/Flow.tsx@@ -588,7 +588,9 @@ const FlowEditor: React.FC<{<SaveControlagentMeta={savedAgent}onSave={(isTemplate) => requestSave(isTemplate ?? false)}+ agentDescription={agentDescription}onDescriptionChange={setAgentDescription}+ agentName={agentName}onNameChange={setAgentName}/></ControlPanel>diff --git a/rnd/autogpt_builder/src/components/edit/control/SaveControl.tsx b/rnd/autogpt_builder/src/components/edit/control/SaveControl.tsxindex ae7aec4f3e..ccaaa2f0be 100644--- a/rnd/autogpt_builder/src/components/edit/control/SaveControl.tsx+++ b/rnd/autogpt_builder/src/components/edit/control/SaveControl.tsx@@ -18,6 +18,8 @@ import {interface SaveControlProps {agentMeta: GraphMeta | null;+ agentName: string;+ agentDescription: string;onSave: (isTemplate: boolean | undefined) => void;onNameChange: (name: string) => void;onDescriptionChange: (description: string) => void;@@ -35,7 +37,9 @@ interface SaveControlProps {export const SaveControl = ({agentMeta,onSave,+ agentName,onNameChange,+ agentDescription,onDescriptionChange,}: SaveControlProps) => {/**@@ -75,7 +79,7 @@ export const SaveControl = ({id="name"placeholder="Enter your agent name"className="col-span-3"- defaultValue={agentMeta?.name || ""}+ value={agentName}onChange={(e) => onNameChange(e.target.value)}/><Label htmlFor="description">Description</Label>@@ -83,9 +87,21 @@ export const SaveControl = ({id="description"placeholder="Your agent description"className="col-span-3"- defaultValue={agentMeta?.description || ""}+ value={agentDescription}onChange={(e) => onDescriptionChange(e.target.value)}/>+ {agentMeta?.version && (+ <>+ <Label htmlFor="version">Version</Label>+ <Input+ id="version"+ placeholder="Version"+ className="col-span-3"+ value={agentMeta?.version || "-"}+ disabled+ />+ </>+ )}</div></CardContent><CardFooter className="flex flex-col items-stretch gap-2">diff --git a/rnd/autogpt_builder/src/hooks/useAgentGraph.ts b/rnd/autogpt_builder/src/hooks/useAgentGraph.tsindex b5c16a70d9..2eb60a1dd6 100644--- a/rnd/autogpt_builder/src/hooks/useAgentGraph.ts+++ b/rnd/autogpt_builder/src/hooks/useAgentGraph.ts@@ -16,6 +16,7 @@ import {import { Connection, MarkerType } from "@xyflow/react";import Ajv from "ajv";import { useCallback, useEffect, useMemo, useRef, useState } from "react";+import { useRouter, useSearchParams, usePathname } from "next/navigation";const ajv = new Ajv({ strict: false, allErrors: true });@@ -24,6 +25,11 @@ export default function useAgentGraph(template?: boolean,passDataToBeads?: boolean,) {+ const [router, searchParams, pathname] = [+ useRouter(),+ useSearchParams(),+ usePathname(),+ ];const [savedAgent, setSavedAgent] = useState<Graph | null>(null);const [agentDescription, setAgentDescription] = useState<string>("");const [agentName, setAgentName] = useState<string>("");@@ -307,7 +313,7 @@ export default function useAgentGraph((template ? api.getTemplate(flowID) : api.getGraph(flowID)).then((graph) => {- console.log("Loading graph");+ console.debug("Loading graph");loadGraph(graph);},);@@ -608,7 +614,7 @@ export default function useAgentGraph(}));return {- id: node.id,+ id: node.data.backend_id,block_id: node.data.block_id,input_default: inputDefault,input_nodes: inputNodes,@@ -637,11 +643,8 @@ export default function useAgentGraph(nodes: formattedNodes,links: links,};-- if (savedAgent && deepEquals(payload, savedAgent)) {- console.debug(- "No need to save: Graph is the same as version on server",- );+ if (savedAgent && deepEquals(payload, savedAgent, true)) {+ console.warn("No need to save: Graph is the same as version on server");// Trigger state changesetSavedAgent(savedAgent);return;@@ -664,6 +667,13 @@ export default function useAgentGraph(: api.createGraph(payload));console.debug("Response from the API:", newSavedAgent);+ // Route the URL to the new flow ID if it's a new agent.+ if (!savedAgent) {+ const path = new URLSearchParams(searchParams);+ path.set("flowID", newSavedAgent.id);+ router.replace(`${pathname}?${path.toString()}`);+ }+// Update the node IDs on the frontendsetSavedAgent(newSavedAgent);setNodes((prev) => {diff --git a/rnd/autogpt_builder/src/lib/utils.ts b/rnd/autogpt_builder/src/lib/utils.tsindex 3e7917c7a1..b9cd4eadd2 100644--- a/rnd/autogpt_builder/src/lib/utils.ts+++ b/rnd/autogpt_builder/src/lib/utils.ts@@ -20,19 +20,23 @@ export function hashString(str: string): number {}/** Derived from https://stackoverflow.com/a/32922084 */-export function deepEquals(x: any, y: any): boolean {+export function deepEquals(x: any, y: any, allowMissingKeys = false): boolean {const ok = Object.keys,tx = typeof x,ty = typeof y;- return (++ const sk = (a: object, b: object) => ok(a).filter((k) => k in b);+ const skipLengthCheck = allowMissingKeys && !Array.isArray(x);++ const res =x &&y &&tx === ty &&(tx === "object"- ? ok(x).length === ok(y).length &&- ok(x).every((key) => deepEquals(x[key], y[key]))- : x === y)- );+ ? (skipLengthCheck || ok(x).length === ok(y).length) &&+ sk(x, y).every((key) => deepEquals(x[key], y[key], allowMissingKeys))+ : x === y);+ return res;}/** Get tailwind text color class from type name */