Skip to content

Commit

Permalink
feat(auto-gen-form): adapt special model trigger data format (#1206)
Browse files Browse the repository at this point in the history
Because

- adapt special model trigger data format

This commit

- adapt special model trigger data format
  • Loading branch information
EiffelFly committed Jun 5, 2024
1 parent 5f64c2a commit 5183d22
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import * as React from "react";
import { Form, ScrollArea } from "@instill-ai/design-system";
import { AutoFormFieldBaseProps, fillArrayWithZeros } from "../../..";
import {
AutoFormFieldBaseProps,
GeneralRecord,
fillArrayWithZeros,
} from "../../..";
import { readFileToBinary } from "../../../../view";
import { UploadFileInput } from "../trigger-request-form-fields/UploadFileInput";
import { FileListItem } from "../trigger-request-form-fields/FileListItem";
Expand All @@ -19,8 +23,10 @@ export const ImagesField = ({
isRequired,
size,
shortDescription,
instillModelPromptImageBase64ObjectFormat,
}: AutoFormFieldBaseProps & {
shortDescription?: string;
instillModelPromptImageBase64ObjectFormat?: boolean;
}) => {
const [imageFiles, setImageFiles] = React.useState<File[]>([]);
const inputRef = React.useRef<HTMLInputElement>(null);
Expand Down Expand Up @@ -78,13 +84,27 @@ export const ImagesField = ({
onChange={async (e) => {
if (e.target.files && e.target.files.length > 0) {
const files: File[] = [];
const binaries: string[] = [];
for (const file of e.target.files) {
const binary = await readFileToBinary(file);
files.push(file);
binaries.push(binary);

if (instillModelPromptImageBase64ObjectFormat) {
const binaries: GeneralRecord[] = [];
for (const file of e.target.files) {
const binary = await readFileToBinary(file);
files.push(file);
binaries.push({
prompt_image_base64: binary,
});
}
field.onChange(binaries);
} else {
const binaries: string[] = [];
for (const file of e.target.files) {
const binary = await readFileToBinary(file);
files.push(file);
binaries.push(binary);
}
field.onChange(binaries);
}
field.onChange(binaries);

setImageFiles((prev) => [...prev, ...files]);
}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ export function pickRegularFieldsFromInstillFormTree(
size={size}
isHidden={tree.isHidden}
isRequired={tree.isRequired}
instillModelPromptImageBase64ObjectFormat={
tree.instillModelPromptImageBase64ObjectFormat
}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ const baseFields: Array<keyof InstillJSONSchema> = [
"patternProperties",
"instillCredentialMap",
"instillCredential",
"instillModelPromptImageBase64ObjectFormat",
];

function pickBaseFields(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,24 @@ export function transformInstillJSONSchemaToZod({
targetSchema.instillAcceptFormats.length > 0
) {
if (targetSchema.instillAcceptFormats[0].includes("array:image")) {
instillZodSchema = instillZodSchema = z
.array(z.string().nullable().optional())
.nullable()
.optional();
// model trigger have a special format for images field. We need to
// adapt it.

if (targetSchema.instillModelPromptImageBase64ObjectFormat) {
instillZodSchema = instillZodSchema = z
.array(
z.object({
prompt_image_base64: z.string(),
})
)
.nullable()
.optional();
} else {
instillZodSchema = instillZodSchema = z
.array(z.string().nullable().optional())
.nullable()
.optional();
}

if (isHidden) {
instillZodSchema = z.any();
Expand Down
1 change: 1 addition & 0 deletions packages/toolkit/src/lib/use-instill-form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type InstillCustomProps = {
instillPatternErrorMessage?: string;
instillCredentialMap?: InstillCredentialMap;
instillCredential?: boolean;
instillModelPromptImageBase64ObjectFormat?: boolean;
};

export type InstillCredentialMap = {
Expand Down

0 comments on commit 5183d22

Please sign in to comment.