Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

12.3 KB
Raw Blame
import cronstrue from "cronstrue";
import { CalendarClock, Clock } from "lucide-react";
import PropTypes from "prop-types";
import { useEffect, useState } from "react";
import { Button } from "@/components/ui/shims/antd-button";
import { Form } from "@/components/ui/shims/antd-form";
import { Input, Select } from "@/components/ui/shims/antd-inputs";
import { Space } from "@/components/ui/shims/antd-layout";
import { Modal } from "@/components/ui/shims/antd-overlays";
import { Typography } from "@/components/ui/shims/antd-typography";
import { canEditResource } from "../../../helpers/resourceAccess.js";
import { useAxiosPrivate } from "../../../hooks/useAxiosPrivate.js";
import { useAlertStore } from "../../../store/alert-store";
import { usePromptStudioStore } from "../../../store/prompt-studio-store";
import { useSessionStore } from "../../../store/session-store";
import CronGenerator from "../../cron-generator/CronGenerator.jsx";
import { workflowService } from "../../workflows/workflow/workflow-service.js";
import "./EtlTaskDeploy.css";
import { getBackendErrorDetail } from "../../../helpers/GetStaticData.js";
import { useExceptionHandler } from "../../../hooks/useExceptionHandler.jsx";
import usePostHogEvents from "../../../hooks/usePostHogEvents.js";
import { useInitialFetchCount } from "../../../hooks/usePromptStudioFetchCount";
import { useWorkflowStore } from "../../../store/workflow-store.js";
import { usePromptStudioService } from "../../api/prompt-studio-service";
import { PromptStudioModal } from "../../common/PromptStudioModal";

const defaultFromDetails = {
  pipeline_name: "",
  workflow: "",
  cron_string: "",
};

const EtlTaskDeploy = ({
  open,
  setOpen,
  type,
  title,
  setTableData,
  workflowId,
  isEdit,
  selectedRow = {},
  setDeploymentName,
  onDeploymentCreated,
}) => {
  const [form] = Form.useForm();
  const workflowStore = useWorkflowStore();
  const { updateWorkflow } = workflowStore;
  const { sessionDetails } = useSessionStore();
  const { setAlertDetails } = useAlertStore();
  const axiosPrivate = useAxiosPrivate();
  const workflowApiService = workflowService();
  const handleException = useExceptionHandler();

  const { Option } = Select;
  const [workflowList, setWorkflowList] = useState([]);
  const [formDetails, setFormDetails] = useState(
    isEdit ? { ...selectedRow } : { ...defaultFromDetails },
  );
  const [isLoading, setLoading] = useState(false);
  const [openCronGenerator, setOpenCronGenerator] = useState(false);
  const [backendErrors, setBackendErrors] = useState(null);
  const [summary, setSummary] = useState(null);
  const { posthogDeploymentEventText, setPostHogCustomEvent } =
    usePostHogEvents();

  const { count, isLoadingModal, fetchCount } = usePromptStudioStore();
  const [showModal, setShowModal] = useState(false);
  const [modalDismissed, setModalDismissed] = useState(false);
  const { getPromptStudioCount } = usePromptStudioService();
  const initialFetchComplete = useInitialFetchCount(
    fetchCount,
    getPromptStudioCount,
  );

  useEffect(() => {
    if (workflowId) {
      setFormDetails({ ...formDetails, workflow: workflowId });
    }
  }, [workflowId]);

  useEffect(() => {
    if (formDetails?.cron_string) {
      setSummary(cronstrue.toString(formDetails.cron_string));
    }
  }, [formDetails]);

  const getWorkflowList = () => {
    workflowApiService
      .getWorkflowList()
      .then((workflows) => {
        // Deploying is an owner act, and the serializer refuses the rest.
        // Offering them anyway produces an unexplained rejection on save.
        setWorkflowList(
          workflows?.filter((wf) => canEditResource(wf, sessionDetails)) || [],
        );
      })
      .catch(() => {
        console.error("Unable to get workflow list");
      });
  };

  const handleInputChange = (changedValues, allValues) => {
    setFormDetails({ ...formDetails, ...allValues });
    const changedFieldName = Object.keys(changedValues)[0];
    form.setFields([
      {
        name: changedFieldName,
        errors: [],
      },
    ]);
    setBackendErrors((prevErrors) => {
      if (prevErrors) {
        const updatedErrors = prevErrors.errors.filter(
          (error) => error.attr !== changedFieldName,
        );
        return { ...prevErrors, errors: updatedErrors };
      }
      return null;
    });
  };
  const fetchWorkflows = (type) =>
    workflowApiService
      .getWorkflowEndpointList("DESTINATION", type)
      .then((res) =>
        res?.data.map((record) => ({
          ...record,
          id: record.workflow,
        })),
      )
      .catch(() => {
        return [];
      });
  const getWorkflows = () => {
    const connectorType = type === "task" ? "FILESYSTEM" : "DATABASE";
    setWorkflowList([]);
    fetchWorkflows(connectorType).then((data) => {
      if (connectorType === "DATABASE") {
        fetchWorkflows("MANUALREVIEW").then((manualReviewData) => {
          const combinedData = [...data, ...manualReviewData];
          setWorkflowList(combinedData);
        });
      } else {
        setWorkflowList(data);
      }
    });
  };

  useEffect(() => {
    // Only fetch workflow list when workflowId is not provided
    // If workflowId exists, the workflow dropdown is hidden and list fetching is unnecessary
    if (!workflowId) {
      if (type === "app") {
        getWorkflowList();
      } else {
        getWorkflows();
      }
    }
    fetchCount();
  }, [type, fetchCount, workflowId]);

  const clearFormDetails = () => {
    setFormDetails({ ...defaultFromDetails });
  };

  const showCronGenerator = () => {
    setOpenCronGenerator(true);
  };

  const setCronValue = (value) => {
    const updatedValues = { ["cron_string"]: value };
    setFormDetails({ ...formDetails, ...updatedValues });
  };

  const handleCancel = () => {
    setOpen(false);
  };

  const addPipeline = (pipeline) => {
    setTableData((prev) => {
      const prevData = [...prev];
      prevData.push(pipeline);
      return prevData;
    });
  };

  const updatePipelineTable = (pipeline) => {
    setTableData((prev) => {
      const index = prev.findIndex((item) => item?.id === pipeline?.id);
      if (index !== -1) {
        const newData = [...prev];
        newData[index] = { ...newData[index], ...pipeline };
        return newData;
      }
      return prev;
    });
  };

  const updatePipeline = () => {
    const body = formDetails;
    body["pipeline_type"] = type.toUpperCase();

    const requestOptions = {
      method: "PUT",
      url: `/api/v1/unstract/${sessionDetails?.orgId}/pipeline/${body?.id}/`,
      headers: {
        "Content-Type": "application/json",
        "X-CSRFToken": sessionDetails?.csrfToken,
      },
      data: body,
    };
    setLoading(true);
    axiosPrivate(requestOptions)
      .then((res) => {
        updatePipelineTable(res?.data);
        setOpen(false);
        clearFormDetails();
      })
      .catch((err) => {
        setAlertDetails(handleException(err, "", setBackendErrors));
      })
      .finally(() => {
        setLoading(false);
      });
  };

  const createPipeline = () => {
    const wf = workflowList.find((item) => item?.id === formDetails?.workflow);
    setPostHogCustomEvent(posthogDeploymentEventText[`${type}_success`], {
      info: "Clicked on 'Save and Deploy' button",
      deployment_name: formDetails?.pipeline_name,
      workflow_name: wf?.workflow_name,
    });

    const body = formDetails;
    body["pipeline_type"] = type.toUpperCase();

    const requestOptions = {
      method: "POST",
      url: `/api/v1/unstract/${sessionDetails?.orgId}/pipeline/`,
      headers: {
        "Content-Type": "application/json",
        "X-CSRFToken": sessionDetails?.csrfToken,
      },
      data: body,
    };

    setLoading(true);
    axiosPrivate(requestOptions)
      .then((res) => {
        if (workflowId) {
          // Update - can update workflow endpoint status in store
          updateWorkflow({ allowChangeEndpoint: false });
          setDeploymentName(body.pipeline_name);

          // Call the callback to refresh deployment info
          if (onDeploymentCreated) {
            onDeploymentCreated();
          }
        } else {
          addPipeline(res?.data);
        }
        setOpen(false);
        clearFormDetails();
        setAlertDetails({
          type: "success",
          content: "New Pipeline Created Successfully",
        });
      })
      .catch((err) => {
        setAlertDetails(handleException(err, "", setBackendErrors));
      })
      .finally(() => {
        setLoading(false);
      });
  };

  useEffect(() => {
    if (
      initialFetchComplete &&
      !isLoadingModal &&
      count === 0 &&
      !modalDismissed
    ) {
      setShowModal(true);
    }
  }, [initialFetchComplete, isLoadingModal, count, modalDismissed]);

  const handleModalClose = () => {
    setShowModal(false);
    setModalDismissed(true);
  };

  return (
    <>
      {showModal && <PromptStudioModal onClose={handleModalClose} />}
      <Modal
        title={isEdit ? `Update ${title}` : `Add ${title}`}
        centered
        open={open}
        onOk={isEdit ? updatePipeline : createPipeline}
        onCancel={handleCancel}
        okText={isEdit ? "Update and Deploy" : "Save and Deploy"}
        okButtonProps={{
          loading: isLoading,
        }}
        width={400}
        closable={true}
        maskClosable={false}
      >
        <Form
          form={form}
          name="myForm"
          layout="vertical"
          initialValues={formDetails}
          onValuesChange={handleInputChange}
        >
          <Form.Item
            label="Display Name"
            name="pipeline_name"
            rules={[{ required: true, message: "Please enter display name" }]}
            validateStatus={
              getBackendErrorDetail("pipeline_name", backendErrors)
                ? "error"
                : ""
            }
            help={getBackendErrorDetail("pipeline_name", backendErrors)}
          >
            <Input placeholder="Name" />
          </Form.Item>

          {!workflowId && (
            <Form.Item
              label="Workflow"
              name="workflow"
              rules={[{ required: true, message: "Please select an workflow" }]}
              validateStatus={
                getBackendErrorDetail("workflow", backendErrors) ? "error" : ""
              }
              help={getBackendErrorDetail("workflow", backendErrors)}
            >
              <Select showSearch>
                {workflowList.map((workflow) => {
                  return (
                    <Option value={workflow.id} key={workflow.workflow_name}>
                      {workflow.workflow_name}
                    </Option>
                  );
                })}
              </Select>
            </Form.Item>
          )}
          <Form.Item
            label="Cron Schedule"
            name="cron_string"
            validateStatus={
              getBackendErrorDetail("cron_string", backendErrors) ? "error" : ""
            }
            help={getBackendErrorDetail("cron_string", backendErrors)}
          >
            <div className="cron-string-div">
              <Input
                readOnly={true}
                value={formDetails?.cron_string}
                className="cron-string-input"
              />
              <Button
                type="primary"
                onClick={showCronGenerator}
                icon={<CalendarClock />}
                className="cron-string-btn"
              />
            </div>
          </Form.Item>
          <Space>
            <div className="cron-summary-div">
              <Clock />
            </div>
            <div>
              <Typography.Text className="summary-text">
                {summary || "Summary not available."}
              </Typography.Text>
            </div>
          </Space>
        </Form>
      </Modal>
      {openCronGenerator && (
        <CronGenerator
          open={openCronGenerator}
          showCronGenerator={setOpenCronGenerator}
          setCronValue={setCronValue}
        />
      )}
    </>
  );
};

EtlTaskDeploy.propTypes = {
  open: PropTypes.bool.isRequired,
  setOpen: PropTypes.func.isRequired,
  type: PropTypes.string.isRequired,
  title: PropTypes.string.isRequired,
  setTableData: PropTypes.func,
  workflowId: PropTypes.string,
  isEdit: PropTypes.bool,
  selectedRow: PropTypes.object,
  setDeploymentName: PropTypes.func,
  onDeploymentCreated: PropTypes.func,
};

export { EtlTaskDeploy };