From a6e44c43e6ff31e1896d1620428f3fb6b09b5466 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira Date: Fri, 7 Jul 2023 12:27:21 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20refactor(constants.tsx):=20add?= =?UTF-8?q?=20support=20for=20tabsState=20parameter=20in=20getPythonApiCod?= =?UTF-8?q?e,=20getCurlCode,=20and=20getPythonCode=20functions=20to=20buil?= =?UTF-8?q?d=20inputs=20dynamically=20=F0=9F=94=A8=20refactor(modals/ApiMo?= =?UTF-8?q?dal/index.tsx):=20pass=20tabsState=20parameter=20to=20getPython?= =?UTF-8?q?ApiCode,=20getCurlCode,=20and=20getPythonCode=20functions=20to?= =?UTF-8?q?=20build=20inputs=20dynamically=20=F0=9F=94=A8=20refactor(utils?= =?UTF-8?q?.ts):=20add=20buildInputs=20function=20to=20build=20inputs=20dy?= =?UTF-8?q?namically=20based=20on=20tabsState?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/constants.tsx | 28 +++++++++++++++++----- src/frontend/src/modals/ApiModal/index.tsx | 8 +++---- src/frontend/src/utils.ts | 11 +++++++++ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/frontend/src/constants.tsx b/src/frontend/src/constants.tsx index 65a164110..19a417cc0 100644 --- a/src/frontend/src/constants.tsx +++ b/src/frontend/src/constants.tsx @@ -1,7 +1,8 @@ // src/constants.tsx import { FlowType } from "./types/flow"; -import { buildTweaks } from "./utils"; +import { TabsState } from "./types/tabs"; +import { buildInputs, buildTweaks } from "./utils"; /** * The base text for subtitle of Export Dialog (Toolbar) @@ -70,7 +71,11 @@ export const TEXT_DIALOG_SUBTITLE = "Edit your text."; * @param {string} flowId - The id of the flow * @returns {string} - The python code */ -export const getPythonApiCode = (flow: FlowType, tweak?): string => { +export const getPythonApiCode = ( + flow: FlowType, + tweak?: any[], + tabsState?: TabsState +): string => { const flowId = flow.id; // create a dictionary of node ids and the values is an empty dictionary @@ -78,6 +83,7 @@ export const getPythonApiCode = (flow: FlowType, tweak?): string => { // node.data.id // } const tweaks = buildTweaks(flow); + const inputs = buildInputs(tabsState, flow.id); return `import requests BASE_API_URL = "${window.location.protocol}//${ @@ -103,7 +109,7 @@ def run_flow(message: str, flow_id: str, tweaks: dict = None) -> dict: """ api_url = f"{BASE_API_URL}/{flow_id}" - payload = {"inputs": {"input": message}} + payload = {"inputs": ${inputs}} if tweaks: payload["tweaks"] = tweaks @@ -120,15 +126,20 @@ print(run_flow("Your message", flow_id=FLOW_ID, tweaks=TWEAKS))`; * @param {string} flowId - The id of the flow * @returns {string} - The curl code */ -export const getCurlCode = (flow: FlowType, tweak?): string => { +export const getCurlCode = ( + flow: FlowType, + tweak?: any[], + tabsState?: TabsState +): string => { const flowId = flow.id; const tweaks = buildTweaks(flow); + const inputs = buildInputs(tabsState, flow.id); return `curl -X POST \\ ${window.location.protocol}//${ window.location.host }/api/v1/process/${flowId} \\ -H 'Content-Type: application/json' \\ - -d '{"inputs": {"input": message}, "tweaks": ${ + -d '{"inputs": ${inputs}, "tweaks": ${ tweak && tweak.length > 0 ? buildTweakObject(tweak) : JSON.stringify(tweaks, null, 2) @@ -139,9 +150,14 @@ export const getCurlCode = (flow: FlowType, tweak?): string => { * @param {string} flowName - The name of the flow * @returns {string} - The python code */ -export const getPythonCode = (flow: FlowType, tweak?): string => { +export const getPythonCode = ( + flow: FlowType, + tweak?: any[], + tabsState?: TabsState +): string => { const flowName = flow.name; const tweaks = buildTweaks(flow); + const inputs = buildInputs(tabsState, flow.id); return `from langflow import load_flow_from_json TWEAKS = ${ tweak && tweak.length > 0 diff --git a/src/frontend/src/modals/ApiModal/index.tsx b/src/frontend/src/modals/ApiModal/index.tsx index 83da2e796..99fd03941 100644 --- a/src/frontend/src/modals/ApiModal/index.tsx +++ b/src/frontend/src/modals/ApiModal/index.tsx @@ -61,7 +61,7 @@ export default function ApiModal({ flow }: { flow: FlowType }) { const [openAccordion, setOpenAccordion] = useState([]); const tweak = useRef([]); const tweaksList = useRef([]); - const { setTweak, getTweak } = useContext(TabsContext); + const { setTweak, getTweak, tabsState } = useContext(TabsContext); const copyToClipboard = () => { if (!navigator.clipboard || !navigator.clipboard.writeText) { return; @@ -75,9 +75,9 @@ export default function ApiModal({ flow }: { flow: FlowType }) { }, 2000); }); }; - const pythonApiCode = getPythonApiCode(flow, tweak.current); - const curl_code = getCurlCode(flow, tweak.current); - const pythonCode = getPythonCode(flow, tweak.current); + const pythonApiCode = getPythonApiCode(flow, tweak.current, tabsState); + const curl_code = getCurlCode(flow, tweak.current, tabsState); + const pythonCode = getPythonCode(flow, tweak.current, tabsState); const tweaksCode = buildTweaks(flow); const tabs = [ { diff --git a/src/frontend/src/utils.ts b/src/frontend/src/utils.ts index 7db5628bf..bbe5a6df4 100644 --- a/src/frontend/src/utils.ts +++ b/src/frontend/src/utils.ts @@ -904,6 +904,17 @@ export function groupByFamily(data, baseClasses, left, type) { } } +export function buildInputs(tabsState, id) { + console.log(tabsState, id); + return tabsState && + tabsState[id] && + tabsState[id].formKeysData && + tabsState[id].formKeysData.input_keys && + Object.keys(tabsState[id].formKeysData.input_keys).length > 0 + ? JSON.stringify(tabsState[id].formKeysData.input_keys) + : '{"input": message}'; +} + export function buildTweaks(flow) { return flow.data.nodes.reduce((acc, node) => { acc[node.data.id] = {};