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): render array items with a known object shape as a real sub-form (#14057)
Nicholas Tindle0d6928ef238a
diff --git a/autogpt_platform/frontend/src/components/renderers/InputRenderer/__tests__/generate-ui-schema.test.ts b/autogpt_platform/frontend/src/components/renderers/InputRenderer/__tests__/generate-ui-schema.test.tsnew file mode 100644index 0000000000..f28b52627b--- /dev/null+++ b/autogpt_platform/frontend/src/components/renderers/InputRenderer/__tests__/generate-ui-schema.test.ts@@ -0,0 +1,74 @@+import { describe, expect, it } from "vitest";+import { RJSFSchema } from "@rjsf/utils";+import { generateUiSchemaForCustomFields } from "../utils/generate-ui-schema";+import { JSON_TEXT_FIELD_ID } from "../custom/custom-registry";++function arrayOf(items: RJSFSchema): RJSFSchema {+ return {+ type: "object",+ properties: {+ filters: { type: "array", title: "Filters", items },+ },+ };+}++describe("generateUiSchemaForCustomFields — array items", () => {+ it("renders items with named properties as a sub-form, not a JSON textarea", () => {+ // A block input like `filters: list[FilterCondition]`, where the condition+ // has an enum column. Rendering this as JSON forces users to hand-type+ // values from a 40+ member enum.+ const ui = generateUiSchemaForCustomFields(+ arrayOf({+ type: "object",+ title: "FilterCondition",+ properties: {+ column: { type: "string", enum: ["current_title", "first_name"] },+ operator: { type: "string", enum: ["=", "like"] },+ value: { type: "string" },+ },+ }),+ );++ expect((ui.filters as any)?.items?.["ui:field"]).not.toBe(+ JSON_TEXT_FIELD_ID,+ );+ });++ it("still falls back to a JSON textarea for free-form dict items", () => {+ const ui = generateUiSchemaForCustomFields(+ arrayOf({ type: "object", additionalProperties: true }),+ );++ expect((ui.filters as any).items["ui:field"]).toBe(JSON_TEXT_FIELD_ID);+ });++ it("still falls back to a JSON textarea for nested array items", () => {+ const ui = generateUiSchemaForCustomFields(+ arrayOf({ type: "array", items: { type: "string" } }),+ );++ expect((ui.filters as any).items["ui:field"]).toBe(JSON_TEXT_FIELD_ID);+ });++ it("leaves arrays of primitives alone", () => {+ const ui = generateUiSchemaForCustomFields(+ arrayOf({ type: "string", enum: ["markdown", "html"] }),+ );++ expect((ui.filters as any)?.items?.["ui:field"]).toBeUndefined();+ });++ it("keeps routing item-level custom fields ahead of the object check", () => {+ const ui = generateUiSchemaForCustomFields(+ arrayOf({+ type: "object",+ credentials_provider: ["openai"],+ properties: { id: { type: "string" } },+ } as RJSFSchema),+ );++ expect((ui.filters as any).items["ui:field"]).toBe(+ "custom/credential_field",+ );+ });+});diff --git a/autogpt_platform/frontend/src/components/renderers/InputRenderer/utils/generate-ui-schema.ts b/autogpt_platform/frontend/src/components/renderers/InputRenderer/utils/generate-ui-schema.tsindex f7f6bc86c1..c8e0ec82ad 100644--- a/autogpt_platform/frontend/src/components/renderers/InputRenderer/utils/generate-ui-schema.ts+++ b/autogpt_platform/frontend/src/components/renderers/InputRenderer/utils/generate-ui-schema.ts@@ -8,6 +8,21 @@ function isComplexType(schema: RJSFSchema): boolean {return schema.type === "object" || schema.type === "array";}+/**+ * True for object schemas that describe a known shape (a model with named+ * properties), as opposed to a free-form dict. These can be rendered as a real+ * sub-form — each property gets its proper widget, so enums become dropdowns+ * instead of hand-typed JSON.+ */+function hasNamedProperties(schema: RJSFSchema): boolean {+ return (+ schema.type === "object" &&+ !!schema.properties &&+ typeof schema.properties === "object" &&+ Object.keys(schema.properties).length > 0+ );+}+function hasComplexAnyOfOptions(schema: RJSFSchema): boolean {const options = schema.anyOf || schema.oneOf;if (!Array.isArray(options)) return false;@@ -26,6 +41,10 @@ function hasComplexAnyOfOptions(schema: RJSFSchema): boolean {* Nested complex types (arrays/objects inside arrays/objects) are rendered as JsonTextField* to avoid deeply nested form UIs. Users can enter raw JSON for these fields.*+ * Exception: array items that describe a known object shape (named properties) are+ * rendered as a real sub-form, one row per item, so each property keeps its proper+ * widget — an enum property becomes a dropdown rather than hand-typed JSON.+ ** @param schema - The JSON schema* @param existingUiSchema - Existing uiSchema to merge with* @param insideComplexType - Whether we're already inside a complex type (object/array)@@ -114,8 +133,14 @@ export function generateUiSchemaForCustomFields("ui:field": itemsCustomFieldId,},};- } else if (isComplexType(itemsSchema)) {- // Array items that are complex types become JsonTextField+ } else if (+ isComplexType(itemsSchema) &&+ !hasNamedProperties(itemsSchema)+ ) {+ // Array items that are complex types *without* a known shape —+ // free-form dicts, arrays of arrays — stay as JsonTextField.+ // Items that do have named properties fall through to the+ // recursion below so each property keeps its real widget.uiSchema[key] = {...(uiSchema[key] as object),items: {