import { HelpTooltip } from "@/components/HelpTooltip"; import { Checkbox } from "@/components/ui/checkbox"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { Cross2Icon, MagicWandIcon, PaperPlaneIcon, ReloadIcon, } from "@radix-ui/react-icons"; import { useMutation } from "@tanstack/react-query"; import { useCredentialGetter } from "@/hooks/useCredentialGetter"; import { getClient } from "@/api/AxiosClient"; import { CodeEditor } from "@/routes/workflows/components/CodeEditor"; import { helpTooltips } from "@/routes/workflows/editor/helpContent"; import { useCallback, useMemo, useState } from "react"; import { AutoResizingTextarea } from "../AutoResizingTextarea/AutoResizingTextarea"; import { Button } from "../ui/button"; import { AxiosError } from "axios"; import { toast } from "../ui/use-toast"; import { TSON } from "@/util/tson"; import { cn } from "@/util/utils"; type Props = { value: string; onChange: (value: string) => void; suggestionContext: Record; exampleValue: Record; }; function WorkflowDataSchemaInputGroup({ value, onChange, suggestionContext, exampleValue, }: Props) { const credentialGetter = useCredentialGetter(); const [generateWithAIActive, setGenerateWithAIActive] = useState(false); const [generateWithAIPrompt, setGenerateWithAIPrompt] = useState(""); const [pendingSchema, setPendingSchema] = useState(null); const resetAIState = useCallback(() => { setPendingSchema(null); setGenerateWithAIActive(false); setGenerateWithAIPrompt(""); }, []); const tsonResult = useMemo(() => { if (value === "null") return null; return TSON.parse(value); }, [value]); const getDataSchemaSuggestionMutation = useMutation({ mutationFn: async () => { const client = await getClient(credentialGetter); return client.post<{ output: Record }>( "/suggest/data_schema", { input: generateWithAIPrompt, context: suggestionContext, }, ); }, onSuccess: (response) => { if (value === "null") { return; } setPendingSchema(JSON.stringify(response.data.output, null, 2)); }, onError: (error: AxiosError) => { toast({ variant: "destructive", title: "Could not generate the data schema", description: error.message ?? "There was an error generating data schema", }); }, }); return (
{ if (!checked) { resetAIState(); } onChange( checked ? JSON.stringify(exampleValue, null, 2) : "null", ); }} />
{value !== "null" && !generateWithAIActive && ( )}
{value !== "null" && (
{generateWithAIActive ? (
{ getDataSchemaSuggestionMutation.reset(); resetAIState(); }} /> { setGenerateWithAIPrompt(event.target.value); }} placeholder="Describe how you want your output formatted" /> {getDataSchemaSuggestionMutation.isPending ? ( ) : ( { if ( pendingSchema !== null || !generateWithAIPrompt.trim() ) { return; } getDataSchemaSuggestionMutation.mutate(); }} /> )}
) : null}
{tsonResult !== null && !tsonResult.success && tsonResult.error && (
{tsonResult.error}
)}
)} {value !== "null" && ( { if (!open) { getDataSchemaSuggestionMutation.reset(); resetAIState(); } }} > Review AI-Generated Schema Review the AI-generated schema before applying it. This will replace your current data schema.
)}
); } export { WorkflowDataSchemaInputGroup };