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(builder): Fix `BlockSchema` handling & type propagation (#7604)
Reinier van der Leer5d1035aeb0cf
diff --git a/rnd/autogpt_builder/src/components/CustomNode.tsx b/rnd/autogpt_builder/src/components/CustomNode.tsxindex 076a991cba..4124cf5de8 100644--- a/rnd/autogpt_builder/src/components/CustomNode.tsx+++ b/rnd/autogpt_builder/src/components/CustomNode.tsx@@ -4,6 +4,8 @@ import 'reactflow/dist/style.css';import './customnode.css';import InputModalComponent from './InputModalComponent';import OutputModalComponent from './OutputModalComponent';+import { BlockIORootSchema, NodeExecutionResult } from '@/lib/autogpt-server-api/types';+import { beautifyString } from '@/lib/utils';import { BlockSchema } from '@/lib/types';import { beautifyString, setNestedProperty } from '@/lib/utils';import { Switch } from "@/components/ui/switch"@@ -15,8 +17,8 @@ import { history } from './history';export type CustomNodeData = {blockType: string;title: string;- inputSchema: BlockSchema;- outputSchema: BlockSchema;+ inputSchema: BlockIORootSchema;+ outputSchema: BlockIORootSchema;hardcodedValues: { [key: string]: any };setHardcodedValues: (values: { [key: string]: any }) => void;connections: Array<{ source: string; sourceHandle: string; target: string; targetHandle: string }>;@@ -77,7 +79,7 @@ const CustomNode: FC<NodeProps<CustomNodeData>> = ({ data, id }) => {});};- const generateOutputHandles = (schema: BlockSchema) => {+ const generateOutputHandles = (schema: BlockIORootSchema) => {if (!schema?.properties) return null;const keys = Object.keys(schema.properties);return keys.map((key) => (@@ -156,7 +158,9 @@ const CustomNode: FC<NodeProps<CustomNodeData>> = ({ data, id }) => {const handleOutputClick = () => {setIsOutputModalOpen(true);- setModalValue(typeof data.output_data === 'object' ? JSON.stringify(data.output_data, null, 2) : data.output_data);+ setModalValue(+ data.output_data ? JSON.stringify(data.output_data, null, 2) : "[no output (yet)]"+ );};const isTextTruncated = (element: HTMLElement | null): boolean => {diff --git a/rnd/autogpt_builder/src/components/Flow.tsx b/rnd/autogpt_builder/src/components/Flow.tsxindex 2aa0201c54..93a435c012 100644--- a/rnd/autogpt_builder/src/components/Flow.tsx+++ b/rnd/autogpt_builder/src/components/Flow.tsx@@ -15,7 +15,7 @@ import ReactFlow, {import 'reactflow/dist/style.css';import CustomNode, { CustomNodeData } from './CustomNode';import './flow.css';-import AutoGPTServerAPI, { Block, Graph, NodeExecutionResult, ObjectSchema } from '@/lib/autogpt-server-api';+import AutoGPTServerAPI, { Block, BlockIOSchema, Graph, NodeExecutionResult, ObjectSchema } from '@/lib/autogpt-server-api';import { Button } from './ui/button';import { Input } from './ui/input';import { ChevronRight, ChevronLeft } from "lucide-react";@@ -212,8 +212,9 @@ const FlowEditor: React.FC<{const outputSchema = node.data.outputSchema;if (!outputSchema) return 'unknown';- const outputType = outputSchema.properties[handleId].type;- return outputType;+ const outputHandle = outputSchema.properties[handleId];+ if (!("type" in outputHandle)) return "unknown";+ return outputHandle.type;}const getNodePos = (id: string) => {@@ -451,13 +452,18 @@ const FlowEditor: React.FC<{return {};}- const getNestedData = (schema: ObjectSchema, values: { [key: string]: any }): { [key: string]: any } => {+ const getNestedData = (+ schema: BlockIOSchema, values: { [key: string]: any }+ ): { [key: string]: any } => {let inputData: { [key: string]: any } = {};- if (schema.properties) {+ if ("properties" in schema) {Object.keys(schema.properties).forEach((key) => {if (values[key] !== undefined) {- if (schema.properties[key].type === 'object') {+ if (+ "properties" in schema.properties[key]+ || "additionalProperties" in schema.properties[key]+ ) {inputData[key] = getNestedData(schema.properties[key], values[key]);} else {inputData[key] = values[key];@@ -466,7 +472,7 @@ const FlowEditor: React.FC<{});}- if (schema.additionalProperties) {+ if ("additionalProperties" in schema) {inputData = { ...inputData, ...values };}diff --git a/rnd/autogpt_builder/src/components/NodeHandle.tsx b/rnd/autogpt_builder/src/components/NodeHandle.tsxindex 381a76d51f..9eb0c4a704 100644--- a/rnd/autogpt_builder/src/components/NodeHandle.tsx+++ b/rnd/autogpt_builder/src/components/NodeHandle.tsx@@ -1,4 +1,4 @@-import { BlockSchema } from "@/lib/types";+import { BlockIOSchema } from "@/lib/autogpt-server-api/types";import { beautifyString, getTypeBgColor, getTypeTextColor } from "@/lib/utils";import { FC } from "react";import { Handle, Position } from "reactflow";@@ -6,7 +6,7 @@ import SchemaTooltip from "./SchemaTooltip";type HandleProps = {keyName: string,- schema: BlockSchema,+ schema: BlockIOSchema,isConnected: boolean,isRequired?: boolean,side: 'left' | 'right'diff --git a/rnd/autogpt_builder/src/components/NodeInputField.tsx b/rnd/autogpt_builder/src/components/NodeInputField.tsxindex 583d033278..2433eb4696 100644--- a/rnd/autogpt_builder/src/components/NodeInputField.tsx+++ b/rnd/autogpt_builder/src/components/NodeInputField.tsx@@ -1,11 +1,12 @@import { beautifyString } from "@/lib/utils";+import { BlockIOSchema } from "@/lib/autogpt-server-api/types";import { FC, useState } from "react";import { Button } from "./ui/button";import { Input } from "./ui/input";type BlockInputFieldProps = {keyName: string- schema: any+ schema: BlockIOSchemaparentKey?: stringvalue: string | Array<string> | { [key: string]: string }handleInputClick: (key: string) => void@@ -49,11 +50,11 @@ const NodeInputField: FC<BlockInputFieldProps> =)};- if (schema.type === 'object' && schema.properties) {+ if ("properties" in schema) {return (<div key={fullKey} className="object-input"><strong>{displayKey}:</strong>- {Object.entries(schema.properties).map(([propKey, propSchema]: [string, any]) => (+ {Object.entries(schema.properties).map(([propKey, propSchema]) => (<div key={`${fullKey}.${propKey}`} className="nested-input"><NodeInputFieldkeyName={propKey}@@ -70,7 +71,7 @@ const NodeInputField: FC<BlockInputFieldProps> =);}- if (schema.type === 'object' && schema.additionalProperties) {+ if ("additionalProperties" in schema) {const objectValue = value || {};return (<div key={fullKey} className="object-input">@@ -137,8 +138,8 @@ const NodeInputField: FC<BlockInputFieldProps> =);}- if (schema.anyOf) {- const types = schema.anyOf.map((s: any) => s.type);+ if ("anyOf" in schema) {+ const types = schema.anyOf.map(s => "type" in s ? s.type : undefined);if (types.includes('string') && types.includes('null')) {return (<div key={fullKey} className="input-container">@@ -149,11 +150,12 @@ const NodeInputField: FC<BlockInputFieldProps> =}}- if (schema.allOf) {+ if ("allOf" in schema) {return (<div key={fullKey} className="object-input"><strong>{displayKey}:</strong>- {schema.allOf[0].properties && Object.entries(schema.allOf[0].properties).map(([propKey, propSchema]: [string, any]) => (+ {"properties" in schema.allOf[0] &&+ Object.entries(schema.allOf[0].properties).map(([propKey, propSchema]) => (<div key={`${fullKey}.${propKey}`} className="nested-input"><NodeInputFieldkeyName={propKey}@@ -170,11 +172,12 @@ const NodeInputField: FC<BlockInputFieldProps> =);}- if (schema.oneOf) {+ if ("oneOf" in schema) {return (<div key={fullKey} className="object-input"><strong>{displayKey}:</strong>- {schema.oneOf[0].properties && Object.entries(schema.oneOf[0].properties).map(([propKey, propSchema]: [string, any]) => (+ {"properties" in schema.oneOf[0] &&+ Object.entries(schema.oneOf[0].properties).map(([propKey, propSchema]) => (<div key={`${fullKey}.${propKey}`} className="nested-input"><NodeInputFieldkeyName={propKey}@@ -191,6 +194,16 @@ const NodeInputField: FC<BlockInputFieldProps> =);}+ if (!("type" in schema)) {+ console.warn(`Schema for input ${key} does not specify a type:`, schema);+ return (+ <div key={fullKey} className="input-container">+ {renderClickableInput(value as string, schema.placeholder || `Enter ${beautifyString(displayKey)} (Complex)`)}+ {error && <span className="error-message">{error}</span>}+ </div>+ );+ }+switch (schema.type) {case 'string':if (schema.enum) {@@ -284,6 +297,7 @@ const NodeInputField: FC<BlockInputFieldProps> =}return null;default:+ console.warn(`Schema for input ${key} specifies unknown type:`, schema);return (<div key={fullKey} className="input-container">{renderClickableInput(value as string, schema.placeholder || `Enter ${beautifyString(displayKey)} (Complex)`)}diff --git a/rnd/autogpt_builder/src/components/SchemaTooltip.tsx b/rnd/autogpt_builder/src/components/SchemaTooltip.tsxindex 87cd8c225d..52357eaa6f 100644--- a/rnd/autogpt_builder/src/components/SchemaTooltip.tsx+++ b/rnd/autogpt_builder/src/components/SchemaTooltip.tsx@@ -4,11 +4,11 @@ import {TooltipProvider,TooltipTrigger,} from "@/components/ui/tooltip"-import { BlockSchema } from "@/lib/types";+import { BlockIOSchema } from "@/lib/autogpt-server-api/types";import { Info } from 'lucide-react';import ReactMarkdown from 'react-markdown';-const SchemaTooltip: React.FC<{ schema: BlockSchema }> = ({ schema }) => {+const SchemaTooltip: React.FC<{ schema: BlockIOSchema }> = ({ schema }) => {if (!schema.description) return null;return (diff --git a/rnd/autogpt_builder/src/lib/autogpt-server-api/types.ts b/rnd/autogpt_builder/src/lib/autogpt-server-api/types.tsindex 64bbe17dd8..20551536e3 100644--- a/rnd/autogpt_builder/src/lib/autogpt-server-api/types.ts+++ b/rnd/autogpt_builder/src/lib/autogpt-server-api/types.ts@@ -3,15 +3,56 @@ export type Block = {id: string;name: string;description: string;- inputSchema: ObjectSchema;- outputSchema: ObjectSchema;+ inputSchema: BlockIORootSchema;+ outputSchema: BlockIORootSchema;};-export type ObjectSchema = {- type: string;- properties: { [key: string]: any };+export type BlockIORootSchema = {+ type: "object";+ properties: { [key: string]: BlockIOSchema };+ required?: string[];additionalProperties?: { type: string };+}++export type BlockIOSchema = {+ title?: string;+ description?: string;+ placeholder?: string;+} & (BlockIOSimpleTypeSchema | BlockIOCombinedTypeSchema);++type BlockIOSimpleTypeSchema = {+ type: "object";+ properties: { [key: string]: BlockIOSchema };required?: string[];+ additionalProperties?: { type: string };+} | {+ type: "array";+ items?: BlockIOSimpleTypeSchema;+} | {+ type: "string";+ enum?: string[];+ secret?: true;+ default?: string;+} | {+ type: "integer" | "number";+ default?: number;+} | {+ type: "boolean";+ default?: boolean;+} | {+ type: "null";+};++// At the time of writing, combined schemas only occur on the first nested level in a+// block schema. It is typed this way to make the use of these objects less tedious.+type BlockIOCombinedTypeSchema = {+ allOf: [BlockIOSimpleTypeSchema];+} | {+ anyOf: BlockIOSimpleTypeSchema[];+ default?: string | number | boolean | null;+} | {+ oneOf: BlockIOSimpleTypeSchema[];+ default?: string | number | boolean | null;};/* Mirror of autogpt_server/data/graph.py:Node */diff --git a/rnd/autogpt_builder/src/lib/types.ts b/rnd/autogpt_builder/src/lib/types.tsdeleted file mode 100644index 48f5857bbe..0000000000--- a/rnd/autogpt_builder/src/lib/types.ts+++ /dev/null@@ -1,14 +0,0 @@-export type BlockSchema = {- type: string;- properties: { [key: string]: any };- required?: string[];- enum?: string[];- items?: BlockSchema;- additionalProperties?: { type: string };- title?: string;- description?: string;- placeholder?: string;- allOf?: any[];- anyOf?: any[];- oneOf?: any[];-};