From 7a238f1f4d098d39ca657e551943205eaee58708 Mon Sep 17 00:00:00 2001 From: SwiftyOS Date: Fri, 27 Sep 2024 16:26:28 +0200 Subject: [PATCH] formatting --- .../src/components/node-input-components.tsx | 896 +++++++++--------- 1 file changed, 448 insertions(+), 448 deletions(-) diff --git a/autogpt_platform/frontend/src/components/node-input-components.tsx b/autogpt_platform/frontend/src/components/node-input-components.tsx index 6da7c1f382..2b4089a3c7 100644 --- a/autogpt_platform/frontend/src/components/node-input-components.tsx +++ b/autogpt_platform/frontend/src/components/node-input-components.tsx @@ -105,46 +105,160 @@ export const NodeGenericInputField: FC<{ className, displayName, }) => { - displayName ||= propSchema.title || beautifyString(propKey); + displayName ||= propSchema.title || beautifyString(propKey); - if ("allOf" in propSchema) { - // If this happens, that is because Pydantic wraps $refs in an allOf if the - // $ref has sibling schema properties (which isn't technically allowed), - // so there will only be one item in allOf[]. - // AFAIK this should NEVER happen though, as $refs are resolved server-side. - propSchema = propSchema.allOf[0]; - console.warn(`Unsupported 'allOf' in schema for '${propKey}'!`, propSchema); - } + if ("allOf" in propSchema) { + // If this happens, that is because Pydantic wraps $refs in an allOf if the + // $ref has sibling schema properties (which isn't technically allowed), + // so there will only be one item in allOf[]. + // AFAIK this should NEVER happen though, as $refs are resolved server-side. + propSchema = propSchema.allOf[0]; + console.warn(`Unsupported 'allOf' in schema for '${propKey}'!`, propSchema); + } - if ("credentials_provider" in propSchema) { + if ("credentials_provider" in propSchema) { + return ( + + ); + } + + if ("properties" in propSchema) { + return ( + + ); + } + + if ("additionalProperties" in propSchema) { + return ( + + ); + } + + if ("anyOf" in propSchema) { + // optional items + const types = propSchema.anyOf.map((s) => + "type" in s ? s.type : undefined, + ); + if (types.includes("string") && types.includes("null")) { + // optional string return ( - ); } + } - if ("properties" in propSchema) { + if ("oneOf" in propSchema) { + // At the time of writing, this isn't used in the backend -> no impl. needed + console.error( + `Unsupported 'oneOf' in schema for '${propKey}'!`, + propSchema, + ); + return null; + } + + if (!("type" in propSchema)) { + return ( + + ); + } + + switch (propSchema.type) { + case "string": return ( - + ); + case "boolean": + return ( + ); - } - - if ("additionalProperties" in propSchema) { + case "number": + case "integer": + return ( + + ); + case "array": + return ( + + ); + case "object": return ( ); - } - - if ("anyOf" in propSchema) { - // optional items - const types = propSchema.anyOf.map((s) => - "type" in s ? s.type : undefined, - ); - if (types.includes("string") && types.includes("null")) { - // optional string - return ( - - ); - } - } - - if ("oneOf" in propSchema) { - // At the time of writing, this isn't used in the backend -> no impl. needed - console.error( - `Unsupported 'oneOf' in schema for '${propKey}'!`, + default: + console.warn( + `Schema for '${propKey}' specifies unknown type:`, propSchema, ); - return null; - } - - if (!("type" in propSchema)) { return ( ); - } - - switch (propSchema.type) { - case "string": - return ( - - ); - case "boolean": - return ( - - ); - case "number": - case "integer": - return ( - - ); - case "array": - return ( - - ); - case "object": - return ( - - ); - default: - console.warn( - `Schema for '${propKey}' specifies unknown type:`, - propSchema, - ); - return ( - - ); - } - }; + } +}; const NodeCredentialsInput: FC<{ selfKey: string; @@ -332,134 +332,134 @@ const NodeKeyValueInput: FC<{ className, displayName, }) => { - const getPairValues = useCallback(() => { - let defaultEntries = new Map(); + const getPairValues = useCallback(() => { + let defaultEntries = new Map(); - connections - .filter((c) => c.targetHandle.startsWith(`${selfKey}_`)) - .forEach((c) => { - const key = c.targetHandle.slice(`${selfKey}_#_`.length); - defaultEntries.set(key, ""); - }); - - Object.entries(entries ?? schema.default ?? {}).forEach(([key, value]) => { - defaultEntries.set(key, value); + connections + .filter((c) => c.targetHandle.startsWith(`${selfKey}_`)) + .forEach((c) => { + const key = c.targetHandle.slice(`${selfKey}_#_`.length); + defaultEntries.set(key, ""); }); - return Array.from(defaultEntries, ([key, value]) => ({ key, value })); - }, [connections, entries, schema.default, selfKey]); + Object.entries(entries ?? schema.default ?? {}).forEach(([key, value]) => { + defaultEntries.set(key, value); + }); - const [keyValuePairs, setKeyValuePairs] = useState< - { key: string; value: string | number | null }[] - >([]); + return Array.from(defaultEntries, ([key, value]) => ({ key, value })); + }, [connections, entries, schema.default, selfKey]); - useEffect( - () => setKeyValuePairs(getPairValues()), - [connections, entries, schema.default, getPairValues], + const [keyValuePairs, setKeyValuePairs] = useState< + { key: string; value: string | number | null }[] + >([]); + + useEffect( + () => setKeyValuePairs(getPairValues()), + [connections, entries, schema.default, getPairValues], + ); + + function updateKeyValuePairs(newPairs: typeof keyValuePairs) { + setKeyValuePairs(newPairs); + handleInputChange( + selfKey, + newPairs.reduce((obj, { key, value }) => ({ ...obj, [key]: value }), {}), ); + } - function updateKeyValuePairs(newPairs: typeof keyValuePairs) { - setKeyValuePairs(newPairs); - handleInputChange( - selfKey, - newPairs.reduce((obj, { key, value }) => ({ ...obj, [key]: value }), {}), - ); - } + function convertValueType(value: string): string | number | null { + if ( + !schema.additionalProperties || + schema.additionalProperties.type == "string" + ) + return value; + if (!value) return null; + return Number(value); + } - function convertValueType(value: string): string | number | null { - if ( - !schema.additionalProperties || - schema.additionalProperties.type == "string" - ) - return value; - if (!value) return null; - return Number(value); - } + function getEntryKey(key: string): string { + return `${selfKey}_#_${key}`; + } + function isConnected(key: string): boolean { + return connections.some((c) => c.targetHandle === getEntryKey(key)); + } - function getEntryKey(key: string): string { - return `${selfKey}_#_${key}`; - } - function isConnected(key: string): boolean { - return connections.some((c) => c.targetHandle === getEntryKey(key)); - } - - return ( -
0 ? "flex flex-col" : "")} - > -
- {keyValuePairs.map(({ key, value }, index) => ( -
- {key && ( - 0 ? "flex flex-col" : "")} + > +
+ {keyValuePairs.map(({ key, value }, index) => ( +
+ {key && ( + + )} + {!isConnected(key) && ( +
+ + updateKeyValuePairs( + keyValuePairs.toSpliced(index, 1, { + key: e.target.value, + value: value, + }), + ) + } /> - )} - {!isConnected(key) && ( -
- - updateKeyValuePairs( - keyValuePairs.toSpliced(index, 1, { - key: e.target.value, - value: value, - }), - ) - } - /> - - updateKeyValuePairs( - keyValuePairs.toSpliced(index, 1, { - key: key, - value: convertValueType(e.target.value), - }), - ) - } - /> - -
- )} - {errors[`${selfKey}.${key}`] && ( - - {errors[`${selfKey}.${key}`]} - - )} -
- ))} - -
- {errors[selfKey] && ( - {errors[selfKey]} - )} + + updateKeyValuePairs( + keyValuePairs.toSpliced(index, 1, { + key: key, + value: convertValueType(e.target.value), + }), + ) + } + /> + +
+ )} + {errors[`${selfKey}.${key}`] && ( + + {errors[`${selfKey}.${key}`]} + + )} +
+ ))} +
- ); - }; + {errors[selfKey] && ( + {errors[selfKey]} + )} +
+ ); +}; const NodeArrayInput: FC<{ selfKey: string; @@ -482,79 +482,79 @@ const NodeArrayInput: FC<{ className, displayName, }) => { - entries ??= schema.default ?? []; - const isItemObject = "items" in schema && "properties" in schema.items!; - const error = - typeof errors[selfKey] === "string" ? errors[selfKey] : undefined; - return ( -
- {displayName && {displayName}} - {entries.map((entry: any, index: number) => { - const entryKey = `${selfKey}_$_${index}`; - const isConnected = - connections && connections.some((c) => c.targetHandle === entryKey); - return ( -
-
- - {!isConnected && - (schema.items ? ( - - ) : ( - - ))} - {!isConnected && ( - - )} -
- {errors[entryKey] && typeof errors[entryKey] === "string" && ( - {errors[entryKey]} + entries ??= schema.default ?? []; + const isItemObject = "items" in schema && "properties" in schema.items!; + const error = + typeof errors[selfKey] === "string" ? errors[selfKey] : undefined; + return ( +
+ {displayName && {displayName}} + {entries.map((entry: any, index: number) => { + const entryKey = `${selfKey}_$_${index}`; + const isConnected = + connections && connections.some((c) => c.targetHandle === entryKey); + return ( +
+
+ + {!isConnected && + (schema.items ? ( + + ) : ( + + ))} + {!isConnected && ( + )}
- ); - })} - - {error && {error}} -
- ); - }; + {errors[entryKey] && typeof errors[entryKey] === "string" && ( + {errors[entryKey]} + )} +
+ ); + })} + + {error && {error}} +
+ ); +}; const NodeStringInput: FC<{ selfKey: string; @@ -575,56 +575,56 @@ const NodeStringInput: FC<{ className, displayName, }) => { - value ||= schema.default || ""; - return ( -
- {schema.enum ? ( - handleInputChange(selfKey, newValue)} + > + + + + + {schema.enum.map((option, index) => ( + + {beautifyString(option)} + + ))} + + + ) : ( +
handleInputClick(selfKey) : undefined} + > + handleInputChange(selfKey, e.target.value)} + className="rounded-xl pr-8 read-only:cursor-pointer read-only:text-gray-500" + /> + -
- )} - {error && {error}} -
- ); - }; + + +
+ )} + {error && {error}} + + ); +}; export const NodeTextBoxInput: FC<{ selfKey: string; @@ -645,33 +645,33 @@ export const NodeTextBoxInput: FC<{ className, displayName, }) => { - value ||= schema.default || ""; - return ( -
-
handleInputClick(selfKey) : undefined} - > -