From b9be01e63a474885490a97777f04a39b45507ffc Mon Sep 17 00:00:00 2001 From: Tahier Hussain Date: Thu, 30 Jan 2025 12:46:59 +0530 Subject: [PATCH 1/3] Integration of the transform formdata function for LLMW Paid Adapter --- .../input-output/add-source/AddSource.jsx | 46 ++++++++++++++++--- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/frontend/src/components/input-output/add-source/AddSource.jsx b/frontend/src/components/input-output/add-source/AddSource.jsx index ddaff0a34..cc8083700 100644 --- a/frontend/src/components/input-output/add-source/AddSource.jsx +++ b/frontend/src/components/input-output/add-source/AddSource.jsx @@ -14,6 +14,7 @@ let transformLlmWhispererJsonSchema; let LLMW_V2_ID; let PLAN_TYPES; let unstractSubscriptionPlanStore; +let useLlmWhipererAdapterSchema; try { transformLlmWhispererJsonSchema = require("../../../plugins/unstract-subscription/helper/transformLlmWhispererJsonSchema").transformLlmWhispererJsonSchema; @@ -22,6 +23,8 @@ try { PLAN_TYPES = require("../../../plugins/unstract-subscription/helper/constants").PLAN_TYPES; unstractSubscriptionPlanStore = require("../../../plugins/store/unstract-subscription-plan-store"); + useLlmWhipererAdapterSchema = + require("../../../plugins/unstract-subscription/hooks/useLlmWhispererAdapterSchema.js").useLlmWhipererAdapterSchema; } catch (err) { // Ignore if not available } @@ -47,6 +50,15 @@ function AddSource({ const { setAlertDetails } = useAlertStore(); const axiosPrivate = useAxiosPrivate(); const handleException = useExceptionHandler(); + const [isLLMWPaidSchema, setIsLLMWPaidSchema] = useState(false); + + let transformLlmWhispererFormData; + try { + transformLlmWhispererFormData = + useLlmWhipererAdapterSchema()?.transformLlmWhispererFormData; + } catch { + // Ignore if not available + } let planType; if (unstractSubscriptionPlanStore?.useUnstractSubscriptionPlanStore) { @@ -55,6 +67,32 @@ function AddSource({ ); } + useEffect(() => { + setIsLLMWPaidSchema( + LLMW_V2_ID && + transformLlmWhispererJsonSchema && + PLAN_TYPES && + selectedSourceId === LLMW_V2_ID && + planType === PLAN_TYPES?.PAID + ); + }, [ + LLMW_V2_ID, + transformLlmWhispererJsonSchema, + PLAN_TYPES, + selectedSourceId, + planType, + ]); + + useEffect(() => { + if (!isLLMWPaidSchema || !transformLlmWhispererFormData) return; + + const modifiedFormData = transformLlmWhispererFormData(formData); + + if (JSON.stringify(modifiedFormData) !== JSON.stringify(formData)) { + setFormData(modifiedFormData); + } + }, [isLLMWPaidSchema, formData]); + useEffect(() => { if (!selectedSourceId) { setSpec({}); @@ -80,13 +118,7 @@ function AddSource({ const data = res?.data; setFormData(metadata || {}); - if ( - LLMW_V2_ID && - transformLlmWhispererJsonSchema && - PLAN_TYPES && - selectedSourceId === LLMW_V2_ID && - planType === PLAN_TYPES?.PAID - ) { + if (isLLMWPaidSchema) { setSpec(transformLlmWhispererJsonSchema(data?.json_schema || {})); } else { setSpec(data?.json_schema || {}); From 5cd5ddcfefd45026b410ff8dad2798d89761475f Mon Sep 17 00:00:00 2001 From: Tahier Hussain Date: Thu, 30 Jan 2025 13:28:12 +0530 Subject: [PATCH 2/3] Fixed sonar issue --- .../src/components/input-output/add-source/AddSource.jsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/frontend/src/components/input-output/add-source/AddSource.jsx b/frontend/src/components/input-output/add-source/AddSource.jsx index cc8083700..10f047bc7 100644 --- a/frontend/src/components/input-output/add-source/AddSource.jsx +++ b/frontend/src/components/input-output/add-source/AddSource.jsx @@ -14,7 +14,7 @@ let transformLlmWhispererJsonSchema; let LLMW_V2_ID; let PLAN_TYPES; let unstractSubscriptionPlanStore; -let useLlmWhipererAdapterSchema; +let llmWhipererAdapterSchema; try { transformLlmWhispererJsonSchema = require("../../../plugins/unstract-subscription/helper/transformLlmWhispererJsonSchema").transformLlmWhispererJsonSchema; @@ -23,8 +23,7 @@ try { PLAN_TYPES = require("../../../plugins/unstract-subscription/helper/constants").PLAN_TYPES; unstractSubscriptionPlanStore = require("../../../plugins/store/unstract-subscription-plan-store"); - useLlmWhipererAdapterSchema = - require("../../../plugins/unstract-subscription/hooks/useLlmWhispererAdapterSchema.js").useLlmWhipererAdapterSchema; + llmWhipererAdapterSchema = require("../../../plugins/unstract-subscription/hooks/useLlmWhispererAdapterSchema.js"); } catch (err) { // Ignore if not available } @@ -55,7 +54,8 @@ function AddSource({ let transformLlmWhispererFormData; try { transformLlmWhispererFormData = - useLlmWhipererAdapterSchema()?.transformLlmWhispererFormData; + llmWhipererAdapterSchema?.useLlmWhipererAdapterSchema() + ?.transformLlmWhispererFormData; } catch { // Ignore if not available } From 244a9a6e8e14e34b94fa56053660f85ebfb13a5b Mon Sep 17 00:00:00 2001 From: Tahier Hussain Date: Thu, 30 Jan 2025 14:57:32 +0530 Subject: [PATCH 3/3] Code optimization in declaring variable --- .../input-output/add-source/AddSource.jsx | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/frontend/src/components/input-output/add-source/AddSource.jsx b/frontend/src/components/input-output/add-source/AddSource.jsx index 10f047bc7..96fb5d60a 100644 --- a/frontend/src/components/input-output/add-source/AddSource.jsx +++ b/frontend/src/components/input-output/add-source/AddSource.jsx @@ -1,6 +1,6 @@ import { Typography } from "antd"; import PropTypes from "prop-types"; -import { useEffect, useState } from "react"; +import { useEffect, useMemo, useState } from "react"; import { sourceTypes } from "../../../helpers/GetStaticData"; import { useAxiosPrivate } from "../../../hooks/useAxiosPrivate"; @@ -49,7 +49,6 @@ function AddSource({ const { setAlertDetails } = useAlertStore(); const axiosPrivate = useAxiosPrivate(); const handleException = useExceptionHandler(); - const [isLLMWPaidSchema, setIsLLMWPaidSchema] = useState(false); let transformLlmWhispererFormData; try { @@ -67,13 +66,13 @@ function AddSource({ ); } - useEffect(() => { - setIsLLMWPaidSchema( + const isLLMWPaidSchema = useMemo(() => { + return ( LLMW_V2_ID && - transformLlmWhispererJsonSchema && - PLAN_TYPES && - selectedSourceId === LLMW_V2_ID && - planType === PLAN_TYPES?.PAID + transformLlmWhispererJsonSchema && + PLAN_TYPES && + selectedSourceId === LLMW_V2_ID && + planType === PLAN_TYPES?.PAID ); }, [ LLMW_V2_ID,