Skip to content

Commit

Permalink
fix(frontend): Fix client-side validation for Agent Executor Block (#…
Browse files Browse the repository at this point in the history
…8643)

* feat(frontend): Center initial canvas & add option to open graph on agent executor blok

* Removed unused variable
  • Loading branch information
majdyz authored Nov 13, 2024
1 parent a3655b8 commit 6724475
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
18 changes: 17 additions & 1 deletion autogpt_platform/frontend/src/components/CustomNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ import NodeOutputs from "./NodeOutputs";
import { IconCoin } from "./ui/icons";
import * as Separator from "@radix-ui/react-separator";
import * as ContextMenu from "@radix-ui/react-context-menu";
import { DotsVerticalIcon, TrashIcon, CopyIcon } from "@radix-ui/react-icons";
import {
DotsVerticalIcon,
TrashIcon,
CopyIcon,
ExitIcon,
} from "@radix-ui/react-icons";

export type ConnectionData = Array<{
edge_id: string;
Expand Down Expand Up @@ -96,11 +101,13 @@ export function CustomNode({
>();
const isInitialSetup = useRef(true);
const flowContext = useContext(FlowContext);
let nodeFlowId = "";

if (data.uiType === BlockUIType.AGENT) {
// Display the graph's schema instead AgentExecutorBlock's schema.
data.inputSchema = data.hardcodedValues?.input_schema || {};
data.outputSchema = data.hardcodedValues?.output_schema || {};
nodeFlowId = data.hardcodedValues?.graph_id || nodeFlowId;
}

if (!flowContext) {
Expand Down Expand Up @@ -496,6 +503,15 @@ export function CustomNode({
<CopyIcon className="mr-2 h-5 w-5" />
<span>Copy</span>
</ContextMenu.Item>
{nodeFlowId && (
<ContextMenu.Item
onSelect={() => window.open(`/build?flowID=${nodeFlowId}`)}
className="flex cursor-pointer items-center rounded-md px-3 py-2 hover:bg-gray-100"
>
<ExitIcon className="mr-2 h-5 w-5" />
<span>Open agent</span>
</ContextMenu.Item>
)}
<ContextMenu.Separator className="my-1 h-px bg-gray-300" />
<ContextMenu.Item
onSelect={deleteNode}
Expand Down
19 changes: 12 additions & 7 deletions autogpt_platform/frontend/src/hooks/useAgentGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,19 @@ export default function useAgentGraph(
});
}, [updateQueue, nodesSyncedWithSavedAgent, updateNodesWithExecutionData]);

const validateNodes = useCallback((): boolean => {
let isValid = true;
const validateNodes = useCallback((): string | null => {
let errorMessage = null;

nodes.forEach((node) => {
const validate = ajv.compile(node.data.inputSchema);
const errors = {} as { [key: string]: string };

// Validate values against schema using AJV
const valid = validate(node.data.hardcodedValues);
const inputData =
node.data.uiType === BlockUIType.AGENT
? node.data.hardcodedValues?.data || {}
: node.data.hardcodedValues || {};
const valid = validate(inputData);
if (!valid) {
// Populate errors if validation fails
validate.errors?.forEach((error) => {
Expand All @@ -385,7 +389,7 @@ export default function useAgentGraph(
return;
}
console.warn("Error", error);
isValid = false;
errorMessage = error.message || "Invalid input";
if (path && error.message) {
const key = path.slice(1);
console.log("Error", key, error.message);
Expand Down Expand Up @@ -417,7 +421,7 @@ export default function useAgentGraph(
});
});

return isValid;
return errorMessage;
}, [nodes]);

// Handle user requests
Expand Down Expand Up @@ -471,10 +475,11 @@ export default function useAgentGraph(
});
// If run was requested, run the agent
} else if (saveRunRequest.request === "run") {
if (!validateNodes()) {
const validationError = validateNodes();
if (validationError) {
console.error("Validation failed; aborting run");
toast({
title: "Invalid credentials or inputs",
title: `Validation failed: ${validationError}`,
variant: "destructive",
duration: 2000,
});
Expand Down

0 comments on commit 6724475

Please sign in to comment.